# Text parse unified patch

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

Parse a one-file unified patch into path names and 0-based hunks.

**Use when you need to: text parse unified patch · parse unified diff · parse unified patch headers and hunks.**

## Supported

- text parse unified patch
- parse unified diff
- parse unified patch headers and hunks

## Not supported

- preserve original CR/LF through unified text
- git diff --git headers
- index/binary markers
- multi-file patches
- offset/fuzz patch apply
- RFC 6902
- filesystem access

## Behavior

- Empty unified yields from_name "a", to_name "b", hunks [], and identical true.
- Non-empty patches must start with --- and +++ headers. git diff --git, index, and binary markers are rejected.
- An omitted hunk count is parsed as 1. Output hunk starts are 0-based. Nonzero counts subtract one from the 1-based text start; a zero count keeps the GNU insertion index from the header.
- Body values are {text, eol} records. eol is "" iff a "\ No newline at end of file" line immediately followed that body line; otherwise eol is "\n".
- The unified document uses LF line breaks and does not round-trip original CR/LF from the source file.
- strip is an integer 0..8 (default 0) that removes that many leading slash-separated components from both paths; an empty path after strip is rejected.
- identical is true iff there are no hunks. Unknown input fields are rejected.

## Input

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

## Output

- `from_name` (string, required)
- `to_name` (string, required)
- `hunks` (array of object, required): max items 1024
- `identical` (boolean, required)

## Limits

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

## Example

Request input:

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

Response:

```json
{
  "result": {
    "from_name": "a",
    "to_name": "b",
    "hunks": [
      {
        "old_start": 0,
        "old_count": 1,
        "new_start": 0,
        "new_count": 1,
        "ops": [
          {
            "op": "delete",
            "value": {
              "text": "a",
              "eol": "\n"
            }
          },
          {
            "op": "insert",
            "value": {
              "text": "b",
              "eol": "\n"
            }
          }
        ]
      }
    ],
    "identical": false
  }
}
```

## How to call it

### MCP

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

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

### HTTP (no key)

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

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

### CLI

```sh
node cli.mjs run text-parse-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 apply unified patch](/tools/text-apply-unified-patch): Apply a one-file unified patch to text at original line addresses with exact hunk matches.
- [Text diff stats](/tools/text-diff-stats): Summarize a line-record diff as keep, insert, delete, and hunk counts.
- [Text unified diff](/tools/text-unified-diff): Emit a one-file unified diff of two texts with comma counts and LF document breaks.
- [Text conflict parse](/tools/text-conflict-parse): Parse merge or diff3 conflict-marker text into ordered text and conflict parts.
- [Text extract between](/tools/text-extract-between): Extract exact substrings between start and end markers, left to right and non-overlapping.
