Skip to content

Commit

Permalink
feat: inherit exclude option in groups (#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox authored Jan 10, 2025
1 parent f5b0840 commit b8c4121
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/mdbook/configuration/jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pre-commit:
**Job Groups**

- Groups can include other jobs.
- Flow within groups can be parallel or piped. Options like `glob` and `root` apply to all jobs in the group, including nested ones.
- Flow within groups can be parallel or piped. Options `glob`, `root`, and `exclude` apply to all jobs in the group, including nested ones.

### Job options

Expand Down Expand Up @@ -55,7 +55,7 @@ Below are the available options for configuring jobs.

### Example

> **Note:** Currently, only root and glob options are applied to group jobs. Other options must be set for each job individually. Submit a [feature request](https://github.com/evilmartians/lefthook/issues/new?assignees=&labels=feature+request&projects=&template=feature_request.md) if this limits your workflow.
> **Note:** Currently, only `root`, `glob`, and `exclude` options are applied to group jobs. Other options must be set for each job individually. Submit a [feature request](https://github.com/evilmartians/lefthook/issues/new?assignees=&labels=feature+request&projects=&template=feature_request.md) if this limits your workflow.

A configuration demonstrating a piped group running in parallel with other jobs:

Expand Down
37 changes: 35 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,22 @@ 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...)
case interface{}:
if len(result) > 0 {
return result
} else {
return a
}
default:
}
}

return result
}
24 changes: 21 additions & 3 deletions testdata/exclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ exec git config user.email "[email protected]"
exec git config user.name "Your Name"
exec git add -A
exec lefthook run -f all
stdout 'a.txt b.txt dir/a.txt dir/b.txt lefthook.yml'
stdout '^a.txt b.txt dir/a.txt dir/b.txt lefthook.yml\s*$'
exec lefthook run -f regexp
stdout 'dir/a.txt dir/b.txt lefthook.yml'
stdout '^dir/a.txt dir/b.txt lefthook.yml\s*$'
exec lefthook run -f array
stdout 'dir/a.txt dir/b.txt'
stdout '^dir/a.txt dir/b.txt\s*$'
exec lefthook run -f nested
stdout '^lefthook.yml\s+dir/b.txt lefthook.yml\s+b.txt dir/b.txt\s*$'

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

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

Expand Down

0 comments on commit b8c4121

Please sign in to comment.