Compare commits

...

2 Commits

Author SHA1 Message Date
Luka Markušić
d8cffa314e Check for skipping pre-commit hooks in more situations 2024-04-27 08:48:21 +02:00
Luka Markušić
4ec41c4414 Add integration tests for skipping pre-commit hooks 2024-04-27 08:47:54 +02:00
3 changed files with 104 additions and 1 deletions

View File

@@ -22,7 +22,13 @@ func NewCommitCommands(gitCommon *GitCommon) *CommitCommands {
// ResetAuthor resets the author of the topmost commit
func (self *CommitCommands) ResetAuthor() error {
message, err := self.GetCommitMessage("HEAD")
if err != nil {
return err
}
skipHookPrefix := self.UserConfig.Git.SkipHookPrefix
cmdArgs := NewGitCmd("commit").
ArgIf(skipHookPrefix != "" && strings.HasPrefix(message, skipHookPrefix), "--no-verify").
Arg("--allow-empty", "--only", "--no-edit", "--amend", "--reset-author").
ToArgv()
@@ -31,7 +37,14 @@ func (self *CommitCommands) ResetAuthor() error {
// Sets the commit's author to the supplied value. Value is expected to be of the form 'Name <Email>'
func (self *CommitCommands) SetAuthor(value string) error {
message, err := self.GetCommitMessage("HEAD")
if err != nil {
return err
}
skipHookPrefix := self.UserConfig.Git.SkipHookPrefix
cmdArgs := NewGitCmd("commit").
ArgIf(skipHookPrefix != "" && strings.HasPrefix(message, skipHookPrefix), "--no-verify").
Arg("--allow-empty", "--only", "--no-edit", "--amend", "--author="+value).
ToArgv()
@@ -47,7 +60,10 @@ func (self *CommitCommands) AddCoAuthor(hash string, author string) error {
message = AddCoAuthorToMessage(message, author)
skipHookPrefix := self.UserConfig.Git.SkipHookPrefix
cmdArgs := NewGitCmd("commit").
ArgIf(skipHookPrefix != "" && strings.HasPrefix(message, skipHookPrefix), "--no-verify").
Arg("--allow-empty", "--amend", "--only", "-m", message).
ToArgv()
@@ -100,11 +116,15 @@ func (self *CommitCommands) CommitCmdObj(summary string, description string) osc
}
func (self *CommitCommands) RewordLastCommitInEditorCmdObj() oscommands.ICmdObj {
return self.cmd.New(NewGitCmd("commit").Arg("--allow-empty", "--amend", "--only").ToArgv())
return self.cmd.New(NewGitCmd("commit").
// TODO: how to decide if we should add --no-verify if we're using the editor?
Arg("--allow-empty", "--amend", "--only").ToArgv())
}
func (self *CommitCommands) RewordLastCommitInEditorWithMessageFileCmdObj(tmpMessageFile string) oscommands.ICmdObj {
return self.cmd.New(NewGitCmd("commit").
// TODO: how to decide if we should add --no-verify if we're using the editor?
Arg("--allow-empty", "--amend", "--only", "--edit", "--file="+tmpMessageFile).ToArgv())
}
@@ -120,7 +140,10 @@ func (self *CommitCommands) CommitInEditorWithMessageFileCmdObj(tmpMessageFile s
func (self *CommitCommands) RewordLastCommit(summary string, description string) error {
messageArgs := self.commitMessageArgs(summary, description)
skipHookPrefix := self.UserConfig.Git.SkipHookPrefix
cmdArgs := NewGitCmd("commit").
ArgIf(skipHookPrefix != "" && strings.HasPrefix(summary, skipHookPrefix), "--no-verify").
Arg("--allow-empty", "--amend", "--only").
Arg(messageArgs...).
ToArgv()
@@ -248,7 +271,15 @@ func (self *CommitCommands) AmendHead() error {
}
func (self *CommitCommands) AmendHeadCmdObj() oscommands.ICmdObj {
message, err := self.GetCommitMessage("HEAD")
if err != nil {
// TODO: what to do here? we can't return err
// return err
}
skipHookPrefix := self.UserConfig.Git.SkipHookPrefix
cmdArgs := NewGitCmd("commit").
ArgIf(skipHookPrefix != "" && strings.HasPrefix(message, skipHookPrefix), "--no-verify").
Arg("--amend", "--no-edit", "--allow-empty").
ToArgv()

View File

@@ -0,0 +1,71 @@
package commit
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
const preCommitHook = `#!/bin/bash
exit 1
`
var CommitSkipHook = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Commit with pre-commit hook and skip hook config option in various situations.",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(testConfig *config.AppConfig) {
testConfig.UserConfig.Git.SkipHookPrefix = "skip! "
},
SetupRepo: func(shell *Shell) {
shell.SetConfig("user.email", "Bill@example.com")
shell.SetConfig("user.name", "Bill Smith")
shell.CreateFileAndAdd("initial file", "initial content")
shell.Commit("initial commit")
shell.SetConfig("user.email", "John@example.com")
shell.SetConfig("user.name", "John Smith")
shell.CreateFile(".git/hooks/pre-commit", preCommitHook)
shell.MakeExecutable(".git/hooks/pre-commit")
shell.CreateFileAndAdd("testfile", "I'm just testing pre-commit hooks")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Files().
Focus().
Press(keys.Files.CommitChanges)
// hook should trigger when creating a regular commit
t.ExpectPopup().CommitMessagePanel().Type("my commit message").Confirm()
t.ExpectPopup().Alert().Title(Equals("Error")).Content(Contains("Git command failed")).Confirm()
t.Views().Files().
Focus().
Press(keys.Files.CommitChanges)
// we should be able to skip hooks when creating a regular commit
t.ExpectPopup().CommitMessagePanel().Clear().Type("skip! my commit message").Confirm()
t.Views().Commits().Focus().Lines(
Contains("skip! my commit message"),
Contains("initial commit"),
)
// we should be able to skip hooks when rewording a commit
t.Views().Commits().Focus().Press(keys.Commits.RenameCommit)
t.ExpectPopup().CommitMessagePanel().Type(" (reworded)").Confirm()
t.Views().Commits().IsFocused().
Lines(
Contains("skip! my commit message (reworded)"),
Contains("initial commit"),
)
// we should be able to skip hooks when changing authors
t.Views().Commits().IsFocused().SelectedLine(Contains("JS").IsSelected())
t.Views().Commits().Focus().Press(keys.Commits.ResetCommitAuthor)
t.Views().Commits().IsFocused().Lines(Contains("JS").IsSelected())
},
})

View File

@@ -71,6 +71,7 @@ var tests = []*components.IntegrationTest{
commit.Commit,
commit.CommitMultiline,
commit.CommitSwitchToEditor,
commit.CommitSkipHook,
commit.CommitWipWithPrefix,
commit.CommitWithPrefix,
commit.CreateAmendCommit,