Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stage fixed when root specified #449

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 13 additions & 5 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
30 changes: 28 additions & 2 deletions internal/lefthook/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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",
Expand Down