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