@matheusbsbs / matheus-bagliano / commits / 8441a9a842
Fork 0

builtin-apply.c: simplify calling site to apply_line()

The function apply_line() changed its behaviour depending on the ws_error_action, whitespace_error and if the input was a context. Make its caller responsible for such checking so that we can convert the function to copy the contents of line while fixing whitespace breakage more easily. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 30, 2008 at 13:19 UTC 8441a9a84278718e9bfd49112dcc55136cea394f
1 file changed +20 -18
builtin-apply.c
+20 -18
@@ -1642,7 +1642,7 @@ static void remove_last_line(struct image *img)
1642 img->len -= img->line[--img->nr].len;
1643 }
1644
1645 -static int apply_line(char *output, const char *patch, int plen,
1645 +static int copy_wsfix(char *output, const char *patch, int plen,
1646 unsigned ws_rule)
1647 {
1648 /*
@@ -1659,12 +1659,6 @@ static int apply_line(char *output, const char *patch, int plen,
1659 int need_fix_leading_space = 0;
1660 char *buf;
1661
1662 - if ((ws_error_action != correct_ws_error) || !whitespace_error ||
1663 - *patch != '+') {
1664 - memcpy(output, patch + 1, plen);
1665 - return plen;
1666 - }
1667 -
1662 /*
1663 * Strip trailing whitespace
1664 */
@@ -1821,7 +1815,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
1815 while (size > 0) {
1816 char first;
1817 int len = linelen(patch, size);
1824 - int plen;
1818 + int plen, added;
1819 int added_blank_line = 0;
1820
1821 if (!len)
@@ -1866,17 +1860,25 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
1860 break;
1861 /* Fall-through for ' ' */
1862 case '+':
1869 - if (first != '+' || !no_add) {
1870 - int added = apply_line(new, patch,
1871 - plen, ws_rule);
1872 - add_line_info(&postimage, new, added,
1873 - (first == '+' ? 0 : LINE_COMMON));
1874 -
1875 - new += added;
1876 - if (first == '+' &&
1877 - added == 1 && new[-1] == '\n')
1878 - added_blank_line = 1;
1863 + /* --no-add does not add new lines */
1864 + if (first == '+' && no_add)
1865 + break;
1866 +
1867 + if (first != '+' ||
1868 + !whitespace_error ||
1869 + ws_error_action != correct_ws_error) {
1870 + memcpy(new, patch + 1, plen);
1871 + added = plen;
1872 + }
1873 + else {
1874 + added = copy_wsfix(new, patch, plen, ws_rule);
1875 }
1876 + add_line_info(&postimage, new, added,
1877 + (first == '+' ? 0 : LINE_COMMON));
1878 + new += added;
1879 + if (first == '+' &&
1880 + added == 1 && new[-1] == '\n')
1881 + added_blank_line = 1;
1882 break;
1883 case '@': case '\\':
1884 /* Ignore it, we already handled it */