From a643957f89c85fc304b78e3a6aad6e5c1a365d50 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 13 Feb 2022 18:26:44 +1100 Subject: [PATCH] include stash in commitish controller --- pkg/gui/context/local_commits_context.go | 10 +++ pkg/gui/context/reflog_commits_context.go | 10 +++ pkg/gui/context/stash_context.go | 14 ++++ pkg/gui/context/sub_commits_context.go | 10 +++ pkg/gui/controllers/commitish_controller.go | 82 +++++++++++++++++++ .../controllers/common_commit_controller.go | 81 ------------------ pkg/gui/gui.go | 7 +- pkg/gui/keybindings.go | 6 -- pkg/gui/stash_panel.go | 14 ---- pkg/i18n/chinese.go | 3 +- pkg/i18n/dutch.go | 3 +- pkg/i18n/english.go | 6 +- pkg/i18n/polish.go | 2 +- 13 files changed, 135 insertions(+), 113 deletions(-) create mode 100644 pkg/gui/controllers/commitish_controller.go delete mode 100644 pkg/gui/controllers/common_commit_controller.go diff --git a/pkg/gui/context/local_commits_context.go b/pkg/gui/context/local_commits_context.go index 1937995ff..2930348e8 100644 --- a/pkg/gui/context/local_commits_context.go +++ b/pkg/gui/context/local_commits_context.go @@ -86,6 +86,16 @@ func (self *LocalCommitsContext) CanRebase() bool { return true } +func (self *LocalCommitsContext) GetSelectedRefName() string { + item := self.GetSelected() + + if item == nil { + return "" + } + + return item.RefName() +} + func (self *LocalCommitsViewModel) GetItemsLength() int { return len(self.getModel()) } diff --git a/pkg/gui/context/reflog_commits_context.go b/pkg/gui/context/reflog_commits_context.go index 8e0dfb8ba..fa136a7d4 100644 --- a/pkg/gui/context/reflog_commits_context.go +++ b/pkg/gui/context/reflog_commits_context.go @@ -62,6 +62,16 @@ func (self *ReflogCommitsContext) CanRebase() bool { return false } +func (self *ReflogCommitsContext) GetSelectedRefName() string { + item := self.GetSelected() + + if item == nil { + return "" + } + + return item.RefName() +} + type ReflogCommitsViewModel struct { *traits.ListCursor getModel func() []*models.Commit diff --git a/pkg/gui/context/stash_context.go b/pkg/gui/context/stash_context.go index 95efeaef1..d538fadf1 100644 --- a/pkg/gui/context/stash_context.go +++ b/pkg/gui/context/stash_context.go @@ -58,6 +58,20 @@ func (self *StashContext) GetSelectedItemId() string { return item.ID() } +func (self *StashContext) CanRebase() bool { + return false +} + +func (self *StashContext) GetSelectedRefName() string { + item := self.GetSelected() + + if item == nil { + return "" + } + + return item.RefName() +} + type StashViewModel struct { *traits.ListCursor getModel func() []*models.StashEntry diff --git a/pkg/gui/context/sub_commits_context.go b/pkg/gui/context/sub_commits_context.go index b12d86f13..83e76e1e0 100644 --- a/pkg/gui/context/sub_commits_context.go +++ b/pkg/gui/context/sub_commits_context.go @@ -63,6 +63,16 @@ func (self *SubCommitsContext) CanRebase() bool { return false } +func (self *SubCommitsContext) GetSelectedRefName() string { + item := self.GetSelected() + + if item == nil { + return "" + } + + return item.RefName() +} + type SubCommitsViewModel struct { *traits.ListCursor getModel func() []*models.Commit diff --git a/pkg/gui/controllers/commitish_controller.go b/pkg/gui/controllers/commitish_controller.go new file mode 100644 index 000000000..b570e4aba --- /dev/null +++ b/pkg/gui/controllers/commitish_controller.go @@ -0,0 +1,82 @@ +package controllers + +import ( + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +// This controller is for all contexts that contain commit files. + +type CommitishControllerFactory struct { + controllerCommon *controllerCommon + viewFiles func(SwitchToCommitFilesContextOpts) error +} + +var _ types.IController = &CommitishController{} + +type Commitish interface { + types.Context + CanRebase() bool + GetSelectedRefName() string +} + +type CommitishController struct { + baseController + *controllerCommon + context Commitish + + viewFiles func(SwitchToCommitFilesContextOpts) error +} + +func NewCommitishControllerFactory( + common *controllerCommon, + viewFiles func(SwitchToCommitFilesContextOpts) error, +) *CommitishControllerFactory { + return &CommitishControllerFactory{ + controllerCommon: common, + viewFiles: viewFiles, + } +} + +func (self *CommitishControllerFactory) Create(context Commitish) *CommitishController { + return &CommitishController{ + baseController: baseController{}, + controllerCommon: self.controllerCommon, + context: context, + viewFiles: self.viewFiles, + } +} + +func (self *CommitishController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { + bindings := []*types.Binding{ + { + Key: opts.GetKey(opts.Config.Universal.GoInto), + Handler: self.checkSelected(self.enter), + Description: self.c.Tr.LcViewItemFiles, + }, + } + + return bindings +} + +func (self *CommitishController) checkSelected(callback func(string) error) func() error { + return func() error { + refName := self.context.GetSelectedRefName() + if refName == "" { + return nil + } + + return callback(refName) + } +} + +func (self *CommitishController) enter(refName string) error { + return self.viewFiles(SwitchToCommitFilesContextOpts{ + RefName: refName, + CanRebase: self.context.CanRebase(), + Context: self.context, + }) +} + +func (self *CommitishController) Context() types.Context { + return self.context +} diff --git a/pkg/gui/controllers/common_commit_controller.go b/pkg/gui/controllers/common_commit_controller.go deleted file mode 100644 index f6ae68eab..000000000 --- a/pkg/gui/controllers/common_commit_controller.go +++ /dev/null @@ -1,81 +0,0 @@ -package controllers - -import ( - "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/types" -) - -type CommonCommitControllerFactory struct { - controllerCommon *controllerCommon - viewFiles func(SwitchToCommitFilesContextOpts) error -} - -var _ types.IController = &CommonCommitController{} - -type CommitContext interface { - types.Context - CanRebase() bool - GetSelected() *models.Commit -} - -type CommonCommitController struct { - baseController - *controllerCommon - context CommitContext - - viewFiles func(SwitchToCommitFilesContextOpts) error -} - -func NewCommonCommitControllerFactory( - common *controllerCommon, - viewFiles func(SwitchToCommitFilesContextOpts) error, -) *CommonCommitControllerFactory { - return &CommonCommitControllerFactory{ - controllerCommon: common, - viewFiles: viewFiles, - } -} - -func (self *CommonCommitControllerFactory) Create(context CommitContext) *CommonCommitController { - return &CommonCommitController{ - baseController: baseController{}, - controllerCommon: self.controllerCommon, - context: context, - viewFiles: self.viewFiles, - } -} - -func (self *CommonCommitController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { - bindings := []*types.Binding{ - { - Key: opts.GetKey(opts.Config.Universal.GoInto), - Handler: self.checkSelected(self.enter), - Description: self.c.Tr.LcViewCommitFiles, - }, - } - - return bindings -} - -func (self *CommonCommitController) checkSelected(callback func(*models.Commit) error) func() error { - return func() error { - commit := self.context.GetSelected() - if commit == nil { - return nil - } - - return callback(commit) - } -} - -func (self *CommonCommitController) enter(commit *models.Commit) error { - return self.viewFiles(SwitchToCommitFilesContextOpts{ - RefName: commit.Sha, - CanRebase: self.context.CanRebase(), - Context: self.context, - }) -} - -func (self *CommonCommitController) Context() types.Context { - return self.context -} diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 77b406e6f..a31c781ed 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -580,17 +580,18 @@ func (gui *Gui) resetControllers() { controllers.AttachControllers(context, switchToSubCommitsControllerFactory.Create(context)) } - commonCommitControllerFactory := controllers.NewCommonCommitControllerFactory( + commitishControllerFactory := controllers.NewCommitishControllerFactory( common, gui.SwitchToCommitFilesContext, ) - for _, context := range []controllers.CommitContext{ + for _, context := range []controllers.Commitish{ gui.State.Contexts.LocalCommits, gui.State.Contexts.ReflogCommits, gui.State.Contexts.SubCommits, + gui.State.Contexts.Stash, } { - controllers.AttachControllers(context, commonCommitControllerFactory.Create(context)) + controllers.AttachControllers(context, commitishControllerFactory.Create(context)) } controllers.AttachControllers(gui.State.Contexts.Branches, branchesController, gitFlowController) diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 9d278aa2d..ba30bd407 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -430,12 +430,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi Handler: self.handleCopySelectedSideContextItemToClipboard, Description: self.c.Tr.LcCopyCommitShaToClipboard, }, - { - ViewName: "stash", - Key: opts.GetKey(opts.Config.Universal.GoInto), - Handler: self.handleViewStashFiles, - Description: self.c.Tr.LcViewStashFiles, - }, { ViewName: "stash", Key: opts.GetKey(opts.Config.Universal.Select), diff --git a/pkg/gui/stash_panel.go b/pkg/gui/stash_panel.go index 6da862004..c0110f553 100644 --- a/pkg/gui/stash_panel.go +++ b/pkg/gui/stash_panel.go @@ -2,7 +2,6 @@ package gui import ( "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/controllers" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -114,19 +113,6 @@ func (gui *Gui) postStashRefresh() error { return gui.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH, types.FILES}}) } -func (gui *Gui) handleViewStashFiles() error { - stashEntry := gui.getSelectedStashEntry() - if stashEntry == nil { - return nil - } - - return gui.SwitchToCommitFilesContext(controllers.SwitchToCommitFilesContextOpts{ - RefName: stashEntry.RefName(), - CanRebase: false, - Context: gui.State.Contexts.Stash, - }) -} - func (gui *Gui) handleNewBranchOffStashEntry() error { stashEntry := gui.getSelectedStashEntry() if stashEntry == nil { diff --git a/pkg/i18n/chinese.go b/pkg/i18n/chinese.go index ac4e53b1b..551f55750 100644 --- a/pkg/i18n/chinese.go +++ b/pkg/i18n/chinese.go @@ -245,7 +245,7 @@ func chineseTranslationSet() TranslationSet { CheckingOutStatus: "检出", CommittingStatus: "正在提交", CommitFiles: "提交文件", - LcViewCommitFiles: "查看提交的文件", + LcViewItemFiles: "查看提交的文件", CommitFilesTitle: "提交文件", LcCheckoutCommitFile: "检出文件", LcDiscardOldFileChange: "放弃对此文件的提交更改", @@ -380,7 +380,6 @@ func chineseTranslationSet() TranslationSet { UnstageLinesTitle: "未暂存的行", UnstageLinesPrompt: "您确定要删除所选的行(git reset)吗?这是不可逆的。\n要禁用此对话框,请将 'gui.skipUnstageLineWarning' 的配置键设置为 true", LcCreateNewBranchFromCommit: "从提交创建新分支", - LcViewStashFiles: "查看贮藏条目中的文件", LcBuildingPatch: "正在构建补丁", LcViewCommits: "查看提交", MinGitVersionError: "Git 版本必须至少为 2.0(即从 2014 年开始)。请升级您的 git 版本。或者在 https://github.com/jesseduffield/lazygit/issues 上提出一个问题,以使 lazygit 更加向后兼容。", diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index c7b31e157..147cf3c6e 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -214,7 +214,7 @@ func dutchTranslationSet() TranslationSet { RedoingStatus: "redoing", CheckingOutStatus: "uitchecken", CommitFiles: "Commit bestanden", - LcViewCommitFiles: "bekijk gecommite bestanden", + LcViewItemFiles: "bekijk gecommite bestanden", CommitFilesTitle: "Commit bestanden", LcCheckoutCommitFile: "bestand uitchecken", LcDiscardOldFileChange: "uitsluit deze commit zijn veranderingen aan dit bestand", @@ -357,7 +357,6 @@ func dutchTranslationSet() TranslationSet { LcAddSubmodule: "voeg nieuwe submodule toe", LcInitSubmodule: "initialiseer submodule", LcViewBulkSubmoduleOptions: "bekijk bulk submodule opties", - LcViewStashFiles: "bekijk bestanden van stash entry", CreatePullRequestOptions: "Bekijk opties voor pull-aanvraag", LcCreatePullRequestOptions: "bekijk opties voor pull-aanvraag", ConfirmRevertCommit: "Weet u zeker dat u {{.selectedCommit}} ongedaan wilt maken?", diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 711ec02da..649b04fd9 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -231,7 +231,7 @@ type TranslationSet struct { CheckingOutStatus string CommittingStatus string CommitFiles string - LcViewCommitFiles string + LcViewItemFiles string CommitFilesTitle string LcCheckoutCommitFile string LcDiscardOldFileChange string @@ -375,7 +375,6 @@ type TranslationSet struct { UnstageLinesTitle string UnstageLinesPrompt string LcCreateNewBranchFromCommit string - LcViewStashFiles string LcBuildingPatch string LcViewCommits string MinGitVersionError string @@ -804,7 +803,7 @@ func EnglishTranslationSet() TranslationSet { CheckingOutStatus: "checking out", CommittingStatus: "committing", CommitFiles: "Commit files", - LcViewCommitFiles: "view commit's files", + LcViewItemFiles: "view selected item's files", CommitFilesTitle: "Commit Files", LcCheckoutCommitFile: "checkout file", LcDiscardOldFileChange: "discard this commit's changes to this file", @@ -949,7 +948,6 @@ func EnglishTranslationSet() TranslationSet { UnstageLinesTitle: "Unstage lines", UnstageLinesPrompt: "Are you sure you want to delete the selected lines (git reset)? It is irreversible.\nTo disable this dialogue set the config key of 'gui.skipUnstageLineWarning' to true", LcCreateNewBranchFromCommit: "create new branch off of commit", - LcViewStashFiles: "view stash entry's files", LcBuildingPatch: "building patch", LcViewCommits: "view commits", MinGitVersionError: "Git version must be at least 2.0 (i.e. from 2014 onwards). Please upgrade your git version. Alternatively raise an issue at https://github.com/jesseduffield/lazygit/issues for lazygit to be more backwards compatible.", diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go index 5ff66f979..ae6942abe 100644 --- a/pkg/i18n/polish.go +++ b/pkg/i18n/polish.go @@ -175,7 +175,7 @@ func polishTranslationSet() TranslationSet { AmendingStatus: "poprawianie", CherryPickingStatus: "przebieranie", CommitFiles: "Pliki commita", - LcViewCommitFiles: "przeglądaj pliki commita", + LcViewItemFiles: "przeglądaj pliki commita", CommitFilesTitle: "Pliki commita", LcCheckoutCommitFile: "plik wybierania", LcDiscardOldFileChange: "porzuć zmiany commita dla tego pliku",