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

pkg/commands: Don't duplicate line breaks when retrieving commit message

When using the "copy commit message to clipboard" action, the message will end
up in the clipboard with duplicate line breaks. The same issue also affects the
"Reword Commit" command. GetCommitMessage(), the function used to retrieve the
commit message first splits the output returned by git into separate lines -
without removing the line breaks. After removing the first line (which contains
the commit SHA), it joins the lines of the message itself back together - adding
a second set of line breaks along the way. Stop this from happening.

Fixes #1808.
This commit is contained in:
Moritz Haase 2022-03-21 13:29:34 +01:00 committed by Jesse Duffield
parent ac687a5a2a
commit 8fb47fb7d6
2 changed files with 47 additions and 1 deletions

View File

@ -70,7 +70,7 @@ func (self *CommitCommands) GetHeadCommitMessage() (string, error) {
func (self *CommitCommands) GetCommitMessage(commitSha string) (string, error) {
cmdStr := "git rev-list --format=%B --max-count=1 " + commitSha
messageWithHeader, err := self.cmd.New(cmdStr).DontLog().RunWithOutput()
message := strings.Join(strings.SplitAfter(messageWithHeader, "\n")[1:], "\n")
message := strings.Join(strings.SplitAfter(messageWithHeader, "\n")[1:], "")
return strings.TrimSpace(message), err
}

View File

@ -161,3 +161,49 @@ func TestCommitShowCmdObj(t *testing.T) {
})
}
}
func TestGetCommitMsg(t *testing.T) {
type scenario struct {
testName string
input string
expectedOutput string
}
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`,
},
{
"with line breaks",
`commit deadbeef
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
'git-rev parse' should be 'git rev-parse'`,
},
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildCommitCommands(commonDeps{
runner: oscommands.NewFakeRunner(t).Expect("git rev-list --format=%B --max-count=1 deadbeef", s.input, nil),
})
output, err := instance.GetCommitMessage("deadbeef")
assert.NoError(t, err)
assert.Equal(t, s.expectedOutput, output)
})
}
}