mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-03 13:21:56 +02:00
add common commit controller
This commit is contained in:
parent
574c5ca0de
commit
bef26b9634
@ -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())
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
81
pkg/gui/controllers/common_commit_controller.go
Normal file
81
pkg/gui/controllers/common_commit_controller.go
Normal file
@ -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
|
||||
}
|
@ -162,11 +162,6 @@ func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) [
|
||||
Handler: self.checkSelected(self.handleCreateCommitResetMenu),
|
||||
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),
|
||||
@ -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,
|
||||
|
@ -28,11 +28,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),
|
||||
@ -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())
|
||||
}
|
||||
|
@ -28,11 +28,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),
|
||||
@ -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(), "")
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user