From 7921082ececc9a605534438497f0328547cef87a Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Thu, 19 Feb 2015 09:10:32 +0200 Subject: [PATCH] Correctly handle ^ and $ in ignore patterns (fixes #1365) --- internal/fnmatch/fnmatch.go | 2 ++ internal/fnmatch/fnmatch_test.go | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/internal/fnmatch/fnmatch.go b/internal/fnmatch/fnmatch.go index df19b29d..74a12754 100644 --- a/internal/fnmatch/fnmatch.go +++ b/internal/fnmatch/fnmatch.go @@ -56,6 +56,8 @@ func Convert(pattern string, flags int) (*regexp.Regexp, error) { } pattern = strings.Replace(pattern, ".", "\\.", -1) pattern = strings.Replace(pattern, "+", "\\+", -1) + pattern = strings.Replace(pattern, "$", "\\$", -1) + pattern = strings.Replace(pattern, "^", "\\^", -1) pattern = strings.Replace(pattern, "**", "[:doublestar:]", -1) pattern = strings.Replace(pattern, "*", any+"*", -1) pattern = strings.Replace(pattern, "[:doublestar:]", ".*", -1) diff --git a/internal/fnmatch/fnmatch_test.go b/internal/fnmatch/fnmatch_test.go index 1a7fead7..f12c9859 100644 --- a/internal/fnmatch/fnmatch_test.go +++ b/internal/fnmatch/fnmatch_test.go @@ -62,6 +62,12 @@ var testcases = []testcase{ {"**/foo.txt", "bar/baz/foo.txt", FNM_PATHNAME, true}, {"foo.txt", "foo.TXT", FNM_CASEFOLD, true}, + + // These characters are literals in glob, but not in regexp. + {"hey$hello", "hey$hello", 0, true}, + {"hey^hello", "hey^hello", 0, true}, + {"hey{hello", "hey{hello", 0, true}, + {"hey}hello", "hey}hello", 0, true}, } func TestMatch(t *testing.T) {