unidiff

0.7.5 · active · verified Sun Apr 05

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.

Warnings

Install

Imports

Quickstart

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.

import urllib.request
from unidiff import PatchSet

# Example: Parse a diff from a GitHub pull request URL
diff_url = 'https://github.com/matiasb/python-unidiff/pull/3.diff'
with urllib.request.urlopen(diff_url) as response:
    diff_data = response.read()
    encoding = response.headers.get_content_charset() or 'utf-8'
    patch = PatchSet(diff_data.decode(encoding))

print(f"Parsed {len(patch)} files in the diff.")
for patched_file in patch:
    print(f"File: {patched_file.path}")
    print(f"  Added: {patched_file.added} lines, Removed: {patched_file.removed} lines")
    for hunk in patched_file:
        for line in hunk:
            if line.is_added:
                print(f"    + {line.value.strip()}")
            elif line.is_removed:
                print(f"    - {line.value.strip()}")

view raw JSON →