summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Haller <stefan@haller-berlin.de>2023-12-19 16:47:55 +0100
committerStefan Haller <stefan@haller-berlin.de>2024-01-09 14:31:53 +0100
commitcd50c79ae49b1fb710ce2d4f934f2132d762d3c0 (patch)
tree04edd9dfe43e0de0e23eeda58d7ec3a8c6d67d15
parent3ebba5f32cdd1549f032a68592928b23bbddf3f8 (diff)
Preserve the commit message correctly even if the description has blank lines
There are two possible fixes for this bug, and they differ in behavior when rewording a commit. The one I chose here always splits at the first line feed, which means that for an improperly formatted commit message such as this one: This is a very long multi-line subject, which you shouldn't really use in git. And this is the body (we call it "description" in lazygit). we split after the first line instead of after the first paragraph. This is arguably not what the original author meant, but splitting after the first paragraph doesn't really work well in lazygit, because we would try to put both lines into the one-line subject field of the message panel, and you'd only see the second and not even know that there are more. The other potential fix would have been to join subject and description with two line feeds instead of one in JoinCommitMessageAndDescription; this would have fixed our bug in the same way, but would result in splitting the above message after the second line instead of the first. I think that's worse, so I decided for the first fix. While we're at it, simplify the code a little bit; strings.Cut is documented to return (s, "") when the separator is not found, so there's no need to do this on our side. We do have to trim spaces on the description now, to support the regular reword case where subject and body are separated by a blank line.
-rw-r--r--pkg/gui/controllers/helpers/commits_helper.go9
-rw-r--r--pkg/integration/tests/commit/preserve_commit_message.go8
2 files changed, 2 insertions, 15 deletions
diff --git a/pkg/gui/controllers/helpers/commits_helper.go b/pkg/gui/controllers/helpers/commits_helper.go
index 5d388c319..8691518cd 100644
--- a/pkg/gui/controllers/helpers/commits_helper.go
+++ b/pkg/gui/controllers/helpers/commits_helper.go
@@ -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")
+ return msg, strings.TrimSpace(description)
}
func (self *CommitsHelper) SetMessageAndDescriptionInView(message string) {
diff --git a/pkg/integration/tests/commit/preserve_commit_message.go b/pkg/integration/tests/commit/preserve_commit_message.go
index 5a580e854..e9297ab76 100644
--- a/pkg/integration/tests/commit/preserve_commit_message.go
+++ b/pkg/integration/tests/commit/preserve_commit_message.go
@@ -32,17 +32,9 @@ var PreserveCommitMessage = NewIntegrationTest(NewIntegrationTestArgs{
IsFocused().
Press(keys.Files.CommitChanges)
- /* EXPECTED:
t.ExpectPopup().CommitMessagePanel().
Content(Equals("my commit message")).
SwitchToDescription().
Content(Equals("first paragraph\n\nsecond paragraph"))
-
- ACTUAL:
- */
- t.ExpectPopup().CommitMessagePanel().
- Content(Equals("my commit message\nfirst paragraph")).
- SwitchToDescription().
- Content(Equals("second paragraph"))
},
})