Merge branch 'mc/ws'

* mc/ws:
  git-apply: Fix removal of new trailing blank lines.
  Teach 'git-apply --whitespace=strip' to remove empty lines at the end of file
This commit is contained in:
Junio C Hamano 2007-05-21 23:57:05 -07:00
commit a3342a2f52

View File

@ -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 new_blank_lines_at_end = 0;
unsigned long leading, trailing; unsigned long leading, trailing;
int pos, lines; int pos, lines;
@ -1678,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;
@ -1699,6 +1701,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
else if (first == '+') else if (first == '+')
first = '-'; first = '-';
} }
switch (first) { switch (first) {
case '\n': case '\n':
/* Newer GNU diff, empty context line */ /* Newer GNU diff, empty context line */
@ -1716,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 */
@ -1728,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;
} }
@ -1770,9 +1782,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 -= new_blank_lines_at_end;
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.