View Javadoc
1   /*
2    * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.treewalk.filter;
46  
47  import java.util.Collection;
48  
49  import org.eclipse.jgit.errors.StopWalkException;
50  import org.eclipse.jgit.internal.JGitText;
51  import org.eclipse.jgit.treewalk.TreeWalk;
52  import org.eclipse.jgit.treewalk.filter.ByteArraySet.Hasher;
53  import org.eclipse.jgit.util.RawParseUtils;
54  
55  /**
56   * Includes tree entries only if they match one or more configured paths.
57   * <p>
58   * Operates like {@link PathFilter} but causes the walk to abort as soon as the
59   * tree can no longer match any of the paths within the group. This may bypass
60   * the boolean logic of a higher level AND or OR group, but does improve
61   * performance for the common case of examining one or more modified paths.
62   * <p>
63   * This filter is effectively an OR group around paths, with the early abort
64   * feature described above.
65   */
66  public class PathFilterGroup {
67  	/**
68  	 * Create a collection of path filters from Java strings.
69  	 * <p>
70  	 * Path strings are relative to the root of the repository. If the user's
71  	 * input should be assumed relative to a subdirectory of the repository the
72  	 * caller must prepend the subdirectory's path prior to creating the filter.
73  	 * <p>
74  	 * Path strings use '/' to delimit directories on all platforms.
75  	 * <p>
76  	 * Paths may appear in any order within the collection. Sorting may be done
77  	 * internally when the group is constructed if doing so will improve path
78  	 * matching performance.
79  	 *
80  	 * @param paths
81  	 *            the paths to test against. Must have at least one entry.
82  	 * @return a new filter for the list of paths supplied.
83  	 */
84  	public static TreeFilter createFromStrings(final Collection<String> paths) {
85  		if (paths.isEmpty())
86  			throw new IllegalArgumentException(
87  					JGitText.get().atLeastOnePathIsRequired);
88  		final PathFilter[] p = new PathFilter[paths.size()];
89  		int i = 0;
90  		for (final String s : paths)
91  			p[i++] = PathFilter.create(s);
92  		return create(p);
93  	}
94  
95  	/**
96  	 * Create a collection of path filters from Java strings.
97  	 * <p>
98  	 * Path strings are relative to the root of the repository. If the user's
99  	 * input should be assumed relative to a subdirectory of the repository the
100 	 * caller must prepend the subdirectory's path prior to creating the filter.
101 	 * <p>
102 	 * Path strings use '/' to delimit directories on all platforms.
103 	 * <p>
104 	 * Paths may appear in any order. Sorting may be done internally when the
105 	 * group is constructed if doing so will improve path matching performance.
106 	 *
107 	 * @param paths
108 	 *            the paths to test against. Must have at least one entry.
109 	 * @return a new filter for the paths supplied.
110 	 */
111 	public static TreeFilter createFromStrings(final String... paths) {
112 		if (paths.length == 0)
113 			throw new IllegalArgumentException(
114 					JGitText.get().atLeastOnePathIsRequired);
115 		final int length = paths.length;
116 		final PathFilter[] p = new PathFilter[length];
117 		for (int i = 0; i < length; i++)
118 			p[i] = PathFilter.create(paths[i]);
119 		return create(p);
120 	}
121 
122 	/**
123 	 * Create a collection of path filters.
124 	 * <p>
125 	 * Paths may appear in any order within the collection. Sorting may be done
126 	 * internally when the group is constructed if doing so will improve path
127 	 * matching performance.
128 	 *
129 	 * @param paths
130 	 *            the paths to test against. Must have at least one entry.
131 	 * @return a new filter for the list of paths supplied.
132 	 */
133 	public static TreeFilter create(final Collection<PathFilter> paths) {
134 		if (paths.isEmpty())
135 			throw new IllegalArgumentException(
136 					JGitText.get().atLeastOnePathIsRequired);
137 		final PathFilter[] p = new PathFilter[paths.size()];
138 		paths.toArray(p);
139 		return create(p);
140 	}
141 
142 	private static TreeFilter create(final PathFilter[] p) {
143 		if (p.length == 1)
144 			return new Single(p[0]);
145 		return new Group(p);
146 	}
147 
148 	static class Single extends TreeFilter {
149 		private final PathFilter path;
150 
151 		private final byte[] raw;
152 
153 		private Single(final PathFilter p) {
154 			path = p;
155 			raw = path.pathRaw;
156 		}
157 
158 		@Override
159 		public boolean include(final TreeWalk walker) {
160 			final int cmp = walker.isPathPrefix(raw, raw.length);
161 			if (cmp > 0)
162 				throw StopWalkException.INSTANCE;
163 			return cmp == 0;
164 		}
165 
166 		@Override
167 		public boolean shouldBeRecursive() {
168 			return path.shouldBeRecursive();
169 		}
170 
171 		@Override
172 		public TreeFilter clone() {
173 			return this;
174 		}
175 
176 		@Override
177 		public String toString() {
178 			return "FAST_" + path.toString(); //$NON-NLS-1$
179 		}
180 	}
181 
182 	static class Group extends TreeFilter {
183 
184 		private ByteArraySet fullpaths;
185 
186 		private ByteArraySet prefixes;
187 
188 		private byte[] max;
189 
190 		private Group(final PathFilter[] pathFilters) {
191 			fullpaths = new ByteArraySet(pathFilters.length);
192 			prefixes = new ByteArraySet(pathFilters.length / 5);
193 			// 5 is an empirically derived ratio of #paths/#prefixes from:
194 			// egit/jgit: 8
195 			// git: 5
196 			// linux kernel: 13
197 			// eclipse.platform.ui: 7
198 			max = pathFilters[0].pathRaw;
199 			Hasher hasher = new Hasher(null, 0);
200 			for (PathFilter pf : pathFilters) {
201 				hasher.init(pf.pathRaw, pf.pathRaw.length);
202 				while (hasher.hasNext()) {
203 					int hash = hasher.nextHash();
204 					if (hasher.hasNext())
205 						prefixes.addIfAbsent(pf.pathRaw, hasher.length(), hash);
206 				}
207 				fullpaths.addIfAbsent(pf.pathRaw, pf.pathRaw.length,
208 						hasher.getHash());
209 				if (compare(max, pf.pathRaw) < 0)
210 					max = pf.pathRaw;
211 			}
212 			// Adjust max for the git sort order. A path we compare
213 			// with may end with a slash at any position (but the
214 			// first, but we ignore that here since it's not relevant).
215 			// Such paths must be included in the processing
216 			// before we can give up and throw a StopWalkException.
217 			byte[] newMax = new byte[max.length + 1];
218 			for (int i = 0; i < max.length; ++i)
219 				if ((max[i] & 0xFF) < '/')
220 					newMax[i] = '/';
221 				else
222 					newMax[i] = max[i];
223 			newMax[newMax.length - 1] = '/';
224 			max = newMax;
225 		}
226 
227 		private static int compare(byte[] a, byte[] b) {
228 			int i = 0;
229 			while (i < a.length && i < b.length) {
230 				int ba = a[i] & 0xFF;
231 				int bb = b[i] & 0xFF;
232 				int cmp = ba - bb;
233 				if (cmp != 0)
234 					return cmp;
235 				++i;
236 			}
237 			return a.length - b.length;
238 		}
239 
240 		@Override
241 		public boolean include(final TreeWalk walker) {
242 
243 			byte[] rp = walker.getRawPath();
244 			Hasher hasher = new Hasher(rp, walker.getPathLength());
245 			while (hasher.hasNext()) {
246 				int hash = hasher.nextHash();
247 				if (fullpaths.contains(rp, hasher.length(), hash))
248 					return true;
249 				if (!hasher.hasNext() && walker.isSubtree()
250 						&& prefixes.contains(rp, hasher.length(), hash))
251 					return true;
252 			}
253 
254 			final int cmp = walker.isPathPrefix(max, max.length);
255 			if (cmp > 0)
256 				throw StopWalkException.INSTANCE;
257 
258 			return false;
259 		}
260 
261 		@Override
262 		public boolean shouldBeRecursive() {
263 			return !prefixes.isEmpty();
264 		}
265 
266 		@Override
267 		public TreeFilter clone() {
268 			return this;
269 		}
270 
271 		@Override
272 		public String toString() {
273 			final StringBuilder r = new StringBuilder();
274 			r.append("FAST("); //$NON-NLS-1$
275 			boolean first = true;
276 			for (byte[] p : fullpaths.toArray()) {
277 				if (!first) {
278 					r.append(" OR "); //$NON-NLS-1$
279 				}
280 				r.append(RawParseUtils.decode(p));
281 				first = false;
282 			}
283 			r.append(")"); //$NON-NLS-1$
284 			return r.toString();
285 		}
286 	}
287 
288 }