Don't trim trailing space if it is escaped with backslash

According to [1] backslash can escape trailing space in ignore rules.

[1] https://www.kernel.org/pub/software/scm/git/docs/gitignore.html

Bug: 463581
Change-Id: I9cf13f8775cb49f0b6d61cfd3ca3fd6d665fccd8
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
This commit is contained in:
Andrey Loskutov 2015-07-13 23:58:04 +02:00 committed by Matthias Sohn
parent f19b1f2d07
commit 1d295cb7fb
2 changed files with 30 additions and 1 deletions

View File

@ -835,6 +835,12 @@ public void testEscapedBackslash() throws Exception {
assertMatch("a\\\\b", "a\\b", true);
}
@Test
public void testEscapedTrailingSpaces() throws Exception {
assertMatch("\\ ", " ", true);
assertMatch("a\\ ", "a ", true);
}
@Test
public void testNotEscapingBackslash() throws Exception {
assertMatch("\\out", "\\out", true);

View File

@ -118,7 +118,7 @@ static private List<IMatcher> createMatchers(List<String> segments,
public static IMatcher createPathMatcher(String pattern,
Character pathSeparator, boolean dirOnly)
throws InvalidPatternException {
pattern = pattern.trim();
pattern = trim(pattern);
char slash = Strings.getPathSeparator(pathSeparator);
// ignore possible leading and trailing slash
int slashIdx = pattern.indexOf(slash, 1);
@ -127,6 +127,29 @@ public static IMatcher createPathMatcher(String pattern,
return createNameMatcher0(pattern, pathSeparator, dirOnly);
}
/**
* Trim trailing spaces, unless they are escaped with backslash, see
* https://www.kernel.org/pub/software/scm/git/docs/gitignore.html
*
* @param pattern
* non null
* @return trimmed pattern
*/
private static String trim(String pattern) {
while (pattern.length() > 0
&& pattern.charAt(pattern.length() - 1) == ' ') {
if (pattern.length() > 1
&& pattern.charAt(pattern.length() - 2) == '\\') {
// last space was escaped by backslash: remove backslash and
// keep space
pattern = pattern.substring(0, pattern.length() - 2) + " "; //$NON-NLS-1$
return pattern;
}
pattern = pattern.substring(0, pattern.length() - 1);
}
return pattern;
}
private static IMatcher createNameMatcher0(String segment,
Character pathSeparator, boolean dirOnly)
throws InvalidPatternException {