Skip to content

Commit 9704072

Browse files
committed
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.
1 parent f76ee92 commit 9704072

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

pkg/gui/context/commit_message_context.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ type CommitMessageViewModel struct {
3030
// if true, then upon escaping from the commit message panel, we will preserve
3131
// the message so that it's still shown next time we open the panel
3232
preserveMessage bool
33+
// we remember the initial message so that we can tell whether we should preserve
34+
// the message; if it's still identical to the initial message, we don't
35+
initialMessage string
3336
// the full preserved message (combined summary and description)
3437
preservedMessage string
3538
// invoked when pressing enter in the commit message panel
@@ -84,6 +87,10 @@ func (self *CommitMessageContext) SetPreservedMessage(message string) {
8487
self.viewModel.preservedMessage = message
8588
}
8689

90+
func (self *CommitMessageContext) GetInitialMessage() string {
91+
return strings.TrimSpace(self.viewModel.initialMessage)
92+
}
93+
8794
func (self *CommitMessageContext) GetHistoryMessage() string {
8895
return self.viewModel.historyMessage
8996
}
@@ -101,11 +108,13 @@ func (self *CommitMessageContext) SetPanelState(
101108
summaryTitle string,
102109
descriptionTitle string,
103110
preserveMessage bool,
111+
initialMessage string,
104112
onConfirm func(string, string) error,
105113
onSwitchToEditor func(string) error,
106114
) {
107115
self.viewModel.selectedindex = index
108116
self.viewModel.preserveMessage = preserveMessage
117+
self.viewModel.initialMessage = initialMessage
109118
self.viewModel.onConfirm = onConfirm
110119
self.viewModel.onSwitchToEditor = onSwitchToEditor
111120
self.GetView().Title = summaryTitle

pkg/gui/controllers/helpers/commits_helper.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func (self *CommitsHelper) OpenCommitMessagePanel(opts *OpenCommitMessagePanelOp
143143
opts.SummaryTitle,
144144
opts.DescriptionTitle,
145145
opts.PreserveMessage,
146+
opts.InitialMessage,
146147
onConfirm,
147148
opts.OnSwitchToEditor,
148149
)
@@ -177,8 +178,9 @@ func (self *CommitsHelper) HandleCommitConfirm() error {
177178
func (self *CommitsHelper) CloseCommitMessagePanel() {
178179
if self.c.Contexts().CommitMessage.GetPreserveMessage() {
179180
message := self.JoinCommitMessageAndUnwrappedDescription()
180-
181-
self.c.Contexts().CommitMessage.SetPreservedMessage(message)
181+
if message != self.c.Contexts().CommitMessage.GetInitialMessage() {
182+
self.c.Contexts().CommitMessage.SetPreservedMessage(message)
183+
}
182184
} else {
183185
self.SetMessageAndDescriptionInView("")
184186
}

pkg/integration/tests/commit/commit_with_prefix.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@ var CommitWithPrefix = NewIntegrationTest(NewIntegrationTestArgs{
4141

4242
t.ExpectPopup().CommitMessagePanel().
4343
Title(Equals("Commit summary")).
44-
/* EXPECTED:
4544
InitialText(Equals("[TEST-001]: ")).
4645
Type("my commit message").
47-
ACTUAL: */
48-
InitialText(Equals("[TEST-001]:")).
49-
Type(" my commit message").
5046
Cancel()
5147

5248
t.Views().Files().

0 commit comments

Comments
 (0)