Expand description
Git diff and merge engine.
Re-exports§
pub use format::*;
Modules§
- format
- Diff rendering format helpers (word-diff, colors, funcname adapters).
- range
- Patch-series assignment for
range-diff. - render
- Unified-diff / patch RENDERER: turn a computed file diff (the old/new
blob contents) into the textual unified-diff hunk body git’s
diff.cemit path produces (emit_diff_symbol/fn_out_consume). - ws
- Whitespace-rule engine — a port of git’s
ws.c/ws.h.
Structs§
- Apply
File Options - Options for
apply_file_patch_with_options, mirroring thegit applyflags that change fragment placement. - BString
- A byte string for git paths and similar on-disk identifiers.
- Binary
Hunk - One binary hunk: the encoding method and the still-deflated data, plus the declared original (inflated) length.
- Binary
Patch - A
GIT binary patchpayload: a mandatory forward hunk (preimage → postimage) and an optional reverse hunk (postimage → preimage), mirroring git’sparse_binary. - Conflict
- Diff
Line - A single line of a blob, slicing into the original buffer.
- Diff
Name Status Options - File
Patch - A patch targeting a single file. Produced by
parse_unified_patch. - Hunk
- A single
@@ -old_start,old_len +new_start,new_len @@hunk. - Index
Gitlink Entry - Index
Worktree Diff - Index
Worktree Validated Entry - Index
Worktree Validation Entry - Merge
Blob Options - Labels and style controlling
merge_blobsconflict markers. - Merge
Blob Result - The outcome of a 3-way blob merge.
- Merge
Stages - Per-stage higher-order index entries for a conflicted path.
- Merge
Trees Options - Options controlling a
merge_treesrun. - Merge
Trees Result - The outcome of a 3-way tree merge: the merged top-level tree plus per-path detail and a clean/conflicted flag.
- Merged
Path - One resolved/conflicted path in the merged tree.
- Name
Status Entry - Name
Status With Rename Diagnostics - Patch
Path Options - Path-resolution options for
parse_unified_patch_with_options, mirroringgit apply’s-p<n>strip (p_value) and--directory=<root>prefix. - Reject
Apply - The outcome of a hunk-by-hunk apply (
git apply --reject). - Rename
Limit Diagnostics - WsApply
Options - Options for
apply_file_patch_ws. - WsIgnore
- Whitespace-ignore flags for line comparison, mirroring git’s
XDF_WHITESPACE_FLAGS(-w,-b,--ignore-space-at-eol,--ignore-cr-at-eol). Only one of the whitespace flavours is honoured per git’s precedence (-w⊃-b⊃--ignore-space-at-eol⊃--ignore-cr-at-eol); when several are set, the strongest wins, matching the cascade inxdl_recmatch.
Enums§
- Apply
Outcome - Outcome of applying a
FilePatchto a base buffer. - Binary
Method - How a binary hunk encodes the postimage.
- Conflict
Style - Which conflict-marker style
merge_blobsemits. - Diff
Algorithm - DiffOp
- A run-length entry in a Myers edit script.
- Directory
Renames - How directory-rename detection behaves, mirroring git’s
merge.directoryRenamesconfiguration. - File
Change - Hunk
Line - A single line inside a hunk. The stored bytes never include the trailing
line terminator; whether the line is terminated by
\nis tracked separately on theHunk(seeHunk::old_no_newline/Hunk::new_no_newline) so the no-final-newline case can be reproduced byte-for-byte. - Merge
Conflict Kind - The kind of conflict recorded for a path, used to render the stable conflict-type token and human message.
- Merge
Favor - Whether to favour one side wholesale for textual conflicts (
-Xours/-Xtheirs), or to leave conflict markers in place. - Merge
Info Message - Non-conflict merge information that porcelain commands may print.
- Name
Status - WsApply
Outcome - Outcome of
apply_file_patch_ws.
Constants§
- DEFAULT_
RENAME_ THRESHOLD - git’s default minimum similarity (as a percentage) for a pair of files to be
reported as a rename or copy. Matches
git’s built-in-M/-Cthreshold of 50% (DEFAULT_RENAME_SCOREisMAX_SCORE / 2). - S_IFMT
- The bit mask isolating the file-type bits of a git mode (
S_IFMT). Regular files are0o100000, symlinks0o120000, gitlinks0o160000, trees0o040000.
Functions§
- apply_
file_ patch - Apply a single-file patch to
base, returning the patched bytes. - apply_
file_ patch_ rejecting - Apply a single-file patch hunk-by-hunk, collecting the hunks that do not
apply rather than rejecting the whole patch —
git apply --reject. - apply_
file_ patch_ with_ options - Apply a single-file patch with explicit fragment-placement options.
- apply_
file_ patch_ ws - Whitespace-aware single-file apply — git’s
apply_one_fragmentmatching path. - augment_
with_ stat_ dirty_ entries - Append a
Modifiedentry for every stage-0 index path whose worktree file is present and whose cached stat is dirty (ce_match_stat“changed”) but whichcontent_changesdid not already report. The result is re-sorted by path so the merged set keeps git’s diff-queue ordering. New-side oids on the added entries are leftNone(rendered as zeros in raw output), matching git, which reports the worktree blob oid only for entries it has hashed. - basename_
min_ score - The stricter score a basename match must reach: git’s
min_basename_scorewith the defaultGIT_BASENAME_FACTORof 0.5, i.e. halfway between the rename threshold and 100%. (For the default 50% threshold this is 75%.) - basename_
rename_ matches - git’s
find_basename_matches: among the still-unmatched rename sources and destinations, pair those whose basename is UNIQUE on both sides and whose similarity meetsbasename_min_score. Returns the(src_local, dst_local, score)pairings to apply before the full O(n·m) similarity matrix, so a same-basename rename wins over a globally-more-similar different-basename candidate (diffcore-rename.c). - blob_
similarity - Compute the content similarity of two blobs as an integer percentage in
0..=100, using git’s span-hash counting metric (see the module comment above for the exact definition). - corrupted_
cache_ tree_ error - count_
changes - Sum, over every hash present in both maps, the smaller of the two byte
counts. This is git’s
src_copied: the number of bytes that appear on both sides (counting multiplicity via the per-hash byte totals). gitdiffcore_count_changes(): span-hash byte accounting between two blobs. Returns(src_copied, literal_added)— the bytes ofsrcthat survive intodst, and the bytes ofdstnot accounted for bysrc.--dirstat’s default “changes” damage is(src.len() - src_copied) + literal_added. - diff_
lines_ with_ algorithm - Dispatch to the line-diff implementation selected by
algorithm. - diff_
name_ status_ empty_ tree_ with_ options - diff_
name_ status_ head_ index - diff_
name_ status_ head_ index_ with_ options - diff_
name_ status_ head_ index_ with_ options_ and_ diagnostics - diff_
name_ status_ head_ worktree - diff_
name_ status_ head_ worktree_ with_ options - diff_
name_ status_ index_ worktree - diff_
name_ status_ index_ worktree_ for_ diff_ files_ with_ options - Index-vs-worktree name-status for
git diff-files(plumbing), which selects changed paths by the cached stat rather than by content. - diff_
name_ status_ index_ worktree_ with_ options - diff_
name_ status_ index_ worktree_ with_ options_ and_ gitlinks - diff_
name_ status_ index_ worktree_ with_ options_ and_ gitlinks_ validated - diff_
name_ status_ tree_ index_ with_ options - Name-status diff of an arbitrary tree against the index, the engine behind
git diff-index --cached <tree-ish>. Exact rename/copy detection followsoptions; all blob content comes from the object database. - diff_
name_ status_ tree_ index_ with_ options_ and_ diagnostics - diff_
name_ status_ tree_ worktree_ with_ options - Name-status diff of an arbitrary tree against the working tree, the engine
behind plain
git diff-index <tree-ish>(no--cached). New-side oids for paths whose worktree contents differ from the index are cleared (rendered as zeros), matching git, which only reports the worktree blob oid when it is known-clean against the index. - diff_
name_ status_ trees_ with_ options - diff_
name_ status_ trees_ with_ options_ and_ diagnostics - flatten_
tree - Read a tree object (by oid) into a flattened path -> (mode, oid) map, descending into subtrees. The canonical empty tree yields an empty map.
- git_
patch_ delta - Apply a git delta (
delta.cpatch_delta) to reconstruct the postimage frombase. The delta begins with the base size and result size as varints, followed by copy (0x80bit set: offset/size from base) and insert (literal bytes) opcodes. ReturnsNoneon any malformed/inconsistent delta. - gitlink_
broken_ gitdir - When
sub_rootholds a broken gitlink — a.gitfile whosegitdir:pointer names a directory that no longer exists (e.g. the submodule’s git directory was moved out of.git/modules/) — return that unresolved gitdir path. git’s status / diff-index fail fatally (“not a git repository: …”) here. ReturnsNonefor a valid gitlink (a.gitdirectory, or a.gitfile with a live gitdir) and for an unpopulated gitlink (no.gitentry at all), both of which git treats as non-fatal (the latter as unchanged). - gitlink_
git_ dir - Resolve the git directory of an embedded repository whose working tree is
at
sub_root. A.gitdirectory is returned as-is; a.gitfile is followed through itsgitdir: <path>pointer (a relative pointer resolves againstsub_root). ReturnsNonewhen there is no.gitentry or the pointer does not name an existing directory. - gitlink_
head_ oid - Resolve the commit checked out in the embedded repository at
sub_root(the value a gitlink entry for that path records): its git directory’s HEAD, followed through symbolic refs.Nonewhensub_rootis not a repository or its HEAD does not resolve to a commit (e.g. an unborn branch) — upstream’sresolve_gitlink_ref() < 0case. - histogram_
diff_ lines - Compute a line-level edit script transforming
oldintonewusing the histogram diff algorithm (as ingit diff --histogram, derived from JGit). - is_
mergeable_ file_ mode - True for a plain file blob (regular or executable) — i.e. a mode whose content can be textually 3-way merged. Symlinks and gitlinks are excluded.
- is_
type_ change - Whether a pair of (non-zero) modes constitutes a git “typechange”: the file
type bits (
S_IFMT) differ. Mirrorsdiffcore.h’sDIFF_PAIR_TYPE_CHANGED((S_IFMT & one->mode) != (S_IFMT & two->mode)). An exec-bit-only change (0o100644↔0o100755) is NOT a typechange — sameS_IFMT. - merge_
blobs - Perform a 3-way merge of three blobs using the diff3 algorithm.
- merge_
entry_ maps merge_treesoperating on already-flattened entry maps. The merge porcelains often hold the flattened maps already (e.g. cherry-pick buildstheirsfrom a picked commit’s tree), so this avoids re-reading them.- merge_
trees - 3-way merge of three trees into a single merged tree.
- modify_
or_ type_ change - Classify a both-sides-present change whose entries already differ: a
NameStatus::TypeChangedwhen the modes’S_IFMTbits differ, otherwise a plainNameStatus::Modified. git setsDIFF_STATUS_TYPE_CHANGEDbefore any rename/modify resolution (diff.c~6650). - myers_
diff_ lines - Compute a minimal line-level edit script transforming
oldintonewusing Myers’ O(ND) difference algorithm. - myers_
diff_ lines_ ws - Compute a line-level edit script transforming
oldintonew, comparing lines under the whitespace-ignore flagsignorewhile the returned ops still index the original lines position-for-position. - parse_
unified_ patch - Parse a unified/git diff into one
FilePatchper file it touches. - parse_
unified_ patch_ with_ options - Parse a unified/git diff, applying
-p<n>strip and--directoryprefix to every resolved pathname exactly asgit applydoes. - parse_
unified_ patch_ with_ recount - Parse a unified/git diff, optionally ignoring hunk header line counts and
recounting them from the hunk body. This mirrors
git apply --recount. - path_
basename - The basename of a slash-separated path: the portion after the last
/(git’sget_basename). - patience_
diff_ lines - Compute a line-level edit script transforming
oldintonewusing the patience diff algorithm (Bram Cohen’s algorithm, as ingit diff --patience). - patience_
diff_ lines_ anchored - As
patience_diff_lines, but pins lines whose content has any ofanchorsas a byte prefix into the common subsequence (git’s--anchored=<text>). - render_
reject_ hunk - Reconstruct the unified-diff text of one hunk for a
.rejfile. Mirrors the raw fragment text git copies into<file>.rej: the@@ -os[,oc] +ns[,nc] @@header (the,1count is omitted, matching git) followed by each line with its/+/-prefix, plus the\ No newline at end of filenote where the old/new side’s final line is unterminated. - reverse_
file_ patch - Reverse a file patch (
git apply -R): swap the old/new names, modes, hunk ranges, and no-newline flags, exchange add↔delete status, and flip everyInsert/Deleteline. Applying the result undoes the original patch. - split_
lines - Split a blob into lines, preserving the exact bytes of each line.
- symlink_
target_ bytes - Read a symbolic link’s target as git stores it: the raw target path bytes,
with no trailing newline. This is the “content” of a symlink blob (mode
120000) — git’sdiff_populate_filespecusesstrbuf_readlinkfor a worktree symlink rather than dereferencing it. - tree_
has_ duplicate_ leaf_ paths - virtual_
ancestor_ entry_ map - Build the flattened entry map of the virtual ancestor for a 3-way merge, recursively merging the merge bases together (merge-recursive’s criss-cross construction). Conflicts in the virtual merge are folded into the tree rather than surfaced — the outer merge owns conflict reporting.
Type Aliases§
- Index
Worktree Stat Clean Validator - Merge
Entry Map - Flattened tree: repository-relative path -> (mode, blob/symlink/gitlink oid).