feat(desktop): prefer local checkouts for overview repo snapshot scan

Remote git storage can be empty or unreachable (staging relay repos have
no refs), leaving the overview language and repository aggregates blank.
Scan local checkouts under the workspace repos dir first — no network,
no clone — and only fall back to a remote blobless clone for projects
without usable local data.
This commit is contained in:
Thomas Petersen
2026-07-06 13:40:35 +02:00
parent cc2e079d3c
commit 30e9fd9879
2 changed files with 57 additions and 14 deletions

View File

@@ -616,7 +616,10 @@ export function ProjectsView() {
() => (filter === "all" ? uniqueRepositories(projects) : []),
[filter, projects],
);
const repoSnapshotsQuery = useProjectsRepoSnapshotsQuery(snapshotProjects);
const repoSnapshotsQuery = useProjectsRepoSnapshotsQuery(
snapshotProjects,
activeWorkspace?.reposDir,
);
const [storedViewMode, setStoredViewMode] =
React.useState<ProjectsViewMode | null>(() => readStoredViewMode());
const [sort, setSort] = React.useState<ProjectsSort>(() => readStoredSort());

View File

@@ -1,16 +1,57 @@
import { useQuery } from "@tanstack/react-query";
import * as React from "react";
import { getProjectRepoSnapshot } from "@/shared/api/projectGit";
import {
getProjectLocalRepoSnapshot,
getProjectRepoSnapshot,
} from "@/shared/api/projectGit";
import type { ProjectRepoSnapshot } from "@/shared/api/types";
import type { Project } from "./hooks";
// Repo snapshots are backed by a blobless `git clone` per repository, so the
// Remote snapshots are backed by a blobless `git clone` per repository, so the
// overview scan is deliberately throttled and cached for a long time.
const OVERVIEW_SNAPSHOT_CONCURRENCY = 3;
function snapshotHasData(snapshot: ProjectRepoSnapshot | null | undefined) {
return Boolean(
snapshot && (snapshot.files.length > 0 || snapshot.latestCommit),
);
}
/**
* Local checkouts are instant (no network, no clone) and keep working when
* the relay's git storage is empty or unreachable, so they are preferred.
* Only repositories without usable local data fall back to a remote clone.
*/
async function fetchProjectSnapshot(
project: Project,
reposDir: string | null | undefined,
): Promise<ProjectRepoSnapshot | null> {
try {
const local = await getProjectLocalRepoSnapshot({
reposDir,
projectDtag: project.dtag,
cloneUrl: project.cloneUrls[0] ?? null,
defaultBranch: project.defaultBranch,
baseBranch: project.defaultBranch,
});
if (snapshotHasData(local?.snapshot)) return local?.snapshot ?? null;
} catch {
// Best-effort: fall through to the remote snapshot.
}
const cloneUrl = project.cloneUrls[0];
if (!cloneUrl) return null;
return getProjectRepoSnapshot({
cloneUrl,
defaultBranch: project.defaultBranch,
baseBranch: project.defaultBranch,
});
}
async function fetchProjectsRepoSnapshots(
projects: Project[],
reposDir: string | null | undefined,
): Promise<Record<string, ProjectRepoSnapshot>> {
const snapshots: Record<string, ProjectRepoSnapshot> = {};
const queue = [...projects];
@@ -21,14 +62,9 @@ async function fetchProjectsRepoSnapshots(
for (;;) {
const project = queue.shift();
if (!project) return;
const cloneUrl = project.cloneUrls[0];
if (!cloneUrl) continue;
try {
snapshots[project.id] = await getProjectRepoSnapshot({
cloneUrl,
defaultBranch: project.defaultBranch,
baseBranch: project.defaultBranch,
});
const snapshot = await fetchProjectSnapshot(project, reposDir);
if (snapshot) snapshots[project.id] = snapshot;
} catch {
// Best-effort: unreachable or empty repositories are skipped.
}
@@ -43,9 +79,13 @@ async function fetchProjectsRepoSnapshots(
/**
* Fetches repo snapshots for a set of projects (throttled, failure-tolerant)
* for workspace-wide aggregates like the overview language breakdown.
* Callers should pre-filter and cap `projects` — one git clone per entry.
* Prefers local checkouts under `reposDir`; falls back to remote clones.
* Callers should pre-filter and cap `projects` — up to one git clone per entry.
*/
export function useProjectsRepoSnapshotsQuery(projects: Project[]) {
export function useProjectsRepoSnapshotsQuery(
projects: Project[],
reposDir?: string | null,
) {
const projectIds = React.useMemo(
() => projects.map((project) => project.id).sort(),
[projects],
@@ -53,8 +93,8 @@ export function useProjectsRepoSnapshotsQuery(projects: Project[]) {
return useQuery({
enabled: projects.length > 0,
queryKey: ["projects", "repo-snapshots", projectIds],
queryFn: () => fetchProjectsRepoSnapshots(projects),
queryKey: ["projects", "repo-snapshots", reposDir ?? "default", projectIds],
queryFn: () => fetchProjectsRepoSnapshots(projects, reposDir),
staleTime: 15 * 60_000,
retry: 0,
});