View Javadoc
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.revwalk;
11  
12  import java.io.IOException;
13  import java.util.Collection;
14  import java.util.Iterator;
15  import java.util.Optional;
16  import java.util.stream.Stream;
17  
18  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
19  import org.eclipse.jgit.errors.MissingObjectException;
20  
21  /**
22   * Checks the reachability walking the graph from the starters towards the
23   * target.
24   */
25  class PedestrianReachabilityChecker implements ReachabilityChecker {
26  
27  	private final boolean topoSort;
28  
29  	private final RevWalk walk;
30  
31  	/**
32  	 * New instance of the reachability checker using a existing walk.
33  	 *
34  	 * @param topoSort
35  	 *            walk commits in topological order
36  	 * @param walk
37  	 *            RevWalk instance to reuse. Caller retains ownership.
38  	 */
39  	public PedestrianReachabilityChecker(boolean topoSort,
40  			RevWalk walk) {
41  		this.topoSort = topoSort;
42  		this.walk = walk;
43  	}
44  
45  	@Override
46  	public Optional<RevCommit> areAllReachable(Collection<RevCommit> targets,
47  			Stream<RevCommit> starters)
48  					throws MissingObjectException, IncorrectObjectTypeException,
49  					IOException {
50  		walk.reset();
51  		if (topoSort) {
52  			walk.sort(RevSort.TOPO);
53  		}
54  
55  		for (RevCommit target: targets) {
56  			walk.markStart(target);
57  		}
58  
59  		Iterator<RevCommit> iterator = starters.iterator();
60  		while (iterator.hasNext()) {
61  			walk.markUninteresting(iterator.next());
62  		}
63  
64  		return Optional.ofNullable(walk.next());
65  	}
66  }