DevTools Logo

Code Diff & Patch Generator

Code Diff & Patch Generator

Create and apply unified diffs locally in your browser, with strict context validation.

Original text

The source file before changes.

Changed text

The desired file after changes.

Examples

One-line change keeps line numbers

Input
old: "a\nb\nc\n" → new: "a\nB\nc\n"
Output
@@ -1,3 +1,3 @@
 a
-B
+B
 c

Three old and three new lines are reported with 1-based starts; the applier maps them back with offset tracking.

New file from empty

Input
generateUnifiedDiff("", "hello\nworld\n")
Output
--- /dev/null
+++ b/file
@@ -0,0 +1,2 @@
+hello
+world

An empty old side renders --- /dev/null with a -0,0 range, and applying it to "" reproduces the new file.

About this tool

Code Diff & Patch Generator computes a deterministic longest-common-subsequence diff between two texts with generateUnifiedDiff and emits standard unified hunks with @@ -old,count +new,count @@ headers. Empty inputs get /dev/null file headers (--- /dev/null for an empty old file, +++ /dev/null for an empty new one), zero counts render as -0,0, and identical files produce an empty string rather than an empty hunk. The contextLines option (default 3, floored at 0) controls how many unchanged lines surround each change, and nearby changes merge into one hunk.

The apply side validates before mutating: applyUnifiedDiff walks each hunk in order with offset tracking, checking every context (space) line and every removal (-) line against the source. A tampered line throws Patch context mismatch at line N or Patch removal mismatch at line N, and a patch with no parseable hunks throws Invalid unified diff: no hunks found — nothing is partially applied. Additions (+) never need to match, so only the old-file side constrains the apply.

Line-ending details are preserved faithfully: inputs are split on normalized newlines, a missing trailing newline is recorded with a \ No newline at end of file marker, and the result rejoins with the correct trailing newline. isUnifiedDiff detects the --- / +++ / @@ shape for routing pasted text to the apply path. All computation is client-side LCS over line arrays.

How to use this tool

  1. Compare two texts

    Paste the original and changed text. The unified diff updates with contextLines surrounding each change and line-numbered @@ headers.

  2. Transfer the patch

    Copy the diff or paste any compatible unified patch into the apply tab; isUnifiedDiff recognizes the ---/+++/@@ shape.

  3. Apply against the exact original

    Call applyUnifiedDiff(original, patch). Context or removal mismatches throw instead of producing a silently wrong file.

Use cases

Reviewing generated changes

Inspect a compact unified diff with hunk headers before committing or sharing a change.

Applying offline patches

Validate and apply a patch in the browser with mismatch rejection and no upload.

Scaffolding new files

Diff against empty to produce /dev/null patches that create files when applied.

Common mistakes

Mistake:Applying a patch to a similar but different file.

Fix:The applier intentionally throws on context/removal mismatches. Start from the exact old text the diff was generated against.

Mistake:Treating an empty patch as an error.

Fix:An empty string means the files are identical — there is nothing to apply. Only non-empty text without hunks throws Invalid unified diff.

Frequently asked questions

What does this tool do?

It diffs two texts with generateUnifiedDiff into standard @@ -old,count +new,count @@ hunks (empty sides use /dev/null headers, identical inputs yield an empty string) and reapplies patches with applyUnifiedDiff, which validates every context and removal line with offset tracking.

Does apply(diff(a,b),a) always equal b, and what do headers mean?

Yes for generated diffs — e.g. a→B in a/b/c yields @@ -1,3 +1,3 @@ and applying it returns the new text. A file created from empty shows --- /dev/null with @@ -0,0 +1,2 @@; corrupted context throws Patch context/removal mismatch instead of misapplying.

Is my code uploaded or stored?

No. The LCS diff, hunk building, and patch application run entirely in your browser — source text never leaves your device.

Why did my patch fail to apply?

Every space and - line must match the source exactly; a changed line throws a mismatch error and nothing is partially applied. Re-generate the diff against the exact original, and treat an empty patch as identical files rather than an error.

References & standards

.env Secret Safety Linter

Validate .env syntax and spot likely leaked credentials or placeholder secrets.

.htaccess Generator

Build an Apache .htaccess — HTTPS redirects, rewrites, error pages, security headers, and basic auth

2D Fluid Dynamics & Particle Physics Sandbox

A physics playground with two engines sharing one canvas. The fluid mode runs a real Eulerian Navier-Stokes solver — semi-Lagrangian advection, explicit diffusion, Jacobi pressure projection and vorticity confinement — that you paint with dye and velocity using mouse or touch. The particle mode is a damped Verlet N-body engine with gravity wells, Coulomb attractors and repulsors, spring links, wall restitution and elastic circle collisions. Particle counts adapt to the device (100k target on WebGL2 desktop, 2k-5k on CPU fallback), and every configuration exports as runnable code: a Matter.js world, a PixiJS ticker setup, or a standalone vanilla Canvas engine.

AI Function Calling Schema Generator

Generate function calling JSON schemas from TypeScript interfaces

Algorithm Arena & Data Structure Visualizer

Interactive arena for algorithms and data structures: eight sorting algorithms racing on one seeded input, six pathfinding algorithms walking generated mazes, and BST, AVL, Red-Black, B-tree, heap and trie operations replayed step by step with rotations highlighted. Every run shows the Big-O table and the phase-highlighted reference implementation in four languages, and compared values can be played as pitch (sonification) through the shared audio engine. Everything runs client-side; inputs never leave the browser.

Ansible Playbook Generator

Build an Ansible playbook — hosts, vars, and tasks with modules — as YAML