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.eclipse.jgit.lib.Constants.OBJ_BLOB;
14  import static org.eclipse.jgit.lib.Constants.encode;
15  import static org.junit.Assert.assertEquals;
16  import static org.junit.Assert.assertFalse;
17  import static org.junit.Assert.assertTrue;
18  
19  import org.eclipse.jgit.junit.RepositoryTestCase;
20  import org.eclipse.jgit.lib.FileMode;
21  import org.eclipse.jgit.lib.ObjectId;
22  import org.eclipse.jgit.lib.ObjectInserter;
23  import org.eclipse.jgit.lib.TreeFormatter;
24  import org.eclipse.jgit.treewalk.filter.TreeFilter;
25  import org.junit.Test;
26  
27  public class TreeWalkBasicDiffTest extends RepositoryTestCase {
28  	@Test
29  	public void testMissingSubtree_DetectFileAdded_FileModified()
30  			throws Exception {
31  		final ObjectId oldTree, newTree, bFileId, cFileId1, cFileId2;
32  		try (ObjectInserter inserter = db.newObjectInserter()) {
33  			final ObjectId aFileId = inserter.insert(OBJ_BLOB, encode("a"));
34  			bFileId = inserter.insert(OBJ_BLOB, encode("b"));
35  			cFileId1 = inserter.insert(OBJ_BLOB, encode("c-1"));
36  			cFileId2 = inserter.insert(OBJ_BLOB, encode("c-2"));
37  
38  			// Create sub-a/empty, sub-c/empty = hello.
39  			{
40  				TreeFormatter root = new TreeFormatter();
41  				{
42  					TreeFormatter subA = new TreeFormatter();
43  					subA.append("empty", FileMode.REGULAR_FILE, aFileId);
44  					root.append("sub-a", FileMode.TREE, inserter.insert(subA));
45  				}
46  				{
47  					TreeFormatter subC = new TreeFormatter();
48  					subC.append("empty", FileMode.REGULAR_FILE, cFileId1);
49  					root.append("sub-c", FileMode.TREE, inserter.insert(subC));
50  				}
51  				oldTree = inserter.insert(root);
52  			}
53  
54  			// Create sub-a/empty, sub-b/empty, sub-c/empty.
55  			{
56  				TreeFormatter root = new TreeFormatter();
57  				{
58  					TreeFormatter subA = new TreeFormatter();
59  					subA.append("empty", FileMode.REGULAR_FILE, aFileId);
60  					root.append("sub-a", FileMode.TREE, inserter.insert(subA));
61  				}
62  				{
63  					TreeFormatter subB = new TreeFormatter();
64  					subB.append("empty", FileMode.REGULAR_FILE, bFileId);
65  					root.append("sub-b", FileMode.TREE, inserter.insert(subB));
66  				}
67  				{
68  					TreeFormatter subC = new TreeFormatter();
69  					subC.append("empty", FileMode.REGULAR_FILE, cFileId2);
70  					root.append("sub-c", FileMode.TREE, inserter.insert(subC));
71  				}
72  				newTree = inserter.insert(root);
73  			}
74  			inserter.flush();
75  		}
76  
77  		try (TreeWalk tw = new TreeWalk(db)) {
78  			tw.reset(oldTree, newTree);
79  			tw.setRecursive(true);
80  			tw.setFilter(TreeFilter.ANY_DIFF);
81  
82  			assertTrue(tw.next());
83  			assertEquals("sub-b/empty", tw.getPathString());
84  			assertEquals(FileMode.MISSING, tw.getFileMode(0));
85  			assertEquals(FileMode.REGULAR_FILE, tw.getFileMode(1));
86  			assertEquals(ObjectId.zeroId(), tw.getObjectId(0));
87  			assertEquals(bFileId, tw.getObjectId(1));
88  
89  			assertTrue(tw.next());
90  			assertEquals("sub-c/empty", tw.getPathString());
91  			assertEquals(FileMode.REGULAR_FILE, tw.getFileMode(0));
92  			assertEquals(FileMode.REGULAR_FILE, tw.getFileMode(1));
93  			assertEquals(cFileId1, tw.getObjectId(0));
94  			assertEquals(cFileId2, tw.getObjectId(1));
95  
96  			assertFalse(tw.next());
97  		}
98  	}
99  }