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

feat: add ability to specify job names to run command #904

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
5 changes: 5 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ func (run) New(opts *lefthook.Options) *cobra.Command {
"run only specified commands",
)

runCmd.Flags().StringSliceVar(
&runArgs.RunOnlyJobs, "jobs", nil,
"run only specified jobs",
)

err := runCmd.Flags().MarkDeprecated("files", "use --file flag instead")
if err != nil {
log.Warn("Unexpected error:", err)
Expand Down
2 changes: 2 additions & 0 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type RunArgs struct {
SkipLFS bool
Files []string
RunOnlyCommands []string
RunOnlyJobs []string
}

func Run(opts *Options, args RunArgs, hookName string, gitArgs []string) error {
Expand Down Expand Up @@ -170,6 +171,7 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {
Files: args.Files,
Force: args.Force,
RunOnlyCommands: args.RunOnlyCommands,
RunOnlyJobs: args.RunOnlyJobs,
SourceDirs: sourceDirs,
},
)
Expand Down
17 changes: 14 additions & 3 deletions internal/lefthook/runner/run_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"path/filepath"
"slices"
"strconv"
"sync"
"sync/atomic"
Expand All @@ -24,8 +25,9 @@ var (
type domain struct {
failed *atomic.Bool

glob string
root string
glob string
root string
onlyJobs []string
}

func (r *Runner) runJobs(ctx context.Context) []Result {
Expand All @@ -35,7 +37,7 @@ func (r *Runner) runJobs(ctx context.Context) []Result {
resultsChan := make(chan Result, len(r.Hook.Jobs))

var failed atomic.Bool
domain := &domain{failed: &failed}
domain := &domain{failed: &failed, onlyJobs: r.RunOnlyJobs}

for i, job := range r.Hook.Jobs {
id := strconv.Itoa(i)
Expand Down Expand Up @@ -81,6 +83,10 @@ func (r *Runner) runJob(ctx context.Context, domain *domain, id string, job *con
}

if len(job.Run) != 0 || len(job.Script) != 0 {
if len(domain.onlyJobs) != 0 && !slices.Contains(domain.onlyJobs, job.Name) {
return skipped(job.PrintableName(id))
}

return r.runSingleJob(ctx, domain, id, job)
}

Expand All @@ -89,6 +95,11 @@ func (r *Runner) runJob(ctx context.Context, domain *domain, id string, job *con
inheritedDomain.glob = first(job.Glob, domain.glob)
inheritedDomain.root = first(job.Root, domain.root)
groupName := first(job.Name, "["+id+"]")

if len(domain.onlyJobs) != 0 && slices.Contains(domain.onlyJobs, job.Name) {
inheritedDomain.onlyJobs = []string{}
}

return r.runGroup(ctx, groupName, &inheritedDomain, job.Group)
}

Expand Down
1 change: 1 addition & 0 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Options struct {
Force bool
Files []string
RunOnlyCommands []string
RunOnlyJobs []string
SourceDirs []string
}

Expand Down
34 changes: 34 additions & 0 deletions testdata/cli_run_only.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
exec git init
exec git config user.email "[email protected]"
exec git config user.name "Your Name"
exec git add -A
exec lefthook install
exec lefthook run hook --jobs a --jobs c --jobs db --commands lint
stdout '\s*a\s*ca\s*cb\s*db\s*lint\s*'

-- lefthook.yml --
output:
- execution_out
hook:
jobs:
- name: a
run: echo a
- name: b
run: echo b
- name: c
group:
jobs:
- run: echo ca
- run: echo cb
- name: d
group:
jobs:
- name: da
run: echo da
- name: db
run: echo db
commands:
lint:
run: echo lint
test:
run: echo test
Loading