diff --git a/Cargo.lock b/Cargo.lock index 2abe61b64a..440ee9f89a 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,22 +1236,27 @@ 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-object", "gix-odb", "gix-pack", "gix-path", + "gix-pathspec", "gix-protocol", "gix-ref", "gix-refspec", @@ -1235,12 +1264,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", @@ -1260,6 +1291,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" @@ -1269,6 +1317,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" @@ -1357,8 +1424,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", ] @@ -1398,6 +1474,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" @@ -1447,6 +1544,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" @@ -1559,6 +1669,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" @@ -1572,6 +1694,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.0", + "bstr", + "gix-attributes", + "gix-config-value", + "gix-glob", + "gix-path", + "thiserror 2.0.12", +] + [[package]] name = "gix-protocol" version = "0.49.0" @@ -1694,12 +1831,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", @@ -1780,6 +1933,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" @@ -2004,6 +2176,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" @@ -2177,6 +2358,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 7966e82e5c..b591620efb 100644 --- a/asyncgit/Cargo.toml +++ b/asyncgit/Cargo.toml @@ -22,7 +22,9 @@ git2-hooks = { path = "../git2-hooks", version = ">=0.4" } gix = { version = "0.71.0", default-features = false, features = [ "max-performance", "revision", + "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/error.rs b/asyncgit/src/error.rs index 2113e93d6e..e226b18cd2 100644 --- a/asyncgit/src/error.rs +++ b/asyncgit/src/error.rs @@ -113,6 +113,32 @@ pub enum Error { #[error("gix::revision::walk error: {0}")] GixRevisionWalk(#[from] gix::revision::walk::Error), + /// + #[error("gix::traverse::commit::topo error: {0}")] + GixTraverseCommitTopo(#[from] gix::traverse::commit::topo::Error), + + /// + #[error("gix::repository::diff_resource_cache error: {0}")] + GixRepositoryDiffResourceCache( + #[from] Box, + ), + + /// + #[error("gix::repository::commit_graph_if_enabled error: {0}")] + GixRepositoryCommitGraphIfEnabled( + #[from] gix::repository::commit_graph_if_enabled::Error, + ), + + /// + #[error("gix::config::diff::algorithm error: {0}")] + GixConfigDiffAlgorithm( + #[from] gix::config::diff::algorithm::Error, + ), + + /// + #[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, @@ -146,3 +172,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 19f125f6b3..5043c2012c 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,17 +34,13 @@ 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() - } +fn object_id_to_oid(object_id: gix::ObjectId) -> git2::Oid { + // TODO + // This should not fail. It will also become obsolete once `gix::ObjectId` is used throughout + // `gitui`. + #[allow(clippy::expect_used)] + git2::Oid::from_bytes(object_id.as_bytes()) + .expect("ObjectId could not be converted to Oid") } /// @@ -61,35 +51,54 @@ 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 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)?; + 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()?; + let mut resource_cache = + repo.diff_resource_cache_for_tree_diff()?; - let object = repo.revparse_single(&spec)?; - let blob = repo.find_blob(object.id())?; + let diff_algorithm = repo.diff_algorithm()?; - if blob.is_binary() { - return Err(Error::NoBlameOnBinaryFile); - } + let options = gix_blame::Options { + diff_algorithm, + range: None, + since: None, + }; - 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, + options, + )?; - 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(object_id_to_oid(entry.commit_id))) .collect(); let mut commit_ids = Vec::with_capacity(unique_commit_ids.len()); commit_ids.extend(unique_commit_ids); @@ -100,46 +109,50 @@ 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, - }; - - return ( - Some(hunk), - line.unwrap_or_else(|_| String::new()), - ); - } - } - - (None, line.unwrap_or_else(|_| String::new())) + // 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(object_id_to_oid(entry.commit_id)); + 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 + 1, + }), + trimmed_line, + ); + } + + (None, trimmed_line) + }) + .collect::>() }) .collect(); let file_blame = FileBlame { commit_id, - path: file_path.into(), + path: file_path.to_string(), lines, };