@@ -26,7 +26,7 @@ impl crate::Repository {
26
26
#[ momo]
27
27
pub fn find_object ( & self , id : impl Into < ObjectId > ) -> Result < Object < ' _ > , object:: find:: existing:: Error > {
28
28
let id = id. into ( ) ;
29
- if id == gix_hash :: ObjectId :: empty_tree ( self . object_hash ( ) ) {
29
+ if id == ObjectId :: empty_tree ( self . object_hash ( ) ) {
30
30
return Ok ( Object {
31
31
id,
32
32
kind : gix_object:: Kind :: Tree ,
@@ -46,7 +46,7 @@ impl crate::Repository {
46
46
#[ momo]
47
47
pub fn find_header ( & self , id : impl Into < ObjectId > ) -> Result < gix_odb:: find:: Header , object:: find:: existing:: Error > {
48
48
let id = id. into ( ) ;
49
- if id == gix_hash :: ObjectId :: empty_tree ( self . object_hash ( ) ) {
49
+ if id == ObjectId :: empty_tree ( self . object_hash ( ) ) {
50
50
return Ok ( gix_odb:: find:: Header :: Loose {
51
51
kind : gix_object:: Kind :: Tree ,
52
52
size : 0 ,
@@ -55,6 +55,25 @@ impl crate::Repository {
55
55
self . objects . header ( id)
56
56
}
57
57
58
+ /// Return `true` if `id` exists in the object database.
59
+ ///
60
+ /// # Performance
61
+ ///
62
+ /// This method can be slow if the underlying [object database](crate::Repository::objects) has
63
+ /// an unsuitable [RefreshMode](gix_odb::store::RefreshMode) and `id` is not likely to exist.
64
+ /// Use [`repo.objects.refresh_never()`](gix_odb::store::Handle::refresh_never) to avoid expensive
65
+ /// IO-bound refreshes if an object wasn't found.
66
+ #[ doc( alias = "exists" , alias = "git2" ) ]
67
+ #[ momo]
68
+ pub fn has_object ( & self , id : impl AsRef < gix_hash:: oid > ) -> bool {
69
+ let id = id. as_ref ( ) ;
70
+ if id == ObjectId :: empty_tree ( self . object_hash ( ) ) {
71
+ true
72
+ } else {
73
+ self . objects . contains ( id)
74
+ }
75
+ }
76
+
58
77
/// Obtain information about an object without fully decoding it, or `None` if the object doesn't exist.
59
78
///
60
79
/// Note that despite being cheaper than [`Self::try_find_object()`], there is still some effort traversing delta-chains.
@@ -64,7 +83,7 @@ impl crate::Repository {
64
83
id : impl Into < ObjectId > ,
65
84
) -> Result < Option < gix_odb:: find:: Header > , object:: find:: Error > {
66
85
let id = id. into ( ) ;
67
- if id == gix_hash :: ObjectId :: empty_tree ( self . object_hash ( ) ) {
86
+ if id == ObjectId :: empty_tree ( self . object_hash ( ) ) {
68
87
return Ok ( Some ( gix_odb:: find:: Header :: Loose {
69
88
kind : gix_object:: Kind :: Tree ,
70
89
size : 0 ,
@@ -77,7 +96,7 @@ impl crate::Repository {
77
96
#[ momo]
78
97
pub fn try_find_object ( & self , id : impl Into < ObjectId > ) -> Result < Option < Object < ' _ > > , object:: find:: Error > {
79
98
let id = id. into ( ) ;
80
- if id == gix_hash :: ObjectId :: empty_tree ( self . object_hash ( ) ) {
99
+ if id == ObjectId :: empty_tree ( self . object_hash ( ) ) {
81
100
return Ok ( Some ( Object {
82
101
id,
83
102
kind : gix_object:: Kind :: Tree ,
@@ -236,7 +255,7 @@ impl crate::Repository {
236
255
reference : FullName ,
237
256
message : & str ,
238
257
tree : ObjectId ,
239
- parents : SmallVec < [ gix_hash :: ObjectId ; 1 ] > ,
258
+ parents : SmallVec < [ ObjectId ; 1 ] > ,
240
259
) -> Result < Id < ' _ > , commit:: Error > {
241
260
use gix_ref:: {
242
261
transaction:: { Change , RefEdit } ,
@@ -310,12 +329,12 @@ impl crate::Repository {
310
329
self . commit_as ( committer, author, reference, message, tree, parents)
311
330
}
312
331
313
- /// Return an empty tree object, suitable for [getting changes](crate:: Tree::changes()).
332
+ /// Return an empty tree object, suitable for [getting changes](Tree::changes()).
314
333
///
315
334
/// Note that the returned object is special and doesn't necessarily physically exist in the object database.
316
335
/// This means that this object can be used in an uninitialized, empty repository which would report to have no objects at all.
317
336
pub fn empty_tree ( & self ) -> Tree < ' _ > {
318
- self . find_object ( gix_hash :: ObjectId :: empty_tree ( self . object_hash ( ) ) )
337
+ self . find_object ( ObjectId :: empty_tree ( self . object_hash ( ) ) )
319
338
. expect ( "always present" )
320
339
. into_tree ( )
321
340
}
0 commit comments