PedestrianReachabilityChecker.java

  1. /*
  2.  * Copyright (C) 2019, Google LLC. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.internal.revwalk;

  11. import java.io.IOException;
  12. import java.util.Collection;
  13. import java.util.Iterator;
  14. import java.util.Optional;
  15. import java.util.stream.Stream;

  16. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  17. import org.eclipse.jgit.errors.MissingObjectException;
  18. import org.eclipse.jgit.revwalk.ReachabilityChecker;
  19. import org.eclipse.jgit.revwalk.RevCommit;
  20. import org.eclipse.jgit.revwalk.RevSort;
  21. import org.eclipse.jgit.revwalk.RevWalk;

  22. /**
  23.  * Checks the reachability walking the graph from the starters towards the
  24.  * target.
  25.  */
  26. public class PedestrianReachabilityChecker implements ReachabilityChecker {

  27.     private final boolean topoSort;

  28.     private final RevWalk walk;

  29.     /**
  30.      * New instance of the reachability checker using a existing walk.
  31.      *
  32.      * @param topoSort
  33.      *            walk commits in topological order
  34.      * @param walk
  35.      *            RevWalk instance to reuse. Caller retains ownership.
  36.      */
  37.     public PedestrianReachabilityChecker(boolean topoSort,
  38.             RevWalk walk) {
  39.         this.topoSort = topoSort;
  40.         this.walk = walk;
  41.     }

  42.     @Override
  43.     public Optional<RevCommit> areAllReachable(Collection<RevCommit> targets,
  44.             Stream<RevCommit> starters)
  45.                     throws MissingObjectException, IncorrectObjectTypeException,
  46.                     IOException {
  47.         walk.reset();
  48.         if (topoSort) {
  49.             walk.sort(RevSort.TOPO);
  50.         }

  51.         for (RevCommit target: targets) {
  52.             walk.markStart(target);
  53.         }

  54.         Iterator<RevCommit> iterator = starters.iterator();
  55.         while (iterator.hasNext()) {
  56.             walk.markUninteresting(iterator.next());
  57.         }

  58.         return Optional.ofNullable(walk.next());
  59.     }
  60. }