| 22 | } |
| 23 | |
| 24 | pub(crate) fn encode( |
| 25 | closure: &[Hash], |
| 26 | mut intern: impl FnMut(&[u8; 32], &[u8; 64]) -> PristineResult<()>, |
| 27 | ) -> PristineResult<Vec<u8>> { |
| 28 | let count = |
| 29 | u32::try_from(closure.len()).map_err(|_| invalid("Git closure exceeds u32 length"))?; |
| 30 | if !closure |
| 31 | .windows(2) |
| 32 | .all(|pair| pair[0].as_bytes() < pair[1].as_bytes()) |
| 33 | { |
| 34 | let mut bytes = Vec::with_capacity(4 + closure.len() * 32); |
| 35 | bytes.extend_from_slice(&count.to_le_bytes()); |
| 36 | for hash in closure { |
| 37 | bytes.extend_from_slice(hash.as_bytes()); |
| 38 | } |
| 39 | return Ok(bytes); |
| 40 | } |
| 41 | let tree = encode_tree(closure, &mut intern)?; |
| 42 | // 44 bytes is never a valid legacy count + hashes row (4 mod 32). |
| 43 | let mut root = Vec::with_capacity(44); |
| 44 | root.extend_from_slice(MAGIC); |
| 45 | root.extend_from_slice(&count.to_le_bytes()); |
| 46 | root.extend_from_slice(&tree); |
| 47 | Ok(root) |
| 48 | } |
| 49 | |
| 50 | pub(crate) fn is_shared(bytes: &[u8]) -> bool { |
| 51 | bytes.len() == 44 && bytes.starts_with(MAGIC) |