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