diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go index 38f50bdda..75256878f 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -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 } diff --git a/pkg/commands/git_commands/commit_test.go b/pkg/commands/git_commands/commit_test.go index 08ee7e5cc..96a46ecf4 100644 --- a/pkg/commands/git_commands/commit_test.go +++ b/pkg/commands/git_commands/commit_test.go @@ -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) + }) + } +}