Skip to content

Commit

Permalink
chore: remove redundant parallelisation
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Apr 3, 2024
1 parent 08d69af commit 5283257
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 125 deletions.
23 changes: 6 additions & 17 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ Run 'lefthook install' manually.`,
return err
}

startTime := time.Now()
resultChan := make(chan runner.Result, len(hook.Commands)+len(hook.Scripts))

if args.FilesFromStdin {
paths, err := io.ReadAll(os.Stdin)
if err != nil {
Expand Down Expand Up @@ -160,7 +157,6 @@ Run 'lefthook install' manually.`,
Hook: hook,
HookName: hookName,
GitArgs: gitArgs,
ResultChan: resultChan,
LogSettings: logSettings,
DisableTTY: cfg.NoTTY || args.NoTTY,
Files: args.Files,
Expand All @@ -169,15 +165,8 @@ Run 'lefthook install' manually.`,
},
)

go func() {
r.RunAll(ctx, sourceDirs)
close(resultChan)
}()

var results []runner.Result
for res := range resultChan {
results = append(results, res)
}
startTime := time.Now()
results := r.RunAll(ctx, sourceDirs)

if ctx.Err() != nil {
return errors.New("Interrupted")
Expand All @@ -186,7 +175,7 @@ Run 'lefthook install' manually.`,
printSummary(time.Since(startTime), results, logSettings)

for _, result := range results {
if result.Err != nil {
if result.Failure() {
return errors.New("") // No error should be printed
}
}
Expand Down Expand Up @@ -227,7 +216,7 @@ func printSummary(

if logSettings.LogSuccess() {
for _, result := range results {
if result.Err != nil {
if !result.Success() {
continue
}

Expand All @@ -237,11 +226,11 @@ func printSummary(

if logSettings.LogFailure() {
for _, result := range results {
if result.Err == nil {
if !result.Failure() {
continue
}

failText := result.Err.Error()
failText := result.Text()
if len(failText) != 0 {
failText = fmt.Sprintf(": %s", failText)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/runner/exec/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// Options contains the data that controls the execution.
type Options struct {
Name, Root, FailText string
Name, Root string
Commands []string
Env map[string]string
Interactive, UseStdin bool
Expand Down
11 changes: 9 additions & 2 deletions internal/lefthook/runner/prepare_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import (
"github.com/evilmartians/lefthook/internal/log"
)

type validationError struct {
err error
}

func (e *validationError) Error() string {
return fmt.Sprintf("validation error: %s", e.err.Error())
}

// An object that describes the single command's run option.
type run struct {
commands []string
Expand Down Expand Up @@ -48,8 +56,7 @@ func (r *Runner) prepareCommand(name string, command *config.Command) (*run, err
}

if err := command.Validate(); err != nil {
r.fail(name, err)
return nil, err
return nil, &validationError{err}
}

args, err, skipReason := r.buildRun(command)
Expand Down
12 changes: 10 additions & 2 deletions internal/lefthook/runner/prepare_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ package runner

import (
"errors"
"fmt"
"os"
"strings"

"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/log"
)

type osError struct {
err error
}

func (e *osError) Error() string {
return fmt.Sprintf("os error: %s", e.err.Error())
}

func (r *Runner) prepareScript(script *config.Script, path string, file os.FileInfo) (string, error) {
if script.DoSkip(r.Repo.State()) {
return "", errors.New("settings")
Expand All @@ -28,8 +37,7 @@ func (r *Runner) prepareScript(script *config.Script, path string, file os.FileI
if (file.Mode() & executableMask) == 0 {
if err := r.Repo.Fs.Chmod(path, executableFileMode); err != nil {
log.Errorf("Couldn't change file mode to make file executable: %s", err)
r.fail(file.Name(), nil)
return "", errors.New("system error")
return "", &osError{err}
}
}

Expand Down
40 changes: 40 additions & 0 deletions internal/lefthook/runner/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package runner

type status int8

const (
success status = iota
failure
skip
)

// Result contains name of a command/script and an optional fail string.
type Result struct {
Name string
status status
text string
}

func (r Result) Success() bool {
return r.status == success
}

func (r Result) Failure() bool {
return r.status == failure
}

func (r Result) Text() string {
return r.text
}

func skipped(name string) Result {
return Result{Name: name, status: skip}
}

func succeeded(name string) Result {
return Result{Name: name, status: success}
}

func failed(name, text string) Result {
return Result{Name: name, status: failure, text: text}
}
Loading

0 comments on commit 5283257

Please sign in to comment.