Merge branch 'rs/apply-name-terminate'
authorJunio C Hamano <gitster@pobox.com>
Fri, 3 Jun 2016 21:38:04 +0000 (14:38 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 3 Jun 2016 21:38:04 +0000 (14:38 -0700)
Code clean-up.

* rs/apply-name-terminate:
apply: remove unused parameters from name_terminate()

1  2 
builtin/apply.c
diff --combined builtin/apply.c
index 8e4da2e1bdaf02590289f54750a9602281f4a365,5d1a15e4f62307b14520f8759b7eeaf924feda7d..c770d7d3d9fd923957478667291a77d33935ebe5
@@@ -77,7 -77,8 +77,7 @@@ static enum ws_ignore 
  
  
  static const char *patch_input_file;
 -static const char *root;
 -static int root_len;
 +static struct strbuf root = STRBUF_INIT;
  static int read_stdin = 1;
  static int options;
  
@@@ -207,7 -208,7 +207,7 @@@ struct patch 
        struct patch *next;
  
        /* three-way fallback result */
 -      unsigned char threeway_stage[3][20];
 +      struct object_id threeway_stage[3];
  };
  
  static void free_fragment_list(struct fragment *list)
@@@ -442,7 -443,7 +442,7 @@@ static int is_dev_null(const char *str
  #define TERM_SPACE    1
  #define TERM_TAB      2
  
- static int name_terminate(const char *name, int namelen, int c, int terminate)
+ static int name_terminate(int c, int terminate)
  {
        if (c == ' ' && !(terminate & TERM_SPACE))
                return 0;
@@@ -493,8 -494,8 +493,8 @@@ static char *find_name_gnu(const char *
        }
  
        strbuf_remove(&name, 0, cp - name.buf);
 -      if (root)
 -              strbuf_insert(&name, 0, root, root_len);
 +      if (root.len)
 +              strbuf_insert(&name, 0, root.buf, root.len);
        return squash_slash(strbuf_detach(&name, NULL));
  }
  
@@@ -671,7 -672,7 +671,7 @@@ static char *find_name_common(const cha
                if (!end && isspace(c)) {
                        if (c == '\n')
                                break;
-                       if (name_terminate(start, line-start, c, terminate))
+                       if (name_terminate(c, terminate))
                                break;
                }
                line++;
                        return squash_slash(xstrdup(def));
        }
  
 -      if (root) {
 -              char *ret = xmalloc(root_len + len + 1);
 -              strcpy(ret, root);
 -              memcpy(ret + root_len, start, len);
 -              ret[root_len + len] = '\0';
 +      if (root.len) {
 +              char *ret = xstrfmt("%s%.*s", root.buf, len, start);
                return squash_slash(ret);
        }
  
@@@ -781,7 -785,7 +781,7 @@@ static int guess_p_value(const char *na
  }
  
  /*
 - * Does the ---/+++ line has the POSIX timestamp after the last HT?
 + * Does the ---/+++ line have the POSIX timestamp after the last HT?
   * GNU diff puts epoch there to signal a creation/deletion event.  Is
   * this such a timestamp?
   */
@@@ -931,19 -935,22 +931,19 @@@ static char *gitdiff_verify_name(const 
                return find_name(line, NULL, p_value, TERM_TAB);
  
        if (orig_name) {
 -              int len;
 -              const char *name;
 +              int len = strlen(orig_name);
                char *another;
 -              name = orig_name;
 -              len = strlen(name);
                if (isnull)
 -                      die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"), name, linenr);
 +                      die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
 +                          orig_name, linenr);
                another = find_name(line, NULL, p_value, TERM_TAB);
 -              if (!another || memcmp(another, name, len + 1))
 +              if (!another || memcmp(another, orig_name, len + 1))
                        die((side == DIFF_NEW_NAME) ?
                            _("git apply: bad git-diff - inconsistent new filename on line %d") :
                            _("git apply: bad git-diff - inconsistent old filename on line %d"), linenr);
                free(another);
                return orig_name;
 -      }
 -      else {
 +      } else {
                /* expect "/dev/null" */
                if (memcmp("/dev/null", line, 9) || line[9] != '\n')
                        die(_("git apply: bad git-diff - expected /dev/null on line %d"), linenr);
  
  static int gitdiff_oldname(const char *line, struct patch *patch)
  {
 -      char *orig = patch->old_name;
        patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name,
                                              DIFF_OLD_NAME);
 -      if (orig != patch->old_name)
 -              free(orig);
        return 0;
  }
  
  static int gitdiff_newname(const char *line, struct patch *patch)
  {
 -      char *orig = patch->new_name;
        patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name,
                                              DIFF_NEW_NAME);
 -      if (orig != patch->new_name)
 -              free(orig);
        return 0;
  }
  
@@@ -1264,8 -1277,8 +1264,8 @@@ static int parse_git_header(const char 
         * the default name from the header.
         */
        patch->def_name = git_header_name(line, len);
 -      if (patch->def_name && root) {
 -              char *s = xstrfmt("%s%s", root, patch->def_name);
 +      if (patch->def_name && root.len) {
 +              char *s = xstrfmt("%s%s", root.buf, patch->def_name);
                free(patch->def_name);
                patch->def_name = s;
        }
@@@ -1625,9 -1638,6 +1625,9 @@@ static int parse_fragment(const char *l
        }
        if (oldlines || newlines)
                return -1;
 +      if (!deleted && !added)
 +              return -1;
 +
        fragment->leading = leading;
        fragment->trailing = trailing;
  
@@@ -1863,11 -1873,6 +1863,11 @@@ static struct fragment *parse_binary_hu
        return NULL;
  }
  
 +/*
 + * Returns:
 + *   -1 in case of error,
 + *   the length of the parsed binary patch otherwise
 + */
  static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
  {
        /*
@@@ -2013,8 -2018,6 +2013,8 @@@ static int parse_chunk(char *buffer, un
                        linenr++;
                        used = parse_binary(buffer + hd + llen,
                                            size - hd - llen, patch);
 +                      if (used < 0)
 +                              return -1;
                        if (used)
                                patchsize = used + llen;
                        else
@@@ -2630,7 -2633,7 +2630,7 @@@ static void update_image(struct image *
        insert_count = postimage->len;
  
        /* Adjust the contents */
 -      result = xmalloc(img->len + insert_count - remove_count + 1);
 +      result = xmalloc(st_add3(st_sub(img->len, remove_count), insert_count, 1));
        memcpy(result, img->buf, applied_at);
        memcpy(result + applied_at, postimage->buf, postimage->len);
        memcpy(result + applied_at + postimage->len,
@@@ -3423,11 -3426,11 +3423,11 @@@ static int try_threeway(struct image *i
        if (status) {
                patch->conflicted_threeway = 1;
                if (patch->is_new)
 -                      hashclr(patch->threeway_stage[0]);
 +                      oidclr(&patch->threeway_stage[0]);
                else
 -                      hashcpy(patch->threeway_stage[0], pre_sha1);
 -              hashcpy(patch->threeway_stage[1], our_sha1);
 -              hashcpy(patch->threeway_stage[2], post_sha1);
 +                      hashcpy(patch->threeway_stage[0].hash, pre_sha1);
 +              hashcpy(patch->threeway_stage[1].hash, our_sha1);
 +              hashcpy(patch->threeway_stage[2].hash, post_sha1);
                fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
        } else {
                fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
@@@ -4183,14 -4186,14 +4183,14 @@@ static void add_conflicted_stages_file(
  
        remove_file_from_cache(patch->new_name);
        for (stage = 1; stage < 4; stage++) {
 -              if (is_null_sha1(patch->threeway_stage[stage - 1]))
 +              if (is_null_oid(&patch->threeway_stage[stage - 1]))
                        continue;
                ce = xcalloc(1, ce_size);
                memcpy(ce->name, patch->new_name, namelen);
                ce->ce_mode = create_ce_mode(mode);
                ce->ce_flags = create_ce_flags(stage);
                ce->ce_namelen = namelen;
 -              hashcpy(ce->sha1, patch->threeway_stage[stage - 1]);
 +              hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);
                if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
                        die(_("unable to add cache entry for %s"), patch->new_name);
        }
@@@ -4371,10 -4374,8 +4371,10 @@@ static int apply_patch(int fd, const ch
                patch->inaccurate_eof = !!(options & INACCURATE_EOF);
                patch->recount =  !!(options & RECOUNT);
                nr = parse_chunk(buf.buf + offset, buf.len - offset, patch);
 -              if (nr < 0)
 +              if (nr < 0) {
 +                      free_patch(patch);
                        break;
 +              }
                if (apply_in_reverse)
                        reverse_patches(patch);
                if (use_patch(patch)) {
                        listp = &patch->next;
                }
                else {
 +                      if (apply_verbosely)
 +                              say_patch_name(stderr, _("Skipped patch '%s'."), patch);
                        free_patch(patch);
                        skipped_patch++;
                }
@@@ -4466,6 -4465,16 +4466,6 @@@ static int option_parse_p(const struct 
        return 0;
  }
  
 -static int option_parse_z(const struct option *opt,
 -                        const char *arg, int unset)
 -{
 -      if (unset)
 -              line_termination = '\n';
 -      else
 -              line_termination = 0;
 -      return 0;
 -}
 -
  static int option_parse_space_change(const struct option *opt,
                          const char *arg, int unset)
  {
@@@ -4489,9 -4498,14 +4489,9 @@@ static int option_parse_whitespace(cons
  static int option_parse_directory(const struct option *opt,
                                  const char *arg, int unset)
  {
 -      root_len = strlen(arg);
 -      if (root_len && arg[root_len - 1] != '/') {
 -              char *new_root;
 -              root = new_root = xmalloc(root_len + 2);
 -              strcpy(new_root, arg);
 -              strcpy(new_root + root_len++, "/");
 -      } else
 -              root = arg;
 +      strbuf_reset(&root);
 +      strbuf_addstr(&root, arg);
 +      strbuf_complete(&root, '/');
        return 0;
  }
  
@@@ -4538,9 -4552,9 +4538,9 @@@ int cmd_apply(int argc, const char **ar
                         N_( "attempt three-way merge if a patch does not apply")),
                OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,
                        N_("build a temporary index based on embedded index information")),
 -              { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
 -                      N_("paths are separated with NUL character"),
 -                      PARSE_OPT_NOARG, option_parse_z },
 +              /* Think twice before adding "--nul" synonym to this */
 +              OPT_SET_INT('z', NULL, &line_termination,
 +                      N_("paths are separated with NUL character"), '\0'),
                OPT_INTEGER('C', NULL, &p_context,
                                N_("ensure at least <n> lines of context match")),
                { OPTION_CALLBACK, 0, "whitespace", &whitespace_option, N_("action"),