diff --git a/internal/fnmatch/fnmatch.go b/internal/fnmatch/fnmatch.go index d2a0b302..0882f30f 100644 --- a/internal/fnmatch/fnmatch.go +++ b/internal/fnmatch/fnmatch.go @@ -14,9 +14,9 @@ import ( ) const ( - FNM_NOESCAPE = (1 << iota) - FNM_PATHNAME - FNM_CASEFOLD + NoEscape = (1 << iota) + PathName + CaseFold ) func Convert(pattern string, flags int) (*regexp.Regexp, error) { @@ -24,16 +24,16 @@ func Convert(pattern string, flags int) (*regexp.Regexp, error) { switch runtime.GOOS { case "windows": - flags |= FNM_NOESCAPE | FNM_CASEFOLD + flags |= NoEscape | CaseFold pattern = filepath.FromSlash(pattern) - if flags&FNM_PATHNAME != 0 { + if flags&PathName != 0 { any = "[^\\\\]" } case "darwin": - flags |= FNM_CASEFOLD + flags |= CaseFold fallthrough default: - if flags&FNM_PATHNAME != 0 { + if flags&PathName != 0 { any = "[^/]" } } @@ -41,11 +41,11 @@ func Convert(pattern string, flags int) (*regexp.Regexp, error) { // Support case insensitive ignores ignore := strings.TrimPrefix(pattern, "(?i)") if ignore != pattern { - flags |= FNM_CASEFOLD + flags |= CaseFold pattern = ignore } - if flags&FNM_NOESCAPE != 0 { + if flags&NoEscape != 0 { pattern = strings.Replace(pattern, "\\", "\\\\", -1) } else { pattern = strings.Replace(pattern, "\\*", "[:escapedstar:]", -1) @@ -69,7 +69,7 @@ func Convert(pattern string, flags int) (*regexp.Regexp, error) { pattern = strings.Replace(pattern, "[:escapeddot:]", "\\.", -1) pattern = "^" + pattern + "$" - if flags&FNM_CASEFOLD != 0 { + if flags&CaseFold != 0 { pattern = "(?i)" + pattern } return regexp.Compile(pattern) diff --git a/internal/fnmatch/fnmatch_test.go b/internal/fnmatch/fnmatch_test.go index cbba8634..07486e65 100644 --- a/internal/fnmatch/fnmatch_test.go +++ b/internal/fnmatch/fnmatch_test.go @@ -41,18 +41,18 @@ var testcases = []testcase{ {"f[ab]o.txt", "fco.txt", 0, false}, {"f[ab]o.txt", "fabo.txt", 0, false}, {"f[ab]o.txt", "f[ab]o.txt", 0, false}, - {"f\\[ab\\]o.txt", "f[ab]o.txt", FNM_NOESCAPE, false}, + {"f\\[ab\\]o.txt", "f[ab]o.txt", NoEscape, false}, {"*foo.txt", "bar/foo.txt", 0, true}, - {"*foo.txt", "bar/foo.txt", FNM_PATHNAME, false}, + {"*foo.txt", "bar/foo.txt", PathName, false}, {"*/foo.txt", "bar/foo.txt", 0, true}, - {"*/foo.txt", "bar/foo.txt", FNM_PATHNAME, true}, + {"*/foo.txt", "bar/foo.txt", PathName, true}, {"*/foo.txt", "bar/baz/foo.txt", 0, true}, - {"*/foo.txt", "bar/baz/foo.txt", FNM_PATHNAME, false}, + {"*/foo.txt", "bar/baz/foo.txt", PathName, false}, {"**/foo.txt", "bar/baz/foo.txt", 0, true}, - {"**/foo.txt", "bar/baz/foo.txt", FNM_PATHNAME, true}, + {"**/foo.txt", "bar/baz/foo.txt", PathName, true}, - {"foo.txt", "foo.TXT", FNM_CASEFOLD, true}, + {"foo.txt", "foo.TXT", CaseFold, true}, {"foo.txt", "foo.TXT", 0, false}, {"(?i)foo.txt", "foo.TXT", 0, true}, @@ -78,7 +78,7 @@ func TestMatch(t *testing.T) { testcases = append(testcases, testcase{"f\\[ab\\]o.txt", "f[ab]o.txt", 0, true}) testcases = append(testcases, testcase{"foo\\.txt", "foo.txt", 0, true}) testcases = append(testcases, testcase{"foo\\*.txt", "foo*.txt", 0, true}) - testcases = append(testcases, testcase{"foo\\.txt", "foo.txt", FNM_NOESCAPE, false}) + testcases = append(testcases, testcase{"foo\\.txt", "foo.txt", NoEscape, false}) testcases = append(testcases, testcase{"f\\\\\\[ab\\\\\\]o.txt", "f\\[ab\\]o.txt", 0, true}) } diff --git a/internal/ignore/ignore.go b/internal/ignore/ignore.go index 6a9b343f..866d0a74 100644 --- a/internal/ignore/ignore.go +++ b/internal/ignore/ignore.go @@ -200,20 +200,20 @@ func parseIgnoreFile(fd io.Reader, currentFile string, seen map[string]bool) ([] if strings.HasPrefix(line, "/") { // Pattern is rooted in the current dir only - exp, err := fnmatch.Convert(line[1:], fnmatch.FNM_PATHNAME) + exp, err := fnmatch.Convert(line[1:], fnmatch.PathName) 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.FNM_PATHNAME) + exp, err := fnmatch.Convert(line, fnmatch.PathName) 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.FNM_PATHNAME) + exp, err = fnmatch.Convert(line[3:], fnmatch.PathName) if err != nil { return fmt.Errorf("Invalid pattern %q in ignore file", line) } @@ -228,13 +228,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.FNM_PATHNAME) + exp, err := fnmatch.Convert(line, fnmatch.PathName) 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.FNM_PATHNAME) + exp, err = fnmatch.Convert("**/"+line, fnmatch.PathName) if err != nil { return fmt.Errorf("Invalid pattern %q in ignore file", line) }