View Javadoc
1   /*
2    * Copyright (C) 2009-2010, 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.assertEquals;
14  import static org.junit.Assert.assertNull;
15  import static org.junit.Assert.assertTrue;
16  import static org.junit.Assert.fail;
17  
18  import org.eclipse.jgit.internal.JGitText;
19  import org.junit.Test;
20  
21  public class RevWalkSortTest extends RevWalkTestCase {
22  	@Test
23  	public void testSort_Default() throws Exception {
24  		final RevCommit a = commit();
25  		final RevCommit b = commit(1, a);
26  		final RevCommit c = commit(1, b);
27  		final RevCommit d = commit(1, c);
28  
29  		markStart(d);
30  		assertCommit(d, rw.next());
31  		assertCommit(c, rw.next());
32  		assertCommit(b, rw.next());
33  		assertCommit(a, rw.next());
34  		assertNull(rw.next());
35  	}
36  
37  	@Test
38  	public void testSort_COMMIT_TIME_DESC() throws Exception {
39  		final RevCommit a = commit();
40  		final RevCommit b = commit(a);
41  		final RevCommit c = commit(b);
42  		final RevCommit d = commit(c);
43  
44  		rw.sort(RevSort.COMMIT_TIME_DESC);
45  		markStart(d);
46  		assertCommit(d, rw.next());
47  		assertCommit(c, rw.next());
48  		assertCommit(b, rw.next());
49  		assertCommit(a, rw.next());
50  		assertNull(rw.next());
51  	}
52  
53  	@Test
54  	public void testSort_REVERSE() throws Exception {
55  		final RevCommit a = commit();
56  		final RevCommit b = commit(a);
57  		final RevCommit c = commit(b);
58  		final RevCommit d = commit(c);
59  
60  		rw.sort(RevSort.REVERSE);
61  		markStart(d);
62  		assertCommit(a, rw.next());
63  		assertCommit(b, rw.next());
64  		assertCommit(c, rw.next());
65  		assertCommit(d, rw.next());
66  		assertNull(rw.next());
67  	}
68  
69  	@Test
70  	public void testSort_COMMIT_TIME_DESC_OutOfOrder1() throws Exception {
71  		// Despite being out of order time-wise, a strand-of-pearls must
72  		// still maintain topological order.
73  		//
74  		final RevCommit a = commit();
75  		final RevCommit b = commit(a);
76  		final RevCommit c = commit(-5, b);
77  		final RevCommit d = commit(10, c);
78  		assertTrue(parseBody(a).getCommitTime() < parseBody(d).getCommitTime());
79  		assertTrue(parseBody(c).getCommitTime() < parseBody(b).getCommitTime());
80  
81  		rw.sort(RevSort.COMMIT_TIME_DESC);
82  		markStart(d);
83  		assertCommit(d, rw.next());
84  		assertCommit(c, rw.next());
85  		assertCommit(b, rw.next());
86  		assertCommit(a, rw.next());
87  		assertNull(rw.next());
88  	}
89  
90  	@Test
91  	public void testSort_COMMIT_TIME_DESC_OutOfOrder2() throws Exception {
92  		// c1 is back dated before its parent.
93  		//
94  		final RevCommit a = commit();
95  		final RevCommit b = commit(a);
96  		final RevCommit c1 = commit(-5, b);
97  		final RevCommit c2 = commit(10, b);
98  		final RevCommit d = commit(c1, c2);
99  
100 		rw.sort(RevSort.COMMIT_TIME_DESC);
101 		markStart(d);
102 		assertCommit(d, rw.next());
103 		assertCommit(c2, rw.next());
104 		assertCommit(b, rw.next());
105 		assertCommit(a, rw.next());
106 		assertCommit(c1, rw.next());
107 		assertNull(rw.next());
108 	}
109 
110 	@Test
111 	public void testSort_TOPO() throws Exception {
112 		// c1 is back dated before its parent.
113 		//
114 		final RevCommit a = commit();
115 		final RevCommit b = commit(a);
116 		final RevCommit c1 = commit(-5, b);
117 		final RevCommit c2 = commit(10, b);
118 		final RevCommit d = commit(c1, c2);
119 
120 		rw.sort(RevSort.TOPO);
121 		markStart(d);
122 		assertCommit(d, rw.next());
123 		assertCommit(c2, rw.next());
124 		assertCommit(c1, rw.next());
125 		assertCommit(b, rw.next());
126 		assertCommit(a, rw.next());
127 		assertNull(rw.next());
128 	}
129 
130 	@Test
131 	public void testSort_TOPO_REVERSE() throws Exception {
132 		// c1 is back dated before its parent.
133 		//
134 		final RevCommit a = commit();
135 		final RevCommit b = commit(a);
136 		final RevCommit c1 = commit(-5, b);
137 		final RevCommit c2 = commit(10, b);
138 		final RevCommit d = commit(c1, c2);
139 
140 		rw.sort(RevSort.TOPO);
141 		rw.sort(RevSort.REVERSE, true);
142 		markStart(d);
143 		assertCommit(a, rw.next());
144 		assertCommit(b, rw.next());
145 		assertCommit(c1, rw.next());
146 		assertCommit(c2, rw.next());
147 		assertCommit(d, rw.next());
148 		assertNull(rw.next());
149 	}
150 
151 	@Test
152 	public void testSort_TOPO_NON_INTERMIX() throws Exception {
153 		// c1 is back dated before its parent.
154 		//
155 		final RevCommit a = commit();
156 		final RevCommit b = commit(a);
157 		final RevCommit c1 = commit(-5, b);
158 		final RevCommit c2 = commit(10, b);
159 		final RevCommit d = commit(c1, c2);
160 
161 		rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
162 		markStart(d);
163 		assertCommit(d, rw.next());
164 		assertCommit(c2, rw.next());
165 		assertCommit(c1, rw.next());
166 		assertCommit(b, rw.next());
167 		assertCommit(a, rw.next());
168 		assertNull(rw.next());
169 	}
170 
171 	@Test
172 	public void testSort_TOPO_NON_INTERMIX_OutOfOrderCommitTimes()
173 			throws Exception {
174 		// b is committed before c2 in a different line of history.
175 		//
176 		final RevCommit a = commit();
177 		final RevCommit c1 = commit(a);
178 		final RevCommit b = commit(a);
179 		final RevCommit c2 = commit(c1);
180 		final RevCommit d = commit(b, c2);
181 
182 		rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
183 		markStart(d);
184 		assertCommit(d, rw.next());
185 		assertCommit(c2, rw.next());
186 		assertCommit(c1, rw.next());
187 		assertCommit(b, rw.next());
188 		assertCommit(a, rw.next());
189 		assertNull(rw.next());
190 	}
191 
192 	@Test
193 	public void testSort_TOPO_NON_INTERMIX_MultipleLinesOfHistory()
194 			throws Exception {
195 		final RevCommit a1 = commit();
196 		final RevCommit b1 = commit(a1);
197 		final RevCommit a2 = commit(a1, b1);
198 		final RevCommit b2 = commit(b1);
199 		final RevCommit b3 = commit(b1);
200 		final RevCommit a3 = commit(a2, b2);
201 		final RevCommit a4 = commit(a3, b3);
202 
203 		rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
204 		markStart(a4);
205 		assertCommit(a4, rw.next());
206 		assertCommit(b3, rw.next());
207 		assertCommit(a3, rw.next());
208 		assertCommit(b2, rw.next());
209 		assertCommit(a2, rw.next());
210 		assertCommit(b1, rw.next());
211 		assertCommit(a1, rw.next());
212 		assertNull(rw.next());
213 	}
214 
215 	@Test
216 	public void testSort_TOPO_NON_INTERMIX_REVERSE() throws Exception {
217 		// c1 is back dated before its parent.
218 		//
219 		final RevCommit a = commit();
220 		final RevCommit b = commit(a);
221 		final RevCommit c1 = commit(-5, b);
222 		final RevCommit c2 = commit(10, b);
223 		final RevCommit d = commit(c1, c2);
224 
225 		rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
226 		rw.sort(RevSort.REVERSE, true);
227 		markStart(d);
228 		assertCommit(a, rw.next());
229 		assertCommit(b, rw.next());
230 		assertCommit(c1, rw.next());
231 		assertCommit(c2, rw.next());
232 		assertCommit(d, rw.next());
233 		assertNull(rw.next());
234 	}
235 
236 	@Test
237 	public void testSort_TOPO_NON_INTERMIX_REVERSE_MultipleLinesOfHistory()
238 			throws Exception {
239 		final RevCommit a1 = commit();
240 		final RevCommit b1 = commit(a1);
241 		final RevCommit a2 = commit(a1, b1);
242 		final RevCommit b2 = commit(b1);
243 		final RevCommit b3 = commit(b1);
244 		final RevCommit a3 = commit(a2, b2);
245 		final RevCommit a4 = commit(a3, b3);
246 
247 		rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
248 		rw.sort(RevSort.REVERSE, true);
249 		markStart(a4);
250 		assertCommit(a1, rw.next());
251 		assertCommit(b1, rw.next());
252 		assertCommit(a2, rw.next());
253 		assertCommit(b2, rw.next());
254 		assertCommit(a3, rw.next());
255 		assertCommit(b3, rw.next());
256 		assertCommit(a4, rw.next());
257 		assertNull(rw.next());
258 	}
259 
260 	@Test
261 	public void testSort_TOPO_NON_INTERMIX_ParentOfMultipleStartChildren()
262 			throws Exception {
263 		final RevCommit a = commit();
264 		final RevCommit b = commit(a);
265 		final RevCommit c = commit(a);
266 		final RevCommit d1 = commit(a);
267 		final RevCommit d2 = commit(d1);
268 		final RevCommit e = commit(a);
269 
270 		rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
271 		markStart(b);
272 		markStart(c);
273 		markStart(d2);
274 		markStart(e);
275 		assertCommit(e, rw.next());
276 		assertCommit(d2, rw.next());
277 		assertCommit(d1, rw.next());
278 		assertCommit(c, rw.next());
279 		assertCommit(b, rw.next());
280 		assertCommit(a, rw.next());
281 		assertNull(rw.next());
282 	}
283 
284 	@Test
285 	public void testSort_TOPO_NON_INTERMIX_Uninteresting() throws Exception {
286 		final RevCommit a1 = commit();
287 		final RevCommit a2 = commit(a1);
288 		final RevCommit a3 = commit(a2);
289 		final RevCommit b = commit(a1);
290 		final RevCommit a4 = commit(a3, b);
291 
292 		rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
293 		markStart(a4);
294 		markUninteresting(a2);
295 		assertCommit(a4, rw.next());
296 		assertCommit(b, rw.next());
297 		assertCommit(a3, rw.next());
298 		assertNull(rw.next());
299 	}
300 
301 	@Test
302 	public void testSort_TOPO_NON_INTERMIX_and_TOPO_throws() throws Exception {
303 		final RevCommit a = commit();
304 
305 		rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
306 		rw.sort(RevSort.TOPO, true);
307 		markStart(a);
308 		try {
309 			rw.next();
310 			fail("did not throw IllegalStateException");
311 		} catch (IllegalStateException e) {
312 			assertEquals(
313 					JGitText.get().cannotCombineTopoSortWithTopoKeepBranchTogetherSort,
314 					e.getMessage());
315 		}
316 	}
317 }