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

Fix: Fallback to upstream branch name when remote does not match branch #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/git/remoteNameAndBranch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down
18 changes: 16 additions & 2 deletions src/git/remoteNameAndBranch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ export async function gitRemoteNameAndBranch(
}
): Promise<RemoteName & Branch> {
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
Expand All @@ -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
Expand Down