diff --git a/extensions/git/src/historyProvider.ts b/extensions/git/src/historyProvider.ts index df928afdfc7..04772f79f93 100644 --- a/extensions/git/src/historyProvider.ts +++ b/extensions/git/src/historyProvider.ts @@ -9,7 +9,7 @@ import { Repository, Resource } from './repository'; import { IDisposable } from './util'; import { toGitUri } from './uri'; import { SyncActionButton } from './actionButton'; -import { Status } from './api/git'; +import { RefType, Status } from './api/git'; export class GitHistoryProvider implements SourceControlHistoryProvider, FileDecorationProvider, IDisposable { @@ -142,9 +142,23 @@ export class GitHistoryProvider implements SourceControlHistoryProvider, FileDec return this.currentHistoryItemGroup.upstream; } - // Default branch - const defaultBranch = await this.repository.getDefaultBranch(); - return defaultBranch.name ? { id: `refs/heads/${defaultBranch.name}`, label: defaultBranch.name } : undefined; + // Branch base + const branchBase = await this.repository.getBranchBase(historyItemGroupId); + + if (branchBase?.name && branchBase?.type === RefType.Head) { + return { + id: `refs/heads/${branchBase.name}`, + label: branchBase.name + }; + } + if (branchBase?.name && branchBase.remote && branchBase?.type === RefType.RemoteHead) { + return { + id: `refs/remotes/${branchBase.remote}/${branchBase.name}`, + label: `${branchBase.remote}/${branchBase.name}` + }; + } + + return undefined; } async resolveHistoryItemGroupCommonAncestor(refId1: string, refId2: string): Promise<{ id: string; ahead: number; behind: number } | undefined> { diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index 1065762a3fb..8ae0143f1f2 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -1439,10 +1439,6 @@ export class Repository implements Disposable { await this.run(Operation.Move, () => this.repository.move(from, to)); } - async getDefaultBranch(): Promise { - return await this.run(Operation.GetBranch, () => this.repository.getDefaultBranch()); - } - async getBranch(name: string): Promise { return await this.run(Operation.GetBranch, () => this.repository.getBranch(name)); } @@ -1454,6 +1450,32 @@ export class Repository implements Disposable { }); } + async getBranchBase(ref: string): Promise { + const branch = await this.getBranch(ref); + + // Upstream + if (branch.upstream) { + return this.getBranch(`refs/remotes/${branch.upstream.remote}/${branch.upstream.name}`); + } + + // Default branch + return await this.getDefaultBranch(); + } + + private async getDefaultBranch(): Promise { + try { + const defaultBranchResult = await this.repository.exec(['symbolic-ref', '--short', 'refs/remotes/origin/HEAD']); + if (defaultBranchResult.stdout.trim() === '' || defaultBranchResult.stderr) { + return undefined; + } + + return this.getBranch(defaultBranchResult.stdout.trim()); + } + catch (err) { } + + return undefined; + } + async getRefs(query: RefQuery = {}, cancellationToken?: CancellationToken): Promise { const config = workspace.getConfiguration('git'); let defaultSort = config.get<'alphabetically' | 'committerdate'>('branchSortOrder');