Skip to content

feat(builtins): add patch command #552

@chaliy

Description

@chaliy

Summary

patch applies unified diff patches to files. Combined with the existing diff builtin, this enables full diff-based code modification workflows — particularly valuable for AI agents that generate and apply code changes.

Requested Flags

Flag Description
-p NUM Strip NUM leading path components from filenames
-R Reverse — unapply the patch
-i file Read patch from file instead of stdin
--dry-run Check if patch applies without modifying files
-N Ignore patches that appear already applied
-b Create .orig backup files
--fuzz=NUM Allow NUM lines of fuzz for context matching

Use Cases

# Apply a unified diff
diff -u old.py new.py > fix.patch
patch -p0 < fix.patch

# Apply patch with path stripping (common for git-format patches)
patch -p1 < feature.patch

# Dry run to check if patch applies cleanly
patch --dry-run -p1 < update.patch

# Reverse a patch
patch -R -p1 < applied.patch

# Agent workflow: generate diff, review, apply
diff -u original.rs modified.rs > changes.patch
cat changes.patch  # review
patch -p0 < changes.patch

Implementation Notes

  • Must parse unified diff format (---/+++/@@ headers, +/-/ lines)
  • Should work on VFS files
  • Context matching with fuzz is important for robustness
  • Reject patches for files outside VFS sandbox
  • Exit code: 0 = success, 1 = some hunks failed, 2 = serious trouble

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions