Code

apply: Allow blank *trailing* context lines to match beyond EOF
authorBjörn Gustavsson <bgustavsson@gmail.com>
Thu, 8 Apr 2010 04:14:31 +0000 (06:14 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 8 Apr 2010 06:46:23 +0000 (23:46 -0700)
In 51667147be, "git apply --whitespace=fix" was extended to
allow a blank context line to match beyond the end of the file,
but only if the context line was in the leading part of the
hunk (i.e. the hunk inserted additional contents at the end
of the file).

Drop the restriction that the context line must be in the
leading part of the hunk, thus allowing a file to be changed
from:

 a
 (blank line)

to:

 b
 a
 (blank line)

Note that the blank line will be kept, because "--whitespace=fix"
only removes trailing blank lines that a hunk would add, never
trailing blank lines in the context.

Signed-off-by: Björn Gustavsson <bgustavsson@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-apply.c
t/t4124-apply-ws-rule.sh

index 7ca90472c10eed8535e76b1e0ec25c92af7cff05..d17e0468955072d6b1bdae4775fe2e263a52d531 100644 (file)
@@ -1864,13 +1864,13 @@ static int match_fragment(struct image *img,
                if (match_end && (preimage->nr + try_lno != img->nr))
                        return 0;
        } else if (ws_error_action == correct_ws_error &&
-                  (ws_rule & WS_BLANK_AT_EOF) && match_end) {
+                  (ws_rule & WS_BLANK_AT_EOF)) {
                /*
-                * This hunk that matches at the end extends beyond
-                * the end of img, and we are removing blank lines
-                * at the end of the file.  This many lines from the
-                * beginning of the preimage must match with img, and
-                * the remainder of the preimage must be blank.
+                * This hunk extends beyond the end of img, and we are
+                * removing blank lines at the end of the file.  This
+                * many lines from the beginning of the preimage must
+                * match with img, and the remainder of the preimage
+                * must be blank.
                 */
                preimage_limit = img->nr - try_lno;
        } else {
index fb9ad247bf76c07a8b6dbbb0d6bf1ab830041770..451d75e3fb2ea3efb17825f6af92fa3213c620aa 100755 (executable)
@@ -325,6 +325,18 @@ test_expect_success 'two missing blank lines at end with --whitespace=fix' '
        test_cmp one expect
 '
 
+test_expect_success 'missing blank line at end, insert before end, --whitespace=fix' '
+       { echo a; echo; } >one &&
+       git add one &&
+       { echo b; echo a; echo; } >one &&
+       cp one expect &&
+       git diff -- one >patch &&
+       echo a >one &&
+       test_must_fail git apply patch &&
+       git apply --whitespace=fix patch &&
+       test_cmp one expect
+'
+
 test_expect_success 'shrink file with tons of missing blanks at end of file' '
        { echo a; echo b; echo c; } >one &&
        cp one no-blank-lines &&