Correctly handle (?i) in ignores (fixes #1953)
This commit is contained in:
@@ -38,14 +38,6 @@ func Convert(pattern string, flags int) (*regexp.Regexp, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Support case insensitive ignores. We do the loop because we may in some
|
||||
// circumstances end up with multiple insensitivity prefixes
|
||||
// ("(?i)(?i)foo"), which should be accepted.
|
||||
for ignore := strings.TrimPrefix(pattern, "(?i)"); ignore != pattern; ignore = strings.TrimPrefix(pattern, "(?i)") {
|
||||
flags |= CaseFold
|
||||
pattern = ignore
|
||||
}
|
||||
|
||||
if flags&NoEscape != 0 {
|
||||
pattern = strings.Replace(pattern, "\\", "\\\\", -1)
|
||||
} else {
|
||||
|
||||
@@ -53,10 +53,6 @@ var testcases = []testcase{
|
||||
{"**/foo.txt", "bar/baz/foo.txt", PathName, true},
|
||||
|
||||
{"foo.txt", "foo.TXT", CaseFold, true},
|
||||
{"(?i)foo.txt", "foo.TXT", 0, true},
|
||||
{"(?i)(?i)foo.txt", "foo.TXT", 0, true}, // repeated prefix should be fine
|
||||
{"(?i)**foo.txt", "/dev/tmp/foo.TXT", 0, true},
|
||||
{"(?i)!**foo.txt", "/dev/tmp/foo.TXT", 0, false},
|
||||
|
||||
// These characters are literals in glob, but not in regexp.
|
||||
{"hey$hello", "hey$hello", 0, true},
|
||||
|
||||
@@ -206,22 +206,28 @@ func parseIgnoreFile(fd io.Reader, currentFile string, seen map[string]bool) ([]
|
||||
include = false
|
||||
}
|
||||
|
||||
flags := fnmatch.PathName
|
||||
if strings.HasPrefix(line, "(?i)") {
|
||||
line = line[4:]
|
||||
flags |= fnmatch.CaseFold
|
||||
}
|
||||
|
||||
if strings.HasPrefix(line, "/") {
|
||||
// Pattern is rooted in the current dir only
|
||||
exp, err := fnmatch.Convert(line[1:], fnmatch.PathName)
|
||||
exp, err := fnmatch.Convert(line[1:], flags)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
||||
}
|
||||
patterns = append(patterns, Pattern{exp, include})
|
||||
} else if strings.HasPrefix(line, "**/") {
|
||||
// Add the pattern as is, and without **/ so it matches in current dir
|
||||
exp, err := fnmatch.Convert(line, fnmatch.PathName)
|
||||
exp, err := fnmatch.Convert(line, flags)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
||||
}
|
||||
patterns = append(patterns, Pattern{exp, include})
|
||||
|
||||
exp, err = fnmatch.Convert(line[3:], fnmatch.PathName)
|
||||
exp, err = fnmatch.Convert(line[3:], flags)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
||||
}
|
||||
@@ -236,13 +242,13 @@ func parseIgnoreFile(fd io.Reader, currentFile string, seen map[string]bool) ([]
|
||||
} else {
|
||||
// Path name or pattern, add it so it matches files both in
|
||||
// current directory and subdirs.
|
||||
exp, err := fnmatch.Convert(line, fnmatch.PathName)
|
||||
exp, err := fnmatch.Convert(line, flags)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
||||
}
|
||||
patterns = append(patterns, Pattern{exp, include})
|
||||
|
||||
exp, err = fnmatch.Convert("**/"+line, fnmatch.PathName)
|
||||
exp, err = fnmatch.Convert("**/"+line, flags)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user