A Git object id parsed from its hex form, algorithm-tagged by width.
(hex: &str)
| 3193 | |
| 3194 | /// A Git object id parsed from its hex form, algorithm-tagged by width. |
| 3195 | fn git_object_id_from_hex(hex: &str) -> Result<GitObjectId, RepositoryError> { |
| 3196 | let bytes = hex_decode_bytes(hex)?; |
| 3197 | let algorithm = match bytes.len() { |
| 3198 | 20 => atomic_core::operation::GitHashAlgorithm::Sha1, |
| 3199 | 32 => atomic_core::operation::GitHashAlgorithm::Sha256, |
| 3200 | other => { |
| 3201 | return Err(RepositoryError::InvalidRepository { |
| 3202 | reason: format!("unexpected Git OID width {other} in '{hex}'"), |
| 3203 | }) |
| 3204 | } |
| 3205 | }; |
| 3206 | GitObjectId::new(algorithm, bytes).map_err(|error| RepositoryError::InvalidRepository { |
| 3207 | reason: error.to_string(), |
| 3208 | }) |
| 3209 | } |
| 3210 | |
| 3211 | fn hex_decode_bytes(hex: &str) -> Result<Vec<u8>, RepositoryError> { |
| 3212 | if !hex.len().is_multiple_of(2) || !hex.bytes().all(|byte| byte.is_ascii_hexdigit()) { |
no test coverage detected