ref-filter: add objectsize to used_atom
[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 "object-store.h"
7 #include "repository.h"
8 #include "commit.h"
9 #include "remote.h"
10 #include "color.h"
11 #include "tag.h"
12 #include "quote.h"
13 #include "ref-filter.h"
14 #include "revision.h"
15 #include "utf8.h"
16 #include "git-compat-util.h"
17 #include "version.h"
18 #include "trailer.h"
19 #include "wt-status.h"
20 #include "commit-slab.h"
21 #include "commit-graph.h"
22 #include "commit-reach.h"
23 #include "worktree.h"
24 #include "hashmap.h"
25 #include "strvec.h"
26
27 static struct ref_msg {
28         const char *gone;
29         const char *ahead;
30         const char *behind;
31         const char *ahead_behind;
32 } msgs = {
33          /* Untranslated plumbing messages: */
34         "gone",
35         "ahead %d",
36         "behind %d",
37         "ahead %d, behind %d"
38 };
39
40 void setup_ref_filter_porcelain_msg(void)
41 {
42         msgs.gone = _("gone");
43         msgs.ahead = _("ahead %d");
44         msgs.behind = _("behind %d");
45         msgs.ahead_behind = _("ahead %d, behind %d");
46 }
47
48 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
49 typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status;
50 typedef enum { SOURCE_NONE = 0, SOURCE_OBJ, SOURCE_OTHER } info_source;
51
52 struct align {
53         align_type position;
54         unsigned int width;
55 };
56
57 struct if_then_else {
58         cmp_status cmp_status;
59         const char *str;
60         unsigned int then_atom_seen : 1,
61                 else_atom_seen : 1,
62                 condition_satisfied : 1;
63 };
64
65 struct refname_atom {
66         enum { R_NORMAL, R_SHORT, R_LSTRIP, R_RSTRIP } option;
67         int lstrip, rstrip;
68 };
69
70 static struct ref_trailer_buf {
71         struct string_list filter_list;
72         struct strbuf sepbuf;
73         struct strbuf kvsepbuf;
74 } ref_trailer_buf = {STRING_LIST_INIT_NODUP, STRBUF_INIT, STRBUF_INIT};
75
76 static struct expand_data {
77         struct object_id oid;
78         enum object_type type;
79         unsigned long size;
80         off_t disk_size;
81         struct object_id delta_base_oid;
82         void *content;
83
84         struct object_info info;
85 } oi, oi_deref;
86
87 struct ref_to_worktree_entry {
88         struct hashmap_entry ent;
89         struct worktree *wt; /* key is wt->head_ref */
90 };
91
92 static int ref_to_worktree_map_cmpfnc(const void *unused_lookupdata,
93                                       const struct hashmap_entry *eptr,
94                                       const struct hashmap_entry *kptr,
95                                       const void *keydata_aka_refname)
96 {
97         const struct ref_to_worktree_entry *e, *k;
98
99         e = container_of(eptr, const struct ref_to_worktree_entry, ent);
100         k = container_of(kptr, const struct ref_to_worktree_entry, ent);
101
102         return strcmp(e->wt->head_ref,
103                 keydata_aka_refname ? keydata_aka_refname : k->wt->head_ref);
104 }
105
106 static struct ref_to_worktree_map {
107         struct hashmap map;
108         struct worktree **worktrees;
109 } ref_to_worktree_map;
110
111 /*
112  * An atom is a valid field atom listed below, possibly prefixed with
113  * a "*" to denote deref_tag().
114  *
115  * We parse given format string and sort specifiers, and make a list
116  * of properties that we need to extract out of objects.  ref_array_item
117  * structure will hold an array of values extracted that can be
118  * indexed with the "atom number", which is an index into this
119  * array.
120  */
121 static struct used_atom {
122         const char *name;
123         cmp_type type;
124         info_source source;
125         union {
126                 char color[COLOR_MAXLEN];
127                 struct align align;
128                 struct {
129                         enum {
130                                 RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME, RR_REMOTE_REF
131                         } option;
132                         struct refname_atom refname;
133                         unsigned int nobracket : 1, push : 1, push_remote : 1;
134                 } remote_ref;
135                 struct {
136                         enum { C_BARE, C_BODY, C_BODY_DEP, C_LENGTH, C_LINES,
137                                C_SIG, C_SUB, C_SUB_SANITIZE, C_TRAILERS } option;
138                         struct process_trailer_options trailer_opts;
139                         unsigned int nlines;
140                 } contents;
141                 struct {
142                         cmp_status cmp_status;
143                         const char *str;
144                 } if_then_else;
145                 struct {
146                         enum { O_FULL, O_LENGTH, O_SHORT } option;
147                         unsigned int length;
148                 } oid;
149                 struct {
150                         enum { O_SIZE, O_SIZE_DISK } option;
151                 } objectsize;
152                 struct email_option {
153                         enum { EO_RAW, EO_TRIM, EO_LOCALPART } option;
154                 } email_option;
155                 struct refname_atom refname;
156                 char *head;
157         } u;
158 } *used_atom;
159 static int used_atom_cnt, need_tagged, need_symref;
160
161 /*
162  * Expand string, append it to strbuf *sb, then return error code ret.
163  * Allow to save few lines of code.
164  */
165 static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...)
166 {
167         va_list ap;
168         va_start(ap, fmt);
169         strbuf_vaddf(sb, fmt, ap);
170         va_end(ap);
171         return ret;
172 }
173
174 static int color_atom_parser(const struct ref_format *format, struct used_atom *atom,
175                              const char *color_value, struct strbuf *err)
176 {
177         if (!color_value)
178                 return strbuf_addf_ret(err, -1, _("expected format: %%(color:<color>)"));
179         if (color_parse(color_value, atom->u.color) < 0)
180                 return strbuf_addf_ret(err, -1, _("unrecognized color: %%(color:%s)"),
181                                        color_value);
182         /*
183          * We check this after we've parsed the color, which lets us complain
184          * about syntactically bogus color names even if they won't be used.
185          */
186         if (!want_color(format->use_color))
187                 color_parse("", atom->u.color);
188         return 0;
189 }
190
191 static int refname_atom_parser_internal(struct refname_atom *atom, const char *arg,
192                                          const char *name, struct strbuf *err)
193 {
194         if (!arg)
195                 atom->option = R_NORMAL;
196         else if (!strcmp(arg, "short"))
197                 atom->option = R_SHORT;
198         else if (skip_prefix(arg, "lstrip=", &arg) ||
199                  skip_prefix(arg, "strip=", &arg)) {
200                 atom->option = R_LSTRIP;
201                 if (strtol_i(arg, 10, &atom->lstrip))
202                         return strbuf_addf_ret(err, -1, _("Integer value expected refname:lstrip=%s"), arg);
203         } else if (skip_prefix(arg, "rstrip=", &arg)) {
204                 atom->option = R_RSTRIP;
205                 if (strtol_i(arg, 10, &atom->rstrip))
206                         return strbuf_addf_ret(err, -1, _("Integer value expected refname:rstrip=%s"), arg);
207         } else
208                 return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), name, arg);
209         return 0;
210 }
211
212 static int remote_ref_atom_parser(const struct ref_format *format, struct used_atom *atom,
213                                   const char *arg, struct strbuf *err)
214 {
215         struct string_list params = STRING_LIST_INIT_DUP;
216         int i;
217
218         if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:"))
219                 atom->u.remote_ref.push = 1;
220
221         if (!arg) {
222                 atom->u.remote_ref.option = RR_REF;
223                 return refname_atom_parser_internal(&atom->u.remote_ref.refname,
224                                                     arg, atom->name, err);
225         }
226
227         atom->u.remote_ref.nobracket = 0;
228         string_list_split(&params, arg, ',', -1);
229
230         for (i = 0; i < params.nr; i++) {
231                 const char *s = params.items[i].string;
232
233                 if (!strcmp(s, "track"))
234                         atom->u.remote_ref.option = RR_TRACK;
235                 else if (!strcmp(s, "trackshort"))
236                         atom->u.remote_ref.option = RR_TRACKSHORT;
237                 else if (!strcmp(s, "nobracket"))
238                         atom->u.remote_ref.nobracket = 1;
239                 else if (!strcmp(s, "remotename")) {
240                         atom->u.remote_ref.option = RR_REMOTE_NAME;
241                         atom->u.remote_ref.push_remote = 1;
242                 } else if (!strcmp(s, "remoteref")) {
243                         atom->u.remote_ref.option = RR_REMOTE_REF;
244                         atom->u.remote_ref.push_remote = 1;
245                 } else {
246                         atom->u.remote_ref.option = RR_REF;
247                         if (refname_atom_parser_internal(&atom->u.remote_ref.refname,
248                                                          arg, atom->name, err)) {
249                                 string_list_clear(&params, 0);
250                                 return -1;
251                         }
252                 }
253         }
254
255         string_list_clear(&params, 0);
256         return 0;
257 }
258
259 static int objecttype_atom_parser(const struct ref_format *format, struct used_atom *atom,
260                                   const char *arg, struct strbuf *err)
261 {
262         if (arg)
263                 return strbuf_addf_ret(err, -1, _("%%(objecttype) does not take arguments"));
264         if (*atom->name == '*')
265                 oi_deref.info.typep = &oi_deref.type;
266         else
267                 oi.info.typep = &oi.type;
268         return 0;
269 }
270
271 static int objectsize_atom_parser(const struct ref_format *format, struct used_atom *atom,
272                                   const char *arg, struct strbuf *err)
273 {
274         if (!arg) {
275                 atom->u.objectsize.option = O_SIZE;
276                 if (*atom->name == '*')
277                         oi_deref.info.sizep = &oi_deref.size;
278                 else
279                         oi.info.sizep = &oi.size;
280         } else if (!strcmp(arg, "disk")) {
281                 atom->u.objectsize.option = O_SIZE_DISK;
282                 if (*atom->name == '*')
283                         oi_deref.info.disk_sizep = &oi_deref.disk_size;
284                 else
285                         oi.info.disk_sizep = &oi.disk_size;
286         } else
287                 return strbuf_addf_ret(err, -1, _("unrecognized %%(objectsize) argument: %s"), arg);
288         return 0;
289 }
290
291 static int deltabase_atom_parser(const struct ref_format *format, struct used_atom *atom,
292                                  const char *arg, struct strbuf *err)
293 {
294         if (arg)
295                 return strbuf_addf_ret(err, -1, _("%%(deltabase) does not take arguments"));
296         if (*atom->name == '*')
297                 oi_deref.info.delta_base_oid = &oi_deref.delta_base_oid;
298         else
299                 oi.info.delta_base_oid = &oi.delta_base_oid;
300         return 0;
301 }
302
303 static int body_atom_parser(const struct ref_format *format, struct used_atom *atom,
304                             const char *arg, struct strbuf *err)
305 {
306         if (arg)
307                 return strbuf_addf_ret(err, -1, _("%%(body) does not take arguments"));
308         atom->u.contents.option = C_BODY_DEP;
309         return 0;
310 }
311
312 static int subject_atom_parser(const struct ref_format *format, struct used_atom *atom,
313                                const char *arg, struct strbuf *err)
314 {
315         if (!arg)
316                 atom->u.contents.option = C_SUB;
317         else if (!strcmp(arg, "sanitize"))
318                 atom->u.contents.option = C_SUB_SANITIZE;
319         else
320                 return strbuf_addf_ret(err, -1, _("unrecognized %%(subject) argument: %s"), arg);
321         return 0;
322 }
323
324 static int trailers_atom_parser(const struct ref_format *format, struct used_atom *atom,
325                                 const char *arg, struct strbuf *err)
326 {
327         atom->u.contents.trailer_opts.no_divider = 1;
328
329         if (arg) {
330                 const char *argbuf = xstrfmt("%s)", arg);
331                 char *invalid_arg = NULL;
332
333                 if (format_set_trailers_options(&atom->u.contents.trailer_opts,
334                     &ref_trailer_buf.filter_list,
335                     &ref_trailer_buf.sepbuf,
336                     &ref_trailer_buf.kvsepbuf,
337                     &argbuf, &invalid_arg)) {
338                         if (!invalid_arg)
339                                 strbuf_addf(err, _("expected %%(trailers:key=<value>)"));
340                         else
341                                 strbuf_addf(err, _("unknown %%(trailers) argument: %s"), invalid_arg);
342                         free((char *)invalid_arg);
343                         return -1;
344                 }
345         }
346         atom->u.contents.option = C_TRAILERS;
347         return 0;
348 }
349
350 static int contents_atom_parser(const struct ref_format *format, struct used_atom *atom,
351                                 const char *arg, struct strbuf *err)
352 {
353         if (!arg)
354                 atom->u.contents.option = C_BARE;
355         else if (!strcmp(arg, "body"))
356                 atom->u.contents.option = C_BODY;
357         else if (!strcmp(arg, "size"))
358                 atom->u.contents.option = C_LENGTH;
359         else if (!strcmp(arg, "signature"))
360                 atom->u.contents.option = C_SIG;
361         else if (!strcmp(arg, "subject"))
362                 atom->u.contents.option = C_SUB;
363         else if (!strcmp(arg, "trailers")) {
364                 if (trailers_atom_parser(format, atom, NULL, err))
365                         return -1;
366         } else if (skip_prefix(arg, "trailers:", &arg)) {
367                 if (trailers_atom_parser(format, atom, arg, err))
368                         return -1;
369         } else if (skip_prefix(arg, "lines=", &arg)) {
370                 atom->u.contents.option = C_LINES;
371                 if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
372                         return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg);
373         } else
374                 return strbuf_addf_ret(err, -1, _("unrecognized %%(contents) argument: %s"), arg);
375         return 0;
376 }
377
378 static int oid_atom_parser(const struct ref_format *format, struct used_atom *atom,
379                            const char *arg, struct strbuf *err)
380 {
381         if (!arg)
382                 atom->u.oid.option = O_FULL;
383         else if (!strcmp(arg, "short"))
384                 atom->u.oid.option = O_SHORT;
385         else if (skip_prefix(arg, "short=", &arg)) {
386                 atom->u.oid.option = O_LENGTH;
387                 if (strtoul_ui(arg, 10, &atom->u.oid.length) ||
388                     atom->u.oid.length == 0)
389                         return strbuf_addf_ret(err, -1, _("positive value expected '%s' in %%(%s)"), arg, atom->name);
390                 if (atom->u.oid.length < MINIMUM_ABBREV)
391                         atom->u.oid.length = MINIMUM_ABBREV;
392         } else
393                 return strbuf_addf_ret(err, -1, _("unrecognized argument '%s' in %%(%s)"), arg, atom->name);
394         return 0;
395 }
396
397 static int person_email_atom_parser(const struct ref_format *format, struct used_atom *atom,
398                                     const char *arg, struct strbuf *err)
399 {
400         if (!arg)
401                 atom->u.email_option.option = EO_RAW;
402         else if (!strcmp(arg, "trim"))
403                 atom->u.email_option.option = EO_TRIM;
404         else if (!strcmp(arg, "localpart"))
405                 atom->u.email_option.option = EO_LOCALPART;
406         else
407                 return strbuf_addf_ret(err, -1, _("unrecognized email option: %s"), arg);
408         return 0;
409 }
410
411 static int refname_atom_parser(const struct ref_format *format, struct used_atom *atom,
412                                const char *arg, struct strbuf *err)
413 {
414         return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err);
415 }
416
417 static align_type parse_align_position(const char *s)
418 {
419         if (!strcmp(s, "right"))
420                 return ALIGN_RIGHT;
421         else if (!strcmp(s, "middle"))
422                 return ALIGN_MIDDLE;
423         else if (!strcmp(s, "left"))
424                 return ALIGN_LEFT;
425         return -1;
426 }
427
428 static int align_atom_parser(const struct ref_format *format, struct used_atom *atom,
429                              const char *arg, struct strbuf *err)
430 {
431         struct align *align = &atom->u.align;
432         struct string_list params = STRING_LIST_INIT_DUP;
433         int i;
434         unsigned int width = ~0U;
435
436         if (!arg)
437                 return strbuf_addf_ret(err, -1, _("expected format: %%(align:<width>,<position>)"));
438
439         align->position = ALIGN_LEFT;
440
441         string_list_split(&params, arg, ',', -1);
442         for (i = 0; i < params.nr; i++) {
443                 const char *s = params.items[i].string;
444                 int position;
445
446                 if (skip_prefix(s, "position=", &s)) {
447                         position = parse_align_position(s);
448                         if (position < 0) {
449                                 strbuf_addf(err, _("unrecognized position:%s"), s);
450                                 string_list_clear(&params, 0);
451                                 return -1;
452                         }
453                         align->position = position;
454                 } else if (skip_prefix(s, "width=", &s)) {
455                         if (strtoul_ui(s, 10, &width)) {
456                                 strbuf_addf(err, _("unrecognized width:%s"), s);
457                                 string_list_clear(&params, 0);
458                                 return -1;
459                         }
460                 } else if (!strtoul_ui(s, 10, &width))
461                         ;
462                 else if ((position = parse_align_position(s)) >= 0)
463                         align->position = position;
464                 else {
465                         strbuf_addf(err, _("unrecognized %%(align) argument: %s"), s);
466                         string_list_clear(&params, 0);
467                         return -1;
468                 }
469         }
470
471         if (width == ~0U) {
472                 string_list_clear(&params, 0);
473                 return strbuf_addf_ret(err, -1, _("positive width expected with the %%(align) atom"));
474         }
475         align->width = width;
476         string_list_clear(&params, 0);
477         return 0;
478 }
479
480 static int if_atom_parser(const struct ref_format *format, struct used_atom *atom,
481                           const char *arg, struct strbuf *err)
482 {
483         if (!arg) {
484                 atom->u.if_then_else.cmp_status = COMPARE_NONE;
485                 return 0;
486         } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) {
487                 atom->u.if_then_else.cmp_status = COMPARE_EQUAL;
488         } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) {
489                 atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL;
490         } else
491                 return strbuf_addf_ret(err, -1, _("unrecognized %%(if) argument: %s"), arg);
492         return 0;
493 }
494
495 static int head_atom_parser(const struct ref_format *format, struct used_atom *atom,
496                             const char *arg, struct strbuf *unused_err)
497 {
498         atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
499         return 0;
500 }
501
502 static struct {
503         const char *name;
504         info_source source;
505         cmp_type cmp_type;
506         int (*parser)(const struct ref_format *format, struct used_atom *atom,
507                       const char *arg, struct strbuf *err);
508 } valid_atom[] = {
509         { "refname", SOURCE_NONE, FIELD_STR, refname_atom_parser },
510         { "objecttype", SOURCE_OTHER, FIELD_STR, objecttype_atom_parser },
511         { "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser },
512         { "objectname", SOURCE_OTHER, FIELD_STR, oid_atom_parser },
513         { "deltabase", SOURCE_OTHER, FIELD_STR, deltabase_atom_parser },
514         { "tree", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
515         { "parent", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
516         { "numparent", SOURCE_OBJ, FIELD_ULONG },
517         { "object", SOURCE_OBJ },
518         { "type", SOURCE_OBJ },
519         { "tag", SOURCE_OBJ },
520         { "author", SOURCE_OBJ },
521         { "authorname", SOURCE_OBJ },
522         { "authoremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
523         { "authordate", SOURCE_OBJ, FIELD_TIME },
524         { "committer", SOURCE_OBJ },
525         { "committername", SOURCE_OBJ },
526         { "committeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
527         { "committerdate", SOURCE_OBJ, FIELD_TIME },
528         { "tagger", SOURCE_OBJ },
529         { "taggername", SOURCE_OBJ },
530         { "taggeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
531         { "taggerdate", SOURCE_OBJ, FIELD_TIME },
532         { "creator", SOURCE_OBJ },
533         { "creatordate", SOURCE_OBJ, FIELD_TIME },
534         { "subject", SOURCE_OBJ, FIELD_STR, subject_atom_parser },
535         { "body", SOURCE_OBJ, FIELD_STR, body_atom_parser },
536         { "trailers", SOURCE_OBJ, FIELD_STR, trailers_atom_parser },
537         { "contents", SOURCE_OBJ, FIELD_STR, contents_atom_parser },
538         { "upstream", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
539         { "push", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
540         { "symref", SOURCE_NONE, FIELD_STR, refname_atom_parser },
541         { "flag", SOURCE_NONE },
542         { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser },
543         { "color", SOURCE_NONE, FIELD_STR, color_atom_parser },
544         { "worktreepath", SOURCE_NONE },
545         { "align", SOURCE_NONE, FIELD_STR, align_atom_parser },
546         { "end", SOURCE_NONE },
547         { "if", SOURCE_NONE, FIELD_STR, if_atom_parser },
548         { "then", SOURCE_NONE },
549         { "else", SOURCE_NONE },
550         /*
551          * Please update $__git_ref_fieldlist in git-completion.bash
552          * when you add new atoms
553          */
554 };
555
556 #define REF_FORMATTING_STATE_INIT  { 0, NULL }
557
558 struct ref_formatting_stack {
559         struct ref_formatting_stack *prev;
560         struct strbuf output;
561         void (*at_end)(struct ref_formatting_stack **stack);
562         void *at_end_data;
563 };
564
565 struct ref_formatting_state {
566         int quote_style;
567         struct ref_formatting_stack *stack;
568 };
569
570 struct atom_value {
571         const char *s;
572         int (*handler)(struct atom_value *atomv, struct ref_formatting_state *state,
573                        struct strbuf *err);
574         uintmax_t value; /* used for sorting when not FIELD_STR */
575         struct used_atom *atom;
576 };
577
578 /*
579  * Used to parse format string and sort specifiers
580  */
581 static int parse_ref_filter_atom(const struct ref_format *format,
582                                  const char *atom, const char *ep,
583                                  struct strbuf *err)
584 {
585         const char *sp;
586         const char *arg;
587         int i, at, atom_len;
588
589         sp = atom;
590         if (*sp == '*' && sp < ep)
591                 sp++; /* deref */
592         if (ep <= sp)
593                 return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"),
594                                        (int)(ep-atom), atom);
595
596         /* Do we have the atom already used elsewhere? */
597         for (i = 0; i < used_atom_cnt; i++) {
598                 int len = strlen(used_atom[i].name);
599                 if (len == ep - atom && !memcmp(used_atom[i].name, atom, len))
600                         return i;
601         }
602
603         /*
604          * If the atom name has a colon, strip it and everything after
605          * it off - it specifies the format for this entry, and
606          * shouldn't be used for checking against the valid_atom
607          * table.
608          */
609         arg = memchr(sp, ':', ep - sp);
610         atom_len = (arg ? arg : ep) - sp;
611
612         /* Is the atom a valid one? */
613         for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
614                 int len = strlen(valid_atom[i].name);
615                 if (len == atom_len && !memcmp(valid_atom[i].name, sp, len))
616                         break;
617         }
618
619         if (ARRAY_SIZE(valid_atom) <= i)
620                 return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"),
621                                        (int)(ep-atom), atom);
622         if (valid_atom[i].source != SOURCE_NONE && !have_git_dir())
623                 return strbuf_addf_ret(err, -1,
624                                        _("not a git repository, but the field '%.*s' requires access to object data"),
625                                        (int)(ep-atom), atom);
626
627         /* Add it in, including the deref prefix */
628         at = used_atom_cnt;
629         used_atom_cnt++;
630         REALLOC_ARRAY(used_atom, used_atom_cnt);
631         used_atom[at].name = xmemdupz(atom, ep - atom);
632         used_atom[at].type = valid_atom[i].cmp_type;
633         used_atom[at].source = valid_atom[i].source;
634         if (used_atom[at].source == SOURCE_OBJ) {
635                 if (*atom == '*')
636                         oi_deref.info.contentp = &oi_deref.content;
637                 else
638                         oi.info.contentp = &oi.content;
639         }
640         if (arg) {
641                 arg = used_atom[at].name + (arg - atom) + 1;
642                 if (!*arg) {
643                         /*
644                          * Treat empty sub-arguments list as NULL (i.e.,
645                          * "%(atom:)" is equivalent to "%(atom)").
646                          */
647                         arg = NULL;
648                 }
649         }
650         memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
651         if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err))
652                 return -1;
653         if (*atom == '*')
654                 need_tagged = 1;
655         if (!strcmp(valid_atom[i].name, "symref"))
656                 need_symref = 1;
657         return at;
658 }
659
660 static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
661 {
662         switch (quote_style) {
663         case QUOTE_NONE:
664                 strbuf_addstr(s, str);
665                 break;
666         case QUOTE_SHELL:
667                 sq_quote_buf(s, str);
668                 break;
669         case QUOTE_PERL:
670                 perl_quote_buf(s, str);
671                 break;
672         case QUOTE_PYTHON:
673                 python_quote_buf(s, str);
674                 break;
675         case QUOTE_TCL:
676                 tcl_quote_buf(s, str);
677                 break;
678         }
679 }
680
681 static int append_atom(struct atom_value *v, struct ref_formatting_state *state,
682                        struct strbuf *unused_err)
683 {
684         /*
685          * Quote formatting is only done when the stack has a single
686          * element. Otherwise quote formatting is done on the
687          * element's entire output strbuf when the %(end) atom is
688          * encountered.
689          */
690         if (!state->stack->prev)
691                 quote_formatting(&state->stack->output, v->s, state->quote_style);
692         else
693                 strbuf_addstr(&state->stack->output, v->s);
694         return 0;
695 }
696
697 static void push_stack_element(struct ref_formatting_stack **stack)
698 {
699         struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
700
701         strbuf_init(&s->output, 0);
702         s->prev = *stack;
703         *stack = s;
704 }
705
706 static void pop_stack_element(struct ref_formatting_stack **stack)
707 {
708         struct ref_formatting_stack *current = *stack;
709         struct ref_formatting_stack *prev = current->prev;
710
711         if (prev)
712                 strbuf_addbuf(&prev->output, &current->output);
713         strbuf_release(&current->output);
714         free(current);
715         *stack = prev;
716 }
717
718 static void end_align_handler(struct ref_formatting_stack **stack)
719 {
720         struct ref_formatting_stack *cur = *stack;
721         struct align *align = (struct align *)cur->at_end_data;
722         struct strbuf s = STRBUF_INIT;
723
724         strbuf_utf8_align(&s, align->position, align->width, cur->output.buf);
725         strbuf_swap(&cur->output, &s);
726         strbuf_release(&s);
727 }
728
729 static int align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
730                               struct strbuf *unused_err)
731 {
732         struct ref_formatting_stack *new_stack;
733
734         push_stack_element(&state->stack);
735         new_stack = state->stack;
736         new_stack->at_end = end_align_handler;
737         new_stack->at_end_data = &atomv->atom->u.align;
738         return 0;
739 }
740
741 static void if_then_else_handler(struct ref_formatting_stack **stack)
742 {
743         struct ref_formatting_stack *cur = *stack;
744         struct ref_formatting_stack *prev = cur->prev;
745         struct if_then_else *if_then_else = (struct if_then_else *)cur->at_end_data;
746
747         if (!if_then_else->then_atom_seen)
748                 die(_("format: %%(if) atom used without a %%(then) atom"));
749
750         if (if_then_else->else_atom_seen) {
751                 /*
752                  * There is an %(else) atom: we need to drop one state from the
753                  * stack, either the %(else) branch if the condition is satisfied, or
754                  * the %(then) branch if it isn't.
755                  */
756                 if (if_then_else->condition_satisfied) {
757                         strbuf_reset(&cur->output);
758                         pop_stack_element(&cur);
759                 } else {
760                         strbuf_swap(&cur->output, &prev->output);
761                         strbuf_reset(&cur->output);
762                         pop_stack_element(&cur);
763                 }
764         } else if (!if_then_else->condition_satisfied) {
765                 /*
766                  * No %(else) atom: just drop the %(then) branch if the
767                  * condition is not satisfied.
768                  */
769                 strbuf_reset(&cur->output);
770         }
771
772         *stack = cur;
773         free(if_then_else);
774 }
775
776 static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
777                            struct strbuf *unused_err)
778 {
779         struct ref_formatting_stack *new_stack;
780         struct if_then_else *if_then_else = xcalloc(1,
781                                                     sizeof(struct if_then_else));
782
783         if_then_else->str = atomv->atom->u.if_then_else.str;
784         if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status;
785
786         push_stack_element(&state->stack);
787         new_stack = state->stack;
788         new_stack->at_end = if_then_else_handler;
789         new_stack->at_end_data = if_then_else;
790         return 0;
791 }
792
793 static int is_empty(const char *s)
794 {
795         while (*s != '\0') {
796                 if (!isspace(*s))
797                         return 0;
798                 s++;
799         }
800         return 1;
801 }
802
803 static int then_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
804                              struct strbuf *err)
805 {
806         struct ref_formatting_stack *cur = state->stack;
807         struct if_then_else *if_then_else = NULL;
808
809         if (cur->at_end == if_then_else_handler)
810                 if_then_else = (struct if_then_else *)cur->at_end_data;
811         if (!if_then_else)
812                 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used without an %%(if) atom"));
813         if (if_then_else->then_atom_seen)
814                 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used more than once"));
815         if (if_then_else->else_atom_seen)
816                 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used after %%(else)"));
817         if_then_else->then_atom_seen = 1;
818         /*
819          * If the 'equals' or 'notequals' attribute is used then
820          * perform the required comparison. If not, only non-empty
821          * strings satisfy the 'if' condition.
822          */
823         if (if_then_else->cmp_status == COMPARE_EQUAL) {
824                 if (!strcmp(if_then_else->str, cur->output.buf))
825                         if_then_else->condition_satisfied = 1;
826         } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) {
827                 if (strcmp(if_then_else->str, cur->output.buf))
828                         if_then_else->condition_satisfied = 1;
829         } else if (cur->output.len && !is_empty(cur->output.buf))
830                 if_then_else->condition_satisfied = 1;
831         strbuf_reset(&cur->output);
832         return 0;
833 }
834
835 static int else_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
836                              struct strbuf *err)
837 {
838         struct ref_formatting_stack *prev = state->stack;
839         struct if_then_else *if_then_else = NULL;
840
841         if (prev->at_end == if_then_else_handler)
842                 if_then_else = (struct if_then_else *)prev->at_end_data;
843         if (!if_then_else)
844                 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without an %%(if) atom"));
845         if (!if_then_else->then_atom_seen)
846                 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without a %%(then) atom"));
847         if (if_then_else->else_atom_seen)
848                 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used more than once"));
849         if_then_else->else_atom_seen = 1;
850         push_stack_element(&state->stack);
851         state->stack->at_end_data = prev->at_end_data;
852         state->stack->at_end = prev->at_end;
853         return 0;
854 }
855
856 static int end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
857                             struct strbuf *err)
858 {
859         struct ref_formatting_stack *current = state->stack;
860         struct strbuf s = STRBUF_INIT;
861
862         if (!current->at_end)
863                 return strbuf_addf_ret(err, -1, _("format: %%(end) atom used without corresponding atom"));
864         current->at_end(&state->stack);
865
866         /*  Stack may have been popped within at_end(), hence reset the current pointer */
867         current = state->stack;
868
869         /*
870          * Perform quote formatting when the stack element is that of
871          * a supporting atom. If nested then perform quote formatting
872          * only on the topmost supporting atom.
873          */
874         if (!current->prev->prev) {
875                 quote_formatting(&s, current->output.buf, state->quote_style);
876                 strbuf_swap(&current->output, &s);
877         }
878         strbuf_release(&s);
879         pop_stack_element(&state->stack);
880         return 0;
881 }
882
883 /*
884  * In a format string, find the next occurrence of %(atom).
885  */
886 static const char *find_next(const char *cp)
887 {
888         while (*cp) {
889                 if (*cp == '%') {
890                         /*
891                          * %( is the start of an atom;
892                          * %% is a quoted per-cent.
893                          */
894                         if (cp[1] == '(')
895                                 return cp;
896                         else if (cp[1] == '%')
897                                 cp++; /* skip over two % */
898                         /* otherwise this is a singleton, literal % */
899                 }
900                 cp++;
901         }
902         return NULL;
903 }
904
905 /*
906  * Make sure the format string is well formed, and parse out
907  * the used atoms.
908  */
909 int verify_ref_format(struct ref_format *format)
910 {
911         const char *cp, *sp;
912
913         format->need_color_reset_at_eol = 0;
914         for (cp = format->format; *cp && (sp = find_next(cp)); ) {
915                 struct strbuf err = STRBUF_INIT;
916                 const char *color, *ep = strchr(sp, ')');
917                 int at;
918
919                 if (!ep)
920                         return error(_("malformed format string %s"), sp);
921                 /* sp points at "%(" and ep points at the closing ")" */
922                 at = parse_ref_filter_atom(format, sp + 2, ep, &err);
923                 if (at < 0)
924                         die("%s", err.buf);
925                 cp = ep + 1;
926
927                 if (skip_prefix(used_atom[at].name, "color:", &color))
928                         format->need_color_reset_at_eol = !!strcmp(color, "reset");
929                 strbuf_release(&err);
930         }
931         if (format->need_color_reset_at_eol && !want_color(format->use_color))
932                 format->need_color_reset_at_eol = 0;
933         return 0;
934 }
935
936 static const char *do_grab_oid(const char *field, const struct object_id *oid,
937                                struct used_atom *atom)
938 {
939         switch (atom->u.oid.option) {
940         case O_FULL:
941                 return oid_to_hex(oid);
942         case O_LENGTH:
943                 return find_unique_abbrev(oid, atom->u.oid.length);
944         case O_SHORT:
945                 return find_unique_abbrev(oid, DEFAULT_ABBREV);
946         default:
947                 BUG("unknown %%(%s) option", field);
948         }
949 }
950
951 static int grab_oid(const char *name, const char *field, const struct object_id *oid,
952                     struct atom_value *v, struct used_atom *atom)
953 {
954         if (starts_with(name, field)) {
955                 v->s = xstrdup(do_grab_oid(field, oid, atom));
956                 return 1;
957         }
958         return 0;
959 }
960
961 /* See grab_values */
962 static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi)
963 {
964         int i;
965
966         for (i = 0; i < used_atom_cnt; i++) {
967                 const char *name = used_atom[i].name;
968                 struct atom_value *v = &val[i];
969                 if (!!deref != (*name == '*'))
970                         continue;
971                 if (deref)
972                         name++;
973                 if (!strcmp(name, "objecttype"))
974                         v->s = xstrdup(type_name(oi->type));
975                 else if (starts_with(name, "objectsize")) {
976                         if (used_atom[i].u.objectsize.option == O_SIZE_DISK) {
977                                 v->value = oi->disk_size;
978                                 v->s = xstrfmt("%"PRIuMAX, (uintmax_t)oi->disk_size);
979                         } else if (used_atom[i].u.objectsize.option == O_SIZE) {
980                                 v->value = oi->size;
981                                 v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size);
982                         }
983                 } else if (!strcmp(name, "deltabase"))
984                         v->s = xstrdup(oid_to_hex(&oi->delta_base_oid));
985                 else if (deref)
986                         grab_oid(name, "objectname", &oi->oid, v, &used_atom[i]);
987         }
988 }
989
990 /* See grab_values */
991 static void grab_tag_values(struct atom_value *val, int deref, struct object *obj)
992 {
993         int i;
994         struct tag *tag = (struct tag *) obj;
995
996         for (i = 0; i < used_atom_cnt; i++) {
997                 const char *name = used_atom[i].name;
998                 struct atom_value *v = &val[i];
999                 if (!!deref != (*name == '*'))
1000                         continue;
1001                 if (deref)
1002                         name++;
1003                 if (!strcmp(name, "tag"))
1004                         v->s = xstrdup(tag->tag);
1005                 else if (!strcmp(name, "type") && tag->tagged)
1006                         v->s = xstrdup(type_name(tag->tagged->type));
1007                 else if (!strcmp(name, "object") && tag->tagged)
1008                         v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
1009         }
1010 }
1011
1012 /* See grab_values */
1013 static void grab_commit_values(struct atom_value *val, int deref, struct object *obj)
1014 {
1015         int i;
1016         struct commit *commit = (struct commit *) obj;
1017
1018         for (i = 0; i < used_atom_cnt; i++) {
1019                 const char *name = used_atom[i].name;
1020                 struct atom_value *v = &val[i];
1021                 if (!!deref != (*name == '*'))
1022                         continue;
1023                 if (deref)
1024                         name++;
1025                 if (grab_oid(name, "tree", get_commit_tree_oid(commit), v, &used_atom[i]))
1026                         continue;
1027                 if (!strcmp(name, "numparent")) {
1028                         v->value = commit_list_count(commit->parents);
1029                         v->s = xstrfmt("%lu", (unsigned long)v->value);
1030                 }
1031                 else if (starts_with(name, "parent")) {
1032                         struct commit_list *parents;
1033                         struct strbuf s = STRBUF_INIT;
1034                         for (parents = commit->parents; parents; parents = parents->next) {
1035                                 struct object_id *oid = &parents->item->object.oid;
1036                                 if (parents != commit->parents)
1037                                         strbuf_addch(&s, ' ');
1038                                 strbuf_addstr(&s, do_grab_oid("parent", oid, &used_atom[i]));
1039                         }
1040                         v->s = strbuf_detach(&s, NULL);
1041                 }
1042         }
1043 }
1044
1045 static const char *find_wholine(const char *who, int wholen, const char *buf)
1046 {
1047         const char *eol;
1048         while (*buf) {
1049                 if (!strncmp(buf, who, wholen) &&
1050                     buf[wholen] == ' ')
1051                         return buf + wholen + 1;
1052                 eol = strchr(buf, '\n');
1053                 if (!eol)
1054                         return "";
1055                 eol++;
1056                 if (*eol == '\n')
1057                         return ""; /* end of header */
1058                 buf = eol;
1059         }
1060         return "";
1061 }
1062
1063 static const char *copy_line(const char *buf)
1064 {
1065         const char *eol = strchrnul(buf, '\n');
1066         return xmemdupz(buf, eol - buf);
1067 }
1068
1069 static const char *copy_name(const char *buf)
1070 {
1071         const char *cp;
1072         for (cp = buf; *cp && *cp != '\n'; cp++) {
1073                 if (!strncmp(cp, " <", 2))
1074                         return xmemdupz(buf, cp - buf);
1075         }
1076         return xstrdup("");
1077 }
1078
1079 static const char *copy_email(const char *buf, struct used_atom *atom)
1080 {
1081         const char *email = strchr(buf, '<');
1082         const char *eoemail;
1083         if (!email)
1084                 return xstrdup("");
1085         switch (atom->u.email_option.option) {
1086         case EO_RAW:
1087                 eoemail = strchr(email, '>');
1088                 if (eoemail)
1089                         eoemail++;
1090                 break;
1091         case EO_TRIM:
1092                 email++;
1093                 eoemail = strchr(email, '>');
1094                 break;
1095         case EO_LOCALPART:
1096                 email++;
1097                 eoemail = strchr(email, '@');
1098                 if (!eoemail)
1099                         eoemail = strchr(email, '>');
1100                 break;
1101         default:
1102                 BUG("unknown email option");
1103         }
1104
1105         if (!eoemail)
1106                 return xstrdup("");
1107         return xmemdupz(email, eoemail - email);
1108 }
1109
1110 static char *copy_subject(const char *buf, unsigned long len)
1111 {
1112         struct strbuf sb = STRBUF_INIT;
1113         int i;
1114
1115         for (i = 0; i < len; i++) {
1116                 if (buf[i] == '\r' && i + 1 < len && buf[i + 1] == '\n')
1117                         continue; /* ignore CR in CRLF */
1118
1119                 if (buf[i] == '\n')
1120                         strbuf_addch(&sb, ' ');
1121                 else
1122                         strbuf_addch(&sb, buf[i]);
1123         }
1124         return strbuf_detach(&sb, NULL);
1125 }
1126
1127 static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
1128 {
1129         const char *eoemail = strstr(buf, "> ");
1130         char *zone;
1131         timestamp_t timestamp;
1132         long tz;
1133         struct date_mode date_mode = { DATE_NORMAL };
1134         const char *formatp;
1135
1136         /*
1137          * We got here because atomname ends in "date" or "date<something>";
1138          * it's not possible that <something> is not ":<format>" because
1139          * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1140          * ":" means no format is specified, and use the default.
1141          */
1142         formatp = strchr(atomname, ':');
1143         if (formatp != NULL) {
1144                 formatp++;
1145                 parse_date_format(formatp, &date_mode);
1146         }
1147
1148         if (!eoemail)
1149                 goto bad;
1150         timestamp = parse_timestamp(eoemail + 2, &zone, 10);
1151         if (timestamp == TIME_MAX)
1152                 goto bad;
1153         tz = strtol(zone, NULL, 10);
1154         if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
1155                 goto bad;
1156         v->s = xstrdup(show_date(timestamp, tz, &date_mode));
1157         v->value = timestamp;
1158         return;
1159  bad:
1160         v->s = xstrdup("");
1161         v->value = 0;
1162 }
1163
1164 /* See grab_values */
1165 static void grab_person(const char *who, struct atom_value *val, int deref, void *buf)
1166 {
1167         int i;
1168         int wholen = strlen(who);
1169         const char *wholine = NULL;
1170
1171         for (i = 0; i < used_atom_cnt; i++) {
1172                 const char *name = used_atom[i].name;
1173                 struct atom_value *v = &val[i];
1174                 if (!!deref != (*name == '*'))
1175                         continue;
1176                 if (deref)
1177                         name++;
1178                 if (strncmp(who, name, wholen))
1179                         continue;
1180                 if (name[wholen] != 0 &&
1181                     strcmp(name + wholen, "name") &&
1182                     !starts_with(name + wholen, "email") &&
1183                     !starts_with(name + wholen, "date"))
1184                         continue;
1185                 if (!wholine)
1186                         wholine = find_wholine(who, wholen, buf);
1187                 if (!wholine)
1188                         return; /* no point looking for it */
1189                 if (name[wholen] == 0)
1190                         v->s = copy_line(wholine);
1191                 else if (!strcmp(name + wholen, "name"))
1192                         v->s = copy_name(wholine);
1193                 else if (starts_with(name + wholen, "email"))
1194                         v->s = copy_email(wholine, &used_atom[i]);
1195                 else if (starts_with(name + wholen, "date"))
1196                         grab_date(wholine, v, name);
1197         }
1198
1199         /*
1200          * For a tag or a commit object, if "creator" or "creatordate" is
1201          * requested, do something special.
1202          */
1203         if (strcmp(who, "tagger") && strcmp(who, "committer"))
1204                 return; /* "author" for commit object is not wanted */
1205         if (!wholine)
1206                 wholine = find_wholine(who, wholen, buf);
1207         if (!wholine)
1208                 return;
1209         for (i = 0; i < used_atom_cnt; i++) {
1210                 const char *name = used_atom[i].name;
1211                 struct atom_value *v = &val[i];
1212                 if (!!deref != (*name == '*'))
1213                         continue;
1214                 if (deref)
1215                         name++;
1216
1217                 if (starts_with(name, "creatordate"))
1218                         grab_date(wholine, v, name);
1219                 else if (!strcmp(name, "creator"))
1220                         v->s = copy_line(wholine);
1221         }
1222 }
1223
1224 static void find_subpos(const char *buf,
1225                         const char **sub, size_t *sublen,
1226                         const char **body, size_t *bodylen,
1227                         size_t *nonsiglen,
1228                         const char **sig, size_t *siglen)
1229 {
1230         struct strbuf payload = STRBUF_INIT;
1231         struct strbuf signature = STRBUF_INIT;
1232         const char *eol;
1233         const char *end = buf + strlen(buf);
1234         const char *sigstart;
1235
1236         /* parse signature first; we might not even have a subject line */
1237         parse_signature(buf, end - buf, &payload, &signature);
1238
1239         /* skip past header until we hit empty line */
1240         while (*buf && *buf != '\n') {
1241                 eol = strchrnul(buf, '\n');
1242                 if (*eol)
1243                         eol++;
1244                 buf = eol;
1245         }
1246         /* skip any empty lines */
1247         while (*buf == '\n')
1248                 buf++;
1249         *sig = strbuf_detach(&signature, siglen);
1250         sigstart = buf + parse_signed_buffer(buf, strlen(buf));
1251
1252         /* subject is first non-empty line */
1253         *sub = buf;
1254         /* subject goes to first empty line before signature begins */
1255         if ((eol = strstr(*sub, "\n\n"))) {
1256                 eol = eol < sigstart ? eol : sigstart;
1257         /* check if message uses CRLF */
1258         } else if (! (eol = strstr(*sub, "\r\n\r\n"))) {
1259                 /* treat whole message as subject */
1260                 eol = strrchr(*sub, '\0');
1261         }
1262         buf = eol;
1263         *sublen = buf - *sub;
1264         /* drop trailing newline, if present */
1265         while (*sublen && ((*sub)[*sublen - 1] == '\n' ||
1266                            (*sub)[*sublen - 1] == '\r'))
1267                 *sublen -= 1;
1268
1269         /* skip any empty lines */
1270         while (*buf == '\n' || *buf == '\r')
1271                 buf++;
1272         *body = buf;
1273         *bodylen = strlen(buf);
1274         *nonsiglen = sigstart - buf;
1275 }
1276
1277 /*
1278  * If 'lines' is greater than 0, append that many lines from the given
1279  * 'buf' of length 'size' to the given strbuf.
1280  */
1281 static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
1282 {
1283         int i;
1284         const char *sp, *eol;
1285         size_t len;
1286
1287         sp = buf;
1288
1289         for (i = 0; i < lines && sp < buf + size; i++) {
1290                 if (i)
1291                         strbuf_addstr(out, "\n    ");
1292                 eol = memchr(sp, '\n', size - (sp - buf));
1293                 len = eol ? eol - sp : size - (sp - buf);
1294                 strbuf_add(out, sp, len);
1295                 if (!eol)
1296                         break;
1297                 sp = eol + 1;
1298         }
1299 }
1300
1301 /* See grab_values */
1302 static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
1303 {
1304         int i;
1305         const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
1306         size_t sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
1307
1308         for (i = 0; i < used_atom_cnt; i++) {
1309                 struct used_atom *atom = &used_atom[i];
1310                 const char *name = atom->name;
1311                 struct atom_value *v = &val[i];
1312
1313                 if (!!deref != (*name == '*'))
1314                         continue;
1315                 if (deref)
1316                         name++;
1317                 if (strcmp(name, "body") &&
1318                     !starts_with(name, "subject") &&
1319                     !starts_with(name, "trailers") &&
1320                     !starts_with(name, "contents"))
1321                         continue;
1322                 if (!subpos)
1323                         find_subpos(buf,
1324                                     &subpos, &sublen,
1325                                     &bodypos, &bodylen, &nonsiglen,
1326                                     &sigpos, &siglen);
1327
1328                 if (atom->u.contents.option == C_SUB)
1329                         v->s = copy_subject(subpos, sublen);
1330                 else if (atom->u.contents.option == C_SUB_SANITIZE) {
1331                         struct strbuf sb = STRBUF_INIT;
1332                         format_sanitized_subject(&sb, subpos, sublen);
1333                         v->s = strbuf_detach(&sb, NULL);
1334                 } else if (atom->u.contents.option == C_BODY_DEP)
1335                         v->s = xmemdupz(bodypos, bodylen);
1336                 else if (atom->u.contents.option == C_LENGTH)
1337                         v->s = xstrfmt("%"PRIuMAX, (uintmax_t)strlen(subpos));
1338                 else if (atom->u.contents.option == C_BODY)
1339                         v->s = xmemdupz(bodypos, nonsiglen);
1340                 else if (atom->u.contents.option == C_SIG)
1341                         v->s = xmemdupz(sigpos, siglen);
1342                 else if (atom->u.contents.option == C_LINES) {
1343                         struct strbuf s = STRBUF_INIT;
1344                         const char *contents_end = bodypos + nonsiglen;
1345
1346                         /*  Size is the length of the message after removing the signature */
1347                         append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
1348                         v->s = strbuf_detach(&s, NULL);
1349                 } else if (atom->u.contents.option == C_TRAILERS) {
1350                         struct strbuf s = STRBUF_INIT;
1351
1352                         /* Format the trailer info according to the trailer_opts given */
1353                         format_trailers_from_commit(&s, subpos, &atom->u.contents.trailer_opts);
1354
1355                         v->s = strbuf_detach(&s, NULL);
1356                 } else if (atom->u.contents.option == C_BARE)
1357                         v->s = xstrdup(subpos);
1358
1359         }
1360         free((void *)sigpos);
1361 }
1362
1363 /*
1364  * We want to have empty print-string for field requests
1365  * that do not apply (e.g. "authordate" for a tag object)
1366  */
1367 static void fill_missing_values(struct atom_value *val)
1368 {
1369         int i;
1370         for (i = 0; i < used_atom_cnt; i++) {
1371                 struct atom_value *v = &val[i];
1372                 if (v->s == NULL)
1373                         v->s = xstrdup("");
1374         }
1375 }
1376
1377 /*
1378  * val is a list of atom_value to hold returned values.  Extract
1379  * the values for atoms in used_atom array out of (obj, buf, sz).
1380  * when deref is false, (obj, buf, sz) is the object that is
1381  * pointed at by the ref itself; otherwise it is the object the
1382  * ref (which is a tag) refers to.
1383  */
1384 static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf)
1385 {
1386         switch (obj->type) {
1387         case OBJ_TAG:
1388                 grab_tag_values(val, deref, obj);
1389                 grab_sub_body_contents(val, deref, buf);
1390                 grab_person("tagger", val, deref, buf);
1391                 break;
1392         case OBJ_COMMIT:
1393                 grab_commit_values(val, deref, obj);
1394                 grab_sub_body_contents(val, deref, buf);
1395                 grab_person("author", val, deref, buf);
1396                 grab_person("committer", val, deref, buf);
1397                 break;
1398         case OBJ_TREE:
1399                 /* grab_tree_values(val, deref, obj, buf, sz); */
1400                 break;
1401         case OBJ_BLOB:
1402                 /* grab_blob_values(val, deref, obj, buf, sz); */
1403                 break;
1404         default:
1405                 die("Eh?  Object of type %d?", obj->type);
1406         }
1407 }
1408
1409 static inline char *copy_advance(char *dst, const char *src)
1410 {
1411         while (*src)
1412                 *dst++ = *src++;
1413         return dst;
1414 }
1415
1416 static const char *lstrip_ref_components(const char *refname, int len)
1417 {
1418         long remaining = len;
1419         const char *start = xstrdup(refname);
1420         const char *to_free = start;
1421
1422         if (len < 0) {
1423                 int i;
1424                 const char *p = refname;
1425
1426                 /* Find total no of '/' separated path-components */
1427                 for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
1428                         ;
1429                 /*
1430                  * The number of components we need to strip is now
1431                  * the total minus the components to be left (Plus one
1432                  * because we count the number of '/', but the number
1433                  * of components is one more than the no of '/').
1434                  */
1435                 remaining = i + len + 1;
1436         }
1437
1438         while (remaining > 0) {
1439                 switch (*start++) {
1440                 case '\0':
1441                         free((char *)to_free);
1442                         return xstrdup("");
1443                 case '/':
1444                         remaining--;
1445                         break;
1446                 }
1447         }
1448
1449         start = xstrdup(start);
1450         free((char *)to_free);
1451         return start;
1452 }
1453
1454 static const char *rstrip_ref_components(const char *refname, int len)
1455 {
1456         long remaining = len;
1457         const char *start = xstrdup(refname);
1458         const char *to_free = start;
1459
1460         if (len < 0) {
1461                 int i;
1462                 const char *p = refname;
1463
1464                 /* Find total no of '/' separated path-components */
1465                 for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
1466                         ;
1467                 /*
1468                  * The number of components we need to strip is now
1469                  * the total minus the components to be left (Plus one
1470                  * because we count the number of '/', but the number
1471                  * of components is one more than the no of '/').
1472                  */
1473                 remaining = i + len + 1;
1474         }
1475
1476         while (remaining-- > 0) {
1477                 char *p = strrchr(start, '/');
1478                 if (p == NULL) {
1479                         free((char *)to_free);
1480                         return xstrdup("");
1481                 } else
1482                         p[0] = '\0';
1483         }
1484         return start;
1485 }
1486
1487 static const char *show_ref(struct refname_atom *atom, const char *refname)
1488 {
1489         if (atom->option == R_SHORT)
1490                 return shorten_unambiguous_ref(refname, warn_ambiguous_refs);
1491         else if (atom->option == R_LSTRIP)
1492                 return lstrip_ref_components(refname, atom->lstrip);
1493         else if (atom->option == R_RSTRIP)
1494                 return rstrip_ref_components(refname, atom->rstrip);
1495         else
1496                 return xstrdup(refname);
1497 }
1498
1499 static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
1500                                     struct branch *branch, const char **s)
1501 {
1502         int num_ours, num_theirs;
1503         if (atom->u.remote_ref.option == RR_REF)
1504                 *s = show_ref(&atom->u.remote_ref.refname, refname);
1505         else if (atom->u.remote_ref.option == RR_TRACK) {
1506                 if (stat_tracking_info(branch, &num_ours, &num_theirs,
1507                                        NULL, atom->u.remote_ref.push,
1508                                        AHEAD_BEHIND_FULL) < 0) {
1509                         *s = xstrdup(msgs.gone);
1510                 } else if (!num_ours && !num_theirs)
1511                         *s = xstrdup("");
1512                 else if (!num_ours)
1513                         *s = xstrfmt(msgs.behind, num_theirs);
1514                 else if (!num_theirs)
1515                         *s = xstrfmt(msgs.ahead, num_ours);
1516                 else
1517                         *s = xstrfmt(msgs.ahead_behind,
1518                                      num_ours, num_theirs);
1519                 if (!atom->u.remote_ref.nobracket && *s[0]) {
1520                         const char *to_free = *s;
1521                         *s = xstrfmt("[%s]", *s);
1522                         free((void *)to_free);
1523                 }
1524         } else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
1525                 if (stat_tracking_info(branch, &num_ours, &num_theirs,
1526                                        NULL, atom->u.remote_ref.push,
1527                                        AHEAD_BEHIND_FULL) < 0) {
1528                         *s = xstrdup("");
1529                         return;
1530                 }
1531                 if (!num_ours && !num_theirs)
1532                         *s = xstrdup("=");
1533                 else if (!num_ours)
1534                         *s = xstrdup("<");
1535                 else if (!num_theirs)
1536                         *s = xstrdup(">");
1537                 else
1538                         *s = xstrdup("<>");
1539         } else if (atom->u.remote_ref.option == RR_REMOTE_NAME) {
1540                 int explicit;
1541                 const char *remote = atom->u.remote_ref.push ?
1542                         pushremote_for_branch(branch, &explicit) :
1543                         remote_for_branch(branch, &explicit);
1544                 *s = xstrdup(explicit ? remote : "");
1545         } else if (atom->u.remote_ref.option == RR_REMOTE_REF) {
1546                 const char *merge;
1547
1548                 merge = remote_ref_for_branch(branch, atom->u.remote_ref.push);
1549                 *s = xstrdup(merge ? merge : "");
1550         } else
1551                 BUG("unhandled RR_* enum");
1552 }
1553
1554 char *get_head_description(void)
1555 {
1556         struct strbuf desc = STRBUF_INIT;
1557         struct wt_status_state state;
1558         memset(&state, 0, sizeof(state));
1559         wt_status_get_state(the_repository, &state, 1);
1560         if (state.rebase_in_progress ||
1561             state.rebase_interactive_in_progress) {
1562                 if (state.branch)
1563                         strbuf_addf(&desc, _("(no branch, rebasing %s)"),
1564                                     state.branch);
1565                 else
1566                         strbuf_addf(&desc, _("(no branch, rebasing detached HEAD %s)"),
1567                                     state.detached_from);
1568         } else if (state.bisect_in_progress)
1569                 strbuf_addf(&desc, _("(no branch, bisect started on %s)"),
1570                             state.branch);
1571         else if (state.detached_from) {
1572                 if (state.detached_at)
1573                         strbuf_addf(&desc, _("(HEAD detached at %s)"),
1574                                 state.detached_from);
1575                 else
1576                         strbuf_addf(&desc, _("(HEAD detached from %s)"),
1577                                 state.detached_from);
1578         } else
1579                 strbuf_addstr(&desc, _("(no branch)"));
1580
1581         return strbuf_detach(&desc, NULL);
1582 }
1583
1584 static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref)
1585 {
1586         if (!ref->symref)
1587                 return xstrdup("");
1588         else
1589                 return show_ref(&atom->u.refname, ref->symref);
1590 }
1591
1592 static const char *get_refname(struct used_atom *atom, struct ref_array_item *ref)
1593 {
1594         if (ref->kind & FILTER_REFS_DETACHED_HEAD)
1595                 return get_head_description();
1596         return show_ref(&atom->u.refname, ref->refname);
1597 }
1598
1599 static int get_object(struct ref_array_item *ref, int deref, struct object **obj,
1600                       struct expand_data *oi, struct strbuf *err)
1601 {
1602         /* parse_object_buffer() will set eaten to 0 if free() will be needed */
1603         int eaten = 1;
1604         if (oi->info.contentp) {
1605                 /* We need to know that to use parse_object_buffer properly */
1606                 oi->info.sizep = &oi->size;
1607                 oi->info.typep = &oi->type;
1608         }
1609         if (oid_object_info_extended(the_repository, &oi->oid, &oi->info,
1610                                      OBJECT_INFO_LOOKUP_REPLACE))
1611                 return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
1612                                        oid_to_hex(&oi->oid), ref->refname);
1613         if (oi->info.disk_sizep && oi->disk_size < 0)
1614                 BUG("Object size is less than zero.");
1615
1616         if (oi->info.contentp) {
1617                 *obj = parse_object_buffer(the_repository, &oi->oid, oi->type, oi->size, oi->content, &eaten);
1618                 if (!*obj) {
1619                         if (!eaten)
1620                                 free(oi->content);
1621                         return strbuf_addf_ret(err, -1, _("parse_object_buffer failed on %s for %s"),
1622                                                oid_to_hex(&oi->oid), ref->refname);
1623                 }
1624                 grab_values(ref->value, deref, *obj, oi->content);
1625         }
1626
1627         grab_common_values(ref->value, deref, oi);
1628         if (!eaten)
1629                 free(oi->content);
1630         return 0;
1631 }
1632
1633 static void populate_worktree_map(struct hashmap *map, struct worktree **worktrees)
1634 {
1635         int i;
1636
1637         for (i = 0; worktrees[i]; i++) {
1638                 if (worktrees[i]->head_ref) {
1639                         struct ref_to_worktree_entry *entry;
1640                         entry = xmalloc(sizeof(*entry));
1641                         entry->wt = worktrees[i];
1642                         hashmap_entry_init(&entry->ent,
1643                                         strhash(worktrees[i]->head_ref));
1644
1645                         hashmap_add(map, &entry->ent);
1646                 }
1647         }
1648 }
1649
1650 static void lazy_init_worktree_map(void)
1651 {
1652         if (ref_to_worktree_map.worktrees)
1653                 return;
1654
1655         ref_to_worktree_map.worktrees = get_worktrees();
1656         hashmap_init(&(ref_to_worktree_map.map), ref_to_worktree_map_cmpfnc, NULL, 0);
1657         populate_worktree_map(&(ref_to_worktree_map.map), ref_to_worktree_map.worktrees);
1658 }
1659
1660 static char *get_worktree_path(const struct used_atom *atom, const struct ref_array_item *ref)
1661 {
1662         struct hashmap_entry entry, *e;
1663         struct ref_to_worktree_entry *lookup_result;
1664
1665         lazy_init_worktree_map();
1666
1667         hashmap_entry_init(&entry, strhash(ref->refname));
1668         e = hashmap_get(&(ref_to_worktree_map.map), &entry, ref->refname);
1669
1670         if (!e)
1671                 return xstrdup("");
1672
1673         lookup_result = container_of(e, struct ref_to_worktree_entry, ent);
1674
1675         return xstrdup(lookup_result->wt->path);
1676 }
1677
1678 /*
1679  * Parse the object referred by ref, and grab needed value.
1680  */
1681 static int populate_value(struct ref_array_item *ref, struct strbuf *err)
1682 {
1683         struct object *obj;
1684         int i;
1685         struct object_info empty = OBJECT_INFO_INIT;
1686
1687         CALLOC_ARRAY(ref->value, used_atom_cnt);
1688
1689         if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
1690                 ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
1691                                              NULL, NULL);
1692                 if (!ref->symref)
1693                         ref->symref = xstrdup("");
1694         }
1695
1696         /* Fill in specials first */
1697         for (i = 0; i < used_atom_cnt; i++) {
1698                 struct used_atom *atom = &used_atom[i];
1699                 const char *name = used_atom[i].name;
1700                 struct atom_value *v = &ref->value[i];
1701                 int deref = 0;
1702                 const char *refname;
1703                 struct branch *branch = NULL;
1704
1705                 v->handler = append_atom;
1706                 v->atom = atom;
1707
1708                 if (*name == '*') {
1709                         deref = 1;
1710                         name++;
1711                 }
1712
1713                 if (starts_with(name, "refname"))
1714                         refname = get_refname(atom, ref);
1715                 else if (!strcmp(name, "worktreepath")) {
1716                         if (ref->kind == FILTER_REFS_BRANCHES)
1717                                 v->s = get_worktree_path(atom, ref);
1718                         else
1719                                 v->s = xstrdup("");
1720                         continue;
1721                 }
1722                 else if (starts_with(name, "symref"))
1723                         refname = get_symref(atom, ref);
1724                 else if (starts_with(name, "upstream")) {
1725                         const char *branch_name;
1726                         /* only local branches may have an upstream */
1727                         if (!skip_prefix(ref->refname, "refs/heads/",
1728                                          &branch_name)) {
1729                                 v->s = xstrdup("");
1730                                 continue;
1731                         }
1732                         branch = branch_get(branch_name);
1733
1734                         refname = branch_get_upstream(branch, NULL);
1735                         if (refname)
1736                                 fill_remote_ref_details(atom, refname, branch, &v->s);
1737                         else
1738                                 v->s = xstrdup("");
1739                         continue;
1740                 } else if (atom->u.remote_ref.push) {
1741                         const char *branch_name;
1742                         v->s = xstrdup("");
1743                         if (!skip_prefix(ref->refname, "refs/heads/",
1744                                          &branch_name))
1745                                 continue;
1746                         branch = branch_get(branch_name);
1747
1748                         if (atom->u.remote_ref.push_remote)
1749                                 refname = NULL;
1750                         else {
1751                                 refname = branch_get_push(branch, NULL);
1752                                 if (!refname)
1753                                         continue;
1754                         }
1755                         /* We will definitely re-init v->s on the next line. */
1756                         free((char *)v->s);
1757                         fill_remote_ref_details(atom, refname, branch, &v->s);
1758                         continue;
1759                 } else if (starts_with(name, "color:")) {
1760                         v->s = xstrdup(atom->u.color);
1761                         continue;
1762                 } else if (!strcmp(name, "flag")) {
1763                         char buf[256], *cp = buf;
1764                         if (ref->flag & REF_ISSYMREF)
1765                                 cp = copy_advance(cp, ",symref");
1766                         if (ref->flag & REF_ISPACKED)
1767                                 cp = copy_advance(cp, ",packed");
1768                         if (cp == buf)
1769                                 v->s = xstrdup("");
1770                         else {
1771                                 *cp = '\0';
1772                                 v->s = xstrdup(buf + 1);
1773                         }
1774                         continue;
1775                 } else if (!deref && grab_oid(name, "objectname", &ref->objectname, v, atom)) {
1776                         continue;
1777                 } else if (!strcmp(name, "HEAD")) {
1778                         if (atom->u.head && !strcmp(ref->refname, atom->u.head))
1779                                 v->s = xstrdup("*");
1780                         else
1781                                 v->s = xstrdup(" ");
1782                         continue;
1783                 } else if (starts_with(name, "align")) {
1784                         v->handler = align_atom_handler;
1785                         v->s = xstrdup("");
1786                         continue;
1787                 } else if (!strcmp(name, "end")) {
1788                         v->handler = end_atom_handler;
1789                         v->s = xstrdup("");
1790                         continue;
1791                 } else if (starts_with(name, "if")) {
1792                         const char *s;
1793                         if (skip_prefix(name, "if:", &s))
1794                                 v->s = xstrdup(s);
1795                         else
1796                                 v->s = xstrdup("");
1797                         v->handler = if_atom_handler;
1798                         continue;
1799                 } else if (!strcmp(name, "then")) {
1800                         v->handler = then_atom_handler;
1801                         v->s = xstrdup("");
1802                         continue;
1803                 } else if (!strcmp(name, "else")) {
1804                         v->handler = else_atom_handler;
1805                         v->s = xstrdup("");
1806                         continue;
1807                 } else
1808                         continue;
1809
1810                 if (!deref)
1811                         v->s = xstrdup(refname);
1812                 else
1813                         v->s = xstrfmt("%s^{}", refname);
1814                 free((char *)refname);
1815         }
1816
1817         for (i = 0; i < used_atom_cnt; i++) {
1818                 struct atom_value *v = &ref->value[i];
1819                 if (v->s == NULL && used_atom[i].source == SOURCE_NONE)
1820                         return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
1821                                                oid_to_hex(&ref->objectname), ref->refname);
1822         }
1823
1824         if (need_tagged)
1825                 oi.info.contentp = &oi.content;
1826         if (!memcmp(&oi.info, &empty, sizeof(empty)) &&
1827             !memcmp(&oi_deref.info, &empty, sizeof(empty)))
1828                 return 0;
1829
1830
1831         oi.oid = ref->objectname;
1832         if (get_object(ref, 0, &obj, &oi, err))
1833                 return -1;
1834
1835         /*
1836          * If there is no atom that wants to know about tagged
1837          * object, we are done.
1838          */
1839         if (!need_tagged || (obj->type != OBJ_TAG))
1840                 return 0;
1841
1842         /*
1843          * If it is a tag object, see if we use a value that derefs
1844          * the object, and if we do grab the object it refers to.
1845          */
1846         oi_deref.oid = *get_tagged_oid((struct tag *)obj);
1847
1848         /*
1849          * NEEDSWORK: This derefs tag only once, which
1850          * is good to deal with chains of trust, but
1851          * is not consistent with what deref_tag() does
1852          * which peels the onion to the core.
1853          */
1854         return get_object(ref, 1, &obj, &oi_deref, err);
1855 }
1856
1857 /*
1858  * Given a ref, return the value for the atom.  This lazily gets value
1859  * out of the object by calling populate value.
1860  */
1861 static int get_ref_atom_value(struct ref_array_item *ref, int atom,
1862                               struct atom_value **v, struct strbuf *err)
1863 {
1864         if (!ref->value) {
1865                 if (populate_value(ref, err))
1866                         return -1;
1867                 fill_missing_values(ref->value);
1868         }
1869         *v = &ref->value[atom];
1870         return 0;
1871 }
1872
1873 /*
1874  * Return 1 if the refname matches one of the patterns, otherwise 0.
1875  * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1876  * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1877  * matches "refs/heads/mas*", too).
1878  */
1879 static int match_pattern(const struct ref_filter *filter, const char *refname)
1880 {
1881         const char **patterns = filter->name_patterns;
1882         unsigned flags = 0;
1883
1884         if (filter->ignore_case)
1885                 flags |= WM_CASEFOLD;
1886
1887         /*
1888          * When no '--format' option is given we need to skip the prefix
1889          * for matching refs of tags and branches.
1890          */
1891         (void)(skip_prefix(refname, "refs/tags/", &refname) ||
1892                skip_prefix(refname, "refs/heads/", &refname) ||
1893                skip_prefix(refname, "refs/remotes/", &refname) ||
1894                skip_prefix(refname, "refs/", &refname));
1895
1896         for (; *patterns; patterns++) {
1897                 if (!wildmatch(*patterns, refname, flags))
1898                         return 1;
1899         }
1900         return 0;
1901 }
1902
1903 /*
1904  * Return 1 if the refname matches one of the patterns, otherwise 0.
1905  * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1906  * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1907  * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1908  */
1909 static int match_name_as_path(const struct ref_filter *filter, const char *refname)
1910 {
1911         const char **pattern = filter->name_patterns;
1912         int namelen = strlen(refname);
1913         unsigned flags = WM_PATHNAME;
1914
1915         if (filter->ignore_case)
1916                 flags |= WM_CASEFOLD;
1917
1918         for (; *pattern; pattern++) {
1919                 const char *p = *pattern;
1920                 int plen = strlen(p);
1921
1922                 if ((plen <= namelen) &&
1923                     !strncmp(refname, p, plen) &&
1924                     (refname[plen] == '\0' ||
1925                      refname[plen] == '/' ||
1926                      p[plen-1] == '/'))
1927                         return 1;
1928                 if (!wildmatch(p, refname, flags))
1929                         return 1;
1930         }
1931         return 0;
1932 }
1933
1934 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1935 static int filter_pattern_match(struct ref_filter *filter, const char *refname)
1936 {
1937         if (!*filter->name_patterns)
1938                 return 1; /* No pattern always matches */
1939         if (filter->match_as_path)
1940                 return match_name_as_path(filter, refname);
1941         return match_pattern(filter, refname);
1942 }
1943
1944 /*
1945  * This is the same as for_each_fullref_in(), but it tries to iterate
1946  * only over the patterns we'll care about. Note that it _doesn't_ do a full
1947  * pattern match, so the callback still has to match each ref individually.
1948  */
1949 static int for_each_fullref_in_pattern(struct ref_filter *filter,
1950                                        each_ref_fn cb,
1951                                        void *cb_data,
1952                                        int broken)
1953 {
1954         if (!filter->match_as_path) {
1955                 /*
1956                  * in this case, the patterns are applied after
1957                  * prefixes like "refs/heads/" etc. are stripped off,
1958                  * so we have to look at everything:
1959                  */
1960                 return for_each_fullref_in("", cb, cb_data, broken);
1961         }
1962
1963         if (filter->ignore_case) {
1964                 /*
1965                  * we can't handle case-insensitive comparisons,
1966                  * so just return everything and let the caller
1967                  * sort it out.
1968                  */
1969                 return for_each_fullref_in("", cb, cb_data, broken);
1970         }
1971
1972         if (!filter->name_patterns[0]) {
1973                 /* no patterns; we have to look at everything */
1974                 return for_each_fullref_in("", cb, cb_data, broken);
1975         }
1976
1977         return for_each_fullref_in_prefixes(NULL, filter->name_patterns,
1978                                             cb, cb_data, broken);
1979 }
1980
1981 /*
1982  * Given a ref (oid, refname), check if the ref belongs to the array
1983  * of oids. If the given ref is a tag, check if the given tag points
1984  * at one of the oids in the given oid array.
1985  * NEEDSWORK:
1986  * 1. Only a single level of indirection is obtained, we might want to
1987  * change this to account for multiple levels (e.g. annotated tags
1988  * pointing to annotated tags pointing to a commit.)
1989  * 2. As the refs are cached we might know what refname peels to without
1990  * the need to parse the object via parse_object(). peel_ref() might be a
1991  * more efficient alternative to obtain the pointee.
1992  */
1993 static const struct object_id *match_points_at(struct oid_array *points_at,
1994                                                const struct object_id *oid,
1995                                                const char *refname)
1996 {
1997         const struct object_id *tagged_oid = NULL;
1998         struct object *obj;
1999
2000         if (oid_array_lookup(points_at, oid) >= 0)
2001                 return oid;
2002         obj = parse_object(the_repository, oid);
2003         if (!obj)
2004                 die(_("malformed object at '%s'"), refname);
2005         if (obj->type == OBJ_TAG)
2006                 tagged_oid = get_tagged_oid((struct tag *)obj);
2007         if (tagged_oid && oid_array_lookup(points_at, tagged_oid) >= 0)
2008                 return tagged_oid;
2009         return NULL;
2010 }
2011
2012 /*
2013  * Allocate space for a new ref_array_item and copy the name and oid to it.
2014  *
2015  * Callers can then fill in other struct members at their leisure.
2016  */
2017 static struct ref_array_item *new_ref_array_item(const char *refname,
2018                                                  const struct object_id *oid)
2019 {
2020         struct ref_array_item *ref;
2021
2022         FLEX_ALLOC_STR(ref, refname, refname);
2023         oidcpy(&ref->objectname, oid);
2024
2025         return ref;
2026 }
2027
2028 struct ref_array_item *ref_array_push(struct ref_array *array,
2029                                       const char *refname,
2030                                       const struct object_id *oid)
2031 {
2032         struct ref_array_item *ref = new_ref_array_item(refname, oid);
2033
2034         ALLOC_GROW(array->items, array->nr + 1, array->alloc);
2035         array->items[array->nr++] = ref;
2036
2037         return ref;
2038 }
2039
2040 static int ref_kind_from_refname(const char *refname)
2041 {
2042         unsigned int i;
2043
2044         static struct {
2045                 const char *prefix;
2046                 unsigned int kind;
2047         } ref_kind[] = {
2048                 { "refs/heads/" , FILTER_REFS_BRANCHES },
2049                 { "refs/remotes/" , FILTER_REFS_REMOTES },
2050                 { "refs/tags/", FILTER_REFS_TAGS}
2051         };
2052
2053         if (!strcmp(refname, "HEAD"))
2054                 return FILTER_REFS_DETACHED_HEAD;
2055
2056         for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
2057                 if (starts_with(refname, ref_kind[i].prefix))
2058                         return ref_kind[i].kind;
2059         }
2060
2061         return FILTER_REFS_OTHERS;
2062 }
2063
2064 static int filter_ref_kind(struct ref_filter *filter, const char *refname)
2065 {
2066         if (filter->kind == FILTER_REFS_BRANCHES ||
2067             filter->kind == FILTER_REFS_REMOTES ||
2068             filter->kind == FILTER_REFS_TAGS)
2069                 return filter->kind;
2070         return ref_kind_from_refname(refname);
2071 }
2072
2073 struct ref_filter_cbdata {
2074         struct ref_array *array;
2075         struct ref_filter *filter;
2076         struct contains_cache contains_cache;
2077         struct contains_cache no_contains_cache;
2078 };
2079
2080 /*
2081  * A call-back given to for_each_ref().  Filter refs and keep them for
2082  * later object processing.
2083  */
2084 static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
2085 {
2086         struct ref_filter_cbdata *ref_cbdata = cb_data;
2087         struct ref_filter *filter = ref_cbdata->filter;
2088         struct ref_array_item *ref;
2089         struct commit *commit = NULL;
2090         unsigned int kind;
2091
2092         if (flag & REF_BAD_NAME) {
2093                 warning(_("ignoring ref with broken name %s"), refname);
2094                 return 0;
2095         }
2096
2097         if (flag & REF_ISBROKEN) {
2098                 warning(_("ignoring broken ref %s"), refname);
2099                 return 0;
2100         }
2101
2102         /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2103         kind = filter_ref_kind(filter, refname);
2104         if (!(kind & filter->kind))
2105                 return 0;
2106
2107         if (!filter_pattern_match(filter, refname))
2108                 return 0;
2109
2110         if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname))
2111                 return 0;
2112
2113         /*
2114          * A merge filter is applied on refs pointing to commits. Hence
2115          * obtain the commit using the 'oid' available and discard all
2116          * non-commits early. The actual filtering is done later.
2117          */
2118         if (filter->reachable_from || filter->unreachable_from ||
2119             filter->with_commit || filter->no_commit || filter->verbose) {
2120                 commit = lookup_commit_reference_gently(the_repository, oid, 1);
2121                 if (!commit)
2122                         return 0;
2123                 /* We perform the filtering for the '--contains' option... */
2124                 if (filter->with_commit &&
2125                     !commit_contains(filter, commit, filter->with_commit, &ref_cbdata->contains_cache))
2126                         return 0;
2127                 /* ...or for the `--no-contains' option */
2128                 if (filter->no_commit &&
2129                     commit_contains(filter, commit, filter->no_commit, &ref_cbdata->no_contains_cache))
2130                         return 0;
2131         }
2132
2133         /*
2134          * We do not open the object yet; sort may only need refname
2135          * to do its job and the resulting list may yet to be pruned
2136          * by maxcount logic.
2137          */
2138         ref = ref_array_push(ref_cbdata->array, refname, oid);
2139         ref->commit = commit;
2140         ref->flag = flag;
2141         ref->kind = kind;
2142
2143         return 0;
2144 }
2145
2146 /*  Free memory allocated for a ref_array_item */
2147 static void free_array_item(struct ref_array_item *item)
2148 {
2149         free((char *)item->symref);
2150         if (item->value) {
2151                 int i;
2152                 for (i = 0; i < used_atom_cnt; i++)
2153                         free((char *)item->value[i].s);
2154                 free(item->value);
2155         }
2156         free(item);
2157 }
2158
2159 /* Free all memory allocated for ref_array */
2160 void ref_array_clear(struct ref_array *array)
2161 {
2162         int i;
2163
2164         for (i = 0; i < array->nr; i++)
2165                 free_array_item(array->items[i]);
2166         FREE_AND_NULL(array->items);
2167         array->nr = array->alloc = 0;
2168
2169         for (i = 0; i < used_atom_cnt; i++)
2170                 free((char *)used_atom[i].name);
2171         FREE_AND_NULL(used_atom);
2172         used_atom_cnt = 0;
2173
2174         if (ref_to_worktree_map.worktrees) {
2175                 hashmap_clear_and_free(&(ref_to_worktree_map.map),
2176                                         struct ref_to_worktree_entry, ent);
2177                 free_worktrees(ref_to_worktree_map.worktrees);
2178                 ref_to_worktree_map.worktrees = NULL;
2179         }
2180 }
2181
2182 #define EXCLUDE_REACHED 0
2183 #define INCLUDE_REACHED 1
2184 static void reach_filter(struct ref_array *array,
2185                          struct commit_list *check_reachable,
2186                          int include_reached)
2187 {
2188         struct rev_info revs;
2189         int i, old_nr;
2190         struct commit **to_clear;
2191         struct commit_list *cr;
2192
2193         if (!check_reachable)
2194                 return;
2195
2196         CALLOC_ARRAY(to_clear, array->nr);
2197
2198         repo_init_revisions(the_repository, &revs, NULL);
2199
2200         for (i = 0; i < array->nr; i++) {
2201                 struct ref_array_item *item = array->items[i];
2202                 add_pending_object(&revs, &item->commit->object, item->refname);
2203                 to_clear[i] = item->commit;
2204         }
2205
2206         for (cr = check_reachable; cr; cr = cr->next) {
2207                 struct commit *merge_commit = cr->item;
2208                 merge_commit->object.flags |= UNINTERESTING;
2209                 add_pending_object(&revs, &merge_commit->object, "");
2210         }
2211
2212         revs.limited = 1;
2213         if (prepare_revision_walk(&revs))
2214                 die(_("revision walk setup failed"));
2215
2216         old_nr = array->nr;
2217         array->nr = 0;
2218
2219         for (i = 0; i < old_nr; i++) {
2220                 struct ref_array_item *item = array->items[i];
2221                 struct commit *commit = item->commit;
2222
2223                 int is_merged = !!(commit->object.flags & UNINTERESTING);
2224
2225                 if (is_merged == include_reached)
2226                         array->items[array->nr++] = array->items[i];
2227                 else
2228                         free_array_item(item);
2229         }
2230
2231         clear_commit_marks_many(old_nr, to_clear, ALL_REV_FLAGS);
2232
2233         while (check_reachable) {
2234                 struct commit *merge_commit = pop_commit(&check_reachable);
2235                 clear_commit_marks(merge_commit, ALL_REV_FLAGS);
2236         }
2237
2238         free(to_clear);
2239 }
2240
2241 /*
2242  * API for filtering a set of refs. Based on the type of refs the user
2243  * has requested, we iterate through those refs and apply filters
2244  * as per the given ref_filter structure and finally store the
2245  * filtered refs in the ref_array structure.
2246  */
2247 int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
2248 {
2249         struct ref_filter_cbdata ref_cbdata;
2250         int ret = 0;
2251         unsigned int broken = 0;
2252
2253         ref_cbdata.array = array;
2254         ref_cbdata.filter = filter;
2255
2256         if (type & FILTER_REFS_INCLUDE_BROKEN)
2257                 broken = 1;
2258         filter->kind = type & FILTER_REFS_KIND_MASK;
2259
2260         init_contains_cache(&ref_cbdata.contains_cache);
2261         init_contains_cache(&ref_cbdata.no_contains_cache);
2262
2263         /*  Simple per-ref filtering */
2264         if (!filter->kind)
2265                 die("filter_refs: invalid type");
2266         else {
2267                 /*
2268                  * For common cases where we need only branches or remotes or tags,
2269                  * we only iterate through those refs. If a mix of refs is needed,
2270                  * we iterate over all refs and filter out required refs with the help
2271                  * of filter_ref_kind().
2272                  */
2273                 if (filter->kind == FILTER_REFS_BRANCHES)
2274                         ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken);
2275                 else if (filter->kind == FILTER_REFS_REMOTES)
2276                         ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken);
2277                 else if (filter->kind == FILTER_REFS_TAGS)
2278                         ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken);
2279                 else if (filter->kind & FILTER_REFS_ALL)
2280                         ret = for_each_fullref_in_pattern(filter, ref_filter_handler, &ref_cbdata, broken);
2281                 if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
2282                         head_ref(ref_filter_handler, &ref_cbdata);
2283         }
2284
2285         clear_contains_cache(&ref_cbdata.contains_cache);
2286         clear_contains_cache(&ref_cbdata.no_contains_cache);
2287
2288         /*  Filters that need revision walking */
2289         reach_filter(array, filter->reachable_from, INCLUDE_REACHED);
2290         reach_filter(array, filter->unreachable_from, EXCLUDE_REACHED);
2291
2292         return ret;
2293 }
2294
2295 static int compare_detached_head(struct ref_array_item *a, struct ref_array_item *b)
2296 {
2297         if (!(a->kind ^ b->kind))
2298                 BUG("ref_kind_from_refname() should only mark one ref as HEAD");
2299         if (a->kind & FILTER_REFS_DETACHED_HEAD)
2300                 return -1;
2301         else if (b->kind & FILTER_REFS_DETACHED_HEAD)
2302                 return 1;
2303         BUG("should have died in the xor check above");
2304         return 0;
2305 }
2306
2307 static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
2308 {
2309         struct atom_value *va, *vb;
2310         int cmp;
2311         int cmp_detached_head = 0;
2312         cmp_type cmp_type = used_atom[s->atom].type;
2313         struct strbuf err = STRBUF_INIT;
2314
2315         if (get_ref_atom_value(a, s->atom, &va, &err))
2316                 die("%s", err.buf);
2317         if (get_ref_atom_value(b, s->atom, &vb, &err))
2318                 die("%s", err.buf);
2319         strbuf_release(&err);
2320         if (s->sort_flags & REF_SORTING_DETACHED_HEAD_FIRST &&
2321             ((a->kind | b->kind) & FILTER_REFS_DETACHED_HEAD)) {
2322                 cmp = compare_detached_head(a, b);
2323                 cmp_detached_head = 1;
2324         } else if (s->sort_flags & REF_SORTING_VERSION) {
2325                 cmp = versioncmp(va->s, vb->s);
2326         } else if (cmp_type == FIELD_STR) {
2327                 int (*cmp_fn)(const char *, const char *);
2328                 cmp_fn = s->sort_flags & REF_SORTING_ICASE
2329                         ? strcasecmp : strcmp;
2330                 cmp = cmp_fn(va->s, vb->s);
2331         } else {
2332                 if (va->value < vb->value)
2333                         cmp = -1;
2334                 else if (va->value == vb->value)
2335                         cmp = 0;
2336                 else
2337                         cmp = 1;
2338         }
2339
2340         return (s->sort_flags & REF_SORTING_REVERSE && !cmp_detached_head)
2341                 ? -cmp : cmp;
2342 }
2343
2344 static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
2345 {
2346         struct ref_array_item *a = *((struct ref_array_item **)a_);
2347         struct ref_array_item *b = *((struct ref_array_item **)b_);
2348         struct ref_sorting *s;
2349
2350         for (s = ref_sorting; s; s = s->next) {
2351                 int cmp = cmp_ref_sorting(s, a, b);
2352                 if (cmp)
2353                         return cmp;
2354         }
2355         s = ref_sorting;
2356         return s && s->sort_flags & REF_SORTING_ICASE ?
2357                 strcasecmp(a->refname, b->refname) :
2358                 strcmp(a->refname, b->refname);
2359 }
2360
2361 void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting,
2362                                     unsigned int mask, int on)
2363 {
2364         for (; sorting; sorting = sorting->next) {
2365                 if (on)
2366                         sorting->sort_flags |= mask;
2367                 else
2368                         sorting->sort_flags &= ~mask;
2369         }
2370 }
2371
2372 void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
2373 {
2374         QSORT_S(array->items, array->nr, compare_refs, sorting);
2375 }
2376
2377 static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
2378 {
2379         struct strbuf *s = &state->stack->output;
2380
2381         while (*cp && (!ep || cp < ep)) {
2382                 if (*cp == '%') {
2383                         if (cp[1] == '%')
2384                                 cp++;
2385                         else {
2386                                 int ch = hex2chr(cp + 1);
2387                                 if (0 <= ch) {
2388                                         strbuf_addch(s, ch);
2389                                         cp += 3;
2390                                         continue;
2391                                 }
2392                         }
2393                 }
2394                 strbuf_addch(s, *cp);
2395                 cp++;
2396         }
2397 }
2398
2399 int format_ref_array_item(struct ref_array_item *info,
2400                            const struct ref_format *format,
2401                            struct strbuf *final_buf,
2402                            struct strbuf *error_buf)
2403 {
2404         const char *cp, *sp, *ep;
2405         struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
2406
2407         state.quote_style = format->quote_style;
2408         push_stack_element(&state.stack);
2409
2410         for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) {
2411                 struct atom_value *atomv;
2412                 int pos;
2413
2414                 ep = strchr(sp, ')');
2415                 if (cp < sp)
2416                         append_literal(cp, sp, &state);
2417                 pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf);
2418                 if (pos < 0 || get_ref_atom_value(info, pos, &atomv, error_buf) ||
2419                     atomv->handler(atomv, &state, error_buf)) {
2420                         pop_stack_element(&state.stack);
2421                         return -1;
2422                 }
2423         }
2424         if (*cp) {
2425                 sp = cp + strlen(cp);
2426                 append_literal(cp, sp, &state);
2427         }
2428         if (format->need_color_reset_at_eol) {
2429                 struct atom_value resetv;
2430                 resetv.s = GIT_COLOR_RESET;
2431                 if (append_atom(&resetv, &state, error_buf)) {
2432                         pop_stack_element(&state.stack);
2433                         return -1;
2434                 }
2435         }
2436         if (state.stack->prev) {
2437                 pop_stack_element(&state.stack);
2438                 return strbuf_addf_ret(error_buf, -1, _("format: %%(end) atom missing"));
2439         }
2440         strbuf_addbuf(final_buf, &state.stack->output);
2441         pop_stack_element(&state.stack);
2442         return 0;
2443 }
2444
2445 void pretty_print_ref(const char *name, const struct object_id *oid,
2446                       const struct ref_format *format)
2447 {
2448         struct ref_array_item *ref_item;
2449         struct strbuf output = STRBUF_INIT;
2450         struct strbuf err = STRBUF_INIT;
2451
2452         ref_item = new_ref_array_item(name, oid);
2453         ref_item->kind = ref_kind_from_refname(name);
2454         if (format_ref_array_item(ref_item, format, &output, &err))
2455                 die("%s", err.buf);
2456         fwrite(output.buf, 1, output.len, stdout);
2457         putchar('\n');
2458
2459         strbuf_release(&err);
2460         strbuf_release(&output);
2461         free_array_item(ref_item);
2462 }
2463
2464 static int parse_sorting_atom(const char *atom)
2465 {
2466         /*
2467          * This parses an atom using a dummy ref_format, since we don't
2468          * actually care about the formatting details.
2469          */
2470         struct ref_format dummy = REF_FORMAT_INIT;
2471         const char *end = atom + strlen(atom);
2472         struct strbuf err = STRBUF_INIT;
2473         int res = parse_ref_filter_atom(&dummy, atom, end, &err);
2474         if (res < 0)
2475                 die("%s", err.buf);
2476         strbuf_release(&err);
2477         return res;
2478 }
2479
2480 /*  If no sorting option is given, use refname to sort as default */
2481 struct ref_sorting *ref_default_sorting(void)
2482 {
2483         static const char cstr_name[] = "refname";
2484
2485         struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
2486
2487         sorting->next = NULL;
2488         sorting->atom = parse_sorting_atom(cstr_name);
2489         return sorting;
2490 }
2491
2492 void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
2493 {
2494         struct ref_sorting *s;
2495
2496         CALLOC_ARRAY(s, 1);
2497         s->next = *sorting_tail;
2498         *sorting_tail = s;
2499
2500         if (*arg == '-') {
2501                 s->sort_flags |= REF_SORTING_REVERSE;
2502                 arg++;
2503         }
2504         if (skip_prefix(arg, "version:", &arg) ||
2505             skip_prefix(arg, "v:", &arg))
2506                 s->sort_flags |= REF_SORTING_VERSION;
2507         s->atom = parse_sorting_atom(arg);
2508 }
2509
2510 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
2511 {
2512         /*
2513          * NEEDSWORK: We should probably clear the list in this case, but we've
2514          * already munged the global used_atoms list, which would need to be
2515          * undone.
2516          */
2517         BUG_ON_OPT_NEG(unset);
2518
2519         parse_ref_sorting(opt->value, arg);
2520         return 0;
2521 }
2522
2523 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
2524 {
2525         struct ref_filter *rf = opt->value;
2526         struct object_id oid;
2527         struct commit *merge_commit;
2528
2529         BUG_ON_OPT_NEG(unset);
2530
2531         if (get_oid(arg, &oid))
2532                 die(_("malformed object name %s"), arg);
2533
2534         merge_commit = lookup_commit_reference_gently(the_repository, &oid, 0);
2535
2536         if (!merge_commit)
2537                 return error(_("option `%s' must point to a commit"), opt->long_name);
2538
2539         if (starts_with(opt->long_name, "no"))
2540                 commit_list_insert(merge_commit, &rf->unreachable_from);
2541         else
2542                 commit_list_insert(merge_commit, &rf->reachable_from);
2543
2544         return 0;
2545 }