View Javadoc
1   /*
2    * Copyright (C) 2014, Andrey Loskutov <loskutov@gmx.de>
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  package org.eclipse.jgit.ignore.internal;
44  
45  import static org.eclipse.jgit.ignore.internal.Strings.checkWildCards;
46  import static org.eclipse.jgit.ignore.internal.Strings.count;
47  import static org.eclipse.jgit.ignore.internal.Strings.getPathSeparator;
48  import static org.eclipse.jgit.ignore.internal.Strings.isWildCard;
49  import static org.eclipse.jgit.ignore.internal.Strings.split;
50  
51  import java.util.ArrayList;
52  import java.util.List;
53  
54  import org.eclipse.jgit.errors.InvalidPatternException;
55  import org.eclipse.jgit.ignore.internal.Strings.PatternState;
56  
57  /**
58   * Matcher built by patterns consists of multiple path segments.
59   * <p>
60   * This class is immutable and thread safe.
61   */
62  public class PathMatcher extends AbstractMatcher {
63  
64  	private static final WildMatcher WILD_NO_DIRECTORY = new WildMatcher(false);
65  
66  	private static final WildMatcher WILD_ONLY_DIRECTORY = new WildMatcher(
67  			true);
68  
69  	private final List<IMatcher> matchers;
70  
71  	private final char slash;
72  
73  	private final boolean beginning;
74  
75  	private PathMatcher(String pattern, Character pathSeparator,
76  			boolean dirOnly)
77  			throws InvalidPatternException {
78  		super(pattern, dirOnly);
79  		slash = getPathSeparator(pathSeparator);
80  		beginning = pattern.indexOf(slash) == 0;
81  		if (isSimplePathWithSegments(pattern))
82  			matchers = null;
83  		else
84  			matchers = createMatchers(split(pattern, slash), pathSeparator,
85  					dirOnly);
86  	}
87  
88  	private boolean isSimplePathWithSegments(String path) {
89  		return !isWildCard(path) && path.indexOf('\\') < 0
90  				&& count(path, slash, true) > 0;
91  	}
92  
93  	private static List<IMatcher> createMatchers(List<String> segments,
94  			Character pathSeparator, boolean dirOnly)
95  			throws InvalidPatternException {
96  		List<IMatcher> matchers = new ArrayList<>(segments.size());
97  		for (int i = 0; i < segments.size(); i++) {
98  			String segment = segments.get(i);
99  			IMatcher matcher = createNameMatcher0(segment, pathSeparator,
100 					dirOnly, i == segments.size() - 1);
101 			if (i > 0) {
102 				final IMatcher last = matchers.get(matchers.size() - 1);
103 				if (isWild(matcher) && isWild(last))
104 					// collapse wildmatchers **/** is same as **, but preserve
105 					// dirOnly flag (i.e. always use the last wildmatcher)
106 					matchers.remove(matchers.size() - 1);
107 			}
108 
109 			matchers.add(matcher);
110 		}
111 		return matchers;
112 	}
113 
114 	/**
115 	 * Create path matcher
116 	 *
117 	 * @param pattern
118 	 *            a pattern
119 	 * @param pathSeparator
120 	 *            if this parameter isn't null then this character will not
121 	 *            match at wildcards(* and ? are wildcards).
122 	 * @param dirOnly
123 	 *            a boolean.
124 	 * @return never null
125 	 * @throws org.eclipse.jgit.errors.InvalidPatternException
126 	 */
127 	public static IMatcher createPathMatcher(String pattern,
128 			Character pathSeparator, boolean dirOnly)
129 			throws InvalidPatternException {
130 		pattern = trim(pattern);
131 		char slash = Strings.getPathSeparator(pathSeparator);
132 		// ignore possible leading and trailing slash
133 		int slashIdx = pattern.indexOf(slash, 1);
134 		if (slashIdx > 0 && slashIdx < pattern.length() - 1)
135 			return new PathMatcher(pattern, pathSeparator, dirOnly);
136 		return createNameMatcher0(pattern, pathSeparator, dirOnly, true);
137 	}
138 
139 	/**
140 	 * Trim trailing spaces, unless they are escaped with backslash, see
141 	 * https://www.kernel.org/pub/software/scm/git/docs/gitignore.html
142 	 *
143 	 * @param pattern
144 	 *            non null
145 	 * @return trimmed pattern
146 	 */
147 	private static String trim(String pattern) {
148 		while (pattern.length() > 0
149 				&& pattern.charAt(pattern.length() - 1) == ' ') {
150 			if (pattern.length() > 1
151 					&& pattern.charAt(pattern.length() - 2) == '\\') {
152 				// last space was escaped by backslash: remove backslash and
153 				// keep space
154 				pattern = pattern.substring(0, pattern.length() - 2) + " "; //$NON-NLS-1$
155 				return pattern;
156 			}
157 			pattern = pattern.substring(0, pattern.length() - 1);
158 		}
159 		return pattern;
160 	}
161 
162 	private static IMatcher createNameMatcher0(String segment,
163 			Character pathSeparator, boolean dirOnly, boolean lastSegment)
164 			throws InvalidPatternException {
165 		// check if we see /** or ** segments => double star pattern
166 		if (WildMatcher.WILDMATCH.equals(segment)
167 				|| WildMatcher.WILDMATCH2.equals(segment))
168 			return dirOnly && lastSegment ? WILD_ONLY_DIRECTORY
169 					: WILD_NO_DIRECTORY;
170 
171 		PatternState state = checkWildCards(segment);
172 		switch (state) {
173 		case LEADING_ASTERISK_ONLY:
174 			return new LeadingAsteriskMatcher(segment, pathSeparator, dirOnly);
175 		case TRAILING_ASTERISK_ONLY:
176 			return new TrailingAsteriskMatcher(segment, pathSeparator, dirOnly);
177 		case COMPLEX:
178 			return new WildCardMatcher(segment, pathSeparator, dirOnly);
179 		default:
180 			return new NameMatcher(segment, pathSeparator, dirOnly, true);
181 		}
182 	}
183 
184 	/** {@inheritDoc} */
185 	@Override
186 	public boolean matches(String path, boolean assumeDirectory,
187 			boolean pathMatch) {
188 		if (matchers == null) {
189 			return simpleMatch(path, assumeDirectory, pathMatch);
190 		}
191 		return iterate(path, 0, path.length(), assumeDirectory, pathMatch);
192 	}
193 
194 	/*
195 	 * Stupid but fast string comparison: the case where we don't have to match
196 	 * wildcards or single segments (mean: this is multi-segment path which must
197 	 * be at the beginning of the another string)
198 	 */
199 	private boolean simpleMatch(String path, boolean assumeDirectory,
200 			boolean pathMatch) {
201 		boolean hasSlash = path.indexOf(slash) == 0;
202 		if (beginning && !hasSlash) {
203 			path = slash + path;
204 		}
205 		if (!beginning && hasSlash) {
206 			path = path.substring(1);
207 		}
208 		if (path.equals(pattern)) {
209 			// Exact match: must meet directory expectations
210 			return !dirOnly || assumeDirectory;
211 		}
212 		/*
213 		 * Add slashes for startsWith check. This avoids matching e.g.
214 		 * "/src/new" to /src/newfile" but allows "/src/new" to match
215 		 * "/src/new/newfile", as is the git standard
216 		 */
217 		String prefix = pattern + slash;
218 		if (pathMatch) {
219 			return path.equals(prefix) && (!dirOnly || assumeDirectory);
220 		}
221 		if (path.startsWith(prefix)) {
222 			return true;
223 		}
224 		return false;
225 	}
226 
227 	/** {@inheritDoc} */
228 	@Override
229 	public boolean matches(String segment, int startIncl, int endExcl) {
230 		throw new UnsupportedOperationException(
231 				"Path matcher works only on entire paths"); //$NON-NLS-1$
232 	}
233 
234 	private boolean iterate(final String path, final int startIncl,
235 			final int endExcl, boolean assumeDirectory, boolean pathMatch) {
236 		int matcher = 0;
237 		int right = startIncl;
238 		boolean match = false;
239 		int lastWildmatch = -1;
240 		// ** matches may get extended if a later match fails. When that
241 		// happens, we must extend the ** by exactly one segment.
242 		// wildmatchBacktrackPos records the end of the segment after a **
243 		// match, so that we can reset correctly.
244 		int wildmatchBacktrackPos = -1;
245 		while (true) {
246 			int left = right;
247 			right = path.indexOf(slash, right);
248 			if (right == -1) {
249 				if (left < endExcl) {
250 					match = matches(matcher, path, left, endExcl,
251 							assumeDirectory, pathMatch);
252 				} else {
253 					// a/** should not match a/ or a
254 					match = match && !isWild(matchers.get(matcher));
255 				}
256 				if (match) {
257 					if (matcher < matchers.size() - 1
258 							&& isWild(matchers.get(matcher))) {
259 						// ** can match *nothing*: a/**/b match also a/b
260 						matcher++;
261 						match = matches(matcher, path, left, endExcl,
262 								assumeDirectory, pathMatch);
263 					} else if (dirOnly && !assumeDirectory) {
264 						// Directory expectations not met
265 						return false;
266 					}
267 				}
268 				return match && matcher + 1 == matchers.size();
269 			}
270 			if (wildmatchBacktrackPos < 0) {
271 				wildmatchBacktrackPos = right;
272 			}
273 			if (right - left > 0) {
274 				match = matches(matcher, path, left, right, assumeDirectory,
275 						pathMatch);
276 			} else {
277 				// path starts with slash???
278 				right++;
279 				continue;
280 			}
281 			if (match) {
282 				boolean wasWild = isWild(matchers.get(matcher));
283 				if (wasWild) {
284 					lastWildmatch = matcher;
285 					wildmatchBacktrackPos = -1;
286 					// ** can match *nothing*: a/**/b match also a/b
287 					right = left - 1;
288 				}
289 				matcher++;
290 				if (matcher == matchers.size()) {
291 					// We had a prefix match here.
292 					if (!pathMatch) {
293 						return true;
294 					} else {
295 						if (right == endExcl - 1) {
296 							// Extra slash at the end: actually a full match.
297 							// Must meet directory expectations
298 							return !dirOnly || assumeDirectory;
299 						}
300 						// Prefix matches only if pattern ended with /**
301 						if (wasWild) {
302 							return true;
303 						}
304 						if (lastWildmatch >= 0) {
305 							// Consider pattern **/x and input x/x.
306 							// We've matched the prefix x/ so far: we
307 							// must try to extend the **!
308 							matcher = lastWildmatch + 1;
309 							right = wildmatchBacktrackPos;
310 							wildmatchBacktrackPos = -1;
311 						} else {
312 							return false;
313 						}
314 					}
315 				}
316 			} else if (lastWildmatch != -1) {
317 				matcher = lastWildmatch + 1;
318 				right = wildmatchBacktrackPos;
319 				wildmatchBacktrackPos = -1;
320 			} else {
321 				return false;
322 			}
323 			right++;
324 		}
325 	}
326 
327 	private boolean matches(int matcherIdx, String path, int startIncl,
328 			int endExcl, boolean assumeDirectory, boolean pathMatch) {
329 		IMatcher matcher = matchers.get(matcherIdx);
330 
331 		final boolean matches = matcher.matches(path, startIncl, endExcl);
332 		if (!matches || !pathMatch || matcherIdx < matchers.size() - 1
333 				|| !(matcher instanceof AbstractMatcher)) {
334 			return matches;
335 		}
336 
337 		return assumeDirectory || !((AbstractMatcher) matcher).dirOnly;
338 	}
339 
340 	private static boolean isWild(IMatcher matcher) {
341 		return matcher == WILD_NO_DIRECTORY || matcher == WILD_ONLY_DIRECTORY;
342 	}
343 }