Browse Source

Correctly handle ^ and $ in ignore patterns (fixes #1365)

Jakob Borg 10 years ago
parent
commit
7921082ece
2 changed files with 8 additions and 0 deletions
  1. 2 0
      internal/fnmatch/fnmatch.go
  2. 6 0
      internal/fnmatch/fnmatch_test.go

+ 2 - 0
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)

+ 6 - 0
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) {