diff --git a/gix-hash/src/oid.rs b/gix-hash/src/oid.rs index 7b35c310d3c..1cde8755eb2 100644 --- a/gix-hash/src/oid.rs +++ b/gix-hash/src/oid.rs @@ -148,6 +148,22 @@ impl oid { Kind::Sha1 => &self.bytes == oid::null_sha1().as_bytes(), } } + + /// Returns `true` if this hash is equal to an empty blob. + #[inline] + pub fn is_empty_blob(&self) -> bool { + match self.kind() { + Kind::Sha1 => &self.bytes == oid::empty_blob_sha1().as_bytes(), + } + } + + /// Returns `true` if this hash is equal to an empty tree. + #[inline] + pub fn is_empty_tree(&self) -> bool { + match self.kind() { + Kind::Sha1 => &self.bytes == oid::empty_tree_sha1().as_bytes(), + } + } } /// Sha1 specific methods @@ -175,6 +191,18 @@ impl oid { pub(crate) fn null_sha1() -> &'static Self { oid::from_bytes([0u8; SIZE_OF_SHA1_DIGEST].as_ref()) } + + /// Returns an oid representing the hash of an empty blob. + #[inline] + pub(crate) fn empty_blob_sha1() -> &'static Self { + oid::from_bytes(b"\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91") + } + + /// Returns an oid representing the hash of an empty tree. + #[inline] + pub(crate) fn empty_tree_sha1() -> &'static Self { + oid::from_bytes(b"\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04") + } } impl AsRef for &oid { diff --git a/gix-hash/tests/hash/oid.rs b/gix-hash/tests/hash/oid.rs index 7b46e559492..e1e23e7840a 100644 --- a/gix-hash/tests/hash/oid.rs +++ b/gix-hash/tests/hash/oid.rs @@ -19,3 +19,25 @@ fn is_null() { assert!(gix_hash::Kind::Sha1.null().is_null()); assert!(gix_hash::Kind::Sha1.null().as_ref().is_null()); } + +#[test] +fn is_empty_blob() { + let empty_blob = gix_hash::ObjectId::empty_blob(gix_hash::Kind::Sha1); + assert!(empty_blob.is_empty_blob()); + assert!(empty_blob.as_ref().is_empty_blob()); + + let non_empty = gix_hash::Kind::Sha1.null(); + assert!(!non_empty.is_empty_blob()); + assert!(!non_empty.as_ref().is_empty_blob()); +} + +#[test] +fn is_empty_tree() { + let empty_tree = gix_hash::ObjectId::empty_tree(gix_hash::Kind::Sha1); + assert!(empty_tree.is_empty_tree()); + assert!(empty_tree.as_ref().is_empty_tree()); + + let non_empty = gix_hash::Kind::Sha1.null(); + assert!(!non_empty.is_empty_tree()); + assert!(!non_empty.as_ref().is_empty_tree()); +}