@@ -31,13 +31,15 @@ export async function gitRemoteNameAndBranch(
31
31
}
32
32
) : Promise < RemoteName & Branch > {
33
33
let remoteName : string | undefined
34
+ let branch : string | undefined
35
+ let upstreamAndBranch : string | undefined
34
36
35
37
// Used to determine which part of upstreamAndBranch is the remote name, or as fallback if no upstream is set
36
38
const remotes = await git . remotes ( repoDirectory )
37
- const branch = await git . branch ( repoDirectory )
39
+ branch = await git . branch ( repoDirectory )
38
40
39
41
try {
40
- const upstreamAndBranch = await git . upstreamAndBranch ( repoDirectory )
42
+ upstreamAndBranch = await git . upstreamAndBranch ( repoDirectory )
41
43
// Subtract $BRANCH_NAME from $UPSTREAM_REMOTE/$BRANCH_NAME.
42
44
// We can't just split on the delineating `/`, since refnames can include `/`:
43
45
// https://sourcegraph.com/github.com/git/git@454cb6bd52a4de614a3633e4f547af03d5c3b640/-/blob/refs.c#L52-67
@@ -62,6 +64,18 @@ export async function gitRemoteNameAndBranch(
62
64
log ?. appendLine ( `no upstream found, using first git remote: ${ remotes [ 0 ] } ` )
63
65
}
64
66
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
+ }
65
79
}
66
80
67
81
// Throw if a remote still isn't found
0 commit comments