Expand description
Diff machinery — compare trees, index entries, and working tree files.
§Overview
This module provides the core diffing infrastructure shared by diff,
diff-index, status, log, show, commit, and merge.
§Levels of comparison
- Tree-to-tree — compare two tree objects (e.g. for
log/show). - Tree-to-index — compare a tree (usually HEAD) against the index
(staged changes, used by
diff --cachedandstatus). - Index-to-worktree — compare index against the working directory
(unstaged changes, used by
diffandstatus).
§Content diff
Line-level diffing uses the similar crate (Myers, patience, minimal) and,
for Git’s histogram algorithm, imara-diff for output compatible with upstream Git.
Output formats: unified patch, raw (:old-mode new-mode ...), stat,
numstat.
Structs§
- Diff
Entry - A single diff entry representing one changed path.
- Diff
Index ToWorktree Options - Additional inputs for
diff_index_to_worktree_with_options. - Submodule
Porcelain Flags - Submodule dirty bits aligned with Git’s
DIRTY_SUBMODULE_*/ porcelain v2S???token.
Enums§
- Diff
Status - The kind of change between two sides of a diff.
Constants§
- GIT_
DIFF_ DEFAULT_ BREAK_ SCORE - Default break threshold (
DEFAULT_BREAK_SCOREindiffcore.h), internal 0–GIT_DIFF_MAX_SCOREscale. - GIT_
DIFF_ DEFAULT_ MERGE_ SCORE_ AFTER_ BREAK - Default merge threshold after a break (
DEFAULT_MERGE_SCOREindiffcore.h): pairs broken for rename/copy but not consumed are merged back when deletion-weight is below this (60% by default). - GIT_
DIFF_ MAX_ SCORE - Internal maximum diff score used by Git rename/break heuristics (
MAX_SCOREindiffcore.h). - ZERO_
OID - The zero (null) object ID used for “no object” in diff output.
Functions§
- anchored_
unified_ diff - Compute a unified diff with anchored lines.
- count_
changes - Count insertions and deletions between two strings.
- count_
changes_ with_ algorithm - Count insertions and deletions using the given line-diff algorithm.
- count_
git_ lines - Line count for diffstat/
--numstat, matching Git’scount_lines()indiff.c. - detect_
copies - Detect copies among diff entries.
- detect_
renames - Detect renames by pairing Deleted and Added entries with similar content.
- diff_
index_ to_ tree - Compare the index against a tree (usually HEAD’s tree).
- diff_
index_ to_ worktree - Compare the index against the working tree.
- diff_
index_ to_ worktree_ with_ options - Compare the index against the working tree with optional racy-timestamp context.
- diff_
slice_ ops_ compacted - Line-diff ops for string slices after Git
xdl_change_compact(and optional indent heuristic). - diff_
tree_ to_ worktree - Compare a tree against the working tree.
- diff_
trees - Compare two trees and return the list of changed entries.
- diff_
trees_ show_ tree_ entries - Like
diff_treesbut withshow_treesflag: when true, emit entries for tree objects themselves in addition to their recursive contents (the-tflag ofdiff-tree). - diffcore_
count_ changes - Approximate copied vs added material between two blobs (Git
diffcore_count_changes). - empty_
blob_ oid - Return the ObjectId for the empty blob object.
- format_
mode - Format a numeric mode as a zero-padded octal string.
- format_
raw - Format a diff entry in Git’s raw diff format.
- format_
raw_ abbrev - Format a diff entry with abbreviated OIDs.
- format_
rename_ path - Format a rename pair using Git’s compact path format.
- format_
stat_ line - Generate diff stat output (file name + insertions/deletions).
- format_
stat_ line_ width - hash_
worktree_ file - head_
path_ states - Paths present in
HEAD’s tree with mode and blob/commit OID (for status porcelain v2). - indent_
heuristic_ from_ config diff.indentHeuristicfrom config (Git defaults to true when unset).- map_
new_ to_ old_ lines_ compacted - Map each line in
new_joinedto its origin inold_joinedafter Git-style compaction (for blame). - mode_
from_ metadata - Derive a Git file mode from filesystem metadata.
- normalize_
ignore_ space_ change - Normalise text like Git’s
-b/--ignore-space-change: on each line, collapse runs of whitespace to a single ASCII space and trim trailing spaces. - normalize_
ignore_ space_ change_ line - Normalise one line like Git’s
-b/--ignore-space-change. - parse_
diff_ rename_ score_ token - Parse a single Git
parse_rename_scoretoken (50,50%, decimal forms) into internal 0–GIT_DIFF_MAX_SCOREunits. - parse_
indent_ heuristic_ cli_ flags - Parse
--indent-heuristic/--no-indent-heuristicfrom a plumbing argv slice (last occurrence wins). - read_
submodule_ head_ for_ checkout - Read the HEAD commit OID from a submodule checkout directory.
- read_
submodule_ head_ oid - Read the HEAD commit OID from a submodule working tree directory.
- rename_
similarity_ score - Compute rename/copy similarity percentage (0–100) between two byte slices.
- resolve_
indent_ heuristic - Resolve indent heuristic:
--no-indent-heuristicand--indent-heuristicoverride config. - rewrite_
dissimilarity_ index_ percent - Percentage shown in
dissimilarity index N%for a rewrite (similarity_indexin Git’s diff.c). - rewrite_
merge_ score - Git
merge_scorefromdiffcore-break.cwhen a pair is considered broken: how much of the source blob was removed (0–[DIFF_MAX_SCORE] scale). Used fordissimilarity indexmetadata. - should_
break_ rewrite_ for_ stat - Whether this modified blob pair should use Git’s “complete rewrite” diffstat path when
--break-rewritesis in effect (should_breakindiffcore-break.c). - should_
break_ rewrite_ pair - Whether an in-place blob edit should be split into delete+create for rename/copy (
should_breakindiffcore-break.c).break_scoreis on the internal 0–GIT_DIFF_MAX_SCOREscale (default [DIFF_DEFAULT_BREAK_SCORE]). - stat_
matches - status_
apply_ rename_ copy_ detection - Apply Git-style rename and optional copy detection for index↔worktree diffs.
- submodule_
commit_ subject_ line - First line of a commit’s message for
git diff --submodule=logoutput. - submodule_
embedded_ git_ dir - Resolve the embedded git directory for a submodule work tree (
sub_dir/.git). - submodule_
porcelain_ flags - Inspect a checked-out submodule at
rel_path(relative tosuper_worktree) and return flags used forgit status --porcelain=v2submodule tokens. - unified_
diff - Generate a unified diff patch for two blobs.
- unified_
diff_ histogram_ hunks_ only - Unified diff hunks for Git’s histogram algorithm (no
---/+++lines). - unified_
diff_ histogram_ with_ prefix_ and_ funcname - Full unified diff (
---/+++/ hunks) using Git’s histogram algorithm. - unified_
diff_ with_ prefix - Same as
unified_diffbut with configurable source/destination prefixes. - unified_
diff_ with_ prefix_ and_ funcname - Same as
unified_diff_with_prefixwith optional custom hunk-header function-name matching. - unified_
diff_ with_ prefix_ and_ funcname_ and_ algorithm - Same as
unified_diff_with_prefix_and_funcnamebut allows callers to choose the line diff algorithm used for hunk generation. - worktree_
differs_ from_ index_ entry - Quick stat check: does the index entry’s cached stat data match the file?
Returns true when the file at
ie’s path differs from the index entry (mode or blob). - zero_
oid - Return the zero ObjectId.