View Javadoc
1   /*
2    * Copyright (C) 2013, Robin Rosenberg <robin.rosenberg@dewire.com> 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  package org.eclipse.jgit.treewalk.filter;
11  
12  import static java.nio.charset.StandardCharsets.UTF_8;
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertTrue;
16  
17  import java.io.IOException;
18  
19  import org.eclipse.jgit.dircache.DirCache;
20  import org.eclipse.jgit.dircache.DirCacheEditor;
21  import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
22  import org.eclipse.jgit.dircache.DirCacheEntry;
23  import org.eclipse.jgit.dircache.DirCacheIterator;
24  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
25  import org.eclipse.jgit.lib.Constants;
26  import org.eclipse.jgit.lib.FileMode;
27  import org.eclipse.jgit.lib.ObjectId;
28  import org.eclipse.jgit.lib.Repository;
29  import org.eclipse.jgit.treewalk.TreeWalk;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  public class InterIndexDiffFilterTest extends LocalDiskRepositoryTestCase {
34  
35  	private Repository db;
36  
37  	@Override
38  	@Before
39  	public void setUp() throws Exception {
40  		super.setUp();
41  		db = createWorkRepository();
42  	}
43  
44  	@Test
45  	public void testEmpty() throws IOException {
46  		DirCache dc1 = DirCache.newInCore();
47  		DirCache dc2 = DirCache.newInCore();
48  		try (TreeWalk tw = new TreeWalk(db)) {
49  			tw.addTree(new DirCacheIterator(dc1));
50  			tw.addTree(new DirCacheIterator(dc2));
51  			assertFalse(tw.next());
52  		}
53  	}
54  
55  	static final class AddEdit extends PathEdit {
56  
57  		private final ObjectId data;
58  
59  		private final long length;
60  
61  		private boolean assumeValid;
62  
63  		private FileMode type;
64  
65  		public AddEdit(String entryPath, FileMode type, ObjectId data,
66  				long length,
67  				boolean assumeValid) {
68  			super(entryPath);
69  			this.type = type;
70  			this.data = data;
71  			this.length = length;
72  			this.assumeValid = assumeValid;
73  		}
74  
75  		@Override
76  		public void apply(DirCacheEntry ent) {
77  			ent.setFileMode(type);
78  			ent.setLength(length);
79  			ent.setObjectId(data);
80  			ent.setAssumeValid(assumeValid);
81  		}
82  	}
83  
84  	private ObjectId id(String data) {
85  		byte[] bytes = data.getBytes(UTF_8);
86  		return db.newObjectInserter().idFor(Constants.OBJ_BLOB, bytes);
87  	}
88  
89  	@Test
90  	public void testOneOnly() throws IOException {
91  		DirCache dc1 = DirCache.newInCore();
92  		DirCache dc2 = DirCache.newInCore();
93  		DirCacheEditor editor = dc1.editor();
94  		editor.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, false));
95  		editor.finish();
96  
97  		try (TreeWalk tw = new TreeWalk(db)) {
98  			tw.setRecursive(true);
99  			tw.addTree(new DirCacheIterator(dc1));
100 			tw.addTree(new DirCacheIterator(dc2));
101 			tw.setFilter(InterIndexDiffFilter.INSTANCE);
102 			assertTrue(tw.next());
103 			assertEquals("a/a", tw.getPathString());
104 			assertFalse(tw.next());
105 		}
106 	}
107 
108 	@Test
109 	public void testTwoSame() throws IOException {
110 		DirCache dc1 = DirCache.newInCore();
111 		DirCache dc2 = DirCache.newInCore();
112 		DirCacheEditor ed1 = dc1.editor();
113 		ed1.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, false));
114 		ed1.finish();
115 		DirCacheEditor ed2 = dc2.editor();
116 		ed2.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, false));
117 		ed2.finish();
118 
119 		try (TreeWalk tw = new TreeWalk(db)) {
120 			tw.setRecursive(true);
121 			tw.addTree(new DirCacheIterator(dc1));
122 			tw.addTree(new DirCacheIterator(dc2));
123 			tw.setFilter(InterIndexDiffFilter.INSTANCE);
124 
125 			assertFalse(tw.next());
126 		}
127 	}
128 
129 	@Test
130 	public void testTwoSameDifferByAssumeValid() throws IOException {
131 		DirCache dc1 = DirCache.newInCore();
132 		DirCache dc2 = DirCache.newInCore();
133 		DirCacheEditor ed1 = dc1.editor();
134 		ed1.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, false));
135 		ed1.finish();
136 		DirCacheEditor ed2 = dc2.editor();
137 		ed2.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, true));
138 		ed2.finish();
139 
140 		try (TreeWalk tw = new TreeWalk(db)) {
141 			tw.setRecursive(true);
142 			tw.addTree(new DirCacheIterator(dc1));
143 			tw.addTree(new DirCacheIterator(dc2));
144 			tw.setFilter(InterIndexDiffFilter.INSTANCE);
145 
146 			assertTrue(tw.next());
147 			assertEquals("a/a", tw.getPathString());
148 			assertFalse(tw.next());
149 		}
150 	}
151 
152 	@Test
153 	public void testTwoSameSameAssumeValidDifferentContent()
154 			throws IOException {
155 		DirCache dc1 = DirCache.newInCore();
156 		DirCache dc2 = DirCache.newInCore();
157 		DirCacheEditor ed1 = dc1.editor();
158 		ed1.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, true));
159 		ed1.finish();
160 		DirCacheEditor ed2 = dc2.editor();
161 		ed2.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("b"), 1, true));
162 		ed2.finish();
163 
164 		try (TreeWalk tw = new TreeWalk(db)) {
165 			tw.setRecursive(true);
166 			tw.addTree(new DirCacheIterator(dc1));
167 			tw.addTree(new DirCacheIterator(dc2));
168 			tw.setFilter(InterIndexDiffFilter.INSTANCE);
169 
170 			assertFalse(tw.next());
171 		}
172 	}
173 }