Browse Source

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>
stable-4.1
Andrey Loskutov 10 years ago committed by Matthias Sohn
parent
commit
1d295cb7fb
  1. 6
      org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreRuleSpecialCasesTest.java
  2. 25
      org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/PathMatcher.java

6
org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreRuleSpecialCasesTest.java

@ -835,6 +835,12 @@ public class IgnoreRuleSpecialCasesTest {
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);

25
org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/PathMatcher.java

@ -118,7 +118,7 @@ public class PathMatcher extends AbstractMatcher {
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 class PathMatcher extends AbstractMatcher {
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 {

Loading…
Cancel
Save