{"id":2470,"library":"diff-parser","title":"Diff Parser","description":"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.","status":"active","version":"1.1","language":"en","source_language":"en","source_url":"https://github.com/shaiksamad/diff-parser.git","tags":["diff","git","parser","code analysis","version control"],"install":[{"cmd":"pip install diff-parser","lang":"bash","label":"Install stable release"}],"dependencies":[],"imports":[{"symbol":"Diff","correct":"from diff_parser import Diff"}],"quickstart":{"code":"import tempfile\nimport os\nfrom diff_parser import Diff\n\n# Create a dummy diff string representing file changes\ndiff_content = \"\"\"diff --git a/old_file.py b/new_file.py\nindex 1234567..890abcd 100644\n--- a/old_file.py\n+++ b/new_file.py\n@@ -1,3 +1,4 @@\n# This is an old line\n-print(\"Hello, old world!\")\n+print(\"Hello, new world!\")\n+print(\"Another new line\")\n# End of file\n\"\"\"\n\n# diff-parser expects a file path, so we write the diff content to a temporary file.\ntemp_dir = tempfile.mkdtemp()\ndiff_file_path = os.path.join(temp_dir, \"example.diff\")\n\ntry:\n    with open(diff_file_path, \"w\") as f:\n        f.write(diff_content)\n\n    # Initialize the Diff parser with the path to the diff file\n    diff = Diff(diff_file_path)\n\n    # Iterate through each changed file block in the diff\n    for block in diff:\n        print(f\"--- File Change ---\")\n        print(f\"Old filename: {block.old_filename}\")\n        print(f\"New filename: {block.new_filename}\")\n        print(f\"New filepath: {block.new_filepath}\")\n        print(f\"Lines added: {block.added_lines_count}\")\n        print(f\"Lines removed: {block.removed_lines_count}\")\n        # Accessing individual hunks and lines is also possible:\n        # for hunk in block.hunks:\n        #     for line in hunk.lines:\n        #         print(f\"  Line type: {line.type}, Content: {line.value.strip()}\")\n\nfinally:\n    # Clean up the temporary file and directory\n    os.remove(diff_file_path)\n    os.rmdir(temp_dir)\n","lang":"python","description":"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."},"warnings":[{"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.","message":"The `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.","severity":"gotcha","affected_versions":"1.0 to 1.1"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"1.0 to 1.1"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}