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.assertEquals;
47 import static org.junit.Assert.assertTrue;
48
49 import java.io.IOException;
50 import java.util.Set;
51
52 import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
53 import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
54 import org.eclipse.jgit.junit.TestRepository;
55 import org.eclipse.jgit.lib.AnyObjectId;
56 import org.eclipse.jgit.lib.Sets;
57 import org.eclipse.jgit.revwalk.filter.MessageRevFilter;
58 import org.eclipse.jgit.revwalk.filter.NotRevFilter;
59 import org.eclipse.jgit.revwalk.filter.ObjectFilter;
60 import org.junit.After;
61 import org.junit.Before;
62 import org.junit.Test;
63
64 public class ObjectWalkFilterTest {
65 private TestRepository<InMemoryRepository> tr;
66 private ObjectWalk rw;
67
68
69 private static final int OBJECT_COUNT = 12;
70
71 @Before
72 public void setUp() throws Exception {
73 tr = new TestRepository<>(new InMemoryRepository(
74 new DfsRepositoryDescription("test")));
75 rw = new ObjectWalk(tr.getRepository());
76
77 rw.markStart(tr.branch("master").commit()
78 .add("a/a", "1")
79 .add("b/b", "2")
80 .add("c/c", "3")
81 .message("initial commit")
82
83 .child()
84 .rm("a/a")
85 .add("a/A", "1")
86 .message("capitalize a/a")
87
88 .child()
89 .rm("a/A")
90 .add("a/a", "1")
91 .message("make a/A lowercase again")
92 .create());
93 }
94
95 @After
96 public void tearDown() {
97 rw.close();
98 tr.getRepository().close();
99 }
100
101 private static class BlacklistObjectFilter extends ObjectFilter {
102 final Set<AnyObjectId> badObjects;
103
104 BlacklistObjectFilter(Set<AnyObjectId> badObjects) {
105 this.badObjects = badObjects;
106 }
107
108 @Override
109 public boolean include(ObjectWalk walker, AnyObjectId o) {
110 return !badObjects.contains(o);
111 }
112 }
113
114 private AnyObjectId resolve(String revstr) throws Exception {
115 return tr.getRepository().resolve(revstr);
116 }
117
118 private int countObjects() throws IOException {
119 int n = 0;
120 while (rw.next() != null) {
121 n++;
122 }
123 while (rw.nextObject() != null) {
124 n++;
125 }
126 return n;
127 }
128
129 @Test
130 public void testDefaultFilter() throws Exception {
131 assertTrue("filter is ALL",
132 rw.getObjectFilter() == ObjectFilter.ALL);
133 assertEquals(OBJECT_COUNT, countObjects());
134 }
135
136 @Test
137 public void testObjectFilterCanFilterOutBlob() throws Exception {
138 AnyObjectId one = rw.parseAny(resolve("master:a/a"));
139 AnyObjectId two = rw.parseAny(resolve("master:b/b"));
140 rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(one, two)));
141
142
143 assertEquals(OBJECT_COUNT - 2, countObjects());
144 }
145
146 @Test
147 public void testFilteringCommitsHasNoEffect() throws Exception {
148 AnyObjectId initial = rw.parseCommit(resolve("master^^"));
149 rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(initial)));
150 assertEquals(OBJECT_COUNT, countObjects());
151 }
152
153 @Test
154 public void testRevFilterAndObjectFilterCanCombine() throws Exception {
155 AnyObjectId one = rw.parseAny(resolve("master:a/a"));
156 AnyObjectId two = rw.parseAny(resolve("master:b/b"));
157 rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(one, two)));
158 rw.setRevFilter(NotRevFilter.create(
159 MessageRevFilter.create("capitalize")));
160
161
162 assertEquals(OBJECT_COUNT - 5, countObjects());
163 }
164
165 @Test
166 public void testFilteringTreeFiltersSubtrees() throws Exception {
167 AnyObjectId capitalizeTree = rw.parseAny(resolve("master^:"));
168 rw.setObjectFilter(new BlacklistObjectFilter(
169 Sets.of(capitalizeTree)));
170
171
172 assertEquals(OBJECT_COUNT - 2, countObjects());
173 }
174
175 @Test
176 public void testFilteringTreeFiltersReferencedBlobs() throws Exception {
177 AnyObjectId a1 = rw.parseAny(resolve("master:a"));
178 AnyObjectId a2 = rw.parseAny(resolve("master^:a"));
179 rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(a1, a2)));
180
181
182 assertEquals(OBJECT_COUNT - 3, countObjects());
183 }
184 }