1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-05-17 22:32:58 +02:00

Don't preserve commit message when it's unchanged from initial message

Sometimes we populate the commit message panel with a pre-created commit
message. The two cases where this happens is:
- you type `w` to commit, in which case we put the skipHookPrefix in the subject
- you have a commitPrefix pattern, in which case we match it against the branch
  name and populate the subject with the replacement string if it matches

In either case, if you have a preserved commit message, we use that.

Now, when you use either of these and then cancel, we preserve that initial,
unchanged message and reuse it the next time you commit. This has two problems:
it strips spaces, which is a problem for the commitPrefix patterns, which often
end with a space. And also, when you change your config to experiment with
commitPrefix patterns, the change seemingly doesn't take effect, which can be
very confusing.

To fix both of these problems, only preserve the commit message when it is not
identical to the initial message.
This commit is contained in:
Stefan Haller 2024-12-12 11:08:03 +01:00
parent ec92d92bf4
commit 3a30211099
3 changed files with 13 additions and 6 deletions

View File

@ -30,6 +30,9 @@ type CommitMessageViewModel struct {
// if true, then upon escaping from the commit message panel, we will preserve
// the message so that it's still shown next time we open the panel
preserveMessage bool
// we remember the initial message so that we can tell whether we should preserve
// the message; if it's still identical to the initial message, we don't
initialMessage string
// the full preserved message (combined summary and description)
preservedMessage string
// invoked when pressing enter in the commit message panel
@ -84,6 +87,10 @@ func (self *CommitMessageContext) SetPreservedMessage(message string) {
self.viewModel.preservedMessage = message
}
func (self *CommitMessageContext) GetInitialMessage() string {
return strings.TrimSpace(self.viewModel.initialMessage)
}
func (self *CommitMessageContext) GetHistoryMessage() string {
return self.viewModel.historyMessage
}
@ -101,11 +108,13 @@ func (self *CommitMessageContext) SetPanelState(
summaryTitle string,
descriptionTitle string,
preserveMessage bool,
initialMessage string,
onConfirm func(string, string) error,
onSwitchToEditor func(string) error,
) {
self.viewModel.selectedindex = index
self.viewModel.preserveMessage = preserveMessage
self.viewModel.initialMessage = initialMessage
self.viewModel.onConfirm = onConfirm
self.viewModel.onSwitchToEditor = onSwitchToEditor
self.GetView().Title = summaryTitle

View File

@ -143,6 +143,7 @@ func (self *CommitsHelper) OpenCommitMessagePanel(opts *OpenCommitMessagePanelOp
opts.SummaryTitle,
opts.DescriptionTitle,
opts.PreserveMessage,
opts.InitialMessage,
onConfirm,
opts.OnSwitchToEditor,
)
@ -177,8 +178,9 @@ func (self *CommitsHelper) HandleCommitConfirm() error {
func (self *CommitsHelper) CloseCommitMessagePanel() {
if self.c.Contexts().CommitMessage.GetPreserveMessage() {
message := self.JoinCommitMessageAndUnwrappedDescription()
self.c.Contexts().CommitMessage.SetPreservedMessage(message)
if message != self.c.Contexts().CommitMessage.GetInitialMessage() {
self.c.Contexts().CommitMessage.SetPreservedMessage(message)
}
} else {
self.SetMessageAndDescriptionInView("")
}

View File

@ -41,12 +41,8 @@ var CommitWithPrefix = NewIntegrationTest(NewIntegrationTestArgs{
t.ExpectPopup().CommitMessagePanel().
Title(Equals("Commit summary")).
/* EXPECTED:
InitialText(Equals("[TEST-001]: ")).
Type("my commit message").
ACTUAL: */
InitialText(Equals("[TEST-001]:")).
Type(" my commit message").
Cancel()
t.Views().Files().