grit
CLILibrary
HEAD → docs/library/diff
Library guide / DiffMarkdown

Diff

Tree-to-tree and index-to-worktree diffs, blob patches, and the DiffEntry model the grit CLI renders.

Diffing in grit-lib centers on diff and the diffing module view. Results are DiffEntry rows with a DiffStatus letter (M, A, D, …), paths, modes, and object ids — the same shape grit diff and porcelain::status use before formatting output.

Tree-to-tree

diff_trees compares two tree objects recursively and returns changed paths. Pass None for either side to diff against an empty tree. diff_trees_show_tree_entries can emit tree objects themselves (Git’s diff-tree -t behavior).

Commit-to-commit diffs resolve each commit’s tree with parse_commit, then call diff_trees on the parent and child trees.

Index-to-worktree

diff_index_to_worktree compares the index to files on disk. diff_index_to_worktree_with_options adds index mtime, submodule/gitlink handling, broken-gitlink detection, and an optional repository git-dir override. porcelain::add::stage uses diff_index_to_worktree_for_staging internally so staging sees the same dirty paths as status.

Blob diffs

For a single modified file, read old and new bytes from Odb and pass them to unified_diff (histogram algorithm, Git-compatible hunks). The CLI builds human, --json, and --markdown views from DiffEntry lists plus optional unified bodies.

Porcelain models

  • Status — combines index vs HEAD and index vs worktree scans into structured sections (the CLI maps them to default / --json / --markdown output).
  • Add — uses diff results to decide which paths to hash and stage.
  • Diff command — tree-to-tree for commit ranges, index-to-worktree for uncommitted changes, then blob-level rendering for text files.