Build one file diff between two layer byte-sets.
(
path: &str,
old: Option<Vec<u8>>,
new: Option<Vec<u8>>,
new_present: bool,
old_present: bool,
algorithm: Algorithm,
context: usize,
)
| 486 | |
| 487 | /// Build one file diff between two layer byte-sets. |
| 488 | fn build_layer_file_diff( |
| 489 | path: &str, |
| 490 | old: Option<Vec<u8>>, |
| 491 | new: Option<Vec<u8>>, |
| 492 | new_present: bool, |
| 493 | old_present: bool, |
| 494 | algorithm: Algorithm, |
| 495 | context: usize, |
| 496 | ) -> CliResult<FileDiff> { |
| 497 | let (diff, old_bytes, new_bytes) = match (old_present.then_some(()), new_present.then_some(())) |
| 498 | { |
| 499 | (Some(()), Some(())) => { |
| 500 | let diff = FileDiff::modified(path); |
| 501 | (diff, old.unwrap_or_default(), new.unwrap_or_default()) |
| 502 | } |
| 503 | (None, Some(())) => { |
| 504 | let diff = FileDiff::added(path); |
| 505 | (diff, Vec::new(), new.unwrap_or_default()) |
| 506 | } |
| 507 | (Some(()), None) => { |
| 508 | let diff = FileDiff::deleted(path); |
| 509 | (diff, old.unwrap_or_default(), Vec::new()) |
| 510 | } |
| 511 | (None, None) => { |
| 512 | return Err(CliError::InvalidArgument { |
| 513 | message: format!("diff for '{path}' has no content on either side"), |
| 514 | }) |
| 515 | } |
| 516 | }; |
| 517 | |
| 518 | let diff_result = diff_text(&old_bytes, &new_bytes, algorithm); |
| 519 | let mut diff = diff; |
| 520 | if !diff_result.is_unchanged() { |
| 521 | let old_lines: Vec<_> = old_bytes.split(|&b| b == b'\n').collect(); |
| 522 | let new_lines: Vec<_> = new_bytes.split(|&b| b == b'\n').collect(); |
| 523 | let hunks = build_hunks_from_diff(&diff_result, &old_lines, &new_lines, context); |
| 524 | for hunk in hunks { |
| 525 | diff.add_hunk(hunk); |
| 526 | } |
| 527 | } |
| 528 | diff.compute_stats(); |
| 529 | Ok(diff) |
| 530 | } |
| 531 | |
| 532 | impl Default for Diff { |
| 533 | fn default() -> Self { |
no test coverage detected