View Javadoc
1   /*
2    * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.treewalk.filter;
13  
14  import java.util.Collection;
15  
16  import org.eclipse.jgit.errors.StopWalkException;
17  import org.eclipse.jgit.internal.JGitText;
18  import org.eclipse.jgit.treewalk.TreeWalk;
19  import org.eclipse.jgit.treewalk.filter.ByteArraySet.Hasher;
20  import org.eclipse.jgit.util.RawParseUtils;
21  
22  /**
23   * Includes tree entries only if they match one or more configured paths.
24   * <p>
25   * Operates like {@link org.eclipse.jgit.treewalk.filter.PathFilter} but causes
26   * the walk to abort as soon as the tree can no longer match any of the paths
27   * within the group. This may bypass the boolean logic of a higher level AND or
28   * OR group, but does improve performance for the common case of examining one
29   * or more modified paths.
30   * <p>
31   * This filter is effectively an OR group around paths, with the early abort
32   * feature described above.
33   */
34  public class PathFilterGroup {
35  	/**
36  	 * Create a collection of path filters from Java strings.
37  	 * <p>
38  	 * Path strings are relative to the root of the repository. If the user's
39  	 * input should be assumed relative to a subdirectory of the repository the
40  	 * caller must prepend the subdirectory's path prior to creating the filter.
41  	 * <p>
42  	 * Path strings use '/' to delimit directories on all platforms.
43  	 * <p>
44  	 * Paths may appear in any order within the collection. Sorting may be done
45  	 * internally when the group is constructed if doing so will improve path
46  	 * matching performance.
47  	 *
48  	 * @param paths
49  	 *            the paths to test against. Must have at least one entry.
50  	 * @return a new filter for the list of paths supplied.
51  	 */
52  	public static TreeFilter createFromStrings(Collection<String> paths) {
53  		if (paths.isEmpty())
54  			throw new IllegalArgumentException(
55  					JGitText.get().atLeastOnePathIsRequired);
56  		final PathFilterfilter/PathFilter.html#PathFilter">PathFilter[] p = new PathFilter[paths.size()];
57  		int i = 0;
58  		for (String s : paths)
59  			p[i++] = PathFilter.create(s);
60  		return create(p);
61  	}
62  
63  	/**
64  	 * Create a collection of path filters from Java strings.
65  	 * <p>
66  	 * Path strings are relative to the root of the repository. If the user's
67  	 * input should be assumed relative to a subdirectory of the repository the
68  	 * caller must prepend the subdirectory's path prior to creating the filter.
69  	 * <p>
70  	 * Path strings use '/' to delimit directories on all platforms.
71  	 * <p>
72  	 * Paths may appear in any order. Sorting may be done internally when the
73  	 * group is constructed if doing so will improve path matching performance.
74  	 *
75  	 * @param paths
76  	 *            the paths to test against. Must have at least one entry.
77  	 * @return a new filter for the paths supplied.
78  	 */
79  	public static TreeFilter createFromStrings(String... paths) {
80  		if (paths.length == 0)
81  			throw new IllegalArgumentException(
82  					JGitText.get().atLeastOnePathIsRequired);
83  		final int length = paths.length;
84  		final PathFilterfilter/PathFilter.html#PathFilter">PathFilter[] p = new PathFilter[length];
85  		for (int i = 0; i < length; i++)
86  			p[i] = PathFilter.create(paths[i]);
87  		return create(p);
88  	}
89  
90  	/**
91  	 * Create a collection of path filters.
92  	 * <p>
93  	 * Paths may appear in any order within the collection. Sorting may be done
94  	 * internally when the group is constructed if doing so will improve path
95  	 * matching performance.
96  	 *
97  	 * @param paths
98  	 *            the paths to test against. Must have at least one entry.
99  	 * @return a new filter for the list of paths supplied.
100 	 */
101 	public static TreeFilter create(Collection<PathFilter> paths) {
102 		if (paths.isEmpty())
103 			throw new IllegalArgumentException(
104 					JGitText.get().atLeastOnePathIsRequired);
105 		final PathFilterfilter/PathFilter.html#PathFilter">PathFilter[] p = new PathFilter[paths.size()];
106 		paths.toArray(p);
107 		return create(p);
108 	}
109 
110 	private static TreeFilter create(PathFilter[] p) {
111 		if (p.length == 1)
112 			return new Single(p[0]);
113 		return new Group(p);
114 	}
115 
116 	static class Single extends TreeFilter {
117 		private final PathFilter path;
118 
119 		private final byte[] raw;
120 
121 		private Single(PathFilter p) {
122 			path = p;
123 			raw = path.pathRaw;
124 		}
125 
126 		@Override
127 		public boolean include(TreeWalk walker) {
128 			final int cmp = walker.isPathPrefix(raw, raw.length);
129 			if (cmp > 0)
130 				throw StopWalkException.INSTANCE;
131 			return cmp == 0;
132 		}
133 
134 		@Override
135 		public boolean shouldBeRecursive() {
136 			return path.shouldBeRecursive();
137 		}
138 
139 		@Override
140 		public TreeFilter clone() {
141 			return this;
142 		}
143 
144 		@Override
145 		public String toString() {
146 			return "FAST_" + path.toString(); //$NON-NLS-1$
147 		}
148 	}
149 
150 	static class Group extends TreeFilter {
151 
152 		private ByteArraySet fullpaths;
153 
154 		private ByteArraySet prefixes;
155 
156 		private byte[] max;
157 
158 		private Group(PathFilter[] pathFilters) {
159 			fullpaths = new ByteArraySet(pathFilters.length);
160 			prefixes = new ByteArraySet(pathFilters.length / 5);
161 			// 5 is an empirically derived ratio of #paths/#prefixes from:
162 			// egit/jgit: 8
163 			// git: 5
164 			// linux kernel: 13
165 			// eclipse.platform.ui: 7
166 			max = pathFilters[0].pathRaw;
167 			Hasher hasher = new Hasher(null, 0);
168 			for (PathFilter pf : pathFilters) {
169 				hasher.init(pf.pathRaw, pf.pathRaw.length);
170 				while (hasher.hasNext()) {
171 					int hash = hasher.nextHash();
172 					if (hasher.hasNext())
173 						prefixes.addIfAbsent(pf.pathRaw, hasher.length(), hash);
174 				}
175 				fullpaths.addIfAbsent(pf.pathRaw, pf.pathRaw.length,
176 						hasher.getHash());
177 				if (compare(max, pf.pathRaw) < 0)
178 					max = pf.pathRaw;
179 			}
180 			// Adjust max for the git sort order. A path we compare
181 			// with may end with a slash at any position (but the
182 			// first, but we ignore that here since it's not relevant).
183 			// Such paths must be included in the processing
184 			// before we can give up and throw a StopWalkException.
185 			byte[] newMax = new byte[max.length + 1];
186 			for (int i = 0; i < max.length; ++i)
187 				if ((max[i] & 0xFF) < '/')
188 					newMax[i] = '/';
189 				else
190 					newMax[i] = max[i];
191 			newMax[newMax.length - 1] = '/';
192 			max = newMax;
193 		}
194 
195 		private static int compare(byte[] a, byte[] b) {
196 			int i = 0;
197 			while (i < a.length && i < b.length) {
198 				int ba = a[i] & 0xFF;
199 				int bb = b[i] & 0xFF;
200 				int cmp = ba - bb;
201 				if (cmp != 0)
202 					return cmp;
203 				++i;
204 			}
205 			return a.length - b.length;
206 		}
207 
208 		@Override
209 		public boolean include(TreeWalk walker) {
210 
211 			byte[] rp = walker.getRawPath();
212 			Hasher hasher = new Hasher(rp, walker.getPathLength());
213 			while (hasher.hasNext()) {
214 				int hash = hasher.nextHash();
215 				if (fullpaths.contains(rp, hasher.length(), hash))
216 					return true;
217 				if (!hasher.hasNext() && walker.isSubtree()
218 						&& prefixes.contains(rp, hasher.length(), hash))
219 					return true;
220 			}
221 
222 			final int cmp = walker.isPathPrefix(max, max.length);
223 			if (cmp > 0)
224 				throw StopWalkException.INSTANCE;
225 
226 			return false;
227 		}
228 
229 		@Override
230 		public boolean shouldBeRecursive() {
231 			return !prefixes.isEmpty();
232 		}
233 
234 		@Override
235 		public TreeFilter clone() {
236 			return this;
237 		}
238 
239 		@Override
240 		public String toString() {
241 			final StringBuilder r = new StringBuilder();
242 			r.append("FAST("); //$NON-NLS-1$
243 			boolean first = true;
244 			for (byte[] p : fullpaths.toArray()) {
245 				if (!first) {
246 					r.append(" OR "); //$NON-NLS-1$
247 				}
248 				r.append(RawParseUtils.decode(p));
249 				first = false;
250 			}
251 			r.append(")"); //$NON-NLS-1$
252 			return r.toString();
253 		}
254 	}
255 
256 }