Skip to content

Commit 0c3ef98

Browse files
bjornggitster
authored andcommitted
apply: Allow blank *trailing* context lines to match beyond EOF
In 5166714, "git apply --whitespace=fix" was extended to allow a blank context line to match beyond the end of the file, but only if the context line was in the leading part of the hunk (i.e. the hunk inserted additional contents at the end of the file). Drop the restriction that the context line must be in the leading part of the hunk, thus allowing a file to be changed from: a (blank line) to: b a (blank line) Note that the blank line will be kept, because "--whitespace=fix" only removes trailing blank lines that a hunk would add, never trailing blank lines in the context. Signed-off-by: Björn Gustavsson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 59f5ced commit 0c3ef98

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

builtin-apply.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,13 +1864,13 @@ static int match_fragment(struct image *img,
18641864
if (match_end && (preimage->nr + try_lno != img->nr))
18651865
return 0;
18661866
} else if (ws_error_action == correct_ws_error &&
1867-
(ws_rule & WS_BLANK_AT_EOF) && match_end) {
1867+
(ws_rule & WS_BLANK_AT_EOF)) {
18681868
/*
1869-
* This hunk that matches at the end extends beyond
1870-
* the end of img, and we are removing blank lines
1871-
* at the end of the file. This many lines from the
1872-
* beginning of the preimage must match with img, and
1873-
* the remainder of the preimage must be blank.
1869+
* This hunk extends beyond the end of img, and we are
1870+
* removing blank lines at the end of the file. This
1871+
* many lines from the beginning of the preimage must
1872+
* match with img, and the remainder of the preimage
1873+
* must be blank.
18741874
*/
18751875
preimage_limit = img->nr - try_lno;
18761876
} else {

t/t4124-apply-ws-rule.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,18 @@ test_expect_success 'two missing blank lines at end with --whitespace=fix' '
325325
test_cmp one expect
326326
'
327327

328+
test_expect_success 'missing blank line at end, insert before end, --whitespace=fix' '
329+
{ echo a; echo; } >one &&
330+
git add one &&
331+
{ echo b; echo a; echo; } >one &&
332+
cp one expect &&
333+
git diff -- one >patch &&
334+
echo a >one &&
335+
test_must_fail git apply patch &&
336+
git apply --whitespace=fix patch &&
337+
test_cmp one expect
338+
'
339+
328340
test_expect_success 'shrink file with tons of missing blanks at end of file' '
329341
{ echo a; echo b; echo c; } >one &&
330342
cp one no-blank-lines &&

0 commit comments

Comments
 (0)