Skip to content

Commit 98bcb14

Browse files
committedAug 20, 2024
feat: add Repository::find_*() methods for every object type.
1 parent 63c7a03 commit 98bcb14

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed
 

‎gix/src/object/errors.rs

+13
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ pub mod find {
2929
pub mod existing {
3030
/// An object could not be found in the database, or an error occurred when trying to obtain it.
3131
pub type Error = gix_object::find::existing::Error;
32+
///
33+
#[allow(clippy::empty_docs)]
34+
pub mod with_conversion {
35+
/// The error returned by [Repository::find_commit()](crate::Repository::find_commit).
36+
#[derive(Debug, thiserror::Error)]
37+
#[allow(missing_docs)]
38+
pub enum Error {
39+
#[error(transparent)]
40+
Find(#[from] crate::object::find::existing::Error),
41+
#[error(transparent)]
42+
Convert(#[from] crate::object::try_into::Error),
43+
}
44+
}
3245
}
3346
}
3447

‎gix/src/repository/object.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use gix_ref::{
1010
};
1111
use smallvec::SmallVec;
1212

13-
use crate::{commit, ext::ObjectIdExt, object, tag, Blob, Id, Object, Reference, Tree};
13+
use crate::{commit, ext::ObjectIdExt, object, tag, Blob, Commit, Id, Object, Reference, Tag, Tree};
1414

1515
/// Methods related to object creation.
1616
impl crate::Repository {
@@ -38,6 +38,35 @@ impl crate::Repository {
3838
Ok(Object::from_data(id, kind, buf, self))
3939
}
4040

41+
/// Find a commit with `id` or fail if there was no object or the object wasn't a commit.
42+
pub fn find_commit(
43+
&self,
44+
id: impl Into<ObjectId>,
45+
) -> Result<Commit<'_>, object::find::existing::with_conversion::Error> {
46+
Ok(self.find_object(id)?.try_into_commit()?)
47+
}
48+
49+
/// Find a tree with `id` or fail if there was no object or the object wasn't a tree.
50+
pub fn find_tree(
51+
&self,
52+
id: impl Into<ObjectId>,
53+
) -> Result<Tree<'_>, object::find::existing::with_conversion::Error> {
54+
Ok(self.find_object(id)?.try_into_tree()?)
55+
}
56+
57+
/// Find an annotated tag with `id` or fail if there was no object or the object wasn't a tag.
58+
pub fn find_tag(&self, id: impl Into<ObjectId>) -> Result<Tag<'_>, object::find::existing::with_conversion::Error> {
59+
Ok(self.find_object(id)?.try_into_tag()?)
60+
}
61+
62+
/// Find a blob with `id` or fail if there was no object or the object wasn't a blob.
63+
pub fn find_blob(
64+
&self,
65+
id: impl Into<ObjectId>,
66+
) -> Result<Blob<'_>, object::find::existing::with_conversion::Error> {
67+
Ok(self.find_object(id)?.try_into_blob()?)
68+
}
69+
4170
/// Obtain information about an object without fully decoding it, or fail if the object doesn't exist.
4271
///
4372
/// Note that despite being cheaper than [`Self::find_object()`], there is still some effort traversing delta-chains.

0 commit comments

Comments
 (0)
Please sign in to comment.