You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PathFilterGroup.java 9.0KB

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