File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -148,6 +148,22 @@ impl oid {
148
148
Kind :: Sha1 => & self . bytes == oid:: null_sha1 ( ) . as_bytes ( ) ,
149
149
}
150
150
}
151
+
152
+ /// Returns `true` if this hash is equal to an empty blob.
153
+ #[ inline]
154
+ pub fn is_empty_blob ( & self ) -> bool {
155
+ match self . kind ( ) {
156
+ Kind :: Sha1 => & self . bytes == oid:: empty_blob_sha1 ( ) . as_bytes ( ) ,
157
+ }
158
+ }
159
+
160
+ /// Returns `true` if this hash is equal to an empty tree.
161
+ #[ inline]
162
+ pub fn is_empty_tree ( & self ) -> bool {
163
+ match self . kind ( ) {
164
+ Kind :: Sha1 => & self . bytes == oid:: empty_tree_sha1 ( ) . as_bytes ( ) ,
165
+ }
166
+ }
151
167
}
152
168
153
169
/// Sha1 specific methods
@@ -175,6 +191,18 @@ impl oid {
175
191
pub ( crate ) fn null_sha1 ( ) -> & ' static Self {
176
192
oid:: from_bytes ( [ 0u8 ; SIZE_OF_SHA1_DIGEST ] . as_ref ( ) )
177
193
}
194
+
195
+ /// Returns an oid representing the hash of an empty blob.
196
+ #[ inline]
197
+ pub ( crate ) fn empty_blob_sha1 ( ) -> & ' static Self {
198
+ oid:: from_bytes ( b"\xe6 \x9d \xe2 \x9b \xb2 \xd1 \xd6 \x43 \x4b \x8b \x29 \xae \x77 \x5a \xd8 \xc2 \xe4 \x8c \x53 \x91 " )
199
+ }
200
+
201
+ /// Returns an oid representing the hash of an empty tree.
202
+ #[ inline]
203
+ pub ( crate ) fn empty_tree_sha1 ( ) -> & ' static Self {
204
+ oid:: from_bytes ( b"\x4b \x82 \x5d \xc6 \x42 \xcb \x6e \xb9 \xa0 \x60 \xe5 \x4b \xf8 \xd6 \x92 \x88 \xfb \xee \x49 \x04 " )
205
+ }
178
206
}
179
207
180
208
impl AsRef < oid > for & oid {
Original file line number Diff line number Diff line change @@ -19,3 +19,25 @@ fn is_null() {
19
19
assert ! ( gix_hash:: Kind :: Sha1 . null( ) . is_null( ) ) ;
20
20
assert ! ( gix_hash:: Kind :: Sha1 . null( ) . as_ref( ) . is_null( ) ) ;
21
21
}
22
+
23
+ #[ test]
24
+ fn is_empty_blob ( ) {
25
+ let empty_blob = gix_hash:: ObjectId :: empty_blob ( gix_hash:: Kind :: Sha1 ) ;
26
+ assert ! ( empty_blob. is_empty_blob( ) ) ;
27
+ assert ! ( empty_blob. as_ref( ) . is_empty_blob( ) ) ;
28
+
29
+ let non_empty = gix_hash:: Kind :: Sha1 . null ( ) ;
30
+ assert ! ( !non_empty. is_empty_blob( ) ) ;
31
+ assert ! ( !non_empty. as_ref( ) . is_empty_blob( ) ) ;
32
+ }
33
+
34
+ #[ test]
35
+ fn is_empty_tree ( ) {
36
+ let empty_tree = gix_hash:: ObjectId :: empty_tree ( gix_hash:: Kind :: Sha1 ) ;
37
+ assert ! ( empty_tree. is_empty_tree( ) ) ;
38
+ assert ! ( empty_tree. as_ref( ) . is_empty_tree( ) ) ;
39
+
40
+ let non_empty = gix_hash:: Kind :: Sha1 . null ( ) ;
41
+ assert ! ( !non_empty. is_empty_tree( ) ) ;
42
+ assert ! ( !non_empty. as_ref( ) . is_empty_tree( ) ) ;
43
+ }
You can’t perform that action at this time.
0 commit comments