1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-13 13:59:06 +02:00

Fix preserving the commit message when description contains blank lines (#3170)

This commit is contained in:
Stefan Haller 2024-01-09 14:35:43 +01:00 committed by GitHub
commit 15da702140
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 62 additions and 25 deletions

View File

@ -137,12 +137,11 @@ func (self *CommitCommands) signoffFlag() string {
}
func (self *CommitCommands) GetCommitMessage(commitSha string) (string, error) {
cmdArgs := NewGitCmd("rev-list").
cmdArgs := NewGitCmd("log").
Arg("--format=%B", "--max-count=1", commitSha).
ToArgv()
messageWithHeader, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
message := strings.Join(strings.SplitAfter(messageWithHeader, "\n")[1:], "")
message, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
return strings.TrimSpace(message), err
}

View File

@ -260,19 +260,17 @@ func TestGetCommitMsg(t *testing.T) {
scenarios := []scenario{
{
"empty",
` commit deadbeef`,
``,
``,
},
{
"no line breaks (single line)",
`commit deadbeef
use generics to DRY up context code`,
`use generics to DRY up context code`,
`use generics to DRY up context code`,
},
{
"with line breaks",
`commit deadbeef
Merge pull request #1750 from mark2185/fix-issue-template
`Merge pull request #1750 from mark2185/fix-issue-template
'git-rev parse' should be 'git rev-parse'`,
`Merge pull request #1750 from mark2185/fix-issue-template
@ -285,7 +283,7 @@ Merge pull request #1750 from mark2185/fix-issue-template
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildCommitCommands(commonDeps{
runner: oscommands.NewFakeRunner(t).ExpectGitArgs([]string{"rev-list", "--format=%B", "--max-count=1", "deadbeef"}, s.input, nil),
runner: oscommands.NewFakeRunner(t).ExpectGitArgs([]string{"log", "--format=%B", "--max-count=1", "deadbeef"}, s.input, nil),
})
output, err := instance.GetCommitMessage("deadbeef")
@ -306,15 +304,14 @@ func TestGetCommitMessageFromHistory(t *testing.T) {
scenarios := []scenario{
{
"Empty message",
oscommands.NewFakeRunner(t).ExpectGitArgs([]string{"log", "-1", "--skip=2", "--pretty=%H"}, "", nil).ExpectGitArgs([]string{"rev-list", "--format=%B", "--max-count=1"}, "", nil),
oscommands.NewFakeRunner(t).ExpectGitArgs([]string{"log", "-1", "--skip=2", "--pretty=%H"}, "", nil).ExpectGitArgs([]string{"log", "--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).ExpectGitArgs([]string{"log", "-1", "--skip=2", "--pretty=%H"}, "sha3 \n", nil).ExpectGitArgs([]string{"rev-list", "--format=%B", "--max-count=1", "sha3"}, `commit sha3
use generics to DRY up context code`, nil),
oscommands.NewFakeRunner(t).ExpectGitArgs([]string{"log", "-1", "--skip=2", "--pretty=%H"}, "sha3 \n", nil).ExpectGitArgs([]string{"log", "--format=%B", "--max-count=1", "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)

View File

@ -41,13 +41,8 @@ func NewCommitsHelper(
}
func (self *CommitsHelper) SplitCommitMessageAndDescription(message string) (string, string) {
for _, separator := range []string{"\n\n", "\n\r\n\r", "\n", "\n\r"} {
msg, description, found := strings.Cut(message, separator)
if found {
return msg, description
}
}
return message, ""
msg, description, _ := strings.Cut(message, "\n")
return msg, strings.TrimSpace(description)
}
func (self *CommitsHelper) SetMessageAndDescriptionInView(message string) {

View File

@ -8,6 +8,13 @@ func (self *CommitDescriptionPanelDriver) getViewDriver() *ViewDriver {
return self.t.Views().CommitDescription()
}
// asserts on the current context of the description
func (self *CommitDescriptionPanelDriver) Content(expected *TextMatcher) *CommitDescriptionPanelDriver {
self.getViewDriver().Content(expected)
return self
}
func (self *CommitDescriptionPanelDriver) Type(value string) *CommitDescriptionPanelDriver {
self.t.typeContent(value)
@ -29,3 +36,7 @@ func (self *CommitDescriptionPanelDriver) Title(expected *TextMatcher) *CommitDe
return self
}
func (self *CommitDescriptionPanelDriver) Cancel() {
self.getViewDriver().PressEscape()
}

View File

@ -38,12 +38,6 @@ func (self *CommitMessagePanelDriver) SwitchToDescription() *CommitDescriptionPa
return &CommitDescriptionPanelDriver{t: self.t}
}
func (self *CommitMessagePanelDriver) AddNewline() *CommitMessagePanelDriver {
self.t.press(self.t.keys.Universal.Confirm)
return self
}
func (self *CommitMessagePanelDriver) Clear() *CommitMessagePanelDriver {
// clearing multiple times in case there's multiple lines
// (the clear button only clears a single line at a time)

View File

@ -0,0 +1,40 @@
package commit
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var PreserveCommitMessage = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Test that the commit message is preserved correctly when canceling the commit message panel",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.CreateFileAndAdd("myfile", "myfile content")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Files().
IsFocused().
Press(keys.Files.CommitChanges)
t.ExpectPopup().CommitMessagePanel().
InitialText(Equals("")).
Type("my commit message").
SwitchToDescription().
Type("first paragraph").
AddNewline().
AddNewline().
Type("second paragraph").
Cancel()
t.Views().Files().
IsFocused().
Press(keys.Files.CommitChanges)
t.ExpectPopup().CommitMessagePanel().
Content(Equals("my commit message")).
SwitchToDescription().
Content(Equals("first paragraph\n\nsecond paragraph"))
},
})

View File

@ -74,6 +74,7 @@ var tests = []*components.IntegrationTest{
commit.History,
commit.HistoryComplex,
commit.NewBranch,
commit.PreserveCommitMessage,
commit.ResetAuthor,
commit.Revert,
commit.RevertMerge,