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 preserving the commit message when description contains blank lines #3170

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions pkg/commands/git_commands/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
15 changes: 6 additions & 9 deletions pkg/commands/git_commands/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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)
Expand Down
9 changes: 2 additions & 7 deletions pkg/gui/controllers/helpers/commits_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You might wonder if splitting just at \n will work on Windows. I tested this on my Windows machine, and it works fine (I confirmed in the debugger that the message string only contains bare \n characters even when rewording a commit message). I'm not 100% sure if this is true for all git versions on Windows though; I'm using the msys2 based one, maybe the Cygwin distribution behaves differently, I don't know.

However, I'm reasonably confident that only handling bare \n is enough, because if it weren't, then the previous code wouldn't have worked either, as it first tried to split at \n and only then tried \n\r, which is pointless because it would have never got there, so it would have left a bare \r in the result.

Copy link
Owner

Choose a reason for hiding this comment

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

Good catch haha

return msg, strings.TrimSpace(description)
}

func (self *CommitsHelper) SetMessageAndDescriptionInView(message string) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/integration/components/commit_description_panel_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -29,3 +36,7 @@ func (self *CommitDescriptionPanelDriver) Title(expected *TextMatcher) *CommitDe

return self
}

func (self *CommitDescriptionPanelDriver) Cancel() {
self.getViewDriver().PressEscape()
}
6 changes: 0 additions & 6 deletions pkg/integration/components/commit_message_panel_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions pkg/integration/tests/commit/preserve_commit_message.go
Original file line number Diff line number Diff line change
@@ -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"))
},
})
1 change: 1 addition & 0 deletions pkg/integration/tests/test_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var tests = []*components.IntegrationTest{
commit.History,
commit.HistoryComplex,
commit.NewBranch,
commit.PreserveCommitMessage,
commit.ResetAuthor,
commit.Revert,
commit.RevertMerge,
Expand Down
Loading