View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.revwalk;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertNull;
48  
49  import java.util.Collections;
50  
51  import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
52  import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
53  import org.eclipse.jgit.treewalk.filter.TreeFilter;
54  import org.junit.Test;
55  
56  public class RevWalkPathFilter1Test extends RevWalkTestCase {
57  	protected void filter(final String path) {
58  		rw.setTreeFilter(AndTreeFilter.create(PathFilterGroup
59  				.createFromStrings(Collections.singleton(path)),
60  				TreeFilter.ANY_DIFF));
61  	}
62  
63  	@Test
64  	public void testEmpty_EmptyTree() throws Exception {
65  		final RevCommit a = commit();
66  		filter("a");
67  		markStart(a);
68  		assertNull(rw.next());
69  	}
70  
71  	@Test
72  	public void testEmpty_NoMatch() throws Exception {
73  		final RevCommit a = commit(tree(file("0", blob("0"))));
74  		filter("a");
75  		markStart(a);
76  		assertNull(rw.next());
77  	}
78  
79  	@Test
80  	public void testSimple1() throws Exception {
81  		final RevCommit a = commit(tree(file("0", blob("0"))));
82  		filter("0");
83  		markStart(a);
84  		assertCommit(a, rw.next());
85  		assertNull(rw.next());
86  	}
87  
88  	@Test
89  	public void testEdits_MatchNone() throws Exception {
90  		final RevCommit a = commit(tree(file("0", blob("a"))));
91  		final RevCommit b = commit(tree(file("0", blob("b"))), a);
92  		final RevCommit c = commit(tree(file("0", blob("c"))), b);
93  		final RevCommit d = commit(tree(file("0", blob("d"))), c);
94  		filter("a");
95  		markStart(d);
96  		assertNull(rw.next());
97  	}
98  
99  	@Test
100 	public void testEdits_MatchAll() throws Exception {
101 		final RevCommit a = commit(tree(file("0", blob("a"))));
102 		final RevCommit b = commit(tree(file("0", blob("b"))), a);
103 		final RevCommit c = commit(tree(file("0", blob("c"))), b);
104 		final RevCommit d = commit(tree(file("0", blob("d"))), c);
105 		filter("0");
106 		markStart(d);
107 		assertCommit(d, rw.next());
108 		assertCommit(c, rw.next());
109 		assertCommit(b, rw.next());
110 		assertCommit(a, rw.next());
111 		assertNull(rw.next());
112 	}
113 
114 	@Test
115 	public void testStringOfPearls_FilePath1() throws Exception {
116 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
117 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
118 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
119 		filter("d/f");
120 		markStart(c);
121 
122 		assertCommit(c, rw.next());
123 		assertEquals(1, c.getParentCount());
124 		assertCommit(a, c.getParent(0)); // b was skipped
125 
126 		assertCommit(a, rw.next());
127 		assertEquals(0, a.getParentCount());
128 		assertNull(rw.next());
129 	}
130 
131 	@Test
132 	public void testStringOfPearls_FilePath1_NoParentRewriting()
133 			throws Exception {
134 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
135 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
136 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
137 		filter("d/f");
138 		markStart(c);
139 		rw.setRewriteParents(false);
140 
141 		assertCommit(c, rw.next());
142 		assertEquals(1, c.getParentCount());
143 		assertCommit(b, c.getParent(0));
144 
145 		assertCommit(a, rw.next()); // b was skipped
146 		assertEquals(0, a.getParentCount());
147 		assertNull(rw.next());
148 	}
149 
150 	@Test
151 	public void testStringOfPearls_FilePath2() throws Exception {
152 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
153 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
154 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
155 		final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
156 		filter("d/f");
157 		markStart(d);
158 
159 		// d was skipped
160 		assertCommit(c, rw.next());
161 		assertEquals(1, c.getParentCount());
162 		assertCommit(a, c.getParent(0)); // b was skipped
163 
164 		assertCommit(a, rw.next());
165 		assertEquals(0, a.getParentCount());
166 		assertNull(rw.next());
167 	}
168 
169 	@Test
170 	public void testStringOfPearls_FilePath2_NoParentRewriting()
171 	throws Exception {
172 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
173 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
174 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
175 		final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
176 		filter("d/f");
177 		markStart(d);
178 		rw.setRewriteParents(false);
179 
180 		// d was skipped
181 		assertCommit(c, rw.next());
182 		assertEquals(1, c.getParentCount());
183 		assertCommit(b, c.getParent(0));
184 
185 		// b was skipped
186 		assertCommit(a, rw.next());
187 		assertEquals(0, a.getParentCount());
188 		assertNull(rw.next());
189 	}
190 
191 	@Test
192 	public void testStringOfPearls_DirPath2() throws Exception {
193 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
194 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
195 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
196 		final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
197 		filter("d");
198 		markStart(d);
199 
200 		// d was skipped
201 		assertCommit(c, rw.next());
202 		assertEquals(1, c.getParentCount());
203 		assertCommit(a, c.getParent(0)); // b was skipped
204 
205 		assertCommit(a, rw.next());
206 		assertEquals(0, a.getParentCount());
207 		assertNull(rw.next());
208 	}
209 
210 	@Test
211 	public void testStringOfPearls_DirPath2_NoParentRewriting()
212 			throws Exception {
213 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
214 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
215 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
216 		final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
217 		filter("d");
218 		markStart(d);
219 		rw.setRewriteParents(false);
220 
221 		// d was skipped
222 		assertCommit(c, rw.next());
223 		assertEquals(1, c.getParentCount());
224 		assertCommit(b, c.getParent(0));
225 
226 		// b was skipped
227 		assertCommit(a, rw.next());
228 		assertEquals(0, a.getParentCount());
229 		assertNull(rw.next());
230 	}
231 
232 	@Test
233 	public void testStringOfPearls_FilePath3() throws Exception {
234 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
235 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
236 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
237 		final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
238 		final RevCommit e = commit(tree(file("d/f", blob("b"))), d);
239 		final RevCommit f = commit(tree(file("d/f", blob("b"))), e);
240 		final RevCommit g = commit(tree(file("d/f", blob("b"))), f);
241 		final RevCommit h = commit(tree(file("d/f", blob("b"))), g);
242 		final RevCommit i = commit(tree(file("d/f", blob("c"))), h);
243 		filter("d/f");
244 		markStart(i);
245 
246 		assertCommit(i, rw.next());
247 		assertEquals(1, i.getParentCount());
248 		assertCommit(c, i.getParent(0)); // h..d was skipped
249 
250 		assertCommit(c, rw.next());
251 		assertEquals(1, c.getParentCount());
252 		assertCommit(a, c.getParent(0)); // b was skipped
253 
254 		assertCommit(a, rw.next());
255 		assertEquals(0, a.getParentCount());
256 		assertNull(rw.next());
257 	}
258 
259 	@Test
260 	public void testStringOfPearls_FilePath3_NoParentRewriting()
261 			throws Exception {
262 		final RevCommit a = commit(tree(file("d/f", blob("a"))));
263 		final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
264 		final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
265 		final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
266 		final RevCommit e = commit(tree(file("d/f", blob("b"))), d);
267 		final RevCommit f = commit(tree(file("d/f", blob("b"))), e);
268 		final RevCommit g = commit(tree(file("d/f", blob("b"))), f);
269 		final RevCommit h = commit(tree(file("d/f", blob("b"))), g);
270 		final RevCommit i = commit(tree(file("d/f", blob("c"))), h);
271 		filter("d/f");
272 		markStart(i);
273 		rw.setRewriteParents(false);
274 
275 		assertCommit(i, rw.next());
276 		assertEquals(1, i.getParentCount());
277 		assertCommit(h, i.getParent(0));
278 
279 		// h..d was skipped
280 		assertCommit(c, rw.next());
281 		assertEquals(1, c.getParentCount());
282 		assertCommit(b, c.getParent(0));
283 
284 		// b was skipped
285 		assertCommit(a, rw.next());
286 		assertEquals(0, a.getParentCount());
287 		assertNull(rw.next());
288 	}
289 }