9
$ vim patch
Index: toPatch
===================================================================
--- toPatch
+++ toPatch
@@ -2,4 +2,4 @@
  */
-final public class XMLWriter {
+public class XMLWriter {

$ vim toPatch
 */
final public class XMLWriter {

  public static float CURRENT_VERSION=2.2f;
    $ patch -p0 -ui patch
patching file toPatch
Hunk #1 succeeded at 1 with fuzz 2 (offset -1 lines).

Why the fuzz and the line offset? This is a demo case trying to understand diff and patch, since tools sometimes/often don't seem to work as expected.

simpatico
  • 10,709
  • 20
  • 81
  • 126

2 Answers2

18

Patch does some basic checking of consistency of the diff and your file, and if these checks fail, you get offset or fuzz.

You have offset -1, since patch expects the contents of the diff match lines 2--4 of your file. In your file, however, they are lines 1--3.

You have fuzz>0, since the first line of the context (two spaces and a */) does not match the line in the actual file (one space and a */). Because of that, patch did a second pass where it ignored the first and the last line of the context.

This doesn't explain why you see fuzz=2 and not 1. Maybe an error copy-pasting the files? Any other ideas, anybody?

xofon
  • 645
  • 4
  • 9
  • still not sure of the actual problem back then.. I just moved on – simpatico Nov 11 '11 at 19:00
  • this happened to me when copy/pasting the patch file content into my editor. Some lines had only spaces but they got trimmed to empty lines, so the patch file applied correctly, but with fuzz factors warnings... – Julien Jan 27 '16 at 15:33
  • The first line in the patch doesn't have two spaces. The first space only identifies it as a context line. The second space is part of the line. – user253751 Jul 11 '18 at 01:25
7

The indexes in your patch file (the numbers between @@) are wrong.

  • As @xofon said, you have a problem with the starting line, you have 2, but should be 1
  • But also the second number should be 3 not 4, as you have 3 lines of code in your patch file before the patch is applied and 3 lines of code in your patch file after the patch is applied.
  • but there is no issue with the first line */. There are 2 spaces in the patch file, but this is expected, as the first column is used to have - + or character. The fuzz you received was about the offset used (moving all lines up by 1)

So your patch file should be

$ cat patch
--- toPatch
+++ toPatch
@@ -1,3 +1,3 @@
  */
-final public class XMLWriter {
+public class XMLWriter {
   public static float CURRENT_VERSION=2.2f;

and with the given toPatch file

$ cat toPatch
 */
final public class XMLWriter {
  public static float CURRENT_VERSION=2.2f;

Then the patch will apply without fuzz warnings ...

$ patch -p0 -ui patch
patching file toPatch
Julien
  • 1,765
  • 20
  • 26