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

Function parse_gitlink_bytes

atomic-repository/src/repository/project_tree.rs:1569–1589  ·  view source on GitHub ↗
(
    algorithm: GitHashAlgorithm,
    bytes: &[u8],
)

Source from the content-addressed store, hash-verified

1567}
1568
1569fn parse_gitlink_bytes(
1570 algorithm: GitHashAlgorithm,
1571 bytes: &[u8],
1572) -> Result<GitObjectId, ProjectTreeError> {
1573 let expected = match algorithm {
1574 GitHashAlgorithm::Sha1 => 40,
1575 GitHashAlgorithm::Sha256 => 64,
1576 };
1577 if bytes.len() != expected || !bytes.iter().all(u8::is_ascii_hexdigit) {
1578 return Err(ProjectTreeError::InvalidGitlink(format!(
1579 "expected {expected} hexadecimal bytes"
1580 )));
1581 }
1582 let mut decoded = Vec::with_capacity(expected / 2);
1583 for pair in bytes.as_chunks::<2>().0 {
1584 let text = std::str::from_utf8(pair).expect("ASCII hex checked");
1585 decoded.push(u8::from_str_radix(text, 16).expect("hex checked"));
1586 }
1587 GitObjectId::new(algorithm, decoded)
1588 .map_err(|error| ProjectTreeError::GitObjectId(error.to_string()))
1589}
1590
1591fn repo_error(error: impl fmt::Display) -> ProjectTreeError {
1592 ProjectTreeError::Repository(error.to_string())

Calls 5

GitObjectIdClass · 0.85
lenMethod · 0.45
allMethod · 0.45
iterMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected