Fixes erroneous git apply patches to the best of its ability.
Project description
patch-fixer
So you asked an LLM to generate a code diff, tried to apply it with git apply, and got a bunch of malformed patch errors? Well fear no more, patch-fixer is here to save the day... more or less.
This tool can also split patches into separate files based on file lists, making it easy to selectively apply changes.
Installation
# Make sure you're using at least python 3.10
python -m venv .venv/
source .venv/bin/activate
pip install patch-fixer
Usage
Command Line Interface
After installation, patch-fixer provides a unified command-line interface:
Fixing broken patches:
patch-fixer fix original broken.patch fixed.patch
where:
originalis the file or directory you were trying to patchbroken.patchis the malformed patch generated by the LLMfixed.patchis the output file containing the (hopefully) fixed patch
Splitting patches by file:
# Split with files specified on command line
patch-fixer split input.patch included.patch excluded.patch -f file1.py file2.py
# Split using a file list
patch-fixer split input.patch included.patch excluded.patch -i files_to_include.txt
where:
input.patchis the patch file to splitincluded.patchwill contain changes for the specified filesexcluded.patchwill contain changes for all other files-fallows specifying files directly on the command line-ireads the file list from a text file (one file per line)
Python API
Fixing patches:
from patch_fixer import fix_patch
patch_file = "/path/to/broken.patch"
original = "/path/to/original/state" # file or directory being patched
with open(patch_file, encoding="utf-8") as f:
patch_lines = f.readlines()
fixed_lines = fix_patch(patch_lines, original)
output_file = "/path/to/fixed.patch"
with open(output_file, 'w', encoding='utf-8') as f:
f.writelines(fixed_lines)
Splitting patches:
from patch_fixer import split_patch
with open("input.patch", encoding="utf-8") as f:
patch_lines = f.readlines()
# split to include only specific files
files_to_include = ["./src/main.py", "./src/utils.py"]
included, excluded = split_patch(patch_lines, files_to_include)
# write the split patches
with open("included.patch", 'w', encoding='utf-8') as f:
f.writelines(included)
with open("excluded.patch", 'w', encoding='utf-8') as f:
f.writelines(excluded)
Known Limitations
- When fixing patches with missing
indexlines, the tool requires the files to be in a git repository to regenerate the index. This is only needed for file deletions and renames. patch-fixerassumes the patch follows git's unified diff format.- Current implementation is not very robust to corrupted hunk content
- Much more comprehensive fuzzy string matching is planned
Local Testing
git clone https://github.com/ajcm474/patch-fixer.git
cd patch-fixer
pip install -e .[test]
pytest
From version 0.3.0 onward (at least until version 1.0), some test failures are expected
in bugfix versions as I like to use test-driven development to build out new features.
Please only report test failures if the same test existed and passed in the most recent 0.x.0 version.
License
This is free and open source software, released under the Apache 2.0 License. See LICENSE for details.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file patch_fixer-0.3.4.tar.gz.
File metadata
- Download URL: patch_fixer-0.3.4.tar.gz
- Upload date:
- Size: 22.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bda0e84b83c5e73208318770425af80548ecc0f396f7d0d3477dd7cdc2ba40c1
|
|
| MD5 |
635dcf3befe1926b82da290377c1f0bd
|
|
| BLAKE2b-256 |
a4c089347cdab869467c25ec7691f40b0df8f6c0cfd27f8f8a8b830b7aa634e6
|
File details
Details for the file patch_fixer-0.3.4-py3-none-any.whl.
File metadata
- Download URL: patch_fixer-0.3.4-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72ee938a55cac57fd94c6edef8cd000e21e81685724c14a933c447028980a103
|
|
| MD5 |
2426b20494c187d69e3db3dd46f141e4
|
|
| BLAKE2b-256 |
58d963eb07341cd5e163e477b4df0aaffa0648ea3a3e2fde907a456789f6b1e7
|