Skip to content

Commit 787faa6

Browse files
Carla232gigel
andauthored
fix(readers): support windows-style exclude patterns (#710)
Signed-off-by: gigel <gigel@users.noreply.github.com> Co-authored-by: gigel <gigel@users.noreply.github.com>
1 parent 288bba0 commit 787faa6

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

pkg/readers/common.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package readers
22

33
import (
44
"path/filepath"
5+
"strings"
56

67
"github.com/bmatcuk/doublestar/v4"
78

@@ -14,23 +15,31 @@ type exclusionMatcher struct {
1415
Exclusions []string
1516
}
1617

18+
func normalizeGlobCandidate(value string) string {
19+
return strings.ReplaceAll(value, "\\", "/")
20+
}
21+
1722
func newPathExclusionMatcher(exclusions []string) *exclusionMatcher {
1823
return &exclusionMatcher{
1924
Exclusions: exclusions,
2025
}
2126
}
2227

2328
func (ex *exclusionMatcher) Match(term string) bool {
29+
normalizedTerm := normalizeGlobCandidate(term)
30+
2431
for _, exclusionPattern := range ex.Exclusions {
32+
normalizedPattern := normalizeGlobCandidate(exclusionPattern)
33+
2534
// Try matching in current form first
26-
if m, err := doublestar.Match(exclusionPattern, term); err == nil && m {
35+
if m, err := doublestar.Match(normalizedPattern, normalizedTerm); err == nil && m {
2736
return true
2837
}
2938

3039
// If term is relative and pattern is absolute, convert term to absolute
3140
if !filepath.IsAbs(term) && filepath.IsAbs(exclusionPattern) {
3241
if abs, err := filepath.Abs(term); err == nil {
33-
if m, err := doublestar.Match(exclusionPattern, abs); err == nil && m {
42+
if m, err := doublestar.Match(normalizedPattern, normalizeGlobCandidate(abs)); err == nil && m {
3443
return true
3544
}
3645
}
@@ -39,7 +48,7 @@ func (ex *exclusionMatcher) Match(term string) bool {
3948
// If term is absolute and pattern is relative, convert pattern to absolute
4049
if filepath.IsAbs(term) && !filepath.IsAbs(exclusionPattern) {
4150
if abs, err := filepath.Abs(exclusionPattern); err == nil {
42-
if m, err := doublestar.Match(abs, term); err == nil && m {
51+
if m, err := doublestar.Match(normalizeGlobCandidate(abs), normalizedTerm); err == nil && m {
4352
return true
4453
}
4554
}

pkg/readers/common_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package readers
22

33
import (
4+
"path/filepath"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -117,3 +118,18 @@ func TestExcludedPath(t *testing.T) {
117118
})
118119
}
119120
}
121+
122+
func TestExcludedPathWindowsStylePatternAgainstAbsolutePath(t *testing.T) {
123+
absPath, err := filepath.Abs(filepath.Join("pkg", "readers"))
124+
assert.NoError(t, err)
125+
126+
matcher := newPathExclusionMatcher([]string{`pkg\*`})
127+
assert.True(t, matcher.Match(absPath))
128+
}
129+
130+
func TestExcludedPathWindowsStyleSeparators(t *testing.T) {
131+
matcher := newPathExclusionMatcher([]string{`pkg\readers\**`})
132+
133+
assert.True(t, matcher.Match(`pkg\readers\fixtures\requirements.txt`))
134+
assert.False(t, matcher.Match(`cmd\vet\main.go`))
135+
}

0 commit comments

Comments
 (0)