Decode one full-length hex Git object id (40-char SHA-1 or 64-char SHA-256) into its tagged object id (review D3). The hex decoding is strict: the token must be entirely hex digits of exactly one supported length.
(token: &str)
| 604 | /// strict: the token must be entirely hex digits of exactly one supported |
| 605 | /// length. |
| 606 | pub fn parse_git_oid_hex(token: &str) -> Option<GitObjectId> { |
| 607 | let (algorithm, width) = match token.len() { |
| 608 | 40 => (GitHashAlgorithm::Sha1, 20usize), |
| 609 | 64 => (GitHashAlgorithm::Sha256, 32usize), |
| 610 | _ => return None, |
| 611 | }; |
| 612 | if !token.bytes().all(|byte| byte.is_ascii_hexdigit()) { |
| 613 | return None; |
| 614 | } |
| 615 | let mut bytes = Vec::with_capacity(width); |
| 616 | for index in 0..width { |
| 617 | let byte = u8::from_str_radix(&token[index * 2..index * 2 + 2], 16).ok()?; |
| 618 | bytes.push(byte); |
| 619 | } |
| 620 | GitObjectId::new(algorithm, bytes).ok() |
| 621 | } |
| 622 | |
| 623 | /// Parse a post-rewrite event's `<old> <new>` full-OID pair (review D3). |
| 624 | /// |
no test coverage detected