Skip to content

Commit

Permalink
fix: add domains to keep common glob and piping statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Nov 1, 2024
1 parent acc7ba0 commit a5cc9ad
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
1 change: 1 addition & 0 deletions internal/config/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
46 changes: 30 additions & 16 deletions internal/lefthook/runner/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand All @@ -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())
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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())
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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()
Expand Down
20 changes: 11 additions & 9 deletions lefthook.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a5cc9ad

Please sign in to comment.