Skip to main content

compact_diff

Function compact_diff 

Source
pub fn compact_diff(input: &str, keep_line_bodies: bool) -> String
Expand description

Compacts unified-diff text (e.g. git diff output) per the diff_compaction (F-013) contract.

Processes input line by line (via str::lines) and classifies each line:

  • Structural lines — diff --git ..., the git blob-hash index ... line, --- , +++ , or @@ hunk headers (e.g. @@ -12,5 +12,7 @@ optional context) — are always kept verbatim, in order, no matter what.
  • Change-body lines — lines starting with + or - that are not one of the structural --- /+++ forms — are kept verbatim, in order, only when keep_line_bodies is true.
  • Every other line is a context line (typically starting with a leading space) and is never kept.

Lines that are dropped (context lines always; change-body lines too when keep_line_bodies is false) collapse consecutive runs into a single evidence marker line, so the marker is emitted once per run of dropped lines rather than once per line:

  • keep_line_bodies == true: only context lines can ever be dropped in this mode, so the marker reads "[N context lines dropped]".
  • keep_line_bodies == false (the header-only form — valid only when the caller’s TaskScope is ChangeSummary; that policy decision is made by the caller, not here): both context lines and change-body lines are dropped, so the marker reads "[N lines dropped]" to make clear it covers both.

Relative order of everything that survives is preserved. Empty input produces empty output. Output lines are joined with "\n" with no trailing newline.