Browse Source

Allow leading literal '#' and '!' in ignore rules if they are escaped

According to [1] backslash can escape leading special characters '#' and
'!' in ignore rules, so that they are treated literally.

[1] https://www.kernel.org/pub/software/scm/git/docs/gitignore.html

Bug: 463581
Change-Id: I4c02927413a9c63ea5dbf2954877080d902ec1b2
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
stable-4.1
Andrey Loskutov 10 years ago committed by Matthias Sohn
parent
commit
08641ea413
  1. 12
      org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreRuleSpecialCasesTest.java
  2. 37
      org.eclipse.jgit/src/org/eclipse/jgit/ignore/FastIgnoreRule.java

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

@ -835,6 +835,18 @@ public class IgnoreRuleSpecialCasesTest {
assertMatch("a\\\\b", "a\\b", true); assertMatch("a\\\\b", "a\\b", true);
} }
@Test
public void testEscapedExclamationMark() throws Exception {
assertMatch("\\!b!.txt", "!b!.txt", true);
assertMatch("a\\!b!.txt", "a\\!b!.txt", true);
}
@Test
public void testEscapedHash() throws Exception {
assertMatch("\\#b", "#b", true);
assertMatch("a\\#", "a\\#", true);
}
@Test @Test
public void testEscapedTrailingSpaces() throws Exception { public void testEscapedTrailingSpaces() throws Exception {
assertMatch("\\ ", " ", true); assertMatch("\\ ", " ", true);

37
org.eclipse.jgit/src/org/eclipse/jgit/ignore/FastIgnoreRule.java

@ -98,24 +98,31 @@ public class FastIgnoreRule {
if (pattern.charAt(0) == '#') { if (pattern.charAt(0) == '#') {
this.matcher = NO_MATCH; this.matcher = NO_MATCH;
dirOnly = false; dirOnly = false;
} else { return;
dirOnly = pattern.charAt(pattern.length() - 1) == PATH_SEPARATOR; }
if (dirOnly) { if (pattern.charAt(0) == '\\' && pattern.length() > 1) {
pattern = stripTrailing(pattern, PATH_SEPARATOR); char next = pattern.charAt(1);
if (pattern.length() == 0) { if (next == '!' || next == '#') {
this.matcher = NO_MATCH; // remove backslash escaping first special characters
return; pattern = pattern.substring(1);
}
} }
IMatcher m; }
try { dirOnly = pattern.charAt(pattern.length() - 1) == PATH_SEPARATOR;
m = PathMatcher.createPathMatcher(pattern, if (dirOnly) {
Character.valueOf(PATH_SEPARATOR), dirOnly); pattern = stripTrailing(pattern, PATH_SEPARATOR);
} catch (InvalidPatternException e) { if (pattern.length() == 0) {
m = NO_MATCH; this.matcher = NO_MATCH;
return;
} }
this.matcher = m;
} }
IMatcher m;
try {
m = PathMatcher.createPathMatcher(pattern,
Character.valueOf(PATH_SEPARATOR), dirOnly);
} catch (InvalidPatternException e) {
m = NO_MATCH;
}
this.matcher = m;
} }
/** /**

Loading…
Cancel
Save