Skip to content

Commit

Permalink
Fixes repo etag checks in Home
Browse files Browse the repository at this point in the history
  • Loading branch information
d13 committed Jan 28, 2025
1 parent 55afdfe commit 0a149d3
Showing 1 changed file with 55 additions and 17 deletions.
72 changes: 55 additions & 17 deletions src/webviews/home/homeWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type { Issue } from '../../git/models/issue';
import type { GitPausedOperationStatus } from '../../git/models/pausedOperationStatus';
import type { PullRequest } from '../../git/models/pullRequest';
import { RemoteResourceType } from '../../git/models/remoteResource';
import type { Repository } from '../../git/models/repository';
import type { Repository, RepositoryFileSystemChangeEvent } from '../../git/models/repository';
import { RepositoryChange, RepositoryChangeComparisonMode } from '../../git/models/repository';
import { uncommitted } from '../../git/models/revision';
import type { GitStatus } from '../../git/models/status';
Expand Down Expand Up @@ -141,6 +141,8 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
private readonly _disposable: Disposable;
private _discovering: Promise<number | undefined> | undefined;
private _etag?: number;
private _etagFileSystem?: number;
private _etagRepository?: number;
private _etagSubscription?: number;
private _pendingFocusAccount = false;

Expand Down Expand Up @@ -396,6 +398,21 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
}
}

private hasRepositoryChanged(): boolean {
if (this._repositorySubscription?.repo != null) {
if (
this._repositorySubscription.repo.etag !== this._etagRepository ||
this._repositorySubscription.repo.etagFileSystem !== this._etagFileSystem
) {
return true;
}
} else if (this._etag !== this.container.git.etag) {
return true;
}

return false;
}

onVisibilityChanged(visible: boolean): void {
if (!visible) {
this.stopRepositorySubscription();
Expand All @@ -405,7 +422,10 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe

this.resumeRepositorySubscription();

if (this._discovering == null && this._etag !== this.container.git.etag) {
if (
this._discovering == null &&
(this.container.subscription.etag !== this._etagSubscription || this.hasRepositoryChanged())
) {
this.notifyDidChangeRepositories(true);
}
}
Expand Down Expand Up @@ -638,7 +658,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
await this.notifyDidChangeSubscription(e.current);

if (isSubscriptionStatePaidOrTrial(e.current.state) !== isSubscriptionStatePaidOrTrial(e.previous.state)) {
this.onOverviewRepoChanged('repo');
this.onOverviewRepoChanged();
}
}

Expand Down Expand Up @@ -702,7 +722,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
if (repo == null) return undefined;

const forceRepo = this._invalidateOverview === 'repo';
const forceWip = this._invalidateOverview !== undefined;
const forceWip = this._invalidateOverview === 'wip';
const branchesAndWorktrees = await this.getBranchesData(repo, forceRepo);

const { branches, worktreesByBranch } = branchesAndWorktrees;
Expand All @@ -718,15 +738,16 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
this.container,
{
isActive: true,
forceStatus: forceWip ? true : undefined,
forceStatus: forceRepo || forceWip ? true : undefined,
},
);

// TODO: revisit invalidation
if (!forceRepo && forceWip) {
if (forceWip) {
this._invalidateOverview = undefined;
}

this._etagFileSystem = repo.etagFileSystem;

return {
repository: await this.formatRepository(repo),
active: activeOverviewBranch,
Expand Down Expand Up @@ -881,7 +902,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
return Disposable.from(
// TODO: advanced configuration for the watchFileSystem timing
repo.watchFileSystem(1000),
repo.onDidChangeFileSystem(() => this.onOverviewRepoChanged('wip')),
repo.onDidChangeFileSystem(e => this.onOverviewWipChanged(e, repo)),
repo.onDidChange(e => {
if (
e.changed(
Expand All @@ -895,26 +916,42 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
RepositoryChangeComparisonMode.Any,
)
) {
this.onOverviewRepoChanged('repo');
this.onOverviewRepoChanged(repo);
}
}),
);
}

@debug()
private onOverviewRepoChanged(scope: 'repo' | 'wip') {
if (this._etag === this.container.git.etag) return;
private onOverviewWipChanged(e: RepositoryFileSystemChangeEvent, repository: Repository) {
if (e.repository?.path !== repository.path) return;
if (this._etagFileSystem === repository.etagFileSystem) return;

// if the repo is already marked invalid, we already need to recompute the whole overview
if (this._invalidateOverview !== 'repo') {
this._invalidateOverview = scope;
this._invalidateOverview = 'wip';
}

if (!this.host.visible) return;

if (scope === 'wip') {
void this.host.notify(DidChangeRepositoryWip, undefined);
} else {
this.notifyDidChangeRepositories();
void this.host.notify(DidChangeRepositoryWip, undefined);
}

@debug()
private onOverviewRepoChanged(repo?: Repository) {
if (repo != null) {
if (this._etagRepository === repo.etag) {
return;
}
} else if (this._etag === this.container.git.etag) {
return;
}

this._invalidateOverview = 'repo';

if (!this.host.visible) return;

this.notifyDidChangeRepositories();
}

private getSelectedRepository() {
Expand All @@ -928,7 +965,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
private _invalidateOverview: 'repo' | 'wip' | undefined;
private readonly _repositoryBranches: Map<string, RepositoryBranchData> = new Map();
private async getBranchesData(repo: Repository, force = false) {
if (force || !this._repositoryBranches.has(repo.path)) {
if (force || !this._repositoryBranches.has(repo.path) || repo.etag !== this._etagRepository) {
const worktrees = (await repo.git.worktrees()?.getWorktrees()) ?? [];
const worktreesByBranch = groupWorktreesByBranch(worktrees, { includeDefault: true });
const [branchesResult] = await Promise.allSettled([
Expand All @@ -939,6 +976,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
]);

const branches = getSettledValue(branchesResult)?.values ?? [];
this._etagRepository = repo.etag;

this._repositoryBranches.set(repo.path, {
repo: repo,
Expand Down

0 comments on commit 0a149d3

Please sign in to comment.