(
algorithm: GitHashAlgorithm,
kind: GitObjectKind,
bytes: &[u8],
)
| 1178 | } |
| 1179 | |
| 1180 | pub(super) fn git_object_id( |
| 1181 | algorithm: GitHashAlgorithm, |
| 1182 | kind: GitObjectKind, |
| 1183 | bytes: &[u8], |
| 1184 | ) -> Result<GitObjectId, ProjectTreeError> { |
| 1185 | let kind = match kind { |
| 1186 | GitObjectKind::Blob => "blob", |
| 1187 | GitObjectKind::Tree => "tree", |
| 1188 | }; |
| 1189 | let header = format!("{kind} {}\0", bytes.len()); |
| 1190 | let digest = match algorithm { |
| 1191 | GitHashAlgorithm::Sha1 => { |
| 1192 | let mut hasher = sha1::Sha1::new(); |
| 1193 | hasher.update(header.as_bytes()); |
| 1194 | hasher.update(bytes); |
| 1195 | hasher.finalize().to_vec() |
| 1196 | } |
| 1197 | GitHashAlgorithm::Sha256 => { |
| 1198 | let mut hasher = sha2::Sha256::new(); |
| 1199 | hasher.update(header.as_bytes()); |
| 1200 | hasher.update(bytes); |
| 1201 | hasher.finalize().to_vec() |
| 1202 | } |
| 1203 | }; |
| 1204 | GitObjectId::new(algorithm, digest) |
| 1205 | .map_err(|error| ProjectTreeError::GitObjectId(error.to_string())) |
| 1206 | } |
| 1207 | |
| 1208 | impl Repository { |
| 1209 | /// Build the canonical repository manifest and Git tree from recorded view state. |
no test coverage detected