From a5cc9ad79b0a59afa81094a9fc75304204174918 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Fri, 1 Nov 2024 20:31:54 +0300 Subject: [PATCH] fix: add domains to keep common glob and piping statuses --- internal/config/do.go | 1 + internal/lefthook/runner/do.go | 46 ++++++++++++++++++++++------------ lefthook.yml | 20 ++++++++------- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/internal/config/do.go b/internal/config/do.go index ebf540f1..dec1b2a4 100644 --- a/internal/config/do.go +++ b/internal/config/do.go @@ -32,6 +32,7 @@ type Group struct { Root string `json:"root,omitempty" mapstructure:"root" toml:"root,omitempty" yaml:",omitempty"` Parallel bool `json:"parallel,omitempty" mapstructure:"parallel" toml:"parallel,omitempty" yaml:",omitempty"` Piped bool `json:"piped,omitempty" mapstructure:"piped" toml:"piped,omitempty" yaml:",omitempty"` + Glob string `json:"glob,omitempty" mapstructure:"glob" toml:"glob,omitempty" yaml:",omitempty"` Do []*Do `json:"do,omitempty" mapstructure:"do" toml:"do,omitempty" yaml:",omitempty"` } diff --git a/internal/lefthook/runner/do.go b/internal/lefthook/runner/do.go index 222ef688..5c89e882 100644 --- a/internal/lefthook/runner/do.go +++ b/internal/lefthook/runner/do.go @@ -6,6 +6,7 @@ import ( "path/filepath" "strconv" "sync" + "sync/atomic" "github.com/evilmartians/lefthook/internal/config" "github.com/evilmartians/lefthook/internal/lefthook/runner/action" @@ -20,29 +21,35 @@ var ( errEmptyGroup = errors.New("empty groups are not permitted") ) +type domain struct { + failed atomic.Bool + glob string +} + func (r *Runner) do(ctx context.Context) []Result { var wg sync.WaitGroup results := make([]Result, 0, len(r.Hook.Do)) resultsChan := make(chan Result, len(r.Hook.Do)) + domain := &domain{} for i, do := range r.Hook.Do { id := strconv.Itoa(i) - if r.failed.Load() && r.Hook.Piped { + if domain.failed.Load() && r.Hook.Piped { r.logSkip(do.PrintableName(id), "broken pipe") continue } if !r.Hook.Parallel { - results = append(results, r.runDo(ctx, id, do)) + results = append(results, r.runDo(ctx, domain, id, do)) continue } wg.Add(1) - go func(id string, do *config.Do) { + go func(do *config.Do) { defer wg.Done() - resultsChan <- r.runDo(ctx, id, do) - }(id, do) + resultsChan <- r.runDo(ctx, domain, id, do) + }(do) } wg.Wait() @@ -54,7 +61,7 @@ func (r *Runner) do(ctx context.Context) []Result { return results } -func (r *Runner) runDo(ctx context.Context, id string, do *config.Do) Result { +func (r *Runner) runDo(ctx context.Context, domain *domain, id string, do *config.Do) Result { // Check if do action is properly configured if len(do.Run) > 0 && len(do.Script) > 0 { return failed(do.PrintableName(id), errDoContainsBothRunAndScript.Error()) @@ -69,7 +76,7 @@ func (r *Runner) runDo(ctx context.Context, id string, do *config.Do) Result { } if len(do.Run) != 0 || len(do.Script) != 0 { - return r.doAction(ctx, id, do) + return r.doAction(ctx, domain, id, do) } if do.Group != nil { @@ -79,8 +86,14 @@ func (r *Runner) runDo(ctx context.Context, id string, do *config.Do) Result { return failed(do.PrintableName(id), "don't know how to run action") } -func (r *Runner) doAction(ctx context.Context, id string, do *config.Do) Result { +func (r *Runner) doAction(ctx context.Context, domain *domain, id string, do *config.Do) Result { name := do.PrintableName(id) + + glob := do.Glob + if len(glob) == 0 { + glob = domain.glob + } + runAction, err := action.New(name, &action.Params{ Repo: r.Repo, Hook: r.Hook, @@ -93,7 +106,7 @@ func (r *Runner) doAction(ctx context.Context, id string, do *config.Do) Result Root: do.Root, Runner: do.Runner, Script: do.Script, - Glob: do.Glob, + Glob: glob, Files: do.Files, FileTypes: do.FileTypes, Tags: do.Tags, @@ -109,7 +122,7 @@ func (r *Runner) doAction(ctx context.Context, id string, do *config.Do) Result return skipped(name) } - r.failed.Store(true) + domain.failed.Store(true) return failed(name, err.Error()) } @@ -123,7 +136,7 @@ func (r *Runner) doAction(ctx context.Context, id string, do *config.Do) Result }, r.Hook.Follow) if !ok { - r.failed.Store(true) + domain.failed.Store(true) return failed(name, do.FailText) } @@ -167,26 +180,27 @@ func (r *Runner) doGroup(ctx context.Context, groupId string, group *config.Grou results := make([]Result, 0, len(r.Hook.Do)) resultsChan := make(chan Result, len(r.Hook.Do)) + domain := &domain{glob: group.Glob} var wg sync.WaitGroup for i, subdo := range group.Do { id := strconv.Itoa(i) - if r.failed.Load() && group.Piped { + if domain.failed.Load() && group.Piped { r.logSkip(subdo.PrintableName(id), "broken pipe") continue } if !group.Parallel { - results = append(results, r.runDo(ctx, id, subdo)) + results = append(results, r.runDo(ctx, domain, id, subdo)) continue } wg.Add(1) - go func(id string, do *config.Do) { + go func(do *config.Do) { defer wg.Done() - resultsChan <- r.runDo(ctx, id, do) - }(id, subdo) + resultsChan <- r.runDo(ctx, domain, id, do) + }(subdo) } wg.Wait() diff --git a/lefthook.yml b/lefthook.yml index eecc7da4..46c2e10d 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -1,19 +1,21 @@ pre-commit: parallel: true do: - - run: make lint - glob: '*.go' - stage_fixed: true + - group: + name: check code + glob: "*.go" + do: + - run: make lint + stage_fixed: true + + - run: make test - run: lychee --max-concurrency 3 {staged_files} - name: lychee + name: check links glob: '*.md' exclude: - CHANGELOG.md - - run: make test - glob: '*.go' - - - run: typos --write-changes {staged_files} - name: typos + - name: fix typos + run: typos --write-changes {staged_files} stage_fixed: true