From 7f8e2cd8c542e9cdb0c51f3c40c8f5ead837bde0 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Wed, 24 Jul 2024 17:02:35 +0300 Subject: [PATCH] fix: multiple excludes issue --- internal/lefthook/runner/filters/filters.go | 11 +++++++-- testdata/exclude.txt | 26 ++++++++++++++------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/internal/lefthook/runner/filters/filters.go b/internal/lefthook/runner/filters/filters.go index 6be7bda5..e107012e 100644 --- a/internal/lefthook/runner/filters/filters.go +++ b/internal/lefthook/runner/filters/filters.go @@ -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 diff --git a/testdata/exclude.txt b/testdata/exclude.txt index 852fb26f..8213e8b4 100644 --- a/testdata/exclude.txt +++ b/testdata/exclude.txt @@ -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: @@ -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