Expand description
Tree-level structural diff.
Compares two trees identified by their object hash and returns the
minimal list of leaf-level changes (added / removed / modified
/ mode_changed). The walk is lockstep over the two sorted entry
arrays, recurses into matching subtrees only when their hashes
differ, and treats added/removed subtrees as bulk operations on every
contained leaf.
Also contains status_diff — the working-tree vs HEAD diff that
powers mkit status.
Structs§
- Diff
Entry - One leaf-level change.
pathis/-joined relative to the root of the compared trees; subtree directories are NOT emitted as their own entries — only the leaves they contain. - Diff
Result - Sorted (by path) sequence of
DiffEntry. - Hunk
Line - One line of a hunk: its origin plus the raw bytes (no
+/-/space prefix) and whether the source line had a trailing newline. - Patch
Hunk - A single hunk for interactive staging (
add -p): the 1-based old/new line ranges (as in the@@header) plus the ordered context/added/ removed lines. Useapply_hunks_subsetto materialize a chosen subset. - Status
Entry - One entry in the
mkit statusoutput. Combines aDiffEntrywith index-awareness so the caller can render three-way status output.
Enums§
- Diff
Error - Error type for
status_diff. - Diff
Kind - What kind of change a
DiffEntryrepresents. - Hunk
Line Kind - Origin of a line within a
PatchHunk— context (unchanged), added on the new side, or removed from the old side. - Status
Staging - Staging state of a
StatusEntryrelative to the index. - Whitespace
Mode - Whitespace-comparison mode for hunk generation — the primitive behind
git’s
diff -w/--ignore-all-spaceand-b/--ignore-space-change. Only line comparison for the edit script changes; a rendered hunk always shows each line’s real, unmodified bytes. Mirrorsops::blame’s-w(ignore-all-space) semantics — see that module’sstrip_ws— plus the-b(ignore-space-change) mode blame does not need.
Constants§
- BINARY_
SNIFF_ LEN - Sniff window for git’s binary heuristic: content is classified from at
most this many leading bytes. Exposed so a caller that only needs the
classification — not a diff — can read just this much of a blob (e.g.
crate::worktree::LoadedBlob::prefix) instead of paying for full content, which matters for a chunked blob (mkit#606). - DEFAULT_
CONTEXT_ LINES - Default number of unchanged context lines around each hunk — git’s own
-U3default. Exposed so a caller threading an explicit-U<n>(e.g. the CLI’sdiff -U<n>) can fall back to the same default mkit already uses when the flag is omitted.
Functions§
- apply_
hunks_ subset - Rebuild a blob from
base_bytesapplying only the hunks whose index (into the slice returned byenumerate_hunks) is inselected. Selected hunks have their additions applied and removals dropped; unselected hunks keep the base content unchanged.selectedorder does not matter — hunks are always applied in file order — and out-of-range indices are ignored. - detect_
exact_ renames - Collapse exact (identical-content) delete+add pairs in
entriesinto singleDiffKind::Renamedentries, in place. - diff_
line_ counts - Added / deleted line counts between two blobs, from the same Myers edit
script the unified patch uses.
Nonewhen either side is binary — matching Git’s heuristic of a NUL byte within the first 8000 bytes (independent of UTF-8 validity), sodiff --statrendersBin …for exactly the blobs Git would. - diff_
trees - Compare two trees and return their
DiffResult.Nonefor either hash represents the empty tree (use cases: comparing against the initial commit, against a rolled-back state). - enumerate_
hunks - Enumerate the hunks between two blobs as structured
PatchHunks, using the same Myers diff + git-style compaction asunified_hunks. ReturnsNonewhen either side is binary (git’s NUL heuristic); an empty vector means the blobs are textually identical. Unlikeunified_hunks, which renders bytes, this exposes each hunk’s lines so a caller can let the user pick a subset to stage and rebuild the partial blob withapply_hunks_subset. - is_
binary - Git’s binary heuristic (
buffer_is_binary): a NUL byte within the firstBINARY_SNIFF_LENbytes marks the blob binary, regardless of UTF-8 validity.bytesmay be a full blob or just its leadingBINARY_SNIFF_LEN-byte prefix — since this never looks past that many bytes, the result is identical either way. - merge_
blob_ 3way - Line-level 3-way merge of three blobs (diff3-style, conservative).
- status_
diff - Compare HEAD ↔ index and index ↔ worktree, returning a list of
StatusEntrygrouped by staging state. - status_
diff_ observed status_diffthat additionally returns the worktree walk’sworktree::StatObservations — entries whose cache was absent or racy-smudged but whose re-hash matched the staged hash. Callers (thestatusCLI) use them to heal the stat cache from hash-time stats; pairing a later stat with the earlier hash is unsound.- text_
patch - Render a unified-diff patch between two byte blobs.
- unified_
hunks - The hunk body of a unified diff between two blobs: the
@@ … @@headers and their+/-/context lines, with no---/+++file headers, as raw bytes (git diffs are byte-oriented and do not require UTF-8). ReturnsNonewhen either side is binary by git’s heuristic — a NUL byte in the first 8000 bytes — so a caller emits aBinary files …line instead. An empty result means the blobs are textually identical. - unified_
hunks_ opts - Like
unified_hunksbut with an explicit context-line count and whitespace-comparison mode — the primitives behind git’sdiff -U<n>and-w/-b.contextcontrols how many unchanged lines surround each hunk (DEFAULT_CONTEXT_LINESis git’s own default of 3);modecontrols which lines the edit script treats as equal (a rendered hunk always shows each line’s real, unmodified bytes — only comparison changes).