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
Summary
patchapplies unified diff patches to files. Combined with the existingdiffbuiltin, this enables full diff-based code modification workflows — particularly valuable for AI agents that generate and apply code changes.Requested Flags
-p NUMNUMleading path components from filenames-R-i file--dry-run-N-b.origbackup files--fuzz=NUMNUMlines of fuzz for context matchingUse Cases
Implementation Notes
---/+++/@@headers,+/-/lines)