{"id":2658,"library":"patch-ng","title":"patch-ng","description":"Library to parse and apply unified diffs. `patch-ng` is a fork of the original `python-patch` project, providing active maintenance and bug fixes. The current version is 1.19.0, released in October 2025. It is actively maintained by Conan-io with regular releases and is compatible with Python 3.6+.","status":"active","version":"1.19.0","language":"en","source_language":"en","source_url":"https://github.com/conan-io/python-patch-ng","tags":["patching","diff","unified diff","git","file management","version control"],"install":[{"cmd":"pip install patch-ng","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Requires Python 3.6 or higher for compatibility.","package":"python","optional":false}],"imports":[{"symbol":"PatchSet","correct":"from patch_ng import from_string, from_file, PatchSet"},{"symbol":"from_string","correct":"from patch_ng import from_string"},{"symbol":"from_file","correct":"from patch_ng import from_file"},{"note":"The library was renamed from `python-patch` to `patch-ng`. Old import statements referencing `patch` or `python_patch` will fail.","wrong":"import patch","symbol":"patch","correct":"from patch_ng import from_string, PatchSet"}],"quickstart":{"code":"import os\nimport tempfile\nimport shutil\nfrom patch_ng import from_string, PatchSet\n\n# Create a temporary directory to work in\ntemp_dir = tempfile.mkdtemp()\noriginal_file_path = os.path.join(temp_dir, \"my_file.txt\")\n\ntry:\n    # 1. Create an original file\n    with open(original_file_path, \"w\") as f:\n        f.write(\"Line 1: Hello world\\n\")\n        f.write(\"Line 2: This is a test.\\n\")\n        f.write(\"Line 3: End of file.\\n\")\n\n    print(f\"Original file '{os.path.basename(original_file_path)}' created in {temp_dir}:\\n\" \\\n          f\"{open(original_file_path, 'r').read()}\")\n\n    # 2. Define a unified diff patch string\n    # This patch changes 'Line 2' and adds 'New Line 2.5'\n    patch_content = \"\"\"--- a/my_file.txt\n+++ b/my_file.txt\n@@ -1,3 +1,4 @@\n Line 1: Hello world\n-Line 2: This is a test.\n+Line 2: This line was changed.\n+New Line 2.5: Inserted line.\n Line 3: End of file.\n\"\"\"\n\n    # 3. Parse the patch string\n    # The `strip` argument removes leading path components (e.g., 'a/' or 'b/')\n    patchset = from_string(patch_content)\n\n    if not patchset:\n        print(\"Failed to parse patch content.\")\n    else:\n        # 4. Apply the patch to the specified root directory\n        # The `root` argument is crucial to apply the patch outside the current working directory.\n        success = patchset.apply(strip=1, root=temp_dir)\n\n        if success:\n            print(f\"Patch applied successfully to files in {temp_dir}.\")\n            print(f\"\\nPatched file content of '{os.path.basename(original_file_path)}':\\n\" \\\n                  f\"{open(original_file_path, 'r').read()}\")\n        else:\n            print(\"Failed to apply patch.\")\n\nfinally:\n    # Clean up the temporary directory\n    shutil.rmtree(temp_dir)\n    print(f\"\\nCleaned up temporary directory: {temp_dir}\")","lang":"python","description":"This example demonstrates how to create a simple text file, define a unified diff patch as a string, parse it using `from_string`, and then apply the patch to the file within a temporary directory using `patchset.apply()`."},"warnings":[{"fix":"Update installation command to `pip install patch-ng` and change import statements (e.g., `import patch` to `from patch_ng import ...`).","message":"The library was renamed from `python-patch` to `patch-ng` at version 1.17. Users migrating from the old library must update their `pip install` command and `import` statements.","severity":"breaking","affected_versions":">=1.17"},{"fix":"Upgrade your Python environment to 3.6 or later.","message":"As of version 1.18.0, `patch-ng` requires Python 3.6 or newer. Older Python versions are no longer supported.","severity":"breaking","affected_versions":">=1.18.0"},{"fix":"Upgrade to `patch-ng` version 1.19.0 or later to ensure correct and robust patch application in these scenarios.","message":"Prior to version 1.19.0, `patch-ng` had known issues with applying Git patches involving file renames/moves, changes in file permissions, incorrect handling of Git full index format, and general issues with large patch files or quoted file paths. These issues could lead to incomplete or incorrect patch application.","severity":"gotcha","affected_versions":"<1.19.0"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}