Implement a commit filtering API for RevWalk
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / revwalk / filter / PatternMatchRevFilter.java
blob6f8f1ba80bc27a6ca311cfce72f51e9558162e0c
1 /*
2 * Copyright (C) 2008 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.jgit.revwalk.filter;
19 import java.io.IOException;
20 import java.io.UnsupportedEncodingException;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 import org.spearce.jgit.errors.IncorrectObjectTypeException;
25 import org.spearce.jgit.errors.MissingObjectException;
26 import org.spearce.jgit.revwalk.RevCommit;
27 import org.spearce.jgit.revwalk.RevWalk;
28 import org.spearce.jgit.util.RawCharSequence;
30 /** Abstract filter that searches text using extended regular expressions. */
31 public abstract class PatternMatchRevFilter extends RevFilter {
32 /**
33 * Encode a string pattern for faster matching on byte arrays.
34 * <p>
35 * Force the characters to our funny UTF-8 only convention that we use on
36 * raw buffers. This avoids needing to perform character set decodes on the
37 * individual commit buffers.
39 * @param patternText
40 * original pattern string supplied by the user or the
41 * application.
42 * @return same pattern, but re-encoded to match our funny raw UTF-8
43 * character sequence {@link RawCharSequence}.
45 protected static final String forceToRaw(final String patternText) {
46 final byte[] b;
47 try {
48 b = patternText.getBytes("UTF-8");
49 } catch (UnsupportedEncodingException e) {
50 throw new IllegalStateException("JVM lacks UTF-8 support.", e);
53 final StringBuilder needle = new StringBuilder(b.length);
54 for (int i = 0; i < b.length; i++)
55 needle.append((char) (b[i] & 0xff));
56 return needle.toString();
59 private final String patternText;
61 private final Matcher compiledPattern;
63 /**
64 * Construct a new pattern matching filter.
66 * @param pattern
67 * text of the pattern. Callers may want to surround their
68 * pattern with ".*" on either end to allow matching in the
69 * middle of the string.
70 * @param innerString
71 * should .* be wrapped around the pattern of ^ and $ are
72 * missing? Most users will want this set.
73 * @param rawEncoding
74 * should {@link #forceToRaw(String)} be applied to the pattern
75 * before compiling it?
76 * @param flags
77 * flags from {@link Pattern} to control how matching performs.
79 protected PatternMatchRevFilter(String pattern, final boolean innerString,
80 final boolean rawEncoding, final int flags) {
81 if (pattern.length() == 0)
82 throw new IllegalArgumentException("Cannot match on empty string.");
83 patternText = pattern;
85 if (innerString) {
86 if (!pattern.startsWith("^") && !pattern.startsWith(".*"))
87 pattern = ".*" + pattern;
88 if (!pattern.endsWith("$") && !pattern.endsWith(".*"))
89 pattern = pattern + ".*";
91 final String p = rawEncoding ? forceToRaw(pattern) : pattern;
92 compiledPattern = Pattern.compile(p, flags).matcher("");
95 @Override
96 public boolean include(final RevWalk walker, final RevCommit cmit)
97 throws MissingObjectException, IncorrectObjectTypeException,
98 IOException {
99 return compiledPattern.reset(text(cmit)).matches();
103 * Obtain the raw text to match against.
105 * @param cmit
106 * current commit being evaluated.
107 * @return sequence for the commit's content that we need to match on.
109 protected abstract CharSequence text(RevCommit cmit);
111 @Override
112 public String toString() {
113 return super.toString() + "(\"" + patternText + "\")";