# Text apply unified patch

`text-apply-unified-patch` · version 1.0.0 · Text · free, no key needed

Apply a one-file unified patch to text at original line addresses with exact hunk matches.

**Use when you need to: text apply unified patch · apply unified diff to text · apply a unified patch exactly.**

## Supported

- text apply unified patch
- apply unified diff to text
- apply a unified patch exactly

## Not supported

- offset or fuzz patch apply
- preserve original CR/LF through unified text
- git diff --git headers
- binary patches
- fuzzy matching
- filesystem access

## Behavior

- unified is one-file unified diff text; strip is an integer 0..8 (default 0) removing that many leading path components from ---/+++ names.
- text is split into line records; only CRLF, LF, and CR are terminators.
- Hunks apply against the original record array at the parsed 0-based old_start addresses. old ranges must be sorted and disjoint; empty ranges at the same index overlap and are rejected.
- Each hunk old side must equal the original slice; there is no search, fuzz, or offset.
- Parsed body records use eol LF, or empty when a no-newline marker follows that body line. Original CR/LF is not carried through unified text.
- identical is true iff the patch contains no hunks; an empty unified string is a no-op.
- Output text is joinRecords of the patched records and must fit the output byte budget.

## Input

- `text` (string, required): max length 262144
- `unified` (string, required): max length 262144
- `strip` (integer, optional): min 0; max 8

## Output

- `text` (string, required): max length 1048576
- `identical` (boolean, required)

## Limits

- max text bytes: 262144
- max line count: 4096
- max line bytes: 16384
- max hunks: 1024
- max path bytes: 1024
- max strip: 8
- max output bytes: 1048576

## Example

Request input:

```json
{
  "text": "a\nb\n",
  "unified": "--- a\n+++ b\n@@ -1,2 +1,2 @@\n a\n-b\n+c\n"
}
```

Response:

```json
{
  "result": {
    "text": "a\nc\n",
    "identical": false
  }
}
```

## How to call it

### MCP

Connect `https://computefirst.net/mcp` ([setup](/docs#connect)), then call `execute` with:

```json
{
  "id": "text-apply-unified-patch",
  "version": "1.0.0",
  "input": {
    "text": "a\nb\n",
    "unified": "--- a\n+++ b\n@@ -1,2 +1,2 @@\n a\n-b\n+c\n"
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/text-apply-unified-patch/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"text":"a\nb\n","unified":"--- a\n+++ b\n@@ -1,2 +1,2 @@\n a\n-b\n+c\n"}'
```

The machine-readable contract is at [/v1/tools/text-apply-unified-patch/versions/1.0.0](/v1/tools/text-apply-unified-patch/versions/1.0.0).

### CLI

```sh
node cli.mjs run text-apply-unified-patch 1.0.0 --input input.json --base-url https://computefirst.net
```

Get the client at [/clients/cli/](/clients/cli/).

## Related tools

- [Text invert unified patch](/tools/text-invert-unified-patch): Invert a one-file unified patch by swapping file names and delete/insert ops.
- [Text parse unified patch](/tools/text-parse-unified-patch): Parse a one-file unified patch into path names and 0-based hunks.
- [Text unified diff](/tools/text-unified-diff): Emit a one-file unified diff of two texts with comma counts and LF document breaks.
- [Text diff stats](/tools/text-diff-stats): Summarize a line-record diff as keep, insert, delete, and hunk counts.
- [Text line diff](/tools/text-line-diff): Diff two texts as line records, preserving each line terminator.
- [Text line endings normalize](/tools/text-line-endings-normalize): Normalize all line endings in a text to a specific format (LF, CRLF, or CR).
