skillfed

patch-ng

Library to parse and apply unified diffs.

patch-ng v1.19.1 4.7M downloads/30d#2,250 on PyPI40
Permissive license MIT Active released

What it is and what it does

Patch NG is a maintained fork of the original python-patch library, designed to parse and apply unified diff patches to files. It operates as a self-contained Python module with no external dependencies, making it suitable for embedding directly in projects or installing via pip. The library automatically corrects common diff issues—linefeeds mismatched to the target file, diffs corrupted by whitespace stripping, and a/ and b/ prefixes—and detects patch format (SVN, HG, GIT). It runs on Python 3.6 and later across Linux, Windows, and macOS.

The fork was created to address stalled maintenance in the original project, accepting pull requests that had been pending review since mid-2019. It works as both a command-line tool and a library, offering a diffstat histogram for inspection. The package does not handle file renaming, creation, removal, directory tree operations, or version-control-specific metadata; it is limited to applying unified diff formats to existing files.

Use it for:

  • Automatically apply code patches or configuration diffs during build or deployment pipelines.
  • Parse and inspect unified diffs programmatically to extract or validate patch metadata.
  • Correct malformed patches by automatically fixing whitespace and linefeed inconsistencies.
  • Embed patch application logic in tools without introducing external dependencies.
  • Generate and display diffstat histograms for patch review or reporting.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Parses and applies unified diff patches to files, with automatic correction of linefeeds, whitespace issues, and diff prefixes.

Yes. The package is actively maintained, has no external dependencies, and solves a specific, well-defined problem—parsing and applying unified diffs with automatic correction of common issues. It is stable (Production/Stable status), widely used (top 5000 PyPI packages), carries no known vulnerabilities, and uses a permissive MIT license. Install it if you need to apply patches programmatically or embed patch logic without dependency overhead.

Install

patch-ng on PyPI

pip

pip install patch-ng

uv

uv add patch-ng

poetry

poetry add patch-ng

Installing patch-ng

Before you install

Low friction: pure Python with no external dependencies, distributed as a single wheel. Actively maintained with recent commits; last release 113 days ago.

License in practice

MIT license permits unrestricted use, modification, and distribution in commercial and private projects with minimal obligations.

Quickstart

pip install patch-ng

from patch_ng import PatchSet

with open('diff.patch') as f:
    patches = PatchSet(f)
    patches.apply()

Requires Python 3.6 or later; file renaming, creation, removal, and directory operations are not supported.

Verify before relying

  • Exact behavior when applying patches to files with mixed line endings or encoding issues.
  • Performance characteristics on large patches or files.
  • Whether the package handles edge cases in malformed diffs beyond the documented automatic corrections.

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.6)
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance actively maintained — 113 days since the last release
Last repo commit
First released
Downloads 4,710,651/month — #2,250 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: patch_ng-1.19.1-py3-none-any.whl

Keywords: patch, parse, diff, strip, diffstat

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseProgramming Language :: Python :: 3Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Topic :: Software Development :: Build Tools

Tags

parse unified diffsapply patch filesdiff patching libraryunified diff parserpatch file applicationdiff format handlingautomatic patch correction
diff-patchingbuild-automationno-dependencies

More Build Tools packages