View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.revwalk.filter;
12  
13  import java.io.IOException;
14  
15  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
16  import org.eclipse.jgit.errors.MissingObjectException;
17  import org.eclipse.jgit.revwalk.RevCommit;
18  import org.eclipse.jgit.revwalk.RevWalk;
19  import org.eclipse.jgit.util.RawCharSequence;
20  import org.eclipse.jgit.util.RawSubStringPattern;
21  
22  /**
23   * Abstract filter that searches text using only substring search.
24   */
25  public abstract class SubStringRevFilter extends RevFilter {
26  	/**
27  	 * Can this string be safely handled by a substring filter?
28  	 *
29  	 * @param pattern
30  	 *            the pattern text proposed by the user.
31  	 * @return true if a substring filter can perform this pattern match; false
32  	 *         if {@link org.eclipse.jgit.revwalk.filter.PatternMatchRevFilter}
33  	 *         must be used instead.
34  	 */
35  	public static boolean safe(String pattern) {
36  		for (int i = 0; i < pattern.length(); i++) {
37  			final char c = pattern.charAt(i);
38  			switch (c) {
39  			case '.':
40  			case '?':
41  			case '*':
42  			case '+':
43  			case '{':
44  			case '}':
45  			case '(':
46  			case ')':
47  			case '[':
48  			case ']':
49  			case '\\':
50  				return false;
51  			}
52  		}
53  		return true;
54  	}
55  
56  	private final RawSubStringPattern pattern;
57  
58  	/**
59  	 * Construct a new matching filter.
60  	 *
61  	 * @param patternText
62  	 *            text to locate. This should be a safe string as described by
63  	 *            the {@link #safe(String)} as regular expression meta
64  	 *            characters are treated as literals.
65  	 */
66  	protected SubStringRevFilter(String patternText) {
67  		pattern = new RawSubStringPattern(patternText);
68  	}
69  
70  	/** {@inheritDoc} */
71  	@Override
72  	public boolean include(RevWalk walker, RevCommit cmit)
73  			throws MissingObjectException, IncorrectObjectTypeException,
74  			IOException {
75  		return pattern.match(text(cmit)) >= 0;
76  	}
77  
78  	/** {@inheritDoc} */
79  	@Override
80  	public boolean requiresCommitBody() {
81  		return true;
82  	}
83  
84  	/**
85  	 * Obtain the raw text to match against.
86  	 *
87  	 * @param cmit
88  	 *            current commit being evaluated.
89  	 * @return sequence for the commit's content that we need to match on.
90  	 */
91  	protected abstract RawCharSequence text(RevCommit cmit);
92  
93  	/** {@inheritDoc} */
94  	@Override
95  	public RevFilter clone() {
96  		return this; // Typically we are actually thread-safe.
97  	}
98  
99  	/** {@inheritDoc} */
100 	@SuppressWarnings("nls")
101 	@Override
102 	public String toString() {
103 		return super.toString() + "(\"" + pattern.pattern() + "\")";
104 	}
105 }