Why does this patch applied with a fuzz of 1, and fail with fuzz of 0?

笑着哭i 提交于 2020-07-18 05:44:50

问题


$ 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 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


来源:https://stackoverflow.com/questions/6215787/why-does-this-patch-applied-with-a-fuzz-of-1-and-fail-with-fuzz-of-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!