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

91 lines
2.2 KiB
Go
Raw Normal View History

2022-02-06 05:37:16 +02: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 08:03:30 +02:00
var _ types.IController = &SwitchToSubCommitsController{}
2022-02-06 05:37:16 +02:00
2022-03-26 08:08:23 +02:00
type CanSwitchToSubCommits interface {
2022-02-06 05:37:16 +02:00
types.Context
2022-03-26 15:18:08 +02:00
GetSelectedRef() types.Ref
2022-02-06 05:37:16 +02:00
}
2022-03-26 08:03:30 +02:00
type SwitchToSubCommitsController struct {
2022-02-06 05:37:16 +02:00
baseController
2022-02-06 06:54:26 +02:00
*controllerCommon
2022-03-26 08:08:23 +02:00
context CanSwitchToSubCommits
2022-02-06 05:37:16 +02:00
2022-02-06 06:54:26 +02:00
setSubCommits func([]*models.Commit)
2022-02-06 05:37:16 +02:00
}
2022-03-26 08:03:30 +02:00
func NewSwitchToSubCommitsController(
controllerCommon *controllerCommon,
2022-02-06 05:37:16 +02:00
setSubCommits func([]*models.Commit),
2022-03-26 08:08:23 +02:00
context CanSwitchToSubCommits,
2022-03-26 08:03:30 +02:00
) *SwitchToSubCommitsController {
return &SwitchToSubCommitsController{
2022-02-06 06:54:26 +02:00
baseController: baseController{},
2022-03-26 08:03:30 +02:00
controllerCommon: controllerCommon,
2022-02-06 06:54:26 +02:00
context: context,
2022-03-26 08:03:30 +02:00
setSubCommits: setSubCommits,
2022-02-06 05:37:16 +02:00
}
}
2022-03-26 08:03:30 +02:00
func (self *SwitchToSubCommitsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
2022-02-06 05:37:16 +02:00
bindings := []*types.Binding{
{
Handler: self.viewCommits,
Key: opts.GetKey(opts.Config.Universal.GoInto),
Description: self.c.Tr.LcViewCommits,
},
}
return bindings
}
2022-03-26 08:03:30 +02:00
func (self *SwitchToSubCommitsController) GetOnClick() func() error {
2022-02-27 02:42:22 +02:00
return self.viewCommits
}
2022-03-26 08:03:30 +02:00
func (self *SwitchToSubCommitsController) viewCommits() error {
2022-03-26 15:18:08 +02:00
ref := self.context.GetSelectedRef()
if ref == nil {
2022-02-06 05:37:16 +02:00
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,
2022-03-26 15:18:08 +02:00
RefName: ref.RefName(),
2022-02-06 05:37:16 +02:00
},
)
if err != nil {
return err
}
self.setSubCommits(commits)
2022-02-06 06:54:26 +02:00
self.contexts.SubCommits.SetSelectedLineIdx(0)
self.contexts.SubCommits.SetParentContext(self.context)
self.contexts.SubCommits.SetWindowName(self.context.GetWindowName())
2022-03-26 15:18:08 +02:00
self.contexts.SubCommits.SetTitleRef(ref.Description())
self.contexts.SubCommits.SetRefName(ref.RefName())
err = self.c.PostRefreshUpdate(self.contexts.SubCommits)
if err != nil {
return err
}
2022-02-06 05:37:16 +02:00
2022-02-06 06:54:26 +02:00
return self.c.PushContext(self.contexts.SubCommits)
2022-02-06 05:37:16 +02:00
}
2022-03-26 08:03:30 +02:00
func (self *SwitchToSubCommitsController) Context() types.Context {
2022-02-06 05:37:16 +02:00
return self.context
}