Skip to content

Commit

Permalink
Don't preserve commit message when it's unchanged from initial message
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stefanhaller committed Dec 23, 2024
1 parent ec92d92 commit 3a30211
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
9 changes: 9 additions & 0 deletions pkg/gui/context/commit_message_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions pkg/gui/controllers/helpers/commits_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (self *CommitsHelper) OpenCommitMessagePanel(opts *OpenCommitMessagePanelOp
opts.SummaryTitle,
opts.DescriptionTitle,
opts.PreserveMessage,
opts.InitialMessage,
onConfirm,
opts.OnSwitchToEditor,
)
Expand Down Expand Up @@ -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("")
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/integration/tests/commit/commit_with_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down

0 comments on commit 3a30211

Please sign in to comment.