diff --git a/pkg/gui/controllers/reflog_controller.go b/pkg/gui/controllers/reflog_controller.go new file mode 100644 index 000000000..c1251936e --- /dev/null +++ b/pkg/gui/controllers/reflog_controller.go @@ -0,0 +1,120 @@ +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 ReflogController struct { + baseController + *controllerCommon + + switchToCommitFilesContext SwitchToCommitFilesContextFn +} + +var _ types.IController = &ReflogController{} + +func NewReflogController( + common *controllerCommon, + switchToCommitFilesContext SwitchToCommitFilesContextFn, +) *ReflogController { + return &ReflogController{ + baseController: baseController{}, + controllerCommon: common, + switchToCommitFilesContext: switchToCommitFilesContext, + } +} + +func (self *ReflogController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { + bindings := []*types.Binding{ + { + Key: opts.GetKey(opts.Config.Universal.GoInto), + Handler: self.checkSelected(self.handleViewReflogCommitFiles), + Description: self.c.Tr.LcViewCommitFiles, + }, + { + Key: opts.GetKey(opts.Config.Universal.Select), + Handler: self.checkSelected(self.CheckoutReflogCommit), + Description: self.c.Tr.LcCheckoutCommit, + }, + { + Key: opts.GetKey(opts.Config.Commits.ViewResetOptions), + Handler: self.checkSelected(self.handleCreateReflogResetMenu), + Description: self.c.Tr.LcViewResetOptions, + OpensMenu: true, + }, + { + Key: opts.GetKey(opts.Config.Commits.CherryPickCopy), + Handler: opts.Guards.OutsideFilterMode(self.checkSelected(self.handleCopyReflogCommit)), + Description: self.c.Tr.LcCherryPickCopy, + }, + { + Key: opts.GetKey(opts.Config.Commits.CherryPickCopyRange), + Handler: opts.Guards.OutsideFilterMode(self.checkSelected(self.handleCopyReflogCommitRange)), + 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 *ReflogController) 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 *ReflogController) Context() types.Context { + return self.context() +} + +func (self *ReflogController) context() *context.ReflogCommitsContext { + return self.contexts.ReflogCommits +} + +func (self *ReflogController) CheckoutReflogCommit(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.CheckoutReflogCommit) + return self.helpers.Refs.CheckoutRef(commit.Sha, types.CheckoutRefOptions{}) + }, + }) + if err != nil { + return err + } + + return nil +} + +func (self *ReflogController) handleCreateReflogResetMenu(commit *models.Commit) error { + return self.helpers.Refs.CreateGitResetMenu(commit.Sha) +} + +func (self *ReflogController) handleViewReflogCommitFiles(commit *models.Commit) error { + return self.switchToCommitFilesContext(SwitchToCommitFilesContextOpts{ + RefName: commit.Sha, + CanRebase: false, + Context: self.context(), + }) +} + +func (self *ReflogController) handleCopyReflogCommit(commit *models.Commit) error { + return self.helpers.CherryPick.Copy(commit, self.model.FilteredReflogCommits, self.context()) +} + +func (self *ReflogController) handleCopyReflogCommitRange(commit *models.Commit) error { + return self.helpers.CherryPick.CopyRange(self.context().GetSelectedLineIdx(), self.model.FilteredReflogCommits, self.context()) +} diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 8b5cef4c4..e0f6eb41f 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -534,6 +534,8 @@ func (gui *Gui) resetControllers() { bisectController := controllers.NewBisectController(common) + reflogController := controllers.NewReflogController(common, gui.SwitchToCommitFilesContext) + gui.Controllers = Controllers{ Submodules: submodulesController, Global: controllers.NewGlobalController(common), @@ -580,6 +582,7 @@ func (gui *Gui) resetControllers() { controllers.AttachControllers(gui.State.Contexts.Tags, gui.Controllers.Tags) controllers.AttachControllers(gui.State.Contexts.Submodules, gui.Controllers.Submodules) controllers.AttachControllers(gui.State.Contexts.LocalCommits, gui.Controllers.LocalCommits, bisectController) + controllers.AttachControllers(gui.State.Contexts.ReflogCommits, reflogController) controllers.AttachControllers(gui.State.Contexts.Remotes, gui.Controllers.Remotes) controllers.AttachControllers(gui.State.Contexts.Menu, gui.Controllers.Menu) controllers.AttachControllers(gui.State.Contexts.Global, gui.Controllers.Sync, gui.Controllers.Undo, gui.Controllers.Global) diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 662d8a327..4cdf8d8ae 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -424,49 +424,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi Handler: self.helpers.CherryPick.Reset, Description: self.c.Tr.LcResetCherryPick, }, - { - ViewName: "commits", - Contexts: []string{string(context.REFLOG_COMMITS_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Universal.GoInto), - Handler: self.handleViewReflogCommitFiles, - Description: self.c.Tr.LcViewCommitFiles, - }, - { - ViewName: "commits", - Contexts: []string{string(context.REFLOG_COMMITS_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Universal.Select), - Handler: self.CheckoutReflogCommit, - Description: self.c.Tr.LcCheckoutCommit, - }, - { - ViewName: "commits", - Contexts: []string{string(context.REFLOG_COMMITS_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Commits.ViewResetOptions), - Handler: self.handleCreateReflogResetMenu, - Description: self.c.Tr.LcViewResetOptions, - OpensMenu: true, - }, - { - ViewName: "commits", - Contexts: []string{string(context.REFLOG_COMMITS_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Commits.CherryPickCopy), - Handler: opts.Guards.OutsideFilterMode(self.handleCopyReflogCommit), - Description: self.c.Tr.LcCherryPickCopy, - }, - { - ViewName: "commits", - Contexts: []string{string(context.REFLOG_COMMITS_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Commits.CherryPickCopyRange), - Handler: opts.Guards.OutsideFilterMode(self.handleCopyReflogCommitRange), - Description: self.c.Tr.LcCherryPickCopyRange, - }, - { - ViewName: "commits", - Contexts: []string{string(context.REFLOG_COMMITS_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Commits.ResetCherryPick), - Handler: self.helpers.CherryPick.Reset, - Description: self.c.Tr.LcResetCherryPick, - }, { ViewName: "commits", Contexts: []string{string(context.REFLOG_COMMITS_CONTEXT_KEY)}, diff --git a/pkg/gui/reflog_panel.go b/pkg/gui/reflog_panel.go index e45210fdf..f13783fbd 100644 --- a/pkg/gui/reflog_panel.go +++ b/pkg/gui/reflog_panel.go @@ -1,12 +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) reflogCommitsRenderToMain() error { commit := gui.State.Contexts.ReflogCommits.GetSelected() var task updateTask @@ -25,62 +18,3 @@ func (gui *Gui) reflogCommitsRenderToMain() error { }, }) } - -func (gui *Gui) CheckoutReflogCommit() error { - commit := gui.State.Contexts.ReflogCommits.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.CheckoutReflogCommit) - return gui.helpers.Refs.CheckoutRef(commit.Sha, types.CheckoutRefOptions{}) - }, - }) - if err != nil { - return err - } - - return nil -} - -func (gui *Gui) handleCreateReflogResetMenu() error { - commit := gui.State.Contexts.ReflogCommits.GetSelected() - - return gui.helpers.Refs.CreateGitResetMenu(commit.Sha) -} - -func (gui *Gui) handleViewReflogCommitFiles() error { - commit := gui.State.Contexts.ReflogCommits.GetSelected() - if commit == nil { - return nil - } - - return gui.SwitchToCommitFilesContext(controllers.SwitchToCommitFilesContextOpts{ - RefName: commit.Sha, - CanRebase: false, - Context: gui.State.Contexts.ReflogCommits, - }) -} - -func (gui *Gui) handleCopyReflogCommit() error { - commit := gui.State.Contexts.ReflogCommits.GetSelected() - if commit == nil { - return nil - } - - return gui.helpers.CherryPick.Copy(commit, gui.State.Model.FilteredReflogCommits, gui.State.Contexts.ReflogCommits) -} - -func (gui *Gui) handleCopyReflogCommitRange() error { - // just doing this to ensure something is selected - commit := gui.State.Contexts.ReflogCommits.GetSelected() - if commit == nil { - return nil - } - - return gui.helpers.CherryPick.CopyRange(gui.State.Contexts.ReflogCommits.GetSelectedLineIdx(), gui.State.Model.FilteredReflogCommits, gui.State.Contexts.ReflogCommits) -}