diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-07-17 09:02:25 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-07-17 09:20:50 +1000 |
commit | a9cc3219815b7c9de0c7300e1d78f8ae42636990 (patch) | |
tree | cc7e033f7385d74552cb7bf06f9d98b654b66252 | |
parent | 6349214f000fcece292fb7e1176fee833adcdf59 (diff) |
prompt to create new branch if branch not found
-rw-r--r-- | pkg/gui/branches_panel.go | 32 | ||||
-rw-r--r-- | pkg/i18n/dutch.go | 6 | ||||
-rw-r--r-- | pkg/i18n/english.go | 6 | ||||
-rw-r--r-- | pkg/i18n/polish.go | 6 |
4 files changed, 44 insertions, 6 deletions
diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 75c01d0e1..d42d3512c 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -150,6 +150,7 @@ func (gui *Gui) handleForceCheckout(g *gocui.Gui, v *gocui.View) error { type handleCheckoutRefOptions struct { WaitingStatus string EnvVars []string + onRefNotFound func(ref string) error } func (gui *Gui) handleCheckoutRef(ref string, options handleCheckoutRefOptions) error { @@ -171,6 +172,10 @@ func (gui *Gui) handleCheckoutRef(ref string, options handleCheckoutRefOptions) if err := gui.GitCommand.Checkout(ref, cmdOptions); 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 options.onRefNotFound != nil && strings.Contains(err.Error(), "did not match any file(s) known to git") { + return options.onRefNotFound(ref) + } + 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 { @@ -205,7 +210,13 @@ func (gui *Gui) handleCheckoutRef(ref string, options handleCheckoutRefOptions) 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), handleCheckoutRefOptions{}) + return gui.handleCheckoutRef(gui.trimmedContent(v), handleCheckoutRefOptions{ + onRefNotFound: func(ref string) error { + return gui.createConfirmationPanel(gui.g, v, true, gui.Tr.SLocalize("BranchNotFoundTitle"), fmt.Sprintf("%s %s%s", gui.Tr.SLocalize("BranchNotFoundPrompt"), ref, "?"), func(_g *gocui.Gui, _v *gocui.View) error { + return gui.createNewBranchWithName(ref) + }, nil) + }, + }) }) } @@ -229,14 +240,23 @@ func (gui *Gui) handleNewBranch(g *gocui.Gui, v *gocui.View) error { }, ) return gui.createPromptPanel(g, v, message, "", func(g *gocui.Gui, v *gocui.View) error { - if err := gui.GitCommand.NewBranch(gui.trimmedContent(v), branch.Name); err != nil { - return gui.surfaceError(err) - } - gui.State.Panels.Branches.SelectedLine = 0 - return gui.refreshSidePanels(refreshOptions{mode: ASYNC}) + return gui.createNewBranchWithName(gui.trimmedContent(v)) }) } +func (gui *Gui) createNewBranchWithName(newBranchName string) error { + branch := gui.getSelectedBranch() + if branch == nil { + return nil + } + + if err := gui.GitCommand.NewBranch(newBranchName, branch.Name); err != nil { + return gui.surfaceError(err) + } + gui.State.Panels.Branches.SelectedLine = 0 + return gui.refreshSidePanels(refreshOptions{mode: ASYNC}) +} + func (gui *Gui) handleDeleteBranch(g *gocui.Gui, v *gocui.View) error { return gui.deleteBranch(g, v, false) } diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index f4cecf4c8..01a30a70d 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -769,6 +769,12 @@ func addDutch(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "NoFilesStagedPrompt", Other: "You have not staged any files. Commit all files?", + }, &i18n.Message{ + ID: "BranchNotFoundTitle", + Other: "Branch not found", + }, &i18n.Message{ + ID: "BranchNotFoundPrompt", + Other: "Branch not found. Create a new branch named", }, ) } diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index e7a16484a..27b3fa46f 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -1155,6 +1155,12 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "NoFilesStagedPrompt", Other: "You have not staged any files. Commit all files?", + }, &i18n.Message{ + ID: "BranchNotFoundTitle", + Other: "Branch not found", + }, &i18n.Message{ + ID: "BranchNotFoundPrompt", + Other: "Branch not found. Create a new branch named", }, ) } diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go index 54ba3bd2c..f1f893a43 100644 --- a/pkg/i18n/polish.go +++ b/pkg/i18n/polish.go @@ -752,6 +752,12 @@ func addPolish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "NoFilesStagedPrompt", Other: "You have not staged any files. Commit all files?", + }, &i18n.Message{ + ID: "BranchNotFoundTitle", + Other: "Branch not found", + }, &i18n.Message{ + ID: "BranchNotFoundPrompt", + Other: "Branch not found. Create a new branch named", }, ) } |