Skip to content

Commit 4855de1

Browse files
rscharfegitster
authored andcommitted
apply: update line lengths for --inaccurate-eof
Some diff implementations don't report missing newlines at the end of files. Applying such a patch can cause a newline character to be added inadvertently. The option --inaccurate-eof of git apply can be used to remove trailing newlines if needed. apply_one_fragment() cuts it off from the buffers for preimage and postimage. Before it does, it builds an array with the lengths of each line for both. Make sure to update the length of the last line in these line info structures as well to keep them consistent with their respective buffer. Without this fix the added test fails; git apply dies and reports: fatal: BUG: caller miscounted postlen: asked 1, orig = 1, used = 2 That sanity check is only called if whitespace changes are ignored. Reported-by: Mahmoud Al-Qudsi <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9752ad0 commit 4855de1

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

apply.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,6 +2941,8 @@ static int apply_one_fragment(struct apply_state *state,
29412941
newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') {
29422942
old--;
29432943
strbuf_setlen(&newlines, newlines.len - 1);
2944+
preimage.line_allocated[preimage.nr - 1].len--;
2945+
postimage.line_allocated[postimage.nr - 1].len--;
29442946
}
29452947

29462948
leading = frag->leading;

t/t4107-apply-ignore-whitespace.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,18 @@ test_expect_success 'patch5 fails (--no-ignore-whitespace)' '
178178
test_must_fail git apply --no-ignore-whitespace patch5.patch
179179
'
180180

181+
test_expect_success 'apply --ignore-space-change --inaccurate-eof' '
182+
echo 1 >file &&
183+
git apply --ignore-space-change --inaccurate-eof <<-\EOF &&
184+
diff --git a/file b/file
185+
--- a/file
186+
+++ b/file
187+
@@ -1 +1 @@
188+
-1
189+
+2
190+
EOF
191+
printf 2 >expect &&
192+
test_cmp expect file
193+
'
194+
181195
test_done

0 commit comments

Comments
 (0)