Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RevFilter.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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.revwalk.filter;
  44. import java.io.IOException;
  45. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  46. import org.eclipse.jgit.errors.MissingObjectException;
  47. import org.eclipse.jgit.errors.StopWalkException;
  48. import org.eclipse.jgit.internal.JGitText;
  49. import org.eclipse.jgit.revwalk.RevCommit;
  50. import org.eclipse.jgit.revwalk.RevWalk;
  51. /**
  52. * Selects interesting revisions during walking.
  53. * <p>
  54. * This is an abstract interface. Applications may implement a subclass, or use
  55. * one of the predefined implementations already available within this package.
  56. * Filters may be chained together using <code>AndRevFilter</code> and
  57. * <code>OrRevFilter</code> to create complex boolean expressions.
  58. * <p>
  59. * Applications should install the filter on a RevWalk by
  60. * {@link org.eclipse.jgit.revwalk.RevWalk#setRevFilter(RevFilter)} prior to
  61. * starting traversal.
  62. * <p>
  63. * Unless specifically noted otherwise a RevFilter implementation is not thread
  64. * safe and may not be shared by different RevWalk instances at the same time.
  65. * This restriction allows RevFilter implementations to cache state within their
  66. * instances during {@link #include(RevWalk, RevCommit)} if it is beneficial to
  67. * their implementation. Deep clones created by {@link #clone()} may be used to
  68. * construct a thread-safe copy of an existing filter.
  69. *
  70. * <p>
  71. * <b>Message filters:</b>
  72. * <ul>
  73. * <li>Author name/email:
  74. * {@link org.eclipse.jgit.revwalk.filter.AuthorRevFilter}</li>
  75. * <li>Committer name/email:
  76. * {@link org.eclipse.jgit.revwalk.filter.CommitterRevFilter}</li>
  77. * <li>Message body:
  78. * {@link org.eclipse.jgit.revwalk.filter.MessageRevFilter}</li>
  79. * </ul>
  80. *
  81. * <p>
  82. * <b>Merge filters:</b>
  83. * <ul>
  84. * <li>Skip all merges: {@link #NO_MERGES}.</li>
  85. * <li>Skip all non-merges: {@link #ONLY_MERGES}</li>
  86. * </ul>
  87. *
  88. * <p>
  89. * <b>Boolean modifiers:</b>
  90. * <ul>
  91. * <li>AND: {@link org.eclipse.jgit.revwalk.filter.AndRevFilter}</li>
  92. * <li>OR: {@link org.eclipse.jgit.revwalk.filter.OrRevFilter}</li>
  93. * <li>NOT: {@link org.eclipse.jgit.revwalk.filter.NotRevFilter}</li>
  94. * </ul>
  95. */
  96. public abstract class RevFilter {
  97. /** Default filter that always returns true (thread safe). */
  98. public static final RevFilter ALL = new AllFilter();
  99. private static final class AllFilter extends RevFilter {
  100. @Override
  101. public boolean include(final RevWalk walker, final RevCommit c) {
  102. return true;
  103. }
  104. @Override
  105. public RevFilter clone() {
  106. return this;
  107. }
  108. @Override
  109. public boolean requiresCommitBody() {
  110. return false;
  111. }
  112. @Override
  113. public String toString() {
  114. return "ALL"; //$NON-NLS-1$
  115. }
  116. }
  117. /** Default filter that always returns false (thread safe). */
  118. public static final RevFilter NONE = new NoneFilter();
  119. private static final class NoneFilter extends RevFilter {
  120. @Override
  121. public boolean include(final RevWalk walker, final RevCommit c) {
  122. return false;
  123. }
  124. @Override
  125. public RevFilter clone() {
  126. return this;
  127. }
  128. @Override
  129. public boolean requiresCommitBody() {
  130. return false;
  131. }
  132. @Override
  133. public String toString() {
  134. return "NONE"; //$NON-NLS-1$
  135. }
  136. }
  137. /**
  138. * Filter including only merge commits, excluding all commits with less than
  139. * two parents (thread safe).
  140. *
  141. * @since 4.4
  142. */
  143. public static final RevFilter ONLY_MERGES = new OnlyMergesFilter();
  144. private static final class OnlyMergesFilter extends RevFilter {
  145. @Override
  146. public boolean include(RevWalk walker, RevCommit c) {
  147. return c.getParentCount() >= 2;
  148. }
  149. @Override
  150. public RevFilter clone() {
  151. return this;
  152. }
  153. @Override
  154. public boolean requiresCommitBody() {
  155. return false;
  156. }
  157. @Override
  158. public String toString() {
  159. return "ONLY_MERGES"; //$NON-NLS-1$
  160. }
  161. }
  162. /** Excludes commits with more than one parent (thread safe). */
  163. public static final RevFilter NO_MERGES = new NoMergesFilter();
  164. private static final class NoMergesFilter extends RevFilter {
  165. @Override
  166. public boolean include(final RevWalk walker, final RevCommit c) {
  167. return c.getParentCount() < 2;
  168. }
  169. @Override
  170. public RevFilter clone() {
  171. return this;
  172. }
  173. @Override
  174. public boolean requiresCommitBody() {
  175. return false;
  176. }
  177. @Override
  178. public String toString() {
  179. return "NO_MERGES"; //$NON-NLS-1$
  180. }
  181. }
  182. /**
  183. * Selects only merge bases of the starting points (thread safe).
  184. * <p>
  185. * This is a special case filter that cannot be combined with any other
  186. * filter. Its include method always throws an exception as context
  187. * information beyond the arguments is necessary to determine if the
  188. * supplied commit is a merge base.
  189. */
  190. public static final RevFilter MERGE_BASE = new MergeBaseFilter();
  191. private static final class MergeBaseFilter extends RevFilter {
  192. @Override
  193. public boolean include(final RevWalk walker, final RevCommit c) {
  194. throw new UnsupportedOperationException(JGitText.get().cannotBeCombined);
  195. }
  196. @Override
  197. public RevFilter clone() {
  198. return this;
  199. }
  200. @Override
  201. public boolean requiresCommitBody() {
  202. return false;
  203. }
  204. @Override
  205. public String toString() {
  206. return "MERGE_BASE"; //$NON-NLS-1$
  207. }
  208. }
  209. /**
  210. * Create a new filter that does the opposite of this filter.
  211. *
  212. * @return a new filter that includes commits this filter rejects.
  213. */
  214. public RevFilter negate() {
  215. return NotRevFilter.create(this);
  216. }
  217. /**
  218. * Whether the filter needs the commit body to be parsed.
  219. *
  220. * @return true if the filter needs the commit body to be parsed.
  221. */
  222. public boolean requiresCommitBody() {
  223. // Assume true to be backward compatible with prior behavior.
  224. return true;
  225. }
  226. /**
  227. * Determine if the supplied commit should be included in results.
  228. *
  229. * @param walker
  230. * the active walker this filter is being invoked from within.
  231. * @param cmit
  232. * the commit currently being tested. The commit has been parsed
  233. * and its body is available for inspection only if the filter
  234. * returns true from {@link #requiresCommitBody()}.
  235. * @return true to include this commit in the results; false to have this
  236. * commit be omitted entirely from the results.
  237. * @throws org.eclipse.jgit.errors.StopWalkException
  238. * the filter knows for certain that no additional commits can
  239. * ever match, and the current commit doesn't match either. The
  240. * walk is halted and no more results are provided.
  241. * @throws org.eclipse.jgit.errors.MissingObjectException
  242. * an object the filter needs to consult to determine its answer
  243. * does not exist in the Git repository the walker is operating
  244. * on. Filtering this commit is impossible without the object.
  245. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  246. * an object the filter needed to consult was not of the
  247. * expected object type. This usually indicates a corrupt
  248. * repository, as an object link is referencing the wrong type.
  249. * @throws java.io.IOException
  250. * a loose object or pack file could not be read to obtain data
  251. * necessary for the filter to make its decision.
  252. */
  253. public abstract boolean include(RevWalk walker, RevCommit cmit)
  254. throws StopWalkException, MissingObjectException,
  255. IncorrectObjectTypeException, IOException;
  256. /**
  257. * {@inheritDoc}
  258. * <p>
  259. * Clone this revision filter, including its parameters.
  260. * <p>
  261. * This is a deep clone. If this filter embeds objects or other filters it
  262. * must also clone those, to ensure the instances do not share mutable data.
  263. */
  264. @Override
  265. public abstract RevFilter clone();
  266. /** {@inheritDoc} */
  267. @Override
  268. public String toString() {
  269. String n = getClass().getName();
  270. int lastDot = n.lastIndexOf('.');
  271. if (lastDot >= 0) {
  272. n = n.substring(lastDot + 1);
  273. }
  274. return n.replace('$', '.');
  275. }
  276. }