diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-02-13 17:54:36 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-17 19:13:40 +1100 |
commit | bef26b9634a6a4c85028dcb1577161ed2c662b4e (patch) | |
tree | e5c9173615857bbd1e2c683f3189d63d68bd0d95 /pkg | |
parent | 574c5ca0de046fc0572e722822db3bfbddff4d10 (diff) |
add common commit controller
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/gui/context/local_commits_context.go | 4 | ||||
-rw-r--r-- | pkg/gui/context/reflog_commits_context.go | 4 | ||||
-rw-r--r-- | pkg/gui/context/sub_commits_context.go | 4 | ||||
-rw-r--r-- | pkg/gui/controllers/common_commit_controller.go | 81 | ||||
-rw-r--r-- | pkg/gui/controllers/local_commits_controller.go | 13 | ||||
-rw-r--r-- | pkg/gui/controllers/reflog_controller.go | 13 | ||||
-rw-r--r-- | pkg/gui/controllers/sub_commits_controller.go | 13 | ||||
-rw-r--r-- | pkg/gui/gui.go | 13 |
8 files changed, 106 insertions, 39 deletions
diff --git a/pkg/gui/context/local_commits_context.go b/pkg/gui/context/local_commits_context.go index 408a906ba..1937995ff 100644 --- a/pkg/gui/context/local_commits_context.go +++ b/pkg/gui/context/local_commits_context.go @@ -82,6 +82,10 @@ func NewLocalCommitsViewModel(getModel func() []*models.Commit) *LocalCommitsVie return self } +func (self *LocalCommitsContext) CanRebase() bool { + return true +} + func (self *LocalCommitsViewModel) GetItemsLength() int { return len(self.getModel()) } diff --git a/pkg/gui/context/reflog_commits_context.go b/pkg/gui/context/reflog_commits_context.go index 4a53fe393..8e0dfb8ba 100644 --- a/pkg/gui/context/reflog_commits_context.go +++ b/pkg/gui/context/reflog_commits_context.go @@ -58,6 +58,10 @@ func (self *ReflogCommitsContext) GetSelectedItemId() string { return item.ID() } +func (self *ReflogCommitsContext) CanRebase() bool { + return false +} + type ReflogCommitsViewModel struct { *traits.ListCursor getModel func() []*models.Commit diff --git a/pkg/gui/context/sub_commits_context.go b/pkg/gui/context/sub_commits_context.go index 10c2cf41a..b12d86f13 100644 --- a/pkg/gui/context/sub_commits_context.go +++ b/pkg/gui/context/sub_commits_context.go @@ -59,6 +59,10 @@ func (self *SubCommitsContext) GetSelectedItemId() string { return item.ID() } +func (self *SubCommitsContext) CanRebase() bool { + return false +} + type SubCommitsViewModel struct { *traits.ListCursor getModel func() []*models.Commit diff --git a/pkg/gui/controllers/common_commit_controller.go b/pkg/gui/controllers/common_commit_controller.go new file mode 100644 index 000000000..f6ae68eab --- /dev/null +++ b/pkg/gui/controllers/common_commit_controller.go @@ -0,0 +1,81 @@ +package controllers + +import ( + "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +type CommonCommitControllerFactory struct { + controllerCommon *controllerCommon + viewFiles func(SwitchToCommitFilesContextOpts) error +} + +var _ types.IController = &CommonCommitController{} + +type CommitContext interface { + types.Context + CanRebase() bool + GetSelected() *models.Commit +} + +type CommonCommitController struct { + baseController + *controllerCommon + context CommitContext + + viewFiles func(SwitchToCommitFilesContextOpts) error +} + +func NewCommonCommitControllerFactory( + common *controllerCommon, + viewFiles func(SwitchToCommitFilesContextOpts) error, +) *CommonCommitControllerFactory { + return &CommonCommitControllerFactory{ + controllerCommon: common, + viewFiles: viewFiles, + } +} + +func (self *CommonCommitControllerFactory) Create(context CommitContext) *CommonCommitController { + return &CommonCommitController{ + baseController: baseController{}, + controllerCommon: self.controllerCommon, + context: context, + viewFiles: self.viewFiles, + } +} + +func (self *CommonCommitController) 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, + }, + } + + return bindings +} + +func (self *CommonCommitController) 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 *CommonCommitController) enter(commit *models.Commit) error { + return self.viewFiles(SwitchToCommitFilesContextOpts{ + RefName: commit.Sha, + CanRebase: self.context.CanRebase(), + Context: self.context, + }) +} + +func (self *CommonCommitController) Context() types.Context { + return self.context +} diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index d071ab3fb..694d7396f 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -163,11 +163,6 @@ func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) [ Description: self.c.Tr.LcResetToThisCommit, }, { - Key: opts.GetKey(opts.Config.Universal.GoInto), - Handler: self.checkSelected(self.enter), - Description: self.c.Tr.LcViewCommitFiles, - }, - { Key: opts.GetKey(opts.Config.Commits.CheckoutCommit), Handler: self.checkSelected(self.handleCheckoutCommit), Description: self.c.Tr.LcCheckoutCommit, @@ -516,14 +511,6 @@ func (self *LocalCommitsController) afterRevertCommit() error { }) } -func (self *LocalCommitsController) enter(commit *models.Commit) error { - return self.switchToCommitFilesContext(SwitchToCommitFilesContextOpts{ - RefName: commit.Sha, - CanRebase: true, - Context: self.context(), - }) -} - func (self *LocalCommitsController) handleCreateFixupCommit(commit *models.Commit) error { prompt := utils.ResolvePlaceholderString( self.c.Tr.SureCreateFixupCommit, diff --git a/pkg/gui/controllers/reflog_controller.go b/pkg/gui/controllers/reflog_controller.go index 549348040..43413a6ac 100644 --- a/pkg/gui/controllers/reflog_controller.go +++ b/pkg/gui/controllers/reflog_controller.go @@ -29,11 +29,6 @@ func NewReflogController( func (self *ReflogController) 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, @@ -103,14 +98,6 @@ func (self *ReflogController) openResetMenu(commit *models.Commit) error { return self.helpers.Refs.CreateGitResetMenu(commit.Sha) } -func (self *ReflogController) enter(commit *models.Commit) error { - return self.switchToCommitFilesContext(SwitchToCommitFilesContextOpts{ - RefName: commit.Sha, - CanRebase: false, - Context: self.context(), - }) -} - func (self *ReflogController) copy(commit *models.Commit) error { return self.helpers.CherryPick.Copy(commit, self.model.FilteredReflogCommits, self.context()) } diff --git a/pkg/gui/controllers/sub_commits_controller.go b/pkg/gui/controllers/sub_commits_controller.go index 7d07fb644..300f5b3fa 100644 --- a/pkg/gui/controllers/sub_commits_controller.go +++ b/pkg/gui/controllers/sub_commits_controller.go @@ -29,11 +29,6 @@ func NewSubCommitsController( 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, @@ -110,14 +105,6 @@ 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(), "") } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 525521fc0..4e5007f1a 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -578,6 +578,19 @@ func (gui *Gui) resetControllers() { controllers.AttachControllers(context, switchToSubCommitsControllerFactory.Create(context)) } + commonCommitControllerFactory := controllers.NewCommonCommitControllerFactory( + common, + gui.SwitchToCommitFilesContext, + ) + + for _, context := range []controllers.CommitContext{ + gui.State.Contexts.LocalCommits, + gui.State.Contexts.ReflogCommits, + gui.State.Contexts.SubCommits, + } { + controllers.AttachControllers(context, commonCommitControllerFactory.Create(context)) + } + controllers.AttachControllers(gui.State.Contexts.Branches, branchesController) controllers.AttachControllers(gui.State.Contexts.Files, gui.Controllers.Files) controllers.AttachControllers(gui.State.Contexts.Tags, gui.Controllers.Tags) |