Skip to main content

PatchPy

A modern Python library for patch file parsing (diff file parsing)

PatchPy is a Python library for parsing and applying patch files.

patch_file = PatchFile.from_path("example.patch")
patch_file.validate()
reversed_patch = patch_file.reversed()
reversed_patch.apply(root=".")

Features:

  • Parse unified and context diff formats
  • Apply/revert patches with optional path stripping
  • Fix line counts in hunk headers

Installation

Install via PyPI:

pip install patchpy

Usage

Library API

You can use PatchPy programmatically within your Python code. Here is a simple example:

from patchpy import DiffFile

# Load a patch from a file
diff_file = DiffFile.from_path("example.patch")

# Validate the patch (ie. checks line counts in hunk headers)
diff_file.validate()

# Get attributes of the patch
print(diff_file.modifications[0].header)

# Reverse the patch and apply it
reversed_patch = diff_file.reversed()
reversed_patch.apply(root=".")

CLI

The PatchPy CLI provides a simple interface for applying patches. Below is an example of how to use it:

# Apply a patch file
patchypy apply example.patch

# Apply a patch from a URL
patchypy apply https://example.com/patch.diff

# Display diffstat
patchypy diffstat example.patch

# Apply a patch with stripped leading path components
patchypy apply example.patch -p 1

# Transform a patch
patchypy transform --revert example.patch -o reversed.patch 

# Fix line counts in hunk headers in patch
patchpy transfform --fix-counts example.patch -o fixed.patch

Development

PatchPy uses Rye for dependency management and development workflow. To get started with development, ensure you have Rye installed and then clone the repository and set up the environment:

git clone https://github.com/MatthewScholefield/patchpy.git
cd patchpy
rye sync
rye run pre-commit install

# Run tests
rye test

Contributions are welcome! Feel free to open issues or submit pull requests.

Metadata

Release files for patchpy 2.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for patchpy 2.0.4
File Size Uploaded
patchpy-2.0.4.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for patchpy 2.0.4
File Interpreter ABI Platform
patchpy-2.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 15.0 kB

Release files / patchpy-2.0.4.tar.gz

Download URL patchpy-2.0.4.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
abe93dbd8cf972b0ac063239407ed7cf7415a67b73ac85de3b8e7a772af37f82
BLAKE2b-256 checksum
How to use checksums
d3e3cdac841ab3268b3fec521c40111e9299d5bfd234ff76bcb99a5098ee190e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.2

Release files / patchpy-2.0.4-py3-none-any.whl

Download URL patchpy-2.0.4-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5c03b5ee89385f598c0c1c5e262b47f57f48d48bfda51f827f4e8e3ebdbc57b3
BLAKE2b-256 checksum
How to use checksums
fa339233df81238d7162dbbbe7efad6cf29c1d23da8dd40214b38ce8a5108b30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.2

Release history Release notifications | RSS feed

This release

2.0.4 This release

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page