Skip to content

Commit d42dde8

Browse files
committed
handle backslashes correctly
Signed-off-by: Miguel Molina <[email protected]>
1 parent 1e1a095 commit d42dde8

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

path_utils.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ import (
1212
var RegMatchChars = regexp.MustCompile(`(^|[^\\])([*[?])`)
1313

1414
// StripPrefix removes the root path from the given path. Root may be a glob.
15+
// The returned string has all backslashes replaced with slashes.
1516
func StripPrefix(root, path string) (string, error) {
1617
var err error
1718
root, err = filepath.Abs(cleanGlob(root))
1819
if err != nil {
1920
return "", err
2021
}
2122

22-
if !strings.HasSuffix(root, string(filepath.Separator)) {
23+
if !strings.HasSuffix(root, "/") {
2324
root += string(filepath.Separator)
2425
}
2526

@@ -28,21 +29,23 @@ func StripPrefix(root, path string) (string, error) {
2829
return "", err
2930
}
3031

31-
return strings.TrimPrefix(path, root), nil
32+
return strings.TrimPrefix(filepath.ToSlash(path), root), nil
3233
}
3334

34-
// cleanGlob removes all the parts of a glob that are not fixed.
35+
// cleanGlob removes all the parts of a glob that are not fixed. It also
36+
// converts all slashes or backslashes to /.
3537
func cleanGlob(pattern string) string {
38+
pattern = filepath.ToSlash(pattern)
3639
var parts []string
37-
for _, part := range strings.Split(pattern, string(filepath.Separator)) {
40+
for _, part := range strings.Split(pattern, "/") {
3841
if strings.ContainsAny(part, "*?[\\") {
3942
break
4043
}
4144

4245
parts = append(parts, part)
4346
}
4447

45-
return strings.Join(parts, string(filepath.Separator))
48+
return strings.Join(parts, "/")
4649
}
4750

4851
// PatternMatches returns the paths matched and any error found.

0 commit comments

Comments
 (0)