Code

Allow git-apply to recount the lines in a hunk (AKA recountdiff)
[git.git] / builtin-apply.c
index 1103625a4a923d92ca8d335d1a872236630541e5..64cf8af8f6a04bb738f85f55c35d07f0b5da78ed 100644 (file)
@@ -153,6 +153,7 @@ struct patch {
        unsigned int is_binary:1;
        unsigned int is_copy:1;
        unsigned int is_rename:1;
+       unsigned int recount:1;
        struct fragment *fragments;
        char *result;
        size_t resultsize;
@@ -418,7 +419,7 @@ static int guess_p_value(const char *nameline)
 }
 
 /*
- * Get the name etc info from the --/+++ lines of a traditional patch header
+ * Get the name etc info from the ---/+++ lines of a traditional patch header
  *
  * FIXME! The end-of-filename heuristics are kind of screwy. For existing
  * files, we can happily check the index for a match, but for creating a
@@ -882,6 +883,56 @@ static int parse_range(const char *line, int len, int offset, const char *expect
        return offset + ex;
 }
 
+static void recount_diff(char *line, int size, struct fragment *fragment)
+{
+       int oldlines = 0, newlines = 0, ret = 0;
+
+       if (size < 1) {
+               warning("recount: ignore empty hunk");
+               return;
+       }
+
+       for (;;) {
+               int len = linelen(line, size);
+               size -= len;
+               line += len;
+
+               if (size < 1)
+                       break;
+
+               switch (*line) {
+               case ' ': case '\n':
+                       newlines++;
+                       /* fall through */
+               case '-':
+                       oldlines++;
+                       continue;
+               case '+':
+                       newlines++;
+                       continue;
+               case '\\':
+                       break;
+               case '@':
+                       ret = size < 3 || prefixcmp(line, "@@ ");
+                       break;
+               case 'd':
+                       ret = size < 5 || prefixcmp(line, "diff ");
+                       break;
+               default:
+                       ret = -1;
+                       break;
+               }
+               if (ret) {
+                       warning("recount: unexpected line: %.*s",
+                               (int)linelen(line, size), line);
+                       return;
+               }
+               break;
+       }
+       fragment->oldlines = oldlines;
+       fragment->newlines = newlines;
+}
+
 /*
  * Parse a unified diff fragment header of the
  * form "@@ -a,b +c,d @@"
@@ -1013,6 +1064,8 @@ static int parse_fragment(char *line, unsigned long size,
        offset = parse_fragment_header(line, len, fragment);
        if (offset < 0)
                return -1;
+       if (offset > 0 && patch->recount)
+               recount_diff(line + offset, size - offset, fragment);
        oldlines = fragment->oldlines;
        newlines = fragment->newlines;
        leading = 0;
@@ -1143,21 +1196,6 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc
        if (patch->is_delete < 0 &&
            (newlines || (patch->fragments && patch->fragments->next)))
                patch->is_delete = 0;
-       if (!unidiff_zero || context) {
-               /* If the user says the patch is not generated with
-                * --unified=0, or if we have seen context lines,
-                * then not having oldlines means the patch is creation,
-                * and not having newlines means the patch is deletion.
-                */
-               if (patch->is_new < 0 && !oldlines) {
-                       patch->is_new = 1;
-                       patch->old_name = NULL;
-               }
-               if (patch->is_delete < 0 && !newlines) {
-                       patch->is_delete = 1;
-                       patch->new_name = NULL;
-               }
-       }
 
        if (0 < patch->is_new && oldlines)
                die("new file %s depends on old contents", patch->new_name);
@@ -2267,16 +2305,11 @@ static int verify_index_match(struct cache_entry *ce, struct stat *st)
        return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID);
 }
 
-static int check_patch(struct patch *patch, struct patch *prev_patch)
+static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st)
 {
-       struct stat st;
        const char *old_name = patch->old_name;
-       const char *new_name = patch->new_name;
-       const char *name = old_name ? old_name : new_name;
-       struct cache_entry *ce = NULL;
-       int ok_if_exists;
-
-       patch->rejected = 1; /* we will drop this after we succeed */
+       int stat_ret = 0;
+       unsigned st_mode = 0;
 
        /*
         * Make sure that we do not have local modifications from the
@@ -2284,58 +2317,84 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
         * we have the preimage file to be patched in the work tree,
         * unless --cached, which tells git to apply only in the index.
         */
-       if (old_name) {
-               int stat_ret = 0;
-               unsigned st_mode = 0;
-
-               if (!cached)
-                       stat_ret = lstat(old_name, &st);
-               if (check_index) {
-                       int pos = cache_name_pos(old_name, strlen(old_name));
-                       if (pos < 0)
-                               return error("%s: does not exist in index",
-                                            old_name);
-                       ce = active_cache[pos];
-                       if (stat_ret < 0) {
-                               struct checkout costate;
-                               if (errno != ENOENT)
-                                       return error("%s: %s", old_name,
-                                                    strerror(errno));
-                               /* checkout */
-                               costate.base_dir = "";
-                               costate.base_dir_len = 0;
-                               costate.force = 0;
-                               costate.quiet = 0;
-                               costate.not_new = 0;
-                               costate.refresh_cache = 1;
-                               if (checkout_entry(ce,
-                                                  &costate,
-                                                  NULL) ||
-                                   lstat(old_name, &st))
-                                       return -1;
-                       }
-                       if (!cached && verify_index_match(ce, &st))
-                               return error("%s: does not match index",
-                                            old_name);
-                       if (cached)
-                               st_mode = ce->ce_mode;
-               } else if (stat_ret < 0)
-                       return error("%s: %s", old_name, strerror(errno));
-
-               if (!cached)
-                       st_mode = ce_mode_from_stat(ce, st.st_mode);
+       if (!old_name)
+               return 0;
 
+       assert(patch->is_new <= 0);
+       if (!cached) {
+               stat_ret = lstat(old_name, st);
+               if (stat_ret && errno != ENOENT)
+                       return error("%s: %s", old_name, strerror(errno));
+       }
+       if (check_index) {
+               int pos = cache_name_pos(old_name, strlen(old_name));
+               if (pos < 0) {
+                       if (patch->is_new < 0)
+                               goto is_new;
+                       return error("%s: does not exist in index", old_name);
+               }
+               *ce = active_cache[pos];
+               if (stat_ret < 0) {
+                       struct checkout costate;
+                       /* checkout */
+                       costate.base_dir = "";
+                       costate.base_dir_len = 0;
+                       costate.force = 0;
+                       costate.quiet = 0;
+                       costate.not_new = 0;
+                       costate.refresh_cache = 1;
+                       if (checkout_entry(*ce, &costate, NULL) ||
+                           lstat(old_name, st))
+                               return -1;
+               }
+               if (!cached && verify_index_match(*ce, st))
+                       return error("%s: does not match index", old_name);
+               if (cached)
+                       st_mode = (*ce)->ce_mode;
+       } else if (stat_ret < 0) {
                if (patch->is_new < 0)
-                       patch->is_new = 0;
-               if (!patch->old_mode)
-                       patch->old_mode = st_mode;
-               if ((st_mode ^ patch->old_mode) & S_IFMT)
-                       return error("%s: wrong type", old_name);
-               if (st_mode != patch->old_mode)
-                       fprintf(stderr, "warning: %s has type %o, expected %o\n",
-                               old_name, st_mode, patch->old_mode);
+                       goto is_new;
+               return error("%s: %s", old_name, strerror(errno));
        }
 
+       if (!cached)
+               st_mode = ce_mode_from_stat(*ce, st->st_mode);
+
+       if (patch->is_new < 0)
+               patch->is_new = 0;
+       if (!patch->old_mode)
+               patch->old_mode = st_mode;
+       if ((st_mode ^ patch->old_mode) & S_IFMT)
+               return error("%s: wrong type", old_name);
+       if (st_mode != patch->old_mode)
+               fprintf(stderr, "warning: %s has type %o, expected %o\n",
+                       old_name, st_mode, patch->old_mode);
+       return 0;
+
+ is_new:
+       patch->is_new = 1;
+       patch->is_delete = 0;
+       patch->old_name = NULL;
+       return 0;
+}
+
+static int check_patch(struct patch *patch, struct patch *prev_patch)
+{
+       struct stat st;
+       const char *old_name = patch->old_name;
+       const char *new_name = patch->new_name;
+       const char *name = old_name ? old_name : new_name;
+       struct cache_entry *ce = NULL;
+       int ok_if_exists;
+       int status;
+
+       patch->rejected = 1; /* we will drop this after we succeed */
+
+       status = check_preimage(patch, &ce, &st);
+       if (status)
+               return status;
+       old_name = patch->old_name;
+
        if (new_name && prev_patch && 0 < prev_patch->is_delete &&
            !strcmp(prev_patch->old_name, new_name))
                /*
@@ -2906,7 +2965,10 @@ static void prefix_patches(struct patch *p)
        }
 }
 
-static int apply_patch(int fd, const char *filename, int inaccurate_eof)
+#define INACCURATE_EOF (1<<0)
+#define RECOUNT                (1<<1)
+
+static int apply_patch(int fd, const char *filename, int options)
 {
        size_t offset;
        struct strbuf buf;
@@ -2922,7 +2984,8 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
                int nr;
 
                patch = xcalloc(1, sizeof(*patch));
-               patch->inaccurate_eof = inaccurate_eof;
+               patch->inaccurate_eof = !!(options & INACCURATE_EOF);
+               patch->recount =  !!(options & RECOUNT);
                nr = parse_chunk(buf.buf + offset, buf.len - offset, patch);
                if (nr < 0)
                        break;
@@ -2979,11 +3042,11 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
        return 0;
 }
 
-static int git_apply_config(const char *var, const char *value)
+static int git_apply_config(const char *var, const char *value, void *cb)
 {
        if (!strcmp(var, "apply.whitespace"))
                return git_config_string(&apply_default_whitespace, var, value);
-       return git_default_config(var, value);
+       return git_default_config(var, value, cb);
 }
 
 
@@ -2991,7 +3054,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
 {
        int i;
        int read_stdin = 1;
-       int inaccurate_eof = 0;
+       int options = 0;
        int errs = 0;
        int is_not_gitdir;
 
@@ -2999,7 +3062,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
 
        prefix = setup_git_directory_gently(&is_not_gitdir);
        prefix_length = prefix ? strlen(prefix) : 0;
-       git_config(git_apply_config);
+       git_config(git_apply_config, NULL);
        if (apply_default_whitespace)
                parse_whitespace_option(apply_default_whitespace);
 
@@ -3009,7 +3072,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
                int fd;
 
                if (!strcmp(arg, "-")) {
-                       errs |= apply_patch(0, "<stdin>", inaccurate_eof);
+                       errs |= apply_patch(0, "<stdin>", options);
                        read_stdin = 0;
                        continue;
                }
@@ -3109,7 +3172,11 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
                        continue;
                }
                if (!strcmp(arg, "--inaccurate-eof")) {
-                       inaccurate_eof = 1;
+                       options |= INACCURATE_EOF;
+                       continue;
+               }
+               if (!strcmp(arg, "--recount")) {
+                       options |= RECOUNT;
                        continue;
                }
                if (0 < prefix_length)
@@ -3120,12 +3187,12 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
                        die("can't open patch '%s': %s", arg, strerror(errno));
                read_stdin = 0;
                set_default_whitespace_mode(whitespace_option);
-               errs |= apply_patch(fd, arg, inaccurate_eof);
+               errs |= apply_patch(fd, arg, options);
                close(fd);
        }
        set_default_whitespace_mode(whitespace_option);
        if (read_stdin)
-               errs |= apply_patch(0, "<stdin>", inaccurate_eof);
+               errs |= apply_patch(0, "<stdin>", options);
        if (whitespace_error) {
                if (squelch_whitespace_errors &&
                    squelch_whitespace_errors < whitespace_error) {