Skip to content

Commit

Permalink
Also glob commands without templates & files
Browse files Browse the repository at this point in the history
Commands that neither had templates in `run` nor
`files` didn't apply file globs, even if the
`--all-files` CLI param was set
  • Loading branch information
sanmai-NL committed Dec 14, 2023
1 parent 10f5ad4 commit 7277a40
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions internal/lefthook/run/prepare_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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) {
Expand All @@ -169,7 +176,6 @@ func (r *Runner) buildRun(command *config.Command) (*run, error, error) {
return nil, nil, errors.New("no matching push files")
}
}

return result, nil, nil
}

Expand Down Expand Up @@ -232,6 +238,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 {
Expand Down

0 comments on commit 7277a40

Please sign in to comment.