# Apply a patch

Apply a unified diff to a text and get the patched result, with clear errors when a hunk does not fit.

- Endpoint: `POST https://setget.net/v1/tools/patch-apply` with JSON `{"input": {...}}`
- MCP: `run_tool` with `slug` = `patch-apply`
- Page: https://setget.net/tools/patch-apply

## Inputs

- `text` (required, string): The original text to change.
- `patch` (required, string): A unified diff for one file.

## Example 1

```json
{"input":{"text":"apple\nbanana\ncherry\n","patch":"--- before\n+++ after\n@@ -1,3 +1,4 @@\n apple\n-banana\n+blueberry\n cherry\n+date\n"}}
```

Result:

```json
{"text":"apple\nblueberry\ncherry\ndate\n","hunks":1,"added":2,"removed":1}
```

## Example 2: A patch without file headers

```json
{"input":{"text":"one\ntwo\n","patch":"@@ -2 +2 @@\n-two\n+2\n"}}
```

Result:

```json
{"text":"one\n2\n","hunks":1,"added":1,"removed":1}
```

**What if the line numbers in the patch are slightly off?** Each hunk is first tried at the line its header names. If the text there is different, the nearest place where the hunk fits exactly is used.

**Why does a hunk not match?** The text is not the version the patch was made from, or the patch lost its leading spaces when it was copied. The error names the hunk and the first line that differs.

**Can it apply a patch for several files?** No. Apply the part for each file to that file's text.
