diff --git a/Cargo.lock b/Cargo.lock index ecf304a412..d30b8876d3 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.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f151000bf662ef5f641eca6102d942ee31ace80f271a3ef642e99776ce6ddb38" +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,21 @@ dependencies = [ "thiserror 2.0.12", ] +[[package]] +name = "gix-blame" +version = "0.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adc795e239a2347eb50ed18b8c529382dd8b62439c57277f79af3d8f8928a986" +dependencies = [ + "gix-diff", + "gix-hash", + "gix-object", + "gix-trace", + "gix-traverse", + "gix-worktree", + "thiserror 2.0.12", +] + [[package]] name = "gix-chunk" version = "0.4.11" @@ -1357,8 +1420,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 +1470,27 @@ dependencies = [ "walkdir", ] +[[package]] +name = "gix-filter" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdcc36cd7dbc63ed0ec3558645886553d1afd3cd09daa5efb9cba9cceb942bbb" +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 +1540,19 @@ dependencies = [ "parking_lot", ] +[[package]] +name = "gix-ignore" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f529dcb80bf9855c0a7c49f0ac588df6d6952d63a63fefc254b9c869d2cdf6f" +dependencies = [ + "bstr", + "gix-glob", + "gix-path", + "gix-trace", + "unicode-bom", +] + [[package]] name = "gix-index" version = "0.39.0" @@ -1559,6 +1665,18 @@ dependencies = [ "thiserror 2.0.12", ] +[[package]] +name = "gix-packetline-blocking" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1cbf8767c6abd5a6779f586702b5bcd8702380f4208219449cf1c9d0cd1e17c" +dependencies = [ + "bstr", + "faster-hex", + "gix-trace", + "thiserror 2.0.12", +] + [[package]] name = "gix-path" version = "0.10.15" @@ -1572,6 +1690,21 @@ dependencies = [ "thiserror 2.0.12", ] +[[package]] +name = "gix-pathspec" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6430d3a686c08e9d59019806faa78c17315fe22ae73151a452195857ca02f86c" +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 +1827,28 @@ dependencies = [ "thiserror 2.0.12", ] +[[package]] +name = "gix-submodule" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74972fe8d46ac8a09490ae1e843b4caf221c5b157c5ac17057e8e1c38417a3ac" +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 +1929,25 @@ dependencies = [ "thiserror 2.0.12", ] +[[package]] +name = "gix-worktree" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6673512f7eaa57a6876adceca6978a501d6c6569a4f177767dc405f8b9778958" +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 +2172,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 +2354,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..b2d4223a62 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.0.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..0772e4f1a4 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,52 @@ pub fn blame_file( ) -> Result { scope_time!("blame_file"); - let repo = repo(repo_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 traverse = gix::traverse::commit::topo::Builder::from_iters( + &repo.objects, + [tip], + None::>, + ) + .build() + .expect("TODO"); + + let mut resource_cache = + repo.diff_resource_cache_for_tree_diff().expect("TODO"); + + let outcome = gix_blame::file( + &repo.objects, + traverse, + &mut resource_cache, + file_path.into(), + None, + ) + .expect("TODO"); let commit_id = if let Some(commit_id) = commit_id { commit_id } else { + let repo = crate::sync::repo(repo_path)?; + utils::get_head_repo(&repo)? }; - let spec = - format!("{}:{}", commit_id, fixup_windows_path(file_path)); - - let object = repo.revparse_single(&spec)?; - let blob = repo.find_blob(object.id())?; - - if blob.is_binary() { - return Err(Error::NoBlameOnBinaryFile); - } - - let mut opts = BlameOptions::new(); - opts.newest_commit(commit_id.into()); - - let blame = - repo.blame_file(Path::new(file_path), Some(&mut opts))?; - - let reader = BufReader::new(blob.content()); - - 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 +98,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();