Expand description
MG.18b: locating the hunk at a cursor, and turning it into a patch.
Pure functions over diff text. No buffer, no store, no Editor —
magit.md §7.2’s substrate-helper pattern, and what lets MG.22
relocate this into magit-hunk-mode rather than rewrite it.
§Why lines arrive through an accessor
hunk_at_with pulls lines one at a time by index rather than
taking a slice. Its production caller reads a buffer snapshot, and
a *magit:diff* buffer can hold tens of thousands of lines —
materialising all of them to stage one hunk would put an
O(document) copy on a keystroke path (paramount #1). Through the
accessor the read is O(distance to the file header + hunk body),
which is the work the answer actually costs. The slice form below
is the same parser, adapted for tests.
§Why the buffer text is the source of truth
magit.md §7.5: hunk boundaries are already derived from buffer
text. ]c / [c walk hunk_lines in magit_core_mode.rs — a raw
scan for @@ / diff --git, identical in every magit buffer,
consulting no cache. Staging reads the same text, so navigation and
staging cannot disagree about where a hunk begins. A parsed-hunk
cache (§7.2 records one as deliberately absent) would give ]c one
boundary set and s another with nothing forcing agreement.
§Why headers are copied, never reconstructed
HunkPatch::header holds the file’s header lines verbatim —
diff --git, index, mode changes, ---, +++. Rebuilding them
from a parsed path would have to re-derive git’s own quoting for
paths with spaces or non-ASCII bytes, and would silently drop
rename and mode metadata. Copying cannot get any of that wrong.
Structs§
- Hunk
Patch - One hunk, plus the file header needed to apply it on its own.
- Source
Pos - A position in the SOURCE FILE, resolved from a cursor sitting on a
diff row. 0-based row plus a 0-based BYTE offset within it, matching
lattice_protocol::position::Position’s own{ line, byte }shape.
Enums§
- Apply
Direction - Which way a patch will be handed to
git apply, and therefore which side of it the target already matches.
Functions§
- hunk_
at_ with - hunk_
ordinal_ at - MG.18d: the 0-based index of the hunk whose header sits at
header_row, among the hunks of the file it belongs to. - path_
at_ cursor - Locate the hunk containing
cursor, reading lines throughread. - source_
position_ at - MG.50: the SOURCE position the cursor is looking at, inside a diff.