@@ -31,13 +31,16 @@ 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 )
40
+
38
41
39
42
try {
40
- const upstreamAndBranch = await git . upstreamAndBranch ( repoDirectory )
43
+ upstreamAndBranch = await git . upstreamAndBranch ( repoDirectory )
41
44
// Subtract $BRANCH_NAME from $UPSTREAM_REMOTE/$BRANCH_NAME.
42
45
// We can't just split on the delineating `/`, since refnames can include `/`:
43
46
// https://sourcegraph.com/github.com/git/git@454cb6bd52a4de614a3633e4f547af03d5c3b640/-/blob/refs.c#L52-67
@@ -62,6 +65,19 @@ export async function gitRemoteNameAndBranch(
62
65
log ?. appendLine ( `no upstream found, using first git remote: ${ remotes [ 0 ] } ` )
63
66
}
64
67
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
+
65
81
}
66
82
67
83
// Throw if a remote still isn't found
0 commit comments