Skip to content

Commit 806ea47

Browse files
committed
change!: use gix-object::Find trait
1 parent 24e319e commit 806ea47

File tree

7 files changed

+50
-81
lines changed

7 files changed

+50
-81
lines changed

gitoxide-core/src/hours/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ where
175175
}
176176
commit_idx += 1;
177177
}
178-
Err(gix::traverse::commit::ancestors::Error::FindExisting { .. }) => {
178+
Err(gix::traverse::commit::ancestors::Error::Find { .. }) => {
179179
is_shallow = true;
180180
break;
181181
}

gitoxide-core/src/query/engine/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ pub fn update(
354354
break;
355355
}
356356
}
357-
Err(gix::traverse::commit::ancestors::Error::FindExisting { .. }) => {
357+
Err(gix::traverse::commit::ancestors::Error::Find { .. }) => {
358358
writeln!(err, "shallow repository - commit history is truncated").ok();
359359
break;
360360
}

gix-index/src/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ mod from_tree {
4141
{
4242
let _span = gix_features::trace::coarse!("gix_index::State::from_tree()");
4343
let mut buf = Vec::new();
44-
let root = find(tree, &mut buf).ok_or(breadthfirst::Error::NotFound { oid: tree.into() })?;
44+
let root = find(tree, &mut buf).ok_or(breadthfirst::Error::Find { oid: tree.into() })?;
4545

4646
let mut delegate = CollectEntries::new();
4747
breadthfirst(root, breadthfirst::State::default(), &mut find, &mut delegate)?;

gix-traverse/src/commit.rs

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use gix_object::FindExt;
12
use smallvec::SmallVec;
23

34
/// An iterator over the ancestors one or more starting commits
45
pub struct Ancestors<Find, Predicate, StateMut> {
5-
find: Find,
6+
objects: Find,
67
cache: Option<gix_commitgraph::Graph>,
78
predicate: Predicate,
89
state: StateMut,
@@ -95,7 +96,7 @@ pub mod ancestors {
9596
use gix_date::SecondsSinceUnixEpoch;
9697
use gix_hash::{oid, ObjectId};
9798
use gix_hashtable::HashSet;
98-
use gix_object::CommitRefIter;
99+
use gix_object::{CommitRefIter, FindExt};
99100
use smallvec::SmallVec;
100101

101102
use crate::commit::{collect_parents, Ancestors, Either, Info, ParentIds, Parents, Sorting};
@@ -104,11 +105,8 @@ pub mod ancestors {
104105
#[derive(Debug, thiserror::Error)]
105106
#[allow(missing_docs)]
106107
pub enum Error {
107-
#[error("The commit {oid} could not be found")]
108-
FindExisting {
109-
oid: ObjectId,
110-
source: Box<dyn std::error::Error + Send + Sync + 'static>,
111-
},
108+
#[error(transparent)]
109+
Find(#[from] gix_object::find::existing_iter::Error),
112110
#[error(transparent)]
113111
ObjectDecode(#[from] gix_object::decode::Error),
114112
}
@@ -147,11 +145,10 @@ pub mod ancestors {
147145
}
148146

149147
/// Builder
150-
impl<Find, Predicate, StateMut, E> Ancestors<Find, Predicate, StateMut>
148+
impl<Find, Predicate, StateMut> Ancestors<Find, Predicate, StateMut>
151149
where
152-
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
150+
Find: gix_object::Find,
153151
StateMut: BorrowMut<State>,
154-
E: std::error::Error + Send + Sync + 'static,
155152
{
156153
/// Set the sorting method, either topological or by author date
157154
pub fn sorting(mut self, sorting: Sorting) -> Result<Self, Error> {
@@ -164,11 +161,7 @@ pub mod ancestors {
164161
let cutoff_time = self.sorting.cutoff_time();
165162
let state = self.state.borrow_mut();
166163
for commit_id in state.next.drain(..) {
167-
let commit_iter =
168-
(self.find)(&commit_id, &mut state.buf).map_err(|err| Error::FindExisting {
169-
oid: commit_id,
170-
source: err.into(),
171-
})?;
164+
let commit_iter = self.objects.find_commit_iter(&commit_id, &mut state.buf)?;
172165
let time = commit_iter.committer()?.time.seconds;
173166
match cutoff_time {
174167
Some(cutoff_time) if time >= cutoff_time => {
@@ -214,11 +207,10 @@ pub mod ancestors {
214207
}
215208

216209
/// Initialization
217-
impl<Find, StateMut, E> Ancestors<Find, fn(&oid) -> bool, StateMut>
210+
impl<Find, StateMut> Ancestors<Find, fn(&oid) -> bool, StateMut>
218211
where
219-
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
212+
Find: gix_object::Find,
220213
StateMut: BorrowMut<State>,
221-
E: std::error::Error + Send + Sync + 'static,
222214
{
223215
/// Create a new instance.
224216
///
@@ -236,12 +228,11 @@ pub mod ancestors {
236228
}
237229

238230
/// Initialization
239-
impl<Find, Predicate, StateMut, E> Ancestors<Find, Predicate, StateMut>
231+
impl<Find, Predicate, StateMut> Ancestors<Find, Predicate, StateMut>
240232
where
241-
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
233+
Find: gix_object::Find,
242234
Predicate: FnMut(&oid) -> bool,
243235
StateMut: BorrowMut<State>,
244-
E: std::error::Error + Send + Sync + 'static,
245236
{
246237
/// Create a new instance with commit filtering enabled.
247238
///
@@ -274,7 +265,7 @@ pub mod ancestors {
274265
}
275266
}
276267
Self {
277-
find,
268+
objects: find,
278269
cache: None,
279270
predicate,
280271
state,
@@ -299,12 +290,11 @@ pub mod ancestors {
299290
}
300291
}
301292

302-
impl<Find, Predicate, StateMut, E> Iterator for Ancestors<Find, Predicate, StateMut>
293+
impl<Find, Predicate, StateMut> Iterator for Ancestors<Find, Predicate, StateMut>
303294
where
304-
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
295+
Find: gix_object::Find,
305296
Predicate: FnMut(&oid) -> bool,
306297
StateMut: BorrowMut<State>,
307-
E: std::error::Error + Send + Sync + 'static,
308298
{
309299
type Item = Result<Info, Error>;
310300

@@ -334,12 +324,11 @@ pub mod ancestors {
334324
}
335325

336326
/// Utilities
337-
impl<Find, Predicate, StateMut, E> Ancestors<Find, Predicate, StateMut>
327+
impl<Find, Predicate, StateMut> Ancestors<Find, Predicate, StateMut>
338328
where
339-
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
329+
Find: gix_object::Find,
340330
Predicate: FnMut(&oid) -> bool,
341331
StateMut: BorrowMut<State>,
342-
E: std::error::Error + Send + Sync + 'static,
343332
{
344333
fn next_by_commit_date(
345334
&mut self,
@@ -349,7 +338,7 @@ pub mod ancestors {
349338

350339
let (commit_time, oid) = state.queue.pop()?;
351340
let mut parents: ParentIds = Default::default();
352-
match super::find(self.cache.as_ref(), &mut self.find, &oid, &mut state.buf) {
341+
match super::find(self.cache.as_ref(), &self.objects, &oid, &mut state.buf) {
353342
Ok(Either::CachedCommit(commit)) => {
354343
if !collect_parents(&mut state.parent_ids, self.cache.as_ref(), commit.iter_parents()) {
355344
// drop corrupt caches and try again with ODB
@@ -380,7 +369,7 @@ pub mod ancestors {
380369
continue;
381370
}
382371

383-
let parent = (self.find)(id.as_ref(), &mut state.parents_buf).ok();
372+
let parent = self.objects.find_commit_iter(id.as_ref(), &mut state.parents_buf).ok();
384373
let parent_commit_time = parent
385374
.and_then(|parent| parent.committer().ok().map(|committer| committer.time.seconds))
386375
.unwrap_or_default();
@@ -395,12 +384,7 @@ pub mod ancestors {
395384
}
396385
}
397386
}
398-
Err(err) => {
399-
return Some(Err(Error::FindExisting {
400-
oid,
401-
source: err.into(),
402-
}))
403-
}
387+
Err(err) => return Some(Err(err.into())),
404388
}
405389
Some(Ok(Info {
406390
id: oid,
@@ -411,18 +395,17 @@ pub mod ancestors {
411395
}
412396

413397
/// Utilities
414-
impl<Find, Predicate, StateMut, E> Ancestors<Find, Predicate, StateMut>
398+
impl<Find, Predicate, StateMut> Ancestors<Find, Predicate, StateMut>
415399
where
416-
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
400+
Find: gix_object::Find,
417401
Predicate: FnMut(&oid) -> bool,
418402
StateMut: BorrowMut<State>,
419-
E: std::error::Error + Send + Sync + 'static,
420403
{
421404
fn next_by_topology(&mut self) -> Option<Result<Info, Error>> {
422405
let state = self.state.borrow_mut();
423406
let oid = state.next.pop_front()?;
424407
let mut parents: ParentIds = Default::default();
425-
match super::find(self.cache.as_ref(), &mut self.find, &oid, &mut state.buf) {
408+
match super::find(self.cache.as_ref(), &self.objects, &oid, &mut state.buf) {
426409
Ok(Either::CachedCommit(commit)) => {
427410
if !collect_parents(&mut state.parent_ids, self.cache.as_ref(), commit.iter_parents()) {
428411
// drop corrupt caches and try again with ODB
@@ -460,12 +443,7 @@ pub mod ancestors {
460443
}
461444
}
462445
}
463-
Err(err) => {
464-
return Some(Err(Error::FindExisting {
465-
oid,
466-
source: err.into(),
467-
}))
468-
}
446+
Err(err) => return Some(Err(err.into())),
469447
}
470448
Some(Ok(Info {
471449
id: oid,
@@ -503,18 +481,17 @@ fn collect_parents(
503481
true
504482
}
505483

506-
fn find<'cache, 'buf, Find, E>(
484+
fn find<'cache, 'buf, Find>(
507485
cache: Option<&'cache gix_commitgraph::Graph>,
508-
mut find: Find,
486+
objects: Find,
509487
id: &gix_hash::oid,
510488
buf: &'buf mut Vec<u8>,
511-
) -> Result<Either<'buf, 'cache>, E>
489+
) -> Result<Either<'buf, 'cache>, gix_object::find::existing_iter::Error>
512490
where
513-
Find: for<'a> FnMut(&gix_hash::oid, &'a mut Vec<u8>) -> Result<gix_object::CommitRefIter<'a>, E>,
514-
E: std::error::Error + Send + Sync + 'static,
491+
Find: gix_object::Find,
515492
{
516493
match cache.and_then(|cache| cache.commit_by_id(id).map(Either::CachedCommit)) {
517494
Some(c) => Ok(c),
518-
None => find(id, buf).map(Either::CommitRefIter),
495+
None => objects.find_commit_iter(id, buf).map(Either::CommitRefIter),
519496
}
520497
}

gix-traverse/src/tree/breadthfirst.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use gix_hash::ObjectId;
66
#[derive(Debug, thiserror::Error)]
77
#[allow(missing_docs)]
88
pub enum Error {
9-
#[error("The tree {oid} could not be found")]
10-
NotFound { oid: ObjectId },
9+
#[error(transparent)]
10+
Find(#[from] gix_object::find::existing_iter::Error),
1111
#[error("The delegate cancelled the operation")]
1212
Cancelled,
1313
#[error(transparent)]
@@ -31,8 +31,7 @@ impl State {
3131
pub(crate) mod impl_ {
3232
use std::borrow::BorrowMut;
3333

34-
use gix_hash::oid;
35-
use gix_object::{tree::EntryMode, TreeRefIter};
34+
use gix_object::{tree::EntryMode, FindExt, TreeRefIter};
3635

3736
use super::{Error, State};
3837
use crate::tree::Visit;
@@ -46,17 +45,17 @@ pub(crate) mod impl_ {
4645
/// * `find` - a way to lookup new object data during traversal by their `ObjectId`, writing their data into buffer and returning
4746
/// an iterator over entries if the object is present and is a tree. Caching should be implemented within this function
4847
/// as needed. The return value is `Option<TreeIter>` which degenerates all error information. Not finding a commit should also
49-
/// be considered an errors as all objects in the tree DAG should be present in the database. Hence [`Error::NotFound`] should
48+
/// be considered an errors as all objects in the tree DAG should be present in the database. Hence [`Error::Find`] should
5049
/// be escalated into a more specific error if its encountered by the caller.
5150
/// * `delegate` - A way to observe entries and control the iteration while allowing the optimizer to let you pay only for what you use.
5251
pub fn traverse<StateMut, Find, V>(
5352
root: TreeRefIter<'_>,
5453
mut state: StateMut,
55-
mut find: Find,
54+
objects: Find,
5655
delegate: &mut V,
5756
) -> Result<(), Error>
5857
where
59-
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Option<TreeRefIter<'a>>,
58+
Find: gix_object::Find,
6059
StateMut: BorrowMut<State>,
6160
V: Visit,
6261
{
@@ -95,10 +94,7 @@ pub(crate) mod impl_ {
9594
match state.next.pop_front() {
9695
Some(oid) => {
9796
delegate.pop_front_tracked_path_and_set_current();
98-
match find(&oid, &mut state.buf) {
99-
Some(tree_iter) => tree = tree_iter,
100-
None => return Err(Error::NotFound { oid: oid.to_owned() }),
101-
}
97+
tree = objects.find_tree_iter(&oid, &mut state.buf)?;
10298
}
10399
None => break Ok(()),
104100
}

gix-traverse/tests/commit/mod.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
mod ancestor {
22
use gix_hash::{oid, ObjectId};
3-
use gix_odb::pack::FindExt;
43
use gix_traverse::commit;
54

65
use crate::hex_to_id;
@@ -68,7 +67,7 @@ mod ancestor {
6867
let oids = commit::Ancestors::filtered(
6968
tips.clone(),
7069
commit::ancestors::State::default(),
71-
|oid, buf| store.find_commit_iter(oid, buf).map(|t| t.0),
70+
&store,
7271
predicate.clone(),
7372
)
7473
.sorting(self.sorting)?
@@ -86,14 +85,12 @@ mod ancestor {
8685
let (store, tips, expected) = self.setup()?;
8786

8887
for use_commitgraph in [false, true] {
89-
let oids = commit::Ancestors::new(tips.clone(), commit::ancestors::State::default(), |oid, buf| {
90-
store.find_commit_iter(oid, buf).map(|t| t.0)
91-
})
92-
.sorting(self.sorting)?
93-
.parents(self.mode)
94-
.commit_graph(self.setup_commitgraph(store.store_ref(), use_commitgraph))
95-
.map(|res| res.map(|info| info.id))
96-
.collect::<Result<Vec<_>, _>>()?;
88+
let oids = commit::Ancestors::new(tips.clone(), commit::ancestors::State::default(), &store)
89+
.sorting(self.sorting)?
90+
.parents(self.mode)
91+
.commit_graph(self.setup_commitgraph(store.store_ref(), use_commitgraph))
92+
.map(|res| res.map(|info| info.id))
93+
.collect::<Result<Vec<_>, _>>()?;
9794
assert_eq!(oids, expected);
9895
}
9996
Ok(())
@@ -346,7 +343,6 @@ mod ancestor {
346343

347344
/// Some dates adjusted to be a year apart, but still 'c1' and 'c2' with the same date.
348345
mod adjusted_dates {
349-
use gix_object::FindExt;
350346
use gix_traverse::commit::{ancestors, Ancestors, Parents, Sorting};
351347

352348
use crate::{commit::ancestor::TraversalAssertion, hex_to_id};
@@ -403,7 +399,7 @@ mod ancestor {
403399
let iter = Ancestors::new(
404400
Some(hex_to_id("9902e3c3e8f0c569b4ab295ddf473e6de763e1e7" /* c2 */)),
405401
ancestors::State::default(),
406-
move |oid, buf| store.find_commit_iter(oid, buf),
402+
&store,
407403
)
408404
.sorting(Sorting::ByCommitTimeNewestFirstCutoffOlderThan {
409405
seconds: 978393600, // =2001-01-02 00:00:00 +0000

gix-traverse/tests/tree/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn breadth_first_full_path() -> crate::Result<()> {
2424
db.find_tree_iter(&commit.tree_id().expect("a tree is available in a commit"), &mut buf2)?
2525
.0,
2626
tree::breadthfirst::State::default(),
27-
|oid, buf| db.find_tree_iter(oid, buf).ok().map(|t| t.0),
27+
&db,
2828
&mut recorder,
2929
)?;
3030

@@ -111,7 +111,7 @@ fn breadth_first_filename_only() -> crate::Result<()> {
111111
db.find_tree_iter(&commit.tree_id().expect("a tree is available in a commit"), &mut buf2)?
112112
.0,
113113
tree::breadthfirst::State::default(),
114-
|oid, buf| db.find_tree_iter(oid, buf).ok().map(|t| t.0),
114+
&db,
115115
&mut recorder,
116116
)?;
117117

@@ -138,7 +138,7 @@ fn breadth_first_no_location() -> crate::Result<()> {
138138
db.find_tree_iter(&commit.tree_id().expect("a tree is available in a commit"), &mut buf2)?
139139
.0,
140140
tree::breadthfirst::State::default(),
141-
|oid, buf| db.find_tree_iter(oid, buf).ok().map(|t| t.0),
141+
&db,
142142
&mut recorder,
143143
)?;
144144

0 commit comments

Comments
 (0)