Skip to content

Commit 544cdaf

Browse files
authored
Merge pull request #1910 from cruessler/add-tree-id-to-either
Add `Either::tree_id`
2 parents c8c42b4 + 3fad860 commit 544cdaf

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

gix-blame/src/file/function.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,12 @@ fn tree_diff_at_file_path(
452452
lhs_tree_buf: &mut Vec<u8>,
453453
rhs_tree_buf: &mut Vec<u8>,
454454
) -> Result<Option<gix_diff::tree::recorder::Change>, Error> {
455-
let parent_tree_id = tree_id(find_commit(cache, &odb, &parent_id, commit_buf)?)?;
455+
let parent_tree_id = find_commit(cache, &odb, &parent_id, commit_buf)?.tree_id()?;
456456

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

460-
let tree_id = tree_id(find_commit(cache, &odb, &id, commit_buf)?)?;
460+
let tree_id = find_commit(cache, &odb, &id, commit_buf)?.tree_id()?;
461461

462462
let tree_iter = odb.find_tree_iter(&tree_id, rhs_tree_buf)?;
463463
stats.trees_decoded += 1;
@@ -654,7 +654,7 @@ fn find_path_entry_in_commit(
654654
buf2: &mut Vec<u8>,
655655
stats: &mut Statistics,
656656
) -> Result<Option<ObjectId>, Error> {
657-
let tree_id = tree_id(find_commit(cache, odb, commit, buf)?)?;
657+
let tree_id = find_commit(cache, odb, commit, buf)?.tree_id()?;
658658
let tree_iter = odb.find_tree_iter(&tree_id, buf)?;
659659
stats.trees_decoded += 1;
660660

@@ -710,13 +710,6 @@ fn collect_parents(
710710
Ok(parent_ids)
711711
}
712712

713-
fn tree_id(commit: gix_traverse::commit::Either<'_, '_>) -> Result<ObjectId, Error> {
714-
match commit {
715-
gix_traverse::commit::Either::CommitRefIter(mut commit_ref_iter) => Ok(commit_ref_iter.tree_id()?),
716-
gix_traverse::commit::Either::CachedCommit(commit) => Ok(commit.root_tree_id().into()),
717-
}
718-
}
719-
720713
/// Return an iterator over tokens for use in diffing. These are usually lines, but it's important
721714
/// to unify them so the later access shows the right thing.
722715
pub(crate) fn tokens_for_diffing(data: &[u8]) -> impl TokenSource<Token = &[u8]> {

gix-traverse/src/commit/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ pub enum Either<'buf, 'cache> {
7777
CachedCommit(gix_commitgraph::file::Commit<'cache>),
7878
}
7979

80+
impl Either<'_, '_> {
81+
/// Get a commits `tree_id` by either getting it from a [`gix_commitgraph::Graph`], if
82+
/// present, or a [`gix_object::CommitRefIter`] otherwise.
83+
pub fn tree_id(self) -> Result<ObjectId, gix_object::decode::Error> {
84+
match self {
85+
Self::CommitRefIter(mut commit_ref_iter) => commit_ref_iter.tree_id(),
86+
Self::CachedCommit(commit) => Ok(commit.root_tree_id().into()),
87+
}
88+
}
89+
}
90+
8091
/// Find information about a commit by either getting it from a [`gix_commitgraph::Graph`], if
8192
/// present, or a [`gix_object::CommitRefIter`] otherwise.
8293
pub fn find<'cache, 'buf, Find>(

0 commit comments

Comments
 (0)