Skip to content

Commit

Permalink
apply --whitespace=fix: fix handling of blank lines at the eof
Browse files Browse the repository at this point in the history
b94f2ed (builtin-apply.c: make it more line oriented, 2008-01-26) broke
the logic used to detect if a hunk adds blank lines at the end of the
file.  With the new code after that commit:

 - img holds the contents of the file that the hunk is being applied to;

 - preimage has the lines the hunk expects to be in img; and

 - postimage has the lines the hunk wants to update the part in img that
   corresponds to preimage with.

and we need to compare if the last line of preimage (not postimage)
matches the last line of img to see if the hunk applies at the end of the
file.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Junio C Hamano committed Sep 4, 2009
1 parent 82d97da commit ef2035c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -2069,7 +2069,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
if (applied_pos >= 0) {
if (ws_error_action == correct_ws_error &&
new_blank_lines_at_end &&
postimage.nr + applied_pos == img->nr) {
preimage.nr + applied_pos == img->nr) {
/*
* If the patch application adds blank lines
* at the end, and if the patch applies at the
Expand Down
29 changes: 29 additions & 0 deletions t/t4124-apply-ws-rule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,33 @@ do
done
done


test_expect_success 'blank at EOF with --whitespace=fix (1)' '
: these can fail depending on what we did before
git config --unset core.whitespace
rm -f .gitattributes
{ echo a; echo b; echo c; } >one &&
git add one &&
{ echo a; echo b; echo c; } >expect &&
{ cat expect; echo; } >one &&
git diff -- one >patch &&
git checkout one &&
git apply --whitespace=fix patch &&
test_cmp expect one
'

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

test_done

0 comments on commit ef2035c

Please sign in to comment.