Unified diff checker: fix hunk counts and apply a patch

Paste a unified diff to check its hunk headers and line prefixes, get a corrected diff and apply it to your file. Nothing is uploaded.

How it works

  1. Paste the unified diff in the first box
  2. Paste the original file in the second box to apply the patch, or leave it empty to only check
  3. Copy the corrected diff or the patched text, and read the problems table

What the checker verifies

To fix a patch that says malformed or corrupt, paste the diff and the page counts the lines of every hunk and compares them with the numbers in its @@ -a,b +c,d @@ header. Worked example: the sample has a header of -1,6 +1,8 but the body has 6 old lines (5 context and 1 removed) and 7 new lines (5 context and 2 added). The table reports an error on line 3 and the corrected diff has @@ -1,6 +1,7 @@. A count of 1 may be left out of a header, as in @@ -3 +3 @@, and the page reads that as 1. Context lines start with a space, removed lines with a minus sign, added lines with a plus sign, and a backslash line marks a missing newline at the end of a file.

Other problems it reports

A line inside a hunk that starts with anything else is an error, and the corrected diff treats it as a context line by adding a leading space. A completely empty line inside a hunk is a warning, because editors and chat windows often strip the single space of a blank context line; it is read as a blank context line. Hunks that overlap or run out of order, a hunk with no file header lines, a --- line with no +++ line, a malformed @@ line, a hunk with no lines, and a new-side start line that does not follow from the hunks before it are all listed with their line numbers. Mixed CRLF and LF endings inside the hunks are reported and left as they are, since a diff of a CRLF file can legitimately contain both.

Why AI-written diffs often have wrong counts

A language model writes text from left to right, so it has to state the counts in the @@ line before it writes the lines those counts describe. It does not count back, and a line added or dropped while writing the body leaves the header wrong. Chat windows and editors can also remove the trailing space from a blank context line, or an extra line of context can slip in. This page recounts from the body, which is usually right when the body is right. It cannot know what a wrong line was meant to say, so the body still needs reading.

Applying the patch

With the original file pasted, the page applies each hunk by matching its context and removed lines exactly with the file, starting at the line number in the header and using the corrected counts. The search distance option lets it look that many lines above and below, up to 200, as the patch tool does when line numbers have drifted. If a hunk still does not match, you get the first failing hunk with the line the diff expected and the line the file has, and a hint when the context exists further away. The patch is applied to the first file of the diff, a missing newline marker is honoured, and a CRLF original keeps CRLF. Nothing is run or uploaded; the text stays in this tab.

Frequently asked questions

How do I fix a hunk header with the wrong line counts?

Paste the diff and copy the corrected diff. The page counts context plus removed lines for the old side and context plus added lines for the new side, then rewrites the header. In the sample, +1,8 becomes +1,7 because the body has 7 new lines.

Why does git say corrupt patch or patch does not apply?

A common cause is a hunk header whose counts do not match its body, as in a diff written by hand or by a model. Other causes are a missing leading space on a blank context line and context that no longer matches the file. The problems table names the line of each.

What does the search distance do?

It lets the page look for a hunk's context up to that many lines above or below the position in the header, from 0 to 200. Use it when the file has changed a little since the diff was made. At 0 the context must sit exactly where the header says.

Why is an empty line inside a hunk only a warning?

A blank context line should be a single space, but many tools strip trailing spaces, leaving an empty line. The page reads it as a blank context line and the corrected diff puts the space back. If the line was not meant as context, the counts will then disagree and you will see an error.

Can it apply a diff that touches several files?

It checks every file, but it applies the diff to one original and so uses the first file in the diff. A note in the table says when more files were present. Paste each file's diff separately to apply them one by one.

Is my diff or file uploaded?

No. The diff and the file are processed in your browser and not uploaded. Only the search distance setting is saved in this browser, never the text.