Skip to content

Commit c7b5b8e

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 9d41d69 commit c7b5b8e

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

pkg/header/check.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import (
4040
"golang.org/x/sync/errgroup"
4141
)
4242

43+
const currentDir = "./"
44+
4345
// Check checks the license headers of the specified paths/globs.
4446
func Check(config *ConfigHeader, result *Result) error {
4547
fileList, err := listFiles(config)
@@ -67,13 +69,13 @@ func Check(config *ConfigHeader, result *Result) error {
6769
func listFiles(config *ConfigHeader) ([]string, error) {
6870
var fileList []string
6971

70-
repo, err := git.PlainOpen("./")
72+
repo, err := git.PlainOpen(currentDir)
7173

7274
if err != nil { // we're not in a Git workspace, fallback to glob paths
7375
var localFileList []string
7476
for _, pattern := range config.Paths {
7577
if pattern == "." {
76-
pattern = "./"
78+
pattern = currentDir
7779
}
7880
files, err := doublestar.Glob(pattern)
7981
if err != nil {
@@ -156,12 +158,32 @@ func listFiles(config *ConfigHeader) ([]string, error) {
156158
func MatchPaths(file string, patterns []string) bool {
157159
for _, pattern := range patterns {
158160
if pattern == "." {
159-
pattern = "./"
161+
pattern = currentDir
160162
}
161163
matched, err := doublestar.Match(pattern, file)
162164
if err == nil && matched {
163165
return true
164166
}
167+
// Fallback for directory patterns (e.g., "pkg/header/" or "pkg/header")
168+
// to behave consistently with tryMatchPatten in config.go.
169+
// Ensure that pattern ends with a path separator so it clearly denotes a directory.
170+
if strings.HasSuffix(pattern, "/") ||
171+
strings.HasSuffix(pattern, string(os.PathSeparator)) {
172+
dirPattern := strings.TrimRight(pattern, "/")
173+
// Match if file's directory name equals pattern (exact directory match)
174+
if stat, err := os.Stat(file); err == nil {
175+
if stat.Name() == dirPattern {
176+
return true
177+
}
178+
}
179+
// Match if file is under the directory (prefix match)
180+
// Normalize both paths to use '/' so prefix matching is consistent across platforms.
181+
normalizedDir := strings.ReplaceAll(pattern, "\\", "/")
182+
normalizedFile := strings.ReplaceAll(file, "\\", "/")
183+
if strings.HasPrefix(normalizedFile, normalizedDir) {
184+
return true
185+
}
186+
}
165187
}
166188
return false
167189
}

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)