View Javadoc
1   /*
2    * Copyright (C) 2008, 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.treewalk;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertTrue;
16  
17  import org.eclipse.jgit.dircache.DirCache;
18  import org.eclipse.jgit.dircache.DirCacheBuilder;
19  import org.eclipse.jgit.dircache.DirCacheIterator;
20  import org.eclipse.jgit.junit.RepositoryTestCase;
21  import org.eclipse.jgit.lib.FileMode;
22  import org.junit.Test;
23  
24  public class NameConflictTreeWalkTest extends RepositoryTestCase {
25  	private static final FileMode TREE = FileMode.TREE;
26  
27  	private static final FileMode SYMLINK = FileMode.SYMLINK;
28  
29  	private static final FileMode MISSING = FileMode.MISSING;
30  
31  	private static final FileMode REGULAR_FILE = FileMode.REGULAR_FILE;
32  
33  	private static final FileMode EXECUTABLE_FILE = FileMode.EXECUTABLE_FILE;
34  
35  	@Test
36  	public void testNoDF_NoGap() throws Exception {
37  		final DirCache tree0 = db.readDirCache();
38  		final DirCache tree1 = db.readDirCache();
39  		{
40  			final DirCacheBuilder b0 = tree0.builder();
41  			final DirCacheBuilder b1 = tree1.builder();
42  
43  			b0.add(createEntry("a", REGULAR_FILE));
44  			b0.add(createEntry("a.b", EXECUTABLE_FILE));
45  			b1.add(createEntry("a/b", REGULAR_FILE));
46  			b0.add(createEntry("a0b", SYMLINK));
47  
48  			b0.finish();
49  			b1.finish();
50  			assertEquals(3, tree0.getEntryCount());
51  			assertEquals(1, tree1.getEntryCount());
52  		}
53  
54  		try (TreeWalk tw = new TreeWalk(db)) {
55  			tw.addTree(new DirCacheIterator(tree0));
56  			tw.addTree(new DirCacheIterator(tree1));
57  
58  			assertModes("a", REGULAR_FILE, MISSING, tw);
59  			assertModes("a.b", EXECUTABLE_FILE, MISSING, tw);
60  			assertModes("a", MISSING, TREE, tw);
61  			tw.enterSubtree();
62  			assertModes("a/b", MISSING, REGULAR_FILE, tw);
63  			assertModes("a0b", SYMLINK, MISSING, tw);
64  		}
65  	}
66  
67  	@Test
68  	public void testDF_NoGap() throws Exception {
69  		final DirCache tree0 = db.readDirCache();
70  		final DirCache tree1 = db.readDirCache();
71  		{
72  			final DirCacheBuilder b0 = tree0.builder();
73  			final DirCacheBuilder b1 = tree1.builder();
74  
75  			b0.add(createEntry("a", REGULAR_FILE));
76  			b0.add(createEntry("a.b", EXECUTABLE_FILE));
77  			b1.add(createEntry("a/b", REGULAR_FILE));
78  			b0.add(createEntry("a0b", SYMLINK));
79  
80  			b0.finish();
81  			b1.finish();
82  			assertEquals(3, tree0.getEntryCount());
83  			assertEquals(1, tree1.getEntryCount());
84  		}
85  
86  		try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) {
87  			tw.addTree(new DirCacheIterator(tree0));
88  			tw.addTree(new DirCacheIterator(tree1));
89  
90  			assertModes("a", REGULAR_FILE, TREE, tw);
91  			assertTrue(tw.isDirectoryFileConflict());
92  			assertTrue(tw.isSubtree());
93  			tw.enterSubtree();
94  			assertModes("a/b", MISSING, REGULAR_FILE, tw);
95  			assertTrue(tw.isDirectoryFileConflict());
96  			assertModes("a.b", EXECUTABLE_FILE, MISSING, tw);
97  			assertFalse(tw.isDirectoryFileConflict());
98  			assertModes("a0b", SYMLINK, MISSING, tw);
99  			assertFalse(tw.isDirectoryFileConflict());
100 		}
101 	}
102 
103 	@Test
104 	public void testDF_GapByOne() throws Exception {
105 		final DirCache tree0 = db.readDirCache();
106 		final DirCache tree1 = db.readDirCache();
107 		{
108 			final DirCacheBuilder b0 = tree0.builder();
109 			final DirCacheBuilder b1 = tree1.builder();
110 
111 			b0.add(createEntry("a", REGULAR_FILE));
112 			b0.add(createEntry("a.b", EXECUTABLE_FILE));
113 			b1.add(createEntry("a.b", EXECUTABLE_FILE));
114 			b1.add(createEntry("a/b", REGULAR_FILE));
115 			b0.add(createEntry("a0b", SYMLINK));
116 
117 			b0.finish();
118 			b1.finish();
119 			assertEquals(3, tree0.getEntryCount());
120 			assertEquals(2, tree1.getEntryCount());
121 		}
122 
123 		try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) {
124 			tw.addTree(new DirCacheIterator(tree0));
125 			tw.addTree(new DirCacheIterator(tree1));
126 
127 			assertModes("a", REGULAR_FILE, TREE, tw);
128 			assertTrue(tw.isSubtree());
129 			assertTrue(tw.isDirectoryFileConflict());
130 			tw.enterSubtree();
131 			assertModes("a/b", MISSING, REGULAR_FILE, tw);
132 			assertTrue(tw.isDirectoryFileConflict());
133 			assertModes("a.b", EXECUTABLE_FILE, EXECUTABLE_FILE, tw);
134 			assertFalse(tw.isDirectoryFileConflict());
135 			assertModes("a0b", SYMLINK, MISSING, tw);
136 			assertFalse(tw.isDirectoryFileConflict());
137 		}
138 	}
139 
140 	/**
141 	 * The test reproduces https://bugs.eclipse.org/bugs/show_bug.cgi?id=535919.
142 	 */
143 	@Test
144 	public void tesdDF_LastItemsInTreeHasDFConflictAndSpecialNames()
145 			throws Exception {
146 
147 		final DirCache tree0 = db.readDirCache();
148 		final DirCache tree1 = db.readDirCache();
149 
150 		final DirCacheBuilder b0 = tree0.builder();
151 		final DirCacheBuilder b1 = tree1.builder();
152 		// The tree0 has the following order in git:
153 		//     subtree, subtree-0
154 		b0.add(createEntry("subtree", REGULAR_FILE));
155 		b0.add(createEntry("subtree-0", REGULAR_FILE));
156 		// The tree1 has the following order in git:
157 		//     subtree-0, subtree/file
158 		b1.add(createEntry("subtree/file", REGULAR_FILE));
159 		b1.add(createEntry("subtree-0", REGULAR_FILE));
160 
161 		b0.finish();
162 		b1.finish();
163 
164 		try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) {
165 			tw.addTree(new DirCacheIterator(tree0));
166 			tw.addTree(new DirCacheIterator(tree1));
167 
168 			assertModes("subtree", REGULAR_FILE, TREE, tw);
169 			assertTrue(tw.isSubtree());
170 			assertTrue(tw.isDirectoryFileConflict());
171 			tw.enterSubtree();
172 			assertModes("subtree/file", MISSING, REGULAR_FILE, tw);
173 			assertFalse(tw.isSubtree());
174 			// isDirectoryFileConflict is true, because the conflict is detected
175 			// on parent.
176 			assertTrue(tw.isDirectoryFileConflict());
177 			assertModes("subtree-0", REGULAR_FILE, REGULAR_FILE, tw);
178 			assertFalse(tw.isSubtree());
179 			assertFalse(tw.isDirectoryFileConflict());
180 			assertFalse(tw.next());
181 		}
182 	}
183 
184 	/**
185 	 * The test reproduces https://bugs.eclipse.org/bugs/show_bug.cgi?id=535919.
186 	 */
187 	@Test
188 	public void tesdDF_LastItemsInTreeHasDFConflictAndSpecialNames2()
189 			throws Exception {
190 
191 		final DirCache tree0 = db.readDirCache();
192 		final DirCache tree1 = db.readDirCache();
193 
194 		final DirCacheBuilder b0 = tree0.builder();
195 		final DirCacheBuilder b1 = tree1.builder();
196 		// The tree0 has the following order in git:
197 		//     subtree-0, subtree/file
198 		b0.add(createEntry("subtree/file", REGULAR_FILE));
199 		b0.add(createEntry("subtree-0", REGULAR_FILE));
200 		// The tree1 has the following order in git:
201 		//     subtree, subtree-0
202 		b1.add(createEntry("subtree", REGULAR_FILE));
203 		b1.add(createEntry("subtree-0", REGULAR_FILE));
204 
205 		b0.finish();
206 		b1.finish();
207 
208 		try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) {
209 			tw.addTree(new DirCacheIterator(tree0));
210 			tw.addTree(new DirCacheIterator(tree1));
211 
212 			assertModes("subtree", TREE, REGULAR_FILE, tw);
213 			assertTrue(tw.isSubtree());
214 			assertTrue(tw.isDirectoryFileConflict());
215 			tw.enterSubtree();
216 			assertModes("subtree/file", REGULAR_FILE, MISSING, tw);
217 			assertFalse(tw.isSubtree());
218 			// isDirectoryFileConflict is true, because the conflict is detected
219 			// on parent.
220 			assertTrue(tw.isDirectoryFileConflict());
221 			assertModes("subtree-0", REGULAR_FILE, REGULAR_FILE, tw);
222 			assertFalse(tw.isSubtree());
223 			assertFalse(tw.isDirectoryFileConflict());
224 			assertFalse(tw.next());
225 		}
226 	}
227 
228 	@Test
229 	public void tesdDF_NonLastItemsInTreeHasDFConflictAndSpecialNames()
230 			throws Exception {
231 		final DirCache tree0 = db.readDirCache();
232 		final DirCache tree1 = db.readDirCache();
233 
234 		final DirCacheBuilder b0 = tree0.builder();
235 		final DirCacheBuilder b1 = tree1.builder();
236 		b0.add(createEntry("subtree", REGULAR_FILE));
237 		b0.add(createEntry("subtree-0", REGULAR_FILE));
238 		b0.add(createEntry("x", REGULAR_FILE));
239 
240 		b1.add(createEntry("subtree/file", REGULAR_FILE));
241 		b1.add(createEntry("subtree-0", REGULAR_FILE));
242 		b1.add(createEntry("x", REGULAR_FILE));
243 
244 		b0.finish();
245 		b1.finish();
246 
247 		try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) {
248 			tw.addTree(new DirCacheIterator(tree0));
249 			tw.addTree(new DirCacheIterator(tree1));
250 
251 			assertModes("subtree", REGULAR_FILE, TREE, tw);
252 			assertTrue(tw.isSubtree());
253 			assertTrue(tw.isDirectoryFileConflict());
254 			tw.enterSubtree();
255 			assertModes("subtree/file", MISSING, REGULAR_FILE, tw);
256 			assertFalse(tw.isSubtree());
257 			// isDirectoryFileConflict is true, because the conflict is detected
258 			// on parent.
259 			// see JavaDoc for isDirectoryFileConflict for details
260 			assertTrue(tw.isDirectoryFileConflict());
261 			assertModes("subtree-0", REGULAR_FILE, REGULAR_FILE, tw);
262 			assertFalse(tw.isSubtree());
263 			assertFalse(tw.isDirectoryFileConflict());
264 			assertModes("x", REGULAR_FILE, REGULAR_FILE, tw);
265 			assertFalse(tw.isSubtree());
266 			assertFalse(tw.isDirectoryFileConflict());
267 			assertFalse(tw.next());
268 		}
269 	}
270 
271 	@Test
272 	public void tesdDF_NoSpecialNames() throws Exception {
273 		final DirCache tree0 = db.readDirCache();
274 		final DirCache tree1 = db.readDirCache();
275 
276 		final DirCacheBuilder b0 = tree0.builder();
277 		final DirCacheBuilder b1 = tree1.builder();
278 		// In this test both trees (tree0 and tree1) have exactly the same order
279 		// of entries:
280 		//     subtree, xubtree-0
281 		b0.add(createEntry("subtree", REGULAR_FILE));
282 		b0.add(createEntry("xubtree-0", REGULAR_FILE));
283 
284 		b1.add(createEntry("subtree/file", REGULAR_FILE));
285 		b1.add(createEntry("xubtree-0", REGULAR_FILE));
286 
287 		b0.finish();
288 		b1.finish();
289 
290 		try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) {
291 			tw.addTree(new DirCacheIterator(tree0));
292 			tw.addTree(new DirCacheIterator(tree1));
293 
294 			assertModes("subtree", REGULAR_FILE, TREE, tw);
295 			assertTrue(tw.isSubtree());
296 			assertTrue(tw.isDirectoryFileConflict());
297 			tw.enterSubtree();
298 			assertModes("subtree/file", MISSING, REGULAR_FILE, tw);
299 			assertFalse(tw.isSubtree());
300 			// isDirectoryFileConflict is true, because the conflict is detected
301 			// on parent.
302 			// see JavaDoc for isDirectoryFileConflict for details
303 			assertTrue(tw.isDirectoryFileConflict());
304 			assertModes("xubtree-0", REGULAR_FILE, REGULAR_FILE, tw);
305 			assertFalse(tw.isSubtree());
306 			assertFalse(tw.isDirectoryFileConflict());
307 			assertFalse(tw.next());
308 		}
309 	}
310 
311 	@Test
312 	public void testDF_specialFileNames() throws Exception {
313 		final DirCache tree0 = db.readDirCache();
314 		final DirCache tree1 = db.readDirCache();
315 		final DirCache tree2 = db.readDirCache();
316 		{
317 			final DirCacheBuilder b0 = tree0.builder();
318 			final DirCacheBuilder b1 = tree1.builder();
319 			final DirCacheBuilder b2 = tree2.builder();
320 
321 			b0.add(createEntry("gradle.properties", REGULAR_FILE));
322 			b0.add(createEntry("gradle/nested_file.txt", REGULAR_FILE));
323 
324 			b1.add(createEntry("gradle.properties", REGULAR_FILE));
325 
326 			b2.add(createEntry("gradle", REGULAR_FILE));
327 			b2.add(createEntry("gradle.properties", REGULAR_FILE));
328 
329 			b0.finish();
330 			b1.finish();
331 			b2.finish();
332 			assertEquals(2, tree0.getEntryCount());
333 			assertEquals(1, tree1.getEntryCount());
334 			assertEquals(2, tree2.getEntryCount());
335 		}
336 
337 		try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) {
338 			tw.addTree(new DirCacheIterator(tree0));
339 			tw.addTree(new DirCacheIterator(tree1));
340 			tw.addTree(new DirCacheIterator(tree2));
341 
342 			assertModes("gradle", TREE, MISSING, REGULAR_FILE, tw);
343 			assertTrue(tw.isSubtree());
344 			assertTrue(tw.isDirectoryFileConflict());
345 			tw.enterSubtree();
346 			assertModes("gradle/nested_file.txt", REGULAR_FILE, MISSING,
347 					MISSING, tw);
348 			assertFalse(tw.isSubtree());
349 			// isDirectoryFileConflict is true, because the conflict is detected
350 			// on parent.
351 			assertTrue(tw.isDirectoryFileConflict());
352 			assertModes("gradle.properties", REGULAR_FILE, REGULAR_FILE,
353 					REGULAR_FILE, tw);
354 			assertFalse(tw.isSubtree());
355 			assertFalse(tw.isDirectoryFileConflict());
356 		}
357 	}
358 
359 	@Test
360 	public void testDF_SkipsSeenSubtree() throws Exception {
361 		final DirCache tree0 = db.readDirCache();
362 		final DirCache tree1 = db.readDirCache();
363 		{
364 			final DirCacheBuilder b0 = tree0.builder();
365 			final DirCacheBuilder b1 = tree1.builder();
366 
367 			b0.add(createEntry("a", REGULAR_FILE));
368 			b1.add(createEntry("a.b", EXECUTABLE_FILE));
369 			b1.add(createEntry("a/b", REGULAR_FILE));
370 			b0.add(createEntry("a0b", SYMLINK));
371 			b1.add(createEntry("a0b", SYMLINK));
372 
373 			b0.finish();
374 			b1.finish();
375 			assertEquals(2, tree0.getEntryCount());
376 			assertEquals(3, tree1.getEntryCount());
377 		}
378 
379 		try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) {
380 			tw.addTree(new DirCacheIterator(tree0));
381 			tw.addTree(new DirCacheIterator(tree1));
382 
383 			assertModes("a", REGULAR_FILE, TREE, tw);
384 			assertTrue(tw.isSubtree());
385 			assertTrue(tw.isDirectoryFileConflict());
386 			tw.enterSubtree();
387 			assertModes("a/b", MISSING, REGULAR_FILE, tw);
388 			assertTrue(tw.isDirectoryFileConflict());
389 			assertModes("a.b", MISSING, EXECUTABLE_FILE, tw);
390 			assertFalse(tw.isDirectoryFileConflict());
391 			assertModes("a0b", SYMLINK, SYMLINK, tw);
392 			assertFalse(tw.isDirectoryFileConflict());
393 		}
394 	}
395 
396 	@Test
397 	public void testDF_DetectConflict() throws Exception {
398 		final DirCache tree0 = db.readDirCache();
399 		final DirCache tree1 = db.readDirCache();
400 		{
401 			final DirCacheBuilder b0 = tree0.builder();
402 			final DirCacheBuilder b1 = tree1.builder();
403 
404 			b0.add(createEntry("0", REGULAR_FILE));
405 			b0.add(createEntry("a", REGULAR_FILE));
406 			b1.add(createEntry("0", REGULAR_FILE));
407 			b1.add(createEntry("a.b", REGULAR_FILE));
408 			b1.add(createEntry("a/b", REGULAR_FILE));
409 			b1.add(createEntry("a/c/e", REGULAR_FILE));
410 
411 			b0.finish();
412 			b1.finish();
413 			assertEquals(2, tree0.getEntryCount());
414 			assertEquals(4, tree1.getEntryCount());
415 		}
416 
417 		try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) {
418 			tw.addTree(new DirCacheIterator(tree0));
419 			tw.addTree(new DirCacheIterator(tree1));
420 
421 			assertModes("0", REGULAR_FILE, REGULAR_FILE, tw);
422 			assertFalse(tw.isDirectoryFileConflict());
423 			assertModes("a", REGULAR_FILE, TREE, tw);
424 			assertTrue(tw.isSubtree());
425 			assertTrue(tw.isDirectoryFileConflict());
426 			tw.enterSubtree();
427 			assertModes("a/b", MISSING, REGULAR_FILE, tw);
428 			assertTrue(tw.isDirectoryFileConflict());
429 			assertModes("a/c", MISSING, TREE, tw);
430 			assertTrue(tw.isDirectoryFileConflict());
431 			tw.enterSubtree();
432 			assertModes("a/c/e", MISSING, REGULAR_FILE, tw);
433 			assertTrue(tw.isDirectoryFileConflict());
434 
435 			assertModes("a.b", MISSING, REGULAR_FILE, tw);
436 			assertFalse(tw.isDirectoryFileConflict());
437 		}
438 	}
439 
440 	private static void assertModes(String path, FileMode mode0, FileMode mode1,
441 			TreeWalk tw) throws Exception {
442 		assertTrue("has " + path, tw.next());
443 		assertEquals(path, tw.getPathString());
444 		assertEquals(mode0, tw.getFileMode(0));
445 		assertEquals(mode1, tw.getFileMode(1));
446 	}
447 
448 	private static void assertModes(String path, FileMode mode0, FileMode mode1,
449 			FileMode mode2, TreeWalk tw) throws Exception {
450 		assertTrue("has " + path, tw.next());
451 		assertEquals(path, tw.getPathString());
452 		if (tw.getFileMode(0) != FileMode.MISSING) {
453 			assertEquals(path, TreeWalk.pathOf(tw.trees[0]));
454 		}
455 		if (tw.getFileMode(1) != FileMode.MISSING) {
456 			assertEquals(path, TreeWalk.pathOf(tw.trees[1]));
457 		}
458 		if (tw.getFileMode(2) != FileMode.MISSING) {
459 			assertEquals(path, TreeWalk.pathOf(tw.trees[2]));
460 		}
461 		assertEquals(mode0, tw.getFileMode(0));
462 		assertEquals(mode1, tw.getFileMode(1));
463 		assertEquals(mode2, tw.getFileMode(2));
464 	}
465 }