Skip to content

Commit

Permalink
Check for staged files for "Amend commit" and "Create fixup commit" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhaller authored Aug 29, 2023
2 parents a63d589 + 1ba8d30 commit 2b26b38
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 61 deletions.
18 changes: 6 additions & 12 deletions pkg/gui/controllers/files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,19 +615,13 @@ func (self *FilesController) refresh() error {
}

func (self *FilesController) handleAmendCommitPress() error {
if len(self.c.Model().Files) == 0 {
return self.c.ErrorMsg(self.c.Tr.NoFilesStagedTitle)
}

if !self.c.Helpers().WorkingTree.AnyStagedFiles() {
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().WorkingTree.WithEnsureCommitableFiles(func() error {
if len(self.c.Model().Commits) == 0 {
return self.c.ErrorMsg(self.c.Tr.NoCommitToAmend)
}

return self.c.Helpers().AmendHelper.AmendHead()
return self.c.Helpers().AmendHelper.AmendHead()
})
}

func (self *FilesController) handleStatusFilterPressed() error {
Expand Down
72 changes: 36 additions & 36 deletions pkg/gui/controllers/helpers/working_tree_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,19 @@ func (self *WorkingTreeHelper) OpenMergeTool() error {
}

func (self *WorkingTreeHelper) HandleCommitPressWithMessage(initialMessage string) 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(self.HandleCommitPress)
}

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,
},
)
return self.WithEnsureCommitableFiles(func() error {
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 {
Expand Down Expand Up @@ -136,18 +126,12 @@ func (self *WorkingTreeHelper) switchFromCommitMessagePanelToEditor(filepath str
// HandleCommitEditorPress - handle when the user wants to commit changes via
// their editor rather than via the popup panel
func (self *WorkingTreeHelper) HandleCommitEditorPress() error {
if len(self.c.Model().Files) == 0 {
return self.c.ErrorMsg(self.c.Tr.NoFilesStagedTitle)
}

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(),
)
return self.WithEnsureCommitableFiles(func() error {
self.c.LogAction(self.c.Tr.Actions.Commit)
return self.c.RunSubprocessAndRefresh(
self.c.Git().Commit.CommitEditorCmdObj(),
)
})
}

func (self *WorkingTreeHelper) HandleWIPCommitPress() error {
Expand Down Expand Up @@ -179,7 +163,23 @@ func (self *WorkingTreeHelper) HandleCommitPress() error {
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{
Title: self.c.Tr.NoFilesStagedTitle,
Prompt: self.c.Tr.NoFilesStagedPrompt,
Expand Down
32 changes: 19 additions & 13 deletions pkg/gui/controllers/local_commits_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,12 @@ func (self *LocalCommitsController) moveUp(commit *models.Commit) error {

func (self *LocalCommitsController) amendTo(commit *models.Commit) error {
if self.isHeadCommit() {
if err := self.c.Helpers().AmendHelper.AmendHead(); err != nil {
return err
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
return self.c.Helpers().WorkingTree.WithEnsureCommitableFiles(func() error {
if err := self.c.Helpers().AmendHelper.AmendHead(); err != nil {
return err
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
})
}

if self.c.Git().Status.WorkingTreeState() != enums.REBASE_MODE_NONE {
Expand All @@ -582,10 +584,12 @@ func (self *LocalCommitsController) amendTo(commit *models.Commit) error {
Title: self.c.Tr.AmendCommitTitle,
Prompt: self.c.Tr.AmendCommitPrompt,
HandleConfirm: func() error {
return self.c.WithWaitingStatus(self.c.Tr.AmendingStatus, func(gocui.Task) error {
self.c.LogAction(self.c.Tr.Actions.AmendCommit)
err := self.c.Git().Rebase.AmendTo(self.c.Model().Commits, self.context().GetView().SelectedLineIdx())
return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err)
return self.c.Helpers().WorkingTree.WithEnsureCommitableFiles(func() error {
return self.c.WithWaitingStatus(self.c.Tr.AmendingStatus, func(gocui.Task) error {
self.c.LogAction(self.c.Tr.Actions.AmendCommit)
err := self.c.Git().Rebase.AmendTo(self.c.Model().Commits, self.context().GetView().SelectedLineIdx())
return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err)
})
})
},
})
Expand Down Expand Up @@ -709,12 +713,14 @@ func (self *LocalCommitsController) createFixupCommit(commit *models.Commit) err
Title: self.c.Tr.CreateFixupCommit,
Prompt: prompt,
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.CreateFixupCommit)
if err := self.c.Git().Commit.CreateFixupCommit(commit.Sha); err != nil {
return self.c.Error(err)
}
return self.c.Helpers().WorkingTree.WithEnsureCommitableFiles(func() error {
self.c.LogAction(self.c.Tr.Actions.CreateFixupCommit)
if err := self.c.Git().Commit.CreateFixupCommit(commit.Sha); err != nil {
return self.c.Error(err)
}

return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
})
},
})
}
Expand Down

0 comments on commit 2b26b38

Please sign in to comment.