1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-02 09:21:40 +02:00

Extract a WithEnsureCommitableFiles function

This encapsulates the logic to make sure we have something to commit; which is
to
- auto-stage all files if no files are staged and the SkipNoStagedFilesWarning
config is on
- otherwise, prompt the user whether they want to stage all files
- error out if we don't have any files at all

Of these, the first one was only done when committing with the built-in commit
message panel; there's no reason why it shouldn't also be done when committing
with the editor, or when amending, and now it is.
This commit is contained in:
Stefan Haller 2023-08-22 14:54:08 +02:00
parent a63d5891e2
commit f561007fb7
2 changed files with 42 additions and 48 deletions

View File

@ -615,19 +615,13 @@ func (self *FilesController) refresh() error {
} }
func (self *FilesController) handleAmendCommitPress() error { func (self *FilesController) handleAmendCommitPress() error {
if len(self.c.Model().Files) == 0 { return self.c.Helpers().WorkingTree.WithEnsureCommitableFiles(func() error {
return self.c.ErrorMsg(self.c.Tr.NoFilesStagedTitle) if len(self.c.Model().Commits) == 0 {
} return self.c.ErrorMsg(self.c.Tr.NoCommitToAmend)
}
if !self.c.Helpers().WorkingTree.AnyStagedFiles() { return self.c.Helpers().AmendHelper.AmendHead()
return self.c.Helpers().WorkingTree.PromptToStageAllAndRetry(self.handleAmendCommitPress) })
}
if len(self.c.Model().Commits) == 0 {
return self.c.ErrorMsg(self.c.Tr.NoCommitToAmend)
}
return self.c.Helpers().AmendHelper.AmendHead()
} }
func (self *FilesController) handleStatusFilterPressed() error { func (self *FilesController) handleStatusFilterPressed() error {

View File

@ -84,29 +84,19 @@ func (self *WorkingTreeHelper) OpenMergeTool() error {
} }
func (self *WorkingTreeHelper) HandleCommitPressWithMessage(initialMessage string) error { func (self *WorkingTreeHelper) HandleCommitPressWithMessage(initialMessage string) error {
if err := self.prepareFilesForCommit(); err != nil { return self.WithEnsureCommitableFiles(func() error {
return self.c.Error(err) return self.commitsHelper.OpenCommitMessagePanel(
} &OpenCommitMessagePanelOpts{
CommitIndex: context.NoCommitIndex,
if len(self.c.Model().Files) == 0 { InitialMessage: initialMessage,
return self.c.ErrorMsg(self.c.Tr.NoFilesStagedTitle) SummaryTitle: self.c.Tr.CommitSummaryTitle,
} DescriptionTitle: self.c.Tr.CommitDescriptionTitle,
PreserveMessage: true,
if !self.AnyStagedFiles() { OnConfirm: self.handleCommit,
return self.PromptToStageAllAndRetry(self.HandleCommitPress) OnSwitchToEditor: self.switchFromCommitMessagePanelToEditor,
} },
)
return self.commitsHelper.OpenCommitMessagePanel( })
&OpenCommitMessagePanelOpts{
CommitIndex: context.NoCommitIndex,
InitialMessage: initialMessage,
SummaryTitle: self.c.Tr.CommitSummaryTitle,
DescriptionTitle: self.c.Tr.CommitDescriptionTitle,
PreserveMessage: true,
OnConfirm: self.handleCommit,
OnSwitchToEditor: self.switchFromCommitMessagePanelToEditor,
},
)
} }
func (self *WorkingTreeHelper) handleCommit(summary string, description string) error { func (self *WorkingTreeHelper) handleCommit(summary string, description string) error {
@ -136,18 +126,12 @@ func (self *WorkingTreeHelper) switchFromCommitMessagePanelToEditor(filepath str
// HandleCommitEditorPress - handle when the user wants to commit changes via // HandleCommitEditorPress - handle when the user wants to commit changes via
// their editor rather than via the popup panel // their editor rather than via the popup panel
func (self *WorkingTreeHelper) HandleCommitEditorPress() error { func (self *WorkingTreeHelper) HandleCommitEditorPress() error {
if len(self.c.Model().Files) == 0 { return self.WithEnsureCommitableFiles(func() error {
return self.c.ErrorMsg(self.c.Tr.NoFilesStagedTitle) self.c.LogAction(self.c.Tr.Actions.Commit)
} return self.c.RunSubprocessAndRefresh(
self.c.Git().Commit.CommitEditorCmdObj(),
if !self.AnyStagedFiles() { )
return self.PromptToStageAllAndRetry(self.HandleCommitEditorPress) })
}
self.c.LogAction(self.c.Tr.Actions.Commit)
return self.c.RunSubprocessAndRefresh(
self.c.Git().Commit.CommitEditorCmdObj(),
)
} }
func (self *WorkingTreeHelper) HandleWIPCommitPress() error { func (self *WorkingTreeHelper) HandleWIPCommitPress() error {
@ -179,7 +163,23 @@ func (self *WorkingTreeHelper) HandleCommitPress() error {
return self.HandleCommitPressWithMessage(message) return self.HandleCommitPressWithMessage(message)
} }
func (self *WorkingTreeHelper) PromptToStageAllAndRetry(retry func() error) error { func (self *WorkingTreeHelper) WithEnsureCommitableFiles(handler func() error) error {
if err := self.prepareFilesForCommit(); err != nil {
return self.c.Error(err)
}
if len(self.c.Model().Files) == 0 {
return self.c.ErrorMsg(self.c.Tr.NoFilesStagedTitle)
}
if !self.AnyStagedFiles() {
return self.promptToStageAllAndRetry(handler)
}
return handler()
}
func (self *WorkingTreeHelper) promptToStageAllAndRetry(retry func() error) error {
return self.c.Confirm(types.ConfirmOpts{ return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.NoFilesStagedTitle, Title: self.c.Tr.NoFilesStagedTitle,
Prompt: self.c.Tr.NoFilesStagedPrompt, Prompt: self.c.Tr.NoFilesStagedPrompt,