1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.internal.revwalk;
11
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.stream.Stream;
20
21 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
22 import org.eclipse.jgit.errors.MissingObjectException;
23 import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
24 import org.eclipse.jgit.revwalk.BitmapWalker;
25 import org.eclipse.jgit.revwalk.ObjectReachabilityChecker;
26 import org.eclipse.jgit.revwalk.ObjectWalk;
27 import org.eclipse.jgit.revwalk.RevObject;
28
29
30
31
32
33 public class BitmappedObjectReachabilityChecker
34 implements ObjectReachabilityChecker {
35
36 private final ObjectWalk walk;
37
38
39
40
41
42
43
44 public BitmappedObjectReachabilityChecker(ObjectWalk walk) {
45 this.walk = walk;
46 }
47
48
49
50
51
52
53
54
55 @Override
56 public Optional<RevObject> areAllReachable(Collection<RevObject> targets,
57 Stream<RevObject> starters) throws IOException {
58
59 try {
60 List<RevObject> remainingTargets = new ArrayList<>(targets);
61 BitmapWalker bitmapWalker = new BitmapWalker(walk,
62 walk.getObjectReader().getBitmapIndex(), null);
63
64 Iterator<RevObject> starterIt = starters.iterator();
65 BitmapBuilder seen = null;
66 while (starterIt.hasNext()) {
67 List<RevObject> asList = Arrays.asList(starterIt.next());
68 BitmapBuilder visited = bitmapWalker.findObjects(asList, seen,
69 true);
70 seen = seen == null ? visited : seen.or(visited);
71
72 remainingTargets.removeIf(seen::contains);
73 if (remainingTargets.isEmpty()) {
74 return Optional.empty();
75 }
76 }
77
78 return Optional.of(remainingTargets.get(0));
79 } catch (MissingObjectException | IncorrectObjectTypeException e) {
80 throw new IllegalStateException(e);
81 }
82 }
83 }