From b59a2244826dc6eab1c7919f8e660c428a655392 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Thu, 2 Feb 2023 21:29:01 -0800 Subject: [PATCH 1/3] Windows CI --- .github/workflows/ci.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bc3f2fa..b20642b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,9 +9,11 @@ on: jobs: build: name: Build - runs-on: ubuntu-latest + strategy: + matrix: + os: [ubuntu-latest, windows-latest] + runs-on: ${{ matrix.os }} steps: - - name: Set up Go 1.x uses: actions/setup-go@v3 with: From 09e871da9101d5336d6fd9fd50b5685dbc08304a Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Thu, 2 Feb 2023 21:37:47 -0800 Subject: [PATCH 2/3] Always use forward slashes in patterns --- match.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/match.go b/match.go index 84868be..dcab5d8 100644 --- a/match.go +++ b/match.go @@ -17,7 +17,7 @@ type pattern struct { func newPattern(patternStr string) (pattern, error) { pat := pattern{pattern: patternStr} - if !strings.ContainsAny(patternStr, "*?\\") && patternStr[0] == os.PathSeparator { + if !strings.ContainsAny(patternStr, "*?\\") && patternStr[0] == '/' { pat.leftAnchoredLiteral = true } else { patternRegex, err := buildPatternRegex(patternStr) @@ -36,12 +36,12 @@ func (p pattern) match(testPath string) (bool, error) { prefix := p.pattern // Strip the leading slash as we're anchored to the root already - if prefix[0] == os.PathSeparator { + if prefix[0] == '/' { prefix = prefix[1:] } // If the pattern ends with a slash we can do a simple prefix match - if prefix[len(prefix)-1] == os.PathSeparator { + if prefix[len(prefix)-1] == '/' { return strings.HasPrefix(testPath, prefix), nil } @@ -95,7 +95,7 @@ func buildPatternRegex(pattern string) (*regexp.Regexp, error) { segs[len(segs)-1] = "**" } - sep := string(os.PathSeparator) + sep := "/" lastSegIndex := len(segs) - 1 needSlash := false From fbcff3984a7ad67847b1fe50f2991772b3644ed0 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Thu, 2 Feb 2023 21:48:54 -0800 Subject: [PATCH 3/3] Normalize Windows-style path separators to forward slashes --- match.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/match.go b/match.go index dcab5d8..4c34638 100644 --- a/match.go +++ b/match.go @@ -2,7 +2,7 @@ package codeowners import ( "fmt" - "os" + "path/filepath" "regexp" "strings" ) @@ -32,6 +32,9 @@ func newPattern(patternStr string) (pattern, error) { // match tests if the path provided matches the pattern func (p pattern) match(testPath string) (bool, error) { + // Normalize Windows-style path separators to forward slashes + testPath = filepath.ToSlash(testPath) + if p.leftAnchoredLiteral { prefix := p.pattern @@ -51,7 +54,7 @@ func (p pattern) match(testPath string) (bool, error) { } // Otherwise check if the test path is a subdirectory of the pattern - if len(testPath) > len(prefix) && testPath[len(prefix)] == os.PathSeparator { + if len(testPath) > len(prefix) && testPath[len(prefix)] == '/' { return testPath[:len(prefix)] == prefix, nil }