1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-17 00:18:05 +02:00

specify upstream when pushing a branch for the first time

This commit is contained in:
Jesse Duffield
2019-11-11 23:22:09 +11:00
parent 6843741d9e
commit 12b84307ac
10 changed files with 46 additions and 21 deletions

View File

@ -398,13 +398,18 @@ func (c *GitCommand) Pull(ask func(string) string) error {
} }
// Push pushes to a branch // Push pushes to a branch
func (c *GitCommand) Push(branchName string, force bool, ask func(string) string) error { func (c *GitCommand) Push(branchName string, force bool, upstream string, ask func(string) string) error {
forceFlag := "" forceFlag := ""
if force { if force {
forceFlag = "--force-with-lease " forceFlag = "--force-with-lease"
} }
cmd := fmt.Sprintf("git push %s-u origin %s", forceFlag, branchName) setUpstreamArg := ""
if upstream != "" {
setUpstreamArg = "--set-upstream " + upstream
}
cmd := fmt.Sprintf("git push %s %s", forceFlag, setUpstreamArg)
return c.OSCommand.DetectUnamePass(cmd, ask) return c.OSCommand.DetectUnamePass(cmd, ask)
} }

View File

@ -990,7 +990,7 @@ func TestGitCommandPush(t *testing.T) {
"Push with force disabled", "Push with force disabled",
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd) assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"push", "-u", "origin", "test"}, args) assert.EqualValues(t, []string{"push"}, args)
return exec.Command("echo") return exec.Command("echo")
}, },
@ -1003,7 +1003,7 @@ func TestGitCommandPush(t *testing.T) {
"Push with force enabled", "Push with force enabled",
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd) assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"push", "--force-with-lease", "-u", "origin", "test"}, args) assert.EqualValues(t, []string{"push", "--force-with-lease"}, args)
return exec.Command("echo") return exec.Command("echo")
}, },
@ -1016,7 +1016,7 @@ func TestGitCommandPush(t *testing.T) {
"Push with an error occurring", "Push with an error occurring",
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd) assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"push", "-u", "origin", "test"}, args) assert.EqualValues(t, []string{"push"}, args)
return exec.Command("test") return exec.Command("test")
}, },
false, false,
@ -1030,7 +1030,7 @@ func TestGitCommandPush(t *testing.T) {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand() gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = s.command gitCmd.OSCommand.command = s.command
err := gitCmd.Push("test", s.forcePush, func(passOrUname string) string { err := gitCmd.Push("test", s.forcePush, "", func(passOrUname string) string {
return "\n" return "\n"
}) })
s.test(err) s.test(err)

View File

@ -207,7 +207,7 @@ func (gui *Gui) handleCheckoutBranch(branchName string) error {
} }
func (gui *Gui) handleCheckoutByName(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) handleCheckoutByName(g *gocui.Gui, v *gocui.View) error {
gui.createPromptPanel(g, v, gui.Tr.SLocalize("BranchName")+":", func(g *gocui.Gui, v *gocui.View) error { gui.createPromptPanel(g, v, gui.Tr.SLocalize("BranchName")+":", "", func(g *gocui.Gui, v *gocui.View) error {
return gui.handleCheckoutBranch(gui.trimmedContent(v)) return gui.handleCheckoutBranch(gui.trimmedContent(v))
}) })
return nil return nil
@ -221,7 +221,7 @@ func (gui *Gui) handleNewBranch(g *gocui.Gui, v *gocui.View) error {
"branchName": branch.Name, "branchName": branch.Name,
}, },
) )
gui.createPromptPanel(g, v, message, func(g *gocui.Gui, v *gocui.View) error { gui.createPromptPanel(g, v, message, "", func(g *gocui.Gui, v *gocui.View) error {
if err := gui.GitCommand.NewBranch(gui.trimmedContent(v)); err != nil { if err := gui.GitCommand.NewBranch(gui.trimmedContent(v)); err != nil {
return gui.createErrorPanel(g, err.Error()) return gui.createErrorPanel(g, err.Error())
} }

View File

@ -237,7 +237,7 @@ func (gui *Gui) handleRenameCommit(g *gocui.Gui, v *gocui.View) error {
if gui.State.Panels.Commits.SelectedLine != 0 { if gui.State.Panels.Commits.SelectedLine != 0 {
return gui.createErrorPanel(g, gui.Tr.SLocalize("OnlyRenameTopCommit")) return gui.createErrorPanel(g, gui.Tr.SLocalize("OnlyRenameTopCommit"))
} }
return gui.createPromptPanel(g, v, gui.Tr.SLocalize("renameCommit"), func(g *gocui.Gui, v *gocui.View) error { return gui.createPromptPanel(g, v, gui.Tr.SLocalize("renameCommit"), "", func(g *gocui.Gui, v *gocui.View) error {
if err := gui.GitCommand.RenameCommit(v.Buffer()); err != nil { if err := gui.GitCommand.RenameCommit(v.Buffer()); err != nil {
return gui.createErrorPanel(g, err.Error()) return gui.createErrorPanel(g, err.Error())
} }

View File

@ -64,13 +64,16 @@ func (gui *Gui) getConfirmationPanelDimensions(g *gocui.Gui, wrap bool, prompt s
height/2 + panelHeight/2 height/2 + panelHeight/2
} }
func (gui *Gui) createPromptPanel(g *gocui.Gui, currentView *gocui.View, title string, handleConfirm func(*gocui.Gui, *gocui.View) error) error { func (gui *Gui) createPromptPanel(g *gocui.Gui, currentView *gocui.View, title string, initialContent string, handleConfirm func(*gocui.Gui, *gocui.View) error) error {
gui.onNewPopupPanel() gui.onNewPopupPanel()
confirmationView, err := gui.prepareConfirmationPanel(currentView, title, "", false) confirmationView, err := gui.prepareConfirmationPanel(currentView, title, initialContent, false)
if err != nil { if err != nil {
return err return err
} }
confirmationView.Editable = true confirmationView.Editable = true
if err := gui.renderString(g, "confirmation", initialContent); err != nil {
return err
}
// in the future we might want to give createPromptPanel the returnFocusOnClose arg too, but for now we're always setting it to true // in the future we might want to give createPromptPanel the returnFocusOnClose arg too, but for now we're always setting it to true
return gui.setKeyBindings(g, handleConfirm, nil, true) return gui.setKeyBindings(g, handleConfirm, nil, true)
} }

View File

@ -424,14 +424,14 @@ func (gui *Gui) pullFiles(g *gocui.Gui, v *gocui.View) error {
return nil return nil
} }
func (gui *Gui) pushWithForceFlag(g *gocui.Gui, v *gocui.View, force bool) error { func (gui *Gui) pushWithForceFlag(g *gocui.Gui, v *gocui.View, force bool, upstream string) error {
if err := gui.createLoaderPanel(gui.g, v, gui.Tr.SLocalize("PushWait")); err != nil { if err := gui.createLoaderPanel(gui.g, v, gui.Tr.SLocalize("PushWait")); err != nil {
return err return err
} }
go func() { go func() {
unamePassOpend := false unamePassOpend := false
branchName := gui.State.Branches[0].Name branchName := gui.State.Branches[0].Name
err := gui.GitCommand.Push(branchName, force, func(passOrUname string) string { err := gui.GitCommand.Push(branchName, force, upstream, func(passOrUname string) string {
unamePassOpend = true unamePassOpend = true
return gui.waitForPassUname(g, v, passOrUname) return gui.waitForPassUname(g, v, passOrUname)
}) })
@ -443,13 +443,21 @@ func (gui *Gui) pushWithForceFlag(g *gocui.Gui, v *gocui.View, force bool) error
func (gui *Gui) pushFiles(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) pushFiles(g *gocui.Gui, v *gocui.View) error {
// if we have pullables we'll ask if the user wants to force push // if we have pullables we'll ask if the user wants to force push
_, pullables := gui.GitCommand.GetCurrentBranchUpstreamDifferenceCount() _, pullables := gui.GitCommand.GetCurrentBranchUpstreamDifferenceCount()
if pullables == "?" || pullables == "0" { currentBranchName, err := gui.GitCommand.CurrentBranchName()
return gui.pushWithForceFlag(g, v, false) if err != nil {
return err
} }
err := gui.createConfirmationPanel(g, nil, true, gui.Tr.SLocalize("ForcePush"), gui.Tr.SLocalize("ForcePushPrompt"), func(g *gocui.Gui, v *gocui.View) error {
return gui.pushWithForceFlag(g, v, true) if pullables == "?" {
return gui.createPromptPanel(g, v, gui.Tr.SLocalize("EnterUpstream"), "origin "+currentBranchName, func(g *gocui.Gui, v *gocui.View) error {
return gui.pushWithForceFlag(g, v, false, gui.trimmedContent(v))
})
} else if pullables == "0" {
return gui.pushWithForceFlag(g, v, false, "")
}
return gui.createConfirmationPanel(g, nil, true, gui.Tr.SLocalize("ForcePush"), gui.Tr.SLocalize("ForcePushPrompt"), func(g *gocui.Gui, v *gocui.View) error {
return gui.pushWithForceFlag(g, v, true, "")
}, nil) }, nil)
return err
} }
func (gui *Gui) handleSwitchToMerge(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) handleSwitchToMerge(g *gocui.Gui, v *gocui.View) error {
@ -626,7 +634,7 @@ func (gui *Gui) handleCreateResetMenu(g *gocui.Gui, v *gocui.View) error {
} }
func (gui *Gui) handleCustomCommand(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) handleCustomCommand(g *gocui.Gui, v *gocui.View) error {
return gui.createPromptPanel(g, v, gui.Tr.SLocalize("CustomCommand"), func(g *gocui.Gui, v *gocui.View) error { return gui.createPromptPanel(g, v, gui.Tr.SLocalize("CustomCommand"), "", func(g *gocui.Gui, v *gocui.View) error {
command := gui.trimmedContent(v) command := gui.trimmedContent(v)
gui.SubProcess = gui.OSCommand.RunCustomCommand(command) gui.SubProcess = gui.OSCommand.RunCustomCommand(command)
return gui.Errors.ErrSubProcess return gui.Errors.ErrSubProcess

View File

@ -139,7 +139,7 @@ func (gui *Gui) handleStashSave(stashFunc func(message string) error) error {
if len(gui.trackedFiles()) == 0 && len(gui.stagedFiles()) == 0 { if len(gui.trackedFiles()) == 0 && len(gui.stagedFiles()) == 0 {
return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("NoTrackedStagedFilesStash")) return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("NoTrackedStagedFilesStash"))
} }
return gui.createPromptPanel(gui.g, gui.getFilesView(), gui.Tr.SLocalize("StashChanges"), func(g *gocui.Gui, v *gocui.View) error { return gui.createPromptPanel(gui.g, gui.getFilesView(), gui.Tr.SLocalize("StashChanges"), "", func(g *gocui.Gui, v *gocui.View) error {
if err := stashFunc(gui.trimmedContent(v)); err != nil { if err := stashFunc(gui.trimmedContent(v)); err != nil {
gui.createErrorPanel(g, err.Error()) gui.createErrorPanel(g, err.Error())
} }

View File

@ -757,6 +757,9 @@ func addDutch(i18nObject *i18n.Bundle) error {
}, &i18n.Message{ }, &i18n.Message{
ID: "ExitLineByLineMode", ID: "ExitLineByLineMode",
Other: `exit line-by-line mode`, Other: `exit line-by-line mode`,
}, &i18n.Message{
ID: "EnterUpstream",
Other: `Enter upstream as '<remote> <branchname>'`,
}, },
) )
} }

View File

@ -834,6 +834,9 @@ func addEnglish(i18nObject *i18n.Bundle) error {
}, &i18n.Message{ }, &i18n.Message{
ID: "ExitLineByLineMode", ID: "ExitLineByLineMode",
Other: `exit line-by-line mode`, Other: `exit line-by-line mode`,
}, &i18n.Message{
ID: "EnterUpstream",
Other: `Enter upstream as '<remote> <branchname>'`,
}, },
) )
} }

View File

@ -740,6 +740,9 @@ func addPolish(i18nObject *i18n.Bundle) error {
}, &i18n.Message{ }, &i18n.Message{
ID: "ExitLineByLineMode", ID: "ExitLineByLineMode",
Other: `exit line-by-line mode`, Other: `exit line-by-line mode`,
}, &i18n.Message{
ID: "EnterUpstream",
Other: `Enter upstream as '<remote> <branchname>'`,
}, },
) )
} }