Code

Merge branch 'maint'
authorJunio C Hamano <gitster@pobox.com>
Sat, 7 Jul 2007 19:29:09 +0000 (12:29 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sat, 7 Jul 2007 19:29:09 +0000 (12:29 -0700)
* maint:
  Fix "apply --reverse" with regard to whitespace

1  2 
builtin-apply.c
t/t4116-apply-reverse.sh

diff --combined builtin-apply.c
index c6f736c14e4840bde2efd204e7f88a3556e9eed9,490e23ef40c2b2d16272c67a67a14a1eb03b3877..0a0b4a9e3f6550b6a4729e94242811dcb6e8d09a
@@@ -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;
  
                char first;
                int len = linelen(patch, size);
                int plen;
 +              int added_blank_line = 0;
  
                if (!len)
                        break;
                        else if (first == '+')
                                first = '-';
                }
 +
                switch (first) {
                case '\n':
                        /* Newer GNU diff, empty context line */
                                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 */
                                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;
        }
                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 a7f5905f1e651fea6690bfbdb5857c68fc4fda27,11e02807a5324ad263c9b2bce53be3df9daf2117..9ae2b3a8efaad4430bbcea02152470b064263b2b
@@@ -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