diff --git a/crates/forge-git/src/browse.rs b/crates/forge-git/src/browse.rs index 80251b0..c6962c6 100644 --- a/crates/forge-git/src/browse.rs +++ b/crates/forge-git/src/browse.rs @@ -27,6 +27,7 @@ pub struct RefInfo { pub full_name: String, pub target: String, // peeled commit oid pub kind: RefKind, + pub committer_time: i64, // committer time of the target commit (unix seconds) } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -151,11 +152,21 @@ pub fn list_refs(repo: &Path) -> Result> { if !is_commit(&repo, id)? { continue; } + let commit = repo.find_commit(id).map_err(gerr)?; + let committer_time = commit + .decode() + .map_err(gerr)? + .committer() + .map_err(gerr)? + .time() + .map_err(gerr)? + .seconds; out.push(RefInfo { name, full_name, target: id.to_hex().to_string(), kind, + committer_time, }); } } diff --git a/crates/forge-web/src/pages/ci_runs.rs b/crates/forge-web/src/pages/ci_runs.rs index 938329f..11804f8 100644 --- a/crates/forge-web/src/pages/ci_runs.rs +++ b/crates/forge-web/src/pages/ci_runs.rs @@ -2413,18 +2413,21 @@ mod tests { full_name: "refs/heads/dev".into(), target: "a".into(), kind: RefKind::Branch, + committer_time: 0, }, RefInfo { name: "main".into(), full_name: "refs/heads/main".into(), target: "b".into(), kind: RefKind::Branch, + committer_time: 0, }, RefInfo { name: "v1".into(), full_name: "refs/tags/v1".into(), target: "c".into(), kind: RefKind::Tag, + committer_time: 0, }, ]; assert_eq!(branch_choices(&refs, "main"), vec!["main", "dev"]); diff --git a/crates/forge-web/src/pages/refs.rs b/crates/forge-web/src/pages/refs.rs index 66acad0..611a18f 100644 --- a/crates/forge-web/src/pages/refs.rs +++ b/crates/forge-web/src/pages/refs.rs @@ -30,47 +30,42 @@ pub async fn load_refs(path: PathBuf) -> Result> { /// Branch and tag links for the ref switcher, each with the commit time of its tip. The default /// branch sorts first, the rest newest first. -pub async fn ref_links( - path: PathBuf, - refs: Vec, +pub fn ref_links( + refs: &[RefInfo], current: &str, default_branch: &str, repo: &RepoRow, -) -> Result<(Vec, Vec)> { - let mut timed = tokio::task::spawn_blocking(move || -> forge_git::Result<_> { - refs.into_iter() - .map(|r| { - let updated = - browse::read_commit(&path, &r.target)?.map_or(0, |c| c.committer_time); - Ok((r, updated)) - }) - .collect::>>() - }) - .await??; - timed.sort_by(|(a, ta), (b, tb)| { - (a.name != default_branch, Reverse(*ta), a.name.as_str()).cmp(&( - b.name != default_branch, - Reverse(*tb), - b.name.as_str(), - )) +) -> (Vec, Vec) { + let mut sorted: Vec<&RefInfo> = refs.iter().collect(); + sorted.sort_by(|a, b| { + ( + a.name != default_branch, + Reverse(a.committer_time), + a.name.as_str(), + ) + .cmp(&( + b.name != default_branch, + Reverse(b.committer_time), + b.name.as_str(), + )) }); - let link = |(r, updated): &(RefInfo, i64)| RefLink { + let link = |r: &RefInfo| RefLink { name: r.name.clone(), href: repo_href(&repo.owner_name, &repo.name, "tree", &r.name), current: r.name == current, - updated: *updated, + updated: r.committer_time, }; - let branches = timed + let branches = sorted .iter() - .filter(|(r, _)| r.kind == RefKind::Branch) - .map(link) + .filter(|r| r.kind == RefKind::Branch) + .map(|r| link(r)) .collect(); - let tags = timed + let tags = sorted .iter() - .filter(|(r, _)| r.kind == RefKind::Tag) - .map(link) + .filter(|r| r.kind == RefKind::Tag) + .map(|r| link(r)) .collect(); - Ok((branches, tags)) + (branches, tags) } /// One row of the branch or tag table. diff --git a/crates/forge-web/src/pages/repo.rs b/crates/forge-web/src/pages/repo.rs index 306b7c1..1573c3d 100644 --- a/crates/forge-web/src/pages/repo.rs +++ b/crates/forge-web/src/pages/repo.rs @@ -419,8 +419,7 @@ async fn repo_page(cx: &Cx) -> Result { let branch = repo.default_branch.clone(); let refs = load_refs(ctx.path.clone()).await?; - let (branches, tags) = - ref_links(ctx.path.clone(), refs.clone(), &branch, &branch, repo).await?; + let (branches, tags) = ref_links(&refs, &branch, &branch, repo); let mut recent = branches.clone(); recent.sort_by_key(|link| std::cmp::Reverse(link.updated)); recent.truncate(RECENT_BRANCHES); diff --git a/crates/forge-web/src/pages/tree.rs b/crates/forge-web/src/pages/tree.rs index bb03528..b79a6a9 100644 --- a/crates/forge-web/src/pages/tree.rs +++ b/crates/forge-web/src/pages/tree.rs @@ -548,14 +548,7 @@ async fn tree_page(cx: &Cx) -> Result { now_unix(), ctx.viewer_tz(), ); - let (branches, tags) = ref_links( - ctx.path.clone(), - refs.clone(), - &ref_name, - &repo.default_branch, - repo, - ) - .await?; + let (branches, tags) = ref_links(&refs, &ref_name, &repo.default_branch, repo); let branches_href = repo_href(&repo.owner_name, &repo.name, "branches", ""); let tags_href = repo_href(&repo.owner_name, &repo.name, "tags", ""); let live = follows_branch(&refs, &ref_name)