History

fix!: Don't degenerate errors when accessing objects

Sebastian Thiel committed 4 years ago

Commit 6d9533d1 parent 867fc202 files · +29 −15Browse files

Showing the changes to git-diff/src (1 of 2 files). Show all

Unified · Split

git-diff/src/tree/changes.rs+23−7
@@ -12,7 +12,10 @@ use crate::{ · 11 unmodified lines
1212 #[allow(missing_docs)]
1313 pub enum Error {
1414 #[error("The object {oid} referenced by the tree or the tree itself was not found in the database")]
15− NotFound { oid: ObjectId },
15+ FindExisting {
16+ oid: ObjectId,
17+ source: Box<dyn std::error::Error + Send + Sync + 'static>,
18+ },
1619 #[error("The delegate cancelled the operation")]
1720 Cancelled,
1821 #[error(transparent)]
@@ -43,7 +46,7 @@ impl<'a> tree::Changes<'a> { · 24 unmodified lines
4346 ///
4447 /// [git_cmp_c]: https://github.com/git/git/blob/311531c9de557d25ac087c1637818bd2aad6eb3a/tree-diff.c#L49:L65
4548 /// [git_cmp_rs]: https://github.com/Byron/gitoxide/blob/a4d5f99c8dc99bf814790928a3bf9649cd99486b/git-object/src/mutable/tree.rs#L52-L55
46− pub fn needed_to_obtain<FindFn, R, StateMut>(
49+ pub fn needed_to_obtain<FindFn, R, StateMut, E>(
4750 mut self,
4851 other: git_object::TreeRefIter<'_>,
4952 mut state: StateMut,
@@ -51,7 +54,8 @@ impl<'a> tree::Changes<'a> { · 1 unmodified line
5154 delegate: &mut R,
5255 ) -> Result<(), Error>
5356 where
54− FindFn: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Option<git_object::TreeRefIter<'b>>,
57+ FindFn: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Result<git_object::TreeRefIter<'b>, E>,
58+ E: std::error::Error + Send + Sync + 'static,
5559 R: tree::Visit,
5660 StateMut: BorrowMut<tree::State>,
5761 {
@@ -72,16 +76,28 @@ impl<'a> tree::Changes<'a> { · 14 unmodified lines
7276 match state.trees.pop_front() {
7377 Some((None, Some(rhs))) => {
7478 delegate.pop_front_tracked_path_and_set_current();
75− rhs_entries = peekable(find(&rhs, &mut state.buf2).ok_or(Error::NotFound { oid: rhs })?);
79+ rhs_entries = peekable(find(&rhs, &mut state.buf2).map_err(|err| Error::FindExisting {
80+ oid: rhs,
81+ source: err.into(),
82+ })?);
7683 }
7784 Some((Some(lhs), Some(rhs))) => {
7885 delegate.pop_front_tracked_path_and_set_current();
79− lhs_entries = peekable(find(&lhs, &mut state.buf1).ok_or(Error::NotFound { oid: lhs })?);
80− rhs_entries = peekable(find(&rhs, &mut state.buf2).ok_or(Error::NotFound { oid: rhs })?);
86+ lhs_entries = peekable(find(&lhs, &mut state.buf1).map_err(|err| Error::FindExisting {
87+ oid: lhs,
88+ source: err.into(),
89+ })?);
90+ rhs_entries = peekable(find(&rhs, &mut state.buf2).map_err(|err| Error::FindExisting {
91+ oid: rhs,
92+ source: err.into(),
93+ })?);
8194 }
8295 Some((Some(lhs), None)) => {
8396 delegate.pop_front_tracked_path_and_set_current();
84− lhs_entries = peekable(find(&lhs, &mut state.buf1).ok_or(Error::NotFound { oid: lhs })?);
97+ lhs_entries = peekable(find(&lhs, &mut state.buf1).map_err(|err| Error::FindExisting {
98+ oid: lhs,
99+ source: err.into(),
100+ })?);
85101 }
86102 Some((None, None)) => unreachable!("BUG: it makes no sense to fill the stack with empties"),
87103 None => return Ok(()),