From b1b0219f04ab0e17dd0ce0f5469dee7daecf2717 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Thu, 19 Mar 2020 22:53:16 +1100 Subject: [PATCH] autostash changes when hard resetting --- pkg/gui/reflog_panel.go | 43 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/pkg/gui/reflog_panel.go b/pkg/gui/reflog_panel.go index 47f33e767..27b9a5590 100644 --- a/pkg/gui/reflog_panel.go +++ b/pkg/gui/reflog_panel.go @@ -3,7 +3,6 @@ package gui import ( "regexp" - "github.com/davecgh/go-spew/spew" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/gui/presentation" @@ -122,7 +121,7 @@ func (gui *Gui) reflogUndo(g *gocui.Gui, v *gocui.View) error { { regexStr: `^commit|^rebase -i \(start\)`, action: func(match []string, commitSha string, prevCommitSha string) (bool, error) { - return true, gui.resetToRef(prevCommitSha, "hard") + return true, gui.handleHardResetWithAutoStash(prevCommitSha) }, }, } @@ -131,8 +130,6 @@ func (gui *Gui) reflogUndo(g *gocui.Gui, v *gocui.View) error { for _, action := range reflogActions { re := regexp.MustCompile(action.regexStr) match := re.FindStringSubmatch(reflogCommit.Name) - gui.Log.Warn(action.regexStr) - gui.Log.Warn(spew.Sdump(match)) if len(match) == 0 { continue } @@ -140,7 +137,6 @@ func (gui *Gui) reflogUndo(g *gocui.Gui, v *gocui.View) error { if len(gui.State.ReflogCommits)-1 >= i+1 { prevCommitSha = gui.State.ReflogCommits[i+1].Sha } - gui.Log.Warn(prevCommitSha) done, err := action.action(match, reflogCommit.Sha, prevCommitSha) if err != nil { @@ -154,3 +150,40 @@ func (gui *Gui) reflogUndo(g *gocui.Gui, v *gocui.View) error { return nil } + +func (gui *Gui) handleHardResetWithAutoStash(commitSha string) error { + // if we have any modified tracked files we need to ask the user if they want us to stash for them + dirtyWorkingTree := false + for _, file := range gui.State.Files { + if file.Tracked { + dirtyWorkingTree = true + break + } + } + + if dirtyWorkingTree { + // offer to autostash changes + return gui.createConfirmationPanel(gui.g, gui.getBranchesView(), true, gui.Tr.SLocalize("AutoStashTitle"), gui.Tr.SLocalize("AutoStashPrompt"), func(g *gocui.Gui, v *gocui.View) error { + if err := gui.GitCommand.StashSave(gui.Tr.SLocalize("StashPrefix") + commitSha); err != nil { + return gui.createErrorPanel(g, err.Error()) + } + if err := gui.resetToRef(commitSha, "hard"); err != nil { + return gui.createErrorPanel(g, err.Error()) + } + + if err := gui.GitCommand.StashDo(0, "pop"); err != nil { + if err := gui.refreshSidePanels(g); err != nil { + return err + } + return gui.createErrorPanel(g, err.Error()) + } + return gui.refreshSidePanels(g) + }, nil) + } + + if err := gui.resetToRef(commitSha, "hard"); err != nil { + return gui.createErrorPanel(gui.g, err.Error()) + } + + return gui.refreshSidePanels(gui.g) +}