Skip to content

Commit

Permalink
undoing status
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Mar 24, 2020
1 parent 137fd80 commit 6591727
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 26 deletions.
18 changes: 15 additions & 3 deletions pkg/gui/branches_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (gui *Gui) handleBranchPress(g *gocui.Gui, v *gocui.View) error {
return gui.createErrorPanel(g, gui.Tr.SLocalize("AlreadyCheckedOutBranch"))
}
branch := gui.getSelectedBranch()
return gui.handleCheckoutRef(branch.Name, nil)
return gui.handleCheckoutRef(branch.Name, handleCheckoutRefOptions{})
}

func (gui *Gui) handleCreatePullRequestPress(g *gocui.Gui, v *gocui.View) error {
Expand Down Expand Up @@ -143,13 +143,25 @@ func (gui *Gui) handleForceCheckout(g *gocui.Gui, v *gocui.View) error {
}, nil)
}

func (gui *Gui) handleCheckoutRef(ref string, onDone func()) error {
type handleCheckoutRefOptions struct {
onDone func()
waitingStatus string
}

func (gui *Gui) handleCheckoutRef(ref string, options handleCheckoutRefOptions) error {
onDone := options.onDone
waitingStatus := options.waitingStatus
if waitingStatus == "" {
waitingStatus = gui.Tr.SLocalize("CheckingOutStatus")
}

if err := gui.GitCommand.Checkout(ref, false); err != nil {
// note, this will only work for english-language git commands. If we force git to use english, and the error isn't this one, then the user will receive an english command they may not understand. I'm not sure what the best solution to this is. Running the command once in english and a second time in the native language is one option

if strings.Contains(err.Error(), "Please commit your changes or stash them before you switch branch") {
// 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") + ref); err != nil {
return gui.createErrorPanel(g, err.Error())
}
Expand Down Expand Up @@ -189,7 +201,7 @@ func (gui *Gui) handleCheckoutRef(ref string, onDone func()) error {

func (gui *Gui) handleCheckoutByName(g *gocui.Gui, v *gocui.View) error {
return gui.createPromptPanel(g, v, gui.Tr.SLocalize("BranchName")+":", "", func(g *gocui.Gui, v *gocui.View) error {
return gui.handleCheckoutRef(gui.trimmedContent(v), nil)
return gui.handleCheckoutRef(gui.trimmedContent(v), handleCheckoutRefOptions{})
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/commits_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ func (gui *Gui) handleCheckoutCommit(g *gocui.Gui, v *gocui.View) error {
}

return gui.createConfirmationPanel(g, gui.getCommitsView(), true, gui.Tr.SLocalize("checkoutCommit"), gui.Tr.SLocalize("SureCheckoutThisCommit"), func(g *gocui.Gui, v *gocui.View) error {
return gui.handleCheckoutRef(commit.Sha, nil)
return gui.handleCheckoutRef(commit.Sha, handleCheckoutRefOptions{})
}, nil)
}

Expand Down
44 changes: 24 additions & 20 deletions pkg/gui/reflog_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (gui *Gui) handleCheckoutReflogCommit(g *gocui.Gui, v *gocui.View) error {
}

err := gui.createConfirmationPanel(g, gui.getCommitsView(), true, gui.Tr.SLocalize("checkoutCommit"), gui.Tr.SLocalize("SureCheckoutThisCommit"), func(g *gocui.Gui, v *gocui.View) error {
return gui.handleCheckoutRef(commit.Sha, nil)
return gui.handleCheckoutRef(commit.Sha, handleCheckoutRefOptions{})
}, nil)
if err != nil {
return err
Expand Down Expand Up @@ -137,7 +137,7 @@ func (gui *Gui) reflogUndo(g *gocui.Gui, v *gocui.View) error {
if len(match) <= 1 {
return false, nil
}
return true, gui.handleCheckoutRef(match[1], onDone)
return true, gui.handleCheckoutRef(match[1], handleCheckoutRefOptions{onDone: onDone, waitingStatus: gui.Tr.SLocalize("UndoingStatus")})
},
},
{
Expand Down Expand Up @@ -198,27 +198,31 @@ func (gui *Gui) handleHardResetWithAutoStash(commitSha string, onDone func()) er
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())
}
onDone()
return gui.WithWaitingStatus(gui.Tr.SLocalize("UndoingStatus"), func() 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())
}
onDone()

if err := gui.GitCommand.StashDo(0, "pop"); err != nil {
if err := gui.refreshSidePanels(g); err != nil {
return err
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.createErrorPanel(g, err.Error())
}
return gui.refreshSidePanels(g)
return gui.refreshSidePanels(g)
})
}, nil)
}

if err := gui.resetToRef(commitSha, "hard"); err != nil {
return gui.createErrorPanel(gui.g, err.Error())
}
onDone()
return gui.refreshSidePanels(gui.g)
return gui.WithWaitingStatus(gui.Tr.SLocalize("UndoingStatus"), func() error {
if err := gui.resetToRef(commitSha, "hard"); err != nil {
return gui.createErrorPanel(gui.g, err.Error())
}
onDone()
return gui.refreshSidePanels(gui.g)
})
}
2 changes: 1 addition & 1 deletion pkg/gui/remote_branches_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (gui *Gui) handleCheckoutRemoteBranch(g *gocui.Gui, v *gocui.View) error {
if remoteBranch == nil {
return nil
}
if err := gui.handleCheckoutRef(remoteBranch.RemoteName+"/"+remoteBranch.Name, nil); err != nil {
if err := gui.handleCheckoutRef(remoteBranch.RemoteName+"/"+remoteBranch.Name, handleCheckoutRefOptions{}); err != nil {
return err
}
return gui.switchBranchesPanelContext("local-branches")
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/tags_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (gui *Gui) handleCheckoutTag(g *gocui.Gui, v *gocui.View) error {
if tag == nil {
return nil
}
if err := gui.handleCheckoutRef(tag.Name, nil); err != nil {
if err := gui.handleCheckoutRef(tag.Name, handleCheckoutRefOptions{}); err != nil {
return err
}
return gui.switchBranchesPanelContext("local-branches")
Expand Down
6 changes: 6 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,12 @@ func addEnglish(i18nObject *i18n.Bundle) error {
}, &i18n.Message{
ID: "CherryPickingStatus",
Other: "cherry-picking",
}, &i18n.Message{
ID: "UndoingStatus",
Other: "undoing",
}, &i18n.Message{
ID: "CheckingOutStatus",
Other: "checking out",
}, &i18n.Message{
ID: "CommitFiles",
Other: "Commit files",
Expand Down

0 comments on commit 6591727

Please sign in to comment.