Skip to content

Commit efa5744

Browse files
committed
apply --whitespace=fix: detect new blank lines at eof correctly
The command tries to strip blank lines at the end of the file added by a patch. It is done by first detecting if a hunk in patch has additional blank lines at the end of itself, and if so checking if such a hunk applies at the end of file. This patch addresses a bug in the logic to implement the former (the previous one addressed a bug in the latter). If the original ends with blank lines, often the patch hunk ends like this: @@ -l,5 +m,7 @@$ _context$ _context$ -deleted$ +$ +$ +$ _$ _$ where _ stands for SP and $ shows a end-of-line. This example patch adds three trailing blank lines, but the code fails to notice it, because it only pays attention to added blank lines at the very end of the hunk. In this example, the three added blank lines do not appear textually at the end in the patch, even though you can see that they are indeed added at the end, if you rearrange the diff like this: @@ -l,5 +m,7 @@$ _context$ _context$ -deleted$ _$ _$ +$ +$ +$ The fix is not to reset the number of (candidate) added blank lines at the end when the loop sees a context line that is empty. Signed-off-by: Junio C Hamano <[email protected]>
1 parent ef2035c commit efa5744

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

builtin-apply.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
19131913
int len = linelen(patch, size);
19141914
int plen, added;
19151915
int added_blank_line = 0;
1916+
int is_blank_context = 0;
19161917

19171918
if (!len)
19181919
break;
@@ -1945,8 +1946,11 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
19451946
*new++ = '\n';
19461947
add_line_info(&preimage, "\n", 1, LINE_COMMON);
19471948
add_line_info(&postimage, "\n", 1, LINE_COMMON);
1949+
is_blank_context = 1;
19481950
break;
19491951
case ' ':
1952+
if (plen && patch[1] == '\n')
1953+
is_blank_context = 1;
19501954
case '-':
19511955
memcpy(old, patch + 1, plen);
19521956
add_line_info(&preimage, old, plen,
@@ -1986,6 +1990,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
19861990
}
19871991
if (added_blank_line)
19881992
new_blank_lines_at_end++;
1993+
else if (is_blank_context)
1994+
;
19891995
else
19901996
new_blank_lines_at_end = 0;
19911997
patch += len;

t/t4124-apply-ws-rule.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,28 @@ test_expect_success 'blank at EOF with --whitespace=fix (2)' '
177177
test_cmp expect one
178178
'
179179

180+
test_expect_success 'blank at EOF with --whitespace=fix (3)' '
181+
{ echo a; echo b; echo; } >one &&
182+
git add one &&
183+
{ echo a; echo c; echo; } >expect &&
184+
{ cat expect; echo; echo; } >one &&
185+
git diff -- one >patch &&
186+
187+
git checkout one &&
188+
git apply --whitespace=fix patch &&
189+
test_cmp expect one
190+
'
191+
192+
test_expect_success 'blank at end of hunk, not at EOF with --whitespace=fix' '
193+
{ echo a; echo b; echo; echo; echo; echo; echo; echo d; } >one &&
194+
git add one &&
195+
{ echo a; echo c; echo; echo; echo; echo; echo; echo; echo d; } >expect &&
196+
cp expect one &&
197+
git diff -- one >patch &&
198+
199+
git checkout one &&
200+
git apply --whitespace=fix patch &&
201+
test_cmp expect one
202+
'
203+
180204
test_done

0 commit comments

Comments
 (0)