diff --git a/pkg/gui/controllers/commit_message_controller.go b/pkg/gui/controllers/commit_message_controller.go index 098f42d30..dde9b5647 100644 --- a/pkg/gui/controllers/commit_message_controller.go +++ b/pkg/gui/controllers/commit_message_controller.go @@ -6,7 +6,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/gocui" "github.com/jesseduffield/lazygit/pkg/gui/context" - "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/samber/lo" ) @@ -166,9 +165,7 @@ func (self *CommitMessageController) setCommitMessageAtIndex(index int) (bool, e } return false, errors.New(self.c.Tr.CommitWithoutMessageErr) } - if self.c.UserConfig().Git.Commit.AutoWrapCommitMessage { - commitMessage = helpers.TryRemoveHardLineBreaks(commitMessage, self.c.UserConfig().Git.Commit.AutoWrapWidth) - } + commitMessage = self.c.Helpers().Commits.TryRemoveHardLineBreaksIfAutoWrapping(commitMessage) self.c.Helpers().Commits.SetMessageAndDescriptionInView(commitMessage) return true, nil } diff --git a/pkg/gui/controllers/helpers/commits_helper.go b/pkg/gui/controllers/helpers/commits_helper.go index 3d58e6aec..b995e002d 100644 --- a/pkg/gui/controllers/helpers/commits_helper.go +++ b/pkg/gui/controllers/helpers/commits_helper.go @@ -89,7 +89,18 @@ func (self *CommitsHelper) JoinCommitMessageAndUnwrappedDescription() string { return self.getCommitSummary() + "\n" + self.getUnwrappedCommitDescription() } -func TryRemoveHardLineBreaks(message string, autoWrapWidth int) string { +// TryRemoveHardLineBreaksIfAutoWrapping replaces the hard line breaks of a +// message with spaces wherever auto-wrapping would break the line at the same +// place, so that the paragraphs reflow when the user edits them in the panel. +func (self *CommitsHelper) TryRemoveHardLineBreaksIfAutoWrapping(message string) string { + commitConfig := self.c.UserConfig().Git.Commit + if !commitConfig.AutoWrapCommitMessage { + return message + } + return tryRemoveHardLineBreaks(message, commitConfig.AutoWrapWidth) +} + +func tryRemoveHardLineBreaks(message string, autoWrapWidth int) string { lastHardLineStart := 0 result := message for i, b := range message { diff --git a/pkg/gui/controllers/helpers/commits_helper_test.go b/pkg/gui/controllers/helpers/commits_helper_test.go index 6197c3916..fee75b4ef 100644 --- a/pkg/gui/controllers/helpers/commits_helper_test.go +++ b/pkg/gui/controllers/helpers/commits_helper_test.go @@ -34,7 +34,7 @@ func TestTryRemoveHardLineBreaks(t *testing.T) { } for _, s := range scenarios { t.Run(s.name, func(t *testing.T) { - actualResult := TryRemoveHardLineBreaks(s.message, s.autoWrapWidth) + actualResult := tryRemoveHardLineBreaks(s.message, s.autoWrapWidth) assert.Equal(t, s.expectedResult, actualResult) }) } diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index 9b2ae40b4..a00589a76 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -884,9 +884,7 @@ func (self *LocalCommitsController) reword(commit *models.Commit) error { if err != nil { return err } - if self.c.UserConfig().Git.Commit.AutoWrapCommitMessage { - commitMessage = helpers.TryRemoveHardLineBreaks(commitMessage, self.c.UserConfig().Git.Commit.AutoWrapWidth) - } + commitMessage = self.c.Helpers().Commits.TryRemoveHardLineBreaksIfAutoWrapping(commitMessage) self.c.Helpers().Commits.OpenCommitMessagePanel( &helpers.OpenCommitMessagePanelOpts{ CommitIndex: commitIdx, @@ -1579,9 +1577,7 @@ func (self *LocalCommitsController) createAmendCommit(commit *models.Commit, inc if err != nil { return err } - if self.c.UserConfig().Git.Commit.AutoWrapCommitMessage { - commitMessage = helpers.TryRemoveHardLineBreaks(commitMessage, self.c.UserConfig().Git.Commit.AutoWrapWidth) - } + commitMessage = self.c.Helpers().Commits.TryRemoveHardLineBreaksIfAutoWrapping(commitMessage) originalSubject, _, _ := strings.Cut(commitMessage, "\n") self.c.Helpers().Commits.OpenCommitMessagePanel( &helpers.OpenCommitMessagePanelOpts{