mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 04:49:35 +00:00
Git - refactor getting base of a branch (#193973)
This commit is contained in:
parent
79a3ef5a7d
commit
33ba908ab4
|
@ -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> {
|
||||
|
|
|
@ -1439,10 +1439,6 @@ export class Repository implements Disposable {
|
|||
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> {
|
||||
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[]> {
|
||||
const config = workspace.getConfiguration('git');
|
||||
let defaultSort = config.get<'alphabetically' | 'committerdate'>('branchSortOrder');
|
||||
|
|
Loading…
Reference in a new issue