View Javadoc
1   /*
2    * Copyright (C) 2015, 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.assertTrue;
15  
16  import java.io.IOException;
17  import java.util.Set;
18  
19  import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
20  import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
21  import org.eclipse.jgit.junit.TestRepository;
22  import org.eclipse.jgit.lib.AnyObjectId;
23  import org.eclipse.jgit.lib.Sets;
24  import org.eclipse.jgit.revwalk.filter.MessageRevFilter;
25  import org.eclipse.jgit.revwalk.filter.NotRevFilter;
26  import org.eclipse.jgit.revwalk.filter.ObjectFilter;
27  import org.junit.After;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  public class ObjectWalkFilterTest {
32  	private TestRepository<InMemoryRepository> tr;
33  	private ObjectWalk rw;
34  
35  	// 3 commits, 2 top-level trees, 4 subtrees, 3 blobs
36  	private static final int OBJECT_COUNT = 12;
37  
38  	@Before
39  	public void setUp() throws Exception {
40  		tr = new TestRepository<>(new InMemoryRepository(
41  				new DfsRepositoryDescription("test")));
42  		rw = new ObjectWalk(tr.getRepository());
43  
44  		rw.markStart(tr.branch("master").commit()
45  			.add("a/a", "1")
46  			.add("b/b", "2")
47  			.add("c/c", "3")
48  			.message("initial commit")
49  
50  			.child()
51  			.rm("a/a")
52  			.add("a/A", "1")
53  			.message("capitalize a/a")
54  
55  			.child()
56  			.rm("a/A")
57  			.add("a/a", "1")
58  			.message("make a/A lowercase again")
59  			.create());
60  	}
61  
62  	@After
63  	public void tearDown() {
64  		rw.close();
65  		tr.getRepository().close();
66  	}
67  
68  	private static class BlacklistObjectFilter extends ObjectFilter {
69  		final Set<AnyObjectId> badObjects;
70  
71  		BlacklistObjectFilter(Set<AnyObjectId> badObjects) {
72  			this.badObjects = badObjects;
73  		}
74  
75  		@Override
76  		public boolean include(ObjectWalk walker, AnyObjectId o) {
77  			return !badObjects.contains(o);
78  		}
79  	}
80  
81  	private AnyObjectId resolve(String revstr) throws Exception {
82  		return tr.getRepository().resolve(revstr);
83  	}
84  
85  	private int countObjects() throws IOException {
86  		int n = 0;
87  		while (rw.next() != null) {
88  			n++;
89  		}
90  		while (rw.nextObject() != null) {
91  			n++;
92  		}
93  		return n;
94  	}
95  
96  	@Test
97  	public void testDefaultFilter() throws Exception {
98  		assertTrue("filter is ALL",
99  				rw.getObjectFilter() == ObjectFilter.ALL);
100 		assertEquals(OBJECT_COUNT, countObjects());
101 	}
102 
103 	@Test
104 	public void testObjectFilterCanFilterOutBlob() throws Exception {
105 		AnyObjectId one = rw.parseAny(resolve("master:a/a"));
106 		AnyObjectId two = rw.parseAny(resolve("master:b/b"));
107 		rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(one, two)));
108 
109 		// 2 blobs filtered out
110 		assertEquals(OBJECT_COUNT - 2, countObjects());
111 	}
112 
113 	@Test
114 	public void testFilteringCommitsHasNoEffect() throws Exception {
115 		AnyObjectId initial = rw.parseCommit(resolve("master^^"));
116 		rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(initial)));
117 		assertEquals(OBJECT_COUNT, countObjects());
118 	}
119 
120 	@Test
121 	public void testRevFilterAndObjectFilterCanCombine() throws Exception {
122 		AnyObjectId one = rw.parseAny(resolve("master:a/a"));
123 		AnyObjectId two = rw.parseAny(resolve("master:b/b"));
124 		rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(one, two)));
125 		rw.setRevFilter(NotRevFilter.create(
126 				MessageRevFilter.create("capitalize")));
127 
128 		// 2 blobs, one commit, two trees filtered out
129 		assertEquals(OBJECT_COUNT - 5, countObjects());
130 	}
131 
132 	@Test
133 	public void testFilteringTreeFiltersSubtrees() throws Exception {
134 		AnyObjectId capitalizeTree = rw.parseAny(resolve("master^:"));
135 		rw.setObjectFilter(new BlacklistObjectFilter(
136 				Sets.of(capitalizeTree)));
137 
138 		// trees "master^:" and "master^:a" filtered out
139 		assertEquals(OBJECT_COUNT - 2, countObjects());
140 	}
141 
142 	@Test
143 	public void testFilteringTreeFiltersReferencedBlobs() throws Exception {
144 		AnyObjectId a1 = rw.parseAny(resolve("master:a"));
145 		AnyObjectId a2 = rw.parseAny(resolve("master^:a"));
146 		rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(a1, a2)));
147 
148 		// 2 trees, one blob filtered out
149 		assertEquals(OBJECT_COUNT - 3, countObjects());
150 	}
151 }