MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / build_layer_file_diff

Function build_layer_file_diff

atomic-cli/src/commands/diff/command.rs:488–530  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

486
487/// Build one file diff between two layer byte-sets.
488fn 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
532impl Default for Diff {
533 fn default() -> Self {

Callers 1

run_git_diffMethod · 0.85

Calls 9

modifiedFunction · 0.85
addedFunction · 0.85
deletedFunction · 0.85
diff_textFunction · 0.85
build_hunks_from_diffFunction · 0.70
is_unchangedMethod · 0.45
collectMethod · 0.45
add_hunkMethod · 0.45
compute_statsMethod · 0.45

Tested by

no test coverage detected