@@ -2,7 +2,7 @@ package codeowners
2
2
3
3
import (
4
4
"fmt"
5
- "os "
5
+ "path/filepath "
6
6
"regexp"
7
7
"strings"
8
8
)
@@ -17,7 +17,7 @@ type pattern struct {
17
17
func newPattern (patternStr string ) (pattern , error ) {
18
18
pat := pattern {pattern : patternStr }
19
19
20
- if ! strings .ContainsAny (patternStr , "*?\\ " ) && patternStr [0 ] == os . PathSeparator {
20
+ if ! strings .ContainsAny (patternStr , "*?\\ " ) && patternStr [0 ] == '/' {
21
21
pat .leftAnchoredLiteral = true
22
22
} else {
23
23
patternRegex , err := buildPatternRegex (patternStr )
@@ -32,16 +32,19 @@ func newPattern(patternStr string) (pattern, error) {
32
32
33
33
// match tests if the path provided matches the pattern
34
34
func (p pattern ) match (testPath string ) (bool , error ) {
35
+ // Normalize Windows-style path separators to forward slashes
36
+ testPath = filepath .ToSlash (testPath )
37
+
35
38
if p .leftAnchoredLiteral {
36
39
prefix := p .pattern
37
40
38
41
// Strip the leading slash as we're anchored to the root already
39
- if prefix [0 ] == os . PathSeparator {
42
+ if prefix [0 ] == '/' {
40
43
prefix = prefix [1 :]
41
44
}
42
45
43
46
// If the pattern ends with a slash we can do a simple prefix match
44
- if prefix [len (prefix )- 1 ] == os . PathSeparator {
47
+ if prefix [len (prefix )- 1 ] == '/' {
45
48
return strings .HasPrefix (testPath , prefix ), nil
46
49
}
47
50
@@ -51,7 +54,7 @@ func (p pattern) match(testPath string) (bool, error) {
51
54
}
52
55
53
56
// Otherwise check if the test path is a subdirectory of the pattern
54
- if len (testPath ) > len (prefix ) && testPath [len (prefix )] == os . PathSeparator {
57
+ if len (testPath ) > len (prefix ) && testPath [len (prefix )] == '/' {
55
58
return testPath [:len (prefix )] == prefix , nil
56
59
}
57
60
@@ -95,7 +98,7 @@ func buildPatternRegex(pattern string) (*regexp.Regexp, error) {
95
98
segs [len (segs )- 1 ] = "**"
96
99
}
97
100
98
- sep := string ( os . PathSeparator )
101
+ sep := "/"
99
102
100
103
lastSegIndex := len (segs ) - 1
101
104
needSlash := false
0 commit comments