# wilfred/difftastic architecture

> Architecture diagram of the GitHub repository wilfred/difftastic, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wilfred/difftastic
- Repository: https://github.com/wilfred/difftastic

## Overview

Difftastic is a command-line structural diff tool: users provide two files or directories, the program identifies their content and language, parses supported syntax, computes syntax-aware changes, and renders a human-readable or JSON result. It also supports plain-text fallback, merge-conflict inputs, check-only comparisons, and multiple display formats. The graph follows relationships visible in excerpts or documented by the README; where routing to a substantial but unsampled component is documented only indirectly, the graph omits that edge rather than assuming a call.

Main entry points: [`src/main.rs`](https://github.com/wilfred/difftastic/blob/master/src/main.rs) (CLI entry), [`src/display/hunks.rs`](https://github.com/wilfred/difftastic/blob/master/src/display/hunks.rs) (Hunk rendering), [`src/parse/tree_sitter_parser.rs`](https://github.com/wilfred/difftastic/blob/master/src/parse/tree_sitter_parser.rs) (Tree-sitter parsing), [`src/line_parser.rs`](https://github.com/wilfred/difftastic/blob/master/src/line_parser.rs) (Text fallback)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI and inputs"]
  node_entry["CLI entry<br/>[main.rs]"]
  node_options["Options<br/>[options.rs]"]
  node_file_input["File inputs<br/>[files.rs]"]
  node_conflicts["Conflict inputs<br/>[conflicts.rs]"]
end

subgraph group_parse["Language parsing"]
  node_language["Language detection<br/>[guess_language.rs]"]
  node_parser["Tree-sitter parsing"]
  node_syntax["Syntax model<br/>[syntax.rs]"]
  node_line_parser["Text fallback<br/>[line_parser.rs]"]
  node_lines["Line positions<br/>[lines.rs]"]
end

subgraph group_engine["Diff engine"]
  node_unchanged["Unchanged matching<br/>[unchanged.rs]"]
  node_graph_diff["Structural graph diff<br/>[graph.rs]"]
  node_shortest["Path search<br/>[shortest_path.rs]"]
  node_changes["Change metadata<br/>[changes.rs]"]
  node_sliders["Change refinement<br/>[sliders.rs]"]
  node_lcs["Line sequence diff<br/>[lcs_diff.rs]"]
  node_words["Word highlighting<br/>[words.rs]"]
end

subgraph group_output["Results and display"]
  node_context["Display context<br/>[context.rs]"]
  node_hunks["Hunk rendering<br/>[hunks.rs]"]
  node_side_by_side["Side-by-side output<br/>[side_by_side.rs]"]
  node_inline["Inline output<br/>[inline.rs]"]
  node_json["JSON output<br/>[json.rs]"]
  node_style["Terminal styling<br/>[style.rs]"]
end

node_user(("User"))
node_stdout["Diff result"]

node_user -->|"invokes"| node_entry
node_entry -->|"parses options"| node_options
node_entry -->|"reads files"| node_file_input
node_entry -->|"loads conflicts"| node_conflicts
node_entry -->|"detects language"| node_language
node_entry -->|"parses syntax"| node_parser
node_entry -->|"falls back"| node_line_parser
node_language -->|"selects language"| node_parser
node_parser -->|"builds tree"| node_syntax
node_parser -->|"uses positions"| node_lines
node_line_parser -->|"aligns lines"| node_lcs
node_line_parser -->|"emits matches"| node_syntax
node_entry -->|"finds unchanged"| node_unchanged
node_entry -->|"computes diff"| node_shortest
node_unchanged -->|"marks matches"| node_changes
node_unchanged -->|"aligns nodes"| node_lcs
node_shortest -->|"searches graph"| node_graph_diff
node_graph_diff -->|"records changes"| node_changes
node_graph_diff -->|"compares nodes"| node_syntax
node_entry -.->|"refines changes"| node_sliders
node_entry -->|"selects context"| node_context
node_entry -->|"renders hunks"| node_hunks
node_hunks -->|"uses context"| node_context
node_hunks -->|"formats columns"| node_side_by_side
node_entry -.->|"renders inline"| node_inline
node_entry -.->|"renders JSON"| node_json
node_entry -->|"configures styling"| node_style
node_hunks -.->|"styles output"| node_style
node_entry -.->|"writes result"| node_stdout

click node_entry "https://github.com/wilfred/difftastic/blob/master/src/main.rs"
click node_options "https://github.com/wilfred/difftastic/blob/master/src/options.rs"
click node_file_input "https://github.com/wilfred/difftastic/blob/master/src/files.rs"
click node_conflicts "https://github.com/wilfred/difftastic/blob/master/src/conflicts.rs"
click node_language "https://github.com/wilfred/difftastic/blob/master/src/parse/guess_language.rs"
click node_parser "https://github.com/wilfred/difftastic/blob/master/src/parse/tree_sitter_parser.rs"
click node_syntax "https://github.com/wilfred/difftastic/blob/master/src/parse/syntax.rs"
click node_line_parser "https://github.com/wilfred/difftastic/blob/master/src/line_parser.rs"
click node_lines "https://github.com/wilfred/difftastic/blob/master/src/lines.rs"
click node_unchanged "https://github.com/wilfred/difftastic/blob/master/src/diff/unchanged.rs"
click node_graph_diff "https://github.com/wilfred/difftastic/blob/master/src/diff/graph.rs"
click node_shortest "https://github.com/wilfred/difftastic/blob/master/src/diff/shortest_path.rs"
click node_changes "https://github.com/wilfred/difftastic/blob/master/src/diff/changes.rs"
click node_sliders "https://github.com/wilfred/difftastic/blob/master/src/diff/sliders.rs"
click node_lcs "https://github.com/wilfred/difftastic/blob/master/src/diff/lcs_diff.rs"
click node_words "https://github.com/wilfred/difftastic/blob/master/src/words.rs"
click node_context "https://github.com/wilfred/difftastic/blob/master/src/display/context.rs"
click node_hunks "https://github.com/wilfred/difftastic/blob/master/src/display/hunks.rs"
click node_side_by_side "https://github.com/wilfred/difftastic/blob/master/src/display/side_by_side.rs"
click node_inline "https://github.com/wilfred/difftastic/blob/master/src/display/inline.rs"
click node_json "https://github.com/wilfred/difftastic/blob/master/src/display/json.rs"
click node_style "https://github.com/wilfred/difftastic/blob/master/src/display/style.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_options,node_file_input,node_conflicts,node_user toneBlue
class node_language,node_parser,node_syntax,node_line_parser,node_lines toneAmber
class node_unchanged,node_graph_diff,node_shortest,node_changes,node_sliders,node_lcs,node_words toneMint
class node_context,node_hunks,node_side_by_side,node_inline,node_json,node_style toneRose
class node_stdout toneIndigo
```

## Components

### CLI and inputs

- [CLI entry](https://github.com/wilfred/difftastic/blob/master/src/main.rs): `src/main.rs`
- [Options](https://github.com/wilfred/difftastic/blob/master/src/options.rs): `src/options.rs`
- [File inputs](https://github.com/wilfred/difftastic/blob/master/src/files.rs): `src/files.rs`
- [Conflict inputs](https://github.com/wilfred/difftastic/blob/master/src/conflicts.rs): `src/conflicts.rs`

### Language parsing

- [Language detection](https://github.com/wilfred/difftastic/blob/master/src/parse/guess_language.rs): `src/parse/guess_language.rs`
- [Tree-sitter parsing](https://github.com/wilfred/difftastic/blob/master/src/parse/tree_sitter_parser.rs): `src/parse/tree_sitter_parser.rs`
- [Syntax model](https://github.com/wilfred/difftastic/blob/master/src/parse/syntax.rs): `src/parse/syntax.rs`
- [Text fallback](https://github.com/wilfred/difftastic/blob/master/src/line_parser.rs): `src/line_parser.rs`
- [Line positions](https://github.com/wilfred/difftastic/blob/master/src/lines.rs): `src/lines.rs`

### Diff engine

- [Unchanged matching](https://github.com/wilfred/difftastic/blob/master/src/diff/unchanged.rs): `src/diff/unchanged.rs`
- [Structural graph diff](https://github.com/wilfred/difftastic/blob/master/src/diff/graph.rs): `src/diff/graph.rs`
- [Path search](https://github.com/wilfred/difftastic/blob/master/src/diff/shortest_path.rs): `src/diff/shortest_path.rs`
- [Change metadata](https://github.com/wilfred/difftastic/blob/master/src/diff/changes.rs): `src/diff/changes.rs`
- [Change refinement](https://github.com/wilfred/difftastic/blob/master/src/diff/sliders.rs): `src/diff/sliders.rs`
- [Line sequence diff](https://github.com/wilfred/difftastic/blob/master/src/diff/lcs_diff.rs): `src/diff/lcs_diff.rs`
- [Word highlighting](https://github.com/wilfred/difftastic/blob/master/src/words.rs): `src/words.rs`

### Results and display

- [Display context](https://github.com/wilfred/difftastic/blob/master/src/display/context.rs): `src/display/context.rs`
- [Hunk rendering](https://github.com/wilfred/difftastic/blob/master/src/display/hunks.rs): `src/display/hunks.rs`
- [Side-by-side output](https://github.com/wilfred/difftastic/blob/master/src/display/side_by_side.rs): `src/display/side_by_side.rs`
- [Inline output](https://github.com/wilfred/difftastic/blob/master/src/display/inline.rs): `src/display/inline.rs`
- [JSON output](https://github.com/wilfred/difftastic/blob/master/src/display/json.rs): `src/display/json.rs`
- [Terminal styling](https://github.com/wilfred/difftastic/blob/master/src/display/style.rs): `src/display/style.rs`

### Other components

- **User**
- **Diff result**

## Connections

- User → CLI entry: invokes (evidence: `src/main.rs`)
- CLI entry → Options: parses options (evidence: `src/main.rs`)
- CLI entry → File inputs: reads files (evidence: `src/main.rs`)
- CLI entry → Conflict inputs: loads conflicts (evidence: `src/main.rs`)
- CLI entry → Language detection: detects language (evidence: `src/main.rs`)
- CLI entry → Tree-sitter parsing: parses syntax (evidence: `src/main.rs`)
- CLI entry → Text fallback: falls back (evidence: `src/main.rs`)
- Language detection → Tree-sitter parsing: selects language (evidence: `src/parse/tree_sitter_parser.rs`)
- Tree-sitter parsing → Syntax model: builds tree (evidence: `src/parse/tree_sitter_parser.rs`)
- Tree-sitter parsing → Line positions: uses positions (evidence: `src/parse/tree_sitter_parser.rs`)
- Text fallback → Line sequence diff: aligns lines (evidence: `src/line_parser.rs`)
- Text fallback → Syntax model: emits matches (evidence: `src/line_parser.rs`)
- CLI entry → Unchanged matching: finds unchanged (evidence: `src/main.rs`)
- CLI entry → Path search: computes diff (evidence: `src/main.rs`)
- Unchanged matching → Change metadata: marks matches (evidence: `src/diff/unchanged.rs`)
- Unchanged matching → Line sequence diff: aligns nodes (evidence: `src/diff/unchanged.rs`)
- Path search → Structural graph diff: searches graph (evidence: `src/diff/shortest_path.rs`)
- Structural graph diff → Change metadata: records changes (evidence: `src/diff/graph.rs`)
- Structural graph diff → Syntax model: compares nodes (evidence: `src/diff/graph.rs`)
- CLI entry → Change refinement: refines changes (evidence: `src/main.rs`)
- CLI entry → Display context: selects context (evidence: `src/main.rs`)
- CLI entry → Hunk rendering: renders hunks (evidence: `src/main.rs`)
- Hunk rendering → Display context: uses context (evidence: `src/display/hunks.rs`)
- Hunk rendering → Side-by-side output: formats columns (evidence: `src/display/hunks.rs`)
- CLI entry → Inline output: renders inline
- CLI entry → JSON output: renders JSON
- CLI entry → Terminal styling: configures styling (evidence: `src/main.rs`)
- Hunk rendering → Terminal styling: styles output
- CLI entry → Diff result: writes result
