Teach 'git-apply --whitespace=strip' to remove empty lines at the end of file
[jc: with an obvious microfix to avoid doing this unless --whitespace=strip] Signed-off-by: Marco Costalba <mcostalba@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
parent
93d496a560
commit
efe7f35861
@ -1671,6 +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;
|
||||||
unsigned long leading, trailing;
|
unsigned long leading, trailing;
|
||||||
int pos, lines;
|
int pos, lines;
|
||||||
|
|
||||||
@ -1699,6 +1700,17 @@ 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':
|
||||||
/* Newer GNU diff, empty context line */
|
/* Newer GNU diff, empty context line */
|
||||||
@ -1738,6 +1750,24 @@ 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;
|
||||||
@ -1770,9 +1800,16 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
|
|||||||
if (match_beginning && offset)
|
if (match_beginning && offset)
|
||||||
offset = -1;
|
offset = -1;
|
||||||
if (offset >= 0) {
|
if (offset >= 0) {
|
||||||
int diff = newsize - oldsize;
|
int diff;
|
||||||
unsigned long size = desc->size + diff;
|
unsigned long size, alloc;
|
||||||
unsigned long alloc = desc->alloc;
|
|
||||||
|
if (new_whitespace == strip_whitespace &&
|
||||||
|
(desc->size - oldsize - offset == 0)) /* end of file? */
|
||||||
|
newsize -= trailing_added_lines;
|
||||||
|
|
||||||
|
diff = newsize - oldsize;
|
||||||
|
size = desc->size + diff;
|
||||||
|
alloc = desc->alloc;
|
||||||
|
|
||||||
/* Warn if it was necessary to reduce the number
|
/* Warn if it was necessary to reduce the number
|
||||||
* of context lines.
|
* of context lines.
|
||||||
|
Loading…
Reference in New Issue
Block a user