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