From 30e9fd987907ba7786aa8590dc8f9a58c0b77895 Mon Sep 17 00:00:00 2001 From: Thomas Petersen Date: Mon, 6 Jul 2026 13:40:35 +0200 Subject: [PATCH] feat(desktop): prefer local checkouts for overview repo snapshot scan MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../src/features/projects/ui/ProjectsView.tsx | 5 +- .../projects/useProjectsRepoSnapshots.ts | 66 +++++++++++++++---- 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/desktop/src/features/projects/ui/ProjectsView.tsx b/desktop/src/features/projects/ui/ProjectsView.tsx index 36d535aa0e..be8a06068c 100644 --- a/desktop/src/features/projects/ui/ProjectsView.tsx +++ b/desktop/src/features/projects/ui/ProjectsView.tsx @@ -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(() => readStoredViewMode()); const [sort, setSort] = React.useState(() => readStoredSort()); diff --git a/desktop/src/features/projects/useProjectsRepoSnapshots.ts b/desktop/src/features/projects/useProjectsRepoSnapshots.ts index 9bc4f93b41..3ad711a807 100644 --- a/desktop/src/features/projects/useProjectsRepoSnapshots.ts +++ b/desktop/src/features/projects/useProjectsRepoSnapshots.ts @@ -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 { + 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> { const snapshots: Record = {}; 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, });