Skip to content

Replace odb.find_commit by gix_traverse::commit::find #1824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions gix-blame/src/file/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,16 @@ pub fn file(

let mut stats = Statistics::default();
let (mut buf, mut buf2, mut buf3) = (Vec::new(), Vec::new(), Vec::new());
let blamed_file_entry_id = find_path_entry_in_commit(&odb, &suspect, file_path, &mut buf, &mut buf2, &mut stats)?
.ok_or_else(|| Error::FileMissing {
let blamed_file_entry_id = find_path_entry_in_commit(
&odb,
&suspect,
file_path,
cache.as_ref(),
&mut buf,
&mut buf2,
&mut stats,
)?
.ok_or_else(|| Error::FileMissing {
file_path: file_path.to_owned(),
commit_id: suspect,
})?;
Expand Down Expand Up @@ -135,7 +143,15 @@ pub fn file(
.filter(|(id, _)| *id == suspect)
.map(|(_, entry)| entry);
if entry.is_none() {
entry = find_path_entry_in_commit(&odb, &suspect, file_path, &mut buf, &mut buf2, &mut stats)?;
entry = find_path_entry_in_commit(
&odb,
&suspect,
file_path,
cache.as_ref(),
&mut buf,
&mut buf2,
&mut stats,
)?;
}

let Some(entry_id) = entry else {
Expand Down Expand Up @@ -177,9 +193,15 @@ pub fn file(
}

for (pid, (parent_id, parent_commit_time)) in parent_ids.iter().enumerate() {
if let Some(parent_entry_id) =
find_path_entry_in_commit(&odb, parent_id, file_path, &mut buf, &mut buf2, &mut stats)?
{
if let Some(parent_entry_id) = find_path_entry_in_commit(
&odb,
parent_id,
file_path,
cache.as_ref(),
&mut buf,
&mut buf2,
&mut stats,
)? {
let no_change_in_entry = entry_id == parent_entry_id;
if pid == 0 {
previous_entry = Some((*parent_id, parent_entry_id));
Expand All @@ -200,6 +222,7 @@ pub fn file(
file_path,
suspect,
parent_id,
cache.as_ref(),
&mut stats,
&mut diff_state,
&mut buf,
Expand Down Expand Up @@ -401,20 +424,19 @@ fn tree_diff_at_file_path(
file_path: &BStr,
id: ObjectId,
parent_id: ObjectId,
cache: Option<&gix_commitgraph::Graph>,
stats: &mut Statistics,
state: &mut gix_diff::tree::State,
commit_buf: &mut Vec<u8>,
lhs_tree_buf: &mut Vec<u8>,
rhs_tree_buf: &mut Vec<u8>,
) -> Result<Option<gix_diff::tree::recorder::Change>, Error> {
let parent_tree = odb.find_commit(&parent_id, commit_buf)?.tree();
stats.commits_to_tree += 1;
let parent_tree_id = tree_id(find_commit(cache, &odb, &parent_id, commit_buf)?)?;

let parent_tree_iter = odb.find_tree_iter(&parent_tree, lhs_tree_buf)?;
let parent_tree_iter = odb.find_tree_iter(&parent_tree_id, lhs_tree_buf)?;
stats.trees_decoded += 1;

let tree_id = odb.find_commit(&id, commit_buf)?.tree();
stats.commits_to_tree += 1;
let tree_id = tree_id(find_commit(cache, &odb, &id, commit_buf)?)?;

let tree_iter = odb.find_tree_iter(&tree_id, rhs_tree_buf)?;
stats.trees_decoded += 1;
Expand Down Expand Up @@ -605,13 +627,13 @@ fn find_path_entry_in_commit(
odb: &impl gix_object::Find,
commit: &gix_hash::oid,
file_path: &BStr,
cache: Option<&gix_commitgraph::Graph>,
buf: &mut Vec<u8>,
buf2: &mut Vec<u8>,
stats: &mut Statistics,
) -> Result<Option<ObjectId>, Error> {
let commit_id = odb.find_commit(commit, buf)?.tree();
stats.commits_to_tree += 1;
let tree_iter = odb.find_tree_iter(&commit_id, buf)?;
let tree_id = tree_id(find_commit(cache, odb, commit, buf)?)?;
let tree_iter = odb.find_tree_iter(&tree_id, buf)?;
stats.trees_decoded += 1;

let res = tree_iter.lookup_entry(
Expand Down Expand Up @@ -666,6 +688,13 @@ fn collect_parents(
Ok(parent_ids)
}

fn tree_id(commit: gix_traverse::commit::Either<'_, '_>) -> Result<ObjectId, Error> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could imagine providing this function as method on Either one day, which by then could probably also be renamed in something more specific.

match commit {
gix_traverse::commit::Either::CommitRefIter(mut commit_ref_iter) => Ok(commit_ref_iter.tree_id()?),
gix_traverse::commit::Either::CachedCommit(commit) => Ok(commit.root_tree_id().into()),
}
}

/// Return an iterator over tokens for use in diffing. These are usually lines, but it's important
/// to unify them so the later access shows the right thing.
pub(crate) fn tokens_for_diffing(data: &[u8]) -> impl TokenSource<Token = &[u8]> {
Expand Down
2 changes: 0 additions & 2 deletions gix-blame/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ pub struct Outcome {
pub struct Statistics {
/// The amount of commits it traversed until the blame was complete.
pub commits_traversed: usize,
/// The amount of commits whose trees were extracted.
pub commits_to_tree: usize,
/// The amount of trees that were decoded to find the entry of the file to blame.
pub trees_decoded: usize,
/// The amount of tree-diffs to see if the filepath was added, deleted or modified. These diffs
Expand Down
Loading