From 1d295cb7fbd48b659b9a1a37e51dfbb8c3f97675 Mon Sep 17 00:00:00 2001 From: Andrey Loskutov Date: Mon, 13 Jul 2015 23:58:04 +0200 Subject: 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 --- .../jgit/ignore/IgnoreRuleSpecialCasesTest.java | 6 ++++++ .../eclipse/jgit/ignore/internal/PathMatcher.java | 25 +++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreRuleSpecialCasesTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreRuleSpecialCasesTest.java index ee78b18da5..4916d40545 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreRuleSpecialCasesTest.java +++ b/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); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/PathMatcher.java b/org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/PathMatcher.java index d3e5f6a053..6b9b5a806b 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/PathMatcher.java +++ b/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 { -- cgit v1.2.3