Merge branch 'jk/parseopt-string-list'
authorJunio C Hamano <gitster@pobox.com>
Mon, 8 Aug 2016 21:48:43 +0000 (14:48 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 8 Aug 2016 21:48:44 +0000 (14:48 -0700)
A small memory leak in the command line parsing of "git blame"
has been plugged.

* jk/parseopt-string-list:
blame: drop strdup of string literal

1  2 
builtin/blame.c
diff --combined builtin/blame.c
index 2e7b3030c67a4e83f902166533998461df444aa3,f3fe92643c9fb7c8c41d89a54425b7cb457fbeca..7ec782343002d27b1b7fdd7e61fada12e81eeaf1
@@@ -28,7 -28,6 +28,7 @@@
  #include "line-range.h"
  #include "line-log.h"
  #include "dir.h"
 +#include "progress.h"
  
  static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
  
@@@ -51,12 -50,11 +51,12 @@@ static int incremental
  static int xdl_opts;
  static int abbrev = -1;
  static int no_whole_file_rename;
 +static int show_progress;
  
  static struct date_mode blame_date_mode = { DATE_ISO8601 };
  static size_t blame_date_width;
  
 -static struct string_list mailmap;
 +static struct string_list mailmap = STRING_LIST_INIT_NODUP;
  
  #ifndef DEBUG
  #define DEBUG 0
@@@ -129,12 -127,7 +129,12 @@@ struct origin 
        char path[FLEX_ARRAY];
  };
  
 -static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, long ctxlen,
 +struct progress_info {
 +      struct progress *progress;
 +      int blamed_lines;
 +};
 +
 +static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
                      xdl_emit_hunk_consume_func_t hunk_func, void *cb_data)
  {
        xpparam_t xpp = {0};
        xdemitcb_t ecb = {NULL};
  
        xpp.flags = xdl_opts;
 -      xecfg.ctxlen = ctxlen;
        xecfg.hunk_func = hunk_func;
        ecb.priv = cb_data;
        return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
@@@ -465,11 -459,12 +465,11 @@@ static void queue_blames(struct scorebo
  static struct origin *make_origin(struct commit *commit, const char *path)
  {
        struct origin *o;
 -      o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
 +      FLEX_ALLOC_STR(o, path, path);
        o->commit = commit;
        o->refcnt = 1;
        o->next = commit->util;
        commit->util = o;
 -      strcpy(o->path, path);
        return o;
  }
  
@@@ -510,7 -505,7 +510,7 @@@ static int fill_blob_sha1_and_mode(stru
  {
        if (!is_null_sha1(origin->blob_sha1))
                return 0;
 -      if (get_tree_entry(origin->commit->object.sha1,
 +      if (get_tree_entry(origin->commit->object.oid.hash,
                           origin->path,
                           origin->blob_sha1, &origin->mode))
                goto error_out;
@@@ -561,11 -556,11 +561,11 @@@ static struct origin *find_origin(struc
                       PATHSPEC_LITERAL_PATH, "", paths);
        diff_setup_done(&diff_opts);
  
 -      if (is_null_sha1(origin->commit->object.sha1))
 -              do_diff_cache(parent->tree->object.sha1, &diff_opts);
 +      if (is_null_oid(&origin->commit->object.oid))
 +              do_diff_cache(parent->tree->object.oid.hash, &diff_opts);
        else
 -              diff_tree_sha1(parent->tree->object.sha1,
 -                             origin->commit->tree->object.sha1,
 +              diff_tree_sha1(parent->tree->object.oid.hash,
 +                             origin->commit->tree->object.oid.hash,
                               "", &diff_opts);
        diffcore_std(&diff_opts);
  
                            p->status);
                case 'M':
                        porigin = get_origin(sb, parent, origin->path);
 -                      hashcpy(porigin->blob_sha1, p->one->sha1);
 +                      hashcpy(porigin->blob_sha1, p->one->oid.hash);
                        porigin->mode = p->one->mode;
                        break;
                case 'A':
                }
        }
        diff_flush(&diff_opts);
 -      free_pathspec(&diff_opts.pathspec);
 +      clear_pathspec(&diff_opts.pathspec);
        return porigin;
  }
  
@@@ -631,11 -626,11 +631,11 @@@ static struct origin *find_rename(struc
        diff_opts.single_follow = origin->path;
        diff_setup_done(&diff_opts);
  
 -      if (is_null_sha1(origin->commit->object.sha1))
 -              do_diff_cache(parent->tree->object.sha1, &diff_opts);
 +      if (is_null_oid(&origin->commit->object.oid))
 +              do_diff_cache(parent->tree->object.oid.hash, &diff_opts);
        else
 -              diff_tree_sha1(parent->tree->object.sha1,
 -                             origin->commit->tree->object.sha1,
 +              diff_tree_sha1(parent->tree->object.oid.hash,
 +                             origin->commit->tree->object.oid.hash,
                               "", &diff_opts);
        diffcore_std(&diff_opts);
  
                if ((p->status == 'R' || p->status == 'C') &&
                    !strcmp(p->two->path, origin->path)) {
                        porigin = get_origin(sb, parent, p->one->path);
 -                      hashcpy(porigin->blob_sha1, p->one->sha1);
 +                      hashcpy(porigin->blob_sha1, p->one->oid.hash);
                        porigin->mode = p->one->mode;
                        break;
                }
        }
        diff_flush(&diff_opts);
 -      free_pathspec(&diff_opts.pathspec);
 +      clear_pathspec(&diff_opts.pathspec);
        return porigin;
  }
  
@@@ -979,10 -974,10 +979,10 @@@ static void pass_blame_to_parent(struc
        fill_origin_blob(&sb->revs->diffopt, target, &file_o);
        num_get_patch++;
  
 -      if (diff_hunks(&file_p, &file_o, 0, blame_chunk_cb, &d))
 +      if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d))
                die("unable to generate diff (%s -> %s)",
 -                  sha1_to_hex(parent->commit->object.sha1),
 -                  sha1_to_hex(target->commit->object.sha1));
 +                  oid_to_hex(&parent->commit->object.oid),
 +                  oid_to_hex(&target->commit->object.oid));
        /* The rest are the same as the parent */
        blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent);
        *d.dstq = NULL;
@@@ -1128,9 -1123,9 +1128,9 @@@ static void find_copy_in_blob(struct sc
         * file_p partially may match that image.
         */
        memset(split, 0, sizeof(struct blame_entry [3]));
 -      if (diff_hunks(file_p, &file_o, 1, handle_split_cb, &d))
 +      if (diff_hunks(file_p, &file_o, handle_split_cb, &d))
                die("unable to generate diff (%s)",
 -                  sha1_to_hex(parent->commit->object.sha1));
 +                  oid_to_hex(&parent->commit->object.oid));
        /* remainder, if any, all match the preimage */
        handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);
  }
@@@ -1279,11 -1274,11 +1279,11 @@@ static void find_copy_in_parent(struct 
                && (!porigin || strcmp(target->path, porigin->path))))
                DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
  
 -      if (is_null_sha1(target->commit->object.sha1))
 -              do_diff_cache(parent->tree->object.sha1, &diff_opts);
 +      if (is_null_oid(&target->commit->object.oid))
 +              do_diff_cache(parent->tree->object.oid.hash, &diff_opts);
        else
 -              diff_tree_sha1(parent->tree->object.sha1,
 -                             target->commit->tree->object.sha1,
 +              diff_tree_sha1(parent->tree->object.oid.hash,
 +                             target->commit->tree->object.oid.hash,
                               "", &diff_opts);
  
        if (!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))
                                continue;
  
                        norigin = get_origin(sb, parent, p->one->path);
 -                      hashcpy(norigin->blob_sha1, p->one->sha1);
 +                      hashcpy(norigin->blob_sha1, p->one->oid.hash);
                        norigin->mode = p->one->mode;
                        fill_origin_blob(&sb->revs->diffopt, norigin, &file_p);
                        if (!file_p.ptr)
        } while (unblamed);
        target->suspects = reverse_blame(leftover, NULL);
        diff_flush(&diff_opts);
 -      free_pathspec(&diff_opts.pathspec);
 +      clear_pathspec(&diff_opts.pathspec);
  }
  
  /*
@@@ -1694,7 -1689,7 +1694,7 @@@ static void get_commit_info(struct comm
        if (len)
                strbuf_add(&ret->summary, subject, len);
        else
 -              strbuf_addf(&ret->summary, "(%s)", sha1_to_hex(commit->object.sha1));
 +              strbuf_addf(&ret->summary, "(%s)", oid_to_hex(&commit->object.oid));
  
        unuse_commit_buffer(commit, message);
  }
@@@ -1737,7 -1732,7 +1737,7 @@@ static int emit_one_suspect_detail(stru
                printf("boundary\n");
        if (suspect->previous) {
                struct origin *prev = suspect->previous;
 -              printf("previous %s ", sha1_to_hex(prev->commit->object.sha1));
 +              printf("previous %s ", oid_to_hex(&prev->commit->object.oid));
                write_name_quoted(prev->path, stdout, '\n');
        }
  
   * The blame_entry is found to be guilty for the range.
   * Show it in incremental output.
   */
 -static void found_guilty_entry(struct blame_entry *ent)
 +static void found_guilty_entry(struct blame_entry *ent,
 +                         struct progress_info *pi)
  {
        if (incremental) {
                struct origin *suspect = ent->suspect;
  
                printf("%s %d %d %d\n",
 -                     sha1_to_hex(suspect->commit->object.sha1),
 +                     oid_to_hex(&suspect->commit->object.oid),
                       ent->s_lno + 1, ent->lno + 1, ent->num_lines);
                emit_one_suspect_detail(suspect, 0);
                write_filename_info(suspect->path);
                maybe_flush_or_die(stdout, "stdout");
        }
 +      pi->blamed_lines += ent->num_lines;
 +      display_progress(pi->progress, pi->blamed_lines);
  }
  
  /*
@@@ -1775,11 -1767,6 +1775,11 @@@ static void assign_blame(struct scorebo
  {
        struct rev_info *revs = sb->revs;
        struct commit *commit = prio_queue_get(&sb->commits);
 +      struct progress_info pi = { NULL, 0 };
 +
 +      if (show_progress)
 +              pi.progress = start_progress_delay(_("Blaming lines"),
 +                                                 sb->num_lines, 50, 1);
  
        while (commit) {
                struct blame_entry *ent;
                        suspect->guilty = 1;
                        for (;;) {
                                struct blame_entry *next = ent->next;
 -                              found_guilty_entry(ent);
 +                              found_guilty_entry(ent, &pi);
                                if (next) {
                                        ent = next;
                                        continue;
                if (DEBUG) /* sanity */
                        sanity_check_refcnt(sb);
        }
 +
 +      stop_progress(&pi.progress);
  }
  
  static const char *format_time(unsigned long time, const char *tz_str,
@@@ -1894,9 -1879,9 +1894,9 @@@ static void emit_porcelain(struct score
        int cnt;
        const char *cp;
        struct origin *suspect = ent->suspect;
 -      char hex[41];
 +      char hex[GIT_SHA1_HEXSZ + 1];
  
 -      strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
 +      sha1_to_hex_r(hex, suspect->commit->object.oid.hash);
        printf("%s %d %d %d\n",
               hex,
               ent->s_lno + 1,
@@@ -1932,11 -1917,11 +1932,11 @@@ static void emit_other(struct scoreboar
        const char *cp;
        struct origin *suspect = ent->suspect;
        struct commit_info ci;
 -      char hex[41];
 +      char hex[GIT_SHA1_HEXSZ + 1];
        int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
  
        get_commit_info(suspect->commit, &ci, 1);
 -      strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
 +      sha1_to_hex_r(hex, suspect->commit->object.oid.hash);
  
        cp = nth_line(sb, ent->lno);
        for (cnt = 0; cnt < ent->num_lines; cnt++) {
@@@ -2056,8 -2041,7 +2056,8 @@@ static int prepare_lines(struct scorebo
        for (p = buf; p < end; p = get_next_line(p, end))
                num++;
  
 -      sb->lineno = lineno = xmalloc(sizeof(*sb->lineno) * (num + 1));
 +      ALLOC_ARRAY(sb->lineno, num + 1);
 +      lineno = sb->lineno;
  
        for (p = buf; p < end; p = get_next_line(p, end))
                *lineno++ = p - buf;
@@@ -2092,7 -2076,7 +2092,7 @@@ static int read_ancestry(const char *gr
  
  static int update_auto_abbrev(int auto_abbrev, struct origin *suspect)
  {
 -      const char *uniq = find_unique_abbrev(suspect->commit->object.sha1,
 +      const char *uniq = find_unique_abbrev(suspect->commit->object.oid.hash,
                                              auto_abbrev);
        int len = strlen(uniq);
        if (auto_abbrev < len)
@@@ -2168,7 -2152,7 +2168,7 @@@ static void sanity_check_refcnt(struct 
                if (ent->suspect->refcnt <= 0) {
                        fprintf(stderr, "%s in %s has negative refcnt %d\n",
                                ent->suspect->path,
 -                              sha1_to_hex(ent->suspect->commit->object.sha1),
 +                              oid_to_hex(&ent->suspect->commit->object.oid),
                                ent->suspect->refcnt);
                        baa = 1;
                }
@@@ -2229,10 -2213,9 +2229,10 @@@ static int git_blame_config(const char 
  static void verify_working_tree_path(struct commit *work_tree, const char *path)
  {
        struct commit_list *parents;
 +      int pos;
  
        for (parents = work_tree->parents; parents; parents = parents->next) {
 -              const unsigned char *commit_sha1 = parents->item->object.sha1;
 +              const unsigned char *commit_sha1 = parents->item->object.oid.hash;
                unsigned char blob_sha1[20];
                unsigned mode;
  
                    sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
                        return;
        }
 -      die("no such path '%s' in HEAD", path);
 +
 +      pos = cache_name_pos(path, strlen(path));
 +      if (pos >= 0)
 +              ; /* path is in the index */
 +      else if (!strcmp(active_cache[-1 - pos]->name, path))
 +              ; /* path is in the index, unmerged */
 +      else
 +              die("no such path '%s' in HEAD", path);
  }
  
  static struct commit_list **append_parent(struct commit_list **tail, const unsigned char *sha1)
@@@ -2314,7 -2290,6 +2314,7 @@@ static struct commit *fake_working_tree
        unsigned mode;
        struct strbuf msg = STRBUF_INIT;
  
 +      read_cache();
        time(&now);
        commit = alloc_commit_node();
        commit->object.parsed = 1;
        strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n");
        for (parent = commit->parents; parent; parent = parent->next)
                strbuf_addf(&msg, "parent %s\n",
 -                          sha1_to_hex(parent->item->object.sha1));
 +                          oid_to_hex(&parent->item->object.oid));
        strbuf_addf(&msg,
                    "author %s\n"
                    "committer %s\n\n"
        ce->ce_mode = create_ce_mode(mode);
        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
  
 -      /*
 -       * We are not going to write this out, so this does not matter
 -       * right now, but someday we might optimize diff-index --cached
 -       * with cache-tree information.
 -       */
        cache_tree_invalidate_path(&the_index, path);
  
        return commit;
  }
  
 -static char *prepare_final(struct scoreboard *sb)
 +static struct commit *find_single_final(struct rev_info *revs,
 +                                      const char **name_p)
  {
        int i;
 -      const char *final_commit_name = NULL;
 -      struct rev_info *revs = sb->revs;
 +      struct commit *found = NULL;
 +      const char *name = NULL;
  
 -      /*
 -       * There must be one and only one positive commit in the
 -       * revs->pending array.
 -       */
        for (i = 0; i < revs->pending.nr; i++) {
                struct object *obj = revs->pending.objects[i].item;
                if (obj->flags & UNINTERESTING)
                        continue;
 -              while (obj->type == OBJ_TAG)
 -                      obj = deref_tag(obj, NULL, 0);
 +              obj = deref_tag(obj, NULL, 0);
                if (obj->type != OBJ_COMMIT)
                        die("Non commit %s?", revs->pending.objects[i].name);
 -              if (sb->final)
 +              if (found)
                        die("More than one commit to dig from %s and %s?",
 -                          revs->pending.objects[i].name,
 -                          final_commit_name);
 -              sb->final = (struct commit *) obj;
 -              final_commit_name = revs->pending.objects[i].name;
 +                          revs->pending.objects[i].name, name);
 +              found = (struct commit *)obj;
 +              name = revs->pending.objects[i].name;
        }
 -      return xstrdup_or_null(final_commit_name);
 +      if (name_p)
 +              *name_p = name;
 +      return found;
 +}
 +
 +static char *prepare_final(struct scoreboard *sb)
 +{
 +      const char *name;
 +      sb->final = find_single_final(sb->revs, &name);
 +      return xstrdup_or_null(name);
  }
  
  static char *prepare_initial(struct scoreboard *sb)
                struct object *obj = revs->pending.objects[i].item;
                if (!(obj->flags & UNINTERESTING))
                        continue;
 -              while (obj->type == OBJ_TAG)
 -                      obj = deref_tag(obj, NULL, 0);
 +              obj = deref_tag(obj, NULL, 0);
                if (obj->type != OBJ_COMMIT)
                        die("Non commit %s?", revs->pending.objects[i].name);
                if (sb->final)
@@@ -2525,7 -2502,6 +2525,7 @@@ int cmd_blame(int argc, const char **ar
        long dashdash_pos, lno;
        char *final_commit_name = NULL;
        enum object_type type;
 +      struct commit *final_commit = NULL;
  
        struct string_list range_list = STRING_LIST_INIT_NODUP;
        int output_option = 0, opt = 0;
                OPT_BOOL('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")),
                OPT_BOOL(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")),
                OPT_BOOL(0, "show-stats", &show_stats, N_("Show work cost statistics")),
 +              OPT_BOOL(0, "progress", &show_progress, N_("Force progress reporting")),
                OPT_BIT(0, "score-debug", &output_option, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE),
                OPT_BIT('f', "show-name", &output_option, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME),
                OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER),
  
        save_commit_buffer = 0;
        dashdash_pos = 0;
 +      show_progress = -1;
  
        parse_options_start(&ctx, argc, argv, prefix, options,
                            PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
@@@ -2598,13 -2572,6 +2598,13 @@@ parse_done
        DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES);
        argc = parse_options_end(&ctx);
  
 +      if (incremental || (output_option & OUTPUT_PORCELAIN)) {
 +              if (show_progress > 0)
 +                      die("--progress can't be used with --incremental or porcelain formats");
 +              show_progress = 0;
 +      } else if (show_progress < 0)
 +              show_progress = isatty(2);
 +
        if (0 < abbrev)
                /* one more abbrev length is needed for the boundary commit */
                abbrev++;
        case DATE_RAW:
                blame_date_width = sizeof("1161298804 -0700");
                break;
 +      case DATE_UNIX:
 +              blame_date_width = sizeof("1161298804");
 +              break;
        case DATE_SHORT:
                blame_date_width = sizeof("2006-10-19");
                break;
                   fewer display columns. */
                blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
                break;
 -      case DATE_LOCAL:
        case DATE_NORMAL:
                blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
                break;
        }
        else if (contents_from)
                die("--contents and --reverse do not blend well.");
 -      else if (revs.first_parent_only)
 -              die("combining --first-parent and --reverse is not supported");
        else {
                final_commit_name = prepare_initial(&sb);
                sb.commits.compare = compare_commits_by_reverse_commit_date;
 +              if (revs.first_parent_only)
 +                      revs.children.name = NULL;
        }
  
        if (!sb.final) {
        else if (contents_from)
                die("Cannot use --contents with final commit object name");
  
 +      if (reverse && revs.first_parent_only) {
 +              final_commit = find_single_final(sb.revs, NULL);
 +              if (!final_commit)
 +                      die("--reverse and --first-parent together require specified latest commit");
 +      }
 +
        /*
         * If we have bottom, this will mark the ancestors of the
         * bottom commits we would reach while traversing as
        if (prepare_revision_walk(&revs))
                die(_("revision walk setup failed"));
  
 -      if (is_null_sha1(sb.final->object.sha1)) {
 +      if (reverse && revs.first_parent_only) {
 +              struct commit *c = final_commit;
 +
 +              sb.revs->children.name = "children";
 +              while (c->parents &&
 +                     oidcmp(&c->object.oid, &sb.final->object.oid)) {
 +                      struct commit_list *l = xcalloc(1, sizeof(*l));
 +
 +                      l->item = c;
 +                      if (add_decoration(&sb.revs->children,
 +                                         &c->parents->item->object, l))
 +                              die("BUG: not unique item in first-parent chain");
 +                      c = c->parents->item;
 +              }
 +
 +              if (oidcmp(&c->object.oid, &sb.final->object.oid))
 +                      die("--reverse --first-parent together require range along first-parent chain");
 +      }
 +
 +      if (is_null_oid(&sb.final->object.oid)) {
                o = sb.final->util;
                sb.final_buf = xmemdupz(o->file.ptr, o->file.size);
                sb.final_buf_size = o->file.size;
        lno = prepare_lines(&sb);
  
        if (lno && !range_list.nr)
-               string_list_append(&range_list, xstrdup("1"));
+               string_list_append(&range_list, "1");
  
        anchor = 1;
        range_set_init(&ranges, range_list.nr);
  
        read_mailmap(&mailmap, NULL);
  
 +      assign_blame(&sb, opt);
 +
        if (!incremental)
                setup_pager();
  
 -      assign_blame(&sb, opt);
 -
        free(final_commit_name);
  
        if (incremental)