Skip to content

Commit

Permalink
apply --whitespace=fix: detect new blank lines at eof correctly
Browse files Browse the repository at this point in the history
The command tries to strip blank lines at the end of the file added by a
patch.  It is done by first detecting if a hunk in patch has additional
blank lines at the end of itself, and if so checking if such a hunk
applies at the end of file.  This patch addresses a bug in the logic to
implement the former (the previous one addressed a bug in the latter).

If the original ends with blank lines, often the patch hunk ends like
this:

    @@ -l,5 +m,7 @@$
    _context$
    _context$
    -deleted$
    +$
    +$
    +$
    _$
    _$

where _ stands for SP and $ shows a end-of-line.  This example patch adds
three trailing blank lines, but the code fails to notice it, because it
only pays attention to added blank lines at the very end of the hunk.  In
this example, the three added blank lines do not appear textually at the
end in the patch, even though you can see that they are indeed added at
the end, if you rearrange the diff like this:

    @@ -l,5 +m,7 @@$
    _context$
    _context$
    -deleted$
    _$
    _$
    +$
    +$
    +$

The fix is not to reset the number of (candidate) added blank lines at the
end when the loop sees a context line that is empty.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Junio C Hamano committed Sep 4, 2009
1 parent ef2035c commit efa5744
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
int len = linelen(patch, size);
int plen, added;
int added_blank_line = 0;
int is_blank_context = 0;

if (!len)
break;
Expand Down Expand Up @@ -1945,8 +1946,11 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
*new++ = '\n';
add_line_info(&preimage, "\n", 1, LINE_COMMON);
add_line_info(&postimage, "\n", 1, LINE_COMMON);
is_blank_context = 1;
break;
case ' ':
if (plen && patch[1] == '\n')
is_blank_context = 1;
case '-':
memcpy(old, patch + 1, plen);
add_line_info(&preimage, old, plen,
Expand Down Expand Up @@ -1986,6 +1990,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
}
if (added_blank_line)
new_blank_lines_at_end++;
else if (is_blank_context)
;
else
new_blank_lines_at_end = 0;
patch += len;
Expand Down
24 changes: 24 additions & 0 deletions t/t4124-apply-ws-rule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,28 @@ test_expect_success 'blank at EOF with --whitespace=fix (2)' '
test_cmp expect one
'

test_expect_success 'blank at EOF with --whitespace=fix (3)' '
{ echo a; echo b; echo; } >one &&
git add one &&
{ echo a; echo c; echo; } >expect &&
{ cat expect; echo; echo; } >one &&
git diff -- one >patch &&
git checkout one &&
git apply --whitespace=fix patch &&
test_cmp expect one
'

test_expect_success 'blank at end of hunk, not at EOF with --whitespace=fix' '
{ echo a; echo b; echo; echo; echo; echo; echo; echo d; } >one &&
git add one &&
{ echo a; echo c; echo; echo; echo; echo; echo; echo; echo d; } >expect &&
cp expect one &&
git diff -- one >patch &&
git checkout one &&
git apply --whitespace=fix patch &&
test_cmp expect one
'

test_done

0 comments on commit efa5744

Please sign in to comment.