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