Shared unified-diff helpers.
Every diff passing through this module is "\n"-joined from lines that came
from splitlines() or split("\n"), so no element can contain a line boundary.
That is what makes split_diff_lines the exact inverse and splitlines() wrong
here — see its docstring for what breaks. Check any helper added to this module,
and any new producer of a diff it reads, against that invariant.
Matches a hunk header.
Captures, in order: old start line, old line count, new start line, new line count. Either count is absent for a single-line range, where it defaults to 1.
Stand-in line marking where a diff body was clipped for display.
Written by compute_unified_diff and rendered as a truncated row. It is also
the signal that any counts recomputed from the body would be short — see
DiffMessage._recount, which returns None rather than a known-low number.
Match it with is_truncation_marker rather than by hand: the renderer and the
recount must agree, or a body that renders "diff truncated" is also counted as
if it were complete.
Return whether a diff line is the truncation marker.
One predicate for both readers, which previously disagreed: the renderer stripped, the recount compared exactly.
Exact, deliberately. compute_unified_diff writes the marker bare, while
every real diff line carries a +, -, or space prefix — so an exact match
cannot collide with file content, and a source line whose own text is ...
arrives here as " ..." and stays a context row. Stripping would classify
that line as a clipped body and suppress the change counts for a diff that
is complete.
Split a unified diff back into the lines it was assembled from.
Deliberately not splitlines(). Every diff reaching this function is
"\n"-joined from lines that themselves came from splitlines() or
split("\n"), so no element can contain a line boundary and "\n" is the
exact inverse. splitlines() also breaks on \r, \v, \f, U+2028,
U+2029 and U+0085, which splits a single diff line into fragments. The tail
fragment carries no +/- marker, so it would render as an unmarked note —
on the approval prompt that means changed content shown as neutral metadata.
Check any new producer against that invariant rather than against a list of the current ones.
Locate paired file headers immediately preceding a hunk.
A ---/+++ pair is only a file header when it appears outside a hunk
body — a diff of a file that itself contains such lines would otherwise
have its content mistaken for metadata. That is why this walks the hunks'
declared old/new line budgets instead of just matching on the prefix.
Count added and removed lines in unified-diff lines.