mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-15 19:30:38 +08:00
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:
@@ -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,
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user