1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.revwalk.filter;
13
14 import java.io.IOException;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
19 import org.eclipse.jgit.errors.MissingObjectException;
20 import org.eclipse.jgit.internal.JGitText;
21 import org.eclipse.jgit.lib.Constants;
22 import org.eclipse.jgit.revwalk.RevCommit;
23 import org.eclipse.jgit.revwalk.RevWalk;
24
25
26
27
28 public abstract class PatternMatchRevFilter extends RevFilter {
29
30
31
32
33
34
35
36
37
38
39
40
41
42 protected static final String forceToRaw(String patternText) {
43 final byte[] b = Constants.encode(patternText);
44 final StringBuilder needle = new StringBuilder(b.length);
45 for (byte element : b)
46 needle.append((char) (element & 0xff));
47 return needle.toString();
48 }
49
50 private final String patternText;
51
52 private final Matcher compiledPattern;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 protected PatternMatchRevFilter(String pattern, final boolean innerString,
72 final boolean rawEncoding, final int flags) {
73 if (pattern.length() == 0)
74 throw new IllegalArgumentException(JGitText.get().cannotMatchOnEmptyString);
75 patternText = pattern;
76
77 if (innerString) {
78 if (!pattern.startsWith("^") && !pattern.startsWith(".*"))
79 pattern = ".*" + pattern;
80 if (!pattern.endsWith("$") && !pattern.endsWith(".*"))
81 pattern = pattern + ".*";
82 }
83 final String p = rawEncoding ? forceToRaw(pattern) : pattern;
84 compiledPattern = Pattern.compile(p, flags).matcher("");
85 }
86
87
88
89
90
91
92 public String pattern() {
93 return patternText;
94 }
95
96
97 @Override
98 public boolean include(RevWalk walker, RevCommit cmit)
99 throws MissingObjectException, IncorrectObjectTypeException,
100 IOException {
101 return compiledPattern.reset(text(cmit)).matches();
102 }
103
104
105 @Override
106 public boolean requiresCommitBody() {
107 return true;
108 }
109
110
111
112
113
114
115
116
117 protected abstract CharSequence text(RevCommit cmit);
118
119
120 @SuppressWarnings("nls")
121 @Override
122 public String toString() {
123 return super.toString() + "(\"" + patternText + "\")";
124 }
125 }