Registry / serialization / diff-parser

diff-parser

JSON →
library1.1pypypi✓ verified 25d ago

diff-parser is a Python package designed for parsing and representing diff files, specifically supporting git diff data or .diff file formats. It provides structured access to various properties for each changed file, including filenames, file paths, source-hashes, target-hashes, and line counts. Currently at version 1.1, the library appears actively maintained with recent updates, though it doesn't follow a strict release cadence, releasing on demand.

pip install diff-parser
INSTALL
IMPORT
SIG · DIFF-PARSER
D
diff-parser
serializationpythonv1.1
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Diff
from diff_parser import Diff

This quickstart demonstrates how to parse a diff string using `diff-parser` by writing it to a temporary file and then creating a `Diff` object from its path. It then iterates through the parsed file blocks to extract key information such as filenames and line change counts.

import tempfile import os from diff_parser import Diff # Create a dummy diff string representing file changes diff_content = """diff --git a/old_file.py b/new_file.py index 1234567..890abcd 100644 --- a/old_file.py +++ b/new_file.py @@ -1,3 +1,4 @@ # This is an old line -print("Hello, old world!") +print("Hello, new world!") +print("Another new line") # End of file """ # diff-parser expects a file path, so we write the diff content to a temporary file. temp_dir = tempfile.mkdtemp() diff_file_path = os.path.join(temp_dir, "example.diff") try: with open(diff_file_path, "w") as f: f.write(diff_content) # Initialize the Diff parser with the path to the diff file diff = Diff(diff_file_path) # Iterate through each changed file block in the diff for block in diff: print(f"--- File Change ---") print(f"Old filename: {block.old_filename}") print(f"New filename: {block.new_filename}") print(f"New filepath: {block.new_filepath}") print(f"Lines added: {block.added_lines_count}") print(f"Lines removed: {block.removed_lines_count}") # Accessing individual hunks and lines is also possible: # for hunk in block.hunks: # for line in hunk.lines: # print(f" Line type: {line.type}, Content: {line.value.strip()}") finally: # Clean up the temporary file and directory os.remove(diff_file_path) os.rmdir(temp_dir)
Debug
Known issues
gotchaThe `Diff` class constructor strictly expects a file path as input, not a raw diff string. To parse a string, it must first be written to a temporary file.
fix
Write your diff content to a temporary file using `tempfile` or a similar approach, then pass the file's path to the `Diff()` constructor.
affects: 1.0 to 1.1
gotchaThe library primarily targets 'git diff data or .diff file' format. Variations in diff formats (e.g., from different VCS or `difflib`) might not be fully supported or could lead to unexpected parsing behavior. The documentation does not specify robust error handling for malformed diff inputs.
fix
Ensure that the input diff data strictly adheres to standard Git unified diff format. Validate or sanitize diff inputs if they originate from diverse sources to prevent parsing failures.
affects: 1.0 to 1.1
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'diff_parser'
The `diff-parser` package has not been installed in the Python environment, or there is a typo in the import statement.
fix
Ensure the package is installed using pip: `pip install diff-parser`. Then, import it correctly: `from diff_parser import DiffParser`.
diff_parser.DiffParserError: Invalid diff format: No diff header found
The input string or file provided to `DiffParser.parse()` does not conform to the expected git diff or .diff file format, specifically lacking a recognizable header.
fix
Provide a valid diff string that includes the standard git diff headers (e.g., 'diff --git a/file1.py b/file1.py'). Pre-process the input to ensure it's a legitimate diff.
TypeError: 'NoneType' object is not iterable
This typically occurs when attempting to iterate over or access properties of an object that is `None`. In the context of `diff-parser`, this could happen if a parsing method fails (e.g., due to malformed input) and implicitly returns `None`, and the subsequent code doesn't handle this `None` return.
fix
Always check the return value of parsing methods to ensure it's not `None` before attempting to process it. For instance, `parsed_diff = DiffParser(diff_string).parse()` should be followed by a check like `if parsed_diff:` before accessing its attributes or iterating over files.
AttributeError: 'DiffParser' object has no attribute 'files'
This error occurs when attempting to access the `files` attribute (or other result-specific attributes) directly on the `DiffParser` instance before calling the `parse()` method, or if `parse()` failed and returned an unexpected type.
fix
Ensure that the `parse()` method is called on the `DiffParser` instance to process the diff data and populate its attributes, typically assigning the result to a variable that can then be queried. Example: `parser = DiffParser(diff_content); parsed_data = parser.parse(); for file in parsed_data.files:`
Upgrade
Version history
1.1latest on PyPI · released Sep 9, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
25 hits · last 30 days
node
20
Resources
diff-parser — pip install diff-parser · libregistry