unidiff
Rust library for parsing unified diffs and extracting structured patch metadata.
Repository Health
Technical Analysis
unidiff is a small Rust library for parsing unified diff (patch) text into a structured, navigable object model. It reads the output produced by git, svn, hg, and other tools and gives you typed access to the patch set, its files, hunks, and individual added/removed/context lines.
Beyond raw parsing, it exposes metadata such as which files were added, modified, or removed and per-file line counts, making it easy to build code review tooling, diff statistics, and patch-analysis pipelines. Character encoding handling is available via an optional encoding feature.
What You Get
- A
PatchSetparser that consumes unified diff text from git, svn, hg, and similar tools - Typed access to patched files, hunks, and added/removed/context lines
- File-level metadata: added/modified/removed classification and line counts
- Optional character-encoding handling via the
encodingfeature (encoding_rs) - A dependency-light crate built on the
regexengine
Common Use Cases
- Parsing git/svn/hg diffs to compute added and removed line statistics
- Building code review or patch-analysis tooling on top of structured diffs
- Filtering or transforming hunks programmatically before applying or displaying them
- Extracting the list of changed files and their change types from a patch
Under The Hood
Architecture The crate is implemented in a single src/lib.rs that uses compiled regex patterns to recognize the unified-diff grammar — file headers, hunk @@ ranges, and line prefixes — and assembles them into a nested PatchSet -> PatchedFile -> Hunk -> line model, with iterator-based access and derived metadata (added/removed/modified, line counts). Tech Stack Rust (2024 edition) depending only on regex, with an optional encoding feature backed by encoding_rs for non-UTF-8 diff decoding; documentation is published to docs.rs and a benches/ directory holds criterion-style benchmarks. Code Quality For its size the crate is well-structured, with an integration tests/ directory (3 test modules exercising real diff fixtures) and CI, though maintenance cadence and community footprint are modest. API Design The surface is minimal and idiomatic — construct a PatchSet, call parse on diff text, then iterate typed files and hunks — so there is little to learn and almost no boilerplate to get from a diff string to structured data.