From 4ab36461cb975632a4f836f67447e65a92ddbea9 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 19 Apr 2026 12:03:41 +0200 Subject: [PATCH] Tolerate trailing newlines in AddCoAuthorToDescription Callers currently hand this function a trimmed description, so the output is always clean. An upcoming change to the commit-panel getters will stop trimming at the callsite (so that whitespace typed by the user round-trips through the preservation file exactly), at which point the description can end with one or more newlines. Without this change, a user who presses Enter after their description body and then invokes "Add co-author" would end up with two blank lines between the body and the trailer instead of the expected one. --- pkg/commands/git_commands/commit.go | 1 + pkg/commands/git_commands/commit_test.go | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go index 40d2b7319..6abf272b3 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -61,6 +61,7 @@ func AddCoAuthorToMessage(message string, author string) string { } func AddCoAuthorToDescription(description string, author string) string { + description = strings.TrimRight(description, "\n") if description != "" { lines := strings.Split(description, "\n") if strings.HasPrefix(lines[len(lines)-1], "Co-authored-by:") { diff --git a/pkg/commands/git_commands/commit_test.go b/pkg/commands/git_commands/commit_test.go index 6ea914c64..25966c06f 100644 --- a/pkg/commands/git_commands/commit_test.go +++ b/pkg/commands/git_commands/commit_test.go @@ -483,6 +483,11 @@ func TestAddCoAuthorToDescription(t *testing.T) { description: "Body\n\nCo-authored-by: Jane Smith ", expectedResult: "Body\n\nCo-authored-by: Jane Smith \nCo-authored-by: John Doe ", }, + { + name: "Description with trailing newlines", + description: "Body\n\n", + expectedResult: "Body\n\nCo-authored-by: John Doe ", + }, } for _, s := range scenarios { t.Run(s.name, func(t *testing.T) {