1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-12-01 22:52:01 +02:00

controller for viewing sub commits

This commit is contained in:
Jesse Duffield
2022-02-06 14:37:16 +11:00
parent cd31a762b9
commit b93b8cc00a
20 changed files with 210 additions and 86 deletions

View File

@@ -38,8 +38,6 @@ type LocalCommitsController struct {
pullFiles PullFilesFn
getHostingServiceMgr GetHostingServiceMgrFn
switchToCommitFilesContext SwitchToCommitFilesContextFn
getLimitCommits func() bool
setLimitCommits func(bool)
getShowWholeGitGraph func() bool
setShowWholeGitGraph func(bool)
}
@@ -60,8 +58,6 @@ func NewLocalCommitsController(
pullFiles PullFilesFn,
getHostingServiceMgr GetHostingServiceMgrFn,
switchToCommitFilesContext SwitchToCommitFilesContextFn,
getLimitCommits func() bool,
setLimitCommits func(bool),
getShowWholeGitGraph func() bool,
setShowWholeGitGraph func(bool),
) *LocalCommitsController {
@@ -80,8 +76,6 @@ func NewLocalCommitsController(
pullFiles: pullFiles,
getHostingServiceMgr: getHostingServiceMgr,
switchToCommitFilesContext: switchToCommitFilesContext,
getLimitCommits: getLimitCommits,
setLimitCommits: setLimitCommits,
getShowWholeGitGraph: getShowWholeGitGraph,
setShowWholeGitGraph: setShowWholeGitGraph,
}
@@ -466,7 +460,7 @@ func (self *LocalCommitsController) handleCommitMoveDown() error {
}
func (self *LocalCommitsController) handleCommitMoveUp() error {
index := self.context.GetPanelState().GetSelectedLineIdx()
index := self.context.GetSelectedLineIdx()
if index == 0 {
return nil
}
@@ -641,8 +635,8 @@ func (self *LocalCommitsController) handleCreateCommitResetMenu(commit *models.C
func (self *LocalCommitsController) openSearch() error {
// we usually lazyload these commits but now that we're searching we need to load them now
if self.getLimitCommits() {
self.setLimitCommits(false)
if self.context.GetLimitCommits() {
self.context.SetLimitCommits(false)
if err := self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.COMMITS}}); err != nil {
return err
}
@@ -655,8 +649,8 @@ func (self *LocalCommitsController) openSearch() error {
func (self *LocalCommitsController) gotoBottom() error {
// we usually lazyload these commits but now that we're jumping to the bottom we need to load them now
if self.getLimitCommits() {
self.setLimitCommits(false)
if self.context.GetLimitCommits() {
self.context.SetLimitCommits(false)
if err := self.c.Refresh(types.RefreshOptions{Mode: types.SYNC, Scope: []types.RefreshableView{types.COMMITS}}); err != nil {
return err
}
@@ -693,7 +687,7 @@ func (self *LocalCommitsController) handleOpenLogMenu() error {
self.setShowWholeGitGraph(!self.getShowWholeGitGraph())
if self.getShowWholeGitGraph() {
self.setLimitCommits(false)
self.context.SetLimitCommits(false)
}
return self.c.WithWaitingStatus(self.c.Tr.LcLoadingCommits, func() error {

View File

@@ -20,23 +20,20 @@ type IRefsHelper interface {
}
type RefsHelper struct {
c *types.ControllerCommon
git *commands.GitCommand
contexts *context.ContextTree
limitCommits func()
c *types.ControllerCommon
git *commands.GitCommand
contexts *context.ContextTree
}
func NewRefsHelper(
c *types.ControllerCommon,
git *commands.GitCommand,
contexts *context.ContextTree,
limitCommits func(),
) *RefsHelper {
return &RefsHelper{
c: c,
git: git,
contexts: contexts,
limitCommits: limitCommits,
c: c,
git: git,
contexts: contexts,
}
}
@@ -51,11 +48,11 @@ func (self *RefsHelper) CheckoutRef(ref string, options types.CheckoutRefOptions
cmdOptions := git_commands.CheckoutOptions{Force: false, EnvVars: options.EnvVars}
onSuccess := func() {
self.contexts.Branches.GetPanelState().SetSelectedLineIdx(0)
self.contexts.BranchCommits.GetPanelState().SetSelectedLineIdx(0)
self.contexts.ReflogCommits.GetPanelState().SetSelectedLineIdx(0)
self.contexts.Branches.SetSelectedLineIdx(0)
self.contexts.ReflogCommits.SetSelectedLineIdx(0)
self.contexts.BranchCommits.SetSelectedLineIdx(0)
// loading a heap of commits is slow so we limit them whenever doing a reset
self.limitCommits()
self.contexts.BranchCommits.SetLimitCommits(true)
}
return self.c.WithWaitingStatus(waitingStatus, func() error {
@@ -107,10 +104,10 @@ func (self *RefsHelper) ResetToRef(ref string, strength string, envVars []string
return self.c.Error(err)
}
self.contexts.BranchCommits.GetPanelState().SetSelectedLineIdx(0)
self.contexts.ReflogCommits.GetPanelState().SetSelectedLineIdx(0)
self.contexts.BranchCommits.SetSelectedLineIdx(0)
self.contexts.ReflogCommits.SetSelectedLineIdx(0)
// loading a heap of commits is slow so we limit them whenever doing a reset
self.limitCommits()
self.contexts.BranchCommits.SetLimitCommits(true)
if err := self.c.PushContext(self.contexts.BranchCommits); err != nil {
return err
@@ -169,8 +166,8 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest
}
}
self.contexts.BranchCommits.GetPanelState().SetSelectedLineIdx(0)
self.contexts.Branches.GetPanelState().SetSelectedLineIdx(0)
self.contexts.BranchCommits.SetSelectedLineIdx(0)
self.contexts.Branches.SetSelectedLineIdx(0)
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
},

View File

@@ -81,7 +81,7 @@ func (self *RemotesController) enter(remote *models.Remote) error {
if len(remote.Branches) == 0 {
newSelectedLine = -1
}
self.contexts.RemoteBranches.GetPanelState().SetSelectedLineIdx(newSelectedLine)
self.contexts.RemoteBranches.SetSelectedLineIdx(newSelectedLine)
return self.c.PushContext(self.contexts.RemoteBranches)
}

View File

@@ -0,0 +1,105 @@
package controllers
import (
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/loaders"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type SubCommitsSwitchControllerFactory struct {
c *types.ControllerCommon
subCommitsContext *context.SubCommitsContext
git *commands.GitCommand
modes *types.Modes
setSubCommits func([]*models.Commit)
}
var _ types.IController = &SubCommitsSwitchController{}
type ContextWithRefName interface {
types.Context
GetSelectedRefName() string
}
type SubCommitsSwitchController struct {
baseController
c *types.ControllerCommon
context ContextWithRefName
subCommitsContext *context.SubCommitsContext
git *commands.GitCommand
modes *types.Modes
setSubCommits func([]*models.Commit)
}
func NewSubCommitsSwitchControllerFactory(
c *types.ControllerCommon,
subCommitsContext *context.SubCommitsContext,
git *commands.GitCommand,
modes *types.Modes,
setSubCommits func([]*models.Commit),
) *SubCommitsSwitchControllerFactory {
return &SubCommitsSwitchControllerFactory{
c: c,
subCommitsContext: subCommitsContext,
git: git,
modes: modes,
setSubCommits: setSubCommits,
}
}
func (self *SubCommitsSwitchControllerFactory) Create(context ContextWithRefName) *SubCommitsSwitchController {
return &SubCommitsSwitchController{
baseController: baseController{},
c: self.c,
context: context,
subCommitsContext: self.subCommitsContext,
git: self.git,
modes: self.modes,
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.subCommitsContext.SetSelectedLineIdx(0)
self.subCommitsContext.SetParentContext(self.context)
return self.c.PushContext(self.subCommitsContext)
}
func (self *SubCommitsSwitchController) Context() types.Context {
return self.context
}

View File

@@ -78,11 +78,6 @@ func (self *TagsController) GetKeybindings(opts types.KeybindingsOpts) []*types.
Description: self.c.Tr.LcViewResetOptions,
OpensMenu: true,
},
{
Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.withSelectedTag(self.enter),
Description: self.c.Tr.LcViewCommits,
},
}
return bindings
@@ -96,10 +91,6 @@ func (self *TagsController) checkout(tag *models.Tag) error {
return self.c.PushContext(self.contexts.Branches)
}
func (self *TagsController) enter(tag *models.Tag) error {
return self.switchToSubCommitsContext(tag.Name)
}
func (self *TagsController) delete(tag *models.Tag) error {
prompt := utils.ResolvePlaceholderString(
self.c.Tr.DeleteTagPrompt,
@@ -153,7 +144,7 @@ func (self *TagsController) createResetMenu(tag *models.Tag) error {
func (self *TagsController) create() error {
// leaving commit SHA blank so that we're just creating the tag for the current commit
return self.tagsHelper.CreateTagMenu("", func() { self.context.GetPanelState().SetSelectedLineIdx(0) })
return self.tagsHelper.CreateTagMenu("", func() { self.context.SetSelectedLineIdx(0) })
}
func (self *TagsController) withSelectedTag(f func(tag *models.Tag) error) func() error {