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 pre-commit skip hook checks #3532

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 32 additions & 1 deletion pkg/commands/git_commands/commit.go
Expand Up @@ -22,7 +22,13 @@

// ResetAuthor resets the author of the topmost commit
func (self *CommitCommands) ResetAuthor() error {
message, err := self.GetCommitMessage("HEAD")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a bit wasteful to ask git for the commit message here when we have it in our model in memory already. Maybe we should pass in either the subject or a skipHook bool into this function (and the other ones below).

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()

Expand All @@ -31,7 +37,14 @@

// 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()

Expand All @@ -47,7 +60,10 @@

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()

Expand Down Expand Up @@ -100,11 +116,15 @@
}

func (self *CommitCommands) RewordLastCommitInEditorCmdObj() oscommands.ICmdObj {
return self.cmd.New(NewGitCmd("commit").Arg("--allow-empty", "--amend", "--only").ToArgv())

Check failure on line 119 in pkg/commands/git_commands/commit.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
return self.cmd.New(NewGitCmd("commit").
// TODO: how to decide if we should add --no-verify if we're using the editor?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the best we can do is to decide based on the existing subject and hope they don't change it in the editor. Either that, or just never pass --no-verify in this case.

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())
}

Expand All @@ -120,7 +140,10 @@
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()
Expand Down Expand Up @@ -248,7 +271,15 @@
}

func (self *CommitCommands) AmendHeadCmdObj() oscommands.ICmdObj {
message, err := self.GetCommitMessage("HEAD")
if err != nil {

Check failure on line 275 in pkg/commands/git_commands/commit.go

View workflow job for this annotation

GitHub Actions / lint

SA9003: empty branch (staticcheck)

Check failure on line 275 in pkg/commands/git_commands/commit.go

View workflow job for this annotation

GitHub Actions / lint

SA9003: empty branch (staticcheck)
// 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()

Expand Down
23 changes: 7 additions & 16 deletions pkg/integration/tests/commit/commit_skip_hook.go
Expand Up @@ -16,7 +16,7 @@
Skip: false,
SetupConfig: func(testConfig *config.AppConfig) {
testConfig.UserConfig.Git.SkipHookPrefix = "skip! "

Check failure on line 19 in pkg/integration/tests/commit/commit_skip_hook.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
},
SetupRepo: func(shell *Shell) {
shell.SetConfig("user.email", "[email protected]")
Expand Down Expand Up @@ -57,24 +57,15 @@
t.Views().Commits().Focus().Press(keys.Commits.RenameCommit)
t.ExpectPopup().CommitMessagePanel().Type(" (reworded)").Confirm()

/* EXPECTED:
t.Views().Commits().IsFocused().
Lines(
Contains("skip! my commit message (reworded)"),
Contains("initial commit"),
)
ACTUAL:
*/
t.ExpectPopup().Alert().Title(Equals("Error")).Content(Contains("exit status 1")).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("CI").IsSelected())
t.Views().Commits().IsFocused().SelectedLine(Contains("JS").IsSelected())
t.Views().Commits().Focus().Press(keys.Commits.ResetCommitAuthor)
/* EXPECTED:
t.Views().Commits().IsFocused().Lines(Contains("JS").IsSelected())
ACTUAL:
*/
t.ExpectPopup().Alert().Title(Equals("Error")).Content(Contains("exit status 1")).Confirm()

t.Views().Commits().IsFocused().Lines(Contains("JS").IsSelected())
},
})