diff options
author | Daniel Martí <mvdan@mvdan.cc> | 2018-01-11 20:23:22 +0000 |
---|---|---|
committer | Daniel Martí <mvdan@mvdan.cc> | 2018-02-14 11:47:59 +0000 |
commit | 03f27d5f581bac5b40f8c870a9b316d6c814c356 (patch) | |
tree | a13667740250841d62271ff3eaedd9e68111adfd /src/path | |
parent | 821b04dafbc5e94223766b15622d9b7b38b2f576 (diff) | |
download | go-git-03f27d5f581bac5b40f8c870a9b316d6c814c356.tar.gz |
path/filepath: fix escaped chars in Glob on non-Windows
Backslashes are ignored in Match and Glob on Windows, since those
collide with the separator character. However, they should still work in
both functions on other operating systems.
hasMeta did not reflect this logic - it always treated a backslash as a
non-special character. Do that only on Windows.
Assuming this is what the TODO was referring to, remove it. There are no
other characters that scanChunk treats especially.
Fixes #23418.
Change-Id: Ie0bd795812e0ed9d8c8c1bbc3137f29d960cba84
Reviewed-on: https://go-review.googlesource.com/87455
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/path')
-rw-r--r-- | src/path/filepath/match.go | 7 | ||||
-rw-r--r-- | src/path/filepath/match_test.go | 16 |
2 files changed, 21 insertions, 2 deletions
diff --git a/src/path/filepath/match.go b/src/path/filepath/match.go index 057f7f3677..46badb5e84 100644 --- a/src/path/filepath/match.go +++ b/src/path/filepath/match.go @@ -339,6 +339,9 @@ func glob(dir, pattern string, matches []string) (m []string, e error) { // hasMeta reports whether path contains any of the magic characters // recognized by Match. func hasMeta(path string) bool { - // TODO(niemeyer): Should other magic characters be added here? - return strings.ContainsAny(path, "*?[") + magicChars := `*?[` + if runtime.GOOS != "windows" { + magicChars = `*?[\` + } + return strings.ContainsAny(path, magicChars) } diff --git a/src/path/filepath/match_test.go b/src/path/filepath/match_test.go index 18d38bf5bb..1d91c274c7 100644 --- a/src/path/filepath/match_test.go +++ b/src/path/filepath/match_test.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "os" . "path/filepath" + "reflect" "runtime" "sort" "strings" @@ -371,3 +372,18 @@ func TestWindowsGlob(t *testing.T) { } } } + +func TestNonWindowsGlobEscape(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skipf("skipping non-windows specific test") + } + pattern := `\match.go` + want := []string{"match.go"} + matches, err := Glob(pattern) + if err != nil { + t.Fatalf("Glob error for %q: %s", pattern, err) + } + if !reflect.DeepEqual(matches, want) { + t.Fatalf("Glob(%#q) = %v want %v", pattern, matches, want) + } +} |