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