Skip to content

Commit 697936c

Browse files
authored
Git - update getBranchBase so that it returns an upstream branch (microsoft#202586)
1 parent 62cb62c commit 697936c

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

extensions/git/src/repository.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,33 +1475,35 @@ export class Repository implements Disposable {
14751475

14761476
async getBranchBase(ref: string): Promise<Branch | undefined> {
14771477
const branch = await this.getBranch(ref);
1478-
const branchMergeBaseConfigKey = `branch.${branch.name}.vscode-merge-base`;
1478+
const branchUpstream = await this.getUpstreamBranch(branch);
14791479

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;
14831482
}
14841483

14851484
// Git config
1485+
const mergeBaseConfigKey = `branch.${branch.name}.vscode-merge-base`;
1486+
14861487
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;
14911492
}
14921493
} catch (err) { }
14931494

14941495
// Reflog
14951496
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;
14991501
}
15001502

15011503
// Default branch
15021504
const defaultBranch = await this.getDefaultBranch();
15031505
if (defaultBranch) {
1504-
await this.setConfig(branchMergeBaseConfigKey, defaultBranch.name!);
1506+
await this.setConfig(mergeBaseConfigKey, `${defaultBranch.remote}/${defaultBranch.name}`);
15051507
return defaultBranch;
15061508
}
15071509

@@ -1552,6 +1554,21 @@ export class Repository implements Disposable {
15521554
return undefined;
15531555
}
15541556

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+
15551572
async getRefs(query: RefQuery = {}, cancellationToken?: CancellationToken): Promise<Ref[]> {
15561573
const config = workspace.getConfiguration('git');
15571574
let defaultSort = config.get<'alphabetically' | 'committerdate'>('branchSortOrder');

0 commit comments

Comments
 (0)