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