Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions gix-hash/src/oid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<oid> for &oid {
Expand Down
22 changes: 22 additions & 0 deletions gix-hash/tests/hash/oid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Loading