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>
This commit is contained in:
Andrey Loskutov 2015-07-14 00:05:49 +02:00 committed by Matthias Sohn
parent 1d295cb7fb
commit 08641ea413
2 changed files with 36 additions and 17 deletions

View File

@ -835,6 +835,18 @@ public void testEscapedBackslash() throws Exception {
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
public void testEscapedTrailingSpaces() throws Exception {
assertMatch("\\ ", " ", true);

View File

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