diff options
-rw-r--r-- | pkg/gui/controllers/local_commits_controller.go | 9 | ||||
-rw-r--r-- | pkg/gui/controllers/tag_actions.go | 80 | ||||
-rw-r--r-- | pkg/gui/controllers/tags_controller.go | 63 | ||||
-rw-r--r-- | pkg/gui/gui.go | 25 |
4 files changed, 100 insertions, 77 deletions
diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index 7b84fe999..3222a37e3 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -17,7 +17,6 @@ type ( CheckoutRefFn func(refName string, opts types.CheckoutRefOptions) error CreateGitResetMenuFn func(refName string) error SwitchToCommitFilesContextFn func(SwitchToCommitFilesContextOpts) error - CreateTagMenuFn func(commitSha string) error GetHostingServiceMgrFn func() *hosting_service.HostingServiceMgr PullFilesFn func() error CheckMergeOrRebase func(error) error @@ -29,6 +28,7 @@ type LocalCommitsController struct { getContext func() types.IListContext os *oscommands.OSCommand git *commands.GitCommand + tagActions *TagActions refHelper IRefHelper getSelectedLocalCommit func() *models.Commit @@ -36,7 +36,6 @@ type LocalCommitsController struct { getSelectedLocalCommitIdx func() int checkMergeOrRebase CheckMergeOrRebase pullFiles PullFilesFn - createTagMenu CreateTagMenuFn getHostingServiceMgr GetHostingServiceMgrFn switchToCommitFilesContext SwitchToCommitFilesContextFn openSearch OpenSearchFn @@ -53,13 +52,13 @@ func NewLocalCommitsController( getContext func() types.IListContext, os *oscommands.OSCommand, git *commands.GitCommand, + tagActions *TagActions, refHelper IRefHelper, getSelectedLocalCommit func() *models.Commit, getCommits func() []*models.Commit, getSelectedLocalCommitIdx func() int, checkMergeOrRebase CheckMergeOrRebase, pullFiles PullFilesFn, - createTagMenu CreateTagMenuFn, getHostingServiceMgr GetHostingServiceMgrFn, switchToCommitFilesContext SwitchToCommitFilesContextFn, openSearch OpenSearchFn, @@ -73,13 +72,13 @@ func NewLocalCommitsController( getContext: getContext, os: os, git: git, + tagActions: tagActions, refHelper: refHelper, getSelectedLocalCommit: getSelectedLocalCommit, getCommits: getCommits, getSelectedLocalCommitIdx: getSelectedLocalCommitIdx, checkMergeOrRebase: checkMergeOrRebase, pullFiles: pullFiles, - createTagMenu: createTagMenu, getHostingServiceMgr: getHostingServiceMgr, switchToCommitFilesContext: switchToCommitFilesContext, openSearch: openSearch, @@ -607,7 +606,7 @@ func (self *LocalCommitsController) handleSquashAllAboveFixupCommits(commit *mod } 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 { diff --git a/pkg/gui/controllers/tag_actions.go b/pkg/gui/controllers/tag_actions.go new file mode 100644 index 000000000..a48c339ef --- /dev/null +++ b/pkg/gui/controllers/tag_actions.go @@ -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) + }, + }) +} diff --git a/pkg/gui/controllers/tags_controller.go b/pkg/gui/controllers/tags_controller.go index c9967c2e2..1ba03451c 100644 --- a/pkg/gui/controllers/tags_controller.go +++ b/pkg/gui/controllers/tags_controller.go @@ -14,6 +14,7 @@ type TagsController struct { getContext func() *context.TagsContext git *commands.GitCommand getContexts func() context.ContextTree + tagActions *TagActions refHelper IRefHelper suggestionsHelper ISuggestionsHelper @@ -28,6 +29,7 @@ func NewTagsController( getContext func() *context.TagsContext, git *commands.GitCommand, getContexts func() context.ContextTree, + tagActions *TagActions, refHelper IRefHelper, suggestionsHelper ISuggestionsHelper, @@ -38,6 +40,7 @@ func NewTagsController( getContext: getContext, git: git, getContexts: getContexts, + tagActions: tagActions, refHelper: refHelper, suggestionsHelper: suggestionsHelper, @@ -146,67 +149,9 @@ func (self *TagsController) createResetMenu(tag *models.Tag) error { 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 { // 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 { diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index c11820ec6..6796d16de 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -581,15 +581,7 @@ func (gui *Gui) setControllers() { gui.fileHelper = NewFileHelper(controllerCommon, gui.git, osCommand) gui.workingTreeHelper = NewWorkingTreeHelper(func() *filetree.FileTreeViewModel { return gui.State.FileTreeViewModel }) - tagsController := controllers.NewTagsController( - controllerCommon, - func() *context.TagsContext { return gui.State.Contexts.Tags }, - gui.git, - getContexts, - refHelper, - gui.suggestionsHelper, - gui.switchToSubCommitsContext, - ) + tagActions := controllers.NewTagActions(controllerCommon, gui.git) syncController := controllers.NewSyncController( controllerCommon, @@ -629,20 +621,28 @@ func (gui *Gui) setControllers() { gui.fileHelper, 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( controllerCommon, func() types.IListContext { return gui.State.Contexts.BranchCommits }, osCommand, gui.git, + tagActions, refHelper, gui.getSelectedLocalCommit, func() []*models.Commit { return gui.State.Commits }, func() int { return gui.State.Panels.Commits.SelectedLineIdx }, gui.checkMergeOrRebase, syncController.HandlePull, - tagsController.CreateTagMenu, gui.getHostingServiceMgr, gui.SwitchToCommitFilesContext, gui.handleOpenSearch, @@ -651,7 +651,6 @@ func (gui *Gui) setControllers() { func() bool { return gui.ShowWholeGitGraph }, func(value bool) { gui.ShowWholeGitGraph = value }, ), - Remotes: controllers.NewRemotesController( controllerCommon, func() types.IListContext { return gui.State.Contexts.Remotes }, |