A modern Python library for patch file parsing (diff file parsing)
Project description
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file patchpy-2.0.4.tar.gz
.
File metadata
- Download URL: patchpy-2.0.4.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | abe93dbd8cf972b0ac063239407ed7cf7415a67b73ac85de3b8e7a772af37f82 |
|
MD5 | e1973196e964564d3325322678c09965 |
|
BLAKE2b-256 | d3e3cdac841ab3268b3fec521c40111e9299d5bfd234ff76bcb99a5098ee190e |
File details
Details for the file patchpy-2.0.4-py3-none-any.whl
.
File metadata
- Download URL: patchpy-2.0.4-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c03b5ee89385f598c0c1c5e262b47f57f48d48bfda51f827f4e8e3ebdbc57b3 |
|
MD5 | 293495ebf8545b0f8fa1d07014acaa38 |
|
BLAKE2b-256 | fa339233df81238d7162dbbbe7efad6cf29c1d23da8dd40214b38ce8a5108b30 |