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.FastIgnoreRule;
56  import org.eclipse.jgit.ignore.internal.Strings.PatternState;
57  
58  /**
59   * Matcher built by patterns consists of multiple path segments.
60   * <p>
61   * This class is immutable and thread safe.
62   *
63   * @since 3.6
64   */
65  public class PathMatcher extends AbstractMatcher {
66  
67  	private static final WildMatcher WILD = WildMatcher.INSTANCE;
68  
69  	private final List<IMatcher> matchers;
70  
71  	private final char slash;
72  
73  	private boolean beginning;
74  
75  	PathMatcher(String pattern, Character pathSeparator, boolean dirOnly)
76  			throws InvalidPatternException {
77  		super(pattern, dirOnly);
78  		slash = getPathSeparator(pathSeparator);
79  		beginning = pattern.indexOf(slash) == 0;
80  		if (isSimplePathWithSegments(pattern))
81  			matchers = null;
82  		else
83  			matchers = createMatchers(split(pattern, slash), pathSeparator,
84  					dirOnly);
85  	}
86  
87  	private boolean isSimplePathWithSegments(String path) {
88  		return !isWildCard(path) && count(path, slash, true) > 0;
89  	}
90  
91  	static private List<IMatcher> createMatchers(List<String> segments,
92  			Character pathSeparator, boolean dirOnly)
93  			throws InvalidPatternException {
94  		List<IMatcher> matchers = new ArrayList<IMatcher>(segments.size());
95  		for (int i = 0; i < segments.size(); i++) {
96  			String segment = segments.get(i);
97  			IMatcher matcher = createNameMatcher0(segment, pathSeparator,
98  					dirOnly);
99  			if (matcher == WILD && i > 0
100 					&& matchers.get(matchers.size() - 1) == WILD)
101 				// collapse wildmatchers **/** is same as **
102 				continue;
103 			matchers.add(matcher);
104 		}
105 		return matchers;
106 	}
107 
108 	/**
109 	 *
110 	 * @param pattern
111 	 * @param pathSeparator
112 	 *            if this parameter isn't null then this character will not
113 	 *            match at wildcards(* and ? are wildcards).
114 	 * @param dirOnly
115 	 * @return never null
116 	 * @throws InvalidPatternException
117 	 */
118 	public static IMatcher createPathMatcher(String pattern,
119 			Character pathSeparator, boolean dirOnly)
120 			throws InvalidPatternException {
121 		pattern = pattern.trim();
122 		char slash = Strings.getPathSeparator(pathSeparator);
123 		// ignore possible leading and trailing slash
124 		int slashIdx = pattern.indexOf(slash, 1);
125 		if (slashIdx > 0 && slashIdx < pattern.length() - 1)
126 			return new PathMatcher(pattern, pathSeparator, dirOnly);
127 		return createNameMatcher0(pattern, pathSeparator, dirOnly);
128 	}
129 
130 	private static IMatcher createNameMatcher0(String segment,
131 			Character pathSeparator, boolean dirOnly)
132 			throws InvalidPatternException {
133 		// check if we see /** or ** segments => double star pattern
134 		if (WildMatcher.WILDMATCH.equals(segment)
135 				|| WildMatcher.WILDMATCH2.equals(segment))
136 			return WILD;
137 
138 		PatternState state = checkWildCards(segment);
139 		switch (state) {
140 		case LEADING_ASTERISK_ONLY:
141 			return new LeadingAsteriskMatcher(segment, pathSeparator, dirOnly);
142 		case TRAILING_ASTERISK_ONLY:
143 			return new TrailingAsteriskMatcher(segment, pathSeparator, dirOnly);
144 		case COMPLEX:
145 			return new WildCardMatcher(segment, pathSeparator, dirOnly);
146 		default:
147 			return new NameMatcher(segment, pathSeparator, dirOnly);
148 		}
149 	}
150 
151 	public boolean matches(String path, boolean assumeDirectory) {
152 		if (matchers == null)
153 			return simpleMatch(path, assumeDirectory);
154 		return iterate(path, 0, path.length(), assumeDirectory);
155 	}
156 
157 	/*
158 	 * Stupid but fast string comparison: the case where we don't have to match
159 	 * wildcards or single segments (mean: this is multi-segment path which must
160 	 * be at the beginning of the another string)
161 	 */
162 	private boolean simpleMatch(String path, boolean assumeDirectory) {
163 		boolean hasSlash = path.indexOf(slash) == 0;
164 		if (beginning && !hasSlash)
165 			path = slash + path;
166 
167 		if (!beginning && hasSlash)
168 			path = path.substring(1);
169 
170 		if (path.equals(pattern))
171 			// Exact match
172 			if (dirOnly && !assumeDirectory)
173 				// Directory expectations not met
174 				return false;
175 			else
176 				// Directory expectations met
177 				return true;
178 
179 		/*
180 		 * Add slashes for startsWith check. This avoids matching e.g.
181 		 * "/src/new" to /src/newfile" but allows "/src/new" to match
182 		 * "/src/new/newfile", as is the git standard
183 		 */
184 		if (path.startsWith(pattern + FastIgnoreRule.PATH_SEPARATOR))
185 			return true;
186 
187 		return false;
188 	}
189 
190 	public boolean matches(String segment, int startIncl, int endExcl,
191 			boolean assumeDirectory) {
192 		throw new UnsupportedOperationException(
193 				"Path matcher works only on entire paths"); //$NON-NLS-1$
194 	}
195 
196 	boolean iterate(final String path, final int startIncl, final int endExcl,
197 			boolean assumeDirectory) {
198 		int matcher = 0;
199 		int right = startIncl;
200 		boolean match = false;
201 		int lastWildmatch = -1;
202 		while (true) {
203 			int left = right;
204 			right = path.indexOf(slash, right);
205 			if (right == -1) {
206 				if (left < endExcl)
207 					match = matches(matcher, path, left, endExcl,
208 							assumeDirectory);
209 				if (match) {
210 					if (matcher == matchers.size() - 2
211 							&& matchers.get(matcher + 1) == WILD)
212 						// ** can match *nothing*: a/b/** match also a/b
213 						return true;
214 					if (matcher < matchers.size() - 1
215 							&& matchers.get(matcher) == WILD) {
216 						// ** can match *nothing*: a/**/b match also a/b
217 						matcher++;
218 						match = matches(matcher, path, left, endExcl,
219 								assumeDirectory);
220 					} else if (dirOnly && !assumeDirectory)
221 						// Directory expectations not met
222 						return false;
223 				}
224 				return match && matcher + 1 == matchers.size();
225 			}
226 			if (right - left > 0)
227 				match = matches(matcher, path, left, right, assumeDirectory);
228 			else {
229 				// path starts with slash???
230 				right++;
231 				continue;
232 			}
233 			if (match) {
234 				if (matchers.get(matcher) == WILD) {
235 					lastWildmatch = matcher;
236 					// ** can match *nothing*: a/**/b match also a/b
237 					right = left - 1;
238 				}
239 				matcher++;
240 				if (matcher == matchers.size())
241 					return true;
242 			} else if (lastWildmatch != -1)
243 				matcher = lastWildmatch + 1;
244 			else
245 				return false;
246 			right++;
247 		}
248 	}
249 
250 	boolean matches(int matcherIdx, String path, int startIncl, int endExcl,
251 			boolean assumeDirectory) {
252 		IMatcher matcher = matchers.get(matcherIdx);
253 		return matcher.matches(path, startIncl, endExcl, assumeDirectory);
254 	}
255 }