Merge branch 'jk/apply-similaritly-parsing' into maint
authorJunio C Hamano <gitster@pobox.com>
Mon, 25 Feb 2013 16:03:44 +0000 (08:03 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 25 Feb 2013 16:03:44 +0000 (08:03 -0800)
* jk/apply-similaritly-parsing:
builtin/apply: tighten (dis)similarity index parsing

1  2 
builtin/apply.c
diff --combined builtin/apply.c
index 9706ca73ab0bc2ee4c3cf7ce7ab39591830a1fab,fd2b40e18af7a7b20e160be5fadbedfd4c188882..080ce2ea3e4d6290e5db5567c0bdd217725a36c4
@@@ -1041,15 -1041,17 +1041,17 @@@ static int gitdiff_renamedst(const cha
  
  static int gitdiff_similarity(const char *line, struct patch *patch)
  {
-       if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
-               patch->score = 0;
+       unsigned long val = strtoul(line, NULL, 10);
+       if (val <= 100)
+               patch->score = val;
        return 0;
  }
  
  static int gitdiff_dissimilarity(const char *line, struct patch *patch)
  {
-       if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
-               patch->score = 0;
+       unsigned long val = strtoul(line, NULL, 10);
+       if (val <= 100)
+               patch->score = val;
        return 0;
  }
  
@@@ -2095,7 -2097,7 +2097,7 @@@ static void update_pre_post_images(stru
                                   char *buf,
                                   size_t len, size_t postlen)
  {
 -      int i, ctx;
 +      int i, ctx, reduced;
        char *new, *old, *fixed;
        struct image fixed_preimage;
  
         * free "oldlines".
         */
        prepare_image(&fixed_preimage, buf, len, 1);
 -      assert(fixed_preimage.nr == preimage->nr);
 -      for (i = 0; i < preimage->nr; i++)
 +      assert(postlen
 +             ? fixed_preimage.nr == preimage->nr
 +             : fixed_preimage.nr <= preimage->nr);
 +      for (i = 0; i < fixed_preimage.nr; i++)
                fixed_preimage.line[i].flag = preimage->line[i].flag;
        free(preimage->line_allocated);
        *preimage = fixed_preimage;
        else
                new = old;
        fixed = preimage->buf;
 -      for (i = ctx = 0; i < postimage->nr; i++) {
 +
 +      for (i = reduced = ctx = 0; i < postimage->nr; i++) {
                size_t len = postimage->line[i].len;
                if (!(postimage->line[i].flag & LINE_COMMON)) {
                        /* an added line -- no counterparts in preimage */
                        fixed += preimage->line[ctx].len;
                        ctx++;
                }
 -              if (preimage->nr <= ctx)
 -                      die(_("oops"));
 +
 +              /*
 +               * preimage is expected to run out, if the caller
 +               * fixed addition of trailing blank lines.
 +               */
 +              if (preimage->nr <= ctx) {
 +                      reduced++;
 +                      continue;
 +              }
  
                /* and copy it in, while fixing the line length */
                len = preimage->line[ctx].len;
  
        /* Fix the length of the whole thing */
        postimage->len = new - postimage->buf;
 +      postimage->nr -= reduced;
  }
  
  static int match_fragment(struct image *img,
@@@ -3609,6 -3600,7 +3611,6 @@@ static void build_fake_ancestor(struct 
         * worth showing the new sha1 prefix, but until then...
         */
        for (patch = list; patch; patch = patch->next) {
 -              const unsigned char *sha1_ptr;
                unsigned char sha1[20];
                struct cache_entry *ce;
                const char *name;
                name = patch->old_name ? patch->old_name : patch->new_name;
                if (0 < patch->is_new)
                        continue;
 -              else if (get_sha1_blob(patch->old_sha1_prefix, sha1))
 -                      /* git diff has no index line for mode/type changes */
 -                      if (!patch->lines_added && !patch->lines_deleted) {
 -                              if (get_current_sha1(patch->old_name, sha1))
 -                                      die("mode change for %s, which is not "
 -                                              "in current HEAD", name);
 -                              sha1_ptr = sha1;
 -                      } else
 -                              die("sha1 information is lacking or useless "
 -                                      "(%s).", name);
 -              else
 -                      sha1_ptr = sha1;
  
 -              ce = make_cache_entry(patch->old_mode, sha1_ptr, name, 0, 0);
 +              if (S_ISGITLINK(patch->old_mode)) {
 +                      if (get_sha1_hex(patch->old_sha1_prefix, sha1))
 +                              die("submoule change for %s without full index name",
 +                                  name);
 +              } else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
 +                      ; /* ok */
 +              } else if (!patch->lines_added && !patch->lines_deleted) {
 +                      /* mode-only change: update the current */
 +                      if (get_current_sha1(patch->old_name, sha1))
 +                              die("mode change for %s, which is not "
 +                                  "in current HEAD", name);
 +              } else
 +                      die("sha1 information is lacking or useless "
 +                          "(%s).", name);
 +
 +              ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
                if (!ce)
                        die(_("make_cache_entry failed for path '%s'"), name);
                if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
@@@ -4327,7 -4316,7 +4329,7 @@@ int cmd_apply(int argc, const char **ar
                OPT_NOOP_NOARG(0, "allow-binary-replacement"),
                OPT_NOOP_NOARG(0, "binary"),
                OPT_BOOLEAN(0, "numstat", &numstat,
 -                      N_("shows number of added and deleted lines in decimal notation")),
 +                      N_("show number of added and deleted lines in decimal notation")),
                OPT_BOOLEAN(0, "summary", &summary,
                        N_("instead of applying the patch, output a summary for the input")),
                OPT_BOOLEAN(0, "check", &check,