View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc. 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 org.junit.Assert.assertNull;
14  import static org.junit.Assert.fail;
15  
16  import org.eclipse.jgit.revwalk.filter.RevFilter;
17  import org.eclipse.jgit.treewalk.filter.TreeFilter;
18  import org.junit.Test;
19  
20  public class RevWalkMergeBaseTest extends RevWalkTestCase {
21  	@Test
22  	public void testNone() throws Exception {
23  		final RevCommit c1 = commit(commit(commit()));
24  		final RevCommit c2 = commit(commit(commit()));
25  
26  		rw.setRevFilter(RevFilter.MERGE_BASE);
27  		markStart(c1);
28  		markStart(c2);
29  		assertNull(rw.next());
30  	}
31  
32  	@Test
33  	public void testDisallowTreeFilter() throws Exception {
34  		final RevCommit c1 = commit();
35  		final RevCommit c2 = commit();
36  
37  		rw.setRevFilter(RevFilter.MERGE_BASE);
38  		rw.setTreeFilter(TreeFilter.ANY_DIFF);
39  		markStart(c1);
40  		markStart(c2);
41  		try {
42  			assertNull(rw.next());
43  			fail("did not throw IllegalStateException");
44  		} catch (IllegalStateException ise) {
45  			// expected result
46  		}
47  	}
48  
49  	@Test
50  	public void testSimple() throws Exception {
51  		final RevCommit a = commit();
52  		final RevCommit b = commit(a);
53  		final RevCommit c1 = commit(commit(commit(commit(commit(b)))));
54  		final RevCommit c2 = commit(commit(commit(commit(commit(b)))));
55  
56  		rw.setRevFilter(RevFilter.MERGE_BASE);
57  		markStart(c1);
58  		markStart(c2);
59  		assertCommit(b, rw.next());
60  		assertNull(rw.next());
61  	}
62  
63  	@Test
64  	public void testMultipleHeads_SameBase1() throws Exception {
65  		final RevCommit a = commit();
66  		final RevCommit b = commit(a);
67  		final RevCommit c1 = commit(commit(commit(commit(commit(b)))));
68  		final RevCommit c2 = commit(commit(commit(commit(commit(b)))));
69  		final RevCommit c3 = commit(commit(commit(b)));
70  
71  		rw.setRevFilter(RevFilter.MERGE_BASE);
72  		markStart(c1);
73  		markStart(c2);
74  		markStart(c3);
75  		assertCommit(b, rw.next());
76  		assertNull(rw.next());
77  	}
78  
79  	@Test
80  	public void testMultipleHeads_SameBase2() throws Exception {
81  		final RevCommit a = commit();
82  		final RevCommit b = commit(a);
83  		final RevCommit c = commit(b);
84  		final RevCommit d1 = commit(commit(commit(commit(commit(b)))));
85  		final RevCommit d2 = commit(commit(commit(commit(commit(c)))));
86  		final RevCommit d3 = commit(commit(commit(c)));
87  
88  		rw.setRevFilter(RevFilter.MERGE_BASE);
89  		markStart(d1);
90  		markStart(d2);
91  		markStart(d3);
92  		assertCommit(b, rw.next());
93  		assertNull(rw.next());
94  	}
95  
96  	@Test
97  	public void testCrissCross() throws Exception {
98  		// See http://marc.info/?l=git&m=111463358500362&w=2 for a nice
99  		// description of what this test is creating. We don't have a
100 		// clean merge base for d,e as they each merged the parents b,c
101 		// in different orders.
102 		//
103 		final RevCommit a = commit();
104 		final RevCommit b = commit(a);
105 		final RevCommit c = commit(a);
106 		final RevCommit d = commit(b, c);
107 		final RevCommit e = commit(c, b);
108 
109 		rw.setRevFilter(RevFilter.MERGE_BASE);
110 		markStart(d);
111 		markStart(e);
112 		assertCommit(c, rw.next());
113 		assertCommit(b, rw.next());
114 		assertNull(rw.next());
115 	}
116 
117 	@Test
118 	public void testInconsistentCommitTimes() throws Exception {
119 		// When commit times are inconsistent (a parent is younger than a child)
120 		// make sure that not both, parent and child, are reported as merge
121 		// base. In the following repo the merge base between C,D should be B.
122 		// But when A is younger than B the MergeBaseGenerator used to generate
123 		// A before it detected that B is also a merge base.
124 		//
125 		//   +---C
126 		//  /   /
127 		// A---B---D
128 
129 		final RevCommit a = commit(2);
130 		final RevCommit b = commit(-1, a);
131 		final RevCommit c = commit(2, b, a);
132 		final RevCommit d = commit(1, b);
133 
134 		rw.setRevFilter(RevFilter.MERGE_BASE);
135 		markStart(d);
136 		markStart(c);
137 		assertCommit(b, rw.next());
138 		assertNull(rw.next());
139 	}
140 
141 }