mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-27 12:32:37 +02:00
undo via rebase
This commit is contained in:
parent
a15a7b607d
commit
b1941c33f7
@ -313,6 +313,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
||||
Modifier: gocui.ModNone,
|
||||
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"),
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -360,6 +360,9 @@ func addEnglish(i18nObject *i18n.Bundle) error {
|
||||
}, &i18n.Message{
|
||||
ID: "undo",
|
||||
Other: "undo",
|
||||
}, &i18n.Message{
|
||||
ID: "undoReflog",
|
||||
Other: "undo (via reflog)",
|
||||
}, &i18n.Message{
|
||||
ID: "pop",
|
||||
Other: "pop",
|
||||
|
Loading…
x
Reference in New Issue
Block a user