diff --git a/src/git/remoteNameAndBranch.test.ts b/src/git/remoteNameAndBranch.test.ts index d7b78085..2f5bbf83 100644 --- a/src/git/remoteNameAndBranch.test.ts +++ b/src/git/remoteNameAndBranch.test.ts @@ -62,12 +62,22 @@ describe('git', () => { assert.strictEqual(branch, 'feature', 'incorrect branch name') }) - it('falls back to first remote and branch when branch is not pushed to upstream', async () => { + it('falls back to first remote and upstream branch when branch does not match upstream', async () => { const { remoteName, branch } = await gitRemoteNameAndBranch( '', createMockGitHelpers(['origin'], 'feature', 'origin/master') ) + assert.strictEqual(remoteName, 'origin', 'incorrect remote name') + assert.strictEqual(branch, 'master', 'incorrect branch name') + }) + + it('falls back to first remote and branch when remote does not match upstream', async () => { + const { remoteName, branch } = await gitRemoteNameAndBranch( + '', + createMockGitHelpers(['origin'], 'feature', 'upstream/master') + ) + assert.strictEqual(remoteName, 'origin', 'incorrect remote name') assert.strictEqual(branch, 'feature', 'incorrect branch name') }) diff --git a/src/git/remoteNameAndBranch.ts b/src/git/remoteNameAndBranch.ts index 36a0aaa2..5b781103 100644 --- a/src/git/remoteNameAndBranch.ts +++ b/src/git/remoteNameAndBranch.ts @@ -31,13 +31,15 @@ export async function gitRemoteNameAndBranch( } ): Promise { let remoteName: string | undefined + let branch: string | undefined + let upstreamAndBranch: string | undefined // Used to determine which part of upstreamAndBranch is the remote name, or as fallback if no upstream is set const remotes = await git.remotes(repoDirectory) - const branch = await git.branch(repoDirectory) + branch = await git.branch(repoDirectory) try { - const upstreamAndBranch = await git.upstreamAndBranch(repoDirectory) + upstreamAndBranch = await git.upstreamAndBranch(repoDirectory) // Subtract $BRANCH_NAME from $UPSTREAM_REMOTE/$BRANCH_NAME. // We can't just split on the delineating `/`, since refnames can include `/`: // https://sourcegraph.com/github.com/git/git@454cb6bd52a4de614a3633e4f547af03d5c3b640/-/blob/refs.c#L52-67 @@ -62,6 +64,18 @@ export async function gitRemoteNameAndBranch( log?.appendLine(`no upstream found, using first git remote: ${remotes[0]}`) } remoteName = remotes[0] + if (upstreamAndBranch && upstreamAndBranch !== remoteName) { + try { + const remotePosition = upstreamAndBranch.lastIndexOf(remoteName) + const maybeBranch = upstreamAndBranch.slice(remoteName.length + 1, upstreamAndBranch.length) + + if (remotePosition === 0 && upstreamAndBranch !== remoteName && maybeBranch) { + branch = maybeBranch + } + } catch { + // noop. Fallback on local branch name. + } + } } // Throw if a remote still isn't found