Skip to content

Commit 2124745

Browse files
committed
Merge branch 'jc/apply-ws-fix-tab-in-indent' into maint
* jc/apply-ws-fix-tab-in-indent: test: resurrect q_to_tab apply --whitespace=fix: avoid running over the postimage buffer
2 parents 2c697a6 + 329b26e commit 2124745

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

builtin/apply.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,10 +2117,10 @@ static void update_pre_post_images(struct image *preimage,
21172117

21182118
/*
21192119
* Adjust the common context lines in postimage. This can be
2120-
* done in-place when we are just doing whitespace fixing,
2121-
* which does not make the string grow, but needs a new buffer
2122-
* when ignoring whitespace causes the update, since in this case
2123-
* we could have e.g. tabs converted to multiple spaces.
2120+
* done in-place when we are shrinking it with whitespace
2121+
* fixing, but needs a new buffer when ignoring whitespace or
2122+
* expanding leading tabs to spaces.
2123+
*
21242124
* We trust the caller to tell us if the update can be done
21252125
* in place (postlen==0) or not.
21262126
*/
@@ -2185,7 +2185,7 @@ static int match_fragment(struct image *img,
21852185
int i;
21862186
char *fixed_buf, *buf, *orig, *target;
21872187
struct strbuf fixed;
2188-
size_t fixed_len;
2188+
size_t fixed_len, postlen;
21892189
int preimage_limit;
21902190

21912191
if (preimage->nr + try_lno <= img->nr) {
@@ -2335,6 +2335,7 @@ static int match_fragment(struct image *img,
23352335
strbuf_init(&fixed, preimage->len + 1);
23362336
orig = preimage->buf;
23372337
target = img->buf + try;
2338+
postlen = 0;
23382339
for (i = 0; i < preimage_limit; i++) {
23392340
size_t oldlen = preimage->line[i].len;
23402341
size_t tgtlen = img->line[try_lno + i].len;
@@ -2362,6 +2363,7 @@ static int match_fragment(struct image *img,
23622363
match = (tgtfix.len == fixed.len - fixstart &&
23632364
!memcmp(tgtfix.buf, fixed.buf + fixstart,
23642365
fixed.len - fixstart));
2366+
postlen += tgtfix.len;
23652367

23662368
strbuf_release(&tgtfix);
23672369
if (!match)
@@ -2399,8 +2401,10 @@ static int match_fragment(struct image *img,
23992401
* hunk match. Update the context lines in the postimage.
24002402
*/
24012403
fixed_buf = strbuf_detach(&fixed, &fixed_len);
2404+
if (postlen < postimage->len)
2405+
postlen = 0;
24022406
update_pre_post_images(preimage, postimage,
2403-
fixed_buf, fixed_len, 0);
2407+
fixed_buf, fixed_len, postlen);
24042408
return 1;
24052409

24062410
unmatch_exit:

t/t4124-apply-ws-rule.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,4 +486,30 @@ test_expect_success 'same, but with CR-LF line endings && cr-at-eol unset' '
486486
test_cmp one expect
487487
'
488488

489+
test_expect_success 'whitespace=fix to expand' '
490+
qz_to_tab_space >preimage <<-\EOF &&
491+
QQa
492+
QQb
493+
QQc
494+
ZZZZZZZZZZZZZZZZd
495+
QQe
496+
QQf
497+
QQg
498+
EOF
499+
qz_to_tab_space >patch <<-\EOF &&
500+
diff --git a/preimage b/preimage
501+
--- a/preimage
502+
+++ b/preimage
503+
@@ -1,7 +1,6 @@
504+
QQa
505+
QQb
506+
QQc
507+
-QQd
508+
QQe
509+
QQf
510+
QQg
511+
EOF
512+
git -c core.whitespace=tab-in-indent apply --whitespace=fix patch
513+
'
514+
489515
test_done

t/t4150-am.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test_expect_success 'setup: messages' '
1717
vero eos et accusam et justo duo dolores et ea rebum.
1818
1919
EOF
20-
q_to_tab <<-\EOF >>msg &&
20+
qz_to_tab_space <<-\EOF >>msg &&
2121
QDuis autem vel eum iriure dolor in hendrerit in vulputate velit
2222
Qesse molestie consequat, vel illum dolore eu feugiat nulla facilisis
2323
Qat vero eros et accumsan et iusto odio dignissim qui blandit

t/test-lib-functions.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ q_to_tab () {
9191
tr Q '\011'
9292
}
9393

94+
qz_to_tab_space () {
95+
tr QZ '\011\040'
96+
}
97+
9498
append_cr () {
9599
sed -e 's/$/Q/' | tr Q '\015'
96100
}

0 commit comments

Comments
 (0)