Socket
Book a DemoInstallSign in
Socket

codex-apply-patch

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

codex-apply-patch

A CLI tool and Python library for applying patches using a custom patch format designed for AI coding assistants

Source
pipPyPI
Version
0.4.0
Maintainers
1

Codex Apply Patch

A CLI tool and Python library for applying patches using a custom patch format designed for AI coding assistants.

This project is based on OpenAI's original codex apply-patch tool and extends it with additional functionality.

Changes from Original

This fork adds the following enhancements to the original OpenAI tool:

  • In-Memory Patch Application: New functionality to apply patches to files in memory without touching the filesystem, useful for testing and preview scenarios.

  • Python Library Support: Complete Python bindings using PyO3, allowing the patch functionality to be used directly from Python code.

  • PyPI Distribution: The library is packaged and distributed via PyPI for easy installation.

Installation

Python Package

pip install codex-apply-patch

From Source

# Install Rust if you haven't already
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Clone and build
git clone https://github.com/openai/codex-apply-patch
cd codex-apply-patch
cargo build --release

# For Python development
pip install maturin
maturin develop --release

Usage

Command Line

# Apply patch from stdin
echo "*** Begin Patch
*** Add File: hello.txt
+Hello, World!
*** End Patch" | codex_apply_patch

Python Library

import codex_apply_patch as cap

# Apply patch to files on disk
patch = """*** Begin Patch
*** Add File: hello.py
+print("Hello, World!")
*** End Patch"""

result = cap.apply_patch(patch)
print(result)

# Apply patch in memory (new feature)
files = {
    "main.py": "def main():\n    print('old version')\n"
}

patch = """*** Begin Patch
*** Update File: main.py
@@
 def main():
-    print('old version')
+    print('new version')
*** End Patch"""

result = cap.apply_patch_in_memory(patch, files)
print("Modified files:", result.files)
print("Summary:", f"Added: {len(result.added)}, Modified: {len(result.modified)}, Deleted: {len(result.deleted)}")

# Generate patch from file contents (new feature)
original = "def hello():\n    print('old version')"
new = "def hello():\n    print('new version')\n    print('updated!')"
patch = cap.generate_patch("main.py", original, new)
print("Generated patch:")
print(patch)

# Generate patch for multiple files (new feature)
file_changes = {
    "new.py": (None, "print('new file')"),  # Add file
    "old.py": ("old content", None),        # Delete file
    "update.py": ("old", "new")             # Update file
}
multi_patch = cap.generate_patch_from_files(file_changes)
print("Multi-file patch:")
print(multi_patch)

Python API Reference

  • apply_patch(patch_str) - Apply patch to files on disk
  • apply_patch_in_memory(patch_str, files_dict) - Apply patch to in-memory files
  • parse_patch(patch_str) - Parse patch and return structure information
  • generate_patch(path, original_content, new_content) - Generate patch for a single file
  • generate_patch_from_files(files_dict) - Generate patch for multiple files
  • get_tool_instructions() - Get the CLI tool instructions for AI assistants
  • get_api_instructions() - Get the patch format instructions (without CLI specifics)

License

Licensed under the Apache License, Version 2.0. See LICENSE for details.

This project extends the original OpenAI codex apply-patch tool, which is also licensed under Apache-2.0.

Keywords

patch

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.