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.

TreeFilter.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.treewalk.filter;
  44. import java.io.IOException;
  45. import org.eclipse.jgit.dircache.DirCacheIterator;
  46. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  47. import org.eclipse.jgit.errors.MissingObjectException;
  48. import org.eclipse.jgit.treewalk.TreeWalk;
  49. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  50. /**
  51. * Selects interesting tree entries during walking.
  52. * <p>
  53. * This is an abstract interface. Applications may implement a subclass, or use
  54. * one of the predefined implementations already available within this package.
  55. * <p>
  56. * Unless specifically noted otherwise a TreeFilter implementation is not thread
  57. * safe and may not be shared by different TreeWalk instances at the same time.
  58. * This restriction allows TreeFilter implementations to cache state within
  59. * their instances during {@link #include(TreeWalk)} if it is beneficial to
  60. * their implementation. Deep clones created by {@link #clone()} may be used to
  61. * construct a thread-safe copy of an existing filter.
  62. *
  63. * <p>
  64. * <b>Path filters:</b>
  65. * <ul>
  66. * <li>Matching pathname: {@link PathFilter}</li>
  67. * </ul>
  68. *
  69. * <p>
  70. * <b>Difference filters:</b>
  71. * <ul>
  72. * <li>Only select differences: {@link #ANY_DIFF}.</li>
  73. * </ul>
  74. *
  75. * <p>
  76. * <b>Boolean modifiers:</b>
  77. * <ul>
  78. * <li>AND: {@link AndTreeFilter}</li>
  79. * <li>OR: {@link OrTreeFilter}</li>
  80. * <li>NOT: {@link NotTreeFilter}</li>
  81. * </ul>
  82. */
  83. public abstract class TreeFilter {
  84. /** Selects all tree entries. */
  85. public static final TreeFilter ALL = new AllFilter();
  86. private static final class AllFilter extends TreeFilter {
  87. @Override
  88. public boolean include(final TreeWalk walker) {
  89. return true;
  90. }
  91. @Override
  92. public boolean shouldBeRecursive() {
  93. return false;
  94. }
  95. @Override
  96. public TreeFilter clone() {
  97. return this;
  98. }
  99. @Override
  100. public String toString() {
  101. return "ALL"; //$NON-NLS-1$
  102. }
  103. }
  104. /**
  105. * Selects only tree entries which differ between at least 2 trees.
  106. * <p>
  107. * This filter also prevents a TreeWalk from recursing into a subtree if all
  108. * parent trees have the identical subtree at the same path. This
  109. * dramatically improves walk performance as only the changed subtrees are
  110. * entered into.
  111. * <p>
  112. * If this filter is applied to a walker with only one tree it behaves like
  113. * {@link #ALL}, or as though the walker was matching a virtual empty tree
  114. * against the single tree it was actually given. Applications may wish to
  115. * treat such a difference as "all names added".
  116. * <p>
  117. * When comparing {@link WorkingTreeIterator} and {@link DirCacheIterator}
  118. * applications should use {@link IndexDiffFilter}.
  119. */
  120. public static final TreeFilter ANY_DIFF = new AnyDiffFilter();
  121. private static final class AnyDiffFilter extends TreeFilter {
  122. private static final int baseTree = 0;
  123. @Override
  124. public boolean include(final TreeWalk walker) {
  125. final int n = walker.getTreeCount();
  126. if (n == 1) // Assume they meant difference to empty tree.
  127. return true;
  128. final int m = walker.getRawMode(baseTree);
  129. for (int i = 1; i < n; i++)
  130. if (walker.getRawMode(i) != m || !walker.idEqual(i, baseTree))
  131. return true;
  132. return false;
  133. }
  134. @Override
  135. public boolean shouldBeRecursive() {
  136. return false;
  137. }
  138. @Override
  139. public TreeFilter clone() {
  140. return this;
  141. }
  142. @Override
  143. public String toString() {
  144. return "ANY_DIFF"; //$NON-NLS-1$
  145. }
  146. }
  147. /**
  148. * Create a new filter that does the opposite of this filter.
  149. *
  150. * @return a new filter that includes tree entries this filter rejects.
  151. */
  152. public TreeFilter negate() {
  153. return NotTreeFilter.create(this);
  154. }
  155. /**
  156. * Determine if the current entry is interesting to report.
  157. * <p>
  158. * This method is consulted for subtree entries even if
  159. * {@link TreeWalk#isRecursive()} is enabled. The consultation allows the
  160. * filter to bypass subtree recursion on a case-by-case basis, even when
  161. * recursion is enabled at the application level.
  162. *
  163. * @param walker
  164. * the walker the filter needs to examine.
  165. * @return true if the current entry should be seen by the application;
  166. * false to hide the entry.
  167. * @throws MissingObjectException
  168. * an object the filter needs to consult to determine its answer
  169. * does not exist in the Git repository the walker is operating
  170. * on. Filtering this current walker entry is impossible without
  171. * the object.
  172. * @throws IncorrectObjectTypeException
  173. * an object the filter needed to consult was not of the
  174. * expected object type. This usually indicates a corrupt
  175. * repository, as an object link is referencing the wrong type.
  176. * @throws IOException
  177. * a loose object or pack file could not be read to obtain data
  178. * necessary for the filter to make its decision.
  179. */
  180. public abstract boolean include(TreeWalk walker)
  181. throws MissingObjectException, IncorrectObjectTypeException,
  182. IOException;
  183. /**
  184. * Determine if the current entry is a parent, a match, or no match.
  185. * <p>
  186. * This method extends the result returned by {@link #include(TreeWalk)}
  187. * with a third option (-1), splitting the value true. This gives the
  188. * application a possibility to distinguish between an exact match
  189. * and the case when a subtree to the current entry might be a match.
  190. *
  191. * @param walker
  192. * the walker the filter needs to examine.
  193. * @return -1 if the current entry is a parent of the filter but no
  194. * exact match has been made; 0 if the current entry should
  195. * be seen by the application; 1 if it should be hidden.
  196. * @throws MissingObjectException
  197. * as thrown by {@link #include(TreeWalk)}
  198. * @throws IncorrectObjectTypeException
  199. * as thrown by {@link #include(TreeWalk)}
  200. * @throws IOException
  201. * as thrown by {@link #include(TreeWalk)}
  202. * @since 4.7
  203. */
  204. public int matchFilter(final TreeWalk walker)
  205. throws MissingObjectException, IncorrectObjectTypeException,
  206. IOException
  207. {
  208. return include(walker) ? 0 : 1;
  209. }
  210. /**
  211. * Does this tree filter require a recursive walk to match everything?
  212. * <p>
  213. * If this tree filter is matching on full entry path names and its pattern
  214. * is looking for a '/' then the filter would require a recursive TreeWalk
  215. * to accurately make its decisions. The walker is not required to enable
  216. * recursive behavior for any particular filter, this is only a hint.
  217. *
  218. * @return true if the filter would like to have the walker recurse into
  219. * subtrees to make sure it matches everything correctly; false if
  220. * the filter does not require entering subtrees.
  221. */
  222. public abstract boolean shouldBeRecursive();
  223. /**
  224. * Clone this tree filter, including its parameters.
  225. * <p>
  226. * This is a deep clone. If this filter embeds objects or other filters it
  227. * must also clone those, to ensure the instances do not share mutable data.
  228. *
  229. * @return another copy of this filter, suitable for another thread.
  230. */
  231. @Override
  232. public abstract TreeFilter clone();
  233. @Override
  234. public String toString() {
  235. String n = getClass().getName();
  236. int lastDot = n.lastIndexOf('.');
  237. if (lastDot >= 0) {
  238. n = n.substring(lastDot + 1);
  239. }
  240. return n.replace('$', '.');
  241. }
  242. }