diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-03-19 22:22:00 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-03-25 09:39:04 +1100 |
commit | b1941c33f7ec2f6c1868a395656c0c57f7c0ac05 (patch) | |
tree | 92d1352c19567559257c1ae9bb07540f51a4d063 /pkg/gui/reflog_panel.go | |
parent | a15a7b607d2aa8378fb7f4b52c154643a78e8e14 (diff) |
undo via rebase
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 +} |