Skip to main content

Module diff

Module diff 

Source
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§

DiffEntry
One leaf-level change. path is /-joined relative to the root of the compared trees; subtree directories are NOT emitted as their own entries — only the leaves they contain.
DiffResult
Sorted (by path) sequence of DiffEntry.
HunkLine
One line of a hunk: its origin plus the raw bytes (no +/-/space prefix) and whether the source line had a trailing newline.
PatchHunk
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. Use apply_hunks_subset to materialize a chosen subset.
StatusEntry
One entry in the mkit status output. Combines a DiffEntry with index-awareness so the caller can render three-way status output.

Enums§

DiffError
Error type for status_diff.
DiffKind
What kind of change a DiffEntry represents.
HunkLineKind
Origin of a line within a PatchHunk — context (unchanged), added on the new side, or removed from the old side.
StatusStaging
Staging state of a StatusEntry relative to the index.
WhitespaceMode
Whitespace-comparison mode for hunk generation — the primitive behind git’s diff -w/--ignore-all-space and -b/--ignore-space-change. Only line comparison for the edit script changes; a rendered hunk always shows each line’s real, unmodified bytes. Mirrors ops::blame’s -w (ignore-all-space) semantics — see that module’s strip_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 -U3 default. Exposed so a caller threading an explicit -U<n> (e.g. the CLI’s diff -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_bytes applying only the hunks whose index (into the slice returned by enumerate_hunks) is in selected. Selected hunks have their additions applied and removals dropped; unselected hunks keep the base content unchanged. selected order 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 entries into single DiffKind::Renamed entries, in place.
diff_line_counts
Added / deleted line counts between two blobs, from the same Myers edit script the unified patch uses. None when either side is binary — matching Git’s heuristic of a NUL byte within the first 8000 bytes (independent of UTF-8 validity), so diff --stat renders Bin … for exactly the blobs Git would.
diff_trees
Compare two trees and return their DiffResult. None for 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 as unified_hunks. Returns None when either side is binary (git’s NUL heuristic); an empty vector means the blobs are textually identical. Unlike unified_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 with apply_hunks_subset.
is_binary
Git’s binary heuristic (buffer_is_binary): a NUL byte within the first BINARY_SNIFF_LEN bytes marks the blob binary, regardless of UTF-8 validity. bytes may be a full blob or just its leading BINARY_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 StatusEntry grouped by staging state.
status_diff_observed
status_diff that additionally returns the worktree walk’s worktree::StatObservations — entries whose cache was absent or racy-smudged but whose re-hash matched the staged hash. Callers (the status CLI) 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). Returns None when either side is binary by git’s heuristic — a NUL byte in the first 8000 bytes — so a caller emits a Binary files … line instead. An empty result means the blobs are textually identical.
unified_hunks_opts
Like unified_hunks but with an explicit context-line count and whitespace-comparison mode — the primitives behind git’s diff -U<n> and -w/-b. context controls how many unchanged lines surround each hunk (DEFAULT_CONTEXT_LINES is git’s own default of 3); mode controls which lines the edit script treats as equal (a rendered hunk always shows each line’s real, unmodified bytes — only comparison changes).