2022-02-06 05:37:16 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2022-11-11 04:19:29 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
|
2022-02-06 05:37:16 +02:00
|
|
|
"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
|
2023-03-23 09:47:29 +02:00
|
|
|
c *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(
|
2023-03-23 09:47:29 +02:00
|
|
|
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{
|
2023-03-23 09:47:29 +02:00
|
|
|
baseController: baseController{},
|
|
|
|
c: controllerCommon,
|
|
|
|
context: context,
|
|
|
|
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
|
2023-03-23 04:04:57 +02:00
|
|
|
commits, err := self.c.Git().Loaders.CommitLoader.GetCommits(
|
2022-11-11 04:19:29 +02:00
|
|
|
git_commands.GetCommitsOptions{
|
2022-02-06 05:37:16 +02:00
|
|
|
Limit: true,
|
2023-03-23 04:04:57 +02:00
|
|
|
FilterPath: self.c.Modes().Filtering.GetPath(),
|
2022-02-06 05:37:16 +02:00
|
|
|
IncludeRebaseCommits: false,
|
2022-05-12 12:16:58 +02:00
|
|
|
RefName: ref.FullRefName(),
|
2022-02-06 05:37:16 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
self.setSubCommits(commits)
|
2022-03-24 13:07:30 +02:00
|
|
|
|
2023-03-23 04:04:57 +02:00
|
|
|
self.c.Contexts().SubCommits.SetSelectedLineIdx(0)
|
|
|
|
self.c.Contexts().SubCommits.SetParentContext(self.context)
|
|
|
|
self.c.Contexts().SubCommits.SetWindowName(self.context.GetWindowName())
|
|
|
|
self.c.Contexts().SubCommits.SetTitleRef(ref.Description())
|
|
|
|
self.c.Contexts().SubCommits.SetRef(ref)
|
|
|
|
self.c.Contexts().SubCommits.SetLimitCommits(true)
|
2022-03-24 13:07:30 +02:00
|
|
|
|
2023-03-23 04:04:57 +02:00
|
|
|
err = self.c.PostRefreshUpdate(self.c.Contexts().SubCommits)
|
2022-03-24 13:07:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-06 05:37:16 +02:00
|
|
|
|
2023-03-23 04:04:57 +02:00
|
|
|
return self.c.PushContext(self.c.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
|
|
|
|
}
|