ref-filter: use string_list_split over strbuf_split
[git] / ref-filter.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "parse-options.h"
4 #include "refs.h"
5 #include "wildmatch.h"
6 #include "commit.h"
7 #include "remote.h"
8 #include "color.h"
9 #include "tag.h"
10 #include "quote.h"
11 #include "ref-filter.h"
12 #include "revision.h"
13 #include "utf8.h"
14 #include "git-compat-util.h"
15 #include "version.h"
16
17 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
18
19 static struct {
20         const char *name;
21         cmp_type cmp_type;
22 } valid_atom[] = {
23         { "refname" },
24         { "objecttype" },
25         { "objectsize", FIELD_ULONG },
26         { "objectname" },
27         { "tree" },
28         { "parent" },
29         { "numparent", FIELD_ULONG },
30         { "object" },
31         { "type" },
32         { "tag" },
33         { "author" },
34         { "authorname" },
35         { "authoremail" },
36         { "authordate", FIELD_TIME },
37         { "committer" },
38         { "committername" },
39         { "committeremail" },
40         { "committerdate", FIELD_TIME },
41         { "tagger" },
42         { "taggername" },
43         { "taggeremail" },
44         { "taggerdate", FIELD_TIME },
45         { "creator" },
46         { "creatordate", FIELD_TIME },
47         { "subject" },
48         { "body" },
49         { "contents" },
50         { "upstream" },
51         { "push" },
52         { "symref" },
53         { "flag" },
54         { "HEAD" },
55         { "color" },
56         { "align" },
57         { "end" },
58 };
59
60 #define REF_FORMATTING_STATE_INIT  { 0, NULL }
61
62 struct align {
63         align_type position;
64         unsigned int width;
65 };
66
67 struct contents {
68         unsigned int lines;
69         struct object_id oid;
70 };
71
72 struct ref_formatting_stack {
73         struct ref_formatting_stack *prev;
74         struct strbuf output;
75         void (*at_end)(struct ref_formatting_stack *stack);
76         void *at_end_data;
77 };
78
79 struct ref_formatting_state {
80         int quote_style;
81         struct ref_formatting_stack *stack;
82 };
83
84 struct atom_value {
85         const char *s;
86         union {
87                 struct align align;
88                 struct contents contents;
89         } u;
90         void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
91         unsigned long ul; /* used for sorting when not FIELD_STR */
92 };
93
94 /*
95  * An atom is a valid field atom listed above, possibly prefixed with
96  * a "*" to denote deref_tag().
97  *
98  * We parse given format string and sort specifiers, and make a list
99  * of properties that we need to extract out of objects.  ref_array_item
100  * structure will hold an array of values extracted that can be
101  * indexed with the "atom number", which is an index into this
102  * array.
103  */
104 static const char **used_atom;
105 static cmp_type *used_atom_type;
106 static int used_atom_cnt, need_tagged, need_symref;
107 static int need_color_reset_at_eol;
108
109 /*
110  * Used to parse format string and sort specifiers
111  */
112 int parse_ref_filter_atom(const char *atom, const char *ep)
113 {
114         const char *sp;
115         int i, at;
116
117         sp = atom;
118         if (*sp == '*' && sp < ep)
119                 sp++; /* deref */
120         if (ep <= sp)
121                 die("malformed field name: %.*s", (int)(ep-atom), atom);
122
123         /* Do we have the atom already used elsewhere? */
124         for (i = 0; i < used_atom_cnt; i++) {
125                 int len = strlen(used_atom[i]);
126                 if (len == ep - atom && !memcmp(used_atom[i], atom, len))
127                         return i;
128         }
129
130         /* Is the atom a valid one? */
131         for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
132                 int len = strlen(valid_atom[i].name);
133                 /*
134                  * If the atom name has a colon, strip it and everything after
135                  * it off - it specifies the format for this entry, and
136                  * shouldn't be used for checking against the valid_atom
137                  * table.
138                  */
139                 const char *formatp = strchr(sp, ':');
140                 if (!formatp || ep < formatp)
141                         formatp = ep;
142                 if (len == formatp - sp && !memcmp(valid_atom[i].name, sp, len))
143                         break;
144         }
145
146         if (ARRAY_SIZE(valid_atom) <= i)
147                 die("unknown field name: %.*s", (int)(ep-atom), atom);
148
149         /* Add it in, including the deref prefix */
150         at = used_atom_cnt;
151         used_atom_cnt++;
152         REALLOC_ARRAY(used_atom, used_atom_cnt);
153         REALLOC_ARRAY(used_atom_type, used_atom_cnt);
154         used_atom[at] = xmemdupz(atom, ep - atom);
155         used_atom_type[at] = valid_atom[i].cmp_type;
156         if (*atom == '*')
157                 need_tagged = 1;
158         if (!strcmp(used_atom[at], "symref"))
159                 need_symref = 1;
160         return at;
161 }
162
163 static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
164 {
165         switch (quote_style) {
166         case QUOTE_NONE:
167                 strbuf_addstr(s, str);
168                 break;
169         case QUOTE_SHELL:
170                 sq_quote_buf(s, str);
171                 break;
172         case QUOTE_PERL:
173                 perl_quote_buf(s, str);
174                 break;
175         case QUOTE_PYTHON:
176                 python_quote_buf(s, str);
177                 break;
178         case QUOTE_TCL:
179                 tcl_quote_buf(s, str);
180                 break;
181         }
182 }
183
184 static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
185 {
186         /*
187          * Quote formatting is only done when the stack has a single
188          * element. Otherwise quote formatting is done on the
189          * element's entire output strbuf when the %(end) atom is
190          * encountered.
191          */
192         if (!state->stack->prev)
193                 quote_formatting(&state->stack->output, v->s, state->quote_style);
194         else
195                 strbuf_addstr(&state->stack->output, v->s);
196 }
197
198 static void push_stack_element(struct ref_formatting_stack **stack)
199 {
200         struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
201
202         strbuf_init(&s->output, 0);
203         s->prev = *stack;
204         *stack = s;
205 }
206
207 static void pop_stack_element(struct ref_formatting_stack **stack)
208 {
209         struct ref_formatting_stack *current = *stack;
210         struct ref_formatting_stack *prev = current->prev;
211
212         if (prev)
213                 strbuf_addbuf(&prev->output, &current->output);
214         strbuf_release(&current->output);
215         free(current);
216         *stack = prev;
217 }
218
219 static void end_align_handler(struct ref_formatting_stack *stack)
220 {
221         struct align *align = (struct align *)stack->at_end_data;
222         struct strbuf s = STRBUF_INIT;
223
224         strbuf_utf8_align(&s, align->position, align->width, stack->output.buf);
225         strbuf_swap(&stack->output, &s);
226         strbuf_release(&s);
227 }
228
229 static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
230 {
231         struct ref_formatting_stack *new;
232
233         push_stack_element(&state->stack);
234         new = state->stack;
235         new->at_end = end_align_handler;
236         new->at_end_data = &atomv->u.align;
237 }
238
239 static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
240 {
241         struct ref_formatting_stack *current = state->stack;
242         struct strbuf s = STRBUF_INIT;
243
244         if (!current->at_end)
245                 die(_("format: %%(end) atom used without corresponding atom"));
246         current->at_end(current);
247
248         /*
249          * Perform quote formatting when the stack element is that of
250          * a supporting atom. If nested then perform quote formatting
251          * only on the topmost supporting atom.
252          */
253         if (!state->stack->prev->prev) {
254                 quote_formatting(&s, current->output.buf, state->quote_style);
255                 strbuf_swap(&current->output, &s);
256         }
257         strbuf_release(&s);
258         pop_stack_element(&state->stack);
259 }
260
261 static int match_atom_name(const char *name, const char *atom_name, const char **val)
262 {
263         const char *body;
264
265         if (!skip_prefix(name, atom_name, &body))
266                 return 0; /* doesn't even begin with "atom_name" */
267         if (!body[0]) {
268                 *val = NULL; /* %(atom_name) and no customization */
269                 return 1;
270         }
271         if (body[0] != ':')
272                 return 0; /* "atom_namefoo" is not "atom_name" or "atom_name:..." */
273         *val = body + 1; /* "atom_name:val" */
274         return 1;
275 }
276
277 /*
278  * In a format string, find the next occurrence of %(atom).
279  */
280 static const char *find_next(const char *cp)
281 {
282         while (*cp) {
283                 if (*cp == '%') {
284                         /*
285                          * %( is the start of an atom;
286                          * %% is a quoted per-cent.
287                          */
288                         if (cp[1] == '(')
289                                 return cp;
290                         else if (cp[1] == '%')
291                                 cp++; /* skip over two % */
292                         /* otherwise this is a singleton, literal % */
293                 }
294                 cp++;
295         }
296         return NULL;
297 }
298
299 /*
300  * Make sure the format string is well formed, and parse out
301  * the used atoms.
302  */
303 int verify_ref_format(const char *format)
304 {
305         const char *cp, *sp;
306
307         need_color_reset_at_eol = 0;
308         for (cp = format; *cp && (sp = find_next(cp)); ) {
309                 const char *color, *ep = strchr(sp, ')');
310                 int at;
311
312                 if (!ep)
313                         return error("malformed format string %s", sp);
314                 /* sp points at "%(" and ep points at the closing ")" */
315                 at = parse_ref_filter_atom(sp + 2, ep);
316                 cp = ep + 1;
317
318                 if (skip_prefix(used_atom[at], "color:", &color))
319                         need_color_reset_at_eol = !!strcmp(color, "reset");
320         }
321         return 0;
322 }
323
324 /*
325  * Given an object name, read the object data and size, and return a
326  * "struct object".  If the object data we are returning is also borrowed
327  * by the "struct object" representation, set *eaten as well---it is a
328  * signal from parse_object_buffer to us not to free the buffer.
329  */
330 static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
331 {
332         enum object_type type;
333         void *buf = read_sha1_file(sha1, &type, sz);
334
335         if (buf)
336                 *obj = parse_object_buffer(sha1, type, *sz, buf, eaten);
337         else
338                 *obj = NULL;
339         return buf;
340 }
341
342 static int grab_objectname(const char *name, const unsigned char *sha1,
343                             struct atom_value *v)
344 {
345         if (!strcmp(name, "objectname")) {
346                 v->s = xstrdup(sha1_to_hex(sha1));
347                 return 1;
348         }
349         if (!strcmp(name, "objectname:short")) {
350                 v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
351                 return 1;
352         }
353         return 0;
354 }
355
356 /* See grab_values */
357 static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
358 {
359         int i;
360
361         for (i = 0; i < used_atom_cnt; i++) {
362                 const char *name = used_atom[i];
363                 struct atom_value *v = &val[i];
364                 if (!!deref != (*name == '*'))
365                         continue;
366                 if (deref)
367                         name++;
368                 if (!strcmp(name, "objecttype"))
369                         v->s = typename(obj->type);
370                 else if (!strcmp(name, "objectsize")) {
371                         v->ul = sz;
372                         v->s = xstrfmt("%lu", sz);
373                 }
374                 else if (deref)
375                         grab_objectname(name, obj->oid.hash, v);
376         }
377 }
378
379 /* See grab_values */
380 static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
381 {
382         int i;
383         struct tag *tag = (struct tag *) obj;
384
385         for (i = 0; i < used_atom_cnt; i++) {
386                 const char *name = used_atom[i];
387                 struct atom_value *v = &val[i];
388                 if (!!deref != (*name == '*'))
389                         continue;
390                 if (deref)
391                         name++;
392                 if (!strcmp(name, "tag"))
393                         v->s = tag->tag;
394                 else if (!strcmp(name, "type") && tag->tagged)
395                         v->s = typename(tag->tagged->type);
396                 else if (!strcmp(name, "object") && tag->tagged)
397                         v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
398         }
399 }
400
401 /* See grab_values */
402 static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
403 {
404         int i;
405         struct commit *commit = (struct commit *) obj;
406
407         for (i = 0; i < used_atom_cnt; i++) {
408                 const char *name = used_atom[i];
409                 struct atom_value *v = &val[i];
410                 if (!!deref != (*name == '*'))
411                         continue;
412                 if (deref)
413                         name++;
414                 if (!strcmp(name, "tree")) {
415                         v->s = xstrdup(oid_to_hex(&commit->tree->object.oid));
416                 }
417                 else if (!strcmp(name, "numparent")) {
418                         v->ul = commit_list_count(commit->parents);
419                         v->s = xstrfmt("%lu", v->ul);
420                 }
421                 else if (!strcmp(name, "parent")) {
422                         struct commit_list *parents;
423                         struct strbuf s = STRBUF_INIT;
424                         for (parents = commit->parents; parents; parents = parents->next) {
425                                 struct commit *parent = parents->item;
426                                 if (parents != commit->parents)
427                                         strbuf_addch(&s, ' ');
428                                 strbuf_addstr(&s, oid_to_hex(&parent->object.oid));
429                         }
430                         v->s = strbuf_detach(&s, NULL);
431                 }
432         }
433 }
434
435 static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz)
436 {
437         const char *eol;
438         while (*buf) {
439                 if (!strncmp(buf, who, wholen) &&
440                     buf[wholen] == ' ')
441                         return buf + wholen + 1;
442                 eol = strchr(buf, '\n');
443                 if (!eol)
444                         return "";
445                 eol++;
446                 if (*eol == '\n')
447                         return ""; /* end of header */
448                 buf = eol;
449         }
450         return "";
451 }
452
453 static const char *copy_line(const char *buf)
454 {
455         const char *eol = strchrnul(buf, '\n');
456         return xmemdupz(buf, eol - buf);
457 }
458
459 static const char *copy_name(const char *buf)
460 {
461         const char *cp;
462         for (cp = buf; *cp && *cp != '\n'; cp++) {
463                 if (!strncmp(cp, " <", 2))
464                         return xmemdupz(buf, cp - buf);
465         }
466         return "";
467 }
468
469 static const char *copy_email(const char *buf)
470 {
471         const char *email = strchr(buf, '<');
472         const char *eoemail;
473         if (!email)
474                 return "";
475         eoemail = strchr(email, '>');
476         if (!eoemail)
477                 return "";
478         return xmemdupz(email, eoemail + 1 - email);
479 }
480
481 static char *copy_subject(const char *buf, unsigned long len)
482 {
483         char *r = xmemdupz(buf, len);
484         int i;
485
486         for (i = 0; i < len; i++)
487                 if (r[i] == '\n')
488                         r[i] = ' ';
489
490         return r;
491 }
492
493 static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
494 {
495         const char *eoemail = strstr(buf, "> ");
496         char *zone;
497         unsigned long timestamp;
498         long tz;
499         struct date_mode date_mode = { DATE_NORMAL };
500         const char *formatp;
501
502         /*
503          * We got here because atomname ends in "date" or "date<something>";
504          * it's not possible that <something> is not ":<format>" because
505          * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
506          * ":" means no format is specified, and use the default.
507          */
508         formatp = strchr(atomname, ':');
509         if (formatp != NULL) {
510                 formatp++;
511                 parse_date_format(formatp, &date_mode);
512         }
513
514         if (!eoemail)
515                 goto bad;
516         timestamp = strtoul(eoemail + 2, &zone, 10);
517         if (timestamp == ULONG_MAX)
518                 goto bad;
519         tz = strtol(zone, NULL, 10);
520         if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
521                 goto bad;
522         v->s = xstrdup(show_date(timestamp, tz, &date_mode));
523         v->ul = timestamp;
524         return;
525  bad:
526         v->s = "";
527         v->ul = 0;
528 }
529
530 /* See grab_values */
531 static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
532 {
533         int i;
534         int wholen = strlen(who);
535         const char *wholine = NULL;
536
537         for (i = 0; i < used_atom_cnt; i++) {
538                 const char *name = used_atom[i];
539                 struct atom_value *v = &val[i];
540                 if (!!deref != (*name == '*'))
541                         continue;
542                 if (deref)
543                         name++;
544                 if (strncmp(who, name, wholen))
545                         continue;
546                 if (name[wholen] != 0 &&
547                     strcmp(name + wholen, "name") &&
548                     strcmp(name + wholen, "email") &&
549                     !starts_with(name + wholen, "date"))
550                         continue;
551                 if (!wholine)
552                         wholine = find_wholine(who, wholen, buf, sz);
553                 if (!wholine)
554                         return; /* no point looking for it */
555                 if (name[wholen] == 0)
556                         v->s = copy_line(wholine);
557                 else if (!strcmp(name + wholen, "name"))
558                         v->s = copy_name(wholine);
559                 else if (!strcmp(name + wholen, "email"))
560                         v->s = copy_email(wholine);
561                 else if (starts_with(name + wholen, "date"))
562                         grab_date(wholine, v, name);
563         }
564
565         /*
566          * For a tag or a commit object, if "creator" or "creatordate" is
567          * requested, do something special.
568          */
569         if (strcmp(who, "tagger") && strcmp(who, "committer"))
570                 return; /* "author" for commit object is not wanted */
571         if (!wholine)
572                 wholine = find_wholine(who, wholen, buf, sz);
573         if (!wholine)
574                 return;
575         for (i = 0; i < used_atom_cnt; i++) {
576                 const char *name = used_atom[i];
577                 struct atom_value *v = &val[i];
578                 if (!!deref != (*name == '*'))
579                         continue;
580                 if (deref)
581                         name++;
582
583                 if (starts_with(name, "creatordate"))
584                         grab_date(wholine, v, name);
585                 else if (!strcmp(name, "creator"))
586                         v->s = copy_line(wholine);
587         }
588 }
589
590 static void find_subpos(const char *buf, unsigned long sz,
591                         const char **sub, unsigned long *sublen,
592                         const char **body, unsigned long *bodylen,
593                         unsigned long *nonsiglen,
594                         const char **sig, unsigned long *siglen)
595 {
596         const char *eol;
597         /* skip past header until we hit empty line */
598         while (*buf && *buf != '\n') {
599                 eol = strchrnul(buf, '\n');
600                 if (*eol)
601                         eol++;
602                 buf = eol;
603         }
604         /* skip any empty lines */
605         while (*buf == '\n')
606                 buf++;
607
608         /* parse signature first; we might not even have a subject line */
609         *sig = buf + parse_signature(buf, strlen(buf));
610         *siglen = strlen(*sig);
611
612         /* subject is first non-empty line */
613         *sub = buf;
614         /* subject goes to first empty line */
615         while (buf < *sig && *buf && *buf != '\n') {
616                 eol = strchrnul(buf, '\n');
617                 if (*eol)
618                         eol++;
619                 buf = eol;
620         }
621         *sublen = buf - *sub;
622         /* drop trailing newline, if present */
623         if (*sublen && (*sub)[*sublen - 1] == '\n')
624                 *sublen -= 1;
625
626         /* skip any empty lines */
627         while (*buf == '\n')
628                 buf++;
629         *body = buf;
630         *bodylen = strlen(buf);
631         *nonsiglen = *sig - buf;
632 }
633
634 /*
635  * If 'lines' is greater than 0, append that many lines from the given
636  * 'buf' of length 'size' to the given strbuf.
637  */
638 static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
639 {
640         int i;
641         const char *sp, *eol;
642         size_t len;
643
644         sp = buf;
645
646         for (i = 0; i < lines && sp < buf + size; i++) {
647                 if (i)
648                         strbuf_addstr(out, "\n    ");
649                 eol = memchr(sp, '\n', size - (sp - buf));
650                 len = eol ? eol - sp : size - (sp - buf);
651                 strbuf_add(out, sp, len);
652                 if (!eol)
653                         break;
654                 sp = eol + 1;
655         }
656 }
657
658 /* See grab_values */
659 static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
660 {
661         int i;
662         const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
663         unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
664
665         for (i = 0; i < used_atom_cnt; i++) {
666                 const char *name = used_atom[i];
667                 struct atom_value *v = &val[i];
668                 const char *valp = NULL;
669                 if (!!deref != (*name == '*'))
670                         continue;
671                 if (deref)
672                         name++;
673                 if (strcmp(name, "subject") &&
674                     strcmp(name, "body") &&
675                     strcmp(name, "contents") &&
676                     strcmp(name, "contents:subject") &&
677                     strcmp(name, "contents:body") &&
678                     strcmp(name, "contents:signature") &&
679                     !starts_with(name, "contents:lines="))
680                         continue;
681                 if (!subpos)
682                         find_subpos(buf, sz,
683                                     &subpos, &sublen,
684                                     &bodypos, &bodylen, &nonsiglen,
685                                     &sigpos, &siglen);
686
687                 if (!strcmp(name, "subject"))
688                         v->s = copy_subject(subpos, sublen);
689                 else if (!strcmp(name, "contents:subject"))
690                         v->s = copy_subject(subpos, sublen);
691                 else if (!strcmp(name, "body"))
692                         v->s = xmemdupz(bodypos, bodylen);
693                 else if (!strcmp(name, "contents:body"))
694                         v->s = xmemdupz(bodypos, nonsiglen);
695                 else if (!strcmp(name, "contents:signature"))
696                         v->s = xmemdupz(sigpos, siglen);
697                 else if (!strcmp(name, "contents"))
698                         v->s = xstrdup(subpos);
699                 else if (skip_prefix(name, "contents:lines=", &valp)) {
700                         struct strbuf s = STRBUF_INIT;
701                         const char *contents_end = bodylen + bodypos - siglen;
702
703                         if (strtoul_ui(valp, 10, &v->u.contents.lines))
704                                 die(_("positive value expected contents:lines=%s"), valp);
705                         /*  Size is the length of the message after removing the signature */
706                         append_lines(&s, subpos, contents_end - subpos, v->u.contents.lines);
707                         v->s = strbuf_detach(&s, NULL);
708                 }
709         }
710 }
711
712 /*
713  * We want to have empty print-string for field requests
714  * that do not apply (e.g. "authordate" for a tag object)
715  */
716 static void fill_missing_values(struct atom_value *val)
717 {
718         int i;
719         for (i = 0; i < used_atom_cnt; i++) {
720                 struct atom_value *v = &val[i];
721                 if (v->s == NULL)
722                         v->s = "";
723         }
724 }
725
726 /*
727  * val is a list of atom_value to hold returned values.  Extract
728  * the values for atoms in used_atom array out of (obj, buf, sz).
729  * when deref is false, (obj, buf, sz) is the object that is
730  * pointed at by the ref itself; otherwise it is the object the
731  * ref (which is a tag) refers to.
732  */
733 static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
734 {
735         grab_common_values(val, deref, obj, buf, sz);
736         switch (obj->type) {
737         case OBJ_TAG:
738                 grab_tag_values(val, deref, obj, buf, sz);
739                 grab_sub_body_contents(val, deref, obj, buf, sz);
740                 grab_person("tagger", val, deref, obj, buf, sz);
741                 break;
742         case OBJ_COMMIT:
743                 grab_commit_values(val, deref, obj, buf, sz);
744                 grab_sub_body_contents(val, deref, obj, buf, sz);
745                 grab_person("author", val, deref, obj, buf, sz);
746                 grab_person("committer", val, deref, obj, buf, sz);
747                 break;
748         case OBJ_TREE:
749                 /* grab_tree_values(val, deref, obj, buf, sz); */
750                 break;
751         case OBJ_BLOB:
752                 /* grab_blob_values(val, deref, obj, buf, sz); */
753                 break;
754         default:
755                 die("Eh?  Object of type %d?", obj->type);
756         }
757 }
758
759 static inline char *copy_advance(char *dst, const char *src)
760 {
761         while (*src)
762                 *dst++ = *src++;
763         return dst;
764 }
765
766 static const char *strip_ref_components(const char *refname, const char *nr_arg)
767 {
768         char *end;
769         long nr = strtol(nr_arg, &end, 10);
770         long remaining = nr;
771         const char *start = refname;
772
773         if (nr < 1 || *end != '\0')
774                 die(":strip= requires a positive integer argument");
775
776         while (remaining) {
777                 switch (*start++) {
778                 case '\0':
779                         die("ref '%s' does not have %ld components to :strip",
780                             refname, nr);
781                 case '/':
782                         remaining--;
783                         break;
784                 }
785         }
786         return start;
787 }
788
789 /*
790  * Parse the object referred by ref, and grab needed value.
791  */
792 static void populate_value(struct ref_array_item *ref)
793 {
794         void *buf;
795         struct object *obj;
796         int eaten, i;
797         unsigned long size;
798         const unsigned char *tagged;
799
800         ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
801
802         if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
803                 unsigned char unused1[20];
804                 ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
805                                              unused1, NULL);
806                 if (!ref->symref)
807                         ref->symref = "";
808         }
809
810         /* Fill in specials first */
811         for (i = 0; i < used_atom_cnt; i++) {
812                 const char *name = used_atom[i];
813                 struct atom_value *v = &ref->value[i];
814                 int deref = 0;
815                 const char *refname;
816                 const char *formatp;
817                 const char *valp;
818                 struct branch *branch = NULL;
819
820                 v->handler = append_atom;
821
822                 if (*name == '*') {
823                         deref = 1;
824                         name++;
825                 }
826
827                 if (starts_with(name, "refname"))
828                         refname = ref->refname;
829                 else if (starts_with(name, "symref"))
830                         refname = ref->symref ? ref->symref : "";
831                 else if (starts_with(name, "upstream")) {
832                         const char *branch_name;
833                         /* only local branches may have an upstream */
834                         if (!skip_prefix(ref->refname, "refs/heads/",
835                                          &branch_name))
836                                 continue;
837                         branch = branch_get(branch_name);
838
839                         refname = branch_get_upstream(branch, NULL);
840                         if (!refname)
841                                 continue;
842                 } else if (starts_with(name, "push")) {
843                         const char *branch_name;
844                         if (!skip_prefix(ref->refname, "refs/heads/",
845                                          &branch_name))
846                                 continue;
847                         branch = branch_get(branch_name);
848
849                         refname = branch_get_push(branch, NULL);
850                         if (!refname)
851                                 continue;
852                 } else if (match_atom_name(name, "color", &valp)) {
853                         char color[COLOR_MAXLEN] = "";
854
855                         if (!valp)
856                                 die(_("expected format: %%(color:<color>)"));
857                         if (color_parse(valp, color) < 0)
858                                 die(_("unable to parse format"));
859                         v->s = xstrdup(color);
860                         continue;
861                 } else if (!strcmp(name, "flag")) {
862                         char buf[256], *cp = buf;
863                         if (ref->flag & REF_ISSYMREF)
864                                 cp = copy_advance(cp, ",symref");
865                         if (ref->flag & REF_ISPACKED)
866                                 cp = copy_advance(cp, ",packed");
867                         if (cp == buf)
868                                 v->s = "";
869                         else {
870                                 *cp = '\0';
871                                 v->s = xstrdup(buf + 1);
872                         }
873                         continue;
874                 } else if (!deref && grab_objectname(name, ref->objectname, v)) {
875                         continue;
876                 } else if (!strcmp(name, "HEAD")) {
877                         const char *head;
878                         unsigned char sha1[20];
879
880                         head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
881                                                   sha1, NULL);
882                         if (!strcmp(ref->refname, head))
883                                 v->s = "*";
884                         else
885                                 v->s = " ";
886                         continue;
887                 } else if (match_atom_name(name, "align", &valp)) {
888                         struct align *align = &v->u.align;
889                         struct string_list params = STRING_LIST_INIT_DUP;
890                         int i;
891                         int width = -1;
892
893                         if (!valp)
894                                 die(_("expected format: %%(align:<width>,<position>)"));
895
896                         align->position = ALIGN_LEFT;
897
898                         string_list_split(&params, valp, ',', -1);
899                         for (i = 0; i < params.nr; i++) {
900                                 const char *s = params.items[i].string;
901                                 if (!strtoul_ui(s, 10, (unsigned int *)&width))
902                                         ;
903                                 else if (!strcmp(s, "left"))
904                                         align->position = ALIGN_LEFT;
905                                 else if (!strcmp(s, "right"))
906                                         align->position = ALIGN_RIGHT;
907                                 else if (!strcmp(s, "middle"))
908                                         align->position = ALIGN_MIDDLE;
909                                 else
910                                         die(_("improper format entered align:%s"), s);
911                         }
912
913                         if (width < 0)
914                                 die(_("positive width expected with the %%(align) atom"));
915                         align->width = width;
916                         string_list_clear(&params, 0);
917                         v->handler = align_atom_handler;
918                         continue;
919                 } else if (!strcmp(name, "end")) {
920                         v->handler = end_atom_handler;
921                         continue;
922                 } else
923                         continue;
924
925                 formatp = strchr(name, ':');
926                 if (formatp) {
927                         int num_ours, num_theirs;
928                         const char *arg;
929
930                         formatp++;
931                         if (!strcmp(formatp, "short"))
932                                 refname = shorten_unambiguous_ref(refname,
933                                                       warn_ambiguous_refs);
934                         else if (skip_prefix(formatp, "strip=", &arg))
935                                 refname = strip_ref_components(refname, arg);
936                         else if (!strcmp(formatp, "track") &&
937                                  (starts_with(name, "upstream") ||
938                                   starts_with(name, "push"))) {
939
940                                 if (stat_tracking_info(branch, &num_ours,
941                                                        &num_theirs, NULL))
942                                         continue;
943
944                                 if (!num_ours && !num_theirs)
945                                         v->s = "";
946                                 else if (!num_ours)
947                                         v->s = xstrfmt("[behind %d]", num_theirs);
948                                 else if (!num_theirs)
949                                         v->s = xstrfmt("[ahead %d]", num_ours);
950                                 else
951                                         v->s = xstrfmt("[ahead %d, behind %d]",
952                                                        num_ours, num_theirs);
953                                 continue;
954                         } else if (!strcmp(formatp, "trackshort") &&
955                                    (starts_with(name, "upstream") ||
956                                     starts_with(name, "push"))) {
957                                 assert(branch);
958
959                                 if (stat_tracking_info(branch, &num_ours,
960                                                         &num_theirs, NULL))
961                                         continue;
962
963                                 if (!num_ours && !num_theirs)
964                                         v->s = "=";
965                                 else if (!num_ours)
966                                         v->s = "<";
967                                 else if (!num_theirs)
968                                         v->s = ">";
969                                 else
970                                         v->s = "<>";
971                                 continue;
972                         } else
973                                 die("unknown %.*s format %s",
974                                     (int)(formatp - name), name, formatp);
975                 }
976
977                 if (!deref)
978                         v->s = refname;
979                 else
980                         v->s = xstrfmt("%s^{}", refname);
981         }
982
983         for (i = 0; i < used_atom_cnt; i++) {
984                 struct atom_value *v = &ref->value[i];
985                 if (v->s == NULL)
986                         goto need_obj;
987         }
988         return;
989
990  need_obj:
991         buf = get_obj(ref->objectname, &obj, &size, &eaten);
992         if (!buf)
993                 die("missing object %s for %s",
994                     sha1_to_hex(ref->objectname), ref->refname);
995         if (!obj)
996                 die("parse_object_buffer failed on %s for %s",
997                     sha1_to_hex(ref->objectname), ref->refname);
998
999         grab_values(ref->value, 0, obj, buf, size);
1000         if (!eaten)
1001                 free(buf);
1002
1003         /*
1004          * If there is no atom that wants to know about tagged
1005          * object, we are done.
1006          */
1007         if (!need_tagged || (obj->type != OBJ_TAG))
1008                 return;
1009
1010         /*
1011          * If it is a tag object, see if we use a value that derefs
1012          * the object, and if we do grab the object it refers to.
1013          */
1014         tagged = ((struct tag *)obj)->tagged->oid.hash;
1015
1016         /*
1017          * NEEDSWORK: This derefs tag only once, which
1018          * is good to deal with chains of trust, but
1019          * is not consistent with what deref_tag() does
1020          * which peels the onion to the core.
1021          */
1022         buf = get_obj(tagged, &obj, &size, &eaten);
1023         if (!buf)
1024                 die("missing object %s for %s",
1025                     sha1_to_hex(tagged), ref->refname);
1026         if (!obj)
1027                 die("parse_object_buffer failed on %s for %s",
1028                     sha1_to_hex(tagged), ref->refname);
1029         grab_values(ref->value, 1, obj, buf, size);
1030         if (!eaten)
1031                 free(buf);
1032 }
1033
1034 /*
1035  * Given a ref, return the value for the atom.  This lazily gets value
1036  * out of the object by calling populate value.
1037  */
1038 static void get_ref_atom_value(struct ref_array_item *ref, int atom, struct atom_value **v)
1039 {
1040         if (!ref->value) {
1041                 populate_value(ref);
1042                 fill_missing_values(ref->value);
1043         }
1044         *v = &ref->value[atom];
1045 }
1046
1047 enum contains_result {
1048         CONTAINS_UNKNOWN = -1,
1049         CONTAINS_NO = 0,
1050         CONTAINS_YES = 1
1051 };
1052
1053 /*
1054  * Mimicking the real stack, this stack lives on the heap, avoiding stack
1055  * overflows.
1056  *
1057  * At each recursion step, the stack items points to the commits whose
1058  * ancestors are to be inspected.
1059  */
1060 struct contains_stack {
1061         int nr, alloc;
1062         struct contains_stack_entry {
1063                 struct commit *commit;
1064                 struct commit_list *parents;
1065         } *contains_stack;
1066 };
1067
1068 static int in_commit_list(const struct commit_list *want, struct commit *c)
1069 {
1070         for (; want; want = want->next)
1071                 if (!oidcmp(&want->item->object.oid, &c->object.oid))
1072                         return 1;
1073         return 0;
1074 }
1075
1076 /*
1077  * Test whether the candidate or one of its parents is contained in the list.
1078  * Do not recurse to find out, though, but return -1 if inconclusive.
1079  */
1080 static enum contains_result contains_test(struct commit *candidate,
1081                             const struct commit_list *want)
1082 {
1083         /* was it previously marked as containing a want commit? */
1084         if (candidate->object.flags & TMP_MARK)
1085                 return 1;
1086         /* or marked as not possibly containing a want commit? */
1087         if (candidate->object.flags & UNINTERESTING)
1088                 return 0;
1089         /* or are we it? */
1090         if (in_commit_list(want, candidate)) {
1091                 candidate->object.flags |= TMP_MARK;
1092                 return 1;
1093         }
1094
1095         if (parse_commit(candidate) < 0)
1096                 return 0;
1097
1098         return -1;
1099 }
1100
1101 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
1102 {
1103         ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
1104         contains_stack->contains_stack[contains_stack->nr].commit = candidate;
1105         contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
1106 }
1107
1108 static enum contains_result contains_tag_algo(struct commit *candidate,
1109                 const struct commit_list *want)
1110 {
1111         struct contains_stack contains_stack = { 0, 0, NULL };
1112         int result = contains_test(candidate, want);
1113
1114         if (result != CONTAINS_UNKNOWN)
1115                 return result;
1116
1117         push_to_contains_stack(candidate, &contains_stack);
1118         while (contains_stack.nr) {
1119                 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
1120                 struct commit *commit = entry->commit;
1121                 struct commit_list *parents = entry->parents;
1122
1123                 if (!parents) {
1124                         commit->object.flags |= UNINTERESTING;
1125                         contains_stack.nr--;
1126                 }
1127                 /*
1128                  * If we just popped the stack, parents->item has been marked,
1129                  * therefore contains_test will return a meaningful 0 or 1.
1130                  */
1131                 else switch (contains_test(parents->item, want)) {
1132                 case CONTAINS_YES:
1133                         commit->object.flags |= TMP_MARK;
1134                         contains_stack.nr--;
1135                         break;
1136                 case CONTAINS_NO:
1137                         entry->parents = parents->next;
1138                         break;
1139                 case CONTAINS_UNKNOWN:
1140                         push_to_contains_stack(parents->item, &contains_stack);
1141                         break;
1142                 }
1143         }
1144         free(contains_stack.contains_stack);
1145         return contains_test(candidate, want);
1146 }
1147
1148 static int commit_contains(struct ref_filter *filter, struct commit *commit)
1149 {
1150         if (filter->with_commit_tag_algo)
1151                 return contains_tag_algo(commit, filter->with_commit);
1152         return is_descendant_of(commit, filter->with_commit);
1153 }
1154
1155 /*
1156  * Return 1 if the refname matches one of the patterns, otherwise 0.
1157  * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1158  * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1159  * matches "refs/heads/mas*", too).
1160  */
1161 static int match_pattern(const char **patterns, const char *refname)
1162 {
1163         /*
1164          * When no '--format' option is given we need to skip the prefix
1165          * for matching refs of tags and branches.
1166          */
1167         (void)(skip_prefix(refname, "refs/tags/", &refname) ||
1168                skip_prefix(refname, "refs/heads/", &refname) ||
1169                skip_prefix(refname, "refs/remotes/", &refname) ||
1170                skip_prefix(refname, "refs/", &refname));
1171
1172         for (; *patterns; patterns++) {
1173                 if (!wildmatch(*patterns, refname, 0, NULL))
1174                         return 1;
1175         }
1176         return 0;
1177 }
1178
1179 /*
1180  * Return 1 if the refname matches one of the patterns, otherwise 0.
1181  * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1182  * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1183  * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1184  */
1185 static int match_name_as_path(const char **pattern, const char *refname)
1186 {
1187         int namelen = strlen(refname);
1188         for (; *pattern; pattern++) {
1189                 const char *p = *pattern;
1190                 int plen = strlen(p);
1191
1192                 if ((plen <= namelen) &&
1193                     !strncmp(refname, p, plen) &&
1194                     (refname[plen] == '\0' ||
1195                      refname[plen] == '/' ||
1196                      p[plen-1] == '/'))
1197                         return 1;
1198                 if (!wildmatch(p, refname, WM_PATHNAME, NULL))
1199                         return 1;
1200         }
1201         return 0;
1202 }
1203
1204 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1205 static int filter_pattern_match(struct ref_filter *filter, const char *refname)
1206 {
1207         if (!*filter->name_patterns)
1208                 return 1; /* No pattern always matches */
1209         if (filter->match_as_path)
1210                 return match_name_as_path(filter->name_patterns, refname);
1211         return match_pattern(filter->name_patterns, refname);
1212 }
1213
1214 /*
1215  * Given a ref (sha1, refname), check if the ref belongs to the array
1216  * of sha1s. If the given ref is a tag, check if the given tag points
1217  * at one of the sha1s in the given sha1 array.
1218  * the given sha1_array.
1219  * NEEDSWORK:
1220  * 1. Only a single level of inderection is obtained, we might want to
1221  * change this to account for multiple levels (e.g. annotated tags
1222  * pointing to annotated tags pointing to a commit.)
1223  * 2. As the refs are cached we might know what refname peels to without
1224  * the need to parse the object via parse_object(). peel_ref() might be a
1225  * more efficient alternative to obtain the pointee.
1226  */
1227 static const unsigned char *match_points_at(struct sha1_array *points_at,
1228                                             const unsigned char *sha1,
1229                                             const char *refname)
1230 {
1231         const unsigned char *tagged_sha1 = NULL;
1232         struct object *obj;
1233
1234         if (sha1_array_lookup(points_at, sha1) >= 0)
1235                 return sha1;
1236         obj = parse_object(sha1);
1237         if (!obj)
1238                 die(_("malformed object at '%s'"), refname);
1239         if (obj->type == OBJ_TAG)
1240                 tagged_sha1 = ((struct tag *)obj)->tagged->oid.hash;
1241         if (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)
1242                 return tagged_sha1;
1243         return NULL;
1244 }
1245
1246 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1247 static struct ref_array_item *new_ref_array_item(const char *refname,
1248                                                  const unsigned char *objectname,
1249                                                  int flag)
1250 {
1251         size_t len = strlen(refname);
1252         struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item) + len + 1);
1253         memcpy(ref->refname, refname, len);
1254         ref->refname[len] = '\0';
1255         hashcpy(ref->objectname, objectname);
1256         ref->flag = flag;
1257
1258         return ref;
1259 }
1260
1261 static int filter_ref_kind(struct ref_filter *filter, const char *refname)
1262 {
1263         unsigned int i;
1264
1265         static struct {
1266                 const char *prefix;
1267                 unsigned int kind;
1268         } ref_kind[] = {
1269                 { "refs/heads/" , FILTER_REFS_BRANCHES },
1270                 { "refs/remotes/" , FILTER_REFS_REMOTES },
1271                 { "refs/tags/", FILTER_REFS_TAGS}
1272         };
1273
1274         if (filter->kind == FILTER_REFS_BRANCHES ||
1275             filter->kind == FILTER_REFS_REMOTES ||
1276             filter->kind == FILTER_REFS_TAGS)
1277                 return filter->kind;
1278         else if (!strcmp(refname, "HEAD"))
1279                 return FILTER_REFS_DETACHED_HEAD;
1280
1281         for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
1282                 if (starts_with(refname, ref_kind[i].prefix))
1283                         return ref_kind[i].kind;
1284         }
1285
1286         return FILTER_REFS_OTHERS;
1287 }
1288
1289 /*
1290  * A call-back given to for_each_ref().  Filter refs and keep them for
1291  * later object processing.
1292  */
1293 static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
1294 {
1295         struct ref_filter_cbdata *ref_cbdata = cb_data;
1296         struct ref_filter *filter = ref_cbdata->filter;
1297         struct ref_array_item *ref;
1298         struct commit *commit = NULL;
1299         unsigned int kind;
1300
1301         if (flag & REF_BAD_NAME) {
1302                 warning("ignoring ref with broken name %s", refname);
1303                 return 0;
1304         }
1305
1306         if (flag & REF_ISBROKEN) {
1307                 warning("ignoring broken ref %s", refname);
1308                 return 0;
1309         }
1310
1311         /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1312         kind = filter_ref_kind(filter, refname);
1313         if (!(kind & filter->kind))
1314                 return 0;
1315
1316         if (!filter_pattern_match(filter, refname))
1317                 return 0;
1318
1319         if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
1320                 return 0;
1321
1322         /*
1323          * A merge filter is applied on refs pointing to commits. Hence
1324          * obtain the commit using the 'oid' available and discard all
1325          * non-commits early. The actual filtering is done later.
1326          */
1327         if (filter->merge_commit || filter->with_commit || filter->verbose) {
1328                 commit = lookup_commit_reference_gently(oid->hash, 1);
1329                 if (!commit)
1330                         return 0;
1331                 /* We perform the filtering for the '--contains' option */
1332                 if (filter->with_commit &&
1333                     !commit_contains(filter, commit))
1334                         return 0;
1335         }
1336
1337         /*
1338          * We do not open the object yet; sort may only need refname
1339          * to do its job and the resulting list may yet to be pruned
1340          * by maxcount logic.
1341          */
1342         ref = new_ref_array_item(refname, oid->hash, flag);
1343         ref->commit = commit;
1344
1345         REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);
1346         ref_cbdata->array->items[ref_cbdata->array->nr++] = ref;
1347         ref->kind = kind;
1348         return 0;
1349 }
1350
1351 /*  Free memory allocated for a ref_array_item */
1352 static void free_array_item(struct ref_array_item *item)
1353 {
1354         free((char *)item->symref);
1355         free(item);
1356 }
1357
1358 /* Free all memory allocated for ref_array */
1359 void ref_array_clear(struct ref_array *array)
1360 {
1361         int i;
1362
1363         for (i = 0; i < array->nr; i++)
1364                 free_array_item(array->items[i]);
1365         free(array->items);
1366         array->items = NULL;
1367         array->nr = array->alloc = 0;
1368 }
1369
1370 static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata)
1371 {
1372         struct rev_info revs;
1373         int i, old_nr;
1374         struct ref_filter *filter = ref_cbdata->filter;
1375         struct ref_array *array = ref_cbdata->array;
1376         struct commit **to_clear = xcalloc(sizeof(struct commit *), array->nr);
1377
1378         init_revisions(&revs, NULL);
1379
1380         for (i = 0; i < array->nr; i++) {
1381                 struct ref_array_item *item = array->items[i];
1382                 add_pending_object(&revs, &item->commit->object, item->refname);
1383                 to_clear[i] = item->commit;
1384         }
1385
1386         filter->merge_commit->object.flags |= UNINTERESTING;
1387         add_pending_object(&revs, &filter->merge_commit->object, "");
1388
1389         revs.limited = 1;
1390         if (prepare_revision_walk(&revs))
1391                 die(_("revision walk setup failed"));
1392
1393         old_nr = array->nr;
1394         array->nr = 0;
1395
1396         for (i = 0; i < old_nr; i++) {
1397                 struct ref_array_item *item = array->items[i];
1398                 struct commit *commit = item->commit;
1399
1400                 int is_merged = !!(commit->object.flags & UNINTERESTING);
1401
1402                 if (is_merged == (filter->merge == REF_FILTER_MERGED_INCLUDE))
1403                         array->items[array->nr++] = array->items[i];
1404                 else
1405                         free_array_item(item);
1406         }
1407
1408         for (i = 0; i < old_nr; i++)
1409                 clear_commit_marks(to_clear[i], ALL_REV_FLAGS);
1410         clear_commit_marks(filter->merge_commit, ALL_REV_FLAGS);
1411         free(to_clear);
1412 }
1413
1414 /*
1415  * API for filtering a set of refs. Based on the type of refs the user
1416  * has requested, we iterate through those refs and apply filters
1417  * as per the given ref_filter structure and finally store the
1418  * filtered refs in the ref_array structure.
1419  */
1420 int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
1421 {
1422         struct ref_filter_cbdata ref_cbdata;
1423         int ret = 0;
1424         unsigned int broken = 0;
1425
1426         ref_cbdata.array = array;
1427         ref_cbdata.filter = filter;
1428
1429         if (type & FILTER_REFS_INCLUDE_BROKEN)
1430                 broken = 1;
1431         filter->kind = type & FILTER_REFS_KIND_MASK;
1432
1433         /*  Simple per-ref filtering */
1434         if (!filter->kind)
1435                 die("filter_refs: invalid type");
1436         else {
1437                 /*
1438                  * For common cases where we need only branches or remotes or tags,
1439                  * we only iterate through those refs. If a mix of refs is needed,
1440                  * we iterate over all refs and filter out required refs with the help
1441                  * of filter_ref_kind().
1442                  */
1443                 if (filter->kind == FILTER_REFS_BRANCHES)
1444                         ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken);
1445                 else if (filter->kind == FILTER_REFS_REMOTES)
1446                         ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken);
1447                 else if (filter->kind == FILTER_REFS_TAGS)
1448                         ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken);
1449                 else if (filter->kind & FILTER_REFS_ALL)
1450                         ret = for_each_fullref_in("", ref_filter_handler, &ref_cbdata, broken);
1451                 if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
1452                         head_ref(ref_filter_handler, &ref_cbdata);
1453         }
1454
1455
1456         /*  Filters that need revision walking */
1457         if (filter->merge_commit)
1458                 do_merge_filter(&ref_cbdata);
1459
1460         return ret;
1461 }
1462
1463 static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
1464 {
1465         struct atom_value *va, *vb;
1466         int cmp;
1467         cmp_type cmp_type = used_atom_type[s->atom];
1468
1469         get_ref_atom_value(a, s->atom, &va);
1470         get_ref_atom_value(b, s->atom, &vb);
1471         if (s->version)
1472                 cmp = versioncmp(va->s, vb->s);
1473         else if (cmp_type == FIELD_STR)
1474                 cmp = strcmp(va->s, vb->s);
1475         else {
1476                 if (va->ul < vb->ul)
1477                         cmp = -1;
1478                 else if (va->ul == vb->ul)
1479                         cmp = strcmp(a->refname, b->refname);
1480                 else
1481                         cmp = 1;
1482         }
1483
1484         return (s->reverse) ? -cmp : cmp;
1485 }
1486
1487 static struct ref_sorting *ref_sorting;
1488 static int compare_refs(const void *a_, const void *b_)
1489 {
1490         struct ref_array_item *a = *((struct ref_array_item **)a_);
1491         struct ref_array_item *b = *((struct ref_array_item **)b_);
1492         struct ref_sorting *s;
1493
1494         for (s = ref_sorting; s; s = s->next) {
1495                 int cmp = cmp_ref_sorting(s, a, b);
1496                 if (cmp)
1497                         return cmp;
1498         }
1499         return 0;
1500 }
1501
1502 void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
1503 {
1504         ref_sorting = sorting;
1505         qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
1506 }
1507
1508 static int hex1(char ch)
1509 {
1510         if ('0' <= ch && ch <= '9')
1511                 return ch - '0';
1512         else if ('a' <= ch && ch <= 'f')
1513                 return ch - 'a' + 10;
1514         else if ('A' <= ch && ch <= 'F')
1515                 return ch - 'A' + 10;
1516         return -1;
1517 }
1518 static int hex2(const char *cp)
1519 {
1520         if (cp[0] && cp[1])
1521                 return (hex1(cp[0]) << 4) | hex1(cp[1]);
1522         else
1523                 return -1;
1524 }
1525
1526 static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
1527 {
1528         struct strbuf *s = &state->stack->output;
1529
1530         while (*cp && (!ep || cp < ep)) {
1531                 if (*cp == '%') {
1532                         if (cp[1] == '%')
1533                                 cp++;
1534                         else {
1535                                 int ch = hex2(cp + 1);
1536                                 if (0 <= ch) {
1537                                         strbuf_addch(s, ch);
1538                                         cp += 3;
1539                                         continue;
1540                                 }
1541                         }
1542                 }
1543                 strbuf_addch(s, *cp);
1544                 cp++;
1545         }
1546 }
1547
1548 void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
1549 {
1550         const char *cp, *sp, *ep;
1551         struct strbuf *final_buf;
1552         struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
1553
1554         state.quote_style = quote_style;
1555         push_stack_element(&state.stack);
1556
1557         for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
1558                 struct atom_value *atomv;
1559
1560                 ep = strchr(sp, ')');
1561                 if (cp < sp)
1562                         append_literal(cp, sp, &state);
1563                 get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
1564                 atomv->handler(atomv, &state);
1565         }
1566         if (*cp) {
1567                 sp = cp + strlen(cp);
1568                 append_literal(cp, sp, &state);
1569         }
1570         if (need_color_reset_at_eol) {
1571                 struct atom_value resetv;
1572                 char color[COLOR_MAXLEN] = "";
1573
1574                 if (color_parse("reset", color) < 0)
1575                         die("BUG: couldn't parse 'reset' as a color");
1576                 resetv.s = color;
1577                 append_atom(&resetv, &state);
1578         }
1579         if (state.stack->prev)
1580                 die(_("format: %%(end) atom missing"));
1581         final_buf = &state.stack->output;
1582         fwrite(final_buf->buf, 1, final_buf->len, stdout);
1583         pop_stack_element(&state.stack);
1584         putchar('\n');
1585 }
1586
1587 /*  If no sorting option is given, use refname to sort as default */
1588 struct ref_sorting *ref_default_sorting(void)
1589 {
1590         static const char cstr_name[] = "refname";
1591
1592         struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
1593
1594         sorting->next = NULL;
1595         sorting->atom = parse_ref_filter_atom(cstr_name, cstr_name + strlen(cstr_name));
1596         return sorting;
1597 }
1598
1599 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
1600 {
1601         struct ref_sorting **sorting_tail = opt->value;
1602         struct ref_sorting *s;
1603         int len;
1604
1605         if (!arg) /* should --no-sort void the list ? */
1606                 return -1;
1607
1608         s = xcalloc(1, sizeof(*s));
1609         s->next = *sorting_tail;
1610         *sorting_tail = s;
1611
1612         if (*arg == '-') {
1613                 s->reverse = 1;
1614                 arg++;
1615         }
1616         if (skip_prefix(arg, "version:", &arg) ||
1617             skip_prefix(arg, "v:", &arg))
1618                 s->version = 1;
1619         len = strlen(arg);
1620         s->atom = parse_ref_filter_atom(arg, arg+len);
1621         return 0;
1622 }
1623
1624 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
1625 {
1626         struct ref_filter *rf = opt->value;
1627         unsigned char sha1[20];
1628
1629         rf->merge = starts_with(opt->long_name, "no")
1630                 ? REF_FILTER_MERGED_OMIT
1631                 : REF_FILTER_MERGED_INCLUDE;
1632
1633         if (get_sha1(arg, sha1))
1634                 die(_("malformed object name %s"), arg);
1635
1636         rf->merge_commit = lookup_commit_reference_gently(sha1, 0);
1637         if (!rf->merge_commit)
1638                 return opterror(opt, "must point to a commit", 0);
1639
1640         return 0;
1641 }