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 | |
parent | a15a7b607d2aa8378fb7f4b52c154643a78e8e14 (diff) |
undo via rebase
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/gui/keybindings.go | 7 | ||||
-rw-r--r-- | pkg/gui/reflog_panel.go | 55 | ||||
-rw-r--r-- | pkg/gui/reset_menu_panel.go | 56 | ||||
-rw-r--r-- | pkg/i18n/english.go | 3 |
4 files changed, 95 insertions, 26 deletions
diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 320d10190..deb971e4b 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -314,6 +314,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Handler: gui.handleCreateOptionsMenu, }, { + ViewName: "", + Key: gui.getKey("main.undo"), + Modifier: gocui.ModNone, + Handler: gui.reflogUndo, + Description: gui.Tr.SLocalize("undoReflog"), + }, + { ViewName: "status", Key: gui.getKey("universal.edit"), Modifier: gocui.ModNone, 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 +} diff --git a/pkg/gui/reset_menu_panel.go b/pkg/gui/reset_menu_panel.go index 1920aeb1e..49dd95c3c 100644 --- a/pkg/gui/reset_menu_panel.go +++ b/pkg/gui/reset_menu_panel.go @@ -6,11 +6,39 @@ import ( "github.com/fatih/color" ) +func (gui *Gui) resetToRef(ref string, strength string) error { + if err := gui.GitCommand.ResetToCommit(ref, strength); err != nil { + return gui.createErrorPanel(gui.g, err.Error()) + } + + if err := gui.switchCommitsPanelContext("branch-commits"); err != nil { + return err + } + + gui.State.Panels.Commits.SelectedLine = 0 + gui.State.Panels.ReflogCommits.SelectedLine = 0 + + if err := gui.refreshCommits(gui.g); err != nil { + return err + } + if err := gui.refreshFiles(); err != nil { + return err + } + if err := gui.refreshBranches(gui.g); err != nil { + return err + } + if err := gui.resetOrigin(gui.getCommitsView()); err != nil { + return err + } + + return gui.handleCommitSelect(gui.g, gui.getCommitsView()) +} + func (gui *Gui) createResetMenu(ref string) error { strengths := []string{"soft", "mixed", "hard"} menuItems := make([]*menuItem, len(strengths)) for i, strength := range strengths { - innerStrength := strength + strength := strength menuItems[i] = &menuItem{ displayStrings: []string{ fmt.Sprintf("%s reset", strength), @@ -19,31 +47,7 @@ func (gui *Gui) createResetMenu(ref string) error { ), }, onPress: func() error { - if err := gui.GitCommand.ResetToCommit(ref, innerStrength); err != nil { - return gui.createErrorPanel(gui.g, err.Error()) - } - - if err := gui.switchCommitsPanelContext("branch-commits"); err != nil { - return err - } - - gui.State.Panels.Commits.SelectedLine = 0 - gui.State.Panels.ReflogCommits.SelectedLine = 0 - - if err := gui.refreshCommits(gui.g); err != nil { - return err - } - if err := gui.refreshFiles(); err != nil { - return err - } - if err := gui.refreshBranches(gui.g); err != nil { - return err - } - if err := gui.resetOrigin(gui.getCommitsView()); err != nil { - return err - } - - return gui.handleCommitSelect(gui.g, gui.getCommitsView()) + return gui.resetToRef(ref, strength) }, } } diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index d2d473f9a..5e1474425 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -361,6 +361,9 @@ func addEnglish(i18nObject *i18n.Bundle) error { ID: "undo", Other: "undo", }, &i18n.Message{ + ID: "undoReflog", + Other: "undo (via reflog)", + }, &i18n.Message{ ID: "pop", Other: "pop", }, &i18n.Message{ |