MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / commit_object_digest

Function commit_object_digest

atomic-repository/src/git_binding/codec.rs:580–611  ·  view source on GitHub ↗

Git object digest of a raw commit object (RFC 8.2): the format-tagged commit digest `sha1("commit \0" + bytes)` or its SHA-256 equivalent.

(
    format: GitObjectFormat,
    raw_commit: &[u8],
)

Source from the content-addressed store, hash-verified

578/// Git object digest of a raw commit object (RFC 8.2): the format-tagged
579/// commit digest `sha1("commit <len>\0" + bytes)` or its SHA-256 equivalent.
580pub fn commit_object_digest(
581 format: GitObjectFormat,
582 raw_commit: &[u8],
583) -> Result<GitOid, BindingEncodeError> {
584 let header = format!("commit {}\0", raw_commit.len()).into_bytes();
585 match format {
586 GitObjectFormat::Sha1 => {
587 let mut hasher = sha1::Sha1::new();
588 use sha1::Digest as _;
589 hasher.update(&header);
590 hasher.update(raw_commit);
591 let digest: [u8; 20] = hasher
592 .finalize()
593 .as_slice()
594 .try_into()
595 .map_err(|_| BindingEncodeError::Oid("sha1 digest width".to_string()))?;
596 Ok(GitOid::Sha1(digest))
597 }
598 GitObjectFormat::Sha256 => {
599 let mut hasher = sha2::Sha256::new();
600 use sha2::Digest as _;
601 hasher.update(&header);
602 hasher.update(raw_commit);
603 let digest: [u8; 32] = hasher
604 .finalize()
605 .as_slice()
606 .try_into()
607 .map_err(|_| BindingEncodeError::Oid("sha256 digest width".to_string()))?;
608 Ok(GitOid::Sha256(digest))
609 }
610 }
611}
612
613/// Encoding failures: a binding that can never be signed or published.
614#[derive(Debug, thiserror::Error)]

Calls 3

updateMethod · 0.45
as_sliceMethod · 0.45
finalizeMethod · 0.45