From: Junio C Hamano Date: Sat, 7 Jul 2007 19:29:09 +0000 (-0700) Subject: Merge branch 'maint' X-Git-Tag: v1.5.3-rc1~56 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/0707a9d6f200bbafc02d13dd7f2c12bb795999f6?ds=inline;hp=-c Merge branch 'maint' * maint: Fix "apply --reverse" with regard to whitespace --- 0707a9d6f200bbafc02d13dd7f2c12bb795999f6 diff --combined builtin-apply.c index c6f736c14e,490e23ef40..0a0b4a9e3f --- a/builtin-apply.c +++ b/builtin-apply.c @@@ -55,7 -55,7 +55,7 @@@ static enum whitespace_eol } new_whitespace = warn_on_whitespace; static int whitespace_error; static int squelch_whitespace_errors = 5; -static int applied_after_stripping; +static int applied_after_fixing_ws; static const char *patch_input_file; static void parse_whitespace_option(const char *option) @@@ -1003,12 -1003,16 +1003,16 @@@ static int parse_fragment(char *line, u trailing++; break; case '-': + if (apply_in_reverse && + new_whitespace != nowarn_whitespace) + check_whitespace(line, len); deleted++; oldlines--; trailing = 0; break; case '+': - if (new_whitespace != nowarn_whitespace) + if (!apply_in_reverse && + new_whitespace != nowarn_whitespace) check_whitespace(line, len); added++; newlines--; @@@ -1657,7 -1661,7 +1661,7 @@@ static int apply_line(char *output, con if (add_nl_to_tail) output[plen++] = '\n'; if (fixed) - applied_after_stripping++; + applied_after_fixing_ws++; return output + plen - buf; } @@@ -1671,7 -1675,6 +1675,7 @@@ static int apply_one_fragment(struct bu char *new = xmalloc(size); const char *oldlines, *newlines; int oldsize = 0, newsize = 0; + int new_blank_lines_at_end = 0; unsigned long leading, trailing; int pos, lines; @@@ -1679,7 -1682,6 +1683,7 @@@ char first; int len = linelen(patch, size); int plen; + int added_blank_line = 0; if (!len) break; @@@ -1701,7 -1703,6 +1705,7 @@@ else if (first == '+') first = '-'; } + switch (first) { case '\n': /* Newer GNU diff, empty context line */ @@@ -1719,14 -1720,9 +1723,14 @@@ break; /* Fall-through for ' ' */ case '+': - if (first != '+' || !no_add) - newsize += apply_line(new + newsize, patch, - plen); + if (first != '+' || !no_add) { + int added = apply_line(new + newsize, patch, + plen); + newsize += added; + if (first == '+' && + added == 1 && new[newsize-1] == '\n') + added_blank_line = 1; + } break; case '@': case '\\': /* Ignore it, we already handled it */ @@@ -1736,10 -1732,6 +1740,10 @@@ error("invalid start of line: '%c'", first); return -1; } + if (added_blank_line) + new_blank_lines_at_end++; + else + new_blank_lines_at_end = 0; patch += len; size -= len; } @@@ -1782,16 -1774,9 +1786,16 @@@ if (match_beginning && offset) offset = -1; if (offset >= 0) { - int diff = newsize - oldsize; - unsigned long size = desc->size + diff; - unsigned long alloc = desc->alloc; + int diff; + unsigned long size, 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 * of context lines. @@@ -2884,17 -2869,18 +2888,17 @@@ int cmd_apply(int argc, const char **ar squelched == 1 ? "" : "s"); } if (new_whitespace == error_on_whitespace) - die("%d line%s add%s trailing whitespaces.", + die("%d line%s add%s whitespace errors.", whitespace_error, whitespace_error == 1 ? "" : "s", whitespace_error == 1 ? "s" : ""); - if (applied_after_stripping) + if (applied_after_fixing_ws) fprintf(stderr, "warning: %d line%s applied after" - " stripping trailing whitespaces.\n", - applied_after_stripping, - applied_after_stripping == 1 ? "" : "s"); + " fixing whitespace errors.\n", + applied_after_fixing_ws, + applied_after_fixing_ws == 1 ? "" : "s"); else if (whitespace_error) - fprintf(stderr, "warning: %d line%s add%s trailing" - " whitespaces.\n", + fprintf(stderr, "warning: %d line%s add%s whitespace errors.\n", whitespace_error, whitespace_error == 1 ? "" : "s", whitespace_error == 1 ? "s" : ""); diff --combined t/t4116-apply-reverse.sh index a7f5905f1e,11e02807a5..9ae2b3a8ef --- a/t/t4116-apply-reverse.sh +++ b/t/t4116-apply-reverse.sh @@@ -3,7 -3,7 +3,7 @@@ # Copyright (c) 2005 Junio C Hamano # -test_description='git-apply in reverse +test_description='git apply in reverse ' @@@ -82,4 -82,10 +82,10 @@@ test_expect_success 'apply in reverse w ) ' + test_expect_success 'reversing a whitespace introduction' ' + sed "s/a/a /" < file1 > file1.new && + mv file1.new file1 && + git diff | git apply --reverse --whitespace=error + ' + test_done