{"id":3321,"library":"whatthepatch","title":"whatthepatch","description":"What The Patch is a Python library for parsing and applying patch files. It is currently at version 1.0.7, with its latest release on November 16, 2024. While the functions are stable and reliable, the project has historically had limited active development from the maintainer, though pull requests are considered and released.","status":"active","version":"1.0.7","language":"en","source_language":"en","source_url":"https://github.com/cscorley/whatthepatch","tags":["patch parsing","diff","patch application","version control"],"install":[{"cmd":"pip install whatthepatch","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Requires Python 3.9 or newer.","package":"python","optional":false}],"imports":[{"symbol":"whatthepatch","correct":"import whatthepatch"},{"note":"Often accessed via `whatthepatch.parse_patch` after `import whatthepatch`.","symbol":"parse_patch","correct":"from whatthepatch import parse_patch"},{"note":"Often accessed via `whatthepatch.apply_diff` after `import whatthepatch`.","symbol":"apply_diff","correct":"from whatthepatch import apply_diff"}],"quickstart":{"code":"import whatthepatch\nimport io\n\npatch_content = \"\"\"--- lao\\t2012-12-26 23:16:54.000000000 -0600\n+++ tzu\\t2012-12-26 23:16:50.000000000 -0600\n@@ -1,7 +1,6 @@\n-The Way that can be told of is not the eternal Way;\n-The name that can be named is not the eternal name.\n The Nameless is the origin of Heaven and Earth;\n-The Named is the mother of all things.\n+The named is the mother of all things.\n Therefore let there always be non-being, so we may see their subtlety,\n And let there always be being,\n@@ -9,3 +8,6 @@\n The two are the same,\n But after they are produced,\n they have different names.\n+They both may be called deep and profound.\n+Deeper and more profound,\n+The door of all subtleties!\"\"\"\n\noriginal_file_content = \"\"\"The Way that can be told of is not the eternal Way;\nThe name that can be named is not the eternal name.\nThe Nameless is the origin of Heaven and Earth;\nThe Named is the mother of all things.\nTherefore let there always be non-being, so we may see their subtlety,\nAnd let there always be being,\nThe two are the same,\nBut after they are produced,\nthey have different names.\"\"\"\n\n# Parse the patch\nprint(\"--- Parsing Patch ---\")\npatches = list(whatthepatch.parse_patch(patch_content))\nfor diff in patches:\n    print(f\"Diff for file: {diff.header.old_path} -> {diff.header.new_path}\")\n    for change in diff.changes:\n        print(f\"  Change: old={change.old}, new={change.new}, line='{change.line.strip()}'\")\n\n# Apply the patch (example for the first diff in the patch)\nif patches:\n    first_diff = patches[0]\n    print(\"\\n--- Applying Patch ---\")\n    \n    # Simulate reading the original file lines\n    original_lines = original_file_content.splitlines(keepends=True)\n    \n    try:\n        patched_lines = whatthepatch.apply_diff(first_diff, original_lines)\n        print(\"Patch applied successfully. New content:\")\n        print(\"\".join(patched_lines))\n    except whatthepatch.exceptions.PatchException as e:\n        print(f\"Failed to apply patch: {e}\")","lang":"python","description":"This example demonstrates how to parse a patch string and then apply the first detected diff to an original file's content. It prints the parsed changes and the resulting patched content."},"warnings":[{"fix":"Upgrade Python to 3.9+ or pin `whatthepatch` version to `<1.0.0`.","message":"Version 1.0.0 dropped support for Python 2 and Python 3.4. Users on these older Python versions must use `whatthepatch<1.0.0` or upgrade their Python environment.","severity":"breaking","affected_versions":"<1.0.0 to 1.0.0"},{"fix":"Upgrade to `whatthepatch` version 1.0.7 or newer.","message":"An incorrect regular expression matching `diffcmd` was fixed in version 1.0.7. Older versions might misinterpret diff commands in some patch files, leading to parsing errors.","severity":"gotcha","affected_versions":"<1.0.7"},{"fix":"Upgrade to `whatthepatch` version 1.0.5 or newer to ensure correct parsing of binary and context diffs.","message":"Issues with parsing binary diffs and context diffs were fixed in versions 1.0.5 and 1.0.1 respectively. Users dealing with these specific patch formats in older library versions might encounter parsing failures.","severity":"gotcha","affected_versions":"<1.0.5"},{"fix":"Be prepared to contribute pull requests for bug fixes or rely on community contributions.","message":"The project's maintainer notes limited active development; while pull requests are considered, direct issue fixes by the maintainer are not guaranteed. Users encountering bugs may need to contribute fixes.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Check relevant GitHub issues for workarounds or community-contributed solutions if encountering these specific parsing challenges.","message":"Open issues exist regarding the parser not discarding 'a/' and 'b/' prefixes in paths for certain Git patches (issue #43) and incorrect parsing of colored diffs (issue #28). These might affect specific Git patch workflows or diff outputs.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}