Skip to content

Commit

Permalink
fix: multiple excludes (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox authored Jul 24, 2024
1 parent efa67f5 commit 2782f15
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
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

0 comments on commit 2782f15

Please sign in to comment.