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  	private Ref branch(String name, RevCommit dst) throws Exception {
80  		return Git.wrap(db).branchCreate().setName(name)
81  				.setStartPoint(dst.name()).call();
82  	}
83  
84  	private void assertContains(RevCommit commit, Collection<Ref> refsThatShouldContainCommit) throws Exception {
85  		Collection<Ref> allRefs = db.getRefDatabase().getRefs();
86  		Collection<Ref> sortedRefs = RefComparator.sort(allRefs);
87  		List<Ref> actual = RevWalkUtils.findBranchesReachableFrom(commit,
88  				rw, sortedRefs);
89  		assertEquals(refsThatShouldContainCommit, actual);
90  	}
91  
92  }