View Javadoc
1   /*
2    * Copyright (C) 2014, 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.revwalk.filter.OrRevFilter;
52  import org.eclipse.jgit.revwalk.filter.RevFilter;
53  import org.eclipse.jgit.revwalk.filter.SkipRevFilter;
54  import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
55  import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
56  import org.eclipse.jgit.treewalk.filter.TreeFilter;
57  import org.junit.Test;
58  
59  public class TreeRevFilterTest extends RevWalkTestCase {
60  	private RevFilter treeRevFilter(String path) {
61  		return new TreeRevFilter(rw, treeFilter(path));
62  	}
63  
64  	private static TreeFilter treeFilter(String path) {
65  		return AndTreeFilter.create(
66  				PathFilterGroup.createFromStrings(Collections.singleton(path)),
67  				TreeFilter.ANY_DIFF);
68  	}
69  
70  	@Test
71  	public void testStringOfPearls_FilePath1()
72  			throws Exception {
73  		RevCommit a = commit(tree(file("d/f", blob("a"))));
74  		RevCommit b = commit(tree(file("d/f", blob("a"))), a);
75  		RevCommit c = commit(tree(file("d/f", blob("b"))), b);
76  		rw.setRevFilter(treeRevFilter("d/f"));
77  		markStart(c);
78  
79  		assertCommit(c, rw.next());
80  		assertEquals(1, c.getParentCount());
81  		assertCommit(b, c.getParent(0));
82  
83  		assertCommit(a, rw.next()); // b was skipped
84  		assertEquals(0, a.getParentCount());
85  		assertNull(rw.next());
86  	}
87  
88  	@Test
89  	public void testStringOfPearls_FilePath2() throws Exception {
90  		RevCommit a = commit(tree(file("d/f", blob("a"))));
91  		RevCommit b = commit(tree(file("d/f", blob("a"))), a);
92  		RevCommit c = commit(tree(file("d/f", blob("b"))), b);
93  		RevCommit d = commit(tree(file("d/f", blob("b"))), c);
94  		rw.setRevFilter(treeRevFilter("d/f"));
95  		markStart(d);
96  
97  		// d was skipped
98  		assertCommit(c, rw.next());
99  		assertEquals(1, c.getParentCount());
100 		assertCommit(b, c.getParent(0));
101 
102 		// b was skipped
103 		assertCommit(a, rw.next());
104 		assertEquals(0, a.getParentCount());
105 		assertNull(rw.next());
106 	}
107 
108 	@Test
109 	public void testStringOfPearls_DirPath2() throws Exception {
110 		RevCommit a = commit(tree(file("d/f", blob("a"))));
111 		RevCommit b = commit(tree(file("d/f", blob("a"))), a);
112 		RevCommit c = commit(tree(file("d/f", blob("b"))), b);
113 		RevCommit d = commit(tree(file("d/f", blob("b"))), c);
114 		rw.setRevFilter(treeRevFilter("d"));
115 		markStart(d);
116 
117 		// d was skipped
118 		assertCommit(c, rw.next());
119 		assertEquals(1, c.getParentCount());
120 		assertCommit(b, c.getParent(0));
121 
122 		// b was skipped
123 		assertCommit(a, rw.next());
124 		assertEquals(0, a.getParentCount());
125 		assertNull(rw.next());
126 	}
127 
128 	@Test
129 	public void testStringOfPearls_FilePath3() throws Exception {
130 		RevCommit a = commit(tree(file("d/f", blob("a"))));
131 		RevCommit b = commit(tree(file("d/f", blob("a"))), a);
132 		RevCommit c = commit(tree(file("d/f", blob("b"))), b);
133 		RevCommit d = commit(tree(file("d/f", blob("b"))), c);
134 		RevCommit e = commit(tree(file("d/f", blob("b"))), d);
135 		RevCommit f = commit(tree(file("d/f", blob("b"))), e);
136 		RevCommit g = commit(tree(file("d/f", blob("b"))), f);
137 		RevCommit h = commit(tree(file("d/f", blob("b"))), g);
138 		RevCommit i = commit(tree(file("d/f", blob("c"))), h);
139 		rw.setRevFilter(treeRevFilter("d/f"));
140 		markStart(i);
141 
142 		assertCommit(i, rw.next());
143 		assertEquals(1, i.getParentCount());
144 		assertCommit(h, i.getParent(0));
145 
146 		// h..d was skipped
147 		assertCommit(c, rw.next());
148 		assertEquals(1, c.getParentCount());
149 		assertCommit(b, c.getParent(0));
150 
151 		// b was skipped
152 		assertCommit(a, rw.next());
153 		assertEquals(0, a.getParentCount());
154 		assertNull(rw.next());
155 	}
156 
157 	@Test
158 	public void testPathFilterOrOtherFilter() throws Exception {
159 		RevFilter pathFilter = treeRevFilter("d/f");
160 		RevFilter skipFilter = SkipRevFilter.create(1);
161 		RevFilter orFilter = OrRevFilter.create(skipFilter, pathFilter);
162 
163 		RevCommit a = parseBody(commit(5, tree(file("d/f", blob("a")))));
164 		RevCommit b = parseBody(commit(5, tree(file("d/f", blob("a"))), a));
165 		RevCommit c = parseBody(commit(5, tree(file("d/f", blob("b"))), b));
166 
167 		// Path filter matches c, a.
168 		rw.setRevFilter(pathFilter);
169 		markStart(c);
170 		assertCommit(c, rw.next());
171 		assertCommit(a, rw.next());
172 
173 		// Skip filter matches b, a.
174 		rw.reset();
175 		rw.setRevFilter(skipFilter);
176 		markStart(c);
177 		assertCommit(b, rw.next());
178 		assertCommit(a, rw.next());
179 
180 		// (Path OR Skip) matches c, b, a.
181 		rw.reset();
182 		rw.setRevFilter(orFilter);
183 		markStart(c);
184 		assertCommit(c, rw.next());
185 		assertCommit(b, rw.next());
186 		assertCommit(a, rw.next());
187 	}
188 }