Skip to content

Commit ef2035c

Browse files
committed
apply --whitespace=fix: fix handling of blank lines at the eof
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 <[email protected]>
1 parent 82d97da commit ef2035c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

builtin-apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2069,7 +2069,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
20692069
if (applied_pos >= 0) {
20702070
if (ws_error_action == correct_ws_error &&
20712071
new_blank_lines_at_end &&
2072-
postimage.nr + applied_pos == img->nr) {
2072+
preimage.nr + applied_pos == img->nr) {
20732073
/*
20742074
* If the patch application adds blank lines
20752075
* at the end, and if the patch applies at the

t/t4124-apply-ws-rule.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,33 @@ do
148148
done
149149
done
150150

151+
152+
test_expect_success 'blank at EOF with --whitespace=fix (1)' '
153+
: these can fail depending on what we did before
154+
git config --unset core.whitespace
155+
rm -f .gitattributes
156+
157+
{ echo a; echo b; echo c; } >one &&
158+
git add one &&
159+
{ echo a; echo b; echo c; } >expect &&
160+
{ cat expect; echo; } >one &&
161+
git diff -- one >patch &&
162+
163+
git checkout one &&
164+
git apply --whitespace=fix patch &&
165+
test_cmp expect one
166+
'
167+
168+
test_expect_success 'blank at EOF with --whitespace=fix (2)' '
169+
{ echo a; echo b; echo c; } >one &&
170+
git add one &&
171+
{ echo a; echo c; } >expect &&
172+
{ cat expect; echo; echo; } >one &&
173+
git diff -- one >patch &&
174+
175+
git checkout one &&
176+
git apply --whitespace=fix patch &&
177+
test_cmp expect one
178+
'
179+
151180
test_done

0 commit comments

Comments
 (0)