Skip to content

Commit 2bcab48

Browse files
authored
fix: skip if any files template is empty (#1275)
1 parent 87350d6 commit 2bcab48

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

internal/run/controller/command/build_command.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ func (b *Builder) buildCommand(params *JobParams) ([]string, []string, error) {
2727
}
2828

2929
// Checking substitutions and skipping execution if it is empty.
30-
//
30+
if !b.opts.Force && replacer.HasEmpty() {
31+
return nil, nil, SkipError{"no files for inspection"}
32+
}
33+
3134
// Special case when `files` option specified but not referenced in `run`: return if the result is empty.
3235
if !b.opts.Force && len(params.FilesCmd) > 0 && replacer.Empty(config.SubFiles) {
3336
files, err := replacer.Files(config.SubFiles, filter)
@@ -55,10 +58,6 @@ func (b *Builder) buildCommand(params *JobParams) ([]string, []string, error) {
5558
}
5659

5760
if len(files) == 0 {
58-
if replacer.Cached(config.SubStagedFiles) {
59-
return nil, nil, SkipError{"no matching staged files"}
60-
}
61-
6261
files, err = b.git.StagedFilesWithDeleted()
6362
if err != nil {
6463
return nil, nil, err

internal/run/controller/command/replacer/replacer.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ func (r Replacer) Discover(source string, filter *filter.Filter) error {
105105
return nil
106106
}
107107

108+
func (r Replacer) HasEmpty() bool {
109+
for _, entry := range r.cache {
110+
if len(entry.items) == 0 {
111+
return true
112+
}
113+
}
114+
115+
return false
116+
}
117+
108118
func (r Replacer) Cached(key string) bool {
109119
_, ok := r.cache[key]
110120

tests/integration/run_deleted_only.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ exec lefthook install
77
exec rm A.txt
88
exec git add -A
99
exec git commit -m 'test'
10-
stderr 'no matching staged files'
10+
stderr 'no files for inspection'
1111

1212
-- lefthook.yml --
1313
pre-commit:

0 commit comments

Comments
 (0)