Skip to content

Add Either::tree_id #1910

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 2 commits into from
Mar 24, 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
13 changes: 3 additions & 10 deletions gix-blame/src/file/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,12 @@ fn tree_diff_at_file_path(
lhs_tree_buf: &mut Vec<u8>,
rhs_tree_buf: &mut Vec<u8>,
) -> Result<Option<gix_diff::tree::recorder::Change>, Error> {
let parent_tree_id = tree_id(find_commit(cache, &odb, &parent_id, commit_buf)?)?;
let parent_tree_id = find_commit(cache, &odb, &parent_id, commit_buf)?.tree_id()?;

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

let tree_id = tree_id(find_commit(cache, &odb, &id, commit_buf)?)?;
let tree_id = find_commit(cache, &odb, &id, commit_buf)?.tree_id()?;

let tree_iter = odb.find_tree_iter(&tree_id, rhs_tree_buf)?;
stats.trees_decoded += 1;
Expand Down Expand Up @@ -654,7 +654,7 @@ fn find_path_entry_in_commit(
buf2: &mut Vec<u8>,
stats: &mut Statistics,
) -> Result<Option<ObjectId>, Error> {
let tree_id = tree_id(find_commit(cache, odb, commit, buf)?)?;
let tree_id = find_commit(cache, odb, commit, buf)?.tree_id()?;
let tree_iter = odb.find_tree_iter(&tree_id, buf)?;
stats.trees_decoded += 1;

Expand Down Expand Up @@ -710,13 +710,6 @@ fn collect_parents(
Ok(parent_ids)
}

fn tree_id(commit: gix_traverse::commit::Either<'_, '_>) -> Result<ObjectId, Error> {
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
11 changes: 11 additions & 0 deletions gix-traverse/src/commit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ pub enum Either<'buf, 'cache> {
CachedCommit(gix_commitgraph::file::Commit<'cache>),
}

impl Either<'_, '_> {
/// Get a commits `tree_id` by either getting it from a [`gix_commitgraph::Graph`], if
/// present, or a [`gix_object::CommitRefIter`] otherwise.
pub fn tree_id(self) -> Result<ObjectId, gix_object::decode::Error> {
match self {
Self::CommitRefIter(mut commit_ref_iter) => commit_ref_iter.tree_id(),
Self::CachedCommit(commit) => Ok(commit.root_tree_id().into()),
}
}
}

/// Find information about a commit by either getting it from a [`gix_commitgraph::Graph`], if
/// present, or a [`gix_object::CommitRefIter`] otherwise.
pub fn find<'cache, 'buf, Find>(
Expand Down
Loading