diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index fa2df887e..d071ab3fb 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -42,12 +42,12 @@ func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) [ outsideFilterModeBindings := []*types.Binding{ { Key: opts.GetKey(opts.Config.Commits.SquashDown), - Handler: self.squashDown, + Handler: self.checkSelected(self.squashDown), Description: self.c.Tr.LcSquashDown, }, { Key: opts.GetKey(opts.Config.Commits.MarkCommitAsFixup), - Handler: self.fixup, + Handler: self.checkSelected(self.fixup), Description: self.c.Tr.LcFixupCommit, }, { @@ -57,22 +57,22 @@ func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) [ }, { Key: opts.GetKey(opts.Config.Commits.RenameCommitWithEditor), - Handler: self.rewordEditor, + Handler: self.checkSelected(self.rewordEditor), Description: self.c.Tr.LcRenameCommitEditor, }, { Key: opts.GetKey(opts.Config.Universal.Remove), - Handler: self.drop, + Handler: self.checkSelected(self.drop), Description: self.c.Tr.LcDeleteCommit, }, { Key: opts.GetKey(opts.Config.Universal.Edit), - Handler: self.edit, + Handler: self.checkSelected(self.edit), Description: self.c.Tr.LcEditCommit, }, { Key: opts.GetKey(opts.Config.Commits.PickCommit), - Handler: self.pick, + Handler: self.checkSelected(self.pick), Description: self.c.Tr.LcPickCommit, }, { @@ -87,17 +87,17 @@ func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) [ }, { Key: opts.GetKey(opts.Config.Commits.MoveDownCommit), - Handler: self.handleCommitMoveDown, + Handler: self.checkSelected(self.handleCommitMoveDown), Description: self.c.Tr.LcMoveDownCommit, }, { Key: opts.GetKey(opts.Config.Commits.MoveUpCommit), - Handler: self.handleCommitMoveUp, + Handler: self.checkSelected(self.handleCommitMoveUp), Description: self.c.Tr.LcMoveUpCommit, }, { Key: opts.GetKey(opts.Config.Commits.AmendToCommit), - Handler: self.handleCommitAmendTo, + Handler: self.checkSelected(self.handleCommitAmendTo), Description: self.c.Tr.LcAmendToCommit, }, { @@ -192,12 +192,12 @@ func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) [ return bindings } -func (self *LocalCommitsController) squashDown() error { +func (self *LocalCommitsController) squashDown(commit *models.Commit) error { if len(self.model.Commits) <= 1 { return self.c.ErrorMsg(self.c.Tr.YouNoCommitsToSquash) } - applied, err := self.handleMidRebaseCommand("squash") + applied, err := self.handleMidRebaseCommand("squash", commit) if err != nil { return err } @@ -217,12 +217,12 @@ func (self *LocalCommitsController) squashDown() error { }) } -func (self *LocalCommitsController) fixup() error { +func (self *LocalCommitsController) fixup(commit *models.Commit) error { if len(self.model.Commits) <= 1 { return self.c.ErrorMsg(self.c.Tr.YouNoCommitsToSquash) } - applied, err := self.handleMidRebaseCommand("fixup") + applied, err := self.handleMidRebaseCommand("fixup", commit) if err != nil { return err } @@ -243,7 +243,7 @@ func (self *LocalCommitsController) fixup() error { } func (self *LocalCommitsController) reword(commit *models.Commit) error { - applied, err := self.handleMidRebaseCommand("reword") + applied, err := self.handleMidRebaseCommand("reword", commit) if err != nil { return err } @@ -271,8 +271,8 @@ func (self *LocalCommitsController) reword(commit *models.Commit) error { }) } -func (self *LocalCommitsController) rewordEditor() error { - applied, err := self.handleMidRebaseCommand("reword") +func (self *LocalCommitsController) rewordEditor(commit *models.Commit) error { + applied, err := self.handleMidRebaseCommand("reword", commit) if err != nil { return err } @@ -294,8 +294,8 @@ func (self *LocalCommitsController) rewordEditor() error { return nil } -func (self *LocalCommitsController) drop() error { - applied, err := self.handleMidRebaseCommand("drop") +func (self *LocalCommitsController) drop(commit *models.Commit) error { + applied, err := self.handleMidRebaseCommand("drop", commit) if err != nil { return err } @@ -315,8 +315,8 @@ func (self *LocalCommitsController) drop() error { }) } -func (self *LocalCommitsController) edit() error { - applied, err := self.handleMidRebaseCommand("edit") +func (self *LocalCommitsController) edit(commit *models.Commit) error { + applied, err := self.handleMidRebaseCommand("edit", commit) if err != nil { return err } @@ -330,8 +330,8 @@ func (self *LocalCommitsController) edit() error { }) } -func (self *LocalCommitsController) pick() error { - applied, err := self.handleMidRebaseCommand("pick") +func (self *LocalCommitsController) pick(commit *models.Commit) error { + applied, err := self.handleMidRebaseCommand("pick", commit) if err != nil { return err } @@ -352,9 +352,8 @@ func (self *LocalCommitsController) interactiveRebase(action string) error { // handleMidRebaseCommand sees if the selected commit is in fact a rebasing // commit meaning you are trying to edit the todo file rather than actually // begin a rebase. It then updates the todo file with that action -func (self *LocalCommitsController) handleMidRebaseCommand(action string) (bool, error) { - selectedCommit := self.context().GetSelected() - if selectedCommit.Status != "rebasing" { +func (self *LocalCommitsController) handleMidRebaseCommand(action string, commit *models.Commit) (bool, error) { + if commit.Status != "rebasing" { return false, nil } @@ -368,7 +367,7 @@ func (self *LocalCommitsController) handleMidRebaseCommand(action string) (bool, self.c.LogAction("Update rebase TODO") self.c.LogCommand( - fmt.Sprintf("Updating rebase action of commit %s to '%s'", selectedCommit.ShortSha(), action), + fmt.Sprintf("Updating rebase action of commit %s to '%s'", commit.ShortSha(), action), false, ) @@ -383,11 +382,10 @@ func (self *LocalCommitsController) handleMidRebaseCommand(action string) (bool, }) } -func (self *LocalCommitsController) handleCommitMoveDown() error { +func (self *LocalCommitsController) handleCommitMoveDown(commit *models.Commit) error { index := self.context().GetSelectedLineIdx() commits := self.model.Commits - selectedCommit := self.model.Commits[index] - if selectedCommit.Status == "rebasing" { + if commit.Status == "rebasing" { if commits[index+1].Status != "rebasing" { return nil } @@ -395,7 +393,7 @@ func (self *LocalCommitsController) handleCommitMoveDown() error { // logging directly here because MoveTodoDown doesn't have enough information // to provide a useful log self.c.LogAction(self.c.Tr.Actions.MoveCommitDown) - self.c.LogCommand(fmt.Sprintf("Moving commit %s down", selectedCommit.ShortSha()), false) + self.c.LogCommand(fmt.Sprintf("Moving commit %s down", commit.ShortSha()), false) if err := self.git.Rebase.MoveTodoDown(index); err != nil { return self.c.Error(err) @@ -416,19 +414,18 @@ func (self *LocalCommitsController) handleCommitMoveDown() error { }) } -func (self *LocalCommitsController) handleCommitMoveUp() error { +func (self *LocalCommitsController) handleCommitMoveUp(commit *models.Commit) error { index := self.context().GetSelectedLineIdx() if index == 0 { return nil } - selectedCommit := self.model.Commits[index] - if selectedCommit.Status == "rebasing" { + if commit.Status == "rebasing" { // logging directly here because MoveTodoDown doesn't have enough information // to provide a useful log self.c.LogAction(self.c.Tr.Actions.MoveCommitUp) self.c.LogCommand( - fmt.Sprintf("Moving commit %s up", selectedCommit.ShortSha()), + fmt.Sprintf("Moving commit %s up", commit.ShortSha()), false, ) @@ -451,14 +448,14 @@ func (self *LocalCommitsController) handleCommitMoveUp() error { }) } -func (self *LocalCommitsController) handleCommitAmendTo() error { +func (self *LocalCommitsController) handleCommitAmendTo(commit *models.Commit) error { return self.c.Ask(types.AskOpts{ Title: self.c.Tr.AmendCommitTitle, Prompt: self.c.Tr.AmendCommitPrompt, HandleConfirm: func() error { return self.c.WithWaitingStatus(self.c.Tr.AmendingStatus, func() error { self.c.LogAction(self.c.Tr.Actions.AmendCommit) - err := self.git.Rebase.AmendTo(self.context().GetSelected().Sha) + err := self.git.Rebase.AmendTo(commit.Sha) return self.helpers.MergeAndRebase.CheckMergeOrRebase(err) }) }, diff --git a/pkg/gui/controllers/reflog_controller.go b/pkg/gui/controllers/reflog_controller.go index c1251936e..549348040 100644 --- a/pkg/gui/controllers/reflog_controller.go +++ b/pkg/gui/controllers/reflog_controller.go @@ -30,28 +30,28 @@ func (self *ReflogController) GetKeybindings(opts types.KeybindingsOpts) []*type bindings := []*types.Binding{ { Key: opts.GetKey(opts.Config.Universal.GoInto), - Handler: self.checkSelected(self.handleViewReflogCommitFiles), + Handler: self.checkSelected(self.enter), Description: self.c.Tr.LcViewCommitFiles, }, { Key: opts.GetKey(opts.Config.Universal.Select), - Handler: self.checkSelected(self.CheckoutReflogCommit), + Handler: self.checkSelected(self.checkout), Description: self.c.Tr.LcCheckoutCommit, }, { Key: opts.GetKey(opts.Config.Commits.ViewResetOptions), - Handler: self.checkSelected(self.handleCreateReflogResetMenu), + Handler: self.checkSelected(self.openResetMenu), Description: self.c.Tr.LcViewResetOptions, OpensMenu: true, }, { Key: opts.GetKey(opts.Config.Commits.CherryPickCopy), - Handler: opts.Guards.OutsideFilterMode(self.checkSelected(self.handleCopyReflogCommit)), + Handler: opts.Guards.OutsideFilterMode(self.checkSelected(self.copy)), Description: self.c.Tr.LcCherryPickCopy, }, { Key: opts.GetKey(opts.Config.Commits.CherryPickCopyRange), - Handler: opts.Guards.OutsideFilterMode(self.checkSelected(self.handleCopyReflogCommitRange)), + Handler: opts.Guards.OutsideFilterMode(self.checkSelected(self.copyRange)), Description: self.c.Tr.LcCherryPickCopyRange, }, { @@ -83,7 +83,7 @@ func (self *ReflogController) context() *context.ReflogCommitsContext { return self.contexts.ReflogCommits } -func (self *ReflogController) CheckoutReflogCommit(commit *models.Commit) error { +func (self *ReflogController) checkout(commit *models.Commit) error { err := self.c.Ask(types.AskOpts{ Title: self.c.Tr.LcCheckoutCommit, Prompt: self.c.Tr.SureCheckoutThisCommit, @@ -99,11 +99,11 @@ func (self *ReflogController) CheckoutReflogCommit(commit *models.Commit) error return nil } -func (self *ReflogController) handleCreateReflogResetMenu(commit *models.Commit) error { +func (self *ReflogController) openResetMenu(commit *models.Commit) error { return self.helpers.Refs.CreateGitResetMenu(commit.Sha) } -func (self *ReflogController) handleViewReflogCommitFiles(commit *models.Commit) error { +func (self *ReflogController) enter(commit *models.Commit) error { return self.switchToCommitFilesContext(SwitchToCommitFilesContextOpts{ RefName: commit.Sha, CanRebase: false, @@ -111,10 +111,10 @@ func (self *ReflogController) handleViewReflogCommitFiles(commit *models.Commit) }) } -func (self *ReflogController) handleCopyReflogCommit(commit *models.Commit) error { +func (self *ReflogController) copy(commit *models.Commit) error { return self.helpers.CherryPick.Copy(commit, self.model.FilteredReflogCommits, self.context()) } -func (self *ReflogController) handleCopyReflogCommitRange(commit *models.Commit) error { +func (self *ReflogController) copyRange(commit *models.Commit) error { return self.helpers.CherryPick.CopyRange(self.context().GetSelectedLineIdx(), self.model.FilteredReflogCommits, self.context()) } diff --git a/pkg/gui/controllers/sub_commits_controller.go b/pkg/gui/controllers/sub_commits_controller.go new file mode 100644 index 000000000..7d07fb644 --- /dev/null +++ b/pkg/gui/controllers/sub_commits_controller.go @@ -0,0 +1,131 @@ +package controllers + +import ( + "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/jesseduffield/lazygit/pkg/gui/context" + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +type SubCommitsController struct { + baseController + *controllerCommon + + switchToCommitFilesContext SwitchToCommitFilesContextFn +} + +var _ types.IController = &SubCommitsController{} + +func NewSubCommitsController( + common *controllerCommon, + switchToCommitFilesContext SwitchToCommitFilesContextFn, +) *SubCommitsController { + return &SubCommitsController{ + baseController: baseController{}, + controllerCommon: common, + switchToCommitFilesContext: switchToCommitFilesContext, + } +} + +func (self *SubCommitsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { + bindings := []*types.Binding{ + { + Key: opts.GetKey(opts.Config.Universal.GoInto), + Handler: self.checkSelected(self.enter), + Description: self.c.Tr.LcViewCommitFiles, + }, + { + Key: opts.GetKey(opts.Config.Universal.Select), + Handler: self.checkSelected(self.checkout), + Description: self.c.Tr.LcCheckoutCommit, + }, + { + Key: opts.GetKey(opts.Config.Commits.ViewResetOptions), + Handler: self.checkSelected(self.openResetMenu), + Description: self.c.Tr.LcViewResetOptions, + OpensMenu: true, + }, + { + Key: opts.GetKey(opts.Config.Universal.New), + Handler: self.checkSelected(self.newBranch), + Description: self.c.Tr.LcNewBranch, + }, + { + Key: opts.GetKey(opts.Config.Commits.CherryPickCopy), + Handler: self.checkSelected(self.copy), + Description: self.c.Tr.LcCherryPickCopy, + }, + { + Key: opts.GetKey(opts.Config.Commits.CherryPickCopyRange), + Handler: self.checkSelected(self.copyRange), + Description: self.c.Tr.LcCherryPickCopyRange, + }, + { + Key: opts.GetKey(opts.Config.Commits.ResetCherryPick), + Handler: self.helpers.CherryPick.Reset, + Description: self.c.Tr.LcResetCherryPick, + }, + } + + return bindings +} + +func (self *SubCommitsController) checkSelected(callback func(*models.Commit) error) func() error { + return func() error { + commit := self.context().GetSelected() + if commit == nil { + return nil + } + + return callback(commit) + } +} + +func (self *SubCommitsController) Context() types.Context { + return self.context() +} + +func (self *SubCommitsController) context() *context.ReflogCommitsContext { + return self.contexts.ReflogCommits +} + +func (self *SubCommitsController) checkout(commit *models.Commit) error { + err := self.c.Ask(types.AskOpts{ + Title: self.c.Tr.LcCheckoutCommit, + Prompt: self.c.Tr.SureCheckoutThisCommit, + HandleConfirm: func() error { + self.c.LogAction(self.c.Tr.Actions.CheckoutCommit) + return self.helpers.Refs.CheckoutRef(commit.Sha, types.CheckoutRefOptions{}) + }, + }) + if err != nil { + return err + } + + self.context().SetSelectedLineIdx(0) + + return nil +} + +func (self *SubCommitsController) openResetMenu(commit *models.Commit) error { + return self.helpers.Refs.CreateGitResetMenu(commit.Sha) +} + +func (self *SubCommitsController) enter(commit *models.Commit) error { + return self.switchToCommitFilesContext(SwitchToCommitFilesContextOpts{ + RefName: commit.Sha, + CanRebase: false, + Context: self.context(), + }) +} + +func (self *SubCommitsController) newBranch(commit *models.Commit) error { + return self.helpers.Refs.NewBranch(commit.RefName(), commit.Description(), "") +} + +func (self *SubCommitsController) copy(commit *models.Commit) error { + return self.helpers.CherryPick.Copy(commit, self.model.SubCommits, self.context()) +} + +func (self *SubCommitsController) copyRange(commit *models.Commit) error { + return self.helpers.CherryPick.CopyRange(self.context().GetSelectedLineIdx(), self.model.SubCommits, self.context()) +} diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 4cdf8d8ae..c6b1cee82 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -431,56 +431,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi Handler: self.handleCopySelectedSideContextItemToClipboard, Description: self.c.Tr.LcCopyCommitShaToClipboard, }, - { - ViewName: "branches", - Contexts: []string{string(context.SUB_COMMITS_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Universal.GoInto), - Handler: self.handleViewSubCommitFiles, - Description: self.c.Tr.LcViewCommitFiles, - }, - { - ViewName: "branches", - Contexts: []string{string(context.SUB_COMMITS_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Universal.Select), - Handler: self.handleCheckoutSubCommit, - Description: self.c.Tr.LcCheckoutCommit, - }, - { - ViewName: "branches", - Contexts: []string{string(context.SUB_COMMITS_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Commits.ViewResetOptions), - Handler: self.handleCreateSubCommitResetMenu, - Description: self.c.Tr.LcViewResetOptions, - OpensMenu: true, - }, - { - ViewName: "branches", - Contexts: []string{string(context.SUB_COMMITS_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Universal.New), - Handler: self.handleNewBranchOffSubCommit, - Description: self.c.Tr.LcNewBranch, - }, - { - ViewName: "branches", - Contexts: []string{string(context.SUB_COMMITS_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Commits.CherryPickCopy), - Handler: self.handleCopySubCommit, - Description: self.c.Tr.LcCherryPickCopy, - }, - { - ViewName: "branches", - Contexts: []string{string(context.SUB_COMMITS_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Commits.CherryPickCopyRange), - Handler: self.handleCopySubCommitRange, - Description: self.c.Tr.LcCherryPickCopyRange, - }, - { - ViewName: "branches", - Contexts: []string{string(context.SUB_COMMITS_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Commits.ResetCherryPick), - Handler: self.helpers.CherryPick.Reset, - Description: self.c.Tr.LcResetCherryPick, - }, { ViewName: "branches", Contexts: []string{string(context.SUB_COMMITS_CONTEXT_KEY)}, diff --git a/pkg/gui/sub_commits_panel.go b/pkg/gui/sub_commits_panel.go index 0d39038d4..b79e890b9 100644 --- a/pkg/gui/sub_commits_panel.go +++ b/pkg/gui/sub_commits_panel.go @@ -1,10 +1,5 @@ package gui -import ( - "github.com/jesseduffield/lazygit/pkg/gui/controllers" - "github.com/jesseduffield/lazygit/pkg/gui/types" -) - // list panel functions func (gui *Gui) subCommitsRenderToMain() error { @@ -25,73 +20,3 @@ func (gui *Gui) subCommitsRenderToMain() error { }, }) } - -func (gui *Gui) handleCheckoutSubCommit() error { - commit := gui.State.Contexts.SubCommits.GetSelected() - if commit == nil { - return nil - } - - err := gui.c.Ask(types.AskOpts{ - Title: gui.c.Tr.LcCheckoutCommit, - Prompt: gui.c.Tr.SureCheckoutThisCommit, - HandleConfirm: func() error { - gui.c.LogAction(gui.c.Tr.Actions.CheckoutCommit) - return gui.helpers.Refs.CheckoutRef(commit.Sha, types.CheckoutRefOptions{}) - }, - }) - if err != nil { - return err - } - - gui.State.Contexts.SubCommits.SetSelectedLineIdx(0) - - return nil -} - -func (gui *Gui) handleCreateSubCommitResetMenu() error { - commit := gui.State.Contexts.SubCommits.GetSelected() - - return gui.helpers.Refs.CreateGitResetMenu(commit.Sha) -} - -func (gui *Gui) handleViewSubCommitFiles() error { - commit := gui.State.Contexts.SubCommits.GetSelected() - if commit == nil { - return nil - } - - return gui.SwitchToCommitFilesContext(controllers.SwitchToCommitFilesContextOpts{ - RefName: commit.Sha, - CanRebase: false, - Context: gui.State.Contexts.SubCommits, - }) -} - -func (gui *Gui) handleNewBranchOffSubCommit() error { - commit := gui.State.Contexts.SubCommits.GetSelected() - if commit == nil { - return nil - } - - return gui.helpers.Refs.NewBranch(commit.RefName(), commit.Description(), "") -} - -func (gui *Gui) handleCopySubCommit() error { - commit := gui.State.Contexts.SubCommits.GetSelected() - if commit == nil { - return nil - } - - return gui.helpers.CherryPick.Copy(commit, gui.State.Model.SubCommits, gui.State.Contexts.SubCommits) -} - -func (gui *Gui) handleCopySubCommitRange() error { - // just doing this to ensure something is selected - commit := gui.State.Contexts.SubCommits.GetSelected() - if commit == nil { - return nil - } - - return gui.helpers.CherryPick.CopyRange(gui.State.Contexts.SubCommits.GetSelectedLineIdx(), gui.State.Model.SubCommits, gui.State.Contexts.SubCommits) -}