diff options
Diffstat (limited to 'pkg/gui/reflog_panel.go')
-rw-r--r-- | pkg/gui/reflog_panel.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/pkg/gui/reflog_panel.go b/pkg/gui/reflog_panel.go index 1d7151201..47f33e767 100644 --- a/pkg/gui/reflog_panel.go +++ b/pkg/gui/reflog_panel.go @@ -1,6 +1,9 @@ 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" @@ -99,3 +102,55 @@ func (gui *Gui) handleCreateReflogResetMenu(g *gocui.Gui, v *gocui.View) error { return gui.createResetMenu(commit.Sha) } + +type reflogAction struct { + regexStr string + action func(match []string, commitSha string, prevCommitSha string) (bool, error) +} + +func (gui *Gui) reflogUndo(g *gocui.Gui, v *gocui.View) error { + reflogActions := []reflogAction{ + { + regexStr: `^checkout: moving from ([\S]+)`, + action: func(match []string, commitSha string, prevCommitSha string) (bool, error) { + if len(match) <= 1 { + return false, nil + } + return true, gui.handleCheckoutRef(match[1]) + }, + }, + { + regexStr: `^commit|^rebase -i \(start\)`, + action: func(match []string, commitSha string, prevCommitSha string) (bool, error) { + return true, gui.resetToRef(prevCommitSha, "hard") + }, + }, + } + + for i, reflogCommit := range gui.State.ReflogCommits { + 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 + } + prevCommitSha := "" + 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 { + return err + } + if done { + return nil + } + } + } + + return nil +} |