1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.ignore;
11
12 import static org.eclipse.jgit.ignore.IMatcher.NO_MATCH;
13 import static org.eclipse.jgit.ignore.internal.Strings.isDirectoryPattern;
14 import static org.eclipse.jgit.ignore.internal.Strings.stripTrailing;
15 import static org.eclipse.jgit.ignore.internal.Strings.stripTrailingWhitespace;
16
17 import java.text.MessageFormat;
18
19 import org.eclipse.jgit.errors.InvalidPatternException;
20 import org.eclipse.jgit.ignore.internal.PathMatcher;
21 import org.eclipse.jgit.internal.JGitText;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25
26
27
28
29
30
31
32
33 public class FastIgnoreRule {
34 private static final Logger LOG = LoggerFactory
35 .getLogger(FastIgnoreRule.class);
36
37
38
39
40 public static final char PATH_SEPARATOR = '/';
41
42 private IMatcher matcher;
43
44 private boolean inverse;
45
46 private boolean dirOnly;
47
48
49
50
51
52
53
54
55
56
57 public FastIgnoreRule(String pattern) {
58 this();
59 try {
60 parse(pattern);
61 } catch (InvalidPatternException e) {
62 LOG.error(MessageFormat.format(JGitText.get().badIgnorePattern,
63 e.getPattern()), e);
64 }
65 }
66
67 FastIgnoreRule() {
68 matcher = IMatcher.NO_MATCH;
69 }
70
71 void parse(String pattern) throws InvalidPatternException {
72 if (pattern == null) {
73 throw new IllegalArgumentException("Pattern must not be null!");
74 }
75 if (pattern.length() == 0) {
76 dirOnly = false;
77 inverse = false;
78 this.matcher = NO_MATCH;
79 return;
80 }
81 inverse = pattern.charAt(0) == '!';
82 if (inverse) {
83 pattern = pattern.substring(1);
84 if (pattern.length() == 0) {
85 dirOnly = false;
86 this.matcher = NO_MATCH;
87 return;
88 }
89 }
90 if (pattern.charAt(0) == '#') {
91 this.matcher = NO_MATCH;
92 dirOnly = false;
93 return;
94 }
95 if (pattern.charAt(0) == '\\' && pattern.length() > 1) {
96 char next = pattern.charAt(1);
97 if (next == '!' || next == '#') {
98
99 pattern = pattern.substring(1);
100 }
101 }
102 dirOnly = isDirectoryPattern(pattern);
103 if (dirOnly) {
104 pattern = stripTrailingWhitespace(pattern);
105 pattern = stripTrailing(pattern, PATH_SEPARATOR);
106 if (pattern.length() == 0) {
107 this.matcher = NO_MATCH;
108 return;
109 }
110 }
111 this.matcher = PathMatcher.createPathMatcher(pattern,
112 Character.valueOf(PATH_SEPARATOR), dirOnly);
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 public boolean isMatch(String path, boolean directory) {
132 return isMatch(path, directory, false);
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 public boolean isMatch(String path, boolean directory, boolean pathMatch) {
156 if (path == null)
157 return false;
158 if (path.length() == 0)
159 return false;
160 boolean match = matcher.matches(path, directory, pathMatch);
161 return match;
162 }
163
164
165
166
167
168
169 public boolean getNameOnly() {
170 return !(matcher instanceof PathMatcher);
171 }
172
173
174
175
176
177
178 public boolean dirOnly() {
179 return dirOnly;
180 }
181
182
183
184
185
186
187 public boolean getNegation() {
188 return inverse;
189 }
190
191
192
193
194
195
196 public boolean getResult() {
197 return !inverse;
198 }
199
200
201
202
203
204
205
206
207 public boolean isEmpty() {
208 return matcher == NO_MATCH;
209 }
210
211
212 @Override
213 public String toString() {
214 StringBuilder sb = new StringBuilder();
215 if (inverse)
216 sb.append('!');
217 sb.append(matcher);
218 if (dirOnly)
219 sb.append(PATH_SEPARATOR);
220 return sb.toString();
221
222 }
223
224
225 @Override
226 public int hashCode() {
227 final int prime = 31;
228 int result = 1;
229 result = prime * result + (inverse ? 1231 : 1237);
230 result = prime * result + (dirOnly ? 1231 : 1237);
231 result = prime * result + ((matcher == null) ? 0 : matcher.hashCode());
232 return result;
233 }
234
235
236 @Override
237 public boolean equals(Object obj) {
238 if (this == obj)
239 return true;
240 if (!(obj instanceof FastIgnoreRule))
241 return false;
242
243 FastIgnoreRule other = (FastIgnoreRule) obj;
244 if (inverse != other.inverse)
245 return false;
246 if (dirOnly != other.dirOnly)
247 return false;
248 return matcher.equals(other.matcher);
249 }
250 }