Skip to content

Commit 478887b

Browse files
committed
Improves graph worktree status perf
Fixes getting incorrect stats on comparisons with working tree only Removes duplicate code
1 parent 74e3d65 commit 478887b

File tree

10 files changed

+66
-117
lines changed

10 files changed

+66
-117
lines changed

src/commands/explainCommit.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ export class ExplainCommitCommand extends GlCommandBase {
7373
try {
7474
let commit: GitCommit | undefined;
7575
if (args.rev == null) {
76-
const commitsProvider = repository.git.commits;
77-
const log = await commitsProvider.getLog();
76+
const log = await repository.git.commits.getLog();
7877
const pick = await showCommitPicker(log, 'Explain Commit Changes', 'Choose a commit to explain');
7978
if (pick?.sha == null) return;
8079
args.rev = pick.sha;

src/commands/patches.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,33 +413,33 @@ async function createDraft(repository: Repository, args: CreatePatchCommandArgs)
413413

414414
const create: CreateDraft = { changes: [change], title: args.title, description: args.description };
415415

416-
const commitsProvider = repository.git.commits;
416+
const { git: svc } = repository;
417417

418-
const commit = await commitsProvider.getCommit(to);
418+
const commit = await svc.commits.getCommit(to);
419419
if (commit == null) return undefined;
420420

421421
if (args.from == null) {
422422
if (commit.fileset?.files == null) return;
423423

424424
change.files = [...commit.fileset.files];
425425
} else {
426-
const diff = await repository.git.diff.getDiff?.(to, args.from);
426+
const diff = await svc.diff.getDiff?.(to, args.from);
427427
if (diff == null) return;
428428

429-
const result = await repository.git.diff.getDiffFiles?.(diff.contents);
429+
const result = await svc.diff.getDiffFiles?.(diff.contents);
430430
if (result?.files == null) return;
431431

432432
change.files = result.files;
433433

434434
if (!isSha(args.to)) {
435-
const commit = await commitsProvider.getCommit(args.to);
435+
const commit = await svc.commits.getCommit(args.to);
436436
if (commit != null) {
437437
change.revision.to = commit.sha;
438438
}
439439
}
440440

441441
if (!isSha(args.from)) {
442-
const commit = await commitsProvider.getCommit(args.from);
442+
const commit = await svc.commits.getCommit(args.from);
443443
if (commit != null) {
444444
change.revision.from = commit.sha;
445445
}

src/git/queryResults.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ export async function getAheadBehindFilesQuery(
2828
comparison: string,
2929
compareWithWorkingTree: boolean,
3030
): Promise<FilesQueryResults> {
31-
const diffSvc = container.git.getRepositoryService(repoPath).diff;
32-
const [filesResult, workingFilesResult, statsResult, workingStatsResult] = await Promise.allSettled([
33-
diffSvc.getDiffStatus(comparison),
34-
compareWithWorkingTree ? diffSvc.getDiffStatus('HEAD') : undefined,
35-
diffSvc.getChangedFilesCount(comparison),
36-
compareWithWorkingTree ? diffSvc.getChangedFilesCount('HEAD') : undefined,
31+
const svc = container.git.getRepositoryService(repoPath);
32+
33+
const [filesResult, statsResult, workingFilesResult, workingStatsResult] = await Promise.allSettled([
34+
svc.diff.getDiffStatus(comparison),
35+
svc.diff.getChangedFilesCount(comparison),
36+
compareWithWorkingTree ? svc.diff.getDiffStatus('HEAD') : undefined,
37+
compareWithWorkingTree ? svc.diff.getChangedFilesCount('HEAD') : undefined,
3738
]);
3839

3940
let files = getSettledValue(filesResult) ?? [];
@@ -42,7 +43,7 @@ export async function getAheadBehindFilesQuery(
4243
if (compareWithWorkingTree) {
4344
const workingFiles = getSettledValue(workingFilesResult);
4445
if (workingFiles != null) {
45-
if (files.length === 0) {
46+
if (!files.length) {
4647
files = workingFiles ?? [];
4748
} else {
4849
for (const wf of workingFiles) {
@@ -84,10 +85,10 @@ export function getCommitsQuery(
8485
range: string,
8586
filterByAuthors?: GitUser[] | undefined,
8687
): (limit: number | undefined) => Promise<CommitsQueryResults> {
88+
const svc = container.git.getRepositoryService(repoPath);
89+
8790
return async (limit: number | undefined) => {
88-
const log = await container.git
89-
.getRepositoryService(repoPath)
90-
.commits.getLog(range, { limit: limit, authors: filterByAuthors });
91+
const log = await svc.commits.getLog(range, { limit: limit, authors: filterByAuthors });
9192

9293
const results: Mutable<CommitsQueryResults> = {
9394
log: log,
@@ -120,10 +121,11 @@ export async function getFilesQuery(
120121
comparison = `${ref2}..${ref1}`;
121122
}
122123

123-
const diffSvc = container.git.getRepositoryService(repoPath).diff;
124+
const svc = container.git.getRepositoryService(repoPath);
125+
124126
const [filesResult, statsResult] = await Promise.allSettled([
125-
diffSvc.getDiffStatus(comparison),
126-
diffSvc.getChangedFilesCount(comparison),
127+
svc.diff.getDiffStatus(comparison),
128+
ref1 === '' ? svc.diff.getChangedFilesCount('', comparison) : svc.diff.getChangedFilesCount(comparison),
127129
]);
128130

129131
const files = getSettledValue(filesResult) ?? [];

src/git/utils/-webview/pullRequest.utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ export async function ensurePullRequestRefs(
5757
refs ??= getComparisonRefsForPullRequest(repo.path, pr.refs);
5858
const range = createRevisionRange(refs.base.ref, refs.head.ref, '...');
5959

60-
const commitsProvider = repo.git.commits;
61-
let counts = await commitsProvider.getLeftRightCommitCount(range);
60+
let counts = await repo.git.commits.getLeftRightCommitCount(range);
6261
if (counts == null) {
6362
if (await ensurePullRequestRemote(pr, repo, options)) {
64-
counts = await commitsProvider.getLeftRightCommitCount(range);
63+
counts = await repo.git.commits.getLeftRightCommitCount(range);
6564
}
6665
}
6766

src/git/utils/-webview/worktree.utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ export async function getWorktreesByBranch(
8080
if (repos == null) return worktreesByBranch;
8181

8282
async function addWorktrees(repo: Repository) {
83-
const worktreesProvider = repo.git.worktrees;
84-
if (worktreesProvider == null) return;
83+
if (repo.git.worktrees == null) return;
8584

86-
groupWorktreesByBranch(await worktreesProvider.getWorktrees(cancellation), {
85+
groupWorktreesByBranch(await repo.git.worktrees.getWorktrees(cancellation), {
8786
includeDefault: options?.includeDefault,
8887
worktreesByBranch: worktreesByBranch,
8988
});

src/plus/drafts/draftsService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,10 @@ export class DraftService implements Disposable {
748748
): Promise<ProviderAuth | undefined> {
749749
let integration;
750750
if (isRepository(repoOrIntegrationId)) {
751-
const remoteProvider = await repoOrIntegrationId.git.remotes.getBestRemoteWithIntegration();
752-
if (remoteProvider == null) return undefined;
751+
const remote = await repoOrIntegrationId.git.remotes.getBestRemoteWithIntegration();
752+
if (remote == null) return undefined;
753753

754-
integration = await remoteProvider.getIntegration();
754+
integration = await remote.getIntegration();
755755
} else {
756756
const metadata = providersMetadata[repoOrIntegrationId];
757757
if (metadata == null) return undefined;

src/views/nodes/compareBranchNode.ts

Lines changed: 16 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import type { GitBranch } from '../../git/models/branch';
88
import type { RepositoryFileSystemChangeEvent } from '../../git/models/repository';
99
import type { GitUser } from '../../git/models/user';
1010
import type { CommitsQueryResults, FilesQueryResults } from '../../git/queryResults';
11-
import { getCommitsQuery, getFilesQuery } from '../../git/queryResults';
11+
import { getAheadBehindFilesQuery, getCommitsQuery, getFilesQuery } from '../../git/queryResults';
1212
import { createRevisionRange, shortenRevision } from '../../git/utils/revision.utils';
1313
import { CommandQuickPickItem } from '../../quickpicks/items/common';
1414
import { showReferencePicker } from '../../quickpicks/referencePicker';
1515
import { debug, log } from '../../system/decorators/log';
1616
import { weakEvent } from '../../system/event';
17-
import { getSettledValue } from '../../system/promise';
1817
import { pluralize } from '../../system/string';
1918
import type { ViewsWithBranches } from '../viewBase';
2019
import type { WorktreesView } from '../worktreesView';
@@ -149,11 +148,11 @@ export class CompareBranchNode extends SubscribeableViewNode<
149148
authors: this.filterByAuthors,
150149
});
151150

152-
const refsProvider = this.view.container.git.getRepositoryService(this.repoPath).refs;
151+
const svc = this.view.container.git.getRepositoryService(this.repoPath);
153152
const mergeBase =
154-
(await refsProvider.getMergeBase(behind.ref1, behind.ref2, {
153+
(await svc.refs.getMergeBase(behind.ref1, behind.ref2, {
155154
forkPoint: true,
156-
})) ?? (await refsProvider.getMergeBase(behind.ref1, behind.ref2));
155+
})) ?? (await svc.refs.getMergeBase(behind.ref1, behind.ref2));
157156

158157
const children: ViewNode[] = [
159158
new ResultsCommitsNode(
@@ -338,73 +337,21 @@ export class CompareBranchNode extends SubscribeableViewNode<
338337
}
339338

340339
private async getAheadFilesQuery(): Promise<FilesQueryResults> {
341-
const comparison = createRevisionRange(this._compareWith?.ref || 'HEAD', this.branch.ref || 'HEAD', '...');
342-
343-
const diffProvider = this.view.container.git.getRepositoryService(this.repoPath).diff;
344-
const [filesResult, workingFilesResult, statsResult, workingStatsResult] = await Promise.allSettled([
345-
diffProvider.getDiffStatus(comparison),
346-
this.compareWithWorkingTree ? diffProvider.getDiffStatus('HEAD') : undefined,
347-
diffProvider.getChangedFilesCount(comparison),
348-
this.compareWithWorkingTree ? diffProvider.getChangedFilesCount('HEAD') : undefined,
349-
]);
350-
351-
let files = getSettledValue(filesResult) ?? [];
352-
let stats: FilesQueryResults['stats'] = getSettledValue(statsResult);
353-
354-
if (this.compareWithWorkingTree) {
355-
const workingFiles = getSettledValue(workingFilesResult);
356-
if (workingFiles != null) {
357-
if (files.length === 0) {
358-
files = workingFiles;
359-
} else {
360-
for (const wf of workingFiles) {
361-
const index = files.findIndex(f => f.path === wf.path);
362-
if (index !== -1) {
363-
files.splice(index, 1, wf);
364-
} else {
365-
files.push(wf);
366-
}
367-
}
368-
}
369-
}
370-
371-
const workingStats = getSettledValue(workingStatsResult);
372-
if (workingStats != null) {
373-
if (stats == null) {
374-
stats = workingStats;
375-
} else {
376-
stats = {
377-
additions: stats.additions + workingStats.additions,
378-
deletions: stats.deletions + workingStats.deletions,
379-
files: files.length,
380-
approximated: true,
381-
};
382-
}
383-
}
384-
}
385-
386-
return {
387-
label: `${pluralize('file', files.length, { zero: 'No' })} changed`,
388-
files: files,
389-
stats: stats,
390-
};
340+
return getAheadBehindFilesQuery(
341+
this.view.container,
342+
this.repoPath,
343+
createRevisionRange(this._compareWith?.ref || 'HEAD', this.branch.ref || 'HEAD', '...'),
344+
this.compareWithWorkingTree,
345+
);
391346
}
392347

393348
private async getBehindFilesQuery(): Promise<FilesQueryResults> {
394-
const comparison = createRevisionRange(this.branch.ref, this._compareWith?.ref || 'HEAD', '...');
395-
396-
const diffProvider = this.view.container.git.getRepositoryService(this.repoPath).diff;
397-
const [filesResult, statsResult] = await Promise.allSettled([
398-
diffProvider.getDiffStatus(comparison),
399-
diffProvider.getChangedFilesCount(comparison),
400-
]);
401-
402-
const files = getSettledValue(filesResult) ?? [];
403-
return {
404-
label: `${pluralize('file', files.length, { zero: 'No' })} changed`,
405-
files: files,
406-
stats: getSettledValue(statsResult),
407-
};
349+
return getAheadBehindFilesQuery(
350+
this.view.container,
351+
this.repoPath,
352+
createRevisionRange(this.branch.ref, this._compareWith?.ref || 'HEAD', '...'),
353+
false,
354+
);
408355
}
409356

410357
private getCommitsQuery(range: string): (limit: number | undefined) => Promise<CommitsQueryResults> {

src/views/nodes/compareResultsNode.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,19 @@ export class CompareResultsNode extends SubscribeableViewNode<
140140
};
141141
const behind = { ...this.behind, range: createRevisionRange(this.behind.ref1, this.behind.ref2, '..') };
142142

143-
const counts = await this.view.container.git
144-
.getRepositoryService(this.repoPath)
145-
.commits.getLeftRightCommitCount(createRevisionRange(behind.ref1 || 'HEAD', behind.ref2, '...'), {
143+
const svc = this.view.container.git.getRepositoryService(this.repoPath);
144+
145+
const counts = await svc.commits.getLeftRightCommitCount(
146+
createRevisionRange(behind.ref1 || 'HEAD', behind.ref2, '...'),
147+
{
146148
authors: this.filterByAuthors,
147-
});
149+
},
150+
);
148151

149-
const refsProvider = this.view.container.git.getRepositoryService(this.repoPath).refs;
150152
const mergeBase =
151-
(await refsProvider.getMergeBase(behind.ref1, behind.ref2, {
153+
(await svc.refs.getMergeBase(behind.ref1, behind.ref2, {
152154
forkPoint: true,
153-
})) ?? (await refsProvider.getMergeBase(behind.ref1, behind.ref2));
155+
})) ?? (await svc.refs.getMergeBase(behind.ref1, behind.ref2));
154156

155157
const children: ViewNode[] = [
156158
new ResultsCommitsNode(

src/views/nodes/resultsFilesNode.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,7 @@ export class ResultsFilesNode extends ViewNode<'results-files', ViewsWithCommits
210210
private _filterResults: Promise<void> | undefined;
211211

212212
private async getFilesQueryResults() {
213-
if (this._filesQueryResults === undefined) {
214-
this._filesQueryResults = this._filesQuery();
215-
}
213+
this._filesQueryResults ??= this._filesQuery();
216214

217215
const results = await this._filesQueryResults;
218216
if (
@@ -224,10 +222,7 @@ export class ResultsFilesNode extends ViewNode<'results-files', ViewsWithCommits
224222
return results;
225223
}
226224

227-
if (this._filterResults === undefined) {
228-
this._filterResults = this.filterResults(this.filter, results);
229-
}
230-
225+
this._filterResults ??= this.filterResults(this.filter, results);
231226
await this._filterResults;
232227

233228
return results;

src/webviews/plus/graph/graphWebview.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2465,12 +2465,18 @@ export class GraphWebviewProvider implements WebviewProvider<State, State, Graph
24652465
}
24662466

24672467
private async getWorkingTreeStats(cancellation?: CancellationToken): Promise<GraphWorkingTreeStats | undefined> {
2468-
if (this.repository == null || this.container.git.repositoryCount === 0) return undefined;
2468+
if (this.repository == null || !this.container.git.repositoryCount) return undefined;
24692469

2470-
const statusProvider = this.container.git.getRepositoryService(this.repository.path).status;
2471-
const status = await statusProvider.getStatus(cancellation);
2470+
const svc = this.container.git.getRepositoryService(this.repository.path);
2471+
2472+
const [statusResult, pausedOpStatusResult] = await Promise.allSettled([
2473+
svc.status.getStatus(cancellation),
2474+
svc.status.getPausedOperationStatus?.(cancellation),
2475+
]);
2476+
2477+
const status = getSettledValue(statusResult);
24722478
const workingTreeStatus = status?.getDiffStatus();
2473-
const pausedOpStatus = await statusProvider.getPausedOperationStatus?.(cancellation);
2479+
const pausedOpStatus = getSettledValue(pausedOpStatusResult);
24742480

24752481
return {
24762482
added: workingTreeStatus?.added ?? 0,

0 commit comments

Comments
 (0)