From aaffb991ac6be16de7e681b6e4642dd57b6185d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Fri, 17 Jul 2026 11:54:08 +0200 Subject: [PATCH] Use the diff-stats endpoint for patch badges So the badges stop fetching the full diff just for insertions and deletions; the patch view did it on every load. Falls back to the full diff when the backend doesn't serve the new endpoint. --- http-client/lib/repo.ts | 40 ++++++++++++++++++++++ http-client/tests/repo.test.ts | 8 +++++ src/views/repos/DiffStatBadgeLoader.svelte | 10 +++--- src/views/repos/router.ts | 12 +++++++ 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/http-client/lib/repo.ts b/http-client/lib/repo.ts index 217d9097d..82b818c72 100644 --- a/http-client/lib/repo.ts +++ b/http-client/lib/repo.ts @@ -169,6 +169,14 @@ const diffResponseSchema = object({ files: record(string(), diffBlobSchema), }); +const diffStatsSchema = object({ + filesChanged: number(), + insertions: number(), + deletions: number(), +}); + +export type DiffStats = z.infer; + const statusSchema = union([ literal("started"), literal("failed"), @@ -450,6 +458,38 @@ export class Client { ); } + public async getDiffStats( + rid: string, + revisionBase: string, + revisionOid: string, + options?: RequestOptions, + ): Promise { + try { + return await this.#fetcher.fetchOk( + { + method: "GET", + path: `repos/${rid}/diff/${revisionBase}/${revisionOid}/stats`, + options, + }, + diffStatsSchema, + ); + } catch (error) { + // Older nodes don't serve the dedicated diff-stats endpoint, so fall back + // to the full diff, which also carries the stats. + // TODO: Drop this fallback once enough nodes serve /diff/:base/:oid/stats. + if (error instanceof ResponseError && error.status === 404) { + const { diff } = await this.getDiff( + rid, + revisionBase, + revisionOid, + options, + ); + return diff.stats; + } + throw error; + } + } + public async getJobsByCommit( rid: string, commit: string, diff --git a/http-client/tests/repo.test.ts b/http-client/tests/repo.test.ts index 96435309e..7416fbbfb 100644 --- a/http-client/tests/repo.test.ts +++ b/http-client/tests/repo.test.ts @@ -52,6 +52,14 @@ describe("repo", () => { await api.repo.getCommitCountBySha(sourceBrowsingRid, aliceMainHead); }); + test("#getDiffStats(rid, base, oid)", async () => { + await api.repo.getDiffStats( + sourceBrowsingRid, + "90f6d058ece12f75f349bc7bbe88142187fe0379", + aliceMainHead, + ); + }); + test("#getTree(rid, sha, path)", async () => { await api.repo.getTree(sourceBrowsingRid, aliceMainHead, "src"); }); diff --git a/src/views/repos/DiffStatBadgeLoader.svelte b/src/views/repos/DiffStatBadgeLoader.svelte index 8346104ab..9be4a2926 100644 --- a/src/views/repos/DiffStatBadgeLoader.svelte +++ b/src/views/repos/DiffStatBadgeLoader.svelte @@ -1,7 +1,7 @@ -{#await cachedGetDiff(baseUrl, repoId, latestRevision.base, latestRevision.oid)} +{#await cachedGetDiffStats(baseUrl, repoId, latestRevision.base, latestRevision.oid)} -{:then { diff }} +{:then stats}