Build the real per-file unified diff for a recorded change WITHOUT printing. This is the computation `atomic diff -c ` runs, factored out so other surfaces (e.g. the triage report) can embed actual code hunks instead of re-implementing diffing. It returns every changed file's [`FileDiff`] plus the aggregate [`DiffStats`], using the semantic layer (FileOps) with true hunk offsets and before-
(
repo: &Repository,
change: &Change,
change_hash: &Hash,
config: &DiffOutputConfig,
)
| 1411 | /// `atomic diff` path applies `filter_file_diffs` afterward, which yields the |
| 1412 | /// same result as the previous in-loop filtering. |
| 1413 | pub fn change_file_diffs( |
| 1414 | repo: &Repository, |
| 1415 | change: &Change, |
| 1416 | change_hash: &Hash, |
| 1417 | config: &DiffOutputConfig, |
| 1418 | ) -> Result<(Vec<FileDiff>, DiffStats), crate::error::RepositoryError> { |
| 1419 | let projected: Vec<_> = change |
| 1420 | .file_ops() |
| 1421 | .iter() |
| 1422 | .filter(|ops| matches!(ops.trunk_op(), Some(TrunkOp::ProjectedSpanEdit { .. }))) |
| 1423 | .collect(); |
| 1424 | if !projected.is_empty() { |
| 1425 | let mut ordinary = change.clone(); |
| 1426 | ordinary |
| 1427 | .hashed |
| 1428 | .file_ops |
| 1429 | .retain(|ops| !matches!(ops.trunk_op(), Some(TrunkOp::ProjectedSpanEdit { .. }))); |
| 1430 | let (mut files, mut stats) = change_file_diffs_with(&ordinary, config, |path| { |
| 1431 | repo.get_file_content_before_change(path, change_hash) |
| 1432 | .ok() |
| 1433 | .flatten() |
| 1434 | })?; |
| 1435 | for ops in projected { |
| 1436 | let path = ops.path(); |
| 1437 | let before = repo |
| 1438 | .get_file_content_before_change(path, change_hash)? |
| 1439 | .ok_or_else(|| crate::error::RepositoryError::InvalidOperation { |
| 1440 | message: format!("projected span file '{path}' is absent before the change"), |
| 1441 | })?; |
| 1442 | let after = repo |
| 1443 | .get_file_content_after_change(path, change_hash)? |
| 1444 | .ok_or_else(|| crate::error::RepositoryError::InvalidOperation { |
| 1445 | message: format!("projected span file '{path}' is absent after the change"), |
| 1446 | })?; |
| 1447 | let mut file_diff = FileDiff::modified(path); |
| 1448 | if before.contains(&0) |
| 1449 | || after.contains(&0) |
| 1450 | || std::str::from_utf8(&before).is_err() |
| 1451 | || std::str::from_utf8(&after).is_err() |
| 1452 | { |
| 1453 | file_diff.is_binary = true; |
| 1454 | } else { |
| 1455 | let result = diff_text(&before, &after, Algorithm::Myers); |
| 1456 | if !result.is_unchanged() { |
| 1457 | let old_lines: Vec<_> = before.split(|&byte| byte == b'\n').collect(); |
| 1458 | let new_lines: Vec<_> = after.split(|&byte| byte == b'\n').collect(); |
| 1459 | for hunk in |
| 1460 | build_hunks_from_diff(&result, &old_lines, &new_lines, config.context_lines) |
| 1461 | { |
| 1462 | file_diff.add_hunk(hunk); |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | file_diff.compute_stats(); |
| 1467 | stats.add_file(file_diff.stats.clone()); |
| 1468 | files.push(file_diff); |
| 1469 | } |
| 1470 | return Ok((files, stats)); |