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