Last-commit walk reads trees in-process with gix
Browse files at this commitc25199e98cb613c94200c41cfe66a8612523dd99Parent: 6e3d1e1All checks have passed
Show checks
ci/buildSucceededDetails
- perf
- last-commit walk reads trees in-process with gix
- unchanged dir trees are skipped without reading entries, so the code page no longer spawns git log per view
6 changed files +134 -84
- crates/forge-git/src/browse.rs +8 -4
- crates/forge-git/src/history.rs +118 -69
- crates/forge-git/tests/integration/history.rs +4 -6
- crates/forge-web-pages/src/pages/repo.rs +1 -1
- crates/forge-web-pages/src/pages/tree.rs +2 -3
- crates/forge-web-repo/src/pages/blob.rs +1 -1
crates/forge-git/src/browse.rsmodified+8 -4
| @@ -65,11 +65,11 @@ pub struct CommitInfo { | |||
| 65 | pub message: String, | 65 | pub message: String, |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | -fn gerr(e: impl std::fmt::Display) -> Error { | 68 | +pub(crate) fn gerr(e: impl std::fmt::Display) -> Error { |
| 69 | Error::Failed(e.to_string()) | 69 | Error::Failed(e.to_string()) |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | -fn open(repo: &Path) -> Result<gix::Repository> { | 72 | +pub(crate) fn open(repo: &Path) -> Result<gix::Repository> { |
| 73 | let options = gix::open::Options::isolated().config_overrides(["core.deltaBaseCacheLimit=16m"]); | 73 | let options = gix::open::Options::isolated().config_overrides(["core.deltaBaseCacheLimit=16m"]); |
| 74 | gix::open_opts(repo, options).map_err(gerr) | 74 | gix::open_opts(repo, options).map_err(gerr) |
| 75 | } | 75 | } |
| @@ -99,7 +99,7 @@ fn valid_hex_prefix(s: &str) -> bool { | |||
| 99 | (4..=64).contains(&s.len()) && s.bytes().all(|b| b.is_ascii_hexdigit()) | 99 | (4..=64).contains(&s.len()) && s.bytes().all(|b| b.is_ascii_hexdigit()) |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | -fn parse_oid(hex: &str) -> Result<gix::ObjectId> { | 102 | +pub(crate) fn parse_oid(hex: &str) -> Result<gix::ObjectId> { |
| 103 | gix::ObjectId::from_hex(hex.as_bytes()) | 103 | gix::ObjectId::from_hex(hex.as_bytes()) |
| 104 | .map_err(|_| Error::InvalidInput(format!("not a full object id: {hex}"))) | 104 | .map_err(|_| Error::InvalidInput(format!("not a full object id: {hex}"))) |
| 105 | } | 105 | } |
| @@ -336,9 +336,13 @@ fn tree_at<'r>( | |||
| 336 | commit: &str, | 336 | commit: &str, |
| 337 | path: &str, | 337 | path: &str, |
| 338 | ) -> Result<Option<gix::Tree<'r>>> { | 338 | ) -> Result<Option<gix::Tree<'r>>> { |
| 339 | - let Some(mut root) = commit_root(repo, commit)? else { | 339 | + let Some(root) = commit_root(repo, commit)? else { |
| 340 | return Ok(None); | 340 | return Ok(None); |
| 341 | }; | 341 | }; |
| 342 | + subtree(root, path) | ||
| 343 | +} | ||
| 344 | + | ||
| 345 | +pub(crate) fn subtree<'r>(mut root: gix::Tree<'r>, path: &str) -> Result<Option<gix::Tree<'r>>> { | ||
| 342 | if path.is_empty() { | 346 | if path.is_empty() { |
| 343 | return Ok(Some(root)); | 347 | return Ok(Some(root)); |
| 344 | } | 348 | } |
crates/forge-git/src/history.rsmodified+118 -69
| @@ -2,6 +2,8 @@ use std::collections::{HashMap, HashSet}; | |||
| 2 | use std::path::Path; | 2 | use std::path::Path; |
| 3 | use std::process::Stdio; | 3 | use std::process::Stdio; |
| 4 | 4 | ||
| 5 | +use gix::bstr::ByteSlice; | ||
| 6 | +use gix::revision::walk::{CommitTimeOrder, Sorting}; | ||
| 5 | use tokio::io::{AsyncRead, AsyncReadExt}; | 7 | use tokio::io::{AsyncRead, AsyncReadExt}; |
| 6 | 8 | ||
| 7 | use crate::browse::{self, CommitInfo}; | 9 | use crate::browse::{self, CommitInfo}; |
| @@ -866,7 +868,6 @@ pub async fn list_file_sizes( | |||
| 866 | } | 868 | } |
| 867 | 869 | ||
| 868 | pub async fn last_commits( | 870 | pub async fn last_commits( |
| 869 | - env: &GitEnv, | ||
| 870 | repo: &Path, | 871 | repo: &Path, |
| 871 | commit: &str, | 872 | commit: &str, |
| 872 | dir: &str, | 873 | dir: &str, |
| @@ -877,83 +878,131 @@ pub async fn last_commits( | |||
| 877 | if !dir.is_empty() { | 878 | if !dir.is_empty() { |
| 878 | browse::validate_repo_path(dir)?; | 879 | browse::validate_repo_path(dir)?; |
| 879 | } | 880 | } |
| 880 | - let wanted: HashSet<&str> = names.iter().map(String::as_str).collect(); | 881 | + if names.is_empty() || max_walk == 0 { |
| 881 | - let mut found: HashMap<String, CommitInfo> = HashMap::new(); | 882 | + return Ok(HashMap::new()); |
| 882 | - if wanted.is_empty() || max_walk == 0 { | ||
| 883 | - return Ok(found); | ||
| 884 | - } | ||
| 885 | - | ||
| 886 | - let prefix = if dir.is_empty() { | ||
| 887 | - String::new() | ||
| 888 | - } else { | ||
| 889 | - format!("{dir}/") | ||
| 890 | - }; | ||
| 891 | - let mut cmd = git(env, "log") | ||
| 892 | - .git_dir(repo) | ||
| 893 | - .arg("-z") | ||
| 894 | - .arg("--no-color") | ||
| 895 | - .arg("--name-only") | ||
| 896 | - .arg(COMMIT_FORMAT) | ||
| 897 | - .user_opt("--max-count", &max_walk.to_string())? | ||
| 898 | - .user_revs(&[commit])?; | ||
| 899 | - if !dir.is_empty() { | ||
| 900 | - let spec = literal_spec(dir); | ||
| 901 | - cmd = cmd.paths(&[spec.as_str()]); | ||
| 902 | } | 883 | } |
| 884 | + let repo = repo.to_path_buf(); | ||
| 885 | + let commit = commit.to_owned(); | ||
| 886 | + let dir = dir.to_owned(); | ||
| 887 | + let wanted: HashSet<String> = names.iter().cloned().collect(); | ||
| 888 | + tokio::task::spawn_blocking(move || { | ||
| 889 | + last_commits_blocking(&repo, &commit, &dir, &wanted, max_walk) | ||
| 890 | + }) | ||
| 891 | + .await | ||
| 892 | + .map_err(|e| Error::Failed(e.to_string()))? | ||
| 893 | +} | ||
| 903 | 894 | ||
| 904 | - { | 895 | +fn last_commits_blocking( |
| 905 | - let mut handle = |rec: &[u8]| -> bool { | 896 | + repo: &Path, |
| 906 | - let Some((info, files)) = parse_record(rec) else { | 897 | + commit: &str, |
| 907 | - return true; | 898 | + dir: &str, |
| 908 | - }; | 899 | + wanted: &HashSet<String>, |
| 909 | - let start = files | 900 | + max_walk: usize, |
| 910 | - .iter() | 901 | +) -> Result<HashMap<String, CommitInfo>> { |
| 911 | - .position(|&b| b != 0 && b != b'\n') | 902 | + let repo = browse::open(repo)?; |
| 912 | - .unwrap_or(files.len()); | 903 | + let tip = browse::parse_oid(commit)?; |
| 913 | - let files = &files[start..]; | 904 | + let mut found: HashMap<String, CommitInfo> = HashMap::new(); |
| 914 | - for seg in files.split(|&b| b == 0).filter(|s| !s.is_empty()) { | 905 | + let walk = repo |
| 915 | - let path = String::from_utf8_lossy(seg); | 906 | + .rev_walk([tip]) |
| 916 | - let rest: &str = if prefix.is_empty() { | 907 | + .sorting(Sorting::ByCommitTime(CommitTimeOrder::NewestFirst)) |
| 917 | - &path | 908 | + .all() |
| 918 | - } else if let Some(r) = path.strip_prefix(prefix.as_str()) { | 909 | + .map_err(browse::gerr)?; |
| 919 | - r | 910 | + let mut walked = 0; |
| 920 | - } else { | 911 | + for info in walk { |
| 921 | - continue; | 912 | + if walked == max_walk || found.len() == wanted.len() { |
| 922 | - }; | 913 | + break; |
| 923 | - let child = rest.split('/').next().unwrap_or(rest); | 914 | + } |
| 924 | - if wanted.contains(child) && !found.contains_key(child) { | 915 | + let id = info.map_err(browse::gerr)?.id; |
| 925 | - found.insert(child.to_owned(), info.clone()); | 916 | + let object = repo.find_commit(id).map_err(browse::gerr)?; |
| 926 | - } | 917 | + let parents: Vec<gix::ObjectId> = object.parent_ids().map(|p| p.detach()).collect(); |
| 918 | + if parents.len() > 1 { | ||
| 919 | + continue; | ||
| 920 | + } | ||
| 921 | + let cur = browse::subtree(object.tree().map_err(browse::gerr)?, dir)?; | ||
| 922 | + let prev = match parents.first() { | ||
| 923 | + Some(parent) => { | ||
| 924 | + let root = repo | ||
| 925 | + .find_commit(*parent) | ||
| 926 | + .map_err(browse::gerr)? | ||
| 927 | + .tree() | ||
| 928 | + .map_err(browse::gerr)?; | ||
| 929 | + browse::subtree(root, dir)? | ||
| 927 | } | 930 | } |
| 928 | - found.len() < wanted.len() | 931 | + None => None, |
| 929 | }; | 932 | }; |
| 930 | - | 933 | + if cur.as_ref().map(|t| t.id().detach()) == prev.as_ref().map(|t| t.id().detach()) { |
| 931 | - let mut pending: Vec<u8> = Vec::new(); | 934 | + continue; |
| 932 | - let mut started = false; | 935 | + } |
| 933 | - let mut stopped = false; | 936 | + walked += 1; |
| 934 | - let eof = for_each_chunk(cmd, |chunk| { | 937 | + let cur = dir_entries(cur, wanted)?; |
| 935 | - for &b in chunk { | 938 | + let prev = dir_entries(prev, wanted)?; |
| 936 | - if b == REC { | 939 | + let changed: Vec<String> = wanted |
| 937 | - if started && !handle(&pending) { | 940 | + .iter() |
| 938 | - stopped = true; | 941 | + .filter(|name| !found.contains_key(name.as_str())) |
| 939 | - return false; | 942 | + .filter(|name| cur.get(name.as_str()) != prev.get(name.as_str())) |
| 940 | - } | 943 | + .cloned() |
| 941 | - pending.clear(); | 944 | + .collect(); |
| 942 | - started = true; | 945 | + if changed.is_empty() { |
| 943 | - } else if started { | 946 | + continue; |
| 944 | - pending.push(b); | 947 | + } |
| 945 | - } | 948 | + let info = commit_info(&object)?; |
| 946 | - } | 949 | + for name in changed { |
| 947 | - true | 950 | + found.insert(name, info.clone()); |
| 948 | - }) | ||
| 949 | - .await?; | ||
| 950 | - if eof && started && !stopped { | ||
| 951 | - handle(&pending); | ||
| 952 | } | 951 | } |
| 953 | } | 952 | } |
| 954 | Ok(found) | 953 | Ok(found) |
| 955 | } | 954 | } |
| 956 | 955 | ||
| 956 | +type EntryKey = (gix::ObjectId, gix::objs::tree::EntryMode); | ||
| 957 | + | ||
| 958 | +fn dir_entries( | ||
| 959 | + tree: Option<gix::Tree<'_>>, | ||
| 960 | + wanted: &HashSet<String>, | ||
| 961 | +) -> Result<HashMap<String, EntryKey>> { | ||
| 962 | + let mut out = HashMap::new(); | ||
| 963 | + let Some(tree) = tree else { | ||
| 964 | + return Ok(out); | ||
| 965 | + }; | ||
| 966 | + for entry in tree.iter() { | ||
| 967 | + let entry = entry.map_err(browse::gerr)?; | ||
| 968 | + let name = entry.filename().to_str_lossy(); | ||
| 969 | + if wanted.contains(name.as_ref()) { | ||
| 970 | + out.insert(name.into_owned(), (entry.object_id(), entry.mode())); | ||
| 971 | + } | ||
| 972 | + } | ||
| 973 | + Ok(out) | ||
| 974 | +} | ||
| 975 | + | ||
| 976 | +fn subject(message: &str) -> String { | ||
| 977 | + message | ||
| 978 | + .lines() | ||
| 979 | + .skip_while(|line| line.trim().is_empty()) | ||
| 980 | + .take_while(|line| !line.trim().is_empty()) | ||
| 981 | + .map(str::trim_end) | ||
| 982 | + .collect::<Vec<_>>() | ||
| 983 | + .join(" ") | ||
| 984 | +} | ||
| 985 | + | ||
| 986 | +fn commit_info(commit: &gix::Commit<'_>) -> Result<CommitInfo> { | ||
| 987 | + let decoded = commit.decode().map_err(browse::gerr)?; | ||
| 988 | + let author = decoded.author().map_err(browse::gerr)?; | ||
| 989 | + let committer = decoded.committer().map_err(browse::gerr)?; | ||
| 990 | + let message = String::from_utf8_lossy(commit.message_raw_sloppy()).into_owned(); | ||
| 991 | + let message = message.trim_end_matches('\n').to_owned(); | ||
| 992 | + Ok(CommitInfo { | ||
| 993 | + oid: commit.id().to_hex().to_string(), | ||
| 994 | + parents: decoded.parents().map(|p| p.to_hex().to_string()).collect(), | ||
| 995 | + author_name: String::from_utf8_lossy(author.name).into_owned(), | ||
| 996 | + author_email: String::from_utf8_lossy(author.email).into_owned(), | ||
| 997 | + author_time: author.time().map_err(browse::gerr)?.seconds, | ||
| 998 | + committer_name: String::from_utf8_lossy(committer.name).into_owned(), | ||
| 999 | + committer_email: String::from_utf8_lossy(committer.email).into_owned(), | ||
| 1000 | + committer_time: committer.time().map_err(browse::gerr)?.seconds, | ||
| 1001 | + summary: subject(&message), | ||
| 1002 | + message, | ||
| 1003 | + }) | ||
| 1004 | +} | ||
| 1005 | + | ||
| 957 | pub fn archive_command( | 1006 | pub fn archive_command( |
| 958 | env: &GitEnv, | 1007 | env: &GitEnv, |
| 959 | repo: &Path, | 1008 | repo: &Path, |
crates/forge-git/tests/integration/history.rsmodified+4 -6
| @@ -218,7 +218,7 @@ async fn last_commits_finds_newest_touch_per_child() { | |||
| 218 | .iter() | 218 | .iter() |
| 219 | .map(|s| s.to_string()) | 219 | .map(|s| s.to_string()) |
| 220 | .collect(); | 220 | .collect(); |
| 221 | - let map = last_commits(&f.env, &f.git_dir, &c6, "d", &names, 100) | 221 | + let map = last_commits(&f.git_dir, &c6, "d", &names, 100) |
| 222 | .await | 222 | .await |
| 223 | .unwrap(); | 223 | .unwrap(); |
| 224 | assert_eq!(map.len(), 4); | 224 | assert_eq!(map.len(), 4); |
| @@ -229,15 +229,13 @@ async fn last_commits_finds_newest_touch_per_child() { | |||
| 229 | assert!(!map.contains_key("missing")); | 229 | assert!(!map.contains_key("missing")); |
| 230 | 230 | ||
| 231 | let root_names: Vec<String> = vec!["d".into(), "other.txt".into()]; | 231 | let root_names: Vec<String> = vec!["d".into(), "other.txt".into()]; |
| 232 | - let root = last_commits(&f.env, &f.git_dir, &c6, "", &root_names, 100) | 232 | + let root = last_commits(&f.git_dir, &c6, "", &root_names, 100) |
| 233 | .await | 233 | .await |
| 234 | .unwrap(); | 234 | .unwrap(); |
| 235 | assert_eq!(root["d"].oid, c5); | 235 | assert_eq!(root["d"].oid, c5); |
| 236 | assert_eq!(root["other.txt"].oid, c6); | 236 | assert_eq!(root["other.txt"].oid, c6); |
| 237 | 237 | ||
| 238 | - let short = last_commits(&f.env, &f.git_dir, &c6, "d", &names, 1) | 238 | + let short = last_commits(&f.git_dir, &c6, "d", &names, 1).await.unwrap(); |
| 239 | - .await | ||
| 240 | - .unwrap(); | ||
| 241 | assert_eq!(short.len(), 1); | 239 | assert_eq!(short.len(), 1); |
| 242 | assert_eq!(short["sub"].oid, c5); | 240 | assert_eq!(short["sub"].oid, c5); |
| 243 | } | 241 | } |
| @@ -326,7 +324,7 @@ async fn non_hex_commit_arguments_are_rejected() { | |||
| 326 | Err(Error::InvalidInput(_)) | 324 | Err(Error::InvalidInput(_)) |
| 327 | )); | 325 | )); |
| 328 | assert!(matches!( | 326 | assert!(matches!( |
| 329 | - last_commits(&f.env, &f.git_dir, "g".repeat(40).as_str(), "", &[], 10).await, | 327 | + last_commits(&f.git_dir, "g".repeat(40).as_str(), "", &[], 10).await, |
| 330 | Err(Error::InvalidInput(_)) | 328 | Err(Error::InvalidInput(_)) |
| 331 | )); | 329 | )); |
| 332 | assert!(matches!( | 330 | assert!(matches!( |
crates/forge-web-pages/src/pages/repo.rsmodified+1 -1
| @@ -450,7 +450,7 @@ async fn repo_page(cx: &Cx) -> Result<impl View> { | |||
| 450 | let names: Vec<String> = entries.iter().map(|e| e.name.clone()).collect(); | 450 | let names: Vec<String> = entries.iter().map(|e| e.name.clone()).collect(); |
| 451 | facts = Some(tree_facts(state, repo, &ctx.path, commit, &names).await?); | 451 | facts = Some(tree_facts(state, repo, &ctx.path, commit, &names).await?); |
| 452 | let (last, latest) = tokio::join!( | 452 | let (last, latest) = tokio::join!( |
| 453 | - last_commits(state, &ctx.path, commit, "", &names), | 453 | + last_commits(&ctx.path, commit, "", &names), |
| 454 | latest_bar(state, repo, &ctx.path, commit, lang, ctx.viewer_tz()), | 454 | latest_bar(state, repo, &ctx.path, commit, lang, ctx.viewer_tz()), |
| 455 | ); | 455 | ); |
| 456 | bar = latest; | 456 | bar = latest; |
crates/forge-web-pages/src/pages/tree.rsmodified+2 -3
| @@ -67,13 +67,12 @@ pub async fn read_file(path: PathBuf, commit: String, file: String) -> Result<Op | |||
| 67 | } | 67 | } |
| 68 | 68 | ||
| 69 | pub async fn last_commits( | 69 | pub async fn last_commits( |
| 70 | - state: &AppState, | ||
| 71 | path: &Path, | 70 | path: &Path, |
| 72 | commit: &str, | 71 | commit: &str, |
| 73 | dir: &str, | 72 | dir: &str, |
| 74 | names: &[String], | 73 | names: &[String], |
| 75 | ) -> HashMap<String, CommitInfo> { | 74 | ) -> HashMap<String, CommitInfo> { |
| 76 | - let walk = history::last_commits(&state.git, path, commit, dir, names, LAST_COMMIT_WALK); | 75 | + let walk = history::last_commits(path, commit, dir, names, LAST_COMMIT_WALK); |
| 77 | match tokio::time::timeout(LAST_COMMIT_TIMEOUT, walk).await { | 76 | match tokio::time::timeout(LAST_COMMIT_TIMEOUT, walk).await { |
| 78 | Ok(Ok(map)) => map, | 77 | Ok(Ok(map)) => map, |
| 79 | Ok(Err(e)) => { | 78 | Ok(Err(e)) => { |
| @@ -435,7 +434,7 @@ async fn tree_page(cx: &Cx) -> Result<impl View> { | |||
| 435 | .collect() | 434 | .collect() |
| 436 | }; | 435 | }; |
| 437 | let (last, bar, strip, runs) = tokio::join!( | 436 | let (last, bar, strip, runs) = tokio::join!( |
| 438 | - last_commits(state, &ctx.path, &commit, &dir, &names), | 437 | + last_commits(&ctx.path, &commit, &dir, &names), |
| 439 | latest_bar(state, repo, &ctx.path, &commit, lang, ctx.viewer_tz()), | 438 | latest_bar(state, repo, &ctx.path, &commit, lang, ctx.viewer_tz()), |
| 440 | commit_strip(state, repo, &ctx.path, &commit), | 439 | commit_strip(state, repo, &ctx.path, &commit), |
| 441 | run_rows(state, lang, repo), | 440 | run_rows(state, lang, repo), |
crates/forge-web-repo/src/pages/blob.rsmodified+1 -1
| @@ -257,7 +257,7 @@ async fn blob_page(cx: &Cx) -> Result<impl View> { | |||
| 257 | .collect() | 257 | .collect() |
| 258 | }; | 258 | }; |
| 259 | let (last, bar, strip, runs) = tokio::join!( | 259 | let (last, bar, strip, runs) = tokio::join!( |
| 260 | - last_commits(state, &ctx.path, &commit, &dir, &names), | 260 | + last_commits(&ctx.path, &commit, &dir, &names), |
| 261 | latest_bar(state, repo, &ctx.path, &commit, lang, ctx.viewer_tz()), | 261 | latest_bar(state, repo, &ctx.path, &commit, lang, ctx.viewer_tz()), |
| 262 | commit_strip(state, repo, &ctx.path, &commit), | 262 | commit_strip(state, repo, &ctx.path, &commit), |
| 263 | run_rows(state, lang, repo), | 263 | run_rows(state, lang, repo), |