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