mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-17 00:18:05 +02:00
Fix checking out a different branch while pushing a branch for the first time
When pushing a branch that didn't have an upstream yet, we use the command line git push --set-upstream origin HEAD:branch-name The HEAD: part of this is too unspecific; when checking out a different branch while the push is still running, then git will set the upstream branch on the newly checked out branch, not the branch that was being pushed. This might be considered a bug in git; you might expect that it resolves HEAD at the beginning of the operation, and uses the result at the end. But we can easily work around this by explicitly supplying the real branch name instead of HEAD.
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
package git_commands
|
package git_commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
"github.com/jesseduffield/gocui"
|
"github.com/jesseduffield/gocui"
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
||||||
@ -20,6 +22,7 @@ func NewSyncCommands(gitCommon *GitCommon) *SyncCommands {
|
|||||||
type PushOpts struct {
|
type PushOpts struct {
|
||||||
Force bool
|
Force bool
|
||||||
ForceWithLease bool
|
ForceWithLease bool
|
||||||
|
CurrentBranch string
|
||||||
UpstreamRemote string
|
UpstreamRemote string
|
||||||
UpstreamBranch string
|
UpstreamBranch string
|
||||||
SetUpstream bool
|
SetUpstream bool
|
||||||
@ -35,7 +38,7 @@ func (self *SyncCommands) PushCmdObj(task gocui.Task, opts PushOpts) (oscommands
|
|||||||
ArgIf(opts.ForceWithLease, "--force-with-lease").
|
ArgIf(opts.ForceWithLease, "--force-with-lease").
|
||||||
ArgIf(opts.SetUpstream, "--set-upstream").
|
ArgIf(opts.SetUpstream, "--set-upstream").
|
||||||
ArgIf(opts.UpstreamRemote != "", opts.UpstreamRemote).
|
ArgIf(opts.UpstreamRemote != "", opts.UpstreamRemote).
|
||||||
ArgIf(opts.UpstreamBranch != "", "HEAD:"+opts.UpstreamBranch).
|
ArgIf(opts.UpstreamBranch != "", fmt.Sprintf("refs/heads/%s:%s", opts.CurrentBranch, opts.UpstreamBranch)).
|
||||||
ToArgv()
|
ToArgv()
|
||||||
|
|
||||||
cmdObj := self.cmd.New(cmdArgs).PromptOnCredentialRequest(task)
|
cmdObj := self.cmd.New(cmdArgs).PromptOnCredentialRequest(task)
|
||||||
|
@ -44,11 +44,12 @@ func TestSyncPush(t *testing.T) {
|
|||||||
testName: "Push with force disabled, upstream supplied",
|
testName: "Push with force disabled, upstream supplied",
|
||||||
opts: PushOpts{
|
opts: PushOpts{
|
||||||
ForceWithLease: false,
|
ForceWithLease: false,
|
||||||
|
CurrentBranch: "master",
|
||||||
UpstreamRemote: "origin",
|
UpstreamRemote: "origin",
|
||||||
UpstreamBranch: "master",
|
UpstreamBranch: "master",
|
||||||
},
|
},
|
||||||
test: func(cmdObj oscommands.ICmdObj, err error) {
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
||||||
assert.Equal(t, cmdObj.Args(), []string{"git", "push", "origin", "HEAD:master"})
|
assert.Equal(t, cmdObj.Args(), []string{"git", "push", "origin", "refs/heads/master:master"})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -56,12 +57,13 @@ func TestSyncPush(t *testing.T) {
|
|||||||
testName: "Push with force disabled, setting upstream",
|
testName: "Push with force disabled, setting upstream",
|
||||||
opts: PushOpts{
|
opts: PushOpts{
|
||||||
ForceWithLease: false,
|
ForceWithLease: false,
|
||||||
|
CurrentBranch: "master-local",
|
||||||
UpstreamRemote: "origin",
|
UpstreamRemote: "origin",
|
||||||
UpstreamBranch: "master",
|
UpstreamBranch: "master",
|
||||||
SetUpstream: true,
|
SetUpstream: true,
|
||||||
},
|
},
|
||||||
test: func(cmdObj oscommands.ICmdObj, err error) {
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
||||||
assert.Equal(t, cmdObj.Args(), []string{"git", "push", "--set-upstream", "origin", "HEAD:master"})
|
assert.Equal(t, cmdObj.Args(), []string{"git", "push", "--set-upstream", "origin", "refs/heads/master-local:master"})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -69,12 +71,13 @@ func TestSyncPush(t *testing.T) {
|
|||||||
testName: "Push with force-with-lease enabled, setting upstream",
|
testName: "Push with force-with-lease enabled, setting upstream",
|
||||||
opts: PushOpts{
|
opts: PushOpts{
|
||||||
ForceWithLease: true,
|
ForceWithLease: true,
|
||||||
|
CurrentBranch: "master",
|
||||||
UpstreamRemote: "origin",
|
UpstreamRemote: "origin",
|
||||||
UpstreamBranch: "master",
|
UpstreamBranch: "master",
|
||||||
SetUpstream: true,
|
SetUpstream: true,
|
||||||
},
|
},
|
||||||
test: func(cmdObj oscommands.ICmdObj, err error) {
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
||||||
assert.Equal(t, cmdObj.Args(), []string{"git", "push", "--force-with-lease", "--set-upstream", "origin", "HEAD:master"})
|
assert.Equal(t, cmdObj.Args(), []string{"git", "push", "--force-with-lease", "--set-upstream", "origin", "refs/heads/master:master"})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -200,6 +200,7 @@ func (self *SyncController) pushAux(currentBranch *models.Branch, opts pushOpts)
|
|||||||
git_commands.PushOpts{
|
git_commands.PushOpts{
|
||||||
Force: opts.force,
|
Force: opts.force,
|
||||||
ForceWithLease: opts.forceWithLease,
|
ForceWithLease: opts.forceWithLease,
|
||||||
|
CurrentBranch: currentBranch.Name,
|
||||||
UpstreamRemote: opts.upstreamRemote,
|
UpstreamRemote: opts.upstreamRemote,
|
||||||
UpstreamBranch: opts.upstreamBranch,
|
UpstreamBranch: opts.upstreamBranch,
|
||||||
SetUpstream: opts.setUpstream,
|
SetUpstream: opts.setUpstream,
|
||||||
|
Reference in New Issue
Block a user