From 9e721e74793f908d1b3de198426ad5e77694e89f Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Mon, 19 Jun 2023 13:25:07 +0300 Subject: [PATCH 1/4] fix: auto stage non standard files --- Makefile | 6 ++---- internal/lefthook/runner/runner.go | 16 ++++++++-------- testdata/stage_fixed.txt | 9 +++++++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 63834a69..201f5c38 100644 --- a/Makefile +++ b/Makefile @@ -9,10 +9,8 @@ build-with-coverage: 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: + 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/runner.go b/internal/lefthook/runner/runner.go index 377c8c79..736c07f7 100644 --- a/internal/lefthook/runner/runner.go +++ b/internal/lefthook/runner/runner.go @@ -371,16 +371,16 @@ func (r *Runner) runCommand(name string, command *config.Command) { }, r.Hook.Follow) if finished && config.HookUsesStagedFiles(r.HookName) && command.StageFixed { - files := args.files - if len(files) == 0 { - stagedFiles, err := r.Repo.StagedFiles() - if err != nil { - log.Warn("Couldn't stage fixed files:", err) - return - } - files = prepareFiles(command, stagedFiles) + files, err := r.Repo.StagedFiles() + if err != nil { + log.Warn("Couldn't stage fixed files:", err) + return } + files = filterGlob(files, command.Glob) + files = filterExclude(files, command.Exclude) + files = filterRelative(files, command.Root) + if len(command.Root) > 0 { for i, file := range files { files[i] = filepath.Join(command.Root, file) 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 From c43113176ffc142e7920646655302d946d1ae236 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Mon, 19 Jun 2023 13:59:13 +0300 Subject: [PATCH 2/4] fix: support previously defined staged files --- internal/lefthook/runner/prepare_command.go | 31 +++++++++++++-------- internal/lefthook/runner/runner.go | 19 +++++++------ 2 files changed, 30 insertions(+), 20 deletions(-) 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 736c07f7..45472396 100644 --- a/internal/lefthook/runner/runner.go +++ b/internal/lefthook/runner/runner.go @@ -371,15 +371,18 @@ func (r *Runner) runCommand(name string, command *config.Command) { }, r.Hook.Follow) if finished && config.HookUsesStagedFiles(r.HookName) && command.StageFixed { - files, err := r.Repo.StagedFiles() - if err != nil { - log.Warn("Couldn't stage fixed files:", err) - return - } + files := args.files + + if files == nil || len(files) == 0 { + var err error + files, err = r.Repo.StagedFiles() + if err != nil { + log.Warn("Couldn't stage fixed files:", err) + return + } - files = filterGlob(files, command.Glob) - files = filterExclude(files, command.Exclude) - files = filterRelative(files, command.Root) + files = filterFiles(command, files) + } if len(command.Root) > 0 { for i, file := range files { From 8c44e0fefa7fc0d27e7daec21bf70b815c4fee5f Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Mon, 19 Jun 2023 14:01:48 +0300 Subject: [PATCH 3/4] chore: easier testing --- Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 201f5c38..85075b3d 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,13 @@ 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-integrity: +test-integrity: install go test -cpu 24 -race -count=1 -timeout=30s -tags=integrity integrity_test.go bench: From f507561bdeba3ad24bd91b5ea5682257f06ee748 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Mon, 19 Jun 2023 14:08:45 +0300 Subject: [PATCH 4/4] chore: add a testcase --- internal/lefthook/runner/runner.go | 2 +- testdata/stage_fixed_505.txt | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 testdata/stage_fixed_505.txt diff --git a/internal/lefthook/runner/runner.go b/internal/lefthook/runner/runner.go index 45472396..81d42388 100644 --- a/internal/lefthook/runner/runner.go +++ b/internal/lefthook/runner/runner.go @@ -373,7 +373,7 @@ func (r *Runner) runCommand(name string, command *config.Command) { if finished && config.HookUsesStagedFiles(r.HookName) && command.StageFixed { files := args.files - if files == nil || len(files) == 0 { + if len(files) == 0 { var err error files, err = r.Repo.StagedFiles() if err != nil { 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