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