1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.revwalk;
12
13 import static java.util.Arrays.asList;
14 import static org.junit.Assert.assertEquals;
15
16 import java.util.Collection;
17 import java.util.List;
18
19 import org.eclipse.jgit.api.Git;
20 import org.eclipse.jgit.lib.Ref;
21 import org.eclipse.jgit.lib.RefComparator;
22 import org.junit.Test;
23
24 public class RevWalkUtilsReachableTest extends RevWalkTestCase {
25
26 @Test
27 public void oneCommit() throws Exception {
28 RevCommit a = commit();
29 Ref branchA = branch("a", a);
30
31 assertContains(a, asList(branchA));
32 }
33
34 @Test
35 public void twoCommits() throws Exception {
36 RevCommit a = commit();
37 RevCommit b = commit(a);
38 branch("a", a);
39 Ref branchB = branch("b", b);
40
41 assertContains(b, asList(branchB));
42 }
43
44 @Test
45 public void multipleBranches() throws Exception {
46 RevCommit a = commit();
47 RevCommit b = commit(a);
48 branch("a", a);
49 Ref branchB = branch("b", b);
50 Ref branchB2 = branch("b2", b);
51
52 assertContains(b, asList(branchB, branchB2));
53 }
54
55 @Test
56 public void withMerge() throws Exception {
57 RevCommit a = commit();
58 RevCommit b = commit();
59 RevCommit c = commit(a, b);
60 Ref branchA = branch("a", a);
61 Ref branchB = branch("b", b);
62 Ref branchC = branch("c", c);
63
64 assertContains(a, asList(branchA, branchC));
65 assertContains(b, asList(branchB, branchC));
66 }
67
68 @Test
69 public void withCommitLoadedByDifferentRevWalk() throws Exception {
70 RevCommit a = commit();
71 Ref branchA = branch("a", a);
72
73 try (RevWalk walk = new RevWalk(db)) {
74 RevCommit parsedCommit = walk.parseCommit(a.getId());
75 assertContains(parsedCommit, asList(branchA));
76 }
77 }
78
79 @Test
80 public void findBranchesReachableManyTimes() throws Exception {
81
82
83
84
85
86 RevCommit a = commit();
87 RevCommit b = commit();
88 RevCommit c = commit(a);
89 RevCommit d = commit(b);
90 Ref branchA = branch("a", a);
91 Ref branchB = branch("b", b);
92 Ref branchC = branch("c", c);
93 Ref branchD = branch("d", d);
94
95 assertContains(a, asList(branchA, branchC));
96 assertContains(b, asList(branchB, branchD));
97 assertContains(c, asList(branchC));
98 assertContains(d, asList(branchD));
99 }
100
101 private Ref branch(String name, RevCommit dst) throws Exception {
102 return Git.wrap(db).branchCreate().setName(name)
103 .setStartPoint(dst.name()).call();
104 }
105
106 private void assertContains(RevCommit commit, Collection<Ref> refsThatShouldContainCommit) throws Exception {
107 Collection<Ref> allRefs = db.getRefDatabase().getRefs();
108 Collection<Ref> sortedRefs = RefComparator.sort(allRefs);
109 List<Ref> actual = RevWalkUtils.findBranchesReachableFrom(commit,
110 rw, sortedRefs);
111 assertEquals(refsThatShouldContainCommit, actual);
112 }
113
114 }