1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-15 00:15:32 +02:00

Extract a SubCommitsHelper from SwitchToSubCommitsController

We want to use it from BranchesController too.
This commit is contained in:
Stefan Haller
2023-08-05 11:19:16 +02:00
parent 4de3fadb00
commit e8fac6ca73
4 changed files with 88 additions and 51 deletions

View File

@ -76,6 +76,13 @@ func (gui *Gui) resetHelpersAndControllers() {
helperCommon, helperCommon,
func() *status.StatusManager { return gui.statusManager }, func() *status.StatusManager { return gui.statusManager },
) )
setSubCommits := func(commits []*models.Commit) {
gui.Mutexes.SubCommitsMutex.Lock()
defer gui.Mutexes.SubCommitsMutex.Unlock()
gui.State.Model.SubCommits = commits
}
gui.helpers = &helpers.Helpers{ gui.helpers = &helpers.Helpers{
Refs: refsHelper, Refs: refsHelper,
Host: helpers.NewHostHelper(helperCommon), Host: helpers.NewHostHelper(helperCommon),
@ -111,8 +118,9 @@ func (gui *Gui) resetHelpersAndControllers() {
modeHelper, modeHelper,
appStatusHelper, appStatusHelper,
), ),
Search: helpers.NewSearchHelper(helperCommon), Search: helpers.NewSearchHelper(helperCommon),
Worktree: worktreeHelper, Worktree: worktreeHelper,
SubCommits: helpers.NewSubCommitsHelper(helperCommon, refreshHelper, setSubCommits),
} }
gui.CustomCommandsClient = custom_commands.NewClient( gui.CustomCommandsClient = custom_commands.NewClient(
@ -206,13 +214,6 @@ func (gui *Gui) resetHelpersAndControllers() {
controllers.AttachControllers(context, sideWindowControllerFactory.Create(context)) controllers.AttachControllers(context, sideWindowControllerFactory.Create(context))
} }
setSubCommits := func(commits []*models.Commit) {
gui.Mutexes.SubCommitsMutex.Lock()
defer gui.Mutexes.SubCommitsMutex.Unlock()
gui.State.Model.SubCommits = commits
}
for _, context := range []controllers.CanSwitchToSubCommits{ for _, context := range []controllers.CanSwitchToSubCommits{
gui.State.Contexts.Branches, gui.State.Contexts.Branches,
gui.State.Contexts.RemoteBranches, gui.State.Contexts.RemoteBranches,
@ -220,7 +221,7 @@ func (gui *Gui) resetHelpersAndControllers() {
gui.State.Contexts.ReflogCommits, gui.State.Contexts.ReflogCommits,
} { } {
controllers.AttachControllers(context, controllers.NewSwitchToSubCommitsController( controllers.AttachControllers(context, controllers.NewSwitchToSubCommitsController(
common, setSubCommits, context, common, context,
)) ))
} }

View File

@ -49,6 +49,7 @@ type Helpers struct {
WindowArrangement *WindowArrangementHelper WindowArrangement *WindowArrangementHelper
Search *SearchHelper Search *SearchHelper
Worktree *WorktreeHelper Worktree *WorktreeHelper
SubCommits *SubCommitsHelper
} }
func NewStubHelpers() *Helpers { func NewStubHelpers() *Helpers {
@ -83,5 +84,6 @@ func NewStubHelpers() *Helpers {
WindowArrangement: &WindowArrangementHelper{}, WindowArrangement: &WindowArrangementHelper{},
Search: &SearchHelper{}, Search: &SearchHelper{},
Worktree: &WorktreeHelper{}, Worktree: &WorktreeHelper{},
SubCommits: &SubCommitsHelper{},
} }
} }

View File

@ -0,0 +1,69 @@
package helpers
import (
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
)
type SubCommitsHelper struct {
c *HelperCommon
refreshHelper *RefreshHelper
setSubCommits func([]*models.Commit)
}
func NewSubCommitsHelper(
c *HelperCommon,
refreshHelper *RefreshHelper,
setSubCommits func([]*models.Commit),
) *SubCommitsHelper {
return &SubCommitsHelper{
c: c,
refreshHelper: refreshHelper,
setSubCommits: setSubCommits,
}
}
type ViewSubCommitsOpts struct {
Ref types.Ref
Context types.Context
ShowBranchHeads bool
}
func (self *SubCommitsHelper) ViewSubCommits(opts ViewSubCommitsOpts) error {
commits, err := self.c.Git().Loaders.CommitLoader.GetCommits(
git_commands.GetCommitsOptions{
Limit: true,
FilterPath: self.c.Modes().Filtering.GetPath(),
IncludeRebaseCommits: false,
RefName: opts.Ref.FullRefName(),
RefForPushedStatus: opts.Ref.FullRefName(),
},
)
if err != nil {
return err
}
self.setSubCommits(commits)
self.refreshHelper.RefreshAuthors(commits)
subCommitsContext := self.c.Contexts().SubCommits
subCommitsContext.SetSelectedLineIdx(0)
subCommitsContext.SetParentContext(opts.Context)
subCommitsContext.SetWindowName(opts.Context.GetWindowName())
subCommitsContext.SetTitleRef(utils.TruncateWithEllipsis(opts.Ref.RefName(), 50))
subCommitsContext.SetRef(opts.Ref)
subCommitsContext.SetLimitCommits(true)
subCommitsContext.SetShowBranchHeads(opts.ShowBranchHeads)
subCommitsContext.ClearSearchString()
subCommitsContext.GetView().ClearSearch()
err = self.c.PostRefreshUpdate(self.c.Contexts().SubCommits)
if err != nil {
return err
}
return self.c.PushContext(self.c.Contexts().SubCommits)
}

View File

@ -1,10 +1,8 @@
package controllers package controllers
import ( import (
"github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
) )
var _ types.IController = &SwitchToSubCommitsController{} var _ types.IController = &SwitchToSubCommitsController{}
@ -19,20 +17,16 @@ type SwitchToSubCommitsController struct {
baseController baseController
c *ControllerCommon c *ControllerCommon
context CanSwitchToSubCommits context CanSwitchToSubCommits
setSubCommits func([]*models.Commit)
} }
func NewSwitchToSubCommitsController( func NewSwitchToSubCommitsController(
controllerCommon *ControllerCommon, controllerCommon *ControllerCommon,
setSubCommits func([]*models.Commit),
context CanSwitchToSubCommits, context CanSwitchToSubCommits,
) *SwitchToSubCommitsController { ) *SwitchToSubCommitsController {
return &SwitchToSubCommitsController{ return &SwitchToSubCommitsController{
baseController: baseController{}, baseController: baseController{},
c: controllerCommon, c: controllerCommon,
context: context, context: context,
setSubCommits: setSubCommits,
} }
} }
@ -58,40 +52,11 @@ func (self *SwitchToSubCommitsController) viewCommits() error {
return nil return nil
} }
// need to populate my sub commits return self.c.Helpers().SubCommits.ViewSubCommits(helpers.ViewSubCommitsOpts{
commits, err := self.c.Git().Loaders.CommitLoader.GetCommits( Ref: ref,
git_commands.GetCommitsOptions{ Context: self.context,
Limit: true, ShowBranchHeads: self.context.ShowBranchHeadsInSubCommits(),
FilterPath: self.c.Modes().Filtering.GetPath(), })
IncludeRebaseCommits: false,
RefName: ref.FullRefName(),
RefForPushedStatus: ref.FullRefName(),
},
)
if err != nil {
return err
}
self.setSubCommits(commits)
self.c.Helpers().Refresh.RefreshAuthors(commits)
subCommitsContext := self.c.Contexts().SubCommits
subCommitsContext.SetSelectedLineIdx(0)
subCommitsContext.SetParentContext(self.context)
subCommitsContext.SetWindowName(self.context.GetWindowName())
subCommitsContext.SetTitleRef(utils.TruncateWithEllipsis(ref.RefName(), 50))
subCommitsContext.SetRef(ref)
subCommitsContext.SetLimitCommits(true)
subCommitsContext.SetShowBranchHeads(self.context.ShowBranchHeadsInSubCommits())
subCommitsContext.ClearSearchString()
subCommitsContext.GetView().ClearSearch()
err = self.c.PostRefreshUpdate(self.c.Contexts().SubCommits)
if err != nil {
return err
}
return self.c.PushContext(self.c.Contexts().SubCommits)
} }
func (self *SwitchToSubCommitsController) Context() types.Context { func (self *SwitchToSubCommitsController) Context() types.Context {