View Javadoc
1   /*
2    * Copyright (C) 2009-2010, 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.assertNotNull;
14  import static org.junit.Assert.assertNull;
15  
16  import java.io.IOException;
17  import java.util.Date;
18  
19  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
20  import org.eclipse.jgit.errors.MissingObjectException;
21  import org.eclipse.jgit.errors.StopWalkException;
22  import org.eclipse.jgit.revwalk.filter.AndRevFilter;
23  import org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter;
24  import org.eclipse.jgit.revwalk.filter.NotRevFilter;
25  import org.eclipse.jgit.revwalk.filter.OrRevFilter;
26  import org.eclipse.jgit.revwalk.filter.RevFilter;
27  import org.junit.Test;
28  
29  public class RevWalkFilterTest extends RevWalkTestCase {
30  	private static final MyAll MY_ALL = new MyAll();
31  
32  	@Test
33  	public void testFilter_ALL() throws Exception {
34  		final RevCommit a = commit();
35  		final RevCommit b = commit(a);
36  		final RevCommit c = commit(b);
37  
38  		rw.setRevFilter(RevFilter.ALL);
39  		markStart(c);
40  		assertCommit(c, rw.next());
41  		assertCommit(b, rw.next());
42  		assertCommit(a, rw.next());
43  		assertNull(rw.next());
44  	}
45  
46  	@Test
47  	public void testFilter_Negate_ALL() throws Exception {
48  		final RevCommit a = commit();
49  		final RevCommit b = commit(a);
50  		final RevCommit c = commit(b);
51  
52  		rw.setRevFilter(RevFilter.ALL.negate());
53  		markStart(c);
54  		assertNull(rw.next());
55  	}
56  
57  	@Test
58  	public void testFilter_NOT_ALL() throws Exception {
59  		final RevCommit a = commit();
60  		final RevCommit b = commit(a);
61  		final RevCommit c = commit(b);
62  
63  		rw.setRevFilter(NotRevFilter.create(RevFilter.ALL));
64  		markStart(c);
65  		assertNull(rw.next());
66  	}
67  
68  	@Test
69  	public void testFilter_NONE() throws Exception {
70  		final RevCommit a = commit();
71  		final RevCommit b = commit(a);
72  		final RevCommit c = commit(b);
73  
74  		rw.setRevFilter(RevFilter.NONE);
75  		markStart(c);
76  		assertNull(rw.next());
77  	}
78  
79  	@Test
80  	public void testFilter_NOT_NONE() throws Exception {
81  		final RevCommit a = commit();
82  		final RevCommit b = commit(a);
83  		final RevCommit c = commit(b);
84  
85  		rw.setRevFilter(NotRevFilter.create(RevFilter.NONE));
86  		markStart(c);
87  		assertCommit(c, rw.next());
88  		assertCommit(b, rw.next());
89  		assertCommit(a, rw.next());
90  		assertNull(rw.next());
91  	}
92  
93  	@Test
94  	public void testFilter_ALL_And_NONE() throws Exception {
95  		final RevCommit a = commit();
96  		final RevCommit b = commit(a);
97  		final RevCommit c = commit(b);
98  
99  		rw.setRevFilter(AndRevFilter.create(RevFilter.ALL, RevFilter.NONE));
100 		markStart(c);
101 		assertNull(rw.next());
102 	}
103 
104 	@Test
105 	public void testFilter_NONE_And_ALL() throws Exception {
106 		final RevCommit a = commit();
107 		final RevCommit b = commit(a);
108 		final RevCommit c = commit(b);
109 
110 		rw.setRevFilter(AndRevFilter.create(RevFilter.NONE, RevFilter.ALL));
111 		markStart(c);
112 		assertNull(rw.next());
113 	}
114 
115 	@Test
116 	public void testFilter_ALL_Or_NONE() throws Exception {
117 		final RevCommit a = commit();
118 		final RevCommit b = commit(a);
119 		final RevCommit c = commit(b);
120 
121 		rw.setRevFilter(OrRevFilter.create(RevFilter.ALL, RevFilter.NONE));
122 		markStart(c);
123 		assertCommit(c, rw.next());
124 		assertCommit(b, rw.next());
125 		assertCommit(a, rw.next());
126 		assertNull(rw.next());
127 	}
128 
129 	@Test
130 	public void testFilter_NONE_Or_ALL() throws Exception {
131 		final RevCommit a = commit();
132 		final RevCommit b = commit(a);
133 		final RevCommit c = commit(b);
134 
135 		rw.setRevFilter(OrRevFilter.create(RevFilter.NONE, RevFilter.ALL));
136 		markStart(c);
137 		assertCommit(c, rw.next());
138 		assertCommit(b, rw.next());
139 		assertCommit(a, rw.next());
140 		assertNull(rw.next());
141 	}
142 
143 	@Test
144 	public void testFilter_MY_ALL_And_NONE() throws Exception {
145 		final RevCommit a = commit();
146 		final RevCommit b = commit(a);
147 		final RevCommit c = commit(b);
148 
149 		rw.setRevFilter(AndRevFilter.create(MY_ALL, RevFilter.NONE));
150 		markStart(c);
151 		assertNull(rw.next());
152 	}
153 
154 	@Test
155 	public void testFilter_NONE_And_MY_ALL() throws Exception {
156 		final RevCommit a = commit();
157 		final RevCommit b = commit(a);
158 		final RevCommit c = commit(b);
159 
160 		rw.setRevFilter(AndRevFilter.create(RevFilter.NONE, MY_ALL));
161 		markStart(c);
162 		assertNull(rw.next());
163 	}
164 
165 	@Test
166 	public void testFilter_MY_ALL_Or_NONE() throws Exception {
167 		final RevCommit a = commit();
168 		final RevCommit b = commit(a);
169 		final RevCommit c = commit(b);
170 
171 		rw.setRevFilter(OrRevFilter.create(MY_ALL, RevFilter.NONE));
172 		markStart(c);
173 		assertCommit(c, rw.next());
174 		assertCommit(b, rw.next());
175 		assertCommit(a, rw.next());
176 		assertNull(rw.next());
177 	}
178 
179 	@Test
180 	public void testFilter_NONE_Or_MY_ALL() throws Exception {
181 		final RevCommit a = commit();
182 		final RevCommit b = commit(a);
183 		final RevCommit c = commit(b);
184 
185 		rw.setRevFilter(OrRevFilter.create(RevFilter.NONE, MY_ALL));
186 		markStart(c);
187 		assertCommit(c, rw.next());
188 		assertCommit(b, rw.next());
189 		assertCommit(a, rw.next());
190 		assertNull(rw.next());
191 	}
192 
193 	@Test
194 	public void testFilter_NO_MERGES() throws Exception {
195 		final RevCommit a = commit();
196 		final RevCommit b = commit(a);
197 		final RevCommit c1 = commit(b);
198 		final RevCommit c2 = commit(b);
199 		final RevCommit d = commit(c1, c2);
200 		final RevCommit e = commit(d);
201 
202 		rw.setRevFilter(RevFilter.NO_MERGES);
203 		markStart(e);
204 		assertCommit(e, rw.next());
205 		assertCommit(c2, rw.next());
206 		assertCommit(c1, rw.next());
207 		assertCommit(b, rw.next());
208 		assertCommit(a, rw.next());
209 		assertNull(rw.next());
210 	}
211 
212 	@Test
213 	public void testCommitTimeRevFilter() throws Exception {
214 		final RevCommit a = commit();
215 		tick(100);
216 
217 		final RevCommit b = commit(a);
218 		tick(100);
219 
220 		Date since = getDate();
221 		final RevCommit c1 = commit(b);
222 		tick(100);
223 
224 		final RevCommit c2 = commit(b);
225 		tick(100);
226 
227 		Date until = getDate();
228 		final RevCommit d = commit(c1, c2);
229 		tick(100);
230 
231 		final RevCommit e = commit(d);
232 
233 		{
234 			RevFilter after = CommitTimeRevFilter.after(since);
235 			assertNotNull(after);
236 			rw.setRevFilter(after);
237 			markStart(e);
238 			assertCommit(e, rw.next());
239 			assertCommit(d, rw.next());
240 			assertCommit(c2, rw.next());
241 			assertCommit(c1, rw.next());
242 			assertNull(rw.next());
243 		}
244 
245 		{
246 			RevFilter before = CommitTimeRevFilter.before(until);
247 			assertNotNull(before);
248 			rw.reset();
249 			rw.setRevFilter(before);
250 			markStart(e);
251 			assertCommit(c2, rw.next());
252 			assertCommit(c1, rw.next());
253 			assertCommit(b, rw.next());
254 			assertCommit(a, rw.next());
255 			assertNull(rw.next());
256 		}
257 
258 		{
259 			RevFilter between = CommitTimeRevFilter.between(since, until);
260 			assertNotNull(between);
261 			rw.reset();
262 			rw.setRevFilter(between);
263 			markStart(e);
264 			assertCommit(c2, rw.next());
265 			assertCommit(c1, rw.next());
266 			assertNull(rw.next());
267 		}
268 	}
269 
270 	private static class MyAll extends RevFilter {
271 		@Override
272 		public RevFilter clone() {
273 			return this;
274 		}
275 
276 		@Override
277 		public boolean include(RevWalk walker, RevCommit cmit)
278 				throws StopWalkException, MissingObjectException,
279 				IncorrectObjectTypeException, IOException {
280 			return true;
281 		}
282 
283 		@Override
284 		public boolean requiresCommitBody() {
285 			return false;
286 		}
287 	}
288 }