mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-30 03:23:08 +03:00
Make it easy to create "amend!" commits
To support this, we turn the confirmation prompt of the "Create fixup commit" command into a menu; creating a fixup commit is the first entry, so that "shift-F, enter" behaves the same as before. But there are additional entries for creating "amend!" commits, either with or without file changes. These make it easy to reword commit messages of existing commits.
This commit is contained in:
@ -297,6 +297,21 @@ func (self *CommitCommands) CreateFixupCommit(sha string) error {
|
||||
return self.cmd.New(cmdArgs).Run()
|
||||
}
|
||||
|
||||
// CreateAmendCommit creates a commit that changes the commit message of a previous commit
|
||||
func (self *CommitCommands) CreateAmendCommit(originalSubject, newSubject, newDescription string, includeFileChanges bool) error {
|
||||
description := newSubject
|
||||
if newDescription != "" {
|
||||
description += "\n\n" + newDescription
|
||||
}
|
||||
cmdArgs := NewGitCmd("commit").
|
||||
Arg("-m", "amend! "+originalSubject).
|
||||
Arg("-m", description).
|
||||
ArgIf(!includeFileChanges, "--only", "--allow-empty").
|
||||
ToArgv()
|
||||
|
||||
return self.cmd.New(cmdArgs).Run()
|
||||
}
|
||||
|
||||
// a value of 0 means the head commit, 1 is the parent commit, etc
|
||||
func (self *CommitCommands) GetCommitMessageFromHistory(value int) (string, error) {
|
||||
cmdArgs := NewGitCmd("log").Arg("-1", fmt.Sprintf("--skip=%d", value), "--pretty=%H").
|
||||
|
@ -180,6 +180,57 @@ func TestCommitCreateFixupCommit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommitCreateAmendCommit(t *testing.T) {
|
||||
type scenario struct {
|
||||
testName string
|
||||
originalSubject string
|
||||
newSubject string
|
||||
newDescription string
|
||||
includeFileChanges bool
|
||||
runner *oscommands.FakeCmdObjRunner
|
||||
}
|
||||
|
||||
scenarios := []scenario{
|
||||
{
|
||||
testName: "subject only",
|
||||
originalSubject: "original subject",
|
||||
newSubject: "new subject",
|
||||
newDescription: "",
|
||||
includeFileChanges: true,
|
||||
runner: oscommands.NewFakeRunner(t).
|
||||
ExpectGitArgs([]string{"commit", "-m", "amend! original subject", "-m", "new subject"}, "", nil),
|
||||
},
|
||||
{
|
||||
testName: "subject and description",
|
||||
originalSubject: "original subject",
|
||||
newSubject: "new subject",
|
||||
newDescription: "new description",
|
||||
includeFileChanges: true,
|
||||
runner: oscommands.NewFakeRunner(t).
|
||||
ExpectGitArgs([]string{"commit", "-m", "amend! original subject", "-m", "new subject\n\nnew description"}, "", nil),
|
||||
},
|
||||
{
|
||||
testName: "without file changes",
|
||||
originalSubject: "original subject",
|
||||
newSubject: "new subject",
|
||||
newDescription: "",
|
||||
includeFileChanges: false,
|
||||
runner: oscommands.NewFakeRunner(t).
|
||||
ExpectGitArgs([]string{"commit", "-m", "amend! original subject", "-m", "new subject", "--only", "--allow-empty"}, "", nil),
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarios {
|
||||
s := s
|
||||
t.Run(s.testName, func(t *testing.T) {
|
||||
instance := buildCommitCommands(commonDeps{runner: s.runner})
|
||||
err := instance.CreateAmendCommit(s.originalSubject, s.newSubject, s.newDescription, s.includeFileChanges)
|
||||
assert.NoError(t, err)
|
||||
s.runner.CheckForMissingCalls()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommitShowCmdObj(t *testing.T) {
|
||||
type scenario struct {
|
||||
testName string
|
||||
|
Reference in New Issue
Block a user