From 75b8a7aa368078117034078f2bd1223eb98f6a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20R=C3=BC=C3=9Fler?= Date: Tue, 1 Apr 2025 19:02:36 +0200 Subject: [PATCH 1/6] Draft PoC of blame using gitoxide --- Cargo.lock | 190 +++++++++++++++++++++++++++++++++++++ asyncgit/Cargo.toml | 2 + asyncgit/src/sync/blame.rs | 147 ++++++++++++++-------------- 3 files changed, 266 insertions(+), 73 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37581dae9a..93872d1e49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -174,6 +174,7 @@ dependencies = [ "git2", "git2-hooks", "gix", + "gix-blame", "invalidstring", "log", "openssl-sys", @@ -675,6 +676,20 @@ dependencies = [ "syn", ] +[[package]] +name = "dashmap" +version = "6.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf" +dependencies = [ + "cfg-if", + "crossbeam-utils", + "hashbrown 0.14.5", + "lock_api", + "once_cell", + "parking_lot_core", +] + [[package]] name = "der" version = "0.7.9" @@ -816,6 +831,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "encoding_rs" +version = "0.8.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" +dependencies = [ + "cfg-if", +] + [[package]] name = "env_filter" version = "0.1.3" @@ -1212,16 +1236,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a61e71ec6817fc3c9f12f812682cfe51ee6ea0d2e27e02fc3849c35524617435" dependencies = [ "gix-actor", + "gix-attributes", + "gix-command", "gix-commitgraph", "gix-config", "gix-date", "gix-diff", "gix-discover", "gix-features", + "gix-filter", "gix-fs", "gix-glob", "gix-hash", "gix-hashtable", + "gix-ignore", "gix-index", "gix-lock", "gix-mailmap", @@ -1229,6 +1257,7 @@ dependencies = [ "gix-odb", "gix-pack", "gix-path", + "gix-pathspec", "gix-protocol", "gix-ref", "gix-refspec", @@ -1236,12 +1265,14 @@ dependencies = [ "gix-revwalk", "gix-sec", "gix-shallow", + "gix-submodule", "gix-tempfile", "gix-trace", "gix-traverse", "gix-url", "gix-utils", "gix-validate", + "gix-worktree", "once_cell", "smallvec", "thiserror 2.0.12", @@ -1261,6 +1292,23 @@ dependencies = [ "winnow", ] +[[package]] +name = "gix-attributes" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4e25825e0430aa11096f8b65ced6780d4a96a133f81904edceebb5344c8dd7f" +dependencies = [ + "bstr", + "gix-glob", + "gix-path", + "gix-quote", + "gix-trace", + "kstring", + "smallvec", + "thiserror 2.0.12", + "unicode-bom", +] + [[package]] name = "gix-bitmap" version = "0.2.14" @@ -1270,6 +1318,25 @@ dependencies = [ "thiserror 2.0.12", ] +[[package]] +name = "gix-blame" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b25d5aa111ce9cb6d087c2d1153b96553a3dc491163398e96622b0bd7b40c7c6" +dependencies = [ + "gix-commitgraph", + "gix-date", + "gix-diff", + "gix-hash", + "gix-object", + "gix-revwalk", + "gix-trace", + "gix-traverse", + "gix-worktree", + "smallvec", + "thiserror 2.0.12", +] + [[package]] name = "gix-chunk" version = "0.4.11" @@ -1358,8 +1425,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2c975dad2afc85e4e233f444d1efbe436c3cdcf3a07173984509c436d00a3f8" dependencies = [ "bstr", + "gix-command", + "gix-filter", + "gix-fs", "gix-hash", "gix-object", + "gix-path", + "gix-tempfile", + "gix-trace", + "gix-traverse", + "gix-worktree", + "imara-diff", "thiserror 2.0.12", ] @@ -1399,6 +1475,27 @@ dependencies = [ "walkdir", ] +[[package]] +name = "gix-filter" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb2b2bbffdc5cc9b2b82fc82da1b98163c9b423ac2b45348baa83a947ac9ab89" +dependencies = [ + "bstr", + "encoding_rs", + "gix-attributes", + "gix-command", + "gix-hash", + "gix-object", + "gix-packetline-blocking", + "gix-path", + "gix-quote", + "gix-trace", + "gix-utils", + "smallvec", + "thiserror 2.0.12", +] + [[package]] name = "gix-fs" version = "0.14.0" @@ -1448,6 +1545,19 @@ dependencies = [ "parking_lot", ] +[[package]] +name = "gix-ignore" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a27c8380f493a10d1457f756a3f81924d578fc08d6535e304dfcafbf0261d18" +dependencies = [ + "bstr", + "gix-glob", + "gix-path", + "gix-trace", + "unicode-bom", +] + [[package]] name = "gix-index" version = "0.39.0" @@ -1572,6 +1682,18 @@ dependencies = [ "thiserror 2.0.12", ] +[[package]] +name = "gix-packetline-blocking" +version = "0.18.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ecf3ea2e105c7e45587bac04099824301262a6c43357fad5205da36dbb233b3" +dependencies = [ + "bstr", + "faster-hex", + "gix-trace", + "thiserror 2.0.12", +] + [[package]] name = "gix-path" version = "0.10.15" @@ -1585,6 +1707,21 @@ dependencies = [ "thiserror 2.0.12", ] +[[package]] +name = "gix-pathspec" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fef8422c3c9066d649074b24025125963f85232bfad32d6d16aea9453b82ec14" +dependencies = [ + "bitflags 2.9.1", + "bstr", + "gix-attributes", + "gix-config-value", + "gix-glob", + "gix-path", + "thiserror 2.0.12", +] + [[package]] name = "gix-protocol" version = "0.49.0" @@ -1707,12 +1844,28 @@ dependencies = [ "thiserror 2.0.12", ] +[[package]] +name = "gix-submodule" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c7390c2059505c365e9548016d4edc9f35749c6a9112b7b1214400bbc68da2" +dependencies = [ + "bstr", + "gix-config", + "gix-path", + "gix-pathspec", + "gix-refspec", + "gix-url", + "thiserror 2.0.12", +] + [[package]] name = "gix-tempfile" version = "17.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6de439bbb9a5d3550c9c7fab0e16d2d637d120fcbe0dfbc538772a187f099b" dependencies = [ + "dashmap", "gix-fs", "libc", "once_cell", @@ -1793,6 +1946,25 @@ dependencies = [ "thiserror 2.0.12", ] +[[package]] +name = "gix-worktree" +version = "0.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7760dbc4b79aa274fed30adc0d41dca6b917641f26e7867c4071b1fb4dc727b" +dependencies = [ + "bstr", + "gix-attributes", + "gix-features", + "gix-fs", + "gix-glob", + "gix-hash", + "gix-ignore", + "gix-index", + "gix-object", + "gix-path", + "gix-validate", +] + [[package]] name = "group" version = "0.13.0" @@ -2017,6 +2189,15 @@ dependencies = [ "icu_properties", ] +[[package]] +name = "imara-diff" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17d34b7d42178945f775e84bc4c36dde7c1c6cdfea656d3354d009056f2bb3d2" +dependencies = [ + "hashbrown 0.15.2", +] + [[package]] name = "indexmap" version = "2.9.0" @@ -2190,6 +2371,15 @@ dependencies = [ "libc", ] +[[package]] +name = "kstring" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "558bf9508a558512042d3095138b1f7b8fe90c5467d94f9f1da28b3731c5dbd1" +dependencies = [ + "static_assertions", +] + [[package]] name = "lazy_static" version = "1.5.0" diff --git a/asyncgit/Cargo.toml b/asyncgit/Cargo.toml index e0eaa7f560..ee3079b26b 100644 --- a/asyncgit/Cargo.toml +++ b/asyncgit/Cargo.toml @@ -28,7 +28,9 @@ gix = { version = "0.71.0", default-features = false, features = [ "max-performance", "revision", "mailmap", + "blob-diff" ] } +gix-blame = "0.1.0" log = "0.4" # git2 = { path = "../../extern/git2-rs", features = ["vendored-openssl"]} # git2 = { git="https://github.com/extrawurst/git2-rs.git", rev="fc13dcc", features = ["vendored-openssl"]} diff --git a/asyncgit/src/sync/blame.rs b/asyncgit/src/sync/blame.rs index 19f125f6b3..17775be6ff 100644 --- a/asyncgit/src/sync/blame.rs +++ b/asyncgit/src/sync/blame.rs @@ -1,15 +1,9 @@ //! Sync git API for fetching a file blame use super::{utils, CommitId, RepoPath}; -use crate::{ - error::{Error, Result}, - sync::{get_commits_info, repository::repo}, -}; -use git2::BlameOptions; +use crate::{error::Result, sync::get_commits_info}; use scopetime::scope_time; use std::collections::{HashMap, HashSet}; -use std::io::{BufRead, BufReader}; -use std::path::Path; /// A `BlameHunk` contains all the information that will be shown to the user. #[derive(Clone, Hash, Debug, PartialEq, Eq)] @@ -40,19 +34,6 @@ pub struct FileBlame { pub lines: Vec<(Option, String)>, } -/// fixup `\` windows path separators to git compatible `/` -fn fixup_windows_path(path: &str) -> String { - #[cfg(windows)] - { - path.replace('\\', "/") - } - - #[cfg(not(windows))] - { - path.to_string() - } -} - /// pub fn blame_file( repo_path: &RepoPath, @@ -61,35 +42,50 @@ pub fn blame_file( ) -> Result { scope_time!("blame_file"); - let repo = repo(repo_path)?; - - let commit_id = if let Some(commit_id) = commit_id { - commit_id - } else { - utils::get_head_repo(&repo)? + let repo: gix::Repository = + gix::ThreadSafeRepository::discover_with_environment_overrides(repo_path.gitpath()) + .map(Into::into)?; + let tip: gix::ObjectId = match commit_id { + Some(commit_id) => gix::ObjectId::from_bytes_or_panic( + commit_id.get_oid().as_bytes(), + ), + _ => repo.head()?.peel_to_commit_in_place()?.id, }; - let spec = - format!("{}:{}", commit_id, fixup_windows_path(file_path)); + let cache: Option = + repo.commit_graph_if_enabled().expect("TODO"); + let mut resource_cache = + repo.diff_resource_cache_for_tree_diff().expect("TODO"); - let object = repo.revparse_single(&spec)?; - let blob = repo.find_blob(object.id())?; + let options: gix_blame::Options = Default::default(); - if blob.is_binary() { - return Err(Error::NoBlameOnBinaryFile); - } - - let mut opts = BlameOptions::new(); - opts.newest_commit(commit_id.into()); + let outcome = gix_blame::file( + &repo.objects, + tip, + cache, + &mut resource_cache, + file_path.into(), + options, + ) + .expect("TODO"); - let blame = - repo.blame_file(Path::new(file_path), Some(&mut opts))?; + let commit_id = if let Some(commit_id) = commit_id { + commit_id + } else { + let repo = crate::sync::repo(repo_path)?; - let reader = BufReader::new(blob.content()); + utils::get_head_repo(&repo)? + }; - let unique_commit_ids: HashSet<_> = blame + let unique_commit_ids: HashSet<_> = outcome + .entries .iter() - .map(|hunk| CommitId::new(hunk.final_commit_id())) + .map(|entry| { + CommitId::new( + git2::Oid::from_bytes(entry.commit_id.as_bytes()) + .expect("TODO"), + ) + }) .collect(); let mut commit_ids = Vec::with_capacity(unique_commit_ids.len()); commit_ids.extend(unique_commit_ids); @@ -100,40 +96,45 @@ pub fn blame_file( .map(|commit_info| (commit_info.id, commit_info)) .collect(); - let lines: Vec<(Option, String)> = reader - .lines() - .enumerate() - .map(|(i, line)| { - // Line indices in a `FileBlame` are 1-based. - let corresponding_hunk = blame.get_line(i + 1); - - if let Some(hunk) = corresponding_hunk { - let commit_id = CommitId::new(hunk.final_commit_id()); - // Line indices in a `BlameHunk` are 1-based. - let start_line = - hunk.final_start_line().saturating_sub(1); - let end_line = - start_line.saturating_add(hunk.lines_in_hunk()); - - if let Some(commit_info) = - unique_commit_infos.get(&commit_id) - { - let hunk = BlameHunk { - commit_id, - author: commit_info.author.clone(), - time: commit_info.time, - start_line, - end_line, + // TODO + // The shape of data as returned by `entries_with_lines` is preferable to the one chosen here + // because the former is much closer to what the UI is going to need in the end. + let lines: Vec<(Option, String)> = outcome + .entries_with_lines() + .flat_map(|(entry, lines)| { + let commit_id = CommitId::new( + git2::Oid::from_bytes(entry.commit_id.as_bytes()) + .expect("TODO"), + ); + let start_in_blamed_file = + entry.start_in_blamed_file as usize; + + lines + .iter() + .enumerate() + .map(|(i, line)| { + // TODO + let trimmed_line = + line.to_string().trim_end().to_string(); + + if let Some(commit_info) = + unique_commit_infos.get(&commit_id) + { + return ( + Some(BlameHunk { + commit_id, + author: commit_info.author.clone(), + time: commit_info.time, + start_line: start_in_blamed_file + i, + end_line: start_in_blamed_file + i, + }), + trimmed_line, + ); }; - return ( - Some(hunk), - line.unwrap_or_else(|_| String::new()), - ); - } - } - - (None, line.unwrap_or_else(|_| String::new())) + (None, trimmed_line) + }) + .collect::>() }) .collect(); From c1c12ab1121bd9fc5c73347e7432dc0e29760748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20R=C3=BC=C3=9Fler?= Date: Thu, 3 Apr 2025 15:42:14 +0200 Subject: [PATCH 2/6] Replace expect by ? Add missing `+ 1` to make test pass. --- asyncgit/src/error.rs | 28 ++++++++++++++++++++++++++++ asyncgit/src/sync/blame.rs | 24 ++++++++---------------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/asyncgit/src/error.rs b/asyncgit/src/error.rs index 1578ed1e50..86195addca 100644 --- a/asyncgit/src/error.rs +++ b/asyncgit/src/error.rs @@ -123,6 +123,26 @@ pub enum Error { #[from] gix::object::find::existing::with_conversion::Error, ), + /// + #[error("gix::traverse::commit::topo error: {0}")] + GixTraverseCommitTopo(#[from] gix::traverse::commit::topo::Error), + + /// + #[error("gix::repository::commit_graph_if_enabled error: {0}")] + GixRepositoryCommitGraphIfEnabled( + #[from] gix::repository::commit_graph_if_enabled::Error, + ), + + /// + #[error("gix::repository::diff_resource_cache error: {0}")] + GixRepositoryDiffResourceCache( + #[from] Box, + ), + + /// + #[error("gix_blame error: {0}")] + GixBlame(#[from] gix_blame::Error), + /// #[error("amend error: config commit.gpgsign=true detected.\ngpg signing is not supported for amending non-last commits")] SignAmendNonLastCommit, @@ -156,3 +176,11 @@ impl From for Error { Self::GixDiscover(Box::new(error)) } } + +impl From for Error { + fn from( + error: gix::repository::diff_resource_cache::Error, + ) -> Self { + Self::GixRepositoryDiffResourceCache(Box::new(error)) + } +} diff --git a/asyncgit/src/sync/blame.rs b/asyncgit/src/sync/blame.rs index 17775be6ff..b347f52e39 100644 --- a/asyncgit/src/sync/blame.rs +++ b/asyncgit/src/sync/blame.rs @@ -53,9 +53,9 @@ pub fn blame_file( }; let cache: Option = - repo.commit_graph_if_enabled().expect("TODO"); + repo.commit_graph_if_enabled()?; let mut resource_cache = - repo.diff_resource_cache_for_tree_diff().expect("TODO"); + repo.diff_resource_cache_for_tree_diff()?; let options: gix_blame::Options = Default::default(); @@ -66,8 +66,7 @@ pub fn blame_file( &mut resource_cache, file_path.into(), options, - ) - .expect("TODO"); + )?; let commit_id = if let Some(commit_id) = commit_id { commit_id @@ -77,15 +76,10 @@ pub fn blame_file( utils::get_head_repo(&repo)? }; - let unique_commit_ids: HashSet<_> = outcome + let unique_commit_ids: HashSet = outcome .entries .iter() - .map(|entry| { - CommitId::new( - git2::Oid::from_bytes(entry.commit_id.as_bytes()) - .expect("TODO"), - ) - }) + .map(|entry| entry.commit_id.into()) .collect(); let mut commit_ids = Vec::with_capacity(unique_commit_ids.len()); commit_ids.extend(unique_commit_ids); @@ -102,10 +96,7 @@ pub fn blame_file( let lines: Vec<(Option, String)> = outcome .entries_with_lines() .flat_map(|(entry, lines)| { - let commit_id = CommitId::new( - git2::Oid::from_bytes(entry.commit_id.as_bytes()) - .expect("TODO"), - ); + let commit_id = entry.commit_id.into(); let start_in_blamed_file = entry.start_in_blamed_file as usize; @@ -126,7 +117,8 @@ pub fn blame_file( author: commit_info.author.clone(), time: commit_info.time, start_line: start_in_blamed_file + i, - end_line: start_in_blamed_file + i, + end_line: start_in_blamed_file + + i + 1, }), trimmed_line, ); From 8a05f4790f4eb45a6dad9d41ebde0aff88ab31ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20R=C3=BC=C3=9Fler?= Date: Thu, 17 Apr 2025 09:48:50 +0200 Subject: [PATCH 3/6] Remove unnecessary semicolon --- asyncgit/src/sync/blame.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asyncgit/src/sync/blame.rs b/asyncgit/src/sync/blame.rs index b347f52e39..231f4b7b28 100644 --- a/asyncgit/src/sync/blame.rs +++ b/asyncgit/src/sync/blame.rs @@ -122,7 +122,7 @@ pub fn blame_file( }), trimmed_line, ); - }; + } (None, trimmed_line) }) From b97bc57a485758368fd9deab1bb5a6d395dd4ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20R=C3=BC=C3=9Fler?= Date: Thu, 17 Apr 2025 17:19:05 +0200 Subject: [PATCH 4/6] Convert to unix separators on Windows --- asyncgit/src/sync/blame.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/asyncgit/src/sync/blame.rs b/asyncgit/src/sync/blame.rs index 231f4b7b28..23fa8caedd 100644 --- a/asyncgit/src/sync/blame.rs +++ b/asyncgit/src/sync/blame.rs @@ -42,6 +42,10 @@ pub fn blame_file( ) -> Result { scope_time!("blame_file"); + let file_path: &gix::bstr::BStr = file_path.into(); + let file_path = + gix::path::to_unix_separators_on_windows(file_path); + let repo: gix::Repository = gix::ThreadSafeRepository::discover_with_environment_overrides(repo_path.gitpath()) .map(Into::into)?; @@ -64,7 +68,7 @@ pub fn blame_file( tip, cache, &mut resource_cache, - file_path.into(), + &file_path, options, )?; @@ -132,7 +136,7 @@ pub fn blame_file( let file_blame = FileBlame { commit_id, - path: file_path.into(), + path: file_path.to_string(), lines, }; From 1ef6a7d484f1230c444d5060caba9fcb4d68703c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20R=C3=BC=C3=9Fler?= Date: Wed, 28 May 2025 19:52:33 +0200 Subject: [PATCH 5/6] Get diff algorithm from repo --- asyncgit/src/error.rs | 6 ++++++ asyncgit/src/sync/blame.rs | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/asyncgit/src/error.rs b/asyncgit/src/error.rs index 86195addca..8568a005fd 100644 --- a/asyncgit/src/error.rs +++ b/asyncgit/src/error.rs @@ -139,6 +139,12 @@ pub enum Error { #[from] Box, ), + /// + #[error("gix::config::diff::algorithm error: {0}")] + GixConfigDiffAlgorithm( + #[from] gix::config::diff::algorithm::Error, + ), + /// #[error("gix_blame error: {0}")] GixBlame(#[from] gix_blame::Error), diff --git a/asyncgit/src/sync/blame.rs b/asyncgit/src/sync/blame.rs index 23fa8caedd..b82267f351 100644 --- a/asyncgit/src/sync/blame.rs +++ b/asyncgit/src/sync/blame.rs @@ -61,7 +61,13 @@ pub fn blame_file( let mut resource_cache = repo.diff_resource_cache_for_tree_diff()?; - let options: gix_blame::Options = Default::default(); + let diff_algorithm = repo.diff_algorithm()?; + + let options: gix_blame::Options = gix_blame::Options { + diff_algorithm, + range: None, + since: None, + }; let outcome = gix_blame::file( &repo.objects, From c4720a9f8e96688e688a3568495a171622ed06b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20R=C3=BC=C3=9Fler?= Date: Wed, 28 May 2025 20:37:21 +0200 Subject: [PATCH 6/6] Box large error --- asyncgit/src/error.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/asyncgit/src/error.rs b/asyncgit/src/error.rs index 8568a005fd..6c15d08a4e 100644 --- a/asyncgit/src/error.rs +++ b/asyncgit/src/error.rs @@ -147,7 +147,7 @@ pub enum Error { /// #[error("gix_blame error: {0}")] - GixBlame(#[from] gix_blame::Error), + GixBlame(#[from] Box), /// #[error("amend error: config commit.gpgsign=true detected.\ngpg signing is not supported for amending non-last commits")] @@ -190,3 +190,9 @@ impl From for Error { Self::GixRepositoryDiffResourceCache(Box::new(error)) } } + +impl From for Error { + fn from(error: gix_blame::Error) -> Self { + Self::GixBlame(Box::new(error)) + } +}