I have file a
:
This
file
does
have
an error
in it
that
needs
to be
fixed.
and a similar file b
:
This
file
does
have
no error
in it
that
needs
to be
fixed.
I can create a unified diff with diff -u a b
:
--- a 2018-01-03 14:20:22 +0100
+++ b 2018-01-03 14:20:37 +0100
@@ -2,7 +2,7 @@
file
does
have
-an error
+no error
in it
that
needs
And I can also reduce the context to one line on either side with diff -u1 a b
:
--- a 2018-01-03 14:20:22 +0100
+++ b 2018-01-03 14:20:37 +0100
@@ -4,3 +4,3 @@
have
-an error
+no error
in it
Both of these patches can be applied cleanly with patch
. I did not however find a way to make diff
produce a patch with asymmetric context. I am assuming it cannot do that. So I tried removing some context manually, to make a patch with two lines of context before the change and one after:
--- a 2018-01-03 14:20:22 +0100
+++ b 2018-01-03 14:20:37 +0100
@@ -3,4 +3,4 @@
does
have
-an error
+no error
in it
This seems valid to me in the unified format. However, patch
complains that it had to resort to fuzzing:
patching file a
Hunk #1 succeeded at 3 with fuzz 1.
Am I doing something wrong or is (GNU) patch
actually broken for asymmetric contexts because no-one imagined they would ever be used, since diff
cannot make them anyway?
Also interesting is that the patch works if I reverse the asymmetry, that is one line before and two after:
--- a 2018-01-03 14:20:22 +0100
+++ b 2018-01-03 14:20:37 +0100
@@ -4,4 +4,4 @@
have
-an error
+no error
in it
that
patch
command counts the number of context lines (and I suspect use the context before to count that, I am not able to prove that at 100% from the source code), so in your first case it sees 2 lines of context, but then only one after, whereas in the second example it computes one line of context, and indeed it has one line of context after (and an extra line, that is ignored because patch tries to ignore "garbage" before or after the content being patched)patch
actually checks it too and needs fuzzing if it does not match.