-
Notifications
You must be signed in to change notification settings - Fork 2
[exclude] add support for globs in exclude #6
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
Merged
capcom6
merged 2 commits into
master
from
codex/plan-implementation-for-roadmap-feature
Apr 24, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package exclude | ||
|
|
||
| import "errors" | ||
|
|
||
| var ( | ||
| ErrInvalidPattern = errors.New("invalid exclude pattern") | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package exclude | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/bmatcuk/doublestar/v4" | ||
| ) | ||
|
|
||
| type rule struct { | ||
| value string | ||
| isPattern bool | ||
| } | ||
|
|
||
| type Matcher struct { | ||
| sourceRoot string | ||
| rules []rule | ||
| } | ||
|
|
||
| func New(rules []string, sourceRoot string) (*Matcher, error) { | ||
| absSourceRoot, err := filepath.Abs(sourceRoot) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get absolute path: %w", err) | ||
| } | ||
|
|
||
| compiled := make([]rule, 0, len(rules)) | ||
| for _, raw := range rules { | ||
| normalized := filepath.ToSlash(raw) | ||
| if !doublestar.ValidatePattern(normalized) { | ||
| return nil, fmt.Errorf("%w: exclude rule %q is invalid", ErrInvalidPattern, raw) | ||
| } | ||
|
|
||
| compiled = append(compiled, rule{ | ||
| value: normalized, | ||
| isPattern: hasMeta(normalized), | ||
| }) | ||
| } | ||
|
|
||
| return &Matcher{ | ||
| sourceRoot: absSourceRoot, | ||
| rules: compiled, | ||
| }, nil | ||
| } | ||
|
|
||
| func (m *Matcher) Match(filePath string) bool { | ||
| matched, _ := m.MatchRule(filePath) | ||
| return matched | ||
| } | ||
|
|
||
| func (m *Matcher) MatchRule(filePath string) (bool, string) { | ||
| candidate := filePath | ||
| if filepath.IsAbs(candidate) { | ||
| if rel, err := filepath.Rel(m.sourceRoot, candidate); err == nil { | ||
| candidate = rel | ||
| } | ||
| } | ||
| normalized := path.Clean(filepath.ToSlash(candidate)) | ||
|
capcom6 marked this conversation as resolved.
|
||
|
|
||
| for _, r := range m.rules { | ||
| if !r.isPattern { | ||
| if normalized == r.value || strings.HasPrefix(normalized, r.value+"/") { | ||
| return true, r.value | ||
| } | ||
|
|
||
| continue | ||
| } | ||
|
|
||
| // Try direct match first | ||
| matched, matchErr := doublestar.Match(r.value, normalized) | ||
| if matchErr == nil && matched { | ||
| return true, r.value | ||
| } | ||
|
|
||
| // If direct match fails, try matching against path prefixes | ||
| // This handles cases like: | ||
| // - pattern "build/*" matching "build/out/main.bin" | ||
| // - pattern "**/node_modules" matching "web/node_modules/react/index.js" | ||
| parts := strings.Split(normalized, "/") | ||
| for i := 1; i <= len(parts); i++ { | ||
| prefix := path.Join(parts[:i]...) | ||
| matched, matchErr = doublestar.Match(r.value, prefix) | ||
| if matchErr == nil && matched { | ||
| return true, r.value | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false, "" | ||
| } | ||
|
|
||
| func hasMeta(pattern string) bool { | ||
| return strings.ContainsAny(pattern, "*?{") | ||
| } | ||
|
capcom6 marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package exclude_test | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/capcom6/sftp-sync/internal/exclude" | ||
| ) | ||
|
|
||
| func TestNewRejectsInvalidPattern(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| _, err := exclude.New([]string{"**/["}, ".") | ||
| if err == nil { | ||
| t.Fatal("expected invalid pattern error") | ||
| } | ||
| } | ||
|
|
||
| func TestMatcherLiteralPathMatching(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| matcher, err := exclude.New([]string{".git", "vendor/cache"}, "/repo") | ||
| if err != nil { | ||
| t.Fatalf("New() error = %v", err) | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| path string | ||
| want bool | ||
| }{ | ||
| {name: "exact dir", path: "./.git", want: true}, | ||
| {name: "descendant", path: "./.git/config", want: true}, | ||
| {name: "other path", path: "./pkg/main.go", want: false}, | ||
| {name: "nested descendant", path: "vendor/cache/tmp.txt", want: true}, | ||
| {name: "meta-like characters stay literal", path: "vendor/cache[file].txt", want: false}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| if got := matcher.Match(tt.path); got != tt.want { | ||
| t.Fatalf("Match(%q) = %v, want %v", tt.path, got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
|
capcom6 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func TestMatcherLiteralBackwardCompatibilityWithMetaCharacters(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| matcher, err := exclude.New([]string{"cache[file].txt"}, "/repo") | ||
| if err != nil { | ||
| t.Fatalf("New() error = %v", err) | ||
| } | ||
|
|
||
| if got := matcher.Match("cache[file].txt"); !got { | ||
| t.Fatalf("literal path containing [] should still match exactly") | ||
| } | ||
|
|
||
| if got := matcher.Match("cachef.txt"); got { | ||
| t.Fatalf("literal-first behavior expected; glob expansion should not alter matching") | ||
| } | ||
| } | ||
|
|
||
| func TestMatcherPatternMatching(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| matcher, err := exclude.New([]string{"**/*.tmp", "build/*", "**/node_modules"}, "/repo") | ||
| if err != nil { | ||
| t.Fatalf("New() error = %v", err) | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| path string | ||
| want bool | ||
| }{ | ||
| {name: "recursive extension", path: "a/b/c.tmp", want: true}, | ||
| {name: "single segment", path: "build/main.bin", want: true}, | ||
| {name: "single segment nested via matched ancestor", path: "build/out/main.bin", want: true}, | ||
| {name: "pattern matches ancestor directory", path: "web/node_modules/react/index.js", want: true}, | ||
| {name: "non match", path: "src/main.go", want: false}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| if got := matcher.Match(tt.path); got != tt.want { | ||
| t.Fatalf("Match(%q) = %v, want %v", tt.path, got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
|
capcom6 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func TestMatcherAbsolutePathAndRootBoundary(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| root := filepath.FromSlash("/repo") | ||
| inside := filepath.Join(root, "dist", "bundle.js") | ||
| outside := filepath.FromSlash("/other/dist/bundle.js") | ||
|
|
||
| matcher, err := exclude.New([]string{"dist/**"}, root) | ||
| if err != nil { | ||
| t.Fatalf("New() error = %v", err) | ||
| } | ||
|
|
||
| if got := matcher.Match(inside); !got { | ||
| t.Fatalf("Match(%q) = %v, want true", inside, got) | ||
| } | ||
|
|
||
| if got := matcher.Match(outside); got { | ||
| t.Fatalf("Match(%q) = %v, want false", outside, got) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.