Skip to content

Commit

Permalink
feat: inherit exclude option in groups
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Jan 9, 2025
1 parent f5b0840 commit 3582634
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
32 changes: 30 additions & 2 deletions internal/lefthook/runner/run_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type domain struct {

glob string
root string
exclude interface{}
onlyJobs []string
}

Expand Down Expand Up @@ -94,6 +95,18 @@ func (r *Runner) runJob(ctx context.Context, domain *domain, id string, job *con
inheritedDomain := *domain
inheritedDomain.glob = first(job.Glob, domain.glob)
inheritedDomain.root = first(job.Root, domain.root)
switch list := job.Exclude.(type) {
case []interface{}:
switch inherited := inheritedDomain.exclude.(type) {
case []interface{}:
inherited = append(inherited, list...)
inheritedDomain.exclude = inherited
default:
inheritedDomain.exclude = job.Exclude
}
default:
inheritedDomain.exclude = job.Exclude
}
groupName := first(job.Name, "["+id+"]")

if len(domain.onlyJobs) != 0 && slices.Contains(domain.onlyJobs, job.Name) {
Expand All @@ -111,6 +124,7 @@ func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, jo

root := first(job.Root, domain.root)
glob := first(job.Glob, domain.glob)
exclude := join(job.Exclude, domain.exclude)
executionJob, err := jobs.New(name, &jobs.Params{
Repo: r.Repo,
Hook: r.Hook,
Expand All @@ -127,7 +141,7 @@ func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, jo
Files: job.Files,
FileTypes: job.FileTypes,
Tags: job.Tags,
Exclude: job.Exclude,
Exclude: exclude,
Only: job.Only,
Skip: job.Skip,
})
Expand Down Expand Up @@ -171,7 +185,7 @@ func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, jo
files = filters.Apply(r.Repo.Fs, files, filters.Params{
Glob: glob,
Root: root,
Exclude: job.Exclude,
Exclude: exclude,
FileTypes: job.FileTypes,
})
}
Expand Down Expand Up @@ -236,3 +250,17 @@ func first(args ...string) string {

return ""
}

func join(args ...interface{}) interface{} {
result := []interface{}{}
for _, a := range args {
switch list := a.(type) {
case []interface{}:
result = append(result, list...)
default:
return a
}
}

return result
}
12 changes: 12 additions & 0 deletions testdata/exclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ exec lefthook run -f regexp
stdout 'dir/a.txt dir/b.txt lefthook.yml'
exec lefthook run -f array
stdout 'dir/a.txt dir/b.txt'
exec lefthook run -f nested
stdout 'dir/b.txt lefthook.yml'

-- lefthook.yml --
skip_output:
Expand All @@ -35,6 +37,16 @@ array:
- b.txt
- '*.yml'

nested:
jobs:
- exclude:
- a.txt
- dir/a.txt
group:
jobs:
- exclude:
- b.txt
run: echo {staged_files}
-- a.txt --
a

Expand Down

0 comments on commit 3582634

Please sign in to comment.