Git - refactor getting base of a branch (#193973)

This commit is contained in:
Ladislau Szomoru
2023-09-25 14:41:47 +02:00
committed by GitHub
parent 79a3ef5a7d
commit 33ba908ab4
2 changed files with 44 additions and 8 deletions

View File

@@ -9,7 +9,7 @@ import { Repository, Resource } from './repository';
import { IDisposable } from './util'; import { IDisposable } from './util';
import { toGitUri } from './uri'; import { toGitUri } from './uri';
import { SyncActionButton } from './actionButton'; import { SyncActionButton } from './actionButton';
import { Status } from './api/git'; import { RefType, Status } from './api/git';
export class GitHistoryProvider implements SourceControlHistoryProvider, FileDecorationProvider, IDisposable { export class GitHistoryProvider implements SourceControlHistoryProvider, FileDecorationProvider, IDisposable {
@@ -142,9 +142,23 @@ export class GitHistoryProvider implements SourceControlHistoryProvider, FileDec
return this.currentHistoryItemGroup.upstream; return this.currentHistoryItemGroup.upstream;
} }
// Default branch // Branch base
const defaultBranch = await this.repository.getDefaultBranch(); const branchBase = await this.repository.getBranchBase(historyItemGroupId);
return defaultBranch.name ? { id: `refs/heads/${defaultBranch.name}`, label: defaultBranch.name } : undefined;
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> { async resolveHistoryItemGroupCommonAncestor(refId1: string, refId2: string): Promise<{ id: string; ahead: number; behind: number } | undefined> {

View File

@@ -1439,10 +1439,6 @@ export class Repository implements Disposable {
await this.run(Operation.Move, () => this.repository.move(from, to)); await this.run(Operation.Move, () => this.repository.move(from, to));
} }
async getDefaultBranch(): Promise<Branch> {
return await this.run(Operation.GetBranch, () => this.repository.getDefaultBranch());
}
async getBranch(name: string): Promise<Branch> { async getBranch(name: string): Promise<Branch> {
return await this.run(Operation.GetBranch, () => this.repository.getBranch(name)); return await this.run(Operation.GetBranch, () => this.repository.getBranch(name));
} }
@@ -1454,6 +1450,32 @@ export class Repository implements Disposable {
}); });
} }
async getBranchBase(ref: string): Promise<Branch | undefined> {
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<Branch | undefined> {
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<Ref[]> { async getRefs(query: RefQuery = {}, cancellationToken?: CancellationToken): Promise<Ref[]> {
const config = workspace.getConfiguration('git'); const config = workspace.getConfiguration('git');
let defaultSort = config.get<'alphabetically' | 'committerdate'>('branchSortOrder'); let defaultSort = config.get<'alphabetically' | 'committerdate'>('branchSortOrder');