Skip to content

Commit

Permalink
Merge pull request #16 from hmarr/windows-ci
Browse files Browse the repository at this point in the history
Windows support
  • Loading branch information
Harry Marr authored Feb 5, 2023
2 parents e554638 + fbcff39 commit bd19ed7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 9 additions & 6 deletions match.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package codeowners

import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
Expand All @@ -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)
Expand All @@ -32,16 +32,19 @@ 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

// 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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -95,7 +98,7 @@ func buildPatternRegex(pattern string) (*regexp.Regexp, error) {
segs[len(segs)-1] = "**"
}

sep := string(os.PathSeparator)
sep := "/"

lastSegIndex := len(segs) - 1
needSlash := false
Expand Down

0 comments on commit bd19ed7

Please sign in to comment.