Load and serialize change data from the repository. Loads the change from the repository and serializes it to bytes suitable for uploading to a remote. # Errors Returns `CliError::ChangeNotFound` if the change doesn't exist, or `CliError::Internal` if serialization fails.
(repo: &Repository, hash: &Hash)
| 316 | /// Returns `CliError::ChangeNotFound` if the change doesn't exist, |
| 317 | /// or `CliError::Internal` if serialization fails. |
| 318 | pub fn load_change_data(repo: &Repository, hash: &Hash) -> CliResult<Bytes> { |
| 319 | // Read the raw V3 change file from disk instead of deserializing and |
| 320 | // re-serializing. This is faster, uses less memory, and — critically — |
| 321 | // preserves the exact bytes that produced the content hash. Re-serializing |
| 322 | // can produce different bytes (field ordering, padding) which would break |
| 323 | // hash verification on the server. |
| 324 | let change_path = repo.change_store().change_path(hash); |
| 325 | if change_path.exists() { |
| 326 | let data = std::fs::read(&change_path).map_err(|e| { |
| 327 | CliError::Internal(anyhow::anyhow!( |
| 328 | "Failed to read change file {:?}: {}", |
| 329 | change_path, |
| 330 | e |
| 331 | )) |
| 332 | })?; |
| 333 | return Ok(Bytes::from(data)); |
| 334 | } |
| 335 | |
| 336 | // Fallback: deserialize + re-serialize (legacy path for changes |
| 337 | // whose on-disk file was cleaned up or doesn't exist). |
| 338 | let change = repo |
| 339 | .load_change(hash) |
| 340 | .map_err(|_| CliError::ChangeNotFound { |
| 341 | hash: hash.to_base32(), |
| 342 | })?; |
| 343 | |
| 344 | let mut buffer = Vec::new(); |
| 345 | change |
| 346 | .serialize(&mut buffer) |
| 347 | .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to serialize change: {}", e)))?; |
| 348 | |
| 349 | Ok(Bytes::from(buffer)) |
| 350 | } |
| 351 | |
| 352 | /// Load the message from a change, returning None if it fails. |
| 353 | pub fn load_change_message(repo: &Repository, hash: &Hash) -> Option<String> { |
no test coverage detected