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.

RevFilter.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.revwalk.RevCommit;
  49. import org.eclipse.jgit.revwalk.RevWalk;
  50. /**
  51. * Selects interesting revisions 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. * Filters may be chained together using <code>AndRevFilter</code> and
  56. * <code>OrRevFilter</code> to create complex boolean expressions.
  57. * <p>
  58. * Applications should install the filter on a RevWalk by
  59. * {@link RevWalk#setRevFilter(RevFilter)} prior to starting traversal.
  60. * <p>
  61. * Unless specifically noted otherwise a RevFilter implementation is not thread
  62. * safe and may not be shared by different RevWalk instances at the same time.
  63. * This restriction allows RevFilter implementations to cache state within their
  64. * instances during {@link #include(RevWalk, RevCommit)} if it is beneficial to
  65. * their implementation. Deep clones created by {@link #clone()} may be used to
  66. * construct a thread-safe copy of an existing filter.
  67. *
  68. * <p>
  69. * <b>Message filters:</b>
  70. * <ul>
  71. * <li>Author name/email: {@link AuthorRevFilter}</li>
  72. * <li>Committer name/email: {@link CommitterRevFilter}</li>
  73. * <li>Message body: {@link MessageRevFilter}</li>
  74. * </ul>
  75. *
  76. * <p>
  77. * <b>Merge filters:</b>
  78. * <ul>
  79. * <li>Skip all merges: {@link #NO_MERGES}.</li>
  80. * </ul>
  81. *
  82. * <p>
  83. * <b>Boolean modifiers:</b>
  84. * <ul>
  85. * <li>AND: {@link AndRevFilter}</li>
  86. * <li>OR: {@link OrRevFilter}</li>
  87. * <li>NOT: {@link NotRevFilter}</li>
  88. * </ul>
  89. */
  90. public abstract class RevFilter {
  91. /** Default filter that always returns true (thread safe). */
  92. public static final RevFilter ALL = new RevFilter() {
  93. @Override
  94. public boolean include(final RevWalk walker, final RevCommit c) {
  95. return true;
  96. }
  97. @Override
  98. public RevFilter clone() {
  99. return this;
  100. }
  101. @Override
  102. public String toString() {
  103. return "ALL";
  104. }
  105. };
  106. /** Default filter that always returns false (thread safe). */
  107. public static final RevFilter NONE = new RevFilter() {
  108. @Override
  109. public boolean include(final RevWalk walker, final RevCommit c) {
  110. return false;
  111. }
  112. @Override
  113. public RevFilter clone() {
  114. return this;
  115. }
  116. @Override
  117. public String toString() {
  118. return "NONE";
  119. }
  120. };
  121. /** Excludes commits with more than one parent (thread safe). */
  122. public static final RevFilter NO_MERGES = new RevFilter() {
  123. @Override
  124. public boolean include(final RevWalk walker, final RevCommit c) {
  125. return c.getParentCount() < 2;
  126. }
  127. @Override
  128. public RevFilter clone() {
  129. return this;
  130. }
  131. @Override
  132. public String toString() {
  133. return "NO_MERGES";
  134. }
  135. };
  136. /**
  137. * Selects only merge bases of the starting points (thread safe).
  138. * <p>
  139. * This is a special case filter that cannot be combined with any other
  140. * filter. Its include method always throws an exception as context
  141. * information beyond the arguments is necessary to determine if the
  142. * supplied commit is a merge base.
  143. */
  144. public static final RevFilter MERGE_BASE = new RevFilter() {
  145. @Override
  146. public boolean include(final RevWalk walker, final RevCommit c) {
  147. throw new UnsupportedOperationException("Cannot be combined.");
  148. }
  149. @Override
  150. public RevFilter clone() {
  151. return this;
  152. }
  153. @Override
  154. public String toString() {
  155. return "MERGE_BASE";
  156. }
  157. };
  158. /**
  159. * Create a new filter that does the opposite of this filter.
  160. *
  161. * @return a new filter that includes commits this filter rejects.
  162. */
  163. public RevFilter negate() {
  164. return NotRevFilter.create(this);
  165. }
  166. /**
  167. * Determine if the supplied commit should be included in results.
  168. *
  169. * @param walker
  170. * the active walker this filter is being invoked from within.
  171. * @param cmit
  172. * the commit currently being tested. The commit has been parsed
  173. * and its body is available for inspection.
  174. * @return true to include this commit in the results; false to have this
  175. * commit be omitted entirely from the results.
  176. * @throws StopWalkException
  177. * the filter knows for certain that no additional commits can
  178. * ever match, and the current commit doesn't match either. The
  179. * walk is halted and no more results are provided.
  180. * @throws MissingObjectException
  181. * an object the filter needs to consult to determine its answer
  182. * does not exist in the Git repository the walker is operating
  183. * on. Filtering this commit is impossible without the object.
  184. * @throws IncorrectObjectTypeException
  185. * an object the filter needed to consult was not of the
  186. * expected object type. This usually indicates a corrupt
  187. * repository, as an object link is referencing the wrong type.
  188. * @throws IOException
  189. * a loose object or pack file could not be read to obtain data
  190. * necessary for the filter to make its decision.
  191. */
  192. public abstract boolean include(RevWalk walker, RevCommit cmit)
  193. throws StopWalkException, MissingObjectException,
  194. IncorrectObjectTypeException, IOException;
  195. /**
  196. * Clone this revision filter, including its parameters.
  197. * <p>
  198. * This is a deep clone. If this filter embeds objects or other filters it
  199. * must also clone those, to ensure the instances do not share mutable data.
  200. *
  201. * @return another copy of this filter, suitable for another thread.
  202. */
  203. public abstract RevFilter clone();
  204. @Override
  205. public String toString() {
  206. String n = getClass().getName();
  207. int lastDot = n.lastIndexOf('.');
  208. if (lastDot >= 0) {
  209. n = n.substring(lastDot + 1);
  210. }
  211. return n.replace('$', '.');
  212. }
  213. }