Skip to content

Commit 7842c44

Browse files
committed
Merge branch 'jc/apply-trailing-blank-removal' into maint
"git apply" misbehaved when fixing whitespace breakages by removing excess trailing blank lines. * jc/apply-trailing-blank-removal: apply.c:update_pre_post_images(): the preimage can be truncated
2 parents 659742f + 5de7166 commit 7842c44

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

builtin/apply.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ static void update_pre_post_images(struct image *preimage,
20952095
char *buf,
20962096
size_t len, size_t postlen)
20972097
{
2098-
int i, ctx;
2098+
int i, ctx, reduced;
20992099
char *new, *old, *fixed;
21002100
struct image fixed_preimage;
21012101

@@ -2105,8 +2105,10 @@ static void update_pre_post_images(struct image *preimage,
21052105
* free "oldlines".
21062106
*/
21072107
prepare_image(&fixed_preimage, buf, len, 1);
2108-
assert(fixed_preimage.nr == preimage->nr);
2109-
for (i = 0; i < preimage->nr; i++)
2108+
assert(postlen
2109+
? fixed_preimage.nr == preimage->nr
2110+
: fixed_preimage.nr <= preimage->nr);
2111+
for (i = 0; i < fixed_preimage.nr; i++)
21102112
fixed_preimage.line[i].flag = preimage->line[i].flag;
21112113
free(preimage->line_allocated);
21122114
*preimage = fixed_preimage;
@@ -2126,7 +2128,8 @@ static void update_pre_post_images(struct image *preimage,
21262128
else
21272129
new = old;
21282130
fixed = preimage->buf;
2129-
for (i = ctx = 0; i < postimage->nr; i++) {
2131+
2132+
for (i = reduced = ctx = 0; i < postimage->nr; i++) {
21302133
size_t len = postimage->line[i].len;
21312134
if (!(postimage->line[i].flag & LINE_COMMON)) {
21322135
/* an added line -- no counterparts in preimage */
@@ -2145,8 +2148,15 @@ static void update_pre_post_images(struct image *preimage,
21452148
fixed += preimage->line[ctx].len;
21462149
ctx++;
21472150
}
2148-
if (preimage->nr <= ctx)
2149-
die(_("oops"));
2151+
2152+
/*
2153+
* preimage is expected to run out, if the caller
2154+
* fixed addition of trailing blank lines.
2155+
*/
2156+
if (preimage->nr <= ctx) {
2157+
reduced++;
2158+
continue;
2159+
}
21502160

21512161
/* and copy it in, while fixing the line length */
21522162
len = preimage->line[ctx].len;
@@ -2159,6 +2169,7 @@ static void update_pre_post_images(struct image *preimage,
21592169

21602170
/* Fix the length of the whole thing */
21612171
postimage->len = new - postimage->buf;
2172+
postimage->nr -= reduced;
21622173
}
21632174

21642175
static int match_fragment(struct image *img,

0 commit comments

Comments
 (0)