Apps Part of Codeharbor

Codeharbor Diff

Compare two texts line by line and get a unified diff with added and removed lines.

More options

Try an example: ·

More about this tool

How it works

Paste an original and a changed version of a file to see which lines were added, removed or kept, grouped into hunks with context, as a unified diff like git diff or diff -u — the same comparison Codeharbor uses for snapshots and commits.

  • Both texts are split into lines (CRLF is treated as LF). Lines common to the start and end are set aside, then Myers’ O(ND) algorithm finds a shortest edit script between the rest: the fewest added plus removed lines.
  • Changes are grouped into hunks with the chosen number of unchanged context lines; hunks closer than twice the context are joined. The unified format shows “@@ -start,count +start,count @@” headers and lines starting with “-”, “+” or a space.
  • Similarity = 2 × unchanged lines ÷ (lines of both texts). With “ignore whitespace”, runs of spaces become one space and lines are trimmed before comparing.

Sources

Part of Codeharbor

How to compare two versions of a text or code file line by line

Get a unified diff of two texts with added and removed lines, hunks and a similarity score — the same diff the Codeharbor app shows for commits and snapshots.

What it is for

When a configuration file, a script or a piece of documentation changed, you want to see exactly which lines changed and nothing else. This tool compares an original and a changed text line by line and returns:

  • a unified diff in the familiar diff -u format (- for removed lines, + for added lines, a few unchanged lines around each change);
  • the counts of added, removed and unchanged lines;
  • the hunks — the blocks of changes with their line numbers in both versions;
  • a similarity percentage.

It uses the Myers algorithm, the same one behind git diff, and it is the engine the Codeharbor app uses for commits, snapshots and Teamspaces merges. The texts are only compared, never stored.

Worked example

The first example compares two versions of a deployment file. Version 1.4.0 raises the replica count from 2 to 3, changes the image tag and adds a CPU limit:

-replicas: 2
+replicas: 3
 image:
   name: registry.example.org/harbour-api
-  tag: "1.3.2"
+  tag: "1.4.0"
 resources:
   memory: 256Mi
+  cpu: 0.5

The tool answers:

3 lines added and 2 lines removed in 1 hunk; 66.7% of the lines are unchanged.

Because the changes are close together, they fall into one hunk @@ -1,7 +1,8 @@: it covers 7 lines of the original and 8 lines of the new version, starting at line 1 in both. Five lines are unchanged. The similarity counts unchanged lines on both sides against all lines on both sides: 2 × 5 / (7 + 8) = 66.7 %.

The second example re-indents a function from two to four spaces. With Ignore differences in spaces and indentation switched on, the answer is The two texts have the same lines; nothing changed. Switch it off and both indented lines show up as changed.

How to read the result

  • Added / removed — a changed line counts as one removed and one added line; that is how line diffs work.
  • Hunks — each row gives where the block starts and how many lines it spans in the original (-) and the changed (+) version.
  • Unified diff — download it as a .diff file to attach to a ticket or apply with patch.
  • Context lines — how many unchanged lines are shown around each change (default 3, as in git). Two changes closer than twice that distance share a hunk.

Tips and common mistakes

  • A final newline is not a line of its own, so a file with and without a trailing newline compares as equal here.
  • Line endings are normalised: Windows \r\n and Unix \n files compare by content.
  • Ignoring whitespace also collapses spaces inside lines, so a = 1 equals a = 1. Turn it off when spacing matters (Python, YAML, Makefiles).
  • For very large texts with changes on almost every line the diff falls back to “everything removed, everything added” instead of running for minutes. The counts stay correct.

FAQ

Is this the same as git diff?

It uses the same algorithm (Myers) and the same unified format, so the output looks familiar. It compares exactly two texts; there are no branches, renames or binary files.

Why does a changed line show up as one removed and one added line?

A line diff only knows whole lines. Codeharbor marks the changed words inside such a pair in its own diff view; the tool reports the line counts.

Can I compare whole folders?

Not with this tool — it takes two texts. Inside the Codeharbor app, commits and snapshots show the differences of every changed file in a project.

Can I apply the diff to a file?

Yes. Download the unified diff and apply it with patch or git apply in the folder that holds the original file.

Guide last updated:

For scripts and AI agents

  • JSON API: POST https://codeharbordiff.usefulweb.site/api/v1/calculate (same calculation as this page)
  • Interface description: OpenAPI 3.1 · AI guide: llms.txt
  • MCP server (Streamable HTTP): https://codeharbordiff.usefulweb.site/mcp · tool name codeharbor_diff
Example request
curl -s -X POST https://codeharbordiff.usefulweb.site/api/v1/calculate \
  -H 'content-type: application/json' \
  -d '{"original":"service: harbour-api\nreplicas: 2\nimage:\n  name: registry.example.org/harbour-api\n  tag: \"1.3.2\"\nresources:\n  memory: 256Mi\n","changed":"service: harbour-api\nreplicas: 3\nimage:\n  name: registry.example.org/harbour-api\n  tag: \"1.4.0\"\nresources:\n  memory: 256Mi\n  cpu: 0.5\n","originalName":"deploy.yaml (v1.3.2)","changedName":"deploy.yaml (v1.4.0)"}'
Feedback · ideas & bug reports welcome