-
Notifications
You must be signed in to change notification settings - Fork 65
fix: respect specified paths in git repository #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -40,6 +40,8 @@ import ( | |||||||||||||||||||||||||||||||||||||||||
| "golang.org/x/sync/errgroup" | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const currentDir = "./" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Check checks the license headers of the specified paths/globs. | ||||||||||||||||||||||||||||||||||||||||||
| func Check(config *ConfigHeader, result *Result) error { | ||||||||||||||||||||||||||||||||||||||||||
| fileList, err := listFiles(config) | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -67,13 +69,13 @@ func Check(config *ConfigHeader, result *Result) error { | |||||||||||||||||||||||||||||||||||||||||
| func listFiles(config *ConfigHeader) ([]string, error) { | ||||||||||||||||||||||||||||||||||||||||||
| var fileList []string | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| repo, err := git.PlainOpen("./") | ||||||||||||||||||||||||||||||||||||||||||
| repo, err := git.PlainOpen(currentDir) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if err != nil { // we're not in a Git workspace, fallback to glob paths | ||||||||||||||||||||||||||||||||||||||||||
| var localFileList []string | ||||||||||||||||||||||||||||||||||||||||||
| for _, pattern := range config.Paths { | ||||||||||||||||||||||||||||||||||||||||||
| if pattern == "." { | ||||||||||||||||||||||||||||||||||||||||||
| pattern = "./" | ||||||||||||||||||||||||||||||||||||||||||
| pattern = currentDir | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| files, err := doublestar.Glob(pattern) | ||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -134,12 +136,15 @@ func listFiles(config *ConfigHeader) ([]string, error) { | |||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| seen := make(map[string]bool) | ||||||||||||||||||||||||||||||||||||||||||
| for _, file := range candidates { | ||||||||||||||||||||||||||||||||||||||||||
| if !seen[file] { | ||||||||||||||||||||||||||||||||||||||||||
| seen[file] = true | ||||||||||||||||||||||||||||||||||||||||||
| _, err := os.Stat(file) | ||||||||||||||||||||||||||||||||||||||||||
| for _, candidate := range candidates { | ||||||||||||||||||||||||||||||||||||||||||
| if !seen[candidate] { | ||||||||||||||||||||||||||||||||||||||||||
| seen[candidate] = true | ||||||||||||||||||||||||||||||||||||||||||
| _, err := os.Stat(candidate) | ||||||||||||||||||||||||||||||||||||||||||
| if err == nil { | ||||||||||||||||||||||||||||||||||||||||||
| fileList = append(fileList, file) | ||||||||||||||||||||||||||||||||||||||||||
| // Filter candidates by the paths/patterns specified in config | ||||||||||||||||||||||||||||||||||||||||||
| if MatchPaths(candidate, config.Paths) { | ||||||||||||||||||||||||||||||||||||||||||
| fileList = append(fileList, candidate) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } else if !os.IsNotExist(err) { | ||||||||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -150,6 +155,39 @@ func listFiles(config *ConfigHeader) ([]string, error) { | |||||||||||||||||||||||||||||||||||||||||
| return fileList, nil | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func MatchPaths(file string, patterns []string) bool { | ||||||||||||||||||||||||||||||||||||||||||
| for _, pattern := range patterns { | ||||||||||||||||||||||||||||||||||||||||||
| if pattern == "." { | ||||||||||||||||||||||||||||||||||||||||||
| pattern = currentDir | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| matched, err := doublestar.Match(pattern, file) | ||||||||||||||||||||||||||||||||||||||||||
| if err == nil && matched { | ||||||||||||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
| } | |
| } | |
| // Fallback for directory patterns (e.g., "pkg/header/" or "pkg/header") | |
| // to behave consistently with tryMatchPatten in config.go. | |
| if err == nil { | |
| dirPattern := pattern | |
| // Ensure the pattern ends with a path separator so it clearly denotes a directory. | |
| if !strings.HasSuffix(dirPattern, "/") && !strings.HasSuffix(dirPattern, string(os.PathSeparator)) { | |
| dirPattern = dirPattern + "/" | |
| } | |
| // Normalize both paths to use '/' so prefix matching is consistent across platforms. | |
| normalizedDir := strings.ReplaceAll(dirPattern, "\\", "/") | |
| normalizedFile := strings.ReplaceAll(file, "\\", "/") | |
| if strings.HasPrefix(normalizedFile, normalizedDir) { | |
| return true | |
| } | |
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -273,3 +273,76 @@ func TestListFilesWithWorktreeDetachedHEAD(t *testing.T) { | |||||
| t.Error("Expected to find files with valid commit") | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| func TestMatchPaths(t *testing.T) { | ||||||
| tests := []struct { | ||||||
| name string | ||||||
| file string | ||||||
| patterns []string | ||||||
| expected bool | ||||||
| }{ | ||||||
| { | ||||||
| name: "Exact file match", | ||||||
| file: "test.go", | ||||||
| patterns: []string{"test.go"}, | ||||||
| expected: true, | ||||||
| }, | ||||||
| { | ||||||
| name: "Glob pattern match", | ||||||
| file: "test.go", | ||||||
| patterns: []string{"*.go"}, | ||||||
| expected: true, | ||||||
| }, | ||||||
| { | ||||||
| name: "Double-star glob pattern match", | ||||||
| file: "pkg/header/check.go", | ||||||
| patterns: []string{"**/*.go"}, | ||||||
| expected: true, | ||||||
| }, | ||||||
| { | ||||||
| name: "Multiple patterns with match", | ||||||
| file: "test.go", | ||||||
| patterns: []string{"*.java", "*.go", "*.py"}, | ||||||
| expected: true, | ||||||
| }, | ||||||
| { | ||||||
| name: "Multiple patterns without match", | ||||||
| file: "test.go", | ||||||
| patterns: []string{"*.java", "*.py"}, | ||||||
| expected: false, | ||||||
| }, | ||||||
| { | ||||||
| name: "Directory pattern with trailing slash", | ||||||
| file: "pkg/header/check.go", | ||||||
| patterns: []string{"pkg/header/"}, | ||||||
| expected: true, | ||||||
| }, | ||||||
| { | ||||||
| name: "Directory pattern without trailing slash", | ||||||
| file: "pkg/header/check.go", | ||||||
| patterns: []string{"pkg/header}"}, | ||||||
| expected: false, | ||||||
|
||||||
| expected: false, | |
| expected: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, I think when we pass the paths to the CLI args we intend to check that files regardless of the config file. The args here should be highest priority even if they are not configured in config file