Skip to content

Commit 079f67e

Browse files
committed
Reduces repeat branch lookups
1 parent 70cf3be commit 079f67e

File tree

1 file changed

+87
-56
lines changed

1 file changed

+87
-56
lines changed

src/webviews/home/homeWebview.ts

Lines changed: 87 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import type { ContextKeys } from '../../constants.context';
55
import type { WebviewTelemetryContext } from '../../constants.telemetry';
66
import type { Container } from '../../container';
77
import type { BranchContributorOverview } from '../../git/gitProvider';
8+
import type { GitBranch } from '../../git/models/branch';
89
import { sortBranches } from '../../git/models/branch';
910
import type { PullRequest } from '../../git/models/pullRequest';
1011
import type { Repository } from '../../git/models/repository';
1112
import { RepositoryChange, RepositoryChangeComparisonMode } from '../../git/models/repository';
1213
import type { GitStatus } from '../../git/models/status';
14+
import type { GitWorktree } from '../../git/models/worktree';
1315
import { getOpenedWorktreesByBranch, groupWorktreesByBranch } from '../../git/models/worktree';
1416
import type { Subscription } from '../../plus/gk/account/subscription';
1517
import type { SubscriptionChangeEvent } from '../../plus/gk/account/subscriptionService';
@@ -269,7 +271,23 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
269271
const repo = this.getSelectedRepository();
270272
if (repo == null) return undefined;
271273

272-
return getBranchOverview(repo, this.container);
274+
const branchesAndWorktrees = await this.getBranchesAndWorktrees(repo);
275+
const overviewBranches = await getOverviewBranches(
276+
branchesAndWorktrees?.branches,
277+
branchesAndWorktrees?.worktrees,
278+
this.container,
279+
// TODO: add filters
280+
);
281+
if (overviewBranches == null) return undefined;
282+
283+
const result: GetOverviewResponse = {
284+
repository: {
285+
name: repo.name,
286+
branches: overviewBranches,
287+
},
288+
};
289+
290+
return result;
273291
}
274292

275293
private _repositorySubscription: RepositorySubscription | undefined;
@@ -322,6 +340,22 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
322340
return this._repositorySubscription?.repo;
323341
}
324342

343+
private _repositoryBranches: Map<string, { branches: GitBranch[]; worktrees: GitWorktree[] }> = new Map();
344+
private async getBranchesAndWorktrees(repo: Repository, force = false) {
345+
if (force || !this._repositoryBranches.has(repo.path)) {
346+
const [branchesResult, worktreesResult] = await Promise.allSettled([
347+
repo.git.getBranches({ filter: b => !b.remote }),
348+
repo.git.getWorktrees(),
349+
]);
350+
351+
const branches = getSettledValue(branchesResult)?.values ?? [];
352+
const worktrees = getSettledValue(worktreesResult) ?? [];
353+
this._repositoryBranches.set(repo.path, { branches: branches, worktrees: worktrees });
354+
}
355+
356+
return this._repositoryBranches.get(repo.path)!;
357+
}
358+
325359
private _hostedIntegrationConnected: boolean | undefined;
326360
private isAnyIntegrationConnected(force = false) {
327361
if (this._hostedIntegrationConnected == null || force === true) {
@@ -403,22 +437,19 @@ interface BranchOverviewOptions {
403437
threshold: number;
404438
};
405439
stale?: {
406-
threshold: number;
440+
show?: boolean;
441+
threshold?: number;
407442
};
408443
}
409444

410-
async function getBranchOverview(
411-
repo: Repository,
445+
async function getOverviewBranches(
446+
branches: GitBranch[],
447+
worktrees: GitWorktree[],
412448
container: Container,
413449
options?: BranchOverviewOptions,
414-
): Promise<GetOverviewResponse | undefined> {
415-
const [branchesResult, worktreesResult] = await Promise.allSettled([
416-
repo.git.getBranches({ filter: b => !b.remote }),
417-
repo.git.getWorktrees(),
418-
]);
450+
): Promise<GetOverviewBranches | undefined> {
451+
if (branches.length === 0) return undefined;
419452

420-
const branches = getSettledValue(branchesResult)?.values ?? [];
421-
const worktrees = getSettledValue(worktreesResult) ?? [];
422453
const worktreesByBranch = groupWorktreesByBranch(worktrees);
423454

424455
sortBranches(branches, {
@@ -439,7 +470,6 @@ async function getBranchOverview(
439470

440471
const now = Date.now();
441472
const recentThreshold = now - (options?.recent?.threshold ?? branchOverviewDefaults.recent.threshold);
442-
const staleThreshold = now - (options?.stale?.threshold ?? branchOverviewDefaults.stale.threshold);
443473

444474
for (const branch of branches) {
445475
const wt = worktreesByBranch.get(branch.id);
@@ -489,45 +519,53 @@ async function getBranchOverview(
489519
}
490520
}
491521

492-
sortBranches(branches, {
493-
missingUpstream: true,
494-
orderBy: 'date:asc',
495-
});
496-
for (const branch of branches) {
497-
if (overviewBranches.stale.length > 9) break;
498-
499-
if (
500-
overviewBranches.active.some(b => b.id === branch.id) ||
501-
overviewBranches.recent.some(b => b.id === branch.id)
502-
) {
503-
continue;
504-
}
505-
506-
const timestamp = branch.date?.getTime();
507-
if (branch.upstream?.missing || (timestamp != null && timestamp < staleThreshold)) {
508-
const wt = worktreesByBranch.get(branch.id);
509-
const worktree: GetOverviewBranch['worktree'] = wt ? { name: wt.name, uri: wt.uri.toString() } : undefined;
510-
511-
if (!branch.upstream?.missing) {
512-
prPromises.set(branch.id, branch.getAssociatedPullRequest());
513-
}
514-
if (wt != null) {
515-
statusPromises.set(branch.id, wt.getStatus());
522+
if (options?.stale?.show === true) {
523+
const staleThreshold = now - (options?.stale?.threshold ?? branchOverviewDefaults.stale.threshold);
524+
sortBranches(branches, {
525+
missingUpstream: true,
526+
orderBy: 'date:asc',
527+
});
528+
for (const branch of branches) {
529+
if (overviewBranches.stale.length > 9) break;
530+
531+
if (
532+
overviewBranches.active.some(b => b.id === branch.id) ||
533+
overviewBranches.recent.some(b => b.id === branch.id)
534+
) {
535+
continue;
516536
}
517-
contributorPromises.set(branch.id, container.git.getBranchContributorOverview(branch.repoPath, branch.ref));
518537

519-
overviewBranches.stale.push({
520-
id: branch.id,
521-
name: branch.name,
522-
opened: false,
523-
timestamp: timestamp,
524-
state: branch.state,
525-
status: branch.status,
526-
upstream: branch.upstream,
527-
worktree: worktree,
528-
});
538+
const timestamp = branch.date?.getTime();
539+
if (branch.upstream?.missing || (timestamp != null && timestamp < staleThreshold)) {
540+
const wt = worktreesByBranch.get(branch.id);
541+
const worktree: GetOverviewBranch['worktree'] = wt
542+
? { name: wt.name, uri: wt.uri.toString() }
543+
: undefined;
529544

530-
continue;
545+
if (!branch.upstream?.missing) {
546+
prPromises.set(branch.id, branch.getAssociatedPullRequest());
547+
}
548+
if (wt != null) {
549+
statusPromises.set(branch.id, wt.getStatus());
550+
}
551+
contributorPromises.set(
552+
branch.id,
553+
container.git.getBranchContributorOverview(branch.repoPath, branch.ref),
554+
);
555+
556+
overviewBranches.stale.push({
557+
id: branch.id,
558+
name: branch.name,
559+
opened: false,
560+
timestamp: timestamp,
561+
state: branch.state,
562+
status: branch.status,
563+
upstream: branch.upstream,
564+
worktree: worktree,
565+
});
566+
567+
continue;
568+
}
531569
}
532570
}
533571

@@ -611,12 +649,5 @@ async function getBranchOverview(
611649
}
612650
}
613651

614-
const result: GetOverviewResponse = {
615-
repository: {
616-
name: repo.name,
617-
branches: overviewBranches,
618-
},
619-
};
620-
621-
return result;
652+
return overviewBranches;
622653
}

0 commit comments

Comments
 (0)