Skip to content

Commit

Permalink
Preserve the commit message correctly even if the description has bla…
Browse files Browse the repository at this point in the history
…nk 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.
  • Loading branch information
stefanhaller committed Jan 9, 2024
1 parent 3ebba5f commit cd50c79
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 15 deletions.
9 changes: 2 additions & 7 deletions pkg/gui/controllers/helpers/commits_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 0 additions & 8 deletions pkg/integration/tests/commit/preserve_commit_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
},
})

0 comments on commit cd50c79

Please sign in to comment.