@matheusbsbs / matheus-bagliano / commits / 077e1af598
Fork 0

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>

Junio C Hamano committed May 20, 2007 at 23:51 UTC 077e1af5983000266bc7c1423a87b7768ca03754
1 file changed +15 -33
builtin-apply.c
+15 -33
@@ -1671,7 +1671,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1671 char *new = xmalloc(size);
1672 const char *oldlines, *newlines;
1673 int oldsize = 0, newsize = 0;
1674 - int trailing_added_lines = 0;
1674 + int new_blank_lines_at_end = 0;
1675 unsigned long leading, trailing;
1676 int pos, lines;
1677
@@ -1679,6 +1679,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1679 char first;
1680 int len = linelen(patch, size);
1681 int plen;
1682 + int added_blank_line = 0;
1683
1684 if (!len)
1685 break;
@@ -1700,16 +1701,6 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1701 else if (first == '+')
1702 first = '-';
1703 }
1703 - /*
1704 - * Count lines added at the end of the file.
1705 - * This is not enough to get things right in case of
1706 - * patches generated with --unified=0, but it's a
1707 - * useful upper bound.
1708 - */
1709 - if (first == '+')
1710 - trailing_added_lines++;
1711 - else
1712 - trailing_added_lines = 0;
1704
1705 switch (first) {
1706 case '\n':
@@ -1728,9 +1719,14 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1719 break;
1720 /* Fall-through for ' ' */
1721 case '+':
1731 - if (first != '+' || !no_add)
1732 - newsize += apply_line(new + newsize, patch,
1733 - plen);
1722 + if (first != '+' || !no_add) {
1723 + int added = apply_line(new + newsize, patch,
1724 + plen);
1725 + newsize += added;
1726 + if (first == '+' &&
1727 + added == 1 && new[newsize-1] == '\n')
1728 + added_blank_line = 1;
1729 + }
1730 break;
1731 case '@': case '\\':
1732 /* Ignore it, we already handled it */
@@ -1740,6 +1736,10 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1736 error("invalid start of line: '%c'", first);
1737 return -1;
1738 }
1739 + if (added_blank_line)
1740 + new_blank_lines_at_end++;
1741 + else
1742 + new_blank_lines_at_end = 0;
1743 patch += len;
1744 size -= len;
1745 }
@@ -1750,24 +1750,6 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1750 newsize--;
1751 }
1752
1753 - if (new_whitespace == strip_whitespace) {
1754 - /* Any added empty lines is already cleaned-up here
1755 - * becuase of 'strip_whitespace' flag, so just count '\n'
1756 - */
1757 - int empty = 0;
1758 - while ( empty < trailing_added_lines
1759 - && newsize - empty > 0
1760 - && new[newsize - empty - 1] == '\n')
1761 - empty++;
1762 -
1763 - if (empty < trailing_added_lines)
1764 - empty--;
1765 -
1766 - /* these are the empty lines added at
1767 - * the end of the file, modulo u0 patches.
1768 - */
1769 - trailing_added_lines = empty;
1770 - }
1753 oldlines = old;
1754 newlines = new;
1755 leading = frag->leading;
@@ -1805,7 +1787,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1787
1788 if (new_whitespace == strip_whitespace &&
1789 (desc->size - oldsize - offset == 0)) /* end of file? */
1808 - newsize -= trailing_added_lines;
1790 + newsize -= new_blank_lines_at_end;
1791
1792 diff = newsize - oldsize;
1793 size = desc->size + diff;