builtin-for-each-ref.con commit blame: show "previous" information in --porcelain/--incremental format (96e1170)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "refs.h"
   4#include "object.h"
   5#include "tag.h"
   6#include "commit.h"
   7#include "tree.h"
   8#include "blob.h"
   9#include "quote.h"
  10#include "parse-options.h"
  11
  12/* Quoting styles */
  13#define QUOTE_NONE 0
  14#define QUOTE_SHELL 1
  15#define QUOTE_PERL 2
  16#define QUOTE_PYTHON 4
  17#define QUOTE_TCL 8
  18
  19typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
  20
  21struct atom_value {
  22        const char *s;
  23        unsigned long ul; /* used for sorting when not FIELD_STR */
  24};
  25
  26struct ref_sort {
  27        struct ref_sort *next;
  28        int atom; /* index into used_atom array */
  29        unsigned reverse : 1;
  30};
  31
  32struct refinfo {
  33        char *refname;
  34        unsigned char objectname[20];
  35        struct atom_value *value;
  36};
  37
  38static struct {
  39        const char *name;
  40        cmp_type cmp_type;
  41} valid_atom[] = {
  42        { "refname" },
  43        { "objecttype" },
  44        { "objectsize", FIELD_ULONG },
  45        { "objectname" },
  46        { "tree" },
  47        { "parent" },
  48        { "numparent", FIELD_ULONG },
  49        { "object" },
  50        { "type" },
  51        { "tag" },
  52        { "author" },
  53        { "authorname" },
  54        { "authoremail" },
  55        { "authordate", FIELD_TIME },
  56        { "committer" },
  57        { "committername" },
  58        { "committeremail" },
  59        { "committerdate", FIELD_TIME },
  60        { "tagger" },
  61        { "taggername" },
  62        { "taggeremail" },
  63        { "taggerdate", FIELD_TIME },
  64        { "creator" },
  65        { "creatordate", FIELD_TIME },
  66        { "subject" },
  67        { "body" },
  68        { "contents" },
  69};
  70
  71/*
  72 * An atom is a valid field atom listed above, possibly prefixed with
  73 * a "*" to denote deref_tag().
  74 *
  75 * We parse given format string and sort specifiers, and make a list
  76 * of properties that we need to extract out of objects.  refinfo
  77 * structure will hold an array of values extracted that can be
  78 * indexed with the "atom number", which is an index into this
  79 * array.
  80 */
  81static const char **used_atom;
  82static cmp_type *used_atom_type;
  83static int used_atom_cnt, sort_atom_limit, need_tagged;
  84
  85/*
  86 * Used to parse format string and sort specifiers
  87 */
  88static int parse_atom(const char *atom, const char *ep)
  89{
  90        const char *sp;
  91        int i, at;
  92
  93        sp = atom;
  94        if (*sp == '*' && sp < ep)
  95                sp++; /* deref */
  96        if (ep <= sp)
  97                die("malformed field name: %.*s", (int)(ep-atom), atom);
  98
  99        /* Do we have the atom already used elsewhere? */
 100        for (i = 0; i < used_atom_cnt; i++) {
 101                int len = strlen(used_atom[i]);
 102                if (len == ep - atom && !memcmp(used_atom[i], atom, len))
 103                        return i;
 104        }
 105
 106        /* Is the atom a valid one? */
 107        for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
 108                int len = strlen(valid_atom[i].name);
 109                /*
 110                 * If the atom name has a colon, strip it and everything after
 111                 * it off - it specifies the format for this entry, and
 112                 * shouldn't be used for checking against the valid_atom
 113                 * table.
 114                 */
 115                const char *formatp = strchr(sp, ':');
 116                if (!formatp || ep < formatp)
 117                        formatp = ep;
 118                if (len == formatp - sp && !memcmp(valid_atom[i].name, sp, len))
 119                        break;
 120        }
 121
 122        if (ARRAY_SIZE(valid_atom) <= i)
 123                die("unknown field name: %.*s", (int)(ep-atom), atom);
 124
 125        /* Add it in, including the deref prefix */
 126        at = used_atom_cnt;
 127        used_atom_cnt++;
 128        used_atom = xrealloc(used_atom,
 129                             (sizeof *used_atom) * used_atom_cnt);
 130        used_atom_type = xrealloc(used_atom_type,
 131                                  (sizeof(*used_atom_type) * used_atom_cnt));
 132        used_atom[at] = xmemdupz(atom, ep - atom);
 133        used_atom_type[at] = valid_atom[i].cmp_type;
 134        return at;
 135}
 136
 137/*
 138 * In a format string, find the next occurrence of %(atom).
 139 */
 140static const char *find_next(const char *cp)
 141{
 142        while (*cp) {
 143                if (*cp == '%') {
 144                        /* %( is the start of an atom;
 145                         * %% is a quoted per-cent.
 146                         */
 147                        if (cp[1] == '(')
 148                                return cp;
 149                        else if (cp[1] == '%')
 150                                cp++; /* skip over two % */
 151                        /* otherwise this is a singleton, literal % */
 152                }
 153                cp++;
 154        }
 155        return NULL;
 156}
 157
 158/*
 159 * Make sure the format string is well formed, and parse out
 160 * the used atoms.
 161 */
 162static int verify_format(const char *format)
 163{
 164        const char *cp, *sp;
 165        for (cp = format; *cp && (sp = find_next(cp)); ) {
 166                const char *ep = strchr(sp, ')');
 167                if (!ep)
 168                        return error("malformed format string %s", sp);
 169                /* sp points at "%(" and ep points at the closing ")" */
 170                parse_atom(sp + 2, ep);
 171                cp = ep + 1;
 172        }
 173        return 0;
 174}
 175
 176/*
 177 * Given an object name, read the object data and size, and return a
 178 * "struct object".  If the object data we are returning is also borrowed
 179 * by the "struct object" representation, set *eaten as well---it is a
 180 * signal from parse_object_buffer to us not to free the buffer.
 181 */
 182static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
 183{
 184        enum object_type type;
 185        void *buf = read_sha1_file(sha1, &type, sz);
 186
 187        if (buf)
 188                *obj = parse_object_buffer(sha1, type, *sz, buf, eaten);
 189        else
 190                *obj = NULL;
 191        return buf;
 192}
 193
 194/* See grab_values */
 195static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
 196{
 197        int i;
 198
 199        for (i = 0; i < used_atom_cnt; i++) {
 200                const char *name = used_atom[i];
 201                struct atom_value *v = &val[i];
 202                if (!!deref != (*name == '*'))
 203                        continue;
 204                if (deref)
 205                        name++;
 206                if (!strcmp(name, "objecttype"))
 207                        v->s = typename(obj->type);
 208                else if (!strcmp(name, "objectsize")) {
 209                        char *s = xmalloc(40);
 210                        sprintf(s, "%lu", sz);
 211                        v->ul = sz;
 212                        v->s = s;
 213                }
 214                else if (!strcmp(name, "objectname")) {
 215                        char *s = xmalloc(41);
 216                        strcpy(s, sha1_to_hex(obj->sha1));
 217                        v->s = s;
 218                }
 219        }
 220}
 221
 222/* See grab_values */
 223static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
 224{
 225        int i;
 226        struct tag *tag = (struct tag *) obj;
 227
 228        for (i = 0; i < used_atom_cnt; i++) {
 229                const char *name = used_atom[i];
 230                struct atom_value *v = &val[i];
 231                if (!!deref != (*name == '*'))
 232                        continue;
 233                if (deref)
 234                        name++;
 235                if (!strcmp(name, "tag"))
 236                        v->s = tag->tag;
 237                else if (!strcmp(name, "type") && tag->tagged)
 238                        v->s = typename(tag->tagged->type);
 239                else if (!strcmp(name, "object") && tag->tagged) {
 240                        char *s = xmalloc(41);
 241                        strcpy(s, sha1_to_hex(tag->tagged->sha1));
 242                        v->s = s;
 243                }
 244        }
 245}
 246
 247static int num_parents(struct commit *commit)
 248{
 249        struct commit_list *parents;
 250        int i;
 251
 252        for (i = 0, parents = commit->parents;
 253             parents;
 254             parents = parents->next)
 255                i++;
 256        return i;
 257}
 258
 259/* See grab_values */
 260static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
 261{
 262        int i;
 263        struct commit *commit = (struct commit *) obj;
 264
 265        for (i = 0; i < used_atom_cnt; i++) {
 266                const char *name = used_atom[i];
 267                struct atom_value *v = &val[i];
 268                if (!!deref != (*name == '*'))
 269                        continue;
 270                if (deref)
 271                        name++;
 272                if (!strcmp(name, "tree")) {
 273                        char *s = xmalloc(41);
 274                        strcpy(s, sha1_to_hex(commit->tree->object.sha1));
 275                        v->s = s;
 276                }
 277                if (!strcmp(name, "numparent")) {
 278                        char *s = xmalloc(40);
 279                        v->ul = num_parents(commit);
 280                        sprintf(s, "%lu", v->ul);
 281                        v->s = s;
 282                }
 283                else if (!strcmp(name, "parent")) {
 284                        int num = num_parents(commit);
 285                        int i;
 286                        struct commit_list *parents;
 287                        char *s = xmalloc(41 * num + 1);
 288                        v->s = s;
 289                        for (i = 0, parents = commit->parents;
 290                             parents;
 291                             parents = parents->next, i = i + 41) {
 292                                struct commit *parent = parents->item;
 293                                strcpy(s+i, sha1_to_hex(parent->object.sha1));
 294                                if (parents->next)
 295                                        s[i+40] = ' ';
 296                        }
 297                        if (!i)
 298                                *s = '\0';
 299                }
 300        }
 301}
 302
 303static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz)
 304{
 305        const char *eol;
 306        while (*buf) {
 307                if (!strncmp(buf, who, wholen) &&
 308                    buf[wholen] == ' ')
 309                        return buf + wholen + 1;
 310                eol = strchr(buf, '\n');
 311                if (!eol)
 312                        return "";
 313                eol++;
 314                if (*eol == '\n')
 315                        return ""; /* end of header */
 316                buf = eol;
 317        }
 318        return "";
 319}
 320
 321static const char *copy_line(const char *buf)
 322{
 323        const char *eol = strchrnul(buf, '\n');
 324        return xmemdupz(buf, eol - buf);
 325}
 326
 327static const char *copy_name(const char *buf)
 328{
 329        const char *cp;
 330        for (cp = buf; *cp && *cp != '\n'; cp++) {
 331                if (!strncmp(cp, " <", 2))
 332                        return xmemdupz(buf, cp - buf);
 333        }
 334        return "";
 335}
 336
 337static const char *copy_email(const char *buf)
 338{
 339        const char *email = strchr(buf, '<');
 340        const char *eoemail = strchr(email, '>');
 341        if (!email || !eoemail)
 342                return "";
 343        return xmemdupz(email, eoemail + 1 - email);
 344}
 345
 346static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
 347{
 348        const char *eoemail = strstr(buf, "> ");
 349        char *zone;
 350        unsigned long timestamp;
 351        long tz;
 352        enum date_mode date_mode = DATE_NORMAL;
 353        const char *formatp;
 354
 355        /*
 356         * We got here because atomname ends in "date" or "date<something>";
 357         * it's not possible that <something> is not ":<format>" because
 358         * parse_atom() wouldn't have allowed it, so we can assume that no
 359         * ":" means no format is specified, and use the default.
 360         */
 361        formatp = strchr(atomname, ':');
 362        if (formatp != NULL) {
 363                formatp++;
 364                date_mode = parse_date_format(formatp);
 365        }
 366
 367        if (!eoemail)
 368                goto bad;
 369        timestamp = strtoul(eoemail + 2, &zone, 10);
 370        if (timestamp == ULONG_MAX)
 371                goto bad;
 372        tz = strtol(zone, NULL, 10);
 373        if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
 374                goto bad;
 375        v->s = xstrdup(show_date(timestamp, tz, date_mode));
 376        v->ul = timestamp;
 377        return;
 378 bad:
 379        v->s = "";
 380        v->ul = 0;
 381}
 382
 383/* See grab_values */
 384static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
 385{
 386        int i;
 387        int wholen = strlen(who);
 388        const char *wholine = NULL;
 389
 390        for (i = 0; i < used_atom_cnt; i++) {
 391                const char *name = used_atom[i];
 392                struct atom_value *v = &val[i];
 393                if (!!deref != (*name == '*'))
 394                        continue;
 395                if (deref)
 396                        name++;
 397                if (strncmp(who, name, wholen))
 398                        continue;
 399                if (name[wholen] != 0 &&
 400                    strcmp(name + wholen, "name") &&
 401                    strcmp(name + wholen, "email") &&
 402                    prefixcmp(name + wholen, "date"))
 403                        continue;
 404                if (!wholine)
 405                        wholine = find_wholine(who, wholen, buf, sz);
 406                if (!wholine)
 407                        return; /* no point looking for it */
 408                if (name[wholen] == 0)
 409                        v->s = copy_line(wholine);
 410                else if (!strcmp(name + wholen, "name"))
 411                        v->s = copy_name(wholine);
 412                else if (!strcmp(name + wholen, "email"))
 413                        v->s = copy_email(wholine);
 414                else if (!prefixcmp(name + wholen, "date"))
 415                        grab_date(wholine, v, name);
 416        }
 417
 418        /* For a tag or a commit object, if "creator" or "creatordate" is
 419         * requested, do something special.
 420         */
 421        if (strcmp(who, "tagger") && strcmp(who, "committer"))
 422                return; /* "author" for commit object is not wanted */
 423        if (!wholine)
 424                wholine = find_wholine(who, wholen, buf, sz);
 425        if (!wholine)
 426                return;
 427        for (i = 0; i < used_atom_cnt; i++) {
 428                const char *name = used_atom[i];
 429                struct atom_value *v = &val[i];
 430                if (!!deref != (*name == '*'))
 431                        continue;
 432                if (deref)
 433                        name++;
 434
 435                if (!prefixcmp(name, "creatordate"))
 436                        grab_date(wholine, v, name);
 437                else if (!strcmp(name, "creator"))
 438                        v->s = copy_line(wholine);
 439        }
 440}
 441
 442static void find_subpos(const char *buf, unsigned long sz, const char **sub, const char **body)
 443{
 444        while (*buf) {
 445                const char *eol = strchr(buf, '\n');
 446                if (!eol)
 447                        return;
 448                if (eol[1] == '\n') {
 449                        buf = eol + 1;
 450                        break; /* found end of header */
 451                }
 452                buf = eol + 1;
 453        }
 454        while (*buf == '\n')
 455                buf++;
 456        if (!*buf)
 457                return;
 458        *sub = buf; /* first non-empty line */
 459        buf = strchr(buf, '\n');
 460        if (!buf) {
 461                *body = "";
 462                return; /* no body */
 463        }
 464        while (*buf == '\n')
 465                buf++; /* skip blank between subject and body */
 466        *body = buf;
 467}
 468
 469/* See grab_values */
 470static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
 471{
 472        int i;
 473        const char *subpos = NULL, *bodypos = NULL;
 474
 475        for (i = 0; i < used_atom_cnt; i++) {
 476                const char *name = used_atom[i];
 477                struct atom_value *v = &val[i];
 478                if (!!deref != (*name == '*'))
 479                        continue;
 480                if (deref)
 481                        name++;
 482                if (strcmp(name, "subject") &&
 483                    strcmp(name, "body") &&
 484                    strcmp(name, "contents"))
 485                        continue;
 486                if (!subpos)
 487                        find_subpos(buf, sz, &subpos, &bodypos);
 488                if (!subpos)
 489                        return;
 490
 491                if (!strcmp(name, "subject"))
 492                        v->s = copy_line(subpos);
 493                else if (!strcmp(name, "body"))
 494                        v->s = xstrdup(bodypos);
 495                else if (!strcmp(name, "contents"))
 496                        v->s = xstrdup(subpos);
 497        }
 498}
 499
 500/* We want to have empty print-string for field requests
 501 * that do not apply (e.g. "authordate" for a tag object)
 502 */
 503static void fill_missing_values(struct atom_value *val)
 504{
 505        int i;
 506        for (i = 0; i < used_atom_cnt; i++) {
 507                struct atom_value *v = &val[i];
 508                if (v->s == NULL)
 509                        v->s = "";
 510        }
 511}
 512
 513/*
 514 * val is a list of atom_value to hold returned values.  Extract
 515 * the values for atoms in used_atom array out of (obj, buf, sz).
 516 * when deref is false, (obj, buf, sz) is the object that is
 517 * pointed at by the ref itself; otherwise it is the object the
 518 * ref (which is a tag) refers to.
 519 */
 520static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
 521{
 522        grab_common_values(val, deref, obj, buf, sz);
 523        switch (obj->type) {
 524        case OBJ_TAG:
 525                grab_tag_values(val, deref, obj, buf, sz);
 526                grab_sub_body_contents(val, deref, obj, buf, sz);
 527                grab_person("tagger", val, deref, obj, buf, sz);
 528                break;
 529        case OBJ_COMMIT:
 530                grab_commit_values(val, deref, obj, buf, sz);
 531                grab_sub_body_contents(val, deref, obj, buf, sz);
 532                grab_person("author", val, deref, obj, buf, sz);
 533                grab_person("committer", val, deref, obj, buf, sz);
 534                break;
 535        case OBJ_TREE:
 536                // grab_tree_values(val, deref, obj, buf, sz);
 537                break;
 538        case OBJ_BLOB:
 539                // grab_blob_values(val, deref, obj, buf, sz);
 540                break;
 541        default:
 542                die("Eh?  Object of type %d?", obj->type);
 543        }
 544}
 545
 546/*
 547 * generate a format suitable for scanf from a ref_rev_parse_rules
 548 * rule, that is replace the "%.*s" spec with a "%s" spec
 549 */
 550static void gen_scanf_fmt(char *scanf_fmt, const char *rule)
 551{
 552        char *spec;
 553
 554        spec = strstr(rule, "%.*s");
 555        if (!spec || strstr(spec + 4, "%.*s"))
 556                die("invalid rule in ref_rev_parse_rules: %s", rule);
 557
 558        /* copy all until spec */
 559        strncpy(scanf_fmt, rule, spec - rule);
 560        scanf_fmt[spec - rule] = '\0';
 561        /* copy new spec */
 562        strcat(scanf_fmt, "%s");
 563        /* copy remaining rule */
 564        strcat(scanf_fmt, spec + 4);
 565
 566        return;
 567}
 568
 569/*
 570 * Shorten the refname to an non-ambiguous form
 571 */
 572static char *get_short_ref(struct refinfo *ref)
 573{
 574        int i;
 575        static char **scanf_fmts;
 576        static int nr_rules;
 577        char *short_name;
 578
 579        /* pre generate scanf formats from ref_rev_parse_rules[] */
 580        if (!nr_rules) {
 581                size_t total_len = 0;
 582
 583                /* the rule list is NULL terminated, count them first */
 584                for (; ref_rev_parse_rules[nr_rules]; nr_rules++)
 585                        /* no +1 because strlen("%s") < strlen("%.*s") */
 586                        total_len += strlen(ref_rev_parse_rules[nr_rules]);
 587
 588                scanf_fmts = xmalloc(nr_rules * sizeof(char *) + total_len);
 589
 590                total_len = 0;
 591                for (i = 0; i < nr_rules; i++) {
 592                        scanf_fmts[i] = (char *)&scanf_fmts[nr_rules]
 593                                        + total_len;
 594                        gen_scanf_fmt(scanf_fmts[i], ref_rev_parse_rules[i]);
 595                        total_len += strlen(ref_rev_parse_rules[i]);
 596                }
 597        }
 598
 599        /* bail out if there are no rules */
 600        if (!nr_rules)
 601                return ref->refname;
 602
 603        /* buffer for scanf result, at most ref->refname must fit */
 604        short_name = xstrdup(ref->refname);
 605
 606        /* skip first rule, it will always match */
 607        for (i = nr_rules - 1; i > 0 ; --i) {
 608                int j;
 609                int short_name_len;
 610
 611                if (1 != sscanf(ref->refname, scanf_fmts[i], short_name))
 612                        continue;
 613
 614                short_name_len = strlen(short_name);
 615
 616                /*
 617                 * check if the short name resolves to a valid ref,
 618                 * but use only rules prior to the matched one
 619                 */
 620                for (j = 0; j < i; j++) {
 621                        const char *rule = ref_rev_parse_rules[j];
 622                        unsigned char short_objectname[20];
 623
 624                        /*
 625                         * the short name is ambiguous, if it resolves
 626                         * (with this previous rule) to a valid ref
 627                         * read_ref() returns 0 on success
 628                         */
 629                        if (!read_ref(mkpath(rule, short_name_len, short_name),
 630                                      short_objectname))
 631                                break;
 632                }
 633
 634                /*
 635                 * short name is non-ambiguous if all previous rules
 636                 * haven't resolved to a valid ref
 637                 */
 638                if (j == i)
 639                        return short_name;
 640        }
 641
 642        free(short_name);
 643        return ref->refname;
 644}
 645
 646
 647/*
 648 * Parse the object referred by ref, and grab needed value.
 649 */
 650static void populate_value(struct refinfo *ref)
 651{
 652        void *buf;
 653        struct object *obj;
 654        int eaten, i;
 655        unsigned long size;
 656        const unsigned char *tagged;
 657
 658        ref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt);
 659
 660        buf = get_obj(ref->objectname, &obj, &size, &eaten);
 661        if (!buf)
 662                die("missing object %s for %s",
 663                    sha1_to_hex(ref->objectname), ref->refname);
 664        if (!obj)
 665                die("parse_object_buffer failed on %s for %s",
 666                    sha1_to_hex(ref->objectname), ref->refname);
 667
 668        /* Fill in specials first */
 669        for (i = 0; i < used_atom_cnt; i++) {
 670                const char *name = used_atom[i];
 671                struct atom_value *v = &ref->value[i];
 672                int deref = 0;
 673                if (*name == '*') {
 674                        deref = 1;
 675                        name++;
 676                }
 677                if (!prefixcmp(name, "refname")) {
 678                        const char *formatp = strchr(name, ':');
 679                        const char *refname = ref->refname;
 680
 681                        /* look for "short" refname format */
 682                        if (formatp) {
 683                                formatp++;
 684                                if (!strcmp(formatp, "short"))
 685                                        refname = get_short_ref(ref);
 686                                else
 687                                        die("unknown refname format %s",
 688                                            formatp);
 689                        }
 690
 691                        if (!deref)
 692                                v->s = refname;
 693                        else {
 694                                int len = strlen(refname);
 695                                char *s = xmalloc(len + 4);
 696                                sprintf(s, "%s^{}", refname);
 697                                v->s = s;
 698                        }
 699                }
 700        }
 701
 702        grab_values(ref->value, 0, obj, buf, size);
 703        if (!eaten)
 704                free(buf);
 705
 706        /* If there is no atom that wants to know about tagged
 707         * object, we are done.
 708         */
 709        if (!need_tagged || (obj->type != OBJ_TAG))
 710                return;
 711
 712        /* If it is a tag object, see if we use a value that derefs
 713         * the object, and if we do grab the object it refers to.
 714         */
 715        tagged = ((struct tag *)obj)->tagged->sha1;
 716
 717        /* NEEDSWORK: This derefs tag only once, which
 718         * is good to deal with chains of trust, but
 719         * is not consistent with what deref_tag() does
 720         * which peels the onion to the core.
 721         */
 722        buf = get_obj(tagged, &obj, &size, &eaten);
 723        if (!buf)
 724                die("missing object %s for %s",
 725                    sha1_to_hex(tagged), ref->refname);
 726        if (!obj)
 727                die("parse_object_buffer failed on %s for %s",
 728                    sha1_to_hex(tagged), ref->refname);
 729        grab_values(ref->value, 1, obj, buf, size);
 730        if (!eaten)
 731                free(buf);
 732}
 733
 734/*
 735 * Given a ref, return the value for the atom.  This lazily gets value
 736 * out of the object by calling populate value.
 737 */
 738static void get_value(struct refinfo *ref, int atom, struct atom_value **v)
 739{
 740        if (!ref->value) {
 741                populate_value(ref);
 742                fill_missing_values(ref->value);
 743        }
 744        *v = &ref->value[atom];
 745}
 746
 747struct grab_ref_cbdata {
 748        struct refinfo **grab_array;
 749        const char **grab_pattern;
 750        int grab_cnt;
 751};
 752
 753/*
 754 * A call-back given to for_each_ref().  It is unfortunate that we
 755 * need to use global variables to pass extra information to this
 756 * function.
 757 */
 758static int grab_single_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
 759{
 760        struct grab_ref_cbdata *cb = cb_data;
 761        struct refinfo *ref;
 762        int cnt;
 763
 764        if (*cb->grab_pattern) {
 765                const char **pattern;
 766                int namelen = strlen(refname);
 767                for (pattern = cb->grab_pattern; *pattern; pattern++) {
 768                        const char *p = *pattern;
 769                        int plen = strlen(p);
 770
 771                        if ((plen <= namelen) &&
 772                            !strncmp(refname, p, plen) &&
 773                            (refname[plen] == '\0' ||
 774                             refname[plen] == '/' ||
 775                             p[plen-1] == '/'))
 776                                break;
 777                        if (!fnmatch(p, refname, FNM_PATHNAME))
 778                                break;
 779                }
 780                if (!*pattern)
 781                        return 0;
 782        }
 783
 784        /* We do not open the object yet; sort may only need refname
 785         * to do its job and the resulting list may yet to be pruned
 786         * by maxcount logic.
 787         */
 788        ref = xcalloc(1, sizeof(*ref));
 789        ref->refname = xstrdup(refname);
 790        hashcpy(ref->objectname, sha1);
 791
 792        cnt = cb->grab_cnt;
 793        cb->grab_array = xrealloc(cb->grab_array,
 794                                  sizeof(*cb->grab_array) * (cnt + 1));
 795        cb->grab_array[cnt++] = ref;
 796        cb->grab_cnt = cnt;
 797        return 0;
 798}
 799
 800static int cmp_ref_sort(struct ref_sort *s, struct refinfo *a, struct refinfo *b)
 801{
 802        struct atom_value *va, *vb;
 803        int cmp;
 804        cmp_type cmp_type = used_atom_type[s->atom];
 805
 806        get_value(a, s->atom, &va);
 807        get_value(b, s->atom, &vb);
 808        switch (cmp_type) {
 809        case FIELD_STR:
 810                cmp = strcmp(va->s, vb->s);
 811                break;
 812        default:
 813                if (va->ul < vb->ul)
 814                        cmp = -1;
 815                else if (va->ul == vb->ul)
 816                        cmp = 0;
 817                else
 818                        cmp = 1;
 819                break;
 820        }
 821        return (s->reverse) ? -cmp : cmp;
 822}
 823
 824static struct ref_sort *ref_sort;
 825static int compare_refs(const void *a_, const void *b_)
 826{
 827        struct refinfo *a = *((struct refinfo **)a_);
 828        struct refinfo *b = *((struct refinfo **)b_);
 829        struct ref_sort *s;
 830
 831        for (s = ref_sort; s; s = s->next) {
 832                int cmp = cmp_ref_sort(s, a, b);
 833                if (cmp)
 834                        return cmp;
 835        }
 836        return 0;
 837}
 838
 839static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs)
 840{
 841        ref_sort = sort;
 842        qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs);
 843}
 844
 845static void print_value(struct refinfo *ref, int atom, int quote_style)
 846{
 847        struct atom_value *v;
 848        get_value(ref, atom, &v);
 849        switch (quote_style) {
 850        case QUOTE_NONE:
 851                fputs(v->s, stdout);
 852                break;
 853        case QUOTE_SHELL:
 854                sq_quote_print(stdout, v->s);
 855                break;
 856        case QUOTE_PERL:
 857                perl_quote_print(stdout, v->s);
 858                break;
 859        case QUOTE_PYTHON:
 860                python_quote_print(stdout, v->s);
 861                break;
 862        case QUOTE_TCL:
 863                tcl_quote_print(stdout, v->s);
 864                break;
 865        }
 866}
 867
 868static int hex1(char ch)
 869{
 870        if ('0' <= ch && ch <= '9')
 871                return ch - '0';
 872        else if ('a' <= ch && ch <= 'f')
 873                return ch - 'a' + 10;
 874        else if ('A' <= ch && ch <= 'F')
 875                return ch - 'A' + 10;
 876        return -1;
 877}
 878static int hex2(const char *cp)
 879{
 880        if (cp[0] && cp[1])
 881                return (hex1(cp[0]) << 4) | hex1(cp[1]);
 882        else
 883                return -1;
 884}
 885
 886static void emit(const char *cp, const char *ep)
 887{
 888        while (*cp && (!ep || cp < ep)) {
 889                if (*cp == '%') {
 890                        if (cp[1] == '%')
 891                                cp++;
 892                        else {
 893                                int ch = hex2(cp + 1);
 894                                if (0 <= ch) {
 895                                        putchar(ch);
 896                                        cp += 3;
 897                                        continue;
 898                                }
 899                        }
 900                }
 901                putchar(*cp);
 902                cp++;
 903        }
 904}
 905
 906static void show_ref(struct refinfo *info, const char *format, int quote_style)
 907{
 908        const char *cp, *sp, *ep;
 909
 910        for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
 911                ep = strchr(sp, ')');
 912                if (cp < sp)
 913                        emit(cp, sp);
 914                print_value(info, parse_atom(sp + 2, ep), quote_style);
 915        }
 916        if (*cp) {
 917                sp = cp + strlen(cp);
 918                emit(cp, sp);
 919        }
 920        putchar('\n');
 921}
 922
 923static struct ref_sort *default_sort(void)
 924{
 925        static const char cstr_name[] = "refname";
 926
 927        struct ref_sort *sort = xcalloc(1, sizeof(*sort));
 928
 929        sort->next = NULL;
 930        sort->atom = parse_atom(cstr_name, cstr_name + strlen(cstr_name));
 931        return sort;
 932}
 933
 934static int opt_parse_sort(const struct option *opt, const char *arg, int unset)
 935{
 936        struct ref_sort **sort_tail = opt->value;
 937        struct ref_sort *s;
 938        int len;
 939
 940        if (!arg) /* should --no-sort void the list ? */
 941                return -1;
 942
 943        *sort_tail = s = xcalloc(1, sizeof(*s));
 944        sort_tail = &s->next;
 945
 946        if (*arg == '-') {
 947                s->reverse = 1;
 948                arg++;
 949        }
 950        len = strlen(arg);
 951        s->atom = parse_atom(arg, arg+len);
 952        return 0;
 953}
 954
 955static char const * const for_each_ref_usage[] = {
 956        "git for-each-ref [options] [<pattern>]",
 957        NULL
 958};
 959
 960int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 961{
 962        int i, num_refs;
 963        const char *format = "%(objectname) %(objecttype)\t%(refname)";
 964        struct ref_sort *sort = NULL, **sort_tail = &sort;
 965        int maxcount = 0, quote_style = 0;
 966        struct refinfo **refs;
 967        struct grab_ref_cbdata cbdata;
 968
 969        struct option opts[] = {
 970                OPT_BIT('s', "shell", &quote_style,
 971                        "quote placeholders suitably for shells", QUOTE_SHELL),
 972                OPT_BIT('p', "perl",  &quote_style,
 973                        "quote placeholders suitably for perl", QUOTE_PERL),
 974                OPT_BIT(0 , "python", &quote_style,
 975                        "quote placeholders suitably for python", QUOTE_PYTHON),
 976                OPT_BIT(0 , "tcl",  &quote_style,
 977                        "quote placeholders suitably for tcl", QUOTE_TCL),
 978
 979                OPT_GROUP(""),
 980                OPT_INTEGER( 0 , "count", &maxcount, "show only <n> matched refs"),
 981                OPT_STRING(  0 , "format", &format, "format", "format to use for the output"),
 982                OPT_CALLBACK(0 , "sort", sort_tail, "key",
 983                            "field name to sort on", &opt_parse_sort),
 984                OPT_END(),
 985        };
 986
 987        parse_options(argc, argv, opts, for_each_ref_usage, 0);
 988        if (maxcount < 0) {
 989                error("invalid --count argument: `%d'", maxcount);
 990                usage_with_options(for_each_ref_usage, opts);
 991        }
 992        if (HAS_MULTI_BITS(quote_style)) {
 993                error("more than one quoting style?");
 994                usage_with_options(for_each_ref_usage, opts);
 995        }
 996        if (verify_format(format))
 997                usage_with_options(for_each_ref_usage, opts);
 998
 999        if (!sort)
1000                sort = default_sort();
1001        sort_atom_limit = used_atom_cnt;
1002
1003        memset(&cbdata, 0, sizeof(cbdata));
1004        cbdata.grab_pattern = argv;
1005        for_each_ref(grab_single_ref, &cbdata);
1006        refs = cbdata.grab_array;
1007        num_refs = cbdata.grab_cnt;
1008
1009        for (i = 0; i < used_atom_cnt; i++) {
1010                if (used_atom[i][0] == '*') {
1011                        need_tagged = 1;
1012                        break;
1013                }
1014        }
1015
1016        sort_refs(sort, refs, num_refs);
1017
1018        if (!maxcount || num_refs < maxcount)
1019                maxcount = num_refs;
1020        for (i = 0; i < maxcount; i++)
1021                show_ref(refs[i], format, quote_style);
1022        return 0;
1023}