diff --git a/Makefile b/Makefile index 63834a69..85075b3d 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,14 @@ build: build-with-coverage: go build -cover -ldflags "-s -w -X github.com/evilmartians/lefthook/internal/version.commit=$(COMMIT_HASH)" -o lefthook +install: build + cp lefthook $(GOPATH)/bin/ + test: go test -cpu 24 -race -count=1 -timeout=30s ./... -test-integration: build-with-coverage - ./lefthook dump - ./lefthook dump --json - ./lefthook dump --toml +test-integrity: install + go test -cpu 24 -race -count=1 -timeout=30s -tags=integrity integrity_test.go bench: go test -cpu 24 -race -run=Bench -bench=. ./... diff --git a/internal/lefthook/runner/prepare_command.go b/internal/lefthook/runner/prepare_command.go index aab1c231..67dbfe10 100644 --- a/internal/lefthook/runner/prepare_command.go +++ b/internal/lefthook/runner/prepare_command.go @@ -62,7 +62,7 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error, }, } - filteredFiles := []string{} + filesFiltered := make([]string, 0) runString := command.Run for filesType, filesFn := range filesTypeToFn { // Checking substitutions and skipping execution if it is empty. @@ -79,28 +79,31 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error, return nil, nil, errors.New("no files for inspection") } - filesPrepared := prepareFiles(command, files) - if len(filesPrepared) == 0 { + filtered := filterFiles(command, files) + filesFiltered = append(filesFiltered, filtered...) + + prepared := escapeFiles(filtered) + if len(prepared) == 0 { return nil, nil, errors.New("no files for inspection") } - filteredFiles = append(filteredFiles, filesPrepared...) - runString = replaceQuoted(runString, filesType, filesPrepared) + + runString = replaceQuoted(runString, filesType, prepared) } } - if len(filteredFiles) == 0 && config.HookUsesStagedFiles(r.HookName) { + if len(filesFiltered) == 0 && config.HookUsesStagedFiles(r.HookName) { files, err := r.Repo.StagedFiles() if err == nil { - if len(prepareFiles(command, files)) == 0 { + if len(filterFiles(command, files)) == 0 { return nil, nil, errors.New("no matching staged files") } } } - if len(filteredFiles) == 0 && config.HookUsesPushFiles(r.HookName) { + if len(filesFiltered) == 0 && config.HookUsesPushFiles(r.HookName) { files, err := r.Repo.PushFiles() if err == nil { - if len(prepareFiles(command, files)) == 0 { + if len(filterFiles(command, files)) == 0 { return nil, nil, errors.New("no matching push files") } } @@ -111,7 +114,7 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error, log.Debug("[lefthook] executing: ", runString) return &commandArgs{ - files: filteredFiles, + files: filesFiltered, all: strings.Split(runString, " "), }, nil, nil } @@ -124,7 +127,7 @@ func (r *Runner) replacePositionalArguments(runString string) string { return runString } -func prepareFiles(command *config.Command, files []string) []string { +func filterFiles(command *config.Command, files []string) []string { if files == nil { return []string{} } @@ -137,7 +140,11 @@ func prepareFiles(command *config.Command, files []string) []string { log.Debug("[lefthook] files after filters:\n", files) - // Escape file names to prevent unexpected bugs + return files +} + +// Escape file names to prevent unexpected bugs. +func escapeFiles(files []string) []string { var filesEsc []string for _, fileName := range files { if len(fileName) > 0 { diff --git a/internal/lefthook/runner/runner.go b/internal/lefthook/runner/runner.go index 377c8c79..81d42388 100644 --- a/internal/lefthook/runner/runner.go +++ b/internal/lefthook/runner/runner.go @@ -372,13 +372,16 @@ func (r *Runner) runCommand(name string, command *config.Command) { if finished && config.HookUsesStagedFiles(r.HookName) && command.StageFixed { files := args.files + if len(files) == 0 { - stagedFiles, err := r.Repo.StagedFiles() + var err error + files, err = r.Repo.StagedFiles() if err != nil { log.Warn("Couldn't stage fixed files:", err) return } - files = prepareFiles(command, stagedFiles) + + files = filterFiles(command, files) } if len(command.Root) > 0 { diff --git a/testdata/stage_fixed.txt b/testdata/stage_fixed.txt index 92257d70..fae06bf9 100644 --- a/testdata/stage_fixed.txt +++ b/testdata/stage_fixed.txt @@ -13,8 +13,13 @@ min_version: 1.1.1 pre-commit: commands: edit_file: - run: echo newline >> file.txt + run: | + echo newline >> "[file].js" + echo newline >> file.txt stage_fixed: true -- file.txt -- -firstline +sometext + +-- [file].js -- +somecode diff --git a/testdata/stage_fixed_505.txt b/testdata/stage_fixed_505.txt new file mode 100644 index 00000000..02c68926 --- /dev/null +++ b/testdata/stage_fixed_505.txt @@ -0,0 +1,19 @@ +exec git init +exec lefthook install +exec git config user.email "you@example.com" +exec git config user.name "Your Name" +exec git add -A +exec git status --short +exec git commit -m 'test stage_fixed' +exec git status --short +! stdout . + +-- lefthook.yml -- +pre-commit: + commands: + edit_file: + run: echo "{staged_files}" && echo newline >> "[file].js" + stage_fixed: true + +-- [file].js -- +somecode