View Javadoc
1   /*
2    * Copyright (C) 2014, Robin Stocker <robin@nibor.org> 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  
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  		 *  a   b
83  		 *  |   |
84  		 *  c   d
85  		 *      | \
86  		 *      f  e
87  		 *      | /
88  		 *      g
89  		 */
90  		RevCommit a = commit();
91  		RevCommit b = commit();
92  		RevCommit c = commit(a);
93  		RevCommit d = commit(b);
94  		RevCommit f = commit(d);
95  		RevCommit e = commit(d);
96  		RevCommit g = commit(f, e);
97  		Ref branchA = branch("a", a);
98  		Ref branchB = branch("b", b);
99  		Ref branchC = branch("c", c);
100 		Ref branchD = branch("d", d);
101 		Ref branchE = branch("e", e);
102 		Ref branchF = branch("f", f);
103 		Ref branchG = branch("g", g);
104 
105 		assertContains(a, asList(branchA, branchC));
106 		assertContains(b, asList(branchB, branchD, branchE, branchF, branchG));
107 		assertContains(c, asList(branchC));
108 		assertContains(d, asList(branchD, branchE, branchF, branchG));
109 		assertContains(e, asList(branchE, branchG));
110 		assertContains(f, asList(branchF, branchG));
111 		assertContains(g, asList(branchG));
112 	}
113 
114 	private Ref branch(String name, RevCommit dst) throws Exception {
115 		return Git.wrap(db).branchCreate().setName(name)
116 				.setStartPoint(dst.name()).call();
117 	}
118 
119 	private void assertContains(RevCommit commit, Collection<Ref> refsThatShouldContainCommit) throws Exception {
120 		Collection<Ref> allRefs = db.getRefDatabase().getRefs();
121 		Collection<Ref> sortedRefs = RefComparator.sort(allRefs);
122 		List<Ref> actual = RevWalkUtils.findBranchesReachableFrom(commit,
123 				rw, sortedRefs);
124 		assertEquals(refsThatShouldContainCommit, actual);
125 	}
126 
127 }