问题
$ 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.
回答1:
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?
回答2:
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 yourpatch
file after the patch is applied. - but there is no issue with the first line
*/
. There are 2 spaces in thepatch
file, but this is expected, as the first column is used to have-
+
orcharacter. 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
来源:https://stackoverflow.com/questions/6215787/why-does-this-patch-applied-with-a-fuzz-of-1-and-fail-with-fuzz-of-0