Browse Source

Consider only escaping backslash for regular expressions in ignore rules

While checking if we should consider an ignore rule without '[]'
brackets as a regular expression, check if the backslash escapes one of
the glob special characters '?', '*', '[', '\\'. If not, backslash is
not a part of a regex and should be treated literally.

Bug: 463581
Change-Id: I85208c7f85246fbf6c5029ce3c8b7bb8f4dbd947
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
stable-4.1
Andrey Loskutov 10 years ago committed by Matthias Sohn
parent
commit
f19b1f2d07
  1. 10
      org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreRuleSpecialCasesTest.java
  2. 20
      org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/Strings.java

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

@ -835,6 +835,16 @@ public class IgnoreRuleSpecialCasesTest {
assertMatch("a\\\\b", "a\\b", true);
}
@Test
public void testNotEscapingBackslash() throws Exception {
assertMatch("\\out", "\\out", true);
assertMatch("\\out", "a/\\out", true);
assertMatch("c:\\/", "c:\\/", true);
assertMatch("c:\\/", "a/c:\\/", true);
assertMatch("c:\\tmp", "c:\\tmp", true);
assertMatch("c:\\tmp", "a/c:\\tmp", true);
}
@Test
public void testMultipleEscapedCharacters1() throws Exception {
assertMatch("\\]a?c\\*\\[d\\?\\]", "]abc*[d?]", true);

20
org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/Strings.java

@ -142,9 +142,27 @@ public class Strings {
if (idx2 > idx1)
return true;
}
if (pattern.indexOf('?') != -1) {
return true;
} else {
// check if the backslash escapes one of the glob special characters
// if not, backslash is not part of a regex and treated literally
int backSlash = pattern.indexOf('\\');
if (backSlash >= 0) {
int nextIdx = backSlash + 1;
if (pattern.length() == nextIdx) {
return false;
}
char nextChar = pattern.charAt(nextIdx);
if (nextChar == '?' || nextChar == '*' || nextChar == '['
// required to match escaped backslashes '\\\\'
if (pattern.indexOf('?') != -1 || pattern.indexOf('\\') != -1)
|| nextChar == '\\') {
return true;
} else {
return false;
}
}
}
return false;
}

Loading…
Cancel
Save