1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/controllers/sub_commits_switch_controller.go
2022-03-17 19:13:40 +11:00

89 lines
2.2 KiB
Go

package controllers
import (
"github.com/jesseduffield/lazygit/pkg/commands/loaders"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type SubCommitsSwitchControllerFactory struct {
controllerCommon *controllerCommon
setSubCommits func([]*models.Commit)
}
var _ types.IController = &SubCommitsSwitchController{}
type ContextWithRefName interface {
types.Context
GetSelectedRefName() string
}
type SubCommitsSwitchController struct {
baseController
*controllerCommon
context ContextWithRefName
setSubCommits func([]*models.Commit)
}
func NewSubCommitsSwitchControllerFactory(
common *controllerCommon,
setSubCommits func([]*models.Commit),
) *SubCommitsSwitchControllerFactory {
return &SubCommitsSwitchControllerFactory{
controllerCommon: common,
setSubCommits: setSubCommits,
}
}
func (self *SubCommitsSwitchControllerFactory) Create(context ContextWithRefName) *SubCommitsSwitchController {
return &SubCommitsSwitchController{
baseController: baseController{},
controllerCommon: self.controllerCommon,
context: context,
setSubCommits: self.setSubCommits,
}
}
func (self *SubCommitsSwitchController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Handler: self.viewCommits,
Key: opts.GetKey(opts.Config.Universal.GoInto),
Description: self.c.Tr.LcViewCommits,
},
}
return bindings
}
func (self *SubCommitsSwitchController) viewCommits() error {
refName := self.context.GetSelectedRefName()
if refName == "" {
return nil
}
// need to populate my sub commits
commits, err := self.git.Loaders.Commits.GetCommits(
loaders.GetCommitsOptions{
Limit: true,
FilterPath: self.modes.Filtering.GetPath(),
IncludeRebaseCommits: false,
RefName: refName,
},
)
if err != nil {
return err
}
self.setSubCommits(commits)
self.contexts.SubCommits.SetSelectedLineIdx(0)
self.contexts.SubCommits.SetParentContext(self.context)
return self.c.PushContext(self.contexts.SubCommits)
}
func (self *SubCommitsSwitchController) Context() types.Context {
return self.context
}