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

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

Reply via email to