From a972e07902ce61ce070b3e7affba93c011bd4113 Mon Sep 17 00:00:00 2001 From: Sander Maijers <3374183+sanmai-NL@users.noreply.github.com> Date: Thu, 14 Dec 2023 19:09:34 +0100 Subject: [PATCH] Also glob commands without templates & `files` Commands that neither had templates in `run` nor `files` didn't apply file globs, even if the `--all-files` CLI param was set --- internal/lefthook/run/prepare_command.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/lefthook/run/prepare_command.go b/internal/lefthook/run/prepare_command.go index 644b8bb9..a3d9cb3f 100644 --- a/internal/lefthook/run/prepare_command.go +++ b/internal/lefthook/run/prepare_command.go @@ -97,7 +97,12 @@ func (r *Runner) buildRun(command *config.Command) (*run, error, error) { for filesType, fn := range filesFns { cnt := strings.Count(command.Run, filesType) if cnt == 0 { - continue + if (r.AllFilesIncludingUntracked && filesType == config.SubAllFilesIncludingUntracked) || + (r.AllFiles && filesType == config.SubAllFiles) { + cnt++ + } else { + continue + } } templ := &template{cnt: cnt} @@ -146,8 +151,10 @@ func (r *Runner) buildRun(command *config.Command) (*run, error, error) { } result := replaceInChunks(runString, templates, maxlen) - if r.Force || len(result.files) != 0 { + if r.Force || len(result.files) > 0 { return result, nil, nil + } else if len(result.files) == 0 { + return nil, nil, errors.New("no matching files") } if config.HookUsesStagedFiles(r.HookName) { @@ -232,6 +239,12 @@ func replaceInChunks(str string, templates map[string]*template, maxlen int) *ru template.files = escapeFiles(template.files) } + if len(allFiles) == 0 { + return &run{ + commands: []string{str}, + } + } + maxlen -= len(str) if cnt > 0 {