mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-09 13:47:11 +02:00
add actions abstraction
This commit is contained in:
parent
51547e3822
commit
77d0732fa8
@ -17,7 +17,6 @@ type (
|
|||||||
CheckoutRefFn func(refName string, opts types.CheckoutRefOptions) error
|
CheckoutRefFn func(refName string, opts types.CheckoutRefOptions) error
|
||||||
CreateGitResetMenuFn func(refName string) error
|
CreateGitResetMenuFn func(refName string) error
|
||||||
SwitchToCommitFilesContextFn func(SwitchToCommitFilesContextOpts) error
|
SwitchToCommitFilesContextFn func(SwitchToCommitFilesContextOpts) error
|
||||||
CreateTagMenuFn func(commitSha string) error
|
|
||||||
GetHostingServiceMgrFn func() *hosting_service.HostingServiceMgr
|
GetHostingServiceMgrFn func() *hosting_service.HostingServiceMgr
|
||||||
PullFilesFn func() error
|
PullFilesFn func() error
|
||||||
CheckMergeOrRebase func(error) error
|
CheckMergeOrRebase func(error) error
|
||||||
@ -29,6 +28,7 @@ type LocalCommitsController struct {
|
|||||||
getContext func() types.IListContext
|
getContext func() types.IListContext
|
||||||
os *oscommands.OSCommand
|
os *oscommands.OSCommand
|
||||||
git *commands.GitCommand
|
git *commands.GitCommand
|
||||||
|
tagActions *TagActions
|
||||||
refHelper IRefHelper
|
refHelper IRefHelper
|
||||||
|
|
||||||
getSelectedLocalCommit func() *models.Commit
|
getSelectedLocalCommit func() *models.Commit
|
||||||
@ -36,7 +36,6 @@ type LocalCommitsController struct {
|
|||||||
getSelectedLocalCommitIdx func() int
|
getSelectedLocalCommitIdx func() int
|
||||||
checkMergeOrRebase CheckMergeOrRebase
|
checkMergeOrRebase CheckMergeOrRebase
|
||||||
pullFiles PullFilesFn
|
pullFiles PullFilesFn
|
||||||
createTagMenu CreateTagMenuFn
|
|
||||||
getHostingServiceMgr GetHostingServiceMgrFn
|
getHostingServiceMgr GetHostingServiceMgrFn
|
||||||
switchToCommitFilesContext SwitchToCommitFilesContextFn
|
switchToCommitFilesContext SwitchToCommitFilesContextFn
|
||||||
openSearch OpenSearchFn
|
openSearch OpenSearchFn
|
||||||
@ -53,13 +52,13 @@ func NewLocalCommitsController(
|
|||||||
getContext func() types.IListContext,
|
getContext func() types.IListContext,
|
||||||
os *oscommands.OSCommand,
|
os *oscommands.OSCommand,
|
||||||
git *commands.GitCommand,
|
git *commands.GitCommand,
|
||||||
|
tagActions *TagActions,
|
||||||
refHelper IRefHelper,
|
refHelper IRefHelper,
|
||||||
getSelectedLocalCommit func() *models.Commit,
|
getSelectedLocalCommit func() *models.Commit,
|
||||||
getCommits func() []*models.Commit,
|
getCommits func() []*models.Commit,
|
||||||
getSelectedLocalCommitIdx func() int,
|
getSelectedLocalCommitIdx func() int,
|
||||||
checkMergeOrRebase CheckMergeOrRebase,
|
checkMergeOrRebase CheckMergeOrRebase,
|
||||||
pullFiles PullFilesFn,
|
pullFiles PullFilesFn,
|
||||||
createTagMenu CreateTagMenuFn,
|
|
||||||
getHostingServiceMgr GetHostingServiceMgrFn,
|
getHostingServiceMgr GetHostingServiceMgrFn,
|
||||||
switchToCommitFilesContext SwitchToCommitFilesContextFn,
|
switchToCommitFilesContext SwitchToCommitFilesContextFn,
|
||||||
openSearch OpenSearchFn,
|
openSearch OpenSearchFn,
|
||||||
@ -73,13 +72,13 @@ func NewLocalCommitsController(
|
|||||||
getContext: getContext,
|
getContext: getContext,
|
||||||
os: os,
|
os: os,
|
||||||
git: git,
|
git: git,
|
||||||
|
tagActions: tagActions,
|
||||||
refHelper: refHelper,
|
refHelper: refHelper,
|
||||||
getSelectedLocalCommit: getSelectedLocalCommit,
|
getSelectedLocalCommit: getSelectedLocalCommit,
|
||||||
getCommits: getCommits,
|
getCommits: getCommits,
|
||||||
getSelectedLocalCommitIdx: getSelectedLocalCommitIdx,
|
getSelectedLocalCommitIdx: getSelectedLocalCommitIdx,
|
||||||
checkMergeOrRebase: checkMergeOrRebase,
|
checkMergeOrRebase: checkMergeOrRebase,
|
||||||
pullFiles: pullFiles,
|
pullFiles: pullFiles,
|
||||||
createTagMenu: createTagMenu,
|
|
||||||
getHostingServiceMgr: getHostingServiceMgr,
|
getHostingServiceMgr: getHostingServiceMgr,
|
||||||
switchToCommitFilesContext: switchToCommitFilesContext,
|
switchToCommitFilesContext: switchToCommitFilesContext,
|
||||||
openSearch: openSearch,
|
openSearch: openSearch,
|
||||||
@ -607,7 +606,7 @@ func (self *LocalCommitsController) handleSquashAllAboveFixupCommits(commit *mod
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *LocalCommitsController) handleTagCommit(commit *models.Commit) error {
|
func (self *LocalCommitsController) handleTagCommit(commit *models.Commit) error {
|
||||||
return self.createTagMenu(commit.Sha)
|
return self.tagActions.CreateTagMenu(commit.Sha, func() {})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *LocalCommitsController) handleCheckoutCommit(commit *models.Commit) error {
|
func (self *LocalCommitsController) handleCheckoutCommit(commit *models.Commit) error {
|
||||||
|
80
pkg/gui/controllers/tag_actions.go
Normal file
80
pkg/gui/controllers/tag_actions.go
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Actions structs are for defining functionality that could be used by multiple contexts.
|
||||||
|
// For example, here we have a CreateTagMenu which is applicable to both the tags context
|
||||||
|
// and the commits context.
|
||||||
|
|
||||||
|
type TagActions struct {
|
||||||
|
c *types.ControllerCommon
|
||||||
|
git *commands.GitCommand
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTagActions(c *types.ControllerCommon, git *commands.GitCommand) *TagActions {
|
||||||
|
return &TagActions{
|
||||||
|
c: c,
|
||||||
|
git: git,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *TagActions) CreateTagMenu(commitSha string, onCreate func()) error {
|
||||||
|
return self.c.Menu(types.CreateMenuOptions{
|
||||||
|
Title: self.c.Tr.TagMenuTitle,
|
||||||
|
Items: []*types.MenuItem{
|
||||||
|
{
|
||||||
|
DisplayString: self.c.Tr.LcLightweightTag,
|
||||||
|
OnPress: func() error {
|
||||||
|
return self.handleCreateLightweightTag(commitSha, onCreate)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
DisplayString: self.c.Tr.LcAnnotatedTag,
|
||||||
|
OnPress: func() error {
|
||||||
|
return self.handleCreateAnnotatedTag(commitSha, onCreate)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *TagActions) afterTagCreate(onCreate func()) error {
|
||||||
|
onCreate()
|
||||||
|
return self.c.Refresh(types.RefreshOptions{
|
||||||
|
Mode: types.ASYNC, Scope: []types.RefreshableView{types.COMMITS, types.TAGS},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *TagActions) handleCreateAnnotatedTag(commitSha string, onCreate func()) error {
|
||||||
|
return self.c.Prompt(types.PromptOpts{
|
||||||
|
Title: self.c.Tr.TagNameTitle,
|
||||||
|
HandleConfirm: func(tagName string) error {
|
||||||
|
return self.c.Prompt(types.PromptOpts{
|
||||||
|
Title: self.c.Tr.TagMessageTitle,
|
||||||
|
HandleConfirm: func(msg string) error {
|
||||||
|
self.c.LogAction(self.c.Tr.Actions.CreateAnnotatedTag)
|
||||||
|
if err := self.git.Tag.CreateAnnotated(tagName, commitSha, msg); err != nil {
|
||||||
|
return self.c.Error(err)
|
||||||
|
}
|
||||||
|
return self.afterTagCreate(onCreate)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *TagActions) handleCreateLightweightTag(commitSha string, onCreate func()) error {
|
||||||
|
return self.c.Prompt(types.PromptOpts{
|
||||||
|
Title: self.c.Tr.TagNameTitle,
|
||||||
|
HandleConfirm: func(tagName string) error {
|
||||||
|
self.c.LogAction(self.c.Tr.Actions.CreateLightweightTag)
|
||||||
|
if err := self.git.Tag.CreateLightweight(tagName, commitSha); err != nil {
|
||||||
|
return self.c.Error(err)
|
||||||
|
}
|
||||||
|
return self.afterTagCreate(onCreate)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
@ -14,6 +14,7 @@ type TagsController struct {
|
|||||||
getContext func() *context.TagsContext
|
getContext func() *context.TagsContext
|
||||||
git *commands.GitCommand
|
git *commands.GitCommand
|
||||||
getContexts func() context.ContextTree
|
getContexts func() context.ContextTree
|
||||||
|
tagActions *TagActions
|
||||||
|
|
||||||
refHelper IRefHelper
|
refHelper IRefHelper
|
||||||
suggestionsHelper ISuggestionsHelper
|
suggestionsHelper ISuggestionsHelper
|
||||||
@ -28,6 +29,7 @@ func NewTagsController(
|
|||||||
getContext func() *context.TagsContext,
|
getContext func() *context.TagsContext,
|
||||||
git *commands.GitCommand,
|
git *commands.GitCommand,
|
||||||
getContexts func() context.ContextTree,
|
getContexts func() context.ContextTree,
|
||||||
|
tagActions *TagActions,
|
||||||
refHelper IRefHelper,
|
refHelper IRefHelper,
|
||||||
suggestionsHelper ISuggestionsHelper,
|
suggestionsHelper ISuggestionsHelper,
|
||||||
|
|
||||||
@ -38,6 +40,7 @@ func NewTagsController(
|
|||||||
getContext: getContext,
|
getContext: getContext,
|
||||||
git: git,
|
git: git,
|
||||||
getContexts: getContexts,
|
getContexts: getContexts,
|
||||||
|
tagActions: tagActions,
|
||||||
refHelper: refHelper,
|
refHelper: refHelper,
|
||||||
suggestionsHelper: suggestionsHelper,
|
suggestionsHelper: suggestionsHelper,
|
||||||
|
|
||||||
@ -146,67 +149,9 @@ func (self *TagsController) createResetMenu(tag *models.Tag) error {
|
|||||||
return self.refHelper.CreateGitResetMenu(tag.Name)
|
return self.refHelper.CreateGitResetMenu(tag.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TagsController) CreateTagMenu(commitSha string) error {
|
|
||||||
return self.c.Menu(types.CreateMenuOptions{
|
|
||||||
Title: self.c.Tr.TagMenuTitle,
|
|
||||||
Items: []*types.MenuItem{
|
|
||||||
{
|
|
||||||
DisplayString: self.c.Tr.LcLightweightTag,
|
|
||||||
OnPress: func() error {
|
|
||||||
return self.handleCreateLightweightTag(commitSha)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
DisplayString: self.c.Tr.LcAnnotatedTag,
|
|
||||||
OnPress: func() error {
|
|
||||||
return self.handleCreateAnnotatedTag(commitSha)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *TagsController) afterTagCreate() error {
|
|
||||||
self.getContext().GetPanelState().SetSelectedLineIdx(0)
|
|
||||||
return self.c.Refresh(types.RefreshOptions{
|
|
||||||
Mode: types.ASYNC, Scope: []types.RefreshableView{types.COMMITS, types.TAGS},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *TagsController) handleCreateAnnotatedTag(commitSha string) error {
|
|
||||||
return self.c.Prompt(types.PromptOpts{
|
|
||||||
Title: self.c.Tr.TagNameTitle,
|
|
||||||
HandleConfirm: func(tagName string) error {
|
|
||||||
return self.c.Prompt(types.PromptOpts{
|
|
||||||
Title: self.c.Tr.TagMessageTitle,
|
|
||||||
HandleConfirm: func(msg string) error {
|
|
||||||
self.c.LogAction(self.c.Tr.Actions.CreateAnnotatedTag)
|
|
||||||
if err := self.git.Tag.CreateAnnotated(tagName, commitSha, msg); err != nil {
|
|
||||||
return self.c.Error(err)
|
|
||||||
}
|
|
||||||
return self.afterTagCreate()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *TagsController) handleCreateLightweightTag(commitSha string) error {
|
|
||||||
return self.c.Prompt(types.PromptOpts{
|
|
||||||
Title: self.c.Tr.TagNameTitle,
|
|
||||||
HandleConfirm: func(tagName string) error {
|
|
||||||
self.c.LogAction(self.c.Tr.Actions.CreateLightweightTag)
|
|
||||||
if err := self.git.Tag.CreateLightweight(tagName, commitSha); err != nil {
|
|
||||||
return self.c.Error(err)
|
|
||||||
}
|
|
||||||
return self.afterTagCreate()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *TagsController) create() error {
|
func (self *TagsController) create() error {
|
||||||
// leaving commit SHA blank so that we're just creating the tag for the current commit
|
// leaving commit SHA blank so that we're just creating the tag for the current commit
|
||||||
return self.CreateTagMenu("")
|
return self.tagActions.CreateTagMenu("", func() { self.getContext().GetPanelState().SetSelectedLineIdx(0) })
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TagsController) withSelectedTag(f func(tag *models.Tag) error) func() error {
|
func (self *TagsController) withSelectedTag(f func(tag *models.Tag) error) func() error {
|
||||||
|
@ -581,15 +581,7 @@ func (gui *Gui) setControllers() {
|
|||||||
gui.fileHelper = NewFileHelper(controllerCommon, gui.git, osCommand)
|
gui.fileHelper = NewFileHelper(controllerCommon, gui.git, osCommand)
|
||||||
gui.workingTreeHelper = NewWorkingTreeHelper(func() *filetree.FileTreeViewModel { return gui.State.FileTreeViewModel })
|
gui.workingTreeHelper = NewWorkingTreeHelper(func() *filetree.FileTreeViewModel { return gui.State.FileTreeViewModel })
|
||||||
|
|
||||||
tagsController := controllers.NewTagsController(
|
tagActions := controllers.NewTagActions(controllerCommon, gui.git)
|
||||||
controllerCommon,
|
|
||||||
func() *context.TagsContext { return gui.State.Contexts.Tags },
|
|
||||||
gui.git,
|
|
||||||
getContexts,
|
|
||||||
refHelper,
|
|
||||||
gui.suggestionsHelper,
|
|
||||||
gui.switchToSubCommitsContext,
|
|
||||||
)
|
|
||||||
|
|
||||||
syncController := controllers.NewSyncController(
|
syncController := controllers.NewSyncController(
|
||||||
controllerCommon,
|
controllerCommon,
|
||||||
@ -629,20 +621,28 @@ func (gui *Gui) setControllers() {
|
|||||||
gui.fileHelper,
|
gui.fileHelper,
|
||||||
gui.workingTreeHelper,
|
gui.workingTreeHelper,
|
||||||
),
|
),
|
||||||
Tags: tagsController,
|
Tags: controllers.NewTagsController(
|
||||||
|
controllerCommon,
|
||||||
|
func() *context.TagsContext { return gui.State.Contexts.Tags },
|
||||||
|
gui.git,
|
||||||
|
getContexts,
|
||||||
|
tagActions,
|
||||||
|
refHelper,
|
||||||
|
gui.suggestionsHelper,
|
||||||
|
gui.switchToSubCommitsContext,
|
||||||
|
),
|
||||||
LocalCommits: controllers.NewLocalCommitsController(
|
LocalCommits: controllers.NewLocalCommitsController(
|
||||||
controllerCommon,
|
controllerCommon,
|
||||||
func() types.IListContext { return gui.State.Contexts.BranchCommits },
|
func() types.IListContext { return gui.State.Contexts.BranchCommits },
|
||||||
osCommand,
|
osCommand,
|
||||||
gui.git,
|
gui.git,
|
||||||
|
tagActions,
|
||||||
refHelper,
|
refHelper,
|
||||||
gui.getSelectedLocalCommit,
|
gui.getSelectedLocalCommit,
|
||||||
func() []*models.Commit { return gui.State.Commits },
|
func() []*models.Commit { return gui.State.Commits },
|
||||||
func() int { return gui.State.Panels.Commits.SelectedLineIdx },
|
func() int { return gui.State.Panels.Commits.SelectedLineIdx },
|
||||||
gui.checkMergeOrRebase,
|
gui.checkMergeOrRebase,
|
||||||
syncController.HandlePull,
|
syncController.HandlePull,
|
||||||
tagsController.CreateTagMenu,
|
|
||||||
gui.getHostingServiceMgr,
|
gui.getHostingServiceMgr,
|
||||||
gui.SwitchToCommitFilesContext,
|
gui.SwitchToCommitFilesContext,
|
||||||
gui.handleOpenSearch,
|
gui.handleOpenSearch,
|
||||||
@ -651,7 +651,6 @@ func (gui *Gui) setControllers() {
|
|||||||
func() bool { return gui.ShowWholeGitGraph },
|
func() bool { return gui.ShowWholeGitGraph },
|
||||||
func(value bool) { gui.ShowWholeGitGraph = value },
|
func(value bool) { gui.ShowWholeGitGraph = value },
|
||||||
),
|
),
|
||||||
|
|
||||||
Remotes: controllers.NewRemotesController(
|
Remotes: controllers.NewRemotesController(
|
||||||
controllerCommon,
|
controllerCommon,
|
||||||
func() types.IListContext { return gui.State.Contexts.Remotes },
|
func() types.IListContext { return gui.State.Contexts.Remotes },
|
||||||
|
Loading…
x
Reference in New Issue
Block a user