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 <gitster@pobox.com>
Junio C Hamano committed
Sep 4, 2009 at 01:41 UTC
ef2035c5e55f852905b012dfb2dd242b4f77da70
2 files changed
+30
-1
builtin-apply.c
+1
-1
@@ -2069,7 +2069,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2069
if (applied_pos >= 0) {
2070
if (ws_error_action == correct_ws_error &&
2071
new_blank_lines_at_end &&
2072
- postimage.nr + applied_pos == img->nr) {
2072
+ preimage.nr + applied_pos == img->nr) {
2073
/*
2074
* If the patch application adds blank lines
2075
* at the end, and if the patch applies at the
t/t4124-apply-ws-rule.sh
+29
@@ -148,4 +148,33 @@ do
148
done
149
done
150
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
+
180
test_done