stash controller

This commit is contained in:
Jesse Duffield 2022-02-13 18:33:46 +11:00
parent a643957f89
commit c685a413c9
4 changed files with 144 additions and 130 deletions

View file

@ -0,0 +1,141 @@
package controllers
import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type StashController struct {
baseController
*controllerCommon
}
var _ types.IController = &StashController{}
func NewStashController(
common *controllerCommon,
) *StashController {
return &StashController{
baseController: baseController{},
controllerCommon: common,
}
}
func (self *StashController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Key: opts.GetKey(opts.Config.Universal.Select),
Handler: self.checkSelected(self.handleStashApply),
Description: self.c.Tr.LcApply,
},
{
Key: opts.GetKey(opts.Config.Stash.PopStash),
Handler: self.checkSelected(self.handleStashPop),
Description: self.c.Tr.LcPop,
},
{
Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.checkSelected(self.handleStashDrop),
Description: self.c.Tr.LcDrop,
},
{
Key: opts.GetKey(opts.Config.Universal.New),
Handler: self.checkSelected(self.handleNewBranchOffStashEntry),
Description: self.c.Tr.LcNewBranch,
},
}
return bindings
}
func (self *StashController) checkSelected(callback func(*models.StashEntry) error) func() error {
return func() error {
item := self.context().GetSelected()
if item == nil {
return nil
}
return callback(item)
}
}
func (self *StashController) Context() types.Context {
return self.context()
}
func (self *StashController) context() *context.StashContext {
return self.contexts.Stash
}
func (self *StashController) handleStashApply(stashEntry *models.StashEntry) error {
apply := func() error {
self.c.LogAction(self.c.Tr.Actions.Stash)
err := self.git.Stash.Apply(stashEntry.Index)
_ = self.postStashRefresh()
if err != nil {
return self.c.Error(err)
}
return nil
}
if self.c.UserConfig.Gui.SkipStashWarning {
return apply()
}
return self.c.Ask(types.AskOpts{
Title: self.c.Tr.StashApply,
Prompt: self.c.Tr.SureApplyStashEntry,
HandleConfirm: func() error {
return apply()
},
})
}
func (self *StashController) handleStashPop(stashEntry *models.StashEntry) error {
pop := func() error {
self.c.LogAction(self.c.Tr.Actions.Stash)
err := self.git.Stash.Pop(stashEntry.Index)
_ = self.postStashRefresh()
if err != nil {
return self.c.Error(err)
}
return nil
}
if self.c.UserConfig.Gui.SkipStashWarning {
return pop()
}
return self.c.Ask(types.AskOpts{
Title: self.c.Tr.StashPop,
Prompt: self.c.Tr.SurePopStashEntry,
HandleConfirm: func() error {
return pop()
},
})
}
func (self *StashController) handleStashDrop(stashEntry *models.StashEntry) error {
return self.c.Ask(types.AskOpts{
Title: self.c.Tr.StashDrop,
Prompt: self.c.Tr.SureDropStashEntry,
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.Stash)
err := self.git.Stash.Drop(stashEntry.Index)
_ = self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH}})
if err != nil {
return self.c.Error(err)
}
return nil
},
})
}
func (self *StashController) postStashRefresh() error {
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH, types.FILES}})
}
func (self *StashController) handleNewBranchOffStashEntry(stashEntry *models.StashEntry) error {
return self.helpers.Refs.NewBranch(stashEntry.RefName(), stashEntry.Description(), "")
}

View file

@ -566,6 +566,7 @@ func (gui *Gui) resetControllers() {
branchesController := controllers.NewBranchesController(common)
gitFlowController := controllers.NewGitFlowController(common)
filesRemoveController := controllers.NewFilesRemoveController(common)
stashController := controllers.NewStashController(common)
switchToSubCommitsControllerFactory := controllers.NewSubCommitsSwitchControllerFactory(
common,
@ -602,6 +603,7 @@ func (gui *Gui) resetControllers() {
controllers.AttachControllers(gui.State.Contexts.ReflogCommits, reflogController)
controllers.AttachControllers(gui.State.Contexts.SubCommits, subCommitsController)
controllers.AttachControllers(gui.State.Contexts.Remotes, gui.Controllers.Remotes)
controllers.AttachControllers(gui.State.Contexts.Stash, stashController)
controllers.AttachControllers(gui.State.Contexts.Menu, gui.Controllers.Menu)
controllers.AttachControllers(gui.State.Contexts.Global, gui.Controllers.Sync, gui.Controllers.Undo, gui.Controllers.Global)

View file

@ -430,30 +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.Select),
Handler: self.handleStashApply,
Description: self.c.Tr.LcApply,
},
{
ViewName: "stash",
Key: opts.GetKey(opts.Config.Stash.PopStash),
Handler: self.handleStashPop,
Description: self.c.Tr.LcPop,
},
{
ViewName: "stash",
Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.handleStashDrop,
Description: self.c.Tr.LcDrop,
},
{
ViewName: "stash",
Key: opts.GetKey(opts.Config.Universal.New),
Handler: self.handleNewBranchOffStashEntry,
Description: self.c.Tr.LcNewBranch,
},
{
ViewName: "commitMessage",
Key: opts.GetKey(opts.Config.Universal.SubmitEditorText),

View file

@ -1,19 +1,8 @@
package gui
import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
// list panel functions
func (gui *Gui) getSelectedStashEntry() *models.StashEntry {
return gui.State.Contexts.Stash.GetSelected()
}
func (gui *Gui) stashRenderToMain() error {
var task updateTask
stashEntry := gui.getSelectedStashEntry()
stashEntry := gui.State.Contexts.Stash.GetSelected()
if stashEntry == nil {
task = NewRenderStringTask(gui.c.Tr.NoStashEntries)
} else {
@ -27,97 +16,3 @@ func (gui *Gui) stashRenderToMain() error {
},
})
}
// specific functions
func (gui *Gui) handleStashApply() error {
stashEntry := gui.getSelectedStashEntry()
if stashEntry == nil {
return nil
}
apply := func() error {
gui.c.LogAction(gui.c.Tr.Actions.Stash)
err := gui.git.Stash.Apply(stashEntry.Index)
_ = gui.postStashRefresh()
if err != nil {
return gui.c.Error(err)
}
return nil
}
if gui.c.UserConfig.Gui.SkipStashWarning {
return apply()
}
return gui.c.Ask(types.AskOpts{
Title: gui.c.Tr.StashApply,
Prompt: gui.c.Tr.SureApplyStashEntry,
HandleConfirm: func() error {
return apply()
},
})
}
func (gui *Gui) handleStashPop() error {
stashEntry := gui.getSelectedStashEntry()
if stashEntry == nil {
return nil
}
pop := func() error {
gui.c.LogAction(gui.c.Tr.Actions.Stash)
err := gui.git.Stash.Pop(stashEntry.Index)
_ = gui.postStashRefresh()
if err != nil {
return gui.c.Error(err)
}
return nil
}
if gui.c.UserConfig.Gui.SkipStashWarning {
return pop()
}
return gui.c.Ask(types.AskOpts{
Title: gui.c.Tr.StashPop,
Prompt: gui.c.Tr.SurePopStashEntry,
HandleConfirm: func() error {
return pop()
},
})
}
func (gui *Gui) handleStashDrop() error {
stashEntry := gui.getSelectedStashEntry()
if stashEntry == nil {
return nil
}
return gui.c.Ask(types.AskOpts{
Title: gui.c.Tr.StashDrop,
Prompt: gui.c.Tr.SureDropStashEntry,
HandleConfirm: func() error {
gui.c.LogAction(gui.c.Tr.Actions.Stash)
err := gui.git.Stash.Drop(stashEntry.Index)
_ = gui.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH}})
if err != nil {
return gui.c.Error(err)
}
return nil
},
})
}
func (gui *Gui) postStashRefresh() error {
return gui.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH, types.FILES}})
}
func (gui *Gui) handleNewBranchOffStashEntry() error {
stashEntry := gui.getSelectedStashEntry()
if stashEntry == nil {
return nil
}
return gui.helpers.Refs.NewBranch(stashEntry.RefName(), stashEntry.Description(), "")
}