summaryrefslogtreecommitdiffstats
path: root/pkg/commands
diff options
context:
space:
mode:
authorSean <seand52@gmail.com>2023-01-21 11:38:14 +0000
committerJesse Duffield <jessedduffield@gmail.com>2023-04-30 12:17:34 +1000
commit49da7b482d4f3012814e4100a556ecfad3f0742d (patch)
tree4d403b4301e0918c57ba292b11d6ce9256e8843a /pkg/commands
parent826128a8e03fb50f7287029ebac93c85712faecb (diff)
Split commit message panel into commit summary and commit description panel
When we use the one panel for the entire commit message, its tricky to have a keybinding both for adding a newline and submitting. By having two panels: one for the summary line and one for the description, we allow for 'enter' to submit the message when done from the summary panel, and 'enter' to add a newline when done from the description panel. Alt-enter, for those who can use that key combo, also works for submitting the message from the description panel. For those who can't use that key combo, and don't want to remap the keybinding, they can hit tab to go back to the summary panel and then 'enter' to submit the message. We have some awkwardness in that both contexts (i.e. panels) need to appear and disappear in tandem and we don't have a great way of handling that concept, so we just push both contexts one after the other, and likewise remove both contexts when we escape.
Diffstat (limited to 'pkg/commands')
-rw-r--r--pkg/commands/git_commands/commit.go41
-rw-r--r--pkg/commands/git_commands/commit_test.go67
2 files changed, 92 insertions, 16 deletions
diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go
index 38459e351..0c5008e38 100644
--- a/pkg/commands/git_commands/commit.go
+++ b/pkg/commands/git_commands/commit.go
@@ -8,6 +8,8 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
)
+var ErrInvalidCommitIndex = errors.New("invalid commit index")
+
type CommitCommands struct {
*GitCommon
}
@@ -18,11 +20,6 @@ func NewCommitCommands(gitCommon *GitCommon) *CommitCommands {
}
}
-// RewordLastCommit rewords the topmost commit with the given message
-func (self *CommitCommands) RewordLastCommit(message string) error {
- return self.cmd.New("git commit --allow-empty --amend --only -m " + self.cmd.Quote(message)).Run()
-}
-
// ResetAuthor resets the author of the topmost commit
func (self *CommitCommands) ResetAuthor() error {
return self.cmd.New("git commit --allow-empty --only --no-edit --amend --reset-author").Run()
@@ -45,11 +42,7 @@ func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars [
}
func (self *CommitCommands) CommitCmdObj(message string) oscommands.ICmdObj {
- splitMessage := strings.Split(message, "\n")
- lineArgs := ""
- for _, line := range splitMessage {
- lineArgs += fmt.Sprintf(" -m %s", self.cmd.Quote(line))
- }
+ messageArgs := self.commitMessageArgs(message)
skipHookPrefix := self.UserConfig.Git.SkipHookPrefix
noVerifyFlag := ""
@@ -57,7 +50,23 @@ func (self *CommitCommands) CommitCmdObj(message string) oscommands.ICmdObj {
noVerifyFlag = " --no-verify"
}
- return self.cmd.New(fmt.Sprintf("git commit%s%s%s", noVerifyFlag, self.signoffFlag(), lineArgs))
+ return self.cmd.New(fmt.Sprintf("git commit%s%s%s", noVerifyFlag, self.signoffFlag(), messageArgs))
+}
+
+// RewordLastCommit rewords the topmost commit with the given message
+func (self *CommitCommands) RewordLastCommit(message string) error {
+ messageArgs := self.commitMessageArgs(message)
+ return self.cmd.New(fmt.Sprintf("git commit --allow-empty --amend --only%s", messageArgs)).Run()
+}
+
+func (self *CommitCommands) commitMessageArgs(message string) string {
+ msg, description, _ := strings.Cut(message, "\n")
+ descriptionArgs := ""
+ if description != "" {
+ descriptionArgs = fmt.Sprintf(" -m %s", self.cmd.Quote(description))
+ }
+
+ return fmt.Sprintf(" -m %s%s", self.cmd.Quote(msg), descriptionArgs)
}
// runs git commit without the -m argument meaning it will invoke the user's editor
@@ -178,3 +187,13 @@ func (self *CommitCommands) RevertMerge(sha string, parentNumber int) error {
func (self *CommitCommands) CreateFixupCommit(sha string) error {
return self.cmd.New(fmt.Sprintf("git commit --fixup=%s", sha)).Run()
}
+
+// a value of 0 means the head commit, 1 is the parent commit, etc
+func (self *CommitCommands) GetCommitMessageFromHistory(value int) (string, error) {
+ hash, _ := self.cmd.New(fmt.Sprintf("git log -1 --skip=%d --pretty=%%H", value)).DontLog().RunWithOutput()
+ formattedHash := strings.TrimSpace(hash)
+ if len(formattedHash) == 0 {
+ return "", ErrInvalidCommitIndex
+ }
+ return self.GetCommitMessage(formattedHash)
+}
diff --git a/pkg/commands/git_commands/commit_test.go b/pkg/commands/git_commands/commit_test.go
index 268e44e46..4cc8a8de2 100644
--- a/pkg/commands/git_commands/commit_test.go
+++ b/pkg/commands/git_commands/commit_test.go
@@ -9,12 +9,32 @@ import (
)
func TestCommitRewordCommit(t *testing.T) {
- runner := oscommands.NewFakeRunner(t).
- ExpectGitArgs([]string{"commit", "--allow-empty", "--amend", "--only", "-m", "test"}, "", nil)
- instance := buildCommitCommands(commonDeps{runner: runner})
+ type scenario struct {
+ testName string
+ runner *oscommands.FakeCmdObjRunner
+ input string
+ }
+ scenarios := []scenario{
+ {
+ "Single line reword",
+ oscommands.NewFakeRunner(t).ExpectGitArgs([]string{"commit", "--allow-empty", "--amend", "--only", "-m", "test"}, "", nil),
+ "test",
+ },
+ {
+ "Multi line reword",
+ oscommands.NewFakeRunner(t).ExpectGitArgs([]string{"commit", "--allow-empty", "--amend", "--only", "-m", "test", "-m", "line 2\nline 3"}, "", nil),
+ "test\nline 2\nline 3",
+ },
+ }
+ for _, s := range scenarios {
+ s := s
+ t.Run(s.testName, func(t *testing.T) {
+ instance := buildCommitCommands(commonDeps{runner: s.runner})
- assert.NoError(t, instance.RewordLastCommit("test"))
- runner.CheckForMissingCalls()
+ assert.NoError(t, instance.RewordLastCommit(s.input))
+ s.runner.CheckForMissingCalls()
+ })
+ }
}
func TestCommitResetToCommit(t *testing.T) {
@@ -274,3 +294,40 @@ Merge pull request #1750 from mark2185/fix-issue-template
})
}
}
+
+func TestGetCommitMessageFromHistory(t *testing.T) {
+ type scenario struct {
+ testName string
+ runner *oscommands.FakeCmdObjRunner
+ test func(string, error)
+ }
+ scenarios := []scenario{
+ {
+ "Empty message",
+ oscommands.NewFakeRunner(t).Expect("git log -1 --skip=2 --pretty=%H", "", nil).Expect("git rev-list --format=%B --max-count=1 ", "", nil),
+ func(output string, err error) {
+ assert.Error(t, err)
+ },
+ },
+ {
+ "Default case to retrieve a commit in history",
+ oscommands.NewFakeRunner(t).Expect("git log -1 --skip=2 --pretty=%H", "sha3 \n", nil).Expect("git rev-list --format=%B --max-count=1 sha3", `commit sha3
+ use generics to DRY up context code`, nil),
+ func(output string, err error) {
+ assert.NoError(t, err)
+ assert.Equal(t, "use generics to DRY up context code", output)
+ },
+ },
+ }
+
+ for _, s := range scenarios {
+ s := s
+ t.Run(s.testName, func(t *testing.T) {
+ instance := buildCommitCommands(commonDeps{runner: s.runner})
+
+ output, err := instance.GetCommitMessageFromHistory(2)
+
+ s.test(output, err)
+ })
+ }
+}