@@ -31,13 +31,15 @@ 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 )
3840
3941 try {
40- const upstreamAndBranch = await git . upstreamAndBranch ( repoDirectory )
42+ upstreamAndBranch = await git . upstreamAndBranch ( repoDirectory )
4143 // Subtract $BRANCH_NAME from $UPSTREAM_REMOTE/$BRANCH_NAME.
4244 // We can't just split on the delineating `/`, since refnames can include `/`:
4345 // https://sourcegraph.com/github.com/git/git@454cb6bd52a4de614a3633e4f547af03d5c3b640/-/blob/refs.c#L52-67
@@ -62,6 +64,18 @@ export async function gitRemoteNameAndBranch(
6264 log ?. appendLine ( `no upstream found, using first git remote: ${ remotes [ 0 ] } ` )
6365 }
6466 remoteName = remotes [ 0 ]
67+ if ( upstreamAndBranch && upstreamAndBranch !== remoteName ) {
68+ try {
69+ const remotePosition = upstreamAndBranch . lastIndexOf ( remoteName )
70+ const maybeBranch = upstreamAndBranch . slice ( remoteName . length + 1 , upstreamAndBranch . length )
71+
72+ if ( remotePosition === 0 && upstreamAndBranch !== remoteName && maybeBranch ) {
73+ branch = maybeBranch
74+ }
75+ } catch {
76+ // noop. Fallback on local branch name.
77+ }
78+ }
6579 }
6680
6781 // Throw if a remote still isn't found
0 commit comments