Skip to content

Commit

Permalink
feat: implitic skip on missing files for pre-commit and pre-push hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Mar 15, 2023
1 parent ce9930f commit d6402c8
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 100 deletions.
4 changes: 4 additions & 0 deletions internal/config/available_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func HookUsesStagedFiles(hook string) bool {
return hook == "pre-commit"
}

func HookUsesPushFiles(hook string) bool {
return hook == "pre-push"
}

func HookAvailable(hook string) bool {
for _, name := range AvailableHooks {
if name == hook {
Expand Down
4 changes: 2 additions & 2 deletions internal/lefthook/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ pre-commit:
},
{
name: "When not in rebase flow",
hook: "pre-commit",
hook: "post-commit",
config: `
pre-commit:
post-commit:
parallel: false
piped: true
commands:
Expand Down
34 changes: 26 additions & 8 deletions internal/lefthook/runner/prepare_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ func (r *Runner) prepareCommand(name string, command *config.Command) (*commandA
return nil, errors.New("invalid conig")
}

args, err := r.buildCommandArgs(command)
args, err, skipReason := r.buildCommandArgs(command)
if err != nil {
log.Error(err)
return nil, errors.New("error")
}
if args == nil || len(args.all) == 0 {
return nil, errors.New("no files for inspection")
if skipReason != nil {
return nil, skipReason
}

return args, nil
}

func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error) {
func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error, error) {
filesCommand := r.Hook.Files
if command.Files != "" {
filesCommand = command.Files
Expand All @@ -72,21 +72,39 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error)
filesCommand != "" && filesType == config.SubFiles {
files, err := filesFn()
if err != nil {
return nil, fmt.Errorf("error replacing %s: %w", filesType, err)
return nil, fmt.Errorf("error replacing %s: %w", filesType, err), nil
}
if len(files) == 0 {
return nil, nil
return nil, nil, errors.New("no files for inspection")
}

filesPrepared := prepareFiles(command, files)
if len(filesPrepared) == 0 {
return nil, nil
return nil, nil, errors.New("no files for inspection")
}
filteredFiles = append(filteredFiles, filesPrepared...)
runString = replaceQuoted(runString, filesType, filesPrepared)
}
}

if len(filteredFiles) == 0 && config.HookUsesStagedFiles(r.HookName) {
files, err := r.Repo.StagedFiles()
if err == nil {
if len(prepareFiles(command, files)) == 0 {
return nil, nil, errors.New("no matching staged files")
}
}
}

if len(filteredFiles) == 0 && config.HookUsesPushFiles(r.HookName) {
files, err := r.Repo.PushFiles()
if err == nil {
if len(prepareFiles(command, files)) == 0 {
return nil, nil, errors.New("no matching push files")
}
}
}

runString = strings.ReplaceAll(runString, "{0}", strings.Join(r.GitArgs, " "))
for i, gitArg := range r.GitArgs {
runString = strings.ReplaceAll(runString, fmt.Sprintf("{%d}", i+1), gitArg)
Expand All @@ -97,7 +115,7 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error)
return &commandArgs{
files: filteredFiles,
all: strings.Split(runString, " "),
}, nil
}, nil, nil
}

func prepareFiles(command *config.Command, files []string) []string {
Expand Down
4 changes: 4 additions & 0 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ func (r *Runner) preHook() {
}

func (r *Runner) postHook() {
if !config.HookUsesStagedFiles(r.HookName) {
return
}

if err := r.Repo.RestoreUnstaged(); err != nil {
log.Warnf("Couldn't restore hidden unstaged files: %s\n", err)
return
Expand Down
Loading

0 comments on commit d6402c8

Please sign in to comment.