Merge branch 'rs/use-strbuf-add-unique-abbrev'
authorJunio C Hamano <gitster@pobox.com>
Fri, 12 Aug 2016 16:47:37 +0000 (09:47 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 12 Aug 2016 16:47:37 +0000 (09:47 -0700)
A small code clean-up.

* rs/use-strbuf-add-unique-abbrev:
use strbuf_add_unique_abbrev() for adding short hashes

1  2 
builtin/checkout.c
pretty.c
transport.c
diff --combined builtin/checkout.c
index 8d852d458514ca8d2e9553bec6a818b140b0e478,fa08b73c0f57d0453fe7054586d12012d4267168..48661115226b32ed6e98477ee49eea1278685def
@@@ -276,7 -276,7 +276,7 @@@ static int checkout_paths(const struct 
  
        hold_locked_index(lock_file, 1);
        if (read_cache_preload(&opts->pathspec) < 0)
 -              return error(_("corrupt index file"));
 +              return error(_("index file corrupt"));
  
        if (opts->source_tree)
                read_tree_some(opts->source_tree, &opts->pathspec);
@@@ -470,7 -470,7 +470,7 @@@ static int merge_working_tree(const str
  
        hold_locked_index(lock_file, 1);
        if (read_cache_preload(NULL) < 0)
 -              return error(_("corrupt index file"));
 +              return error(_("index file corrupt"));
  
        resolve_undo_clear();
        if (opts->force) {
                        o.ancestor = old->name;
                        o.branch1 = new->name;
                        o.branch2 = "local";
 -                      merge_trees(&o, new->commit->tree, work,
 +                      ret = merge_trees(&o, new->commit->tree, work,
                                old->commit->tree, &result);
 +                      if (ret < 0)
 +                              exit(128);
                        ret = reset_tree(new->commit->tree, opts, 0,
                                         writeout_error);
 +                      strbuf_release(&o.obuf);
                        if (ret)
                                return ret;
                }
@@@ -706,8 -703,7 +706,7 @@@ static int add_pending_uninteresting_re
  static void describe_one_orphan(struct strbuf *sb, struct commit *commit)
  {
        strbuf_addstr(sb, "  ");
-       strbuf_addstr(sb,
-               find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV));
+       strbuf_add_unique_abbrev(sb, commit->object.oid.hash, DEFAULT_ABBREV);
        strbuf_addch(sb, ' ');
        if (!parse_commit(commit))
                pp_commit_easy(CMIT_FMT_ONELINE, commit, sb);
@@@ -1141,7 -1137,7 +1140,7 @@@ int cmd_checkout(int argc, const char *
                OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"),
                           N_("create/reset and checkout a branch")),
                OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")),
 -              OPT_BOOL(0, "detach", &opts.force_detach, N_("detach the HEAD at named commit")),
 +              OPT_BOOL(0, "detach", &opts.force_detach, N_("detach HEAD at named commit")),
                OPT_SET_INT('t', "track",  &opts.track, N_("set upstream info for new branch"),
                        BRANCH_TRACK_EXPLICIT),
                OPT_STRING(0, "orphan", &opts.new_orphan_branch, N_("new-branch"), N_("new unparented branch")),
diff --combined pretty.c
index 9fa42c2b4e3cd55fe8ec03248bc19780574bf961,dede7de7099e17feb7193d05bd1676d534fc935f..9609afb510e2486ae857501a160a9234be87661f
+++ b/pretty.c
@@@ -92,7 -92,6 +92,7 @@@ static void setup_commit_formats(void
                { "medium",     CMIT_FMT_MEDIUM,        0,      8 },
                { "short",      CMIT_FMT_SHORT,         0,      0 },
                { "email",      CMIT_FMT_EMAIL,         0,      0 },
 +              { "mboxrd",     CMIT_FMT_MBOXRD,        0,      0 },
                { "fuller",     CMIT_FMT_FULLER,        0,      8 },
                { "full",       CMIT_FMT_FULL,          0,      8 },
                { "oneline",    CMIT_FMT_ONELINE,       1,      0 }
@@@ -445,7 -444,7 +445,7 @@@ void pp_user_info(struct pretty_print_c
        if (pp->mailmap)
                map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
  
 -      if (pp->fmt == CMIT_FMT_EMAIL) {
 +      if (cmit_fmt_is_mail(pp->fmt)) {
                if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) {
                        struct strbuf buf = STRBUF_INIT;
  
                            show_ident_date(&ident, &pp->date_mode));
                break;
        case CMIT_FMT_EMAIL:
 +      case CMIT_FMT_MBOXRD:
                strbuf_addf(sb, "Date: %s\n",
                            show_ident_date(&ident, DATE_MODE(RFC2822)));
                break;
@@@ -537,7 -535,7 +537,7 @@@ static void add_merge_info(const struc
  {
        struct commit_list *parent = commit->parents;
  
 -      if ((pp->fmt == CMIT_FMT_ONELINE) || (pp->fmt == CMIT_FMT_EMAIL) ||
 +      if ((pp->fmt == CMIT_FMT_ONELINE) || (cmit_fmt_is_mail(pp->fmt)) ||
            !parent || !parent->next)
                return;
  
@@@ -1143,8 -1141,8 +1143,8 @@@ static size_t format_commit_one(struct 
                        strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
                        return 1;
                }
-               strbuf_addstr(sb, find_unique_abbrev(commit->object.oid.hash,
-                                                    c->pretty_ctx->abbrev));
+               strbuf_add_unique_abbrev(sb, commit->object.oid.hash,
+                                        c->pretty_ctx->abbrev);
                strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
                c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
                return 1;
        case 't':               /* abbreviated tree hash */
                if (add_again(sb, &c->abbrev_tree_hash))
                        return 1;
-               strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.oid.hash,
-                                                    c->pretty_ctx->abbrev));
+               strbuf_add_unique_abbrev(sb, commit->tree->object.oid.hash,
+                                        c->pretty_ctx->abbrev);
                c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
                return 1;
        case 'P':               /* parent hashes */
                for (p = commit->parents; p; p = p->next) {
                        if (p != commit->parents)
                                strbuf_addch(sb, ' ');
-                       strbuf_addstr(sb, find_unique_abbrev(
-                                       p->item->object.oid.hash,
-                                       c->pretty_ctx->abbrev));
+                       strbuf_add_unique_abbrev(sb, p->item->object.oid.hash,
+                                                c->pretty_ctx->abbrev);
                }
                c->abbrev_parent_hashes.len = sb->len -
                                              c->abbrev_parent_hashes.off;
@@@ -1623,7 -1620,7 +1622,7 @@@ void pp_title_line(struct pretty_print_
        if (pp->after_subject) {
                strbuf_addstr(sb, pp->after_subject);
        }
 -      if (pp->fmt == CMIT_FMT_EMAIL) {
 +      if (cmit_fmt_is_mail(pp->fmt)) {
                strbuf_addch(sb, '\n');
        }
  
@@@ -1706,16 -1703,6 +1705,16 @@@ static void pp_handle_indent(struct pre
                strbuf_add(sb, line, linelen);
  }
  
 +static int is_mboxrd_from(const char *line, int len)
 +{
 +      /*
 +       * a line matching /^From $/ here would only have len == 4
 +       * at this point because is_empty_line would've trimmed all
 +       * trailing space
 +       */
 +      return len > 4 && starts_with(line + strspn(line, ">"), "From ");
 +}
 +
  void pp_remainder(struct pretty_print_context *pp,
                  const char **msg_p,
                  struct strbuf *sb,
                else if (pp->expand_tabs_in_log)
                        strbuf_add_tabexpand(sb, pp->expand_tabs_in_log,
                                             line, linelen);
 -              else
 +              else {
 +                      if (pp->fmt == CMIT_FMT_MBOXRD &&
 +                                      is_mboxrd_from(line, linelen))
 +                              strbuf_addch(sb, '>');
 +
                        strbuf_add(sb, line, linelen);
 +              }
                strbuf_addch(sb, '\n');
        }
  }
@@@ -1774,14 -1756,14 +1773,14 @@@ void pretty_print_commit(struct pretty_
        encoding = get_log_output_encoding();
        msg = reencoded = logmsg_reencode(commit, NULL, encoding);
  
 -      if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
 +      if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
                indent = 0;
  
        /*
         * We need to check and emit Content-type: to mark it
         * as 8-bit if we haven't done so.
         */
 -      if (pp->fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
 +      if (cmit_fmt_is_mail(pp->fmt) && need_8bit_cte == 0) {
                int i, ch, in_body;
  
                for (in_body = i = 0; (ch = msg[i]); i++) {
        msg = skip_blank_lines(msg);
  
        /* These formats treat the title line specially. */
 -      if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
 +      if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
                pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
  
        beginning_of_body = sb->len;
         * format.  Make sure we did not strip the blank line
         * between the header and the body.
         */
 -      if (pp->fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
 +      if (cmit_fmt_is_mail(pp->fmt) && sb->len <= beginning_of_body)
                strbuf_addch(sb, '\n');
  
        unuse_commit_buffer(commit, reencoded);
diff --combined transport.c
index c5772a14eeec3d5651796c8051443dd76583c05d,7ef84b9820f3354031c4fa1d2218f8936d42ef10..cf8de6e888fc5ce577c7319fce089245d6c36be3
@@@ -59,7 -59,7 +59,7 @@@ static void set_upstreams(struct transp
                                localname + 11, transport->remote->name,
                                remotename);
                else
 -                      printf("Would set upstream of '%s' to '%s' of '%s'\n",
 +                      printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
                                localname + 11, remotename + 11,
                                transport->remote->name);
        }
@@@ -148,7 -148,7 +148,7 @@@ static int set_git_option(struct git_tr
                        char *end;
                        opts->depth = strtol(value, &end, 0);
                        if (*end)
 -                              die("transport: invalid depth option '%s'", value);
 +                              die(_("transport: invalid depth option '%s'"), value);
                }
                return 0;
        }
@@@ -321,11 -321,6 +321,6 @@@ static void print_ref_status(char flag
        }
  }
  
- static const char *status_abbrev(unsigned char sha1[20])
- {
-       return find_unique_abbrev(sha1, DEFAULT_ABBREV);
- }
  static void print_ok_ref_status(struct ref *ref, int porcelain)
  {
        if (ref->deletion)
                char type;
                const char *msg;
  
-               strbuf_addstr(&quickref, status_abbrev(ref->old_oid.hash));
+               strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash,
+                                        DEFAULT_ABBREV);
                if (ref->forced_update) {
                        strbuf_addstr(&quickref, "...");
                        type = '+';
                        type = ' ';
                        msg = NULL;
                }
-               strbuf_addstr(&quickref, status_abbrev(ref->new_oid.hash));
+               strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
+                                        DEFAULT_ABBREV);
  
                print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
                strbuf_release(&quickref);
  
  static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
  {
 -      if (!count)
 -              fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
 +      if (!count) {
 +              char *url = transport_anonymize_url(dest);
 +              fprintf(porcelain ? stdout : stderr, "To %s\n", url);
 +              free(url);
 +      }
  
        switch(ref->status) {
        case REF_STATUS_NONE:
@@@ -513,7 -507,6 +510,7 @@@ static int git_transport_push(struct tr
        args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
        args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
        args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
 +      args.push_options = transport->push_options;
        args.url = transport->url;
  
        if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
@@@ -567,7 -560,7 +564,7 @@@ void transport_take_over(struct transpo
        struct git_transport_data *data;
  
        if (!transport->smart_options)
 -              die("Bug detected: Taking over transport requires non-NULL "
 +              die("BUG: taking over transport requires non-NULL "
                    "smart_options field.");
  
        data = xcalloc(1, sizeof(*data));
@@@ -771,19 -764,19 +768,19 @@@ static void die_with_unpushed_submodule
  {
        int i;
  
 -      fprintf(stderr, "The following submodule paths contain changes that can\n"
 -                      "not be found on any remote:\n");
 +      fprintf(stderr, _("The following submodule paths contain changes that can\n"
 +                      "not be found on any remote:\n"));
        for (i = 0; i < needs_pushing->nr; i++)
                printf("  %s\n", needs_pushing->items[i].string);
 -      fprintf(stderr, "\nPlease try\n\n"
 -                      "       git push --recurse-submodules=on-demand\n\n"
 -                      "or cd to the path and use\n\n"
 -                      "       git push\n\n"
 -                      "to push them to a remote.\n\n");
 +      fprintf(stderr, _("\nPlease try\n\n"
 +                        "     git push --recurse-submodules=on-demand\n\n"
 +                        "or cd to the path and use\n\n"
 +                        "     git push\n\n"
 +                        "to push them to a remote.\n\n"));
  
        string_list_clear(needs_pushing, 0);
  
 -      die("Aborting.");
 +      die(_("Aborting."));
  }
  
  static int run_pre_push_hook(struct transport *transport,