Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit acaf96a

Browse files
committed
fix: fallback to upstream branch name when remote does not match branch
1 parent eddb761 commit acaf96a

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/git/remoteNameAndBranch.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,22 @@ describe('git', () => {
6262
assert.strictEqual(branch, 'feature', 'incorrect branch name')
6363
})
6464

65-
it('falls back to first remote and branch when branch is not pushed to upstream', async () => {
65+
it('falls back to first remote and upstream branch when branch does not match upstream', async () => {
6666
const { remoteName, branch } = await gitRemoteNameAndBranch(
6767
'',
6868
createMockGitHelpers(['origin'], 'feature', 'origin/master')
6969
)
7070

71+
assert.strictEqual(remoteName, 'origin', 'incorrect remote name')
72+
assert.strictEqual(branch, 'master', 'incorrect branch name')
73+
})
74+
75+
it('falls back to first remote and branch when remote does not match upstream', async () => {
76+
const { remoteName, branch } = await gitRemoteNameAndBranch(
77+
'',
78+
createMockGitHelpers(['origin'], 'feature', 'upstream/master')
79+
)
80+
7181
assert.strictEqual(remoteName, 'origin', 'incorrect remote name')
7282
assert.strictEqual(branch, 'feature', 'incorrect branch name')
7383
})

src/git/remoteNameAndBranch.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ export async function gitRemoteNameAndBranch(
3131
}
3232
): Promise<RemoteName & Branch> {
3333
let remoteName: string | undefined
34+
let branch: string | undefined
35+
let upstreamAndBranch: string | undefined
3436

3537
// Used to determine which part of upstreamAndBranch is the remote name, or as fallback if no upstream is set
3638
const remotes = await git.remotes(repoDirectory)
37-
const branch = await git.branch(repoDirectory)
39+
branch = await git.branch(repoDirectory)
40+
3841

3942
try {
40-
const upstreamAndBranch = await git.upstreamAndBranch(repoDirectory)
43+
upstreamAndBranch = await git.upstreamAndBranch(repoDirectory)
4144
// Subtract $BRANCH_NAME from $UPSTREAM_REMOTE/$BRANCH_NAME.
4245
// We can't just split on the delineating `/`, since refnames can include `/`:
4346
// https://sourcegraph.com/github.com/git/git@454cb6bd52a4de614a3633e4f547af03d5c3b640/-/blob/refs.c#L52-67
@@ -62,6 +65,19 @@ export async function gitRemoteNameAndBranch(
6265
log?.appendLine(`no upstream found, using first git remote: ${remotes[0]}`)
6366
}
6467
remoteName = remotes[0]
68+
if (upstreamAndBranch && upstreamAndBranch !== remoteName){
69+
try {
70+
const remotePosition = upstreamAndBranch.lastIndexOf(remoteName)
71+
const maybeBranch = upstreamAndBranch.slice(remoteName.length + 1, upstreamAndBranch.length)
72+
73+
if (remotePosition === 0 && upstreamAndBranch !== remoteName && maybeBranch) {
74+
branch = maybeBranch
75+
}
76+
} catch {
77+
// noop. Fallback on local branch name.
78+
}
79+
}
80+
6581
}
6682

6783
// Throw if a remote still isn't found

0 commit comments

Comments
 (0)