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.
This commit is contained in:
Rūdolfs Ošiņš
2026-07-29 09:36:31 +02:00
parent 2499fe4d72
commit aaffb991ac
4 changed files with 65 additions and 5 deletions
+40
View File
@@ -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<typeof diffStatsSchema>;
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<DiffStats> {
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,
+8
View File
@@ -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");
});
+5 -5
View File
@@ -1,7 +1,7 @@
<script lang="ts">
import type { BaseUrl, Patch, Revision } from "@http-client";
import { cachedGetDiff } from "@app/views/repos/router";
import { cachedGetDiffStats } from "@app/views/repos/router";
import { formatCommit } from "@app/lib/utils";
import DiffStatBadge from "@app/components/DiffStatBadge.svelte";
@@ -14,9 +14,9 @@
export let latestRevision: Revision;
</script>
{#await cachedGetDiff(baseUrl, repoId, latestRevision.base, latestRevision.oid)}
{#await cachedGetDiffStats(baseUrl, repoId, latestRevision.base, latestRevision.oid)}
<Loading small />
{:then { diff }}
{:then stats}
<Link
title="Compare {formatCommit(latestRevision.base)}..{formatCommit(
latestRevision.oid,
@@ -34,7 +34,7 @@
}}>
<DiffStatBadge
hoverable
insertions={diff.stats.insertions}
deletions={diff.stats.deletions} />
insertions={stats.insertions}
deletions={stats.deletions} />
</Link>
{/await}
+12
View File
@@ -283,6 +283,15 @@ export const cachedGetDiff = cached(
{ max: 200 },
);
export const cachedGetDiffStats = cached(
async (baseUrl: BaseUrl, rid: string, base: string, oid: string) => {
const api = new HttpdClient(baseUrl);
return await api.repo.getDiffStats(rid, base, oid);
},
(...args) => JSON.stringify(args),
{ max: 200 },
);
function parseRevisionToOid(
revision: string | undefined,
defaultBranch: string,
@@ -751,6 +760,9 @@ async function loadPatchView(
]);
const latestRevision = patch.revisions.at(-1) as Revision;
// Take the header badge's stats from the latest revision's full diff (the
// same source the changeset view uses) so the two badges on the patch page
// never disagree. The patch list still uses the fast /stats endpoint.
const {
diff: { stats },
} = await cachedGetDiff(