Ref switcher reads tip times in the same pass that lists refs
Browse files at this commit4c7677f7483da6a943b0925419f565ca30511561Parent: 2c14701- perf
- ref switcher reads tip times in the same pass that lists refs
5 changed files +40 -39
- crates/forge-git/src/browse.rs +11 -0
- crates/forge-web/src/pages/ci_runs.rs +3 -0
- crates/forge-web/src/pages/refs.rs +24 -29
- crates/forge-web/src/pages/repo.rs +1 -2
- crates/forge-web/src/pages/tree.rs +1 -8
crates/forge-git/src/browse.rsmodified+11 -0
| @@ -27,6 +27,7 @@ pub struct RefInfo { | ||
| 27 | 27 | pub full_name: String, |
| 28 | 28 | pub target: String, // peeled commit oid |
| 29 | 29 | pub kind: RefKind, |
| 30 | + pub committer_time: i64, // committer time of the target commit (unix seconds) | |
| 30 | 31 | } |
| 31 | 32 | |
| 32 | 33 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| @@ -151,11 +152,21 @@ pub fn list_refs(repo: &Path) -> Result<Vec<RefInfo>> { | ||
| 151 | 152 | if !is_commit(&repo, id)? { |
| 152 | 153 | continue; |
| 153 | 154 | } |
| 155 | + let commit = repo.find_commit(id).map_err(gerr)?; | |
| 156 | + let committer_time = commit | |
| 157 | + .decode() | |
| 158 | + .map_err(gerr)? | |
| 159 | + .committer() | |
| 160 | + .map_err(gerr)? | |
| 161 | + .time() | |
| 162 | + .map_err(gerr)? | |
| 163 | + .seconds; | |
| 154 | 164 | out.push(RefInfo { |
| 155 | 165 | name, |
| 156 | 166 | full_name, |
| 157 | 167 | target: id.to_hex().to_string(), |
| 158 | 168 | kind, |
| 169 | + committer_time, | |
| 159 | 170 | }); |
| 160 | 171 | } |
| 161 | 172 | } |
crates/forge-web/src/pages/ci_runs.rsmodified+3 -0
| @@ -2413,18 +2413,21 @@ mod tests { | ||
| 2413 | 2413 | full_name: "refs/heads/dev".into(), |
| 2414 | 2414 | target: "a".into(), |
| 2415 | 2415 | kind: RefKind::Branch, |
| 2416 | + committer_time: 0, | |
| 2416 | 2417 | }, |
| 2417 | 2418 | RefInfo { |
| 2418 | 2419 | name: "main".into(), |
| 2419 | 2420 | full_name: "refs/heads/main".into(), |
| 2420 | 2421 | target: "b".into(), |
| 2421 | 2422 | kind: RefKind::Branch, |
| 2423 | + committer_time: 0, | |
| 2422 | 2424 | }, |
| 2423 | 2425 | RefInfo { |
| 2424 | 2426 | name: "v1".into(), |
| 2425 | 2427 | full_name: "refs/tags/v1".into(), |
| 2426 | 2428 | target: "c".into(), |
| 2427 | 2429 | kind: RefKind::Tag, |
| 2430 | + committer_time: 0, | |
| 2428 | 2431 | }, |
| 2429 | 2432 | ]; |
| 2430 | 2433 | assert_eq!(branch_choices(&refs, "main"), vec!["main", "dev"]); |
crates/forge-web/src/pages/refs.rsmodified+24 -29
| @@ -30,47 +30,42 @@ pub async fn load_refs(path: PathBuf) -> Result<Vec<RefInfo>> { | ||
| 30 | 30 | |
| 31 | 31 | /// Branch and tag links for the ref switcher, each with the commit time of its tip. The default |
| 32 | 32 | /// branch sorts first, the rest newest first. |
| 33 | -pub async fn ref_links( | |
| 34 | - path: PathBuf, | |
| 35 | - refs: Vec<RefInfo>, | |
| 33 | +pub fn ref_links( | |
| 34 | + refs: &[RefInfo], | |
| 36 | 35 | current: &str, |
| 37 | 36 | default_branch: &str, |
| 38 | 37 | repo: &RepoRow, |
| 39 | -) -> Result<(Vec<RefLink>, Vec<RefLink>)> { | |
| 40 | - let mut timed = tokio::task::spawn_blocking(move || -> forge_git::Result<_> { | |
| 41 | - refs.into_iter() | |
| 42 | - .map(|r| { | |
| 43 | - let updated = | |
| 44 | - browse::read_commit(&path, &r.target)?.map_or(0, |c| c.committer_time); | |
| 45 | - Ok((r, updated)) | |
| 46 | - }) | |
| 47 | - .collect::<forge_git::Result<Vec<(RefInfo, i64)>>>() | |
| 48 | - }) | |
| 49 | - .await??; | |
| 50 | - timed.sort_by(|(a, ta), (b, tb)| { | |
| 51 | - (a.name != default_branch, Reverse(*ta), a.name.as_str()).cmp(&( | |
| 52 | - b.name != default_branch, | |
| 53 | - Reverse(*tb), | |
| 54 | - b.name.as_str(), | |
| 55 | - )) | |
| 38 | +) -> (Vec<RefLink>, Vec<RefLink>) { | |
| 39 | + let mut sorted: Vec<&RefInfo> = refs.iter().collect(); | |
| 40 | + sorted.sort_by(|a, b| { | |
| 41 | + ( | |
| 42 | + a.name != default_branch, | |
| 43 | + Reverse(a.committer_time), | |
| 44 | + a.name.as_str(), | |
| 45 | + ) | |
| 46 | + .cmp(&( | |
| 47 | + b.name != default_branch, | |
| 48 | + Reverse(b.committer_time), | |
| 49 | + b.name.as_str(), | |
| 50 | + )) | |
| 56 | 51 | }); |
| 57 | - let link = |(r, updated): &(RefInfo, i64)| RefLink { | |
| 52 | + let link = |r: &RefInfo| RefLink { | |
| 58 | 53 | name: r.name.clone(), |
| 59 | 54 | href: repo_href(&repo.owner_name, &repo.name, "tree", &r.name), |
| 60 | 55 | current: r.name == current, |
| 61 | - updated: *updated, | |
| 56 | + updated: r.committer_time, | |
| 62 | 57 | }; |
| 63 | - let branches = timed | |
| 58 | + let branches = sorted | |
| 64 | 59 | .iter() |
| 65 | - .filter(|(r, _)| r.kind == RefKind::Branch) | |
| 66 | - .map(link) | |
| 60 | + .filter(|r| r.kind == RefKind::Branch) | |
| 61 | + .map(|r| link(r)) | |
| 67 | 62 | .collect(); |
| 68 | - let tags = timed | |
| 63 | + let tags = sorted | |
| 69 | 64 | .iter() |
| 70 | - .filter(|(r, _)| r.kind == RefKind::Tag) | |
| 71 | - .map(link) | |
| 65 | + .filter(|r| r.kind == RefKind::Tag) | |
| 66 | + .map(|r| link(r)) | |
| 72 | 67 | .collect(); |
| 73 | - Ok((branches, tags)) | |
| 68 | + (branches, tags) | |
| 74 | 69 | } |
| 75 | 70 | |
| 76 | 71 | /// One row of the branch or tag table. |
crates/forge-web/src/pages/repo.rsmodified+1 -2
| @@ -419,8 +419,7 @@ async fn repo_page(cx: &Cx) -> Result<impl View> { | ||
| 419 | 419 | let branch = repo.default_branch.clone(); |
| 420 | 420 | |
| 421 | 421 | let refs = load_refs(ctx.path.clone()).await?; |
| 422 | - let (branches, tags) = | |
| 423 | - ref_links(ctx.path.clone(), refs.clone(), &branch, &branch, repo).await?; | |
| 422 | + let (branches, tags) = ref_links(&refs, &branch, &branch, repo); | |
| 424 | 423 | let mut recent = branches.clone(); |
| 425 | 424 | recent.sort_by_key(|link| std::cmp::Reverse(link.updated)); |
| 426 | 425 | recent.truncate(RECENT_BRANCHES); |
crates/forge-web/src/pages/tree.rsmodified+1 -8
| @@ -548,14 +548,7 @@ async fn tree_page(cx: &Cx) -> Result<impl View> { | ||
| 548 | 548 | now_unix(), |
| 549 | 549 | ctx.viewer_tz(), |
| 550 | 550 | ); |
| 551 | - let (branches, tags) = ref_links( | |
| 552 | - ctx.path.clone(), | |
| 553 | - refs.clone(), | |
| 554 | - &ref_name, | |
| 555 | - &repo.default_branch, | |
| 556 | - repo, | |
| 557 | - ) | |
| 558 | - .await?; | |
| 551 | + let (branches, tags) = ref_links(&refs, &ref_name, &repo.default_branch, repo); | |
| 559 | 552 | let branches_href = repo_href(&repo.owner_name, &repo.name, "branches", ""); |
| 560 | 553 | let tags_href = repo_href(&repo.owner_name, &repo.name, "tags", ""); |
| 561 | 554 | let live = follows_branch(&refs, &ref_name) |