---
name: fix-patch
description: Fix and regenerate Git patch files that fail to apply after upstream rebases. Use when the user mentions a patch failed, needs to update a patch, or asks to fix patches after pulling upstream changes.
---

# Fix Patch

Automates fixing patch files in `patches/` that fail to apply cleanly after rebasing from upstream.

## Workflow

### 1. Identify the failing patch

If the user doesn't specify which patch failed, run `git apply --check patches/*.patch` to find it. Read the failing `.patch` file from the `patches/` directory.

### 2. Understand the patch

Parse the patch to extract:
- Which files it modifies (the `diff --git a/... b/...` headers)
- What changes it makes (added/removed lines and their surrounding context)

### 3. Read the current source files

Read each file listed in the patch to see the current state after the upstream rebase.

### 4. Apply the changes manually

Edit each source file to implement the same logical changes the patch intended. The exact line numbers will differ from the patch - use the surrounding context lines in the patch hunks to locate the correct positions in the updated files.

### 5. Provide the regeneration command

After all edits are made, give the user the command to regenerate the patch. Always specify exact file paths to avoid capturing unrelated changes or the old patch itself:

```
git diff path/to/file1 path/to/file2 > patches/<patch-name>.patch
```

IMPORTANT: Do not write patch files directly. Always let the user run the `git diff` command themselves.
