1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-04 10:34:55 +02:00

prompt to create new branch if branch not found

This commit is contained in:
Jesse Duffield 2020-07-17 09:02:25 +10:00
parent 6349214f00
commit a9cc321981
4 changed files with 44 additions and 6 deletions

View File

@ -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)
}

View File

@ -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",
},
)
}

View File

@ -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",
},
)
}

View File

@ -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",
},
)
}