Use the dedicated commit-count endpoint

So the header and history stop fetching full tree stats just for the
commit count. Falls back to the previous behaviour 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 436085c154
commit faa21f4504
4 changed files with 40 additions and 5 deletions
+31
View File
@@ -1,4 +1,6 @@
import type { Fetcher, RequestOptions } from "./fetcher.js";
import { ResponseError } from "./fetcher.js";
import type { Commit, Commits } from "./repo/commit.js";
import type { Issue } from "./repo/issue.js";
import type { Patch } from "./repo/patch.js";
@@ -136,6 +138,8 @@ const treeStatsSchema = object({
export type TreeStats = z.infer<typeof treeStatsSchema>;
const commitCountSchema = object({ commits: number() });
export type Tree = z.infer<typeof treeSchema>;
const treeSchema = object({
@@ -337,6 +341,33 @@ export class Client {
return tree;
}
public async getCommitCountBySha(
rid: string,
sha: string,
options?: RequestOptions,
): Promise<number> {
try {
const { commits } = await this.#fetcher.fetchOk(
{
method: "GET",
path: `repos/${rid}/stats/commits/${sha}`,
options,
},
commitCountSchema,
);
return commits;
} catch (error) {
// Older nodes don't serve the dedicated commit-count endpoint, so fall
// back to the tree stats endpoint, which also returns the commit count.
// TODO: Drop this fallback once enough nodes serve /stats/commits.
if (error instanceof ResponseError && error.status === 404) {
const stats = await this.getTreeStatsBySha(rid, sha, options);
return stats.commits;
}
throw error;
}
}
public async getAllRemotes(
rid: string,
options?: RequestOptions,
+4
View File
@@ -48,6 +48,10 @@ describe("repo", () => {
await api.repo.getTreeStatsBySha(sourceBrowsingRid, aliceMainHead);
});
test("#getCommitCountBySha(rid, sha)", async () => {
await api.repo.getCommitCountBySha(sourceBrowsingRid, aliceMainHead);
});
test("#getTree(rid, sha, path)", async () => {
await api.repo.getTree(sourceBrowsingRid, aliceMainHead, "src");
});
+2 -2
View File
@@ -47,8 +47,8 @@
let totalCommits: number | undefined = undefined;
function fetchTotalCommits(rid: string, sha: string) {
void api.repo.getTreeStatsBySha(rid, sha).then(stats => {
totalCommits = stats.commits;
void api.repo.getCommitCountBySha(rid, sha).then(commits => {
totalCommits = commits;
});
}
+3 -3
View File
@@ -40,9 +40,9 @@
if (cached !== undefined) {
commitCount = cached;
} else {
void api.repo.getTreeStatsBySha(rid, sha).then(stats => {
commitCountCache[sha] = stats.commits;
commitCount = stats.commits;
void api.repo.getCommitCountBySha(rid, sha).then(commits => {
commitCountCache[sha] = commits;
commitCount = commits;
});
}
}