@@ -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