mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-20 02:08:47 +00:00
Git - refactor getting base of a branch (#193973)
This commit is contained in:
@@ -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> {
|
||||||
|
|||||||
@@ -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');
|
||||||
|
|||||||
Reference in New Issue
Block a user