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

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