git-apply: Fix removal of new trailing blank lines.
The earlier code removed one newline too many from the hunk that adds new lines at the end of the file. Also the way the code counted the added blank lines was somewhat roundabout; I think the way updated code does it is more direct and easier to follow: * We keep track of the number of blank lines added; * While processing each line, we notice if it adds a blank line, and increment the counter, or reset it to zero otherwise; * When actually we apply the data, we remove the empty lines we counted earlier if we are applying it at the end of the file. Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
parent
efe7f35861
commit
077e1af598
@ -1671,7 +1671,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
|
|||||||
char *new = xmalloc(size);
|
char *new = xmalloc(size);
|
||||||
const char *oldlines, *newlines;
|
const char *oldlines, *newlines;
|
||||||
int oldsize = 0, newsize = 0;
|
int oldsize = 0, newsize = 0;
|
||||||
int trailing_added_lines = 0;
|
int new_blank_lines_at_end = 0;
|
||||||
unsigned long leading, trailing;
|
unsigned long leading, trailing;
|
||||||
int pos, lines;
|
int pos, lines;
|
||||||
|
|
||||||
@ -1679,6 +1679,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
|
|||||||
char first;
|
char first;
|
||||||
int len = linelen(patch, size);
|
int len = linelen(patch, size);
|
||||||
int plen;
|
int plen;
|
||||||
|
int added_blank_line = 0;
|
||||||
|
|
||||||
if (!len)
|
if (!len)
|
||||||
break;
|
break;
|
||||||
@ -1700,16 +1701,6 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
|
|||||||
else if (first == '+')
|
else if (first == '+')
|
||||||
first = '-';
|
first = '-';
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
* Count lines added at the end of the file.
|
|
||||||
* This is not enough to get things right in case of
|
|
||||||
* patches generated with --unified=0, but it's a
|
|
||||||
* useful upper bound.
|
|
||||||
*/
|
|
||||||
if (first == '+')
|
|
||||||
trailing_added_lines++;
|
|
||||||
else
|
|
||||||
trailing_added_lines = 0;
|
|
||||||
|
|
||||||
switch (first) {
|
switch (first) {
|
||||||
case '\n':
|
case '\n':
|
||||||
@ -1728,9 +1719,14 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
|
|||||||
break;
|
break;
|
||||||
/* Fall-through for ' ' */
|
/* Fall-through for ' ' */
|
||||||
case '+':
|
case '+':
|
||||||
if (first != '+' || !no_add)
|
if (first != '+' || !no_add) {
|
||||||
newsize += apply_line(new + newsize, patch,
|
int added = apply_line(new + newsize, patch,
|
||||||
plen);
|
plen);
|
||||||
|
newsize += added;
|
||||||
|
if (first == '+' &&
|
||||||
|
added == 1 && new[newsize-1] == '\n')
|
||||||
|
added_blank_line = 1;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case '@': case '\\':
|
case '@': case '\\':
|
||||||
/* Ignore it, we already handled it */
|
/* Ignore it, we already handled it */
|
||||||
@ -1740,6 +1736,10 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
|
|||||||
error("invalid start of line: '%c'", first);
|
error("invalid start of line: '%c'", first);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
if (added_blank_line)
|
||||||
|
new_blank_lines_at_end++;
|
||||||
|
else
|
||||||
|
new_blank_lines_at_end = 0;
|
||||||
patch += len;
|
patch += len;
|
||||||
size -= len;
|
size -= len;
|
||||||
}
|
}
|
||||||
@ -1750,24 +1750,6 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
|
|||||||
newsize--;
|
newsize--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (new_whitespace == strip_whitespace) {
|
|
||||||
/* Any added empty lines is already cleaned-up here
|
|
||||||
* becuase of 'strip_whitespace' flag, so just count '\n'
|
|
||||||
*/
|
|
||||||
int empty = 0;
|
|
||||||
while ( empty < trailing_added_lines
|
|
||||||
&& newsize - empty > 0
|
|
||||||
&& new[newsize - empty - 1] == '\n')
|
|
||||||
empty++;
|
|
||||||
|
|
||||||
if (empty < trailing_added_lines)
|
|
||||||
empty--;
|
|
||||||
|
|
||||||
/* these are the empty lines added at
|
|
||||||
* the end of the file, modulo u0 patches.
|
|
||||||
*/
|
|
||||||
trailing_added_lines = empty;
|
|
||||||
}
|
|
||||||
oldlines = old;
|
oldlines = old;
|
||||||
newlines = new;
|
newlines = new;
|
||||||
leading = frag->leading;
|
leading = frag->leading;
|
||||||
@ -1805,7 +1787,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
|
|||||||
|
|
||||||
if (new_whitespace == strip_whitespace &&
|
if (new_whitespace == strip_whitespace &&
|
||||||
(desc->size - oldsize - offset == 0)) /* end of file? */
|
(desc->size - oldsize - offset == 0)) /* end of file? */
|
||||||
newsize -= trailing_added_lines;
|
newsize -= new_blank_lines_at_end;
|
||||||
|
|
||||||
diff = newsize - oldsize;
|
diff = newsize - oldsize;
|
||||||
size = desc->size + diff;
|
size = desc->size + diff;
|
||||||
|
Loading…
Reference in New Issue
Block a user