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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.Arrays;
  46. import java.util.Collection;
  47. import java.util.Comparator;
  48. import org.eclipse.jgit.errors.StopWalkException;
  49. import org.eclipse.jgit.treewalk.TreeWalk;
  50. /**
  51. * Includes tree entries only if they match one or more configured paths.
  52. * <p>
  53. * Operates like {@link PathFilter} but causes the walk to abort as soon as the
  54. * tree can no longer match any of the paths within the group. This may bypass
  55. * the boolean logic of a higher level AND or OR group, but does improve
  56. * performance for the common case of examining one or more modified paths.
  57. * <p>
  58. * This filter is effectively an OR group around paths, with the early abort
  59. * feature described above.
  60. */
  61. public class PathFilterGroup {
  62. /**
  63. * Create a collection of path filters from Java strings.
  64. * <p>
  65. * Path strings are relative to the root of the repository. If the user's
  66. * input should be assumed relative to a subdirectory of the repository the
  67. * caller must prepend the subdirectory's path prior to creating the filter.
  68. * <p>
  69. * Path strings use '/' to delimit directories on all platforms.
  70. * <p>
  71. * Paths may appear in any order within the collection. Sorting may be done
  72. * internally when the group is constructed if doing so will improve path
  73. * 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 list of paths supplied.
  78. */
  79. public static TreeFilter createFromStrings(final Collection<String> paths) {
  80. if (paths.isEmpty())
  81. throw new IllegalArgumentException("At least one path is required.");
  82. final PathFilter[] p = new PathFilter[paths.size()];
  83. int i = 0;
  84. for (final String s : paths)
  85. p[i++] = PathFilter.create(s);
  86. return create(p);
  87. }
  88. /**
  89. * Create a collection of path filters.
  90. * <p>
  91. * Paths may appear in any order within the collection. Sorting may be done
  92. * internally when the group is constructed if doing so will improve path
  93. * matching performance.
  94. *
  95. * @param paths
  96. * the paths to test against. Must have at least one entry.
  97. * @return a new filter for the list of paths supplied.
  98. */
  99. public static TreeFilter create(final Collection<PathFilter> paths) {
  100. if (paths.isEmpty())
  101. throw new IllegalArgumentException("At least one path is required.");
  102. final PathFilter[] p = new PathFilter[paths.size()];
  103. paths.toArray(p);
  104. return create(p);
  105. }
  106. private static TreeFilter create(final PathFilter[] p) {
  107. if (p.length == 1)
  108. return new Single(p[0]);
  109. return new Group(p);
  110. }
  111. static class Single extends TreeFilter {
  112. private final PathFilter path;
  113. private final byte[] raw;
  114. private Single(final PathFilter p) {
  115. path = p;
  116. raw = path.pathRaw;
  117. }
  118. @Override
  119. public boolean include(final TreeWalk walker) {
  120. final int cmp = walker.isPathPrefix(raw, raw.length);
  121. if (cmp > 0)
  122. throw StopWalkException.INSTANCE;
  123. return cmp == 0;
  124. }
  125. @Override
  126. public boolean shouldBeRecursive() {
  127. return path.shouldBeRecursive();
  128. }
  129. @Override
  130. public TreeFilter clone() {
  131. return this;
  132. }
  133. public String toString() {
  134. return "FAST_" + path.toString();
  135. }
  136. }
  137. static class Group extends TreeFilter {
  138. private static final Comparator<PathFilter> PATH_SORT = new Comparator<PathFilter>() {
  139. public int compare(final PathFilter o1, final PathFilter o2) {
  140. return o1.pathStr.compareTo(o2.pathStr);
  141. }
  142. };
  143. private final PathFilter[] paths;
  144. private Group(final PathFilter[] p) {
  145. paths = p;
  146. Arrays.sort(paths, PATH_SORT);
  147. }
  148. @Override
  149. public boolean include(final TreeWalk walker) {
  150. final int n = paths.length;
  151. for (int i = 0;;) {
  152. final byte[] r = paths[i].pathRaw;
  153. final int cmp = walker.isPathPrefix(r, r.length);
  154. if (cmp == 0)
  155. return true;
  156. if (++i < n)
  157. continue;
  158. if (cmp > 0)
  159. throw StopWalkException.INSTANCE;
  160. return false;
  161. }
  162. }
  163. @Override
  164. public boolean shouldBeRecursive() {
  165. for (final PathFilter p : paths)
  166. if (p.shouldBeRecursive())
  167. return true;
  168. return false;
  169. }
  170. @Override
  171. public TreeFilter clone() {
  172. return this;
  173. }
  174. public String toString() {
  175. final StringBuilder r = new StringBuilder();
  176. r.append("FAST(");
  177. for (int i = 0; i < paths.length; i++) {
  178. if (i > 0)
  179. r.append(" OR ");
  180. r.append(paths[i].toString());
  181. }
  182. r.append(")");
  183. return r.toString();
  184. }
  185. }
  186. }