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-parserVerified import paths — ran on the pinned version, not inferred.
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.
Write your diff content to a temporary file using `tempfile` or a similar approach, then pass the file's path to the `Diff()` constructor.
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.
Ensure the package is installed using pip: `pip install diff-parser`. Then, import it correctly: `from diff_parser import DiffParser`.
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.
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.
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:`
No dependency data recorded yet.