1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-30 09:16:47 +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

@ -15,8 +15,9 @@ func (gui *Gui) getSelectedLocalCommit() *models.Commit {
}
func (gui *Gui) onCommitFocus() error {
if gui.State.Contexts.BranchCommits.GetSelectedLineIdx() > COMMIT_THRESHOLD && gui.State.LimitCommits {
gui.State.LimitCommits = false
context := gui.State.Contexts.BranchCommits
if context.GetSelectedLineIdx() > COMMIT_THRESHOLD && context.GetLimitCommits() {
context.SetLimitCommits(false)
go utils.Safe(func() {
if err := gui.refreshCommitsWithLimit(); err != nil {
_ = gui.c.Error(err)

View File

@ -84,3 +84,12 @@ func (self *BranchesViewModel) GetSelected() *models.Branch {
return self.getModel()[self.GetSelectedLineIdx()]
}
func (self *BranchesViewModel) GetSelectedRefName() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.RefName()
}

View File

@ -20,11 +20,6 @@ func (self *ListContextTrait) GetList() types.IList {
return self.list
}
// TODO: remove
func (self *ListContextTrait) GetPanelState() types.IListPanelState {
return self.list
}
func (self *ListContextTrait) GetViewTrait() types.IViewTrait {
return self.viewTrait
}

View File

@ -61,12 +61,14 @@ func (self *LocalCommitsContext) GetSelectedItemId() string {
type LocalCommitsViewModel struct {
*traits.ListCursor
getModel func() []*models.Commit
limitCommits bool
getModel func() []*models.Commit
}
func NewLocalCommitsViewModel(getModel func() []*models.Commit) *LocalCommitsViewModel {
self := &LocalCommitsViewModel{
getModel: getModel,
getModel: getModel,
limitCommits: true,
}
self.ListCursor = traits.NewListCursor(self)
@ -85,3 +87,11 @@ func (self *LocalCommitsViewModel) GetSelected() *models.Commit {
return self.getModel()[self.GetSelectedLineIdx()]
}
func (self *LocalCommitsViewModel) SetLimitCommits(value bool) {
self.limitCommits = value
}
func (self *LocalCommitsViewModel) GetLimitCommits() bool {
return self.limitCommits
}

View File

@ -84,3 +84,12 @@ func (self *RemoteBranchesViewModel) GetSelected() *models.RemoteBranch {
return self.getModel()[self.GetSelectedLineIdx()]
}
func (self *RemoteBranchesViewModel) GetSelectedRefName() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.RefName()
}

View File

@ -84,3 +84,12 @@ func (self *TagsViewModel) GetSelected() *models.Tag {
return self.getModel()[self.GetSelectedLineIdx()]
}
func (self *TagsViewModel) GetSelectedRefName() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.RefName()
}

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 {

View File

@ -51,6 +51,6 @@ func (gui *Gui) setFiltering(path string) error {
}
return gui.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.COMMITS}, Then: func() {
gui.State.Contexts.BranchCommits.GetPanelState().SetSelectedLineIdx(0)
gui.State.Contexts.BranchCommits.SetSelectedLineIdx(0)
}})
}

View File

@ -175,7 +175,7 @@ type PrevLayout struct {
type GuiRepoState struct {
Model *types.Model
Modes Modes
Modes *types.Modes
// Suggestions will sometimes appear when typing into a prompt
Suggestions []*types.Suggestion
@ -297,12 +297,6 @@ const (
COMPLETE
)
type Modes struct {
Filtering filtering.Filtering
CherryPicking *cherrypicking.CherryPicking
Diffing diffing.Diffing
}
// if you add a new mutex here be sure to instantiate it. We're using pointers to
// mutexes so that we can pass the mutexes to controllers.
type guiMutexes struct {
@ -397,9 +391,8 @@ func (gui *Gui) resetState(filterPath string, reuseState bool) {
UserVerticalScrolling: false,
},
},
LimitCommits: true,
Ptmx: nil,
Modes: Modes{
Ptmx: nil,
Modes: &types.Modes{
Filtering: filtering.New(filterPath),
CherryPicking: cherrypicking.New(),
Diffing: diffing.New(),
@ -517,7 +510,6 @@ func (gui *Gui) resetControllers() {
controllerCommon,
gui.git,
gui.State.Contexts,
func() { gui.State.LimitCommits = true },
),
Bisect: controllers.NewBisectHelper(controllerCommon, gui.git),
Suggestions: controllers.NewSuggestionsHelper(controllerCommon, model, gui.refreshSuggestions),
@ -608,8 +600,6 @@ func (gui *Gui) resetControllers() {
syncController.HandlePull,
gui.getHostingServiceMgr,
gui.SwitchToCommitFilesContext,
func() bool { return gui.State.LimitCommits },
func(value bool) { gui.State.LimitCommits = value },
func() bool { return gui.ShowWholeGitGraph },
func(value bool) { gui.ShowWholeGitGraph = value },
),
@ -634,6 +624,22 @@ func (gui *Gui) resetControllers() {
Sync: syncController,
}
switchToSubCommitsControllerFactory := controllers.NewSubCommitsSwitchControllerFactory(
controllerCommon,
gui.State.Contexts.SubCommits,
gui.git,
gui.State.Modes,
func(commits []*models.Commit) { gui.State.Model.SubCommits = commits },
)
for _, context := range []controllers.ContextWithRefName{
gui.State.Contexts.Branches,
gui.State.Contexts.RemoteBranches,
gui.State.Contexts.Tags,
} {
controllers.AttachControllers(context, switchToSubCommitsControllerFactory.Create(context))
}
controllers.AttachControllers(gui.State.Contexts.Files, gui.Controllers.Files)
controllers.AttachControllers(gui.State.Contexts.Tags, gui.Controllers.Tags)
controllers.AttachControllers(gui.State.Contexts.Submodules, gui.Controllers.Submodules)

View File

@ -477,13 +477,6 @@ func (gui *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBin
Handler: gui.handleCopySelectedSideContextItemToClipboard,
Description: gui.c.Tr.LcCopyBranchNameToClipboard,
},
{
ViewName: "branches",
Contexts: []string{string(context.LOCAL_BRANCHES_CONTEXT_KEY)},
Key: gui.getKey(config.Universal.GoInto),
Handler: gui.handleEnterBranch,
Description: gui.c.Tr.LcViewCommits,
},
{
ViewName: "branches",
Contexts: []string{string(context.REMOTE_BRANCHES_CONTEXT_KEY)},
@ -499,13 +492,6 @@ func (gui *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBin
Description: gui.c.Tr.LcViewResetOptions,
OpensMenu: true,
},
{
ViewName: "branches",
Contexts: []string{string(context.REMOTE_BRANCHES_CONTEXT_KEY)},
Key: gui.getKey(config.Universal.GoInto),
Handler: gui.handleEnterRemoteBranch,
Description: gui.c.Tr.LcViewCommits,
},
{
ViewName: "commits",
Contexts: []string{string(context.BRANCH_COMMITS_CONTEXT_KEY)},

View File

@ -49,7 +49,7 @@ func (gui *Gui) createMenu(opts types.CreateMenuOptions) error {
}))
gui.State.Contexts.Menu.SetMenuItems(opts.Items)
gui.State.Contexts.Menu.GetPanelState().SetSelectedLineIdx(0)
gui.State.Contexts.Menu.SetSelectedLineIdx(0)
_ = gui.c.PostRefreshUpdate(gui.State.Contexts.Menu)
// TODO: ensure that if we're opened a menu from within a menu that it renders correctly

View File

@ -88,5 +88,5 @@ func (gui *Gui) handleCopyReflogCommitRange() error {
return nil
}
return gui.helpers.CherryPick.CopyRange(gui.State.Contexts.ReflogCommits.GetPanelState().GetSelectedLineIdx(), gui.State.Model.FilteredReflogCommits, gui.State.Contexts.ReflogCommits)
return gui.helpers.CherryPick.CopyRange(gui.State.Contexts.ReflogCommits.GetSelectedLineIdx(), gui.State.Model.FilteredReflogCommits, gui.State.Contexts.ReflogCommits)
}

View File

@ -211,7 +211,7 @@ func (gui *Gui) refreshCommitsWithLimit() error {
commits, err := gui.git.Loaders.Commits.GetCommits(
loaders.GetCommitsOptions{
Limit: gui.State.LimitCommits,
Limit: gui.State.Contexts.BranchCommits.GetLimitCommits(),
FilterPath: gui.State.Modes.Filtering.GetPath(),
IncludeRebaseCommits: true,
RefName: gui.refForLog(),

View File

@ -45,7 +45,7 @@ func (gui *Gui) handleCheckoutSubCommit() error {
return err
}
gui.State.Contexts.SubCommits.GetPanelState().SetSelectedLineIdx(0)
gui.State.Contexts.SubCommits.SetSelectedLineIdx(0)
return nil
}
@ -74,7 +74,7 @@ func (gui *Gui) switchToSubCommitsContext(refName string) error {
// need to populate my sub commits
commits, err := gui.git.Loaders.Commits.GetCommits(
loaders.GetCommitsOptions{
Limit: gui.State.LimitCommits,
Limit: true,
FilterPath: gui.State.Modes.Filtering.GetPath(),
IncludeRebaseCommits: false,
RefName: refName,
@ -85,7 +85,7 @@ func (gui *Gui) switchToSubCommitsContext(refName string) error {
}
gui.State.Model.SubCommits = commits
gui.State.Contexts.SubCommits.GetPanelState().SetSelectedLineIdx(0)
gui.State.Contexts.SubCommits.SetSelectedLineIdx(0)
gui.State.Contexts.SubCommits.SetParentContext(gui.currentSideListContext())
return gui.c.PushContext(gui.State.Contexts.SubCommits)
@ -116,5 +116,5 @@ func (gui *Gui) handleCopySubCommitRange() error {
return nil
}
return gui.helpers.CherryPick.CopyRange(gui.State.Contexts.SubCommits.GetPanelState().GetSelectedLineIdx(), gui.State.Model.SubCommits, gui.State.Contexts.SubCommits)
return gui.helpers.CherryPick.CopyRange(gui.State.Contexts.SubCommits.GetSelectedLineIdx(), gui.State.Model.SubCommits, gui.State.Contexts.SubCommits)
}

View File

@ -59,7 +59,6 @@ type IListContext interface {
OnSearchSelect(selectedLineIdx int) error
FocusLine()
GetPanelState() IListPanelState
GetViewTrait() IViewTrait
}

13
pkg/gui/types/modes.go Normal file
View File

@ -0,0 +1,13 @@
package types
import (
"github.com/jesseduffield/lazygit/pkg/gui/modes/cherrypicking"
"github.com/jesseduffield/lazygit/pkg/gui/modes/diffing"
"github.com/jesseduffield/lazygit/pkg/gui/modes/filtering"
)
type Modes struct {
Filtering filtering.Filtering
CherryPicking *cherrypicking.CherryPicking
Diffing diffing.Diffing
}