Apply a patch

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

Input

The original text to change.
A unified diff for one file.

Result

Fill in the input and press Run.

Your input is processed in memory and then discarded. It is not stored or logged. Even so, don't paste live passwords or keys into any online tool.

Use it from code or an agent

curl -s https://setget.net/v1/tools/patch-apply \
  -H 'Content-Type: application/json' \
  -d '{"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"}}'
import json, urllib.request

body = {'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'}}
request = urllib.request.Request("https://setget.net/v1/tools/patch-apply", json.dumps(body).encode(),
                                 {"Content-Type": "application/json"})
print(json.load(urllib.request.urlopen(request))["result"])
const response = await fetch("https://setget.net/v1/tools/patch-apply", {
  method: "POST",
  headers: {"Content-Type": "application/json"},
  body: JSON.stringify({"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"}}),
});
console.log((await response.json()).result);
Tool: run_tool
Arguments:
{
  "slug": "patch-apply",
  "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"
  }
}
{
  "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"
  }
}

Endpoint: POST /v1/tools/patch-apply. Over MCP: run_tool with slug: "patch-apply". Machine-readable: input schema, Markdown. Connect an agent.

Inputs

NameTypeRequiredWhat it does
text string yes The original text to change.
patch string yes A unified diff for one file.

Examples

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
{
  "text": "apple\nblueberry\ncherry\ndate\n",
  "hunks": 1,
  "added": 2,
  "removed": 1
}

A patch without file headers

Input
{
  "text": "one\ntwo\n",
  "patch": "@@ -2 +2 @@\n-two\n+2\n"
}
Result
{
  "text": "one\n2\n",
  "hunks": 1,
  "added": 1,
  "removed": 1
}

Questions

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.