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
mark2185
wants to merge
2
commits into
master
Choose a base branch
from
fix-skip-hook
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,13 @@ | |
|
||
// 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 @@ | |
|
||
// 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 @@ | |
|
||
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) 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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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) 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) AmendHeadCmdObj() oscommands.ICmdObj { | ||
message, err := self.GetCommitMessage("HEAD") | ||
if err != nil { | ||
Check failure on line 275 in pkg/commands/git_commands/commit.go GitHub Actions / lint
|
||
// 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() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", "[email protected]") | ||
shell.SetConfig("user.name", "Bill Smith") | ||
|
||
shell.CreateFileAndAdd("initial file", "initial content") | ||
shell.Commit("initial commit") | ||
|
||
shell.SetConfig("user.email", "[email protected]") | ||
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()) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).