1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-10 04:07:18 +02:00

autostash changes when hard resetting

This commit is contained in:
Jesse Duffield 2020-03-19 22:53:16 +11:00
parent b1941c33f7
commit b1b0219f04

View File

@ -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)
}