From 436085c15445e1ca5407ffd97d579c534a556b20 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:19:27 +0200 Subject: [PATCH] httpd: Add a dedicated commit-count endpoint The header and history only need a commit count, but /stats/tree walks the full history and builds a contributor set to produce it. Add /stats/commits, counting with `git rev-list --count` (bitmap/commit-graph), falling back to a libgit2 revwalk. --- crates/radicle-httpd/src/api/v1/repos.rs | 63 ++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/crates/radicle-httpd/src/api/v1/repos.rs b/crates/radicle-httpd/src/api/v1/repos.rs index e9288cc9f..8ffdc0e81 100644 --- a/crates/radicle-httpd/src/api/v1/repos.rs +++ b/crates/radicle-httpd/src/api/v1/repos.rs @@ -41,6 +41,10 @@ pub fn router(ctx: Context) -> Router { .route("/repos/{rid}/tree/{sha}/", get(tree_handler_root)) .route("/repos/{rid}/tree/{sha}/{*path}", get(tree_handler)) .route("/repos/{rid}/stats/tree/{sha}", get(stats_tree_handler)) + .route( + "/repos/{rid}/stats/commits/{sha}", + get(stats_commits_handler), + ) .route("/repos/{rid}/remotes", get(remotes_handler)) .route("/repos/{rid}/remotes/{peer}", get(remote_handler)) .route("/repos/{rid}/blob/{sha}/{*path}", get(blob_handler)) @@ -718,6 +722,55 @@ async fn stats_tree_handler( Ok::<_, Error>(immutable_response(stats)) } +/// Get the number of commits reachable from a commit. +/// `GET /repos/:rid/stats/commits/:sha` +async fn stats_commits_handler( + State(ctx): State, + Path((rid, sha)): Path<(RepoId, Oid)>, +) -> impl IntoResponse { + let commits = api::blocking(move || { + let (repo, _) = ctx.repo(rid)?; + commit_count(&repo, sha) + }) + .await?; + + Ok::<_, Error>(immutable_response(json!({ "commits": commits }))) +} + +/// Count commits reachable from `head`. +/// +/// Fast path: `git rev-list --count --use-bitmap-index` uses the pack bitmap / +/// commit-graph for a near-instant count, whereas a libgit2 revwalk ignores +/// those indexes and walks every commit (seconds on large histories). Falls +/// back to the walk if the git binary is unavailable or errors. +fn commit_count(repo: &radicle::storage::git::Repository, head: Oid) -> Result { + let count = std::process::Command::new("git") + .current_dir(repo.backend.path()) + .args([ + "rev-list", + "--count", + "--use-bitmap-index", + &head.to_string(), + ]) + .output() + .ok() + .filter(|output| output.status.success()) + .and_then(|output| { + String::from_utf8_lossy(&output.stdout) + .trim() + .parse::() + .ok() + }); + if let Some(count) = count { + return Ok(count); + } + + let mut revwalk = repo.backend.revwalk()?; + revwalk.push(head.into())?; + + Ok(revwalk.count()) +} + /// Get all repo remotes. /// `GET /repos/:rid/remotes` async fn remotes_handler(State(ctx): State, Path(rid): Path) -> impl IntoResponse { @@ -1911,6 +1964,16 @@ mod routes { ); } + #[tokio::test] + async fn test_repos_stats_commits() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(seed(tmp.path())); + let response = get(&app, format!("/repos/{RID}/stats/commits/{HEAD}")).await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.json().await, json!({ "commits": 3 })); + } + #[tokio::test] async fn test_repos_tree() { let tmp = tempfile::tempdir().unwrap();