Show "hooks disabled" in title bar of commit message editor

It is shown either when committing with `w`, or when typing the skipHooks prefix
if there is one. This should hopefully make it clearer when the hooks are run
and when they are not.
This commit is contained in:
Stefan Haller 2025-04-07 19:53:16 +02:00
parent 2ee80d7150
commit b3bffbec4a
6 changed files with 40 additions and 11 deletions

View file

@ -6,7 +6,6 @@ import (
"strconv"
"strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
@ -43,6 +42,10 @@ type CommitMessageViewModel struct {
// invoked when pressing the switch-to-editor key binding
onSwitchToEditor func(string) error
// the following two fields are used for the display of the "hooks disabled" subtitle
forceSkipHooks bool
skipHooksPrefix string
// The message typed in before cycling through history
// We store this separately to 'preservedMessage' because 'preservedMessage'
// is specifically for committing staged files and we don't want this affected
@ -149,12 +152,16 @@ func (self *CommitMessageContext) SetPanelState(
initialMessage string,
onConfirm func(string, string) error,
onSwitchToEditor func(string) error,
forceSkipHooks bool,
skipHooksPrefix string,
) {
self.viewModel.selectedindex = index
self.viewModel.preserveMessage = preserveMessage
self.viewModel.initialMessage = initialMessage
self.viewModel.onConfirm = onConfirm
self.viewModel.onSwitchToEditor = onSwitchToEditor
self.viewModel.forceSkipHooks = forceSkipHooks
self.viewModel.skipHooksPrefix = skipHooksPrefix
self.GetView().Title = summaryTitle
self.c.Views().CommitDescription.Title = descriptionTitle
@ -167,16 +174,24 @@ func (self *CommitMessageContext) SetPanelState(
self.c.Views().CommitDescription.Visible = true
}
func (self *CommitMessageContext) RenderCommitLength() {
if self.c.UserConfig().Gui.CommitLength.Show {
self.c.Views().CommitMessage.Subtitle = getBufferLength(self.c.Views().CommitMessage)
} else {
self.c.Views().CommitMessage.Subtitle = ""
func (self *CommitMessageContext) RenderSubtitle() {
skipHookPrefix := self.viewModel.skipHooksPrefix
subject := self.c.Views().CommitMessage.TextArea.GetContent()
var subtitle string
if self.viewModel.forceSkipHooks || (skipHookPrefix != "" && strings.HasPrefix(subject, skipHookPrefix)) {
subtitle = self.c.Tr.CommitHooksDisabledSubTitle
}
if self.c.UserConfig().Gui.CommitLength.Show {
if subtitle != "" {
subtitle += "─"
}
subtitle += getBufferLength(subject)
}
self.c.Views().CommitMessage.Subtitle = subtitle
}
func getBufferLength(view *gocui.View) string {
return " " + strconv.Itoa(strings.Count(view.TextArea.GetContent(), "")-1) + " "
func getBufferLength(subject string) string {
return " " + strconv.Itoa(strings.Count(subject, "")-1) + " "
}
func (self *CommitMessageContext) SwitchToEditor(message string) error {

View file

@ -77,7 +77,7 @@ func (self *CommitMessageController) GetOnFocus() func(types.OnFocusOpts) {
func (self *CommitMessageController) GetOnFocusLost() func(types.OnFocusLostOpts) {
return func(types.OnFocusLostOpts) {
self.context().RenderCommitLength()
self.context().RenderSubtitle()
}
}

View file

@ -50,7 +50,7 @@ func (self *CommitsHelper) SetMessageAndDescriptionInView(message string) {
self.setCommitSummary(summary)
self.setCommitDescription(description)
self.c.Contexts().CommitMessage.RenderCommitLength()
self.c.Contexts().CommitMessage.RenderSubtitle()
}
func (self *CommitsHelper) JoinCommitMessageAndUnwrappedDescription() string {
@ -123,6 +123,14 @@ type OpenCommitMessagePanelOpts struct {
OnConfirm func(summary string, description string) error
OnSwitchToEditor func(string) error
InitialMessage string
// The following two fields are only for the display of the "(hooks
// disabled)" display in the commit message panel. They have no effect on
// the actual behavior; make sure what you are passing in matches that.
// Leave unassigned if the concept of skipping hooks doesn't make sense for
// what you are doing, e.g. when creating a tag.
ForceSkipHooks bool
SkipHooksPrefix string
}
func (self *CommitsHelper) OpenCommitMessagePanel(opts *OpenCommitMessagePanelOpts) {
@ -140,6 +148,8 @@ func (self *CommitsHelper) OpenCommitMessagePanel(opts *OpenCommitMessagePanelOp
opts.InitialMessage,
onConfirm,
opts.OnSwitchToEditor,
opts.ForceSkipHooks,
opts.SkipHooksPrefix,
)
self.UpdateCommitPanelView(opts.InitialMessage)

View file

@ -95,6 +95,8 @@ func (self *WorkingTreeHelper) HandleCommitPressWithMessage(initialMessage strin
OnSwitchToEditor: func(filepath string) error {
return self.switchFromCommitMessagePanelToEditor(filepath, forceSkipHooks)
},
ForceSkipHooks: forceSkipHooks,
SkipHooksPrefix: self.c.UserConfig().Git.SkipHookPrefix,
},
)

View file

@ -61,7 +61,7 @@ func (gui *Gui) handleEditorKeypress(textArea *gocui.TextArea, key gocui.Key, ch
func (gui *Gui) commitMessageEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool {
matched := gui.handleEditorKeypress(v.TextArea, key, ch, mod, false)
v.RenderTextArea()
gui.c.Contexts().CommitMessage.RenderCommitLength()
gui.c.Contexts().CommitMessage.RenderSubtitle()
return matched
}

View file

@ -315,6 +315,7 @@ type TranslationSet struct {
CommitDescriptionTitle string
CommitDescriptionSubTitle string
CommitDescriptionFooter string
CommitHooksDisabledSubTitle string
LocalBranchesTitle string
SearchTitle string
TagsTitle string
@ -1366,6 +1367,7 @@ func EnglishTranslationSet() *TranslationSet {
CommitDescriptionTitle: "Commit description",
CommitDescriptionSubTitle: "Press {{.togglePanelKeyBinding}} to toggle focus, {{.commitMenuKeybinding}} to open menu",
CommitDescriptionFooter: "Press {{.confirmInEditorKeybinding}} to commit",
CommitHooksDisabledSubTitle: "(hooks disabled)",
LocalBranchesTitle: "Local branches",
SearchTitle: "Search",
TagsTitle: "Tags",