Skip to main content

Module diff

Module diff 

Source
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

  1. Tree-to-tree — compare two tree objects (e.g. for log/show).
  2. Tree-to-index — compare a tree (usually HEAD) against the index (staged changes, used by diff --cached and status).
  3. Index-to-worktree — compare index against the working directory (unstaged changes, used by diff and status).

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

DiffEntry
A single diff entry representing one changed path.
DiffIndexToWorktreeOptions
Additional inputs for diff_index_to_worktree_with_options.
SubmodulePorcelainFlags
Submodule dirty bits aligned with Git’s DIRTY_SUBMODULE_* / porcelain v2 S??? token.

Enums§

DiffStatus
The kind of change between two sides of a diff.

Constants§

GIT_DIFF_DEFAULT_BREAK_SCORE
Default break threshold (DEFAULT_BREAK_SCORE in diffcore.h), internal 0–GIT_DIFF_MAX_SCORE scale.
GIT_DIFF_DEFAULT_MERGE_SCORE_AFTER_BREAK
Default merge threshold after a break (DEFAULT_MERGE_SCORE in diffcore.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_SCORE in diffcore.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’s count_lines() in diff.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_trees but with show_trees flag: when true, emit entries for tree objects themselves in addition to their recursive contents (the -t flag of diff-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.indentHeuristic from config (Git defaults to true when unset).
map_new_to_old_lines_compacted
Map each line in new_joined to its origin in old_joined after 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_score token (50, 50%, decimal forms) into internal 0–GIT_DIFF_MAX_SCORE units.
parse_indent_heuristic_cli_flags
Parse --indent-heuristic / --no-indent-heuristic from 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-heuristic and --indent-heuristic override config.
rewrite_dissimilarity_index_percent
Percentage shown in dissimilarity index N% for a rewrite (similarity_index in Git’s diff.c).
rewrite_merge_score
Git merge_score from diffcore-break.c when a pair is considered broken: how much of the source blob was removed (0–[DIFF_MAX_SCORE] scale). Used for dissimilarity index metadata.
should_break_rewrite_for_stat
Whether this modified blob pair should use Git’s “complete rewrite” diffstat path when --break-rewrites is in effect (should_break in diffcore-break.c).
should_break_rewrite_pair
Whether an in-place blob edit should be split into delete+create for rename/copy (should_break in diffcore-break.c). break_score is on the internal 0–GIT_DIFF_MAX_SCORE scale (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=log output.
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 to super_worktree) and return flags used for git status --porcelain=v2 submodule 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_diff but with configurable source/destination prefixes.
unified_diff_with_prefix_and_funcname
Same as unified_diff_with_prefix with optional custom hunk-header function-name matching.
unified_diff_with_prefix_and_funcname_and_algorithm
Same as unified_diff_with_prefix_and_funcname but 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.