[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-patch] is this a bug?
From: |
Andreas Grünbacher |
Subject: |
Re: [bug-patch] is this a bug? |
Date: |
Thu, 31 Jan 2019 12:04:54 +0100 |
Am Do., 31. Jan. 2019 um 10:46 Uhr schrieb Steve Summit <address@hidden>:
>
> Consider these two little test files. (If they don't show up
> well in your email client, see the attached reproduction script.)
>
> tf1: tf2:
> b b
> c c
> d d
> e
>
> An ordinary context diff with one line of context is:
>
> *** tf1 2019-01-30 22:18:30.000000000 -0500
> --- tf2 2019-01-30 22:18:31.000000000 -0500
> ***************
> *** 3 ****
> --- 3,4 ----
> d
> + e
>
> Now, consider this altered version of the first file:
>
> tf3:
> a
> b
> c
> d
> z
>
> Applying the patch, however, yields:
>
> a
> b
> c
> e
> d
> z
>
> Notice that line e is inserted before line d, not after it.
> (I'll grant that one line of context isn't much, but it seems to
> convey unambiguously that new line e is to follow existing line d.)
>
> The issue seems to have to do with the extra lines at both the
> beginning and end of tf3. If either of those extras is removed,
> the patch appends line e after line d, as expected.
>
> I tested this with patch 2.7.6.
>
> Reproduction script follows. (Run it in an empty directory.)
>
> ------8<--------8<--------8<--------8<--------8<--------8<------
>
> cat > tf1 <<%EOF%
> b
> c
> d
> %EOF%
>
> cp tf1 tf2
> echo e >> tf2
>
> diff -C 1 tf1 tf2 > tfp
>
> echo a > tf3
> cat tf1 >> tf3
> echo z >> tf3
>
> cp tf3 tf4
> patch tf4 < tfp
I'm getting the following message from patch in this step:
Hunk #1 succeeded at 3 with fuzz 1.
So the patch could only be applied by ignoring one line of context.
This may seem surprising, but patch assumes that hunks that have more
context at the beginning than at the end must apply at the end of the
file (and vice versa for the beginning of the file), so it refuses to
apply the patch as is. After stripping the context line, the hunk
becomes symmetrical, and patch will apply it anywhere in the file, but
the only information left to guide it are the line numbers.
Always be careful with patches that apply with fuzz. When applying
patches automatically, use option -F0.
> echo a > tf4.expected
> cat tf1 >> tf4.expected
> echo e >> tf4.expected
> echo z >> tf4.expected
>
> if cmp -s tf4.expected tf4
> then :
> else
> echo "Unexpected differences:"
> diff tf4.expected tf4
> fi
>
Andreas