Skip to content
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

fix: multiple excludes #782

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions internal/lefthook/runner/filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,20 @@ func byExclude(vs []string, matcher interface{}) []string {
globs = append(globs, glob.MustCompile(name.(string)))
}

var foundMatch bool
vsf := make([]string, 0)
for _, v := range vs {
for _, g := range globs {
if ok := g.Match(v); !ok {
vsf = append(vsf, v)
if ok := g.Match(v); ok {
foundMatch = true
break
}
}

if !foundMatch {
vsf = append(vsf, v)
}
foundMatch = false
}

return vsf
Expand Down
26 changes: 17 additions & 9 deletions testdata/exclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ exec git config user.email "you@example.com"
exec git config user.name "Your Name"
exec git add -A
exec lefthook run -f all
stdout 'excluded.txt lefthook.yml not-excluded.txt'
stdout 'a.txt b.txt dir/a.txt dir/b.txt lefthook.yml'
exec lefthook run -f regexp
stdout 'lefthook.yml not-excluded.txt'
stdout 'dir/a.txt dir/b.txt lefthook.yml'
exec lefthook run -f array
stdout 'not-excluded.txt'
stdout 'dir/a.txt dir/b.txt'

-- lefthook.yml --
skip_output:
Expand All @@ -25,19 +25,27 @@ regexp:
commands:
echo:
run: echo {staged_files}
exclude: '^excluded\.[tx]+'
exclude: '^(a.txt|b.txt)'

array:
commands:
echo:
run: echo {staged_files}
exclude:
- exclude.txt
- a.txt
- b.txt
- '*.yml'

-- not-excluded.txt --
sometext
-- a.txt --
a

-- b.txt --
b

-- dir/a.txt --
dir-a

-- dir/b.txt --
dir-b

-- excluded.txt --
somecode

Loading