Browse Source

Properly support special regex characters in ignore rules

Ignore rules should escape $^(){}+| chars if using regular expressions,
because they should be treated literally if they aren't part of a
character group.

Bug: 478055
Change-Id: Ic7276442d7f8f02594b85eae1ef697362e62d3bd
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
tags/v4.1.0.201509280440-r
Andrey Loskutov 8 years ago
parent
commit
97ef1fe893

+ 78
- 0
org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreRuleSpecialCasesTest.java View File

@@ -868,6 +868,84 @@ public class IgnoreRuleSpecialCasesTest {
assertMatch("\\]a?c\\*\\[d\\?\\]", "]abc*[d?]", true);
}

@Test
public void testDollar() throws Exception {
assertMatch("$", "$", true);
assertMatch("$x", "$x", true);
assertMatch("$x", "x$", false);
assertMatch("$x", "$", false);

assertMatch("$x.*", "$x.a", true);
assertMatch("*$", "x$", true);
assertMatch("*.$", "x.$", true);

assertMatch("$*x", "$ax", true);
assertMatch("x*$", "xa$", true);
assertMatch("x*$", "xa", false);
assertMatch("[a$b]", "$", true);
}

@Test
public void testCaret() throws Exception {
assertMatch("^", "^", true);
assertMatch("^x", "^x", true);
assertMatch("^x", "x^", false);
assertMatch("^x", "^", false);

assertMatch("^x.*", "^x.a", true);
assertMatch("*^", "x^", true);
assertMatch("*.^", "x.^", true);

assertMatch("x*^", "xa^", true);
assertMatch("^*x", "^ax", true);
assertMatch("^*x", "ax", false);
assertMatch("[a^b]", "^", true);
}

@Test
public void testPlus() throws Exception {
assertMatch("+", "+", true);
assertMatch("+x", "+x", true);
assertMatch("+x", "x+", false);
assertMatch("+x", "+", false);
assertMatch("x+", "xx", false);

assertMatch("+x.*", "+x.a", true);
assertMatch("*+", "x+", true);
assertMatch("*.+", "x.+", true);

assertMatch("x*+", "xa+", true);
assertMatch("+*x", "+ax", true);
assertMatch("+*x", "ax", false);
assertMatch("[a+b]", "+", true);
}

@Test
public void testPipe() throws Exception {
assertMatch("|", "|", true);
assertMatch("|x", "|x", true);
assertMatch("|x", "x|", false);
assertMatch("|x", "|", false);
assertMatch("x|x", "xx", false);

assertMatch("x|x.*", "x|x.a", true);
assertMatch("*|", "x|", true);
assertMatch("*.|", "x.|", true);

assertMatch("x*|a", "xb|a", true);
assertMatch("b|*x", "b|ax", true);
assertMatch("b|*x", "ax", false);
assertMatch("[a|b]", "|", true);
}

@Test
public void testBrackets() throws Exception {
assertMatch("{}*()", "{}x()", true);
assertMatch("[a{}()b][a{}()b]?[a{}()b][a{}()b]", "{}x()", true);
assertMatch("x*{x}3", "xa{x}3", true);
assertMatch("a*{x}3", "axxx", false);
}

@Test
public void testFilePathSimpleCase() throws Exception {
assertFileNameMatch("a/b", "a/b", true);

+ 15
- 1
org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/Strings.java View File

@@ -247,7 +247,7 @@ public class Strings {
char[] charClass = new char[6];

for (int i = 0; i < pattern.length(); i++) {
char c = pattern.charAt(i);
final char c = pattern.charAt(i);
switch (c) {

case '*':
@@ -257,6 +257,20 @@ public class Strings {
sb.append('.').append(c);
break;

case '(': // fall-through
case ')': // fall-through
case '{': // fall-through
case '}': // fall-through
case '+': // fall-through
case '$': // fall-through
case '^': // fall-through
case '|':
if (seenEscape || in_brackets > 0)
sb.append(c);
else
sb.append('\\').append(c);
break;

case '.':
if (seenEscape)
sb.append(c);

Loading…
Cancel
Save