# Text unified diff

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

Emit a one-file unified diff of two texts with comma counts and LF document breaks.

**Use when you need to: text unified diff · emit unified diff · unified format text diff.**

## Supported

- text unified diff
- emit unified diff
- unified format text diff

## Not supported

- preserve original CR/LF through unified text
- git diff --git headers
- index/binary markers
- multi-file patches
- binary patches
- fuzzy matching
- filesystem access

## Behavior

- left and right are split with splitRecords; hunks use formatUnified with Myers SES and recordEqual.
- from_name and to_name default to a and b. Names must be non-empty, at most 1024 UTF-8 bytes, and must not contain NUL, CR, LF, or tab.
- context defaults to 3, may be 0, and is at most 16.
- The patch document always uses LF after every line, including headers and the final line.
- Hunk headers always include both counts with a comma, including count 1. Nonzero counts use 1-based starts; a zero count uses the GNU insertion index (0 at file start, otherwise the 1-based line before the empty range).
- Body prefixes are space (keep), minus (delete), and plus (insert). A following "\ No newline at end of file" line marks eol "".
- Identical texts yield unified "" and identical true, with no headers.
- Source CRLF is not encoded in the patch body; applying the emitted patch yields LF records except for the missing-final-newline marker.
- Unknown input fields are rejected.

## Input

- `left` (string, required): max length 262144
- `right` (string, required): max length 262144
- `from_name` (string, optional): min length 1; max length 1024
- `to_name` (string, optional): min length 1; max length 1024
- `context` (integer, optional): min 0; max 16

## Output

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

## Limits

- max text bytes: 262144
- max line count: 4096
- max line bytes: 16384
- max path bytes: 1024
- max context: 16
- max hunks: 1024
- max output bytes: 1048576

## Example

Request input:

```json
{
  "left": "a\n",
  "right": "b\n"
}
```

Response:

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

## How to call it

### MCP

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

```json
{
  "id": "text-unified-diff",
  "version": "1.0.0",
  "input": {
    "left": "a\n",
    "right": "b\n"
  }
}
```

### HTTP (no key)

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

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

### CLI

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

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

## Related tools

- [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 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 conflict render](/tools/text-conflict-render): Render text and conflict parts as merge or diff3 conflict-marker text.
- [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.
