@matheusbsbs / matheus-bagliano / commits / 42ab241cfa
Fork 0

builtin-apply.c: do not feed copy_wsfix() leading '+'

The "patch" parameter used to include leading '+' of an added line in the patch, and the array was treated as 1-based. Make it accept the contents of the line alone and simplify the code. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 30, 2008 at 14:27 UTC 42ab241cfacdcbae910d51c06fa2870cc94b0af3
1 file changed +18 -20
builtin-apply.c
+18 -20
@@ -1646,16 +1646,15 @@ static int copy_wsfix(char *output, const char *patch, int plen,
1646 unsigned ws_rule)
1647 {
1648 /*
1649 - * plen is number of bytes to be copied from patch,
1650 - * starting at patch+1 (patch[0] is '+'). Typically
1651 - * patch[plen] is '\n', unless this is the incomplete
1652 - * last line.
1649 + * plen is number of bytes to be copied from patch, starting
1650 + * at patch. Typically patch[plen-1] is '\n', unless this is
1651 + * the incomplete last line.
1652 */
1653 int i;
1654 int add_nl_to_tail = 0;
1655 int fixed = 0;
1657 - int last_tab_in_indent = 0;
1658 - int last_space_in_indent = 0;
1656 + int last_tab_in_indent = -1;
1657 + int last_space_in_indent = -1;
1658 int need_fix_leading_space = 0;
1659 char *buf;
1660
@@ -1663,11 +1662,11 @@ static int copy_wsfix(char *output, const char *patch, int plen,
1662 * Strip trailing whitespace
1663 */
1664 if ((ws_rule & WS_TRAILING_SPACE) &&
1666 - (1 < plen && isspace(patch[plen-1]))) {
1667 - if (patch[plen] == '\n')
1665 + (2 < plen && isspace(patch[plen-2]))) {
1666 + if (patch[plen-1] == '\n')
1667 add_nl_to_tail = 1;
1668 plen--;
1670 - while (0 < plen && isspace(patch[plen]))
1669 + while (0 < plen && isspace(patch[plen-1]))
1670 plen--;
1671 fixed = 1;
1672 }
@@ -1675,25 +1674,25 @@ static int copy_wsfix(char *output, const char *patch, int plen,
1674 /*
1675 * Check leading whitespaces (indent)
1676 */
1678 - for (i = 1; i < plen; i++) {
1677 + for (i = 0; i < plen; i++) {
1678 char ch = patch[i];
1679 if (ch == '\t') {
1680 last_tab_in_indent = i;
1681 if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
1683 - 0 < last_space_in_indent)
1682 + 0 <= last_space_in_indent)
1683 need_fix_leading_space = 1;
1684 } else if (ch == ' ') {
1685 last_space_in_indent = i;
1686 if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
1687 8 <= i - last_tab_in_indent)
1688 need_fix_leading_space = 1;
1690 - }
1691 - else
1689 + } else
1690 break;
1691 }
1692
1693 buf = output;
1694 if (need_fix_leading_space) {
1695 + /* Process indent ourselves */
1696 int consecutive_spaces = 0;
1697 int last = last_tab_in_indent + 1;
1698
@@ -1706,10 +1705,10 @@ static int copy_wsfix(char *output, const char *patch, int plen,
1705 }
1706
1707 /*
1709 - * between patch[1..last], strip the funny spaces,
1708 + * between patch[0..last-1], strip the funny spaces,
1709 * updating them to tab as needed.
1710 */
1712 - for (i = 1; i < last; i++, plen--) {
1711 + for (i = 0; i < last; i++) {
1712 char ch = patch[i];
1713 if (ch != ' ') {
1714 consecutive_spaces = 0;
@@ -1724,13 +1723,12 @@ static int copy_wsfix(char *output, const char *patch, int plen,
1723 }
1724 while (0 < consecutive_spaces--)
1725 *output++ = ' ';
1726 + plen -= last;
1727 + patch += last;
1728 fixed = 1;
1728 - i = last;
1729 }
1730 - else
1731 - i = 1;
1730
1733 - memcpy(output, patch + i, plen);
1731 + memcpy(output, patch, plen);
1732 if (add_nl_to_tail)
1733 output[plen++] = '\n';
1734 if (fixed)
@@ -1871,7 +1869,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
1869 added = plen;
1870 }
1871 else {
1874 - added = copy_wsfix(new, patch, plen, ws_rule);
1872 + added = copy_wsfix(new, patch + 1, plen, ws_rule);
1873 }
1874 add_line_info(&postimage, new, added,
1875 (first == '+' ? 0 : LINE_COMMON));