Skip to content

Commit 2637f8f

Browse files
committed
change!: use gix-object::Find trait
1 parent bf1e688 commit 2637f8f

File tree

2 files changed

+31
-59
lines changed

2 files changed

+31
-59
lines changed

gix-diff/src/tree/changes.rs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{borrow::BorrowMut, collections::VecDeque};
22

3-
use gix_hash::{oid, ObjectId};
43
use gix_object::tree::EntryRef;
4+
use gix_object::FindExt;
55

66
use crate::{
77
tree,
@@ -12,24 +12,21 @@ use crate::{
1212
#[derive(Debug, thiserror::Error)]
1313
#[allow(missing_docs)]
1414
pub enum Error {
15-
#[error("The object {oid} referenced by the tree or the tree itself was not found in the database")]
16-
FindExisting {
17-
oid: ObjectId,
18-
source: Box<dyn std::error::Error + Send + Sync + 'static>,
19-
},
15+
#[error(transparent)]
16+
Find(#[from] gix_object::find::existing_iter::Error),
2017
#[error("The delegate cancelled the operation")]
2118
Cancelled,
2219
#[error(transparent)]
2320
EntriesDecode(#[from] gix_object::decode::Error),
2421
}
2522

2623
impl<'a> tree::Changes<'a> {
27-
/// Calculate the changes that would need to be applied to `self` to get `other`.
24+
/// Calculate the changes that would need to be applied to `self` to get `other` using `objects` to obtain objects as needed for traversal.
2825
///
2926
/// * The `state` maybe owned or mutably borrowed to allow reuses allocated data structures through multiple runs.
3027
/// * `locate` is a function `f(object_id, &mut buffer) -> Option<TreeIter>` to return a `TreeIter` for the given object id backing
3128
/// its data in the given buffer. Returning `None` is unexpected as these trees are obtained during iteration, and in a typical
32-
/// database errors are not expected either which is why the error case is omitted. To allow proper error reporting, [`Error::FindExisting`]
29+
/// database errors are not expected either which is why the error case is omitted. To allow proper error reporting, [`Error::Find`]
3330
/// should be converted into a more telling error.
3431
/// * `delegate` will receive the computed changes, see the [`Visit`][`tree::Visit`] trait for more information on what to expect.
3532
///
@@ -47,16 +44,14 @@ impl<'a> tree::Changes<'a> {
4744
///
4845
/// [git_cmp_c]: https://github.com/git/git/blob/311531c9de557d25ac087c1637818bd2aad6eb3a/tree-diff.c#L49:L65
4946
/// [git_cmp_rs]: https://github.com/Byron/gitoxide/blob/a4d5f99c8dc99bf814790928a3bf9649cd99486b/gix-object/src/mutable/tree.rs#L52-L55
50-
pub fn needed_to_obtain<FindFn, R, StateMut, E>(
47+
pub fn needed_to_obtain<R, StateMut>(
5148
mut self,
5249
other: gix_object::TreeRefIter<'_>,
5350
mut state: StateMut,
54-
mut find: FindFn,
51+
objects: impl gix_object::Find,
5552
delegate: &mut R,
5653
) -> Result<(), Error>
5754
where
58-
FindFn: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Result<gix_object::TreeRefIter<'b>, E>,
59-
E: std::error::Error + Send + Sync + 'static,
6055
R: tree::Visit,
6156
StateMut: BorrowMut<tree::State>,
6257
{
@@ -77,28 +72,16 @@ impl<'a> tree::Changes<'a> {
7772
match state.trees.pop_front() {
7873
Some((None, Some(rhs))) => {
7974
delegate.pop_front_tracked_path_and_set_current();
80-
rhs_entries = peekable(find(&rhs, &mut state.buf2).map_err(|err| Error::FindExisting {
81-
oid: rhs,
82-
source: err.into(),
83-
})?);
75+
rhs_entries = peekable(objects.find_tree_iter(&rhs, &mut state.buf2)?);
8476
}
8577
Some((Some(lhs), Some(rhs))) => {
8678
delegate.pop_front_tracked_path_and_set_current();
87-
lhs_entries = peekable(find(&lhs, &mut state.buf1).map_err(|err| Error::FindExisting {
88-
oid: lhs,
89-
source: err.into(),
90-
})?);
91-
rhs_entries = peekable(find(&rhs, &mut state.buf2).map_err(|err| Error::FindExisting {
92-
oid: rhs,
93-
source: err.into(),
94-
})?);
79+
lhs_entries = peekable(objects.find_tree_iter(&lhs, &mut state.buf1)?);
80+
rhs_entries = peekable(objects.find_tree_iter(&rhs, &mut state.buf2)?);
9581
}
9682
Some((Some(lhs), None)) => {
9783
delegate.pop_front_tracked_path_and_set_current();
98-
lhs_entries = peekable(find(&lhs, &mut state.buf1).map_err(|err| Error::FindExisting {
99-
oid: lhs,
100-
source: err.into(),
101-
})?);
84+
lhs_entries = peekable(objects.find_tree_iter(&lhs, &mut state.buf1)?);
10285
}
10386
Some((None, None)) => unreachable!("BUG: it makes no sense to fill the stack with empties"),
10487
None => return Ok(()),

gix-diff/tests/tree/mod.rs

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ mod changes {
6161
gix_diff::tree::Changes::from(lhs_tree).needed_to_obtain(
6262
rhs_tree,
6363
gix_diff::tree::State::default(),
64-
|oid, buf| {
65-
use gix_odb::pack::FindExt;
66-
db.find(oid, buf)
67-
.map(|obj| obj.0.try_into_tree_iter().expect("only called for trees"))
68-
},
64+
&db,
6965
&mut recorder,
7066
)?;
7167
Ok(recorder.records)
@@ -108,11 +104,7 @@ mod changes {
108104
gix_diff::tree::Changes::from(previous_tree).needed_to_obtain(
109105
current_tree,
110106
&mut gix_diff::tree::State::default(),
111-
|oid, buf| {
112-
use gix_odb::pack::FindExt;
113-
db.find(oid, buf)
114-
.map(|(obj, _)| obj.try_into_tree_iter().expect("only called for trees"))
115-
},
107+
&db,
116108
&mut recorder,
117109
)?;
118110
Ok(recorder.records)
@@ -141,27 +133,24 @@ mod changes {
141133
let mut buf = Vec::new();
142134

143135
let head = head_of(db);
144-
commit::Ancestors::new(Some(head), commit::ancestors::State::default(), |oid, buf| {
145-
use gix_object::FindExt;
146-
db.find_commit_iter(oid, buf)
147-
})
148-
.collect::<Result<Vec<_>, _>>()
149-
.expect("valid iteration")
150-
.into_iter()
151-
.map(|c| {
152-
use gix_object::FindExt;
153-
(
154-
db.find_commit(&c.id, &mut buf)
155-
.unwrap()
156-
.message
157-
.trim()
158-
.to_str_lossy()
159-
.into_owned(),
160-
c.id,
161-
)
162-
})
163-
.rev()
164-
.collect()
136+
commit::Ancestors::new(Some(head), commit::ancestors::State::default(), &db)
137+
.collect::<Result<Vec<_>, _>>()
138+
.expect("valid iteration")
139+
.into_iter()
140+
.map(|c| {
141+
use gix_object::FindExt;
142+
(
143+
db.find_commit(&c.id, &mut buf)
144+
.unwrap()
145+
.message
146+
.trim()
147+
.to_str_lossy()
148+
.into_owned(),
149+
c.id,
150+
)
151+
})
152+
.rev()
153+
.collect()
165154
}
166155

167156
#[test]

0 commit comments

Comments
 (0)