1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-03 15:02:52 +02:00
lazygit/pkg/gui/controllers/switch_to_sub_commits_controller.go

82 lines
1.9 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"
)
2022-03-26 17:03:30 +11:00
var _ types.IController = &SwitchToSubCommitsController{}
2022-02-06 14:37:16 +11:00
type ContextWithRefName interface {
types.Context
GetSelectedRefName() string
}
2022-03-26 17:03:30 +11:00
type SwitchToSubCommitsController struct {
2022-02-06 14:37:16 +11:00
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
}
2022-03-26 17:03:30 +11:00
func NewSwitchToSubCommitsController(
controllerCommon *controllerCommon,
2022-02-06 14:37:16 +11:00
setSubCommits func([]*models.Commit),
2022-03-26 17:03:30 +11:00
context ContextWithRefName,
) *SwitchToSubCommitsController {
return &SwitchToSubCommitsController{
2022-02-06 15:54:26 +11:00
baseController: baseController{},
2022-03-26 17:03:30 +11:00
controllerCommon: controllerCommon,
2022-02-06 15:54:26 +11:00
context: context,
2022-03-26 17:03:30 +11:00
setSubCommits: setSubCommits,
2022-02-06 14:37:16 +11:00
}
}
2022-03-26 17:03:30 +11:00
func (self *SwitchToSubCommitsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
2022-02-06 14:37:16 +11:00
bindings := []*types.Binding{
{
Handler: self.viewCommits,
Key: opts.GetKey(opts.Config.Universal.GoInto),
Description: self.c.Tr.LcViewCommits,
},
}
return bindings
}
2022-03-26 17:03:30 +11:00
func (self *SwitchToSubCommitsController) GetOnClick() func() error {
2022-02-27 11:42:22 +11:00
return self.viewCommits
}
2022-03-26 17:03:30 +11:00
func (self *SwitchToSubCommitsController) viewCommits() error {
2022-02-06 14:37:16 +11:00
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
}
2022-03-26 17:03:30 +11:00
func (self *SwitchToSubCommitsController) Context() types.Context {
2022-02-06 14:37:16 +11:00
return self.context
}