summaryrefslogtreecommitdiffstats
path: root/pkg/commands
diff options
context:
space:
mode:
authorMoritz Haase <Moritz.Haase@bmw.de>2022-03-21 13:29:34 +0100
committerJesse Duffield <jessedduffield@gmail.com>2022-03-23 08:19:17 +1100
commit8fb47fb7d6a0bda59652ded5cb5a3731421ad40d (patch)
tree8fa743c18c314047dee36084ffff68bc02de953e /pkg/commands
parentac687a5a2a52aa4aa60d03bd8bc42f611a2ac931 (diff)
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.
Diffstat (limited to 'pkg/commands')
-rw-r--r--pkg/commands/git_commands/commit.go2
-rw-r--r--pkg/commands/git_commands/commit_test.go46
2 files changed, 47 insertions, 1 deletions
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)
+ })
+ }
+}