@@ -1475,33 +1475,35 @@ export class Repository implements Disposable {
1475
1475
1476
1476
async getBranchBase ( ref : string ) : Promise < Branch | undefined > {
1477
1477
const branch = await this . getBranch ( ref ) ;
1478
- const branchMergeBaseConfigKey = `branch. ${ branch . name } .vscode-merge-base` ;
1478
+ const branchUpstream = await this . getUpstreamBranch ( branch ) ;
1479
1479
1480
- // Upstream
1481
- if ( branch . upstream ) {
1482
- return await this . getBranch ( `refs/remotes/${ branch . upstream . remote } /${ branch . upstream . name } ` ) ;
1480
+ if ( branchUpstream ) {
1481
+ return branchUpstream ;
1483
1482
}
1484
1483
1485
1484
// Git config
1485
+ const mergeBaseConfigKey = `branch.${ branch . name } .vscode-merge-base` ;
1486
+
1486
1487
try {
1487
- const mergeBase = await this . getConfig ( branchMergeBaseConfigKey ) ;
1488
- if ( mergeBase !== '' ) {
1489
- const mergeBaseBranch = await this . getBranch ( mergeBase ) ;
1490
- return mergeBaseBranch ;
1488
+ const mergeBase = await this . getConfig ( mergeBaseConfigKey ) ;
1489
+ const branchFromConfig = mergeBase !== '' ? await this . getBranch ( mergeBase ) : undefined ;
1490
+ if ( branchFromConfig ) {
1491
+ return branchFromConfig ;
1491
1492
}
1492
1493
} catch ( err ) { }
1493
1494
1494
1495
// Reflog
1495
1496
const branchFromReflog = await this . getBranchBaseFromReflog ( ref ) ;
1496
- if ( branchFromReflog ) {
1497
- await this . setConfig ( branchMergeBaseConfigKey , branchFromReflog . name ! ) ;
1498
- return branchFromReflog ;
1497
+ const branchFromReflogUpstream = branchFromReflog ? await this . getUpstreamBranch ( branchFromReflog ) : undefined ;
1498
+ if ( branchFromReflogUpstream ) {
1499
+ await this . setConfig ( mergeBaseConfigKey , `${ branchFromReflogUpstream . remote } /${ branchFromReflogUpstream . name } ` ) ;
1500
+ return branchFromReflogUpstream ;
1499
1501
}
1500
1502
1501
1503
// Default branch
1502
1504
const defaultBranch = await this . getDefaultBranch ( ) ;
1503
1505
if ( defaultBranch ) {
1504
- await this . setConfig ( branchMergeBaseConfigKey , defaultBranch . name ! ) ;
1506
+ await this . setConfig ( mergeBaseConfigKey , ` ${ defaultBranch . remote } / ${ defaultBranch . name } ` ) ;
1505
1507
return defaultBranch ;
1506
1508
}
1507
1509
@@ -1552,6 +1554,21 @@ export class Repository implements Disposable {
1552
1554
return undefined ;
1553
1555
}
1554
1556
1557
+ private async getUpstreamBranch ( branch : Branch ) : Promise < Branch | undefined > {
1558
+ if ( ! branch . upstream ) {
1559
+ return undefined ;
1560
+ }
1561
+
1562
+ try {
1563
+ const upstreamBranch = await this . getBranch ( `refs/remotes/${ branch . upstream . remote } /${ branch . upstream . name } ` ) ;
1564
+ return upstreamBranch ;
1565
+ }
1566
+ catch ( err ) {
1567
+ this . logger . warn ( `Failed to get branch details for 'refs/remotes/${ branch . upstream . remote } /${ branch . upstream . name } ': ${ err . message } .` ) ;
1568
+ return undefined ;
1569
+ }
1570
+ }
1571
+
1555
1572
async getRefs ( query : RefQuery = { } , cancellationToken ?: CancellationToken ) : Promise < Ref [ ] > {
1556
1573
const config = workspace . getConfiguration ( 'git' ) ;
1557
1574
let defaultSort = config . get < 'alphabetically' | 'committerdate' > ( 'branchSortOrder' ) ;
0 commit comments