1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-11-13 20:53:02 +01:00
git/t/t4125-apply-ws-fuzz.sh
Junio C Hamano c1beba5b47 git-apply --whitespace=fix: fix whitespace fuzz introduced by previous run
When you have more than one patch series, an earlier one of which
tries to introduce whitespace breakages and a later one of which
has such a new line in its context, "git-apply --whitespace=fix"
will apply and fix the whitespace breakages in the earlier one,
making the resulting file not to match the context of the later
patch.

A short demonstration is in the new test, t4125.

For example, suppose the first patch is:

    diff a/hello.txt b/hello.txt
    --- a/hello.txt
    +++ b/hello.txt
    @@ -20,3 +20,3 @@
     Hello world.$
    -How Are you$
    -Today?$
    +How are you $
    +today? $

to fix broken case in the string, but it introduces unwanted
trailing whitespaces to the result (pretend you are looking at
"cat -e" output of the patch --- '$' signs are not in the patch
but are shown to make the EOL stand out).  And the second patch
is to change the wording of the greeting further:

    diff a/hello.txt b/hello.txt
    --- a/hello.txt
    +++ b/hello.txt
    @@ -18,5 +18,5 @@
     Greetings $

    -Hello world.$
    +Hello, everybody. $
     How are you $
    -today? $
    +these days? $

If you apply the first one with --whitespace=fix, you will get
this as the result:

    Hello world.$
    How are you$
    today?$

and this does not match the preimage of the second patch, which
demands extra whitespace after "How are you" and "today?".

This series is about teaching "git apply --whitespace=fix" to
cope with this situation better.  If the patch does not apply,
it rewrites the second patch like this and retries:

    diff a/hello.txt b/hello.txt
    --- a/hello.txt
    +++ b/hello.txt
    @@ -18,5 +18,5 @@
     Greetings$

    -Hello world.$
    +Hello, everybody.$
     How are you$
    -today?$
    +these days?$

This is done by rewriting the preimage lines in the hunk
(i.e. the lines that begin with ' ' or '-'), using the same
whitespace fixing rules as it is using to apply the patches, so
that it can notice what it did to the previous ones in the
series.

A careful reader may notice that the first patch in the example
did not touch the "Greetings" line, so the trailing whitespace
that is in the original preimage of the second patch is not from
the series.  Is rewriting this context line a problem?

If you think about it, you will realize that the reason for the
difference is because the submitter's tree was based on an
earlier version of the file that had whitespaces wrong on that
"Greetings" line, and the change that introduced the "Greetings"
line was added independently of this two-patch series to our
tree already with an earlier "git apply --whitespace=fix".

So it may appear this logic is rewriting too much, it is not
so.  It is just rewriting what we would have rewritten in the
past.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-02-05 00:38:41 -08:00

103 lines
2.2 KiB
Bash
Executable file

#!/bin/sh
test_description='applying patch that has broken whitespaces in context'
. ./test-lib.sh
test_expect_success setup '
>file &&
git add file &&
# file-0 is full of whitespace breakages
for l in a bb c d eeee f ggg h
do
echo "$l "
done >file-0 &&
# patch-0 creates a whitespace broken file
cat file-0 >file &&
git diff >patch-0 &&
git add file &&
# file-1 is still full of whitespace breakages,
# but has one line updated, without fixing any
# whitespaces.
# patch-1 records that change.
sed -e "s/d/D/" file-0 >file-1 &&
cat file-1 >file &&
git diff >patch-1 &&
# patch-all is the effect of both patch-0 and patch-1
>file &&
git add file &&
cat file-1 >file &&
git diff >patch-all &&
# patch-2 is the same as patch-1 but is based
# on a version that already has whitespace fixed,
# and does not introduce whitespace breakages.
sed -e "s/ $//" patch-1 >patch-2 &&
# If all whitespace breakages are fixed the contents
# should look like file-fixed
sed -e "s/ $//" file-1 >file-fixed
'
test_expect_success nofix '
>file &&
git add file &&
# Baseline. Applying without fixing any whitespace
# breakages.
git apply --whitespace=nowarn patch-0 &&
git apply --whitespace=nowarn patch-1 &&
# The result should obviously match.
diff -u file-1 file
'
test_expect_success 'withfix (forward)' '
>file &&
git add file &&
# The first application will munge the context lines
# the second patch depends on. We should be able to
# adjust and still apply.
git apply --whitespace=fix patch-0 &&
git apply --whitespace=fix patch-1 &&
diff -u file-fixed file
'
test_expect_success 'withfix (backward)' '
>file &&
git add file &&
# Now we have a whitespace breakages on our side.
git apply --whitespace=nowarn patch-0 &&
# And somebody sends in a patch based on image
# with whitespace already fixed.
git apply --whitespace=fix patch-2 &&
# The result should accept the whitespace fixed
# postimage. But the line with "h" is beyond context
# horizon and left unfixed.
sed -e /h/d file-fixed >fixed-head &&
sed -e /h/d file >file-head &&
diff -u fixed-head file-head &&
sed -n -e /h/p file-fixed >fixed-tail &&
sed -n -e /h/p file >file-tail &&
! diff -u fixed-tail file-tail
'
test_done