unidiff is a simple Python library for parsing and interacting with unified diff data. It allows developers to extract metadata, file changes, and hunk information from diffs. The current version is 0.7.5, and it maintains an active development status with periodic releases addressing bug fixes and feature enhancements.
pip install unidiffVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to fetch a diff from a URL and parse it using `PatchSet`. It then iterates through the patched files and their hunks to display added and removed lines. `PatchSet` can also be instantiated from a file-like object or directly from a string.
Update your code to use `PatchedFile.path` for the target filename of renamed files. Review `PatchedFile` attributes for source filename if needed.
Upgrade to unidiff v0.7.5 or later to ensure correct parsing of diffs with spaces in filenames and `--no-prefix` headers.
Ensure that the diff data provided to `PatchSet` includes valid filename headers (e.g., `--- a/old_file.py` and `+++ b/new_file.py`). If processing output from `difflib`, you may need to pre-process it to add these headers or handle the `UnidiffParseError`.
Upgrade to unidiff v0.7.5 to ensure robust handling and identification of binary files within patch sets. Use `PatchedFile.is_binary_file` to check file type.
pip install unidiff
```python
import unidiff
with open('my_diff.diff', 'r', encoding='utf-8') as f:
diff_content = f.read() # Read the content into a string
diff_set = unidiff.PatchSet(diff_content)
``````python
import unidiff
diff_text = """---
a/file.py\n+++ b/file.py\n@@ -1 +1 @@\n-old\n+new\n"""
patch_set = unidiff.PatchSet(diff_text)
for diff_obj in patch_set.diffs:
print(f"Processing diff for {diff_obj.source_file} -> {diff_obj.target_file}")
``````python
import unidiff
diff_text = """---
/dev/null\n+++ b/new_file.txt\n@@ -0,0 +1,1 @@\n+New content\n"""
patch_set = unidiff.PatchSet(diff_text)
for diff_obj in patch_set.diffs:
if diff_obj.is_new_file:
print(f"'{diff_obj.target_file}' is a new file.")
```No dependency data recorded yet.