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.assertEquals;
14  import static org.junit.Assert.assertNull;
15  
16  import java.util.Collections;
17  
18  import org.eclipse.jgit.dircache.DirCacheEntry;
19  import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
20  import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
21  import org.eclipse.jgit.treewalk.filter.TreeFilter;
22  import org.junit.Test;
23  
24  public class RevWalkPathFilter1Test extends RevWalkTestCase {
25  	protected void filter(String path) {
26  		rw.setTreeFilter(AndTreeFilter.create(PathFilterGroup
27  				.createFromStrings(Collections.singleton(path)),
28  				TreeFilter.ANY_DIFF));
29  	}
30  
31  	@Test
32  	public void testEmpty_EmptyTree() throws Exception {
33  		final RevCommit a = commit();
34  		filter("a");
35  		markStart(a);
36  		assertNull(rw.next());
37  	}
38  
39  	@Test
40  	public void testEmpty_NoMatch() throws Exception {
41  		final RevCommit a = commit(tree(file("0", blob("0"))));
42  		filter("a");
43  		markStart(a);
44  		assertNull(rw.next());
45  	}
46  
47  	@Test
48  	public void testSimple1() throws Exception {
49  		final RevCommit a = commit(tree(file("0", blob("0"))));
50  		filter("0");
51  		markStart(a);
52  		assertCommit(a, rw.next());
53  		assertNull(rw.next());
54  	}
55  
56  	@Test
57  	public void testEdits_MatchNone() throws Exception {
58  		final RevCommit a = commit(tree(file("0", blob("a"))));
59  		final RevCommit b = commit(tree(file("0", blob("b"))), a);
60  		final RevCommit c = commit(tree(file("0", blob("c"))), b);
61  		final RevCommit d = commit(tree(file("0", blob("d"))), c);
62  		filter("a");
63  		markStart(d);
64  		assertNull(rw.next());
65  	}
66  
67  	@Test
68  	public void testEdits_MatchAll() throws Exception {
69  		final RevCommit a = commit(tree(file("0", blob("a"))));
70  		final RevCommit b = commit(tree(file("0", blob("b"))), a);
71  		final RevCommit c = commit(tree(file("0", blob("c"))), b);
72  		final RevCommit d = commit(tree(file("0", blob("d"))), c);
73  		filter("0");
74  		markStart(d);
75  		assertCommit(d, rw.next());
76  		assertCommit(c, rw.next());
77  		assertCommit(b, rw.next());
78  		assertCommit(a, rw.next());
79  		assertNull(rw.next());
80  	}
81  
82  	@Test
83  	public void testStringOfPearls_FilePath1() throws Exception {
84  		final RevCommit a = commit(tree(file("d/f", blob("a"))));
85  		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
86  		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
87  		filter("d/f");
88  		markStart(c);
89  
90  		assertCommit(c, rw.next());
91  		assertEquals(1, c.getParentCount());
92  		assertCommit(a, c.getParent(0)); // b was skipped
93  
94  		assertCommit(a, rw.next());
95  		assertEquals(0, a.getParentCount());
96  		assertNull(rw.next());
97  	}
98  
99  	@Test
100 	public void testStringOfPearls_FilePath1_NoParentRewriting()
101 			throws Exception {
102 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
103 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
104 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
105 		filter("d/f");
106 		markStart(c);
107 		rw.setRewriteParents(false);
108 
109 		assertCommit(c, rw.next());
110 		assertEquals(1, c.getParentCount());
111 		assertCommit(b, c.getParent(0));
112 
113 		assertCommit(a, rw.next()); // b was skipped
114 		assertEquals(0, a.getParentCount());
115 		assertNull(rw.next());
116 	}
117 
118 	@Test
119 	public void testStringOfPearls_FilePath2() throws Exception {
120 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
121 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
122 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
123 		final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
124 		filter("d/f");
125 		markStart(d);
126 
127 		// d was skipped
128 		assertCommit(c, rw.next());
129 		assertEquals(1, c.getParentCount());
130 		assertCommit(a, c.getParent(0)); // b was skipped
131 
132 		assertCommit(a, rw.next());
133 		assertEquals(0, a.getParentCount());
134 		assertNull(rw.next());
135 	}
136 
137 	@Test
138 	public void testStringOfPearls_FilePath2_NoParentRewriting()
139 	throws Exception {
140 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
141 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
142 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
143 		final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
144 		filter("d/f");
145 		markStart(d);
146 		rw.setRewriteParents(false);
147 
148 		// d was skipped
149 		assertCommit(c, rw.next());
150 		assertEquals(1, c.getParentCount());
151 		assertCommit(b, c.getParent(0));
152 
153 		// b was skipped
154 		assertCommit(a, rw.next());
155 		assertEquals(0, a.getParentCount());
156 		assertNull(rw.next());
157 	}
158 
159 	@Test
160 	public void testStringOfPearls_DirPath2() throws Exception {
161 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
162 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
163 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
164 		final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
165 		filter("d");
166 		markStart(d);
167 
168 		// d was skipped
169 		assertCommit(c, rw.next());
170 		assertEquals(1, c.getParentCount());
171 		assertCommit(a, c.getParent(0)); // b was skipped
172 
173 		assertCommit(a, rw.next());
174 		assertEquals(0, a.getParentCount());
175 		assertNull(rw.next());
176 	}
177 
178 	@Test
179 	public void testStringOfPearls_DirPath2_NoParentRewriting()
180 			throws Exception {
181 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
182 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
183 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
184 		final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
185 		filter("d");
186 		markStart(d);
187 		rw.setRewriteParents(false);
188 
189 		// d was skipped
190 		assertCommit(c, rw.next());
191 		assertEquals(1, c.getParentCount());
192 		assertCommit(b, c.getParent(0));
193 
194 		// b was skipped
195 		assertCommit(a, rw.next());
196 		assertEquals(0, a.getParentCount());
197 		assertNull(rw.next());
198 	}
199 
200 	@Test
201 	public void testStringOfPearls_FilePath3() throws Exception {
202 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
203 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
204 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
205 		final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
206 		final RevCommit e = commit(tree(file("d/f", blob("b"))), d);
207 		final RevCommit f = commit(tree(file("d/f", blob("b"))), e);
208 		final RevCommit g = commit(tree(file("d/f", blob("b"))), f);
209 		final RevCommit h = commit(tree(file("d/f", blob("b"))), g);
210 		final RevCommit i = commit(tree(file("d/f", blob("c"))), h);
211 		filter("d/f");
212 		markStart(i);
213 
214 		assertCommit(i, rw.next());
215 		assertEquals(1, i.getParentCount());
216 		assertCommit(c, i.getParent(0)); // h..d was skipped
217 
218 		assertCommit(c, rw.next());
219 		assertEquals(1, c.getParentCount());
220 		assertCommit(a, c.getParent(0)); // b was skipped
221 
222 		assertCommit(a, rw.next());
223 		assertEquals(0, a.getParentCount());
224 		assertNull(rw.next());
225 	}
226 
227 	@Test
228 	public void testStringOfPearls_FilePath3_NoParentRewriting()
229 			throws Exception {
230 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
231 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
232 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
233 		final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
234 		final RevCommit e = commit(tree(file("d/f", blob("b"))), d);
235 		final RevCommit f = commit(tree(file("d/f", blob("b"))), e);
236 		final RevCommit g = commit(tree(file("d/f", blob("b"))), f);
237 		final RevCommit h = commit(tree(file("d/f", blob("b"))), g);
238 		final RevCommit i = commit(tree(file("d/f", blob("c"))), h);
239 		filter("d/f");
240 		markStart(i);
241 		rw.setRewriteParents(false);
242 
243 		assertCommit(i, rw.next());
244 		assertEquals(1, i.getParentCount());
245 		assertCommit(h, i.getParent(0));
246 
247 		// h..d was skipped
248 		assertCommit(c, rw.next());
249 		assertEquals(1, c.getParentCount());
250 		assertCommit(b, c.getParent(0));
251 
252 		// b was skipped
253 		assertCommit(a, rw.next());
254 		assertEquals(0, a.getParentCount());
255 		assertNull(rw.next());
256 	}
257 
258 	@Test
259 	public void testStopWhenPathDisappears() throws Exception {
260 		DirCacheEntry file1 = file("src/d1/file1", blob("a"));
261 		DirCacheEntry file2 = file("src/d1/file2", blob("a"));
262 		DirCacheEntry file3 = file("src/d1/file3", blob("a"));
263 		RevCommit a = commit(tree(file1));
264 		RevCommit b = commit(tree(file1, file2), a);
265 		RevCommit c = commit(tree(file1, file3), a);
266 		RevCommit d = commit(tree(file1, file2, file3), b, c);
267 		filter("src/d1");
268 		markStart(d);
269 		rw.setRewriteParents(false);
270 
271 		assertCommit(d, rw.next());
272 		assertCommit(c, rw.next());
273 		assertCommit(b, rw.next());
274 		assertCommit(a, rw.next());
275 	}
276 }