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/runner.go b/internal/lefthook/runner/runner.go index d36b14e1..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) } } @@ -382,9 +380,19 @@ func (r *Runner) runCommand(name string, command *config.Command) { files = prepareFiles(command, stagedFiles) } - if err := r.Repo.AddFiles(files); err != nil { - log.Warn("Couldn't stage fixed files:", err) + if len(command.Root) > 0 { + for i, file := range files { + files[i] = filepath.Join(command.Root, file) + } } + + 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",