Skip to content

Commit 0f20e0a

Browse files
CopilotByron
authored andcommitted
feat: Add oid::is_empty_tree() and oid::is_empty_blob()`.
1 parent 42f8db5 commit 0f20e0a

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

gix-hash/src/oid.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,22 @@ impl oid {
148148
Kind::Sha1 => &self.bytes == oid::null_sha1().as_bytes(),
149149
}
150150
}
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+
}
151167
}
152168

153169
/// Sha1 specific methods
@@ -175,6 +191,18 @@ impl oid {
175191
pub(crate) fn null_sha1() -> &'static Self {
176192
oid::from_bytes([0u8; SIZE_OF_SHA1_DIGEST].as_ref())
177193
}
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+
}
178206
}
179207

180208
impl AsRef<oid> for &oid {

gix-hash/tests/hash/oid.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,44 @@ fn is_null() {
1919
assert!(gix_hash::Kind::Sha1.null().is_null());
2020
assert!(gix_hash::Kind::Sha1.null().as_ref().is_null());
2121
}
22+
23+
#[test]
24+
fn is_empty_blob() {
25+
// Test with ObjectId::empty_blob
26+
let empty_blob = gix_hash::ObjectId::empty_blob(gix_hash::Kind::Sha1);
27+
assert!(empty_blob.is_empty_blob());
28+
assert!(empty_blob.as_ref().is_empty_blob());
29+
30+
// Test that non-empty blob hash returns false
31+
let non_empty = gix_hash::Kind::Sha1.null();
32+
assert!(!non_empty.is_empty_blob());
33+
assert!(!non_empty.as_ref().is_empty_blob());
34+
}
35+
36+
#[test]
37+
fn is_empty_tree() {
38+
// Test with ObjectId::empty_tree
39+
let empty_tree = gix_hash::ObjectId::empty_tree(gix_hash::Kind::Sha1);
40+
assert!(empty_tree.is_empty_tree());
41+
assert!(empty_tree.as_ref().is_empty_tree());
42+
43+
// Test that non-empty tree hash returns false
44+
let non_empty = gix_hash::Kind::Sha1.null();
45+
assert!(!non_empty.is_empty_tree());
46+
assert!(!non_empty.as_ref().is_empty_tree());
47+
}
48+
49+
#[test]
50+
fn oid_methods_are_consistent_with_objectid() {
51+
// Verify that the oid methods return the same results as ObjectId methods
52+
let empty_blob = gix_hash::ObjectId::empty_blob(gix_hash::Kind::Sha1);
53+
let empty_tree = gix_hash::ObjectId::empty_tree(gix_hash::Kind::Sha1);
54+
55+
// Check that ObjectId and oid versions give same results
56+
assert_eq!(empty_blob.is_empty_blob(), empty_blob.as_ref().is_empty_blob());
57+
assert_eq!(empty_tree.is_empty_tree(), empty_tree.as_ref().is_empty_tree());
58+
59+
// Check cross-validation (empty blob is not empty tree and vice versa)
60+
assert!(!empty_blob.as_ref().is_empty_tree());
61+
assert!(!empty_tree.as_ref().is_empty_blob());
62+
}

0 commit comments

Comments
 (0)