1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-19 21:28:28 +02:00
lazygit/pkg/gui/controllers/sub_commits_switch_controller.go

89 lines
2.2 KiB
Go
Raw Normal View History

2022-02-06 14:37:16 +11:00
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 {
2022-02-06 15:54:26 +11:00
controllerCommon *controllerCommon
setSubCommits func([]*models.Commit)
2022-02-06 14:37:16 +11:00
}
var _ types.IController = &SubCommitsSwitchController{}
type ContextWithRefName interface {
types.Context
GetSelectedRefName() string
}
type SubCommitsSwitchController struct {
baseController
2022-02-06 15:54:26 +11:00
*controllerCommon
context ContextWithRefName
2022-02-06 14:37:16 +11:00
2022-02-06 15:54:26 +11:00
setSubCommits func([]*models.Commit)
2022-02-06 14:37:16 +11:00
}
func NewSubCommitsSwitchControllerFactory(
2022-02-06 15:54:26 +11:00
common *controllerCommon,
2022-02-06 14:37:16 +11:00
setSubCommits func([]*models.Commit),
) *SubCommitsSwitchControllerFactory {
return &SubCommitsSwitchControllerFactory{
2022-02-06 15:54:26 +11:00
controllerCommon: common,
setSubCommits: setSubCommits,
2022-02-06 14:37:16 +11:00
}
}
func (self *SubCommitsSwitchControllerFactory) Create(context ContextWithRefName) *SubCommitsSwitchController {
return &SubCommitsSwitchController{
2022-02-06 15:54:26 +11:00
baseController: baseController{},
controllerCommon: self.controllerCommon,
context: context,
setSubCommits: self.setSubCommits,
2022-02-06 14:37:16 +11:00
}
}
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)
2022-02-06 15:54:26 +11:00
self.contexts.SubCommits.SetSelectedLineIdx(0)
self.contexts.SubCommits.SetParentContext(self.context)
2022-02-06 14:37:16 +11:00
2022-02-06 15:54:26 +11:00
return self.c.PushContext(self.contexts.SubCommits)
2022-02-06 14:37:16 +11:00
}
func (self *SubCommitsSwitchController) Context() types.Context {
return self.context
}