From 6358e088496fce59b70772cecb97c01b35432d29 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Wed, 15 Mar 2023 19:56:58 +0300 Subject: [PATCH 1/3] fix: stage fixed when root specified --- internal/lefthook/runner/runner.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/lefthook/runner/runner.go b/internal/lefthook/runner/runner.go index d36b14e1..50408f27 100644 --- a/internal/lefthook/runner/runner.go +++ b/internal/lefthook/runner/runner.go @@ -382,6 +382,12 @@ func (r *Runner) runCommand(name string, command *config.Command) { files = prepareFiles(command, stagedFiles) } + if len(command.Root) > 0 { + for i, file := range files { + files[i] = filepath.Join(command.Root, file) + } + } + if err := r.Repo.AddFiles(files); err != nil { log.Warn("Couldn't stage fixed files:", err) } From db0b995286df7b0ca42b614d29cde4011c8e270f Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Thu, 16 Mar 2023 11:15:39 +0300 Subject: [PATCH 2/3] fix: add tests --- internal/lefthook/runner/prepare_command.go | 2 ++ internal/lefthook/runner/runner.go | 14 +++++----- internal/lefthook/runner/runner_test.go | 30 +++++++++++++++++++-- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/internal/lefthook/runner/prepare_command.go b/internal/lefthook/runner/prepare_command.go index e4bcec37..fae05ed0 100644 --- a/internal/lefthook/runner/prepare_command.go +++ b/internal/lefthook/runner/prepare_command.go @@ -89,6 +89,8 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error, if len(filteredFiles) == 0 && config.HookUsesStagedFiles(r.HookName) { files, err := r.Repo.StagedFiles() + log.Infof("FILES: %#v\n", files) + log.Infof("FILES Prepared: %#v\n", prepareFiles(command, files)) if err == nil { if len(prepareFiles(command, files)) == 0 { return nil, nil, errors.New("no matching staged files") diff --git a/internal/lefthook/runner/runner.go b/internal/lefthook/runner/runner.go index 50408f27..35fc4840 100644 --- a/internal/lefthook/runner/runner.go +++ b/internal/lefthook/runner/runner.go @@ -299,9 +299,7 @@ func (r *Runner) runScript(script *config.Script, path string, file os.FileInfo) return } - if err := r.Repo.AddFiles(files); err != nil { - log.Warn("Couldn't stage fixed files:", err) - } + r.addStagedFiles(files) } } @@ -388,9 +386,13 @@ func (r *Runner) runCommand(name string, command *config.Command) { } } - if err := r.Repo.AddFiles(files); err != nil { - log.Warn("Couldn't stage fixed files:", err) - } + r.addStagedFiles(files) + } +} + +func (r *Runner) addStagedFiles(files []string) { + if err := r.Repo.AddFiles(files); err != nil { + log.Warn("Couldn't stage fixed files:", err) } } diff --git a/internal/lefthook/runner/runner_test.go b/internal/lefthook/runner/runner_test.go index 933f7e18..92d1ef1b 100644 --- a/internal/lefthook/runner/runner_test.go +++ b/internal/lefthook/runner/runner_test.go @@ -62,7 +62,7 @@ func (g *GitMock) CmdLines(cmd string) ([]string, error) { cmd == "git diff --name-only HEAD @{push}" { root, _ := filepath.Abs("src") return []string{ - filepath.Join(root, "script.sh"), + filepath.Join(root, "scripts", "script.sh"), filepath.Join(root, "README.md"), }, nil } @@ -423,7 +423,7 @@ func TestRunAll(t *testing.T) { existingFiles: []string{ filepath.Join(root, config.DefaultSourceDir, "pre-commit", "success.sh"), filepath.Join(root, config.DefaultSourceDir, "pre-commit", "failing.js"), - filepath.Join(root, "script.sh"), + filepath.Join(root, "scripts", "script.sh"), filepath.Join(root, "README.md"), }, hook: &config.Hook{ @@ -493,6 +493,32 @@ func TestRunAll(t *testing.T) { "git stash list", }, }, + { + name: "pre-commit hook with stage_fixed under root", + hookName: "pre-commit", + existingFiles: []string{ + filepath.Join(root, "scripts", "script.sh"), + filepath.Join(root, "README.md"), + }, + hook: &config.Hook{ + Commands: map[string]*config.Command{ + "ok": { + Run: "success", + Root: filepath.Join(root, "scripts"), + StageFixed: true, + }, + }, + }, + success: []Result{{Name: "ok", Status: StatusOk}}, + gitCommands: []string{ + "git status --short", + "git diff --name-only --cached --diff-filter=ACMR", + "git diff --name-only --cached --diff-filter=ACMR", + "git add .*scripts.*script.sh", + "git apply -v --whitespace=nowarn --recount --unidiff-zero ", + "git stash list", + }, + }, { name: "pre-push hook with implicit skip", hookName: "pre-push", From 42a76c9ab07f90a8efe238b692283ae53478bf51 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Thu, 16 Mar 2023 11:20:46 +0300 Subject: [PATCH 3/3] docs: update CHANGELOG --- CHANGELOG.md | 1 + internal/lefthook/runner/prepare_command.go | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7d5a6df..e4583ff9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## master (unreleased) +- fix: stage fixed when root specified ([#449](https://github.com/evilmartians/lefthook/pull/449)) by @mrexox - feat: implitic skip on missing files for pre-commit and pre-push hooks ([#448](https://github.com/evilmartians/lefthook/pull/448)) by @mrexox ## 1.3.5 (2024-03-15) diff --git a/internal/lefthook/runner/prepare_command.go b/internal/lefthook/runner/prepare_command.go index fae05ed0..e4bcec37 100644 --- a/internal/lefthook/runner/prepare_command.go +++ b/internal/lefthook/runner/prepare_command.go @@ -89,8 +89,6 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error, if len(filteredFiles) == 0 && config.HookUsesStagedFiles(r.HookName) { files, err := r.Repo.StagedFiles() - log.Infof("FILES: %#v\n", files) - log.Infof("FILES Prepared: %#v\n", prepareFiles(command, files)) if err == nil { if len(prepareFiles(command, files)) == 0 { return nil, nil, errors.New("no matching staged files")