From beedc2553d7b17ca8ac2827965980a2a6bd9fafb Mon Sep 17 00:00:00 2001 From: Jakob Kogler Date: Sun, 5 Dec 2021 22:33:07 +0100 Subject: [PATCH] remember the message if commit fails In case a commit fails, e.g. because a pre-commit hook returns an error, lazygit will now remember the commit message and will suggest it during the next commit (e.g. after fixing the error of the pre-commit hook). --- pkg/gui/commit_message_panel.go | 2 ++ pkg/gui/files_panel.go | 28 +++++++++++++++++----------- pkg/gui/gui.go | 3 +++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/pkg/gui/commit_message_panel.go b/pkg/gui/commit_message_panel.go index 932c1608a..2e3d2edb7 100644 --- a/pkg/gui/commit_message_panel.go +++ b/pkg/gui/commit_message_panel.go @@ -10,6 +10,7 @@ import ( func (gui *Gui) handleCommitConfirm() error { message := strings.TrimSpace(gui.Views.CommitMessage.TextArea.GetContent()) + gui.State.messageFailedCommit = message if message == "" { return gui.createErrorPanel(gui.Tr.CommitWithoutMessageErr) } @@ -29,6 +30,7 @@ func (gui *Gui) handleCommitConfirm() error { _ = gui.returnFromContext() return gui.withGpgHandling(cmdObj, gui.Tr.CommittingStatus, func() error { gui.Views.CommitMessage.ClearTextArea() + gui.State.messageFailedCommit = "" return nil }) } diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index 094060032..ea897f085 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -386,18 +386,24 @@ func (gui *Gui) handleCommitPress() error { return gui.promptToStageAllAndRetry(gui.handleCommitPress) } - commitPrefixConfig := gui.commitPrefixConfigForRepo() - if commitPrefixConfig != nil { - prefixPattern := commitPrefixConfig.Pattern - prefixReplace := commitPrefixConfig.Replace - rgx, err := regexp.Compile(prefixPattern) - if err != nil { - return gui.createErrorPanel(fmt.Sprintf("%s: %s", gui.Tr.LcCommitPrefixPatternError, err.Error())) - } - prefix := rgx.ReplaceAllString(gui.getCheckedOutBranch().Name, prefixReplace) + if len(gui.State.messageFailedCommit) > 0 { gui.Views.CommitMessage.ClearTextArea() - gui.Views.CommitMessage.TextArea.TypeString(prefix) - gui.render() + gui.Views.CommitMessage.TextArea.TypeString(gui.State.messageFailedCommit) + gui.Views.CommitMessage.RenderTextArea() + } else { + commitPrefixConfig := gui.commitPrefixConfigForRepo() + if commitPrefixConfig != nil { + prefixPattern := commitPrefixConfig.Pattern + prefixReplace := commitPrefixConfig.Replace + rgx, err := regexp.Compile(prefixPattern) + if err != nil { + return gui.createErrorPanel(fmt.Sprintf("%s: %s", gui.Tr.LcCommitPrefixPatternError, err.Error())) + } + prefix := rgx.ReplaceAllString(gui.getCheckedOutBranch().Name, prefixReplace) + gui.Views.CommitMessage.ClearTextArea() + gui.Views.CommitMessage.TextArea.TypeString(prefix) + gui.Views.CommitMessage.RenderTextArea() + } } gui.g.Update(func(g *gocui.Gui) error { diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 9b4279f29..08960ff16 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -346,6 +346,9 @@ type guiState struct { // for displaying suggestions while typing in a file name FilesTrie *patricia.Trie + + // this is the message of the last failed commit attempt + messageFailedCommit string } // reuseState determines if we pull the repo state from our repo state map or