@@ -1266,32 +1266,15 @@ export class Git {
12661266 return [ ex . stdout , undefined ] ;
12671267 }
12681268
1269+ let data ;
12691270 try {
1270- const data = await this . exec ( { cwd : repoPath } , 'symbolic-ref' , '--short' , 'HEAD' ) ;
1271+ data = await this . exec ( { cwd : repoPath } , 'symbolic-ref' , '--short' , 'HEAD' ) ;
12711272 if ( data != null ) return [ data . trim ( ) , undefined ] ;
12721273 } catch { }
12731274
1274- try {
1275- const data = await this . exec (
1276- { cwd : repoPath } ,
1277- 'symbolic-ref' ,
1278- '--short' ,
1279- 'refs/remotes/origin/HEAD' ,
1280- ) ;
1281- if ( data != null ) return [ data . trim ( ) . substring ( 'origin/' . length ) , undefined ] ;
1282- } catch ( ex ) {
1283- if ( / i s n o t a s y m b o l i c r e f / . test ( ex . stderr ) ) {
1284- try {
1285- const data = await this . exec ( { cwd : repoPath } , 'ls-remote' , '--symref' , 'origin' , 'HEAD' ) ;
1286- if ( data != null ) {
1287- const match = / r e f : \s ( \S + ) \s + H E A D / m. exec ( data ) ;
1288- if ( match != null ) {
1289- const [ , branch ] = match ;
1290- return [ branch . substring ( 'refs/heads/' . length ) , undefined ] ;
1291- }
1292- }
1293- } catch { }
1294- }
1275+ data = await this . symbolic_ref__HEAD ( repoPath , 'origin' ) ;
1276+ if ( data != null ) {
1277+ return [ data . startsWith ( 'origin/' ) ? data . substring ( 'origin/' . length ) : data , undefined ] ;
12951278 }
12961279
12971280 const defaultBranch = ( await this . config__get ( 'init.defaultBranch' , repoPath ) ) ?? 'main' ;
@@ -1337,6 +1320,42 @@ export class Git {
13371320 }
13381321 }
13391322
1323+ async symbolic_ref__HEAD ( repoPath : string , remote : string ) : Promise < string | undefined > {
1324+ let retried = false ;
1325+ while ( true ) {
1326+ try {
1327+ const data = await this . exec (
1328+ { cwd : repoPath } ,
1329+ 'symbolic-ref' ,
1330+ '--short' ,
1331+ `refs/remotes/${ remote } /HEAD` ,
1332+ ) ;
1333+ return data ?. trim ( ) || undefined ;
1334+ } catch ( ex ) {
1335+ if ( / i s n o t a s y m b o l i c r e f / . test ( ex . stderr ) ) {
1336+ try {
1337+ if ( ! retried ) {
1338+ retried = true ;
1339+ await this . exec ( { cwd : repoPath } , 'remote' , 'set-head' , '-a' , remote ) ;
1340+ continue ;
1341+ }
1342+
1343+ const data = await this . exec ( { cwd : repoPath } , 'ls-remote' , '--symref' , remote , 'HEAD' ) ;
1344+ if ( data != null ) {
1345+ const match = / r e f : \s ( \S + ) \s + H E A D / m. exec ( data ) ;
1346+ if ( match != null ) {
1347+ const [ , branch ] = match ;
1348+ return `${ remote } /${ branch . substring ( 'refs/heads/' . length ) . trim ( ) } ` ;
1349+ }
1350+ }
1351+ } catch { }
1352+ }
1353+
1354+ return undefined ;
1355+ }
1356+ }
1357+ }
1358+
13401359 async rev_parse__git_dir ( cwd : string ) : Promise < { path : string ; commonPath ?: string } | undefined > {
13411360 const data = await this . exec (
13421361 { cwd : cwd , errors : GitErrorHandling . Ignore } ,
0 commit comments