Skip to content

Commit a1f32fa

Browse files
committed
fix: add directory pattern matching to MatchPaths
Add fallback logic to match files within directory patterns (e.g., "pkg/header/" matches "pkg/header/check.go") to behave consistently with tryMatchPatten in config.go. Test pattern without trailing slash now correctly returns false.
1 parent 174e7c7 commit a1f32fa

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pkg/header/check.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ func MatchPaths(file string, patterns []string) bool {
162162
if err == nil && matched {
163163
return true
164164
}
165+
// Fallback for directory patterns (e.g., "pkg/header/" or "pkg/header")
166+
// to behave consistently with tryMatchPatten in config.go.
167+
// Ensure that pattern ends with a path separator so it clearly denotes a directory.
168+
if strings.HasSuffix(pattern, "/") ||
169+
strings.HasSuffix(pattern, string(os.PathSeparator)) {
170+
// Treat as directory pattern - match files by prefix
171+
// Normalize both paths to use '/' so prefix matching is consistent across platforms.
172+
normalizedDir := strings.ReplaceAll(pattern, "\\", "/")
173+
normalizedFile := strings.ReplaceAll(file, "\\", "/")
174+
if strings.HasPrefix(normalizedFile, normalizedDir) {
175+
return true
176+
}
177+
}
165178
}
166179
return false
167180
}

pkg/header/check_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,12 @@ func TestMatchPaths(t *testing.T) {
315315
name: "Directory pattern with trailing slash",
316316
file: "pkg/header/check.go",
317317
patterns: []string{"pkg/header/"},
318+
expected: true,
319+
},
320+
{
321+
name: "Directory pattern without trailing slash",
322+
file: "pkg/header/check.go",
323+
patterns: []string{"pkg/header}"},
318324
expected: false,
319325
},
320326
{

0 commit comments

Comments
 (0)