summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Loskutov <loskutov@gmx.de>2015-07-13 23:58:04 +0200
committerMatthias Sohn <matthias.sohn@sap.com>2015-07-21 00:47:28 +0200
commit1d295cb7fbd48b659b9a1a37e51dfbb8c3f97675 (patch)
tree125149a92af29358fd7f0681bf389908416f9c1b
parentf19b1f2d070d7b63fd7f17cb41025d4b740b6ec1 (diff)
downloadjgit-1d295cb7fbd48b659b9a1a37e51dfbb8c3f97675.tar.gz
jgit-1d295cb7fbd48b659b9a1a37e51dfbb8c3f97675.zip
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>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreRuleSpecialCasesTest.java6
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/PathMatcher.java25
2 files changed, 30 insertions, 1 deletions
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
@@ -836,6 +836,12 @@ public class IgnoreRuleSpecialCasesTest {
}
@Test
+ public void testEscapedTrailingSpaces() throws Exception {
+ assertMatch("\\ ", " ", true);
+ assertMatch("a\\ ", "a ", true);
+ }
+
+ @Test
public void testNotEscapingBackslash() throws Exception {
assertMatch("\\out", "\\out", true);
assertMatch("\\out", "a/\\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 {