]> git.ipfire.org Git - thirdparty/git.git/blob - ref-filter.c
Merge branch 'pb/template-for-single-commit-pr' into maint-2.43
[thirdparty/git.git] / ref-filter.c
1 #include "git-compat-util.h"
2 #include "environment.h"
3 #include "gettext.h"
4 #include "config.h"
5 #include "gpg-interface.h"
6 #include "hex.h"
7 #include "parse-options.h"
8 #include "run-command.h"
9 #include "refs.h"
10 #include "wildmatch.h"
11 #include "object-name.h"
12 #include "object-store-ll.h"
13 #include "oid-array.h"
14 #include "repository.h"
15 #include "commit.h"
16 #include "mailmap.h"
17 #include "ident.h"
18 #include "remote.h"
19 #include "color.h"
20 #include "tag.h"
21 #include "quote.h"
22 #include "ref-filter.h"
23 #include "revision.h"
24 #include "utf8.h"
25 #include "versioncmp.h"
26 #include "trailer.h"
27 #include "wt-status.h"
28 #include "commit-slab.h"
29 #include "commit-reach.h"
30 #include "worktree.h"
31 #include "hashmap.h"
32
33 static struct ref_msg {
34 const char *gone;
35 const char *ahead;
36 const char *behind;
37 const char *ahead_behind;
38 } msgs = {
39 /* Untranslated plumbing messages: */
40 "gone",
41 "ahead %d",
42 "behind %d",
43 "ahead %d, behind %d"
44 };
45
46 void setup_ref_filter_porcelain_msg(void)
47 {
48 msgs.gone = _("gone");
49 msgs.ahead = _("ahead %d");
50 msgs.behind = _("behind %d");
51 msgs.ahead_behind = _("ahead %d, behind %d");
52 }
53
54 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
55 typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status;
56 typedef enum { SOURCE_NONE = 0, SOURCE_OBJ, SOURCE_OTHER } info_source;
57
58 struct align {
59 align_type position;
60 unsigned int width;
61 };
62
63 struct if_then_else {
64 cmp_status cmp_status;
65 const char *str;
66 unsigned int then_atom_seen : 1,
67 else_atom_seen : 1,
68 condition_satisfied : 1;
69 };
70
71 struct refname_atom {
72 enum { R_NORMAL, R_SHORT, R_LSTRIP, R_RSTRIP } option;
73 int lstrip, rstrip;
74 };
75
76 static struct ref_trailer_buf {
77 struct string_list filter_list;
78 struct strbuf sepbuf;
79 struct strbuf kvsepbuf;
80 } ref_trailer_buf = {STRING_LIST_INIT_NODUP, STRBUF_INIT, STRBUF_INIT};
81
82 static struct expand_data {
83 struct object_id oid;
84 enum object_type type;
85 unsigned long size;
86 off_t disk_size;
87 struct object_id delta_base_oid;
88 void *content;
89
90 struct object_info info;
91 } oi, oi_deref;
92
93 struct ref_to_worktree_entry {
94 struct hashmap_entry ent;
95 struct worktree *wt; /* key is wt->head_ref */
96 };
97
98 static int ref_to_worktree_map_cmpfnc(const void *lookupdata UNUSED,
99 const struct hashmap_entry *eptr,
100 const struct hashmap_entry *kptr,
101 const void *keydata_aka_refname)
102 {
103 const struct ref_to_worktree_entry *e, *k;
104
105 e = container_of(eptr, const struct ref_to_worktree_entry, ent);
106 k = container_of(kptr, const struct ref_to_worktree_entry, ent);
107
108 return strcmp(e->wt->head_ref,
109 keydata_aka_refname ? keydata_aka_refname : k->wt->head_ref);
110 }
111
112 static struct ref_to_worktree_map {
113 struct hashmap map;
114 struct worktree **worktrees;
115 } ref_to_worktree_map;
116
117 /*
118 * The enum atom_type is used as the index of valid_atom array.
119 * In the atom parsing stage, it will be passed to used_atom.atom_type
120 * as the identifier of the atom type. We can check the type of used_atom
121 * entry by `if (used_atom[i].atom_type == ATOM_*)`.
122 */
123 enum atom_type {
124 ATOM_REFNAME,
125 ATOM_OBJECTTYPE,
126 ATOM_OBJECTSIZE,
127 ATOM_OBJECTNAME,
128 ATOM_DELTABASE,
129 ATOM_TREE,
130 ATOM_PARENT,
131 ATOM_NUMPARENT,
132 ATOM_OBJECT,
133 ATOM_TYPE,
134 ATOM_TAG,
135 ATOM_AUTHOR,
136 ATOM_AUTHORNAME,
137 ATOM_AUTHOREMAIL,
138 ATOM_AUTHORDATE,
139 ATOM_COMMITTER,
140 ATOM_COMMITTERNAME,
141 ATOM_COMMITTEREMAIL,
142 ATOM_COMMITTERDATE,
143 ATOM_TAGGER,
144 ATOM_TAGGERNAME,
145 ATOM_TAGGEREMAIL,
146 ATOM_TAGGERDATE,
147 ATOM_CREATOR,
148 ATOM_CREATORDATE,
149 ATOM_DESCRIBE,
150 ATOM_SUBJECT,
151 ATOM_BODY,
152 ATOM_TRAILERS,
153 ATOM_CONTENTS,
154 ATOM_SIGNATURE,
155 ATOM_RAW,
156 ATOM_UPSTREAM,
157 ATOM_PUSH,
158 ATOM_SYMREF,
159 ATOM_FLAG,
160 ATOM_HEAD,
161 ATOM_COLOR,
162 ATOM_WORKTREEPATH,
163 ATOM_ALIGN,
164 ATOM_END,
165 ATOM_IF,
166 ATOM_THEN,
167 ATOM_ELSE,
168 ATOM_REST,
169 ATOM_AHEADBEHIND,
170 };
171
172 /*
173 * An atom is a valid field atom listed below, possibly prefixed with
174 * a "*" to denote deref_tag().
175 *
176 * We parse given format string and sort specifiers, and make a list
177 * of properties that we need to extract out of objects. ref_array_item
178 * structure will hold an array of values extracted that can be
179 * indexed with the "atom number", which is an index into this
180 * array.
181 */
182 static struct used_atom {
183 enum atom_type atom_type;
184 const char *name;
185 cmp_type type;
186 info_source source;
187 union {
188 char color[COLOR_MAXLEN];
189 struct align align;
190 struct {
191 enum {
192 RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME, RR_REMOTE_REF
193 } option;
194 struct refname_atom refname;
195 unsigned int nobracket : 1, push : 1, push_remote : 1;
196 } remote_ref;
197 struct {
198 enum { C_BARE, C_BODY, C_BODY_DEP, C_LENGTH, C_LINES,
199 C_SIG, C_SUB, C_SUB_SANITIZE, C_TRAILERS } option;
200 struct process_trailer_options trailer_opts;
201 unsigned int nlines;
202 } contents;
203 struct {
204 enum { RAW_BARE, RAW_LENGTH } option;
205 } raw_data;
206 struct {
207 cmp_status cmp_status;
208 const char *str;
209 } if_then_else;
210 struct {
211 enum { O_FULL, O_LENGTH, O_SHORT } option;
212 unsigned int length;
213 } oid;
214 struct {
215 enum { O_SIZE, O_SIZE_DISK } option;
216 } objectsize;
217 struct {
218 enum { N_RAW, N_MAILMAP } option;
219 } name_option;
220 struct {
221 enum {
222 EO_RAW = 0,
223 EO_TRIM = 1<<0,
224 EO_LOCALPART = 1<<1,
225 EO_MAILMAP = 1<<2,
226 } option;
227 } email_option;
228 struct {
229 enum { S_BARE, S_GRADE, S_SIGNER, S_KEY,
230 S_FINGERPRINT, S_PRI_KEY_FP, S_TRUST_LEVEL } option;
231 } signature;
232 const char **describe_args;
233 struct refname_atom refname;
234 char *head;
235 } u;
236 } *used_atom;
237 static int used_atom_cnt, need_tagged, need_symref;
238
239 /*
240 * Expand string, append it to strbuf *sb, then return error code ret.
241 * Allow to save few lines of code.
242 */
243 __attribute__((format (printf, 3, 4)))
244 static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...)
245 {
246 va_list ap;
247 va_start(ap, fmt);
248 strbuf_vaddf(sb, fmt, ap);
249 va_end(ap);
250 return ret;
251 }
252
253 static int err_no_arg(struct strbuf *sb, const char *name)
254 {
255 size_t namelen = strchrnul(name, ':') - name;
256 strbuf_addf(sb, _("%%(%.*s) does not take arguments"),
257 (int)namelen, name);
258 return -1;
259 }
260
261 static int err_bad_arg(struct strbuf *sb, const char *name, const char *arg)
262 {
263 size_t namelen = strchrnul(name, ':') - name;
264 strbuf_addf(sb, _("unrecognized %%(%.*s) argument: %s"),
265 (int)namelen, name, arg);
266 return -1;
267 }
268
269 /*
270 * Parse option of name "candidate" in the option string "to_parse" of
271 * the form
272 *
273 * "candidate1[=val1],candidate2[=val2],candidate3[=val3],..."
274 *
275 * The remaining part of "to_parse" is stored in "end" (if we are
276 * parsing the last candidate, then this is NULL) and the value of
277 * the candidate is stored in "valuestart" and its length in "valuelen",
278 * that is the portion after "=". Since it is possible for a "candidate"
279 * to not have a value, in such cases, "valuestart" is set to point to
280 * NULL and "valuelen" to 0.
281 *
282 * The function returns 1 on success. It returns 0 if we don't find
283 * "candidate" in "to_parse" or we find "candidate" but it is followed
284 * by more chars (for example, "candidatefoo"), that is, we don't find
285 * an exact match.
286 *
287 * This function only does the above for one "candidate" at a time. So
288 * it has to be called each time trying to parse a "candidate" in the
289 * option string "to_parse".
290 */
291 static int match_atom_arg_value(const char *to_parse, const char *candidate,
292 const char **end, const char **valuestart,
293 size_t *valuelen)
294 {
295 const char *atom;
296
297 if (!skip_prefix(to_parse, candidate, &atom))
298 return 0; /* definitely not "candidate" */
299
300 if (*atom == '=') {
301 /* we just saw "candidate=" */
302 *valuestart = atom + 1;
303 atom = strchrnul(*valuestart, ',');
304 *valuelen = atom - *valuestart;
305 } else if (*atom != ',' && *atom != '\0') {
306 /* key begins with "candidate" but has more chars */
307 return 0;
308 } else {
309 /* just "candidate" without "=val" */
310 *valuestart = NULL;
311 *valuelen = 0;
312 }
313
314 /* atom points at either the ',' or NUL after this key[=val] */
315 if (*atom == ',')
316 atom++;
317 else if (*atom)
318 BUG("Why is *atom not NULL yet?");
319
320 *end = atom;
321 return 1;
322 }
323
324 /*
325 * Parse boolean option of name "candidate" in the option list "to_parse"
326 * of the form
327 *
328 * "candidate1[=bool1],candidate2[=bool2],candidate3[=bool3],..."
329 *
330 * The remaining part of "to_parse" is stored in "end" (if we are parsing
331 * the last candidate, then this is NULL) and the value (if given) is
332 * parsed and stored in "val", so "val" always points to either 0 or 1.
333 * If the value is not given, then "val" is set to point to 1.
334 *
335 * The boolean value is parsed using "git_parse_maybe_bool()", so the
336 * accepted values are
337 *
338 * to set true - "1", "yes", "true"
339 * to set false - "0", "no", "false"
340 *
341 * This function returns 1 on success. It returns 0 when we don't find
342 * an exact match for "candidate" or when the boolean value given is
343 * not valid.
344 */
345 static int match_atom_bool_arg(const char *to_parse, const char *candidate,
346 const char **end, int *val)
347 {
348 const char *argval;
349 char *strval;
350 size_t arglen;
351 int v;
352
353 if (!match_atom_arg_value(to_parse, candidate, end, &argval, &arglen))
354 return 0;
355
356 if (!argval) {
357 *val = 1;
358 return 1;
359 }
360
361 strval = xstrndup(argval, arglen);
362 v = git_parse_maybe_bool(strval);
363 free(strval);
364
365 if (v == -1)
366 return 0;
367
368 *val = v;
369
370 return 1;
371 }
372
373 static int color_atom_parser(struct ref_format *format, struct used_atom *atom,
374 const char *color_value, struct strbuf *err)
375 {
376 if (!color_value)
377 return strbuf_addf_ret(err, -1, _("expected format: %%(color:<color>)"));
378 if (color_parse(color_value, atom->u.color) < 0)
379 return strbuf_addf_ret(err, -1, _("unrecognized color: %%(color:%s)"),
380 color_value);
381 /*
382 * We check this after we've parsed the color, which lets us complain
383 * about syntactically bogus color names even if they won't be used.
384 */
385 if (!want_color(format->use_color))
386 color_parse("", atom->u.color);
387 return 0;
388 }
389
390 static int refname_atom_parser_internal(struct refname_atom *atom, const char *arg,
391 const char *name, struct strbuf *err)
392 {
393 if (!arg)
394 atom->option = R_NORMAL;
395 else if (!strcmp(arg, "short"))
396 atom->option = R_SHORT;
397 else if (skip_prefix(arg, "lstrip=", &arg) ||
398 skip_prefix(arg, "strip=", &arg)) {
399 atom->option = R_LSTRIP;
400 if (strtol_i(arg, 10, &atom->lstrip))
401 return strbuf_addf_ret(err, -1, _("Integer value expected refname:lstrip=%s"), arg);
402 } else if (skip_prefix(arg, "rstrip=", &arg)) {
403 atom->option = R_RSTRIP;
404 if (strtol_i(arg, 10, &atom->rstrip))
405 return strbuf_addf_ret(err, -1, _("Integer value expected refname:rstrip=%s"), arg);
406 } else
407 return err_bad_arg(err, name, arg);
408 return 0;
409 }
410
411 static int remote_ref_atom_parser(struct ref_format *format UNUSED,
412 struct used_atom *atom,
413 const char *arg, struct strbuf *err)
414 {
415 struct string_list params = STRING_LIST_INIT_DUP;
416 int i;
417
418 if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:"))
419 atom->u.remote_ref.push = 1;
420
421 if (!arg) {
422 atom->u.remote_ref.option = RR_REF;
423 return refname_atom_parser_internal(&atom->u.remote_ref.refname,
424 arg, atom->name, err);
425 }
426
427 atom->u.remote_ref.nobracket = 0;
428 string_list_split(&params, arg, ',', -1);
429
430 for (i = 0; i < params.nr; i++) {
431 const char *s = params.items[i].string;
432
433 if (!strcmp(s, "track"))
434 atom->u.remote_ref.option = RR_TRACK;
435 else if (!strcmp(s, "trackshort"))
436 atom->u.remote_ref.option = RR_TRACKSHORT;
437 else if (!strcmp(s, "nobracket"))
438 atom->u.remote_ref.nobracket = 1;
439 else if (!strcmp(s, "remotename")) {
440 atom->u.remote_ref.option = RR_REMOTE_NAME;
441 atom->u.remote_ref.push_remote = 1;
442 } else if (!strcmp(s, "remoteref")) {
443 atom->u.remote_ref.option = RR_REMOTE_REF;
444 atom->u.remote_ref.push_remote = 1;
445 } else {
446 atom->u.remote_ref.option = RR_REF;
447 if (refname_atom_parser_internal(&atom->u.remote_ref.refname,
448 arg, atom->name, err)) {
449 string_list_clear(&params, 0);
450 return -1;
451 }
452 }
453 }
454
455 string_list_clear(&params, 0);
456 return 0;
457 }
458
459 static int objecttype_atom_parser(struct ref_format *format UNUSED,
460 struct used_atom *atom,
461 const char *arg, struct strbuf *err)
462 {
463 if (arg)
464 return err_no_arg(err, "objecttype");
465 if (*atom->name == '*')
466 oi_deref.info.typep = &oi_deref.type;
467 else
468 oi.info.typep = &oi.type;
469 return 0;
470 }
471
472 static int objectsize_atom_parser(struct ref_format *format UNUSED,
473 struct used_atom *atom,
474 const char *arg, struct strbuf *err)
475 {
476 if (!arg) {
477 atom->u.objectsize.option = O_SIZE;
478 if (*atom->name == '*')
479 oi_deref.info.sizep = &oi_deref.size;
480 else
481 oi.info.sizep = &oi.size;
482 } else if (!strcmp(arg, "disk")) {
483 atom->u.objectsize.option = O_SIZE_DISK;
484 if (*atom->name == '*')
485 oi_deref.info.disk_sizep = &oi_deref.disk_size;
486 else
487 oi.info.disk_sizep = &oi.disk_size;
488 } else
489 return err_bad_arg(err, "objectsize", arg);
490 return 0;
491 }
492
493 static int deltabase_atom_parser(struct ref_format *format UNUSED,
494 struct used_atom *atom,
495 const char *arg, struct strbuf *err)
496 {
497 if (arg)
498 return err_no_arg(err, "deltabase");
499 if (*atom->name == '*')
500 oi_deref.info.delta_base_oid = &oi_deref.delta_base_oid;
501 else
502 oi.info.delta_base_oid = &oi.delta_base_oid;
503 return 0;
504 }
505
506 static int body_atom_parser(struct ref_format *format UNUSED,
507 struct used_atom *atom,
508 const char *arg, struct strbuf *err)
509 {
510 if (arg)
511 return err_no_arg(err, "body");
512 atom->u.contents.option = C_BODY_DEP;
513 return 0;
514 }
515
516 static int subject_atom_parser(struct ref_format *format UNUSED,
517 struct used_atom *atom,
518 const char *arg, struct strbuf *err)
519 {
520 if (!arg)
521 atom->u.contents.option = C_SUB;
522 else if (!strcmp(arg, "sanitize"))
523 atom->u.contents.option = C_SUB_SANITIZE;
524 else
525 return err_bad_arg(err, "subject", arg);
526 return 0;
527 }
528
529 static int parse_signature_option(const char *arg)
530 {
531 if (!arg)
532 return S_BARE;
533 else if (!strcmp(arg, "signer"))
534 return S_SIGNER;
535 else if (!strcmp(arg, "grade"))
536 return S_GRADE;
537 else if (!strcmp(arg, "key"))
538 return S_KEY;
539 else if (!strcmp(arg, "fingerprint"))
540 return S_FINGERPRINT;
541 else if (!strcmp(arg, "primarykeyfingerprint"))
542 return S_PRI_KEY_FP;
543 else if (!strcmp(arg, "trustlevel"))
544 return S_TRUST_LEVEL;
545 return -1;
546 }
547
548 static int signature_atom_parser(struct ref_format *format UNUSED,
549 struct used_atom *atom,
550 const char *arg, struct strbuf *err)
551 {
552 int opt = parse_signature_option(arg);
553 if (opt < 0)
554 return err_bad_arg(err, "signature", arg);
555 atom->u.signature.option = opt;
556 return 0;
557 }
558
559 static int trailers_atom_parser(struct ref_format *format UNUSED,
560 struct used_atom *atom,
561 const char *arg, struct strbuf *err)
562 {
563 atom->u.contents.trailer_opts.no_divider = 1;
564
565 if (arg) {
566 const char *argbuf = xstrfmt("%s)", arg);
567 char *invalid_arg = NULL;
568
569 if (format_set_trailers_options(&atom->u.contents.trailer_opts,
570 &ref_trailer_buf.filter_list,
571 &ref_trailer_buf.sepbuf,
572 &ref_trailer_buf.kvsepbuf,
573 &argbuf, &invalid_arg)) {
574 if (!invalid_arg)
575 strbuf_addf(err, _("expected %%(trailers:key=<value>)"));
576 else
577 strbuf_addf(err, _("unknown %%(trailers) argument: %s"), invalid_arg);
578 free((char *)invalid_arg);
579 return -1;
580 }
581 }
582 atom->u.contents.option = C_TRAILERS;
583 return 0;
584 }
585
586 static int contents_atom_parser(struct ref_format *format, struct used_atom *atom,
587 const char *arg, struct strbuf *err)
588 {
589 if (!arg)
590 atom->u.contents.option = C_BARE;
591 else if (!strcmp(arg, "body"))
592 atom->u.contents.option = C_BODY;
593 else if (!strcmp(arg, "size")) {
594 atom->type = FIELD_ULONG;
595 atom->u.contents.option = C_LENGTH;
596 } else if (!strcmp(arg, "signature"))
597 atom->u.contents.option = C_SIG;
598 else if (!strcmp(arg, "subject"))
599 atom->u.contents.option = C_SUB;
600 else if (!strcmp(arg, "trailers")) {
601 if (trailers_atom_parser(format, atom, NULL, err))
602 return -1;
603 } else if (skip_prefix(arg, "trailers:", &arg)) {
604 if (trailers_atom_parser(format, atom, arg, err))
605 return -1;
606 } else if (skip_prefix(arg, "lines=", &arg)) {
607 atom->u.contents.option = C_LINES;
608 if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
609 return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg);
610 } else
611 return err_bad_arg(err, "contents", arg);
612 return 0;
613 }
614
615 static int describe_atom_option_parser(struct strvec *args, const char **arg,
616 struct strbuf *err)
617 {
618 const char *argval;
619 size_t arglen = 0;
620 int optval = 0;
621
622 if (match_atom_bool_arg(*arg, "tags", arg, &optval)) {
623 if (!optval)
624 strvec_push(args, "--no-tags");
625 else
626 strvec_push(args, "--tags");
627 return 1;
628 }
629
630 if (match_atom_arg_value(*arg, "abbrev", arg, &argval, &arglen)) {
631 char *endptr;
632
633 if (!arglen)
634 return strbuf_addf_ret(err, -1,
635 _("argument expected for %s"),
636 "describe:abbrev");
637 if (strtol(argval, &endptr, 10) < 0)
638 return strbuf_addf_ret(err, -1,
639 _("positive value expected %s=%s"),
640 "describe:abbrev", argval);
641 if (endptr - argval != arglen)
642 return strbuf_addf_ret(err, -1,
643 _("cannot fully parse %s=%s"),
644 "describe:abbrev", argval);
645
646 strvec_pushf(args, "--abbrev=%.*s", (int)arglen, argval);
647 return 1;
648 }
649
650 if (match_atom_arg_value(*arg, "match", arg, &argval, &arglen)) {
651 if (!arglen)
652 return strbuf_addf_ret(err, -1,
653 _("value expected %s="),
654 "describe:match");
655
656 strvec_pushf(args, "--match=%.*s", (int)arglen, argval);
657 return 1;
658 }
659
660 if (match_atom_arg_value(*arg, "exclude", arg, &argval, &arglen)) {
661 if (!arglen)
662 return strbuf_addf_ret(err, -1,
663 _("value expected %s="),
664 "describe:exclude");
665
666 strvec_pushf(args, "--exclude=%.*s", (int)arglen, argval);
667 return 1;
668 }
669
670 return 0;
671 }
672
673 static int describe_atom_parser(struct ref_format *format UNUSED,
674 struct used_atom *atom,
675 const char *arg, struct strbuf *err)
676 {
677 struct strvec args = STRVEC_INIT;
678
679 for (;;) {
680 int found = 0;
681 const char *bad_arg = arg;
682
683 if (!arg || !*arg)
684 break;
685
686 found = describe_atom_option_parser(&args, &arg, err);
687 if (found < 0)
688 return found;
689 if (!found)
690 return err_bad_arg(err, "describe", bad_arg);
691 }
692 atom->u.describe_args = strvec_detach(&args);
693 return 0;
694 }
695
696 static int raw_atom_parser(struct ref_format *format UNUSED,
697 struct used_atom *atom,
698 const char *arg, struct strbuf *err)
699 {
700 if (!arg)
701 atom->u.raw_data.option = RAW_BARE;
702 else if (!strcmp(arg, "size")) {
703 atom->type = FIELD_ULONG;
704 atom->u.raw_data.option = RAW_LENGTH;
705 } else
706 return err_bad_arg(err, "raw", arg);
707 return 0;
708 }
709
710 static int oid_atom_parser(struct ref_format *format UNUSED,
711 struct used_atom *atom,
712 const char *arg, struct strbuf *err)
713 {
714 if (!arg)
715 atom->u.oid.option = O_FULL;
716 else if (!strcmp(arg, "short"))
717 atom->u.oid.option = O_SHORT;
718 else if (skip_prefix(arg, "short=", &arg)) {
719 atom->u.oid.option = O_LENGTH;
720 if (strtoul_ui(arg, 10, &atom->u.oid.length) ||
721 atom->u.oid.length == 0)
722 return strbuf_addf_ret(err, -1, _("positive value expected '%s' in %%(%s)"), arg, atom->name);
723 if (atom->u.oid.length < MINIMUM_ABBREV)
724 atom->u.oid.length = MINIMUM_ABBREV;
725 } else
726 return err_bad_arg(err, atom->name, arg);
727 return 0;
728 }
729
730 static int person_name_atom_parser(struct ref_format *format UNUSED,
731 struct used_atom *atom,
732 const char *arg, struct strbuf *err)
733 {
734 if (!arg)
735 atom->u.name_option.option = N_RAW;
736 else if (!strcmp(arg, "mailmap"))
737 atom->u.name_option.option = N_MAILMAP;
738 else
739 return err_bad_arg(err, atom->name, arg);
740 return 0;
741 }
742
743 static int email_atom_option_parser(struct used_atom *atom,
744 const char **arg, struct strbuf *err)
745 {
746 if (!*arg)
747 return EO_RAW;
748 if (skip_prefix(*arg, "trim", arg))
749 return EO_TRIM;
750 if (skip_prefix(*arg, "localpart", arg))
751 return EO_LOCALPART;
752 if (skip_prefix(*arg, "mailmap", arg))
753 return EO_MAILMAP;
754 return -1;
755 }
756
757 static int person_email_atom_parser(struct ref_format *format UNUSED,
758 struct used_atom *atom,
759 const char *arg, struct strbuf *err)
760 {
761 for (;;) {
762 int opt = email_atom_option_parser(atom, &arg, err);
763 const char *bad_arg = arg;
764
765 if (opt < 0)
766 return err_bad_arg(err, atom->name, bad_arg);
767 atom->u.email_option.option |= opt;
768
769 if (!arg || !*arg)
770 break;
771 if (*arg == ',')
772 arg++;
773 else
774 return err_bad_arg(err, atom->name, bad_arg);
775 }
776 return 0;
777 }
778
779 static int refname_atom_parser(struct ref_format *format UNUSED,
780 struct used_atom *atom,
781 const char *arg, struct strbuf *err)
782 {
783 return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err);
784 }
785
786 static align_type parse_align_position(const char *s)
787 {
788 if (!strcmp(s, "right"))
789 return ALIGN_RIGHT;
790 else if (!strcmp(s, "middle"))
791 return ALIGN_MIDDLE;
792 else if (!strcmp(s, "left"))
793 return ALIGN_LEFT;
794 return -1;
795 }
796
797 static int align_atom_parser(struct ref_format *format UNUSED,
798 struct used_atom *atom,
799 const char *arg, struct strbuf *err)
800 {
801 struct align *align = &atom->u.align;
802 struct string_list params = STRING_LIST_INIT_DUP;
803 int i;
804 unsigned int width = ~0U;
805
806 if (!arg)
807 return strbuf_addf_ret(err, -1, _("expected format: %%(align:<width>,<position>)"));
808
809 align->position = ALIGN_LEFT;
810
811 string_list_split(&params, arg, ',', -1);
812 for (i = 0; i < params.nr; i++) {
813 const char *s = params.items[i].string;
814 int position;
815
816 if (skip_prefix(s, "position=", &s)) {
817 position = parse_align_position(s);
818 if (position < 0) {
819 strbuf_addf(err, _("unrecognized position:%s"), s);
820 string_list_clear(&params, 0);
821 return -1;
822 }
823 align->position = position;
824 } else if (skip_prefix(s, "width=", &s)) {
825 if (strtoul_ui(s, 10, &width)) {
826 strbuf_addf(err, _("unrecognized width:%s"), s);
827 string_list_clear(&params, 0);
828 return -1;
829 }
830 } else if (!strtoul_ui(s, 10, &width))
831 ;
832 else if ((position = parse_align_position(s)) >= 0)
833 align->position = position;
834 else {
835 strbuf_addf(err, _("unrecognized %%(%s) argument: %s"), "align", s);
836 string_list_clear(&params, 0);
837 return -1;
838 }
839 }
840
841 if (width == ~0U) {
842 string_list_clear(&params, 0);
843 return strbuf_addf_ret(err, -1, _("positive width expected with the %%(align) atom"));
844 }
845 align->width = width;
846 string_list_clear(&params, 0);
847 return 0;
848 }
849
850 static int if_atom_parser(struct ref_format *format UNUSED,
851 struct used_atom *atom,
852 const char *arg, struct strbuf *err)
853 {
854 if (!arg) {
855 atom->u.if_then_else.cmp_status = COMPARE_NONE;
856 return 0;
857 } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) {
858 atom->u.if_then_else.cmp_status = COMPARE_EQUAL;
859 } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) {
860 atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL;
861 } else
862 return err_bad_arg(err, "if", arg);
863 return 0;
864 }
865
866 static int rest_atom_parser(struct ref_format *format UNUSED,
867 struct used_atom *atom UNUSED,
868 const char *arg, struct strbuf *err)
869 {
870 if (arg)
871 return err_no_arg(err, "rest");
872 return 0;
873 }
874
875 static int ahead_behind_atom_parser(struct ref_format *format,
876 struct used_atom *atom UNUSED,
877 const char *arg, struct strbuf *err)
878 {
879 struct string_list_item *item;
880
881 if (!arg)
882 return strbuf_addf_ret(err, -1, _("expected format: %%(ahead-behind:<committish>)"));
883
884 item = string_list_append(&format->bases, arg);
885 item->util = lookup_commit_reference_by_name(arg);
886 if (!item->util)
887 die("failed to find '%s'", arg);
888
889 return 0;
890 }
891
892 static int head_atom_parser(struct ref_format *format UNUSED,
893 struct used_atom *atom,
894 const char *arg, struct strbuf *err)
895 {
896 if (arg)
897 return err_no_arg(err, "HEAD");
898 atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
899 return 0;
900 }
901
902 static struct {
903 const char *name;
904 info_source source;
905 cmp_type cmp_type;
906 int (*parser)(struct ref_format *format, struct used_atom *atom,
907 const char *arg, struct strbuf *err);
908 } valid_atom[] = {
909 [ATOM_REFNAME] = { "refname", SOURCE_NONE, FIELD_STR, refname_atom_parser },
910 [ATOM_OBJECTTYPE] = { "objecttype", SOURCE_OTHER, FIELD_STR, objecttype_atom_parser },
911 [ATOM_OBJECTSIZE] = { "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser },
912 [ATOM_OBJECTNAME] = { "objectname", SOURCE_OTHER, FIELD_STR, oid_atom_parser },
913 [ATOM_DELTABASE] = { "deltabase", SOURCE_OTHER, FIELD_STR, deltabase_atom_parser },
914 [ATOM_TREE] = { "tree", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
915 [ATOM_PARENT] = { "parent", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
916 [ATOM_NUMPARENT] = { "numparent", SOURCE_OBJ, FIELD_ULONG },
917 [ATOM_OBJECT] = { "object", SOURCE_OBJ },
918 [ATOM_TYPE] = { "type", SOURCE_OBJ },
919 [ATOM_TAG] = { "tag", SOURCE_OBJ },
920 [ATOM_AUTHOR] = { "author", SOURCE_OBJ },
921 [ATOM_AUTHORNAME] = { "authorname", SOURCE_OBJ, FIELD_STR, person_name_atom_parser },
922 [ATOM_AUTHOREMAIL] = { "authoremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
923 [ATOM_AUTHORDATE] = { "authordate", SOURCE_OBJ, FIELD_TIME },
924 [ATOM_COMMITTER] = { "committer", SOURCE_OBJ },
925 [ATOM_COMMITTERNAME] = { "committername", SOURCE_OBJ, FIELD_STR, person_name_atom_parser },
926 [ATOM_COMMITTEREMAIL] = { "committeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
927 [ATOM_COMMITTERDATE] = { "committerdate", SOURCE_OBJ, FIELD_TIME },
928 [ATOM_TAGGER] = { "tagger", SOURCE_OBJ },
929 [ATOM_TAGGERNAME] = { "taggername", SOURCE_OBJ, FIELD_STR, person_name_atom_parser },
930 [ATOM_TAGGEREMAIL] = { "taggeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
931 [ATOM_TAGGERDATE] = { "taggerdate", SOURCE_OBJ, FIELD_TIME },
932 [ATOM_CREATOR] = { "creator", SOURCE_OBJ },
933 [ATOM_CREATORDATE] = { "creatordate", SOURCE_OBJ, FIELD_TIME },
934 [ATOM_DESCRIBE] = { "describe", SOURCE_OBJ, FIELD_STR, describe_atom_parser },
935 [ATOM_SUBJECT] = { "subject", SOURCE_OBJ, FIELD_STR, subject_atom_parser },
936 [ATOM_BODY] = { "body", SOURCE_OBJ, FIELD_STR, body_atom_parser },
937 [ATOM_TRAILERS] = { "trailers", SOURCE_OBJ, FIELD_STR, trailers_atom_parser },
938 [ATOM_CONTENTS] = { "contents", SOURCE_OBJ, FIELD_STR, contents_atom_parser },
939 [ATOM_SIGNATURE] = { "signature", SOURCE_OBJ, FIELD_STR, signature_atom_parser },
940 [ATOM_RAW] = { "raw", SOURCE_OBJ, FIELD_STR, raw_atom_parser },
941 [ATOM_UPSTREAM] = { "upstream", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
942 [ATOM_PUSH] = { "push", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
943 [ATOM_SYMREF] = { "symref", SOURCE_NONE, FIELD_STR, refname_atom_parser },
944 [ATOM_FLAG] = { "flag", SOURCE_NONE },
945 [ATOM_HEAD] = { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser },
946 [ATOM_COLOR] = { "color", SOURCE_NONE, FIELD_STR, color_atom_parser },
947 [ATOM_WORKTREEPATH] = { "worktreepath", SOURCE_NONE },
948 [ATOM_ALIGN] = { "align", SOURCE_NONE, FIELD_STR, align_atom_parser },
949 [ATOM_END] = { "end", SOURCE_NONE },
950 [ATOM_IF] = { "if", SOURCE_NONE, FIELD_STR, if_atom_parser },
951 [ATOM_THEN] = { "then", SOURCE_NONE },
952 [ATOM_ELSE] = { "else", SOURCE_NONE },
953 [ATOM_REST] = { "rest", SOURCE_NONE, FIELD_STR, rest_atom_parser },
954 [ATOM_AHEADBEHIND] = { "ahead-behind", SOURCE_OTHER, FIELD_STR, ahead_behind_atom_parser },
955 /*
956 * Please update $__git_ref_fieldlist in git-completion.bash
957 * when you add new atoms
958 */
959 };
960
961 #define REF_FORMATTING_STATE_INIT { 0 }
962
963 struct ref_formatting_stack {
964 struct ref_formatting_stack *prev;
965 struct strbuf output;
966 void (*at_end)(struct ref_formatting_stack **stack);
967 void *at_end_data;
968 };
969
970 struct ref_formatting_state {
971 int quote_style;
972 struct ref_formatting_stack *stack;
973 };
974
975 struct atom_value {
976 const char *s;
977 ssize_t s_size;
978 int (*handler)(struct atom_value *atomv, struct ref_formatting_state *state,
979 struct strbuf *err);
980 uintmax_t value; /* used for sorting when not FIELD_STR */
981 struct used_atom *atom;
982 };
983
984 #define ATOM_SIZE_UNSPECIFIED (-1)
985
986 #define ATOM_VALUE_INIT { \
987 .s_size = ATOM_SIZE_UNSPECIFIED \
988 }
989
990 /*
991 * Used to parse format string and sort specifiers
992 */
993 static int parse_ref_filter_atom(struct ref_format *format,
994 const char *atom, const char *ep,
995 struct strbuf *err)
996 {
997 const char *sp;
998 const char *arg;
999 int i, at, atom_len;
1000
1001 sp = atom;
1002 if (*sp == '*' && sp < ep)
1003 sp++; /* deref */
1004 if (ep <= sp)
1005 return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"),
1006 (int)(ep-atom), atom);
1007
1008 /*
1009 * If the atom name has a colon, strip it and everything after
1010 * it off - it specifies the format for this entry, and
1011 * shouldn't be used for checking against the valid_atom
1012 * table.
1013 */
1014 arg = memchr(sp, ':', ep - sp);
1015 atom_len = (arg ? arg : ep) - sp;
1016
1017 /* Do we have the atom already used elsewhere? */
1018 for (i = 0; i < used_atom_cnt; i++) {
1019 int len = strlen(used_atom[i].name);
1020 if (len == ep - atom && !memcmp(used_atom[i].name, atom, len))
1021 return i;
1022 }
1023
1024 /* Is the atom a valid one? */
1025 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
1026 int len = strlen(valid_atom[i].name);
1027 if (len == atom_len && !memcmp(valid_atom[i].name, sp, len))
1028 break;
1029 }
1030
1031 if (ARRAY_SIZE(valid_atom) <= i)
1032 return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"),
1033 (int)(ep-atom), atom);
1034 if (valid_atom[i].source != SOURCE_NONE && !have_git_dir())
1035 return strbuf_addf_ret(err, -1,
1036 _("not a git repository, but the field '%.*s' requires access to object data"),
1037 (int)(ep-atom), atom);
1038
1039 /* Add it in, including the deref prefix */
1040 at = used_atom_cnt;
1041 used_atom_cnt++;
1042 REALLOC_ARRAY(used_atom, used_atom_cnt);
1043 used_atom[at].atom_type = i;
1044 used_atom[at].name = xmemdupz(atom, ep - atom);
1045 used_atom[at].type = valid_atom[i].cmp_type;
1046 used_atom[at].source = valid_atom[i].source;
1047 if (used_atom[at].source == SOURCE_OBJ) {
1048 if (*atom == '*')
1049 oi_deref.info.contentp = &oi_deref.content;
1050 else
1051 oi.info.contentp = &oi.content;
1052 }
1053 if (arg) {
1054 arg = used_atom[at].name + (arg - atom) + 1;
1055 if (!*arg) {
1056 /*
1057 * Treat empty sub-arguments list as NULL (i.e.,
1058 * "%(atom:)" is equivalent to "%(atom)").
1059 */
1060 arg = NULL;
1061 }
1062 }
1063 memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
1064 if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err))
1065 return -1;
1066 if (*atom == '*')
1067 need_tagged = 1;
1068 if (i == ATOM_SYMREF)
1069 need_symref = 1;
1070 return at;
1071 }
1072
1073 static void quote_formatting(struct strbuf *s, const char *str, ssize_t len, int quote_style)
1074 {
1075 switch (quote_style) {
1076 case QUOTE_NONE:
1077 if (len < 0)
1078 strbuf_addstr(s, str);
1079 else
1080 strbuf_add(s, str, len);
1081 break;
1082 case QUOTE_SHELL:
1083 sq_quote_buf(s, str);
1084 break;
1085 case QUOTE_PERL:
1086 if (len < 0)
1087 perl_quote_buf(s, str);
1088 else
1089 perl_quote_buf_with_len(s, str, len);
1090 break;
1091 case QUOTE_PYTHON:
1092 python_quote_buf(s, str);
1093 break;
1094 case QUOTE_TCL:
1095 tcl_quote_buf(s, str);
1096 break;
1097 }
1098 }
1099
1100 static int append_atom(struct atom_value *v, struct ref_formatting_state *state,
1101 struct strbuf *err UNUSED)
1102 {
1103 /*
1104 * Quote formatting is only done when the stack has a single
1105 * element. Otherwise quote formatting is done on the
1106 * element's entire output strbuf when the %(end) atom is
1107 * encountered.
1108 */
1109 if (!state->stack->prev)
1110 quote_formatting(&state->stack->output, v->s, v->s_size, state->quote_style);
1111 else if (v->s_size < 0)
1112 strbuf_addstr(&state->stack->output, v->s);
1113 else
1114 strbuf_add(&state->stack->output, v->s, v->s_size);
1115 return 0;
1116 }
1117
1118 static void push_stack_element(struct ref_formatting_stack **stack)
1119 {
1120 struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
1121
1122 strbuf_init(&s->output, 0);
1123 s->prev = *stack;
1124 *stack = s;
1125 }
1126
1127 static void pop_stack_element(struct ref_formatting_stack **stack)
1128 {
1129 struct ref_formatting_stack *current = *stack;
1130 struct ref_formatting_stack *prev = current->prev;
1131
1132 if (prev)
1133 strbuf_addbuf(&prev->output, &current->output);
1134 strbuf_release(&current->output);
1135 free(current);
1136 *stack = prev;
1137 }
1138
1139 static void end_align_handler(struct ref_formatting_stack **stack)
1140 {
1141 struct ref_formatting_stack *cur = *stack;
1142 struct align *align = (struct align *)cur->at_end_data;
1143 struct strbuf s = STRBUF_INIT;
1144
1145 strbuf_utf8_align(&s, align->position, align->width, cur->output.buf);
1146 strbuf_swap(&cur->output, &s);
1147 strbuf_release(&s);
1148 }
1149
1150 static int align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
1151 struct strbuf *err UNUSED)
1152 {
1153 struct ref_formatting_stack *new_stack;
1154
1155 push_stack_element(&state->stack);
1156 new_stack = state->stack;
1157 new_stack->at_end = end_align_handler;
1158 new_stack->at_end_data = &atomv->atom->u.align;
1159 return 0;
1160 }
1161
1162 static void if_then_else_handler(struct ref_formatting_stack **stack)
1163 {
1164 struct ref_formatting_stack *cur = *stack;
1165 struct ref_formatting_stack *prev = cur->prev;
1166 struct if_then_else *if_then_else = (struct if_then_else *)cur->at_end_data;
1167
1168 if (!if_then_else->then_atom_seen)
1169 die(_("format: %%(%s) atom used without a %%(%s) atom"), "if", "then");
1170
1171 if (if_then_else->else_atom_seen) {
1172 /*
1173 * There is an %(else) atom: we need to drop one state from the
1174 * stack, either the %(else) branch if the condition is satisfied, or
1175 * the %(then) branch if it isn't.
1176 */
1177 if (if_then_else->condition_satisfied) {
1178 strbuf_reset(&cur->output);
1179 pop_stack_element(&cur);
1180 } else {
1181 strbuf_swap(&cur->output, &prev->output);
1182 strbuf_reset(&cur->output);
1183 pop_stack_element(&cur);
1184 }
1185 } else if (!if_then_else->condition_satisfied) {
1186 /*
1187 * No %(else) atom: just drop the %(then) branch if the
1188 * condition is not satisfied.
1189 */
1190 strbuf_reset(&cur->output);
1191 }
1192
1193 *stack = cur;
1194 free(if_then_else);
1195 }
1196
1197 static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
1198 struct strbuf *err UNUSED)
1199 {
1200 struct ref_formatting_stack *new_stack;
1201 struct if_then_else *if_then_else = xcalloc(1,
1202 sizeof(struct if_then_else));
1203
1204 if_then_else->str = atomv->atom->u.if_then_else.str;
1205 if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status;
1206
1207 push_stack_element(&state->stack);
1208 new_stack = state->stack;
1209 new_stack->at_end = if_then_else_handler;
1210 new_stack->at_end_data = if_then_else;
1211 return 0;
1212 }
1213
1214 static int is_empty(struct strbuf *buf)
1215 {
1216 const char *cur = buf->buf;
1217 const char *end = buf->buf + buf->len;
1218
1219 while (cur != end && (isspace(*cur)))
1220 cur++;
1221
1222 return cur == end;
1223 }
1224
1225 static int then_atom_handler(struct atom_value *atomv UNUSED,
1226 struct ref_formatting_state *state,
1227 struct strbuf *err)
1228 {
1229 struct ref_formatting_stack *cur = state->stack;
1230 struct if_then_else *if_then_else = NULL;
1231 size_t str_len = 0;
1232
1233 if (cur->at_end == if_then_else_handler)
1234 if_then_else = (struct if_then_else *)cur->at_end_data;
1235 if (!if_then_else)
1236 return strbuf_addf_ret(err, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "then", "if");
1237 if (if_then_else->then_atom_seen)
1238 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used more than once"));
1239 if (if_then_else->else_atom_seen)
1240 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used after %%(else)"));
1241 if_then_else->then_atom_seen = 1;
1242 if (if_then_else->str)
1243 str_len = strlen(if_then_else->str);
1244 /*
1245 * If the 'equals' or 'notequals' attribute is used then
1246 * perform the required comparison. If not, only non-empty
1247 * strings satisfy the 'if' condition.
1248 */
1249 if (if_then_else->cmp_status == COMPARE_EQUAL) {
1250 if (str_len == cur->output.len &&
1251 !memcmp(if_then_else->str, cur->output.buf, cur->output.len))
1252 if_then_else->condition_satisfied = 1;
1253 } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) {
1254 if (str_len != cur->output.len ||
1255 memcmp(if_then_else->str, cur->output.buf, cur->output.len))
1256 if_then_else->condition_satisfied = 1;
1257 } else if (cur->output.len && !is_empty(&cur->output))
1258 if_then_else->condition_satisfied = 1;
1259 strbuf_reset(&cur->output);
1260 return 0;
1261 }
1262
1263 static int else_atom_handler(struct atom_value *atomv UNUSED,
1264 struct ref_formatting_state *state,
1265 struct strbuf *err)
1266 {
1267 struct ref_formatting_stack *prev = state->stack;
1268 struct if_then_else *if_then_else = NULL;
1269
1270 if (prev->at_end == if_then_else_handler)
1271 if_then_else = (struct if_then_else *)prev->at_end_data;
1272 if (!if_then_else)
1273 return strbuf_addf_ret(err, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "if");
1274 if (!if_then_else->then_atom_seen)
1275 return strbuf_addf_ret(err, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "then");
1276 if (if_then_else->else_atom_seen)
1277 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used more than once"));
1278 if_then_else->else_atom_seen = 1;
1279 push_stack_element(&state->stack);
1280 state->stack->at_end_data = prev->at_end_data;
1281 state->stack->at_end = prev->at_end;
1282 return 0;
1283 }
1284
1285 static int end_atom_handler(struct atom_value *atomv UNUSED,
1286 struct ref_formatting_state *state,
1287 struct strbuf *err)
1288 {
1289 struct ref_formatting_stack *current = state->stack;
1290 struct strbuf s = STRBUF_INIT;
1291
1292 if (!current->at_end)
1293 return strbuf_addf_ret(err, -1, _("format: %%(end) atom used without corresponding atom"));
1294 current->at_end(&state->stack);
1295
1296 /* Stack may have been popped within at_end(), hence reset the current pointer */
1297 current = state->stack;
1298
1299 /*
1300 * Perform quote formatting when the stack element is that of
1301 * a supporting atom. If nested then perform quote formatting
1302 * only on the topmost supporting atom.
1303 */
1304 if (!current->prev->prev) {
1305 quote_formatting(&s, current->output.buf, current->output.len, state->quote_style);
1306 strbuf_swap(&current->output, &s);
1307 }
1308 strbuf_release(&s);
1309 pop_stack_element(&state->stack);
1310 return 0;
1311 }
1312
1313 /*
1314 * In a format string, find the next occurrence of %(atom).
1315 */
1316 static const char *find_next(const char *cp)
1317 {
1318 while (*cp) {
1319 if (*cp == '%') {
1320 /*
1321 * %( is the start of an atom;
1322 * %% is a quoted per-cent.
1323 */
1324 if (cp[1] == '(')
1325 return cp;
1326 else if (cp[1] == '%')
1327 cp++; /* skip over two % */
1328 /* otherwise this is a singleton, literal % */
1329 }
1330 cp++;
1331 }
1332 return NULL;
1333 }
1334
1335 static int reject_atom(enum atom_type atom_type)
1336 {
1337 return atom_type == ATOM_REST;
1338 }
1339
1340 /*
1341 * Make sure the format string is well formed, and parse out
1342 * the used atoms.
1343 */
1344 int verify_ref_format(struct ref_format *format)
1345 {
1346 const char *cp, *sp;
1347
1348 format->need_color_reset_at_eol = 0;
1349 for (cp = format->format; *cp && (sp = find_next(cp)); ) {
1350 struct strbuf err = STRBUF_INIT;
1351 const char *color, *ep = strchr(sp, ')');
1352 int at;
1353
1354 if (!ep)
1355 return error(_("malformed format string %s"), sp);
1356 /* sp points at "%(" and ep points at the closing ")" */
1357 at = parse_ref_filter_atom(format, sp + 2, ep, &err);
1358 if (at < 0)
1359 die("%s", err.buf);
1360 if (reject_atom(used_atom[at].atom_type))
1361 die(_("this command reject atom %%(%.*s)"), (int)(ep - sp - 2), sp + 2);
1362
1363 if ((format->quote_style == QUOTE_PYTHON ||
1364 format->quote_style == QUOTE_SHELL ||
1365 format->quote_style == QUOTE_TCL) &&
1366 used_atom[at].atom_type == ATOM_RAW &&
1367 used_atom[at].u.raw_data.option == RAW_BARE)
1368 die(_("--format=%.*s cannot be used with "
1369 "--python, --shell, --tcl"), (int)(ep - sp - 2), sp + 2);
1370 cp = ep + 1;
1371
1372 if (skip_prefix(used_atom[at].name, "color:", &color))
1373 format->need_color_reset_at_eol = !!strcmp(color, "reset");
1374 strbuf_release(&err);
1375 }
1376 if (format->need_color_reset_at_eol && !want_color(format->use_color))
1377 format->need_color_reset_at_eol = 0;
1378 return 0;
1379 }
1380
1381 static const char *do_grab_oid(const char *field, const struct object_id *oid,
1382 struct used_atom *atom)
1383 {
1384 switch (atom->u.oid.option) {
1385 case O_FULL:
1386 return oid_to_hex(oid);
1387 case O_LENGTH:
1388 return repo_find_unique_abbrev(the_repository, oid,
1389 atom->u.oid.length);
1390 case O_SHORT:
1391 return repo_find_unique_abbrev(the_repository, oid,
1392 DEFAULT_ABBREV);
1393 default:
1394 BUG("unknown %%(%s) option", field);
1395 }
1396 }
1397
1398 static int grab_oid(const char *name, const char *field, const struct object_id *oid,
1399 struct atom_value *v, struct used_atom *atom)
1400 {
1401 if (starts_with(name, field)) {
1402 v->s = xstrdup(do_grab_oid(field, oid, atom));
1403 return 1;
1404 }
1405 return 0;
1406 }
1407
1408 /* See grab_values */
1409 static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi)
1410 {
1411 int i;
1412
1413 for (i = 0; i < used_atom_cnt; i++) {
1414 const char *name = used_atom[i].name;
1415 enum atom_type atom_type = used_atom[i].atom_type;
1416 struct atom_value *v = &val[i];
1417 if (!!deref != (*name == '*'))
1418 continue;
1419 if (deref)
1420 name++;
1421 if (atom_type == ATOM_OBJECTTYPE)
1422 v->s = xstrdup(type_name(oi->type));
1423 else if (atom_type == ATOM_OBJECTSIZE) {
1424 if (used_atom[i].u.objectsize.option == O_SIZE_DISK) {
1425 v->value = oi->disk_size;
1426 v->s = xstrfmt("%"PRIuMAX, (uintmax_t)oi->disk_size);
1427 } else if (used_atom[i].u.objectsize.option == O_SIZE) {
1428 v->value = oi->size;
1429 v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size);
1430 }
1431 } else if (atom_type == ATOM_DELTABASE)
1432 v->s = xstrdup(oid_to_hex(&oi->delta_base_oid));
1433 else if (atom_type == ATOM_OBJECTNAME && deref)
1434 grab_oid(name, "objectname", &oi->oid, v, &used_atom[i]);
1435 }
1436 }
1437
1438 /* See grab_values */
1439 static void grab_tag_values(struct atom_value *val, int deref, struct object *obj)
1440 {
1441 int i;
1442 struct tag *tag = (struct tag *) obj;
1443
1444 for (i = 0; i < used_atom_cnt; i++) {
1445 const char *name = used_atom[i].name;
1446 enum atom_type atom_type = used_atom[i].atom_type;
1447 struct atom_value *v = &val[i];
1448 if (!!deref != (*name == '*'))
1449 continue;
1450 if (deref)
1451 name++;
1452 if (atom_type == ATOM_TAG)
1453 v->s = xstrdup(tag->tag);
1454 else if (atom_type == ATOM_TYPE && tag->tagged)
1455 v->s = xstrdup(type_name(tag->tagged->type));
1456 else if (atom_type == ATOM_OBJECT && tag->tagged)
1457 v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
1458 }
1459 }
1460
1461 /* See grab_values */
1462 static void grab_commit_values(struct atom_value *val, int deref, struct object *obj)
1463 {
1464 int i;
1465 struct commit *commit = (struct commit *) obj;
1466
1467 for (i = 0; i < used_atom_cnt; i++) {
1468 const char *name = used_atom[i].name;
1469 enum atom_type atom_type = used_atom[i].atom_type;
1470 struct atom_value *v = &val[i];
1471 if (!!deref != (*name == '*'))
1472 continue;
1473 if (deref)
1474 name++;
1475 if (atom_type == ATOM_TREE &&
1476 grab_oid(name, "tree", get_commit_tree_oid(commit), v, &used_atom[i]))
1477 continue;
1478 if (atom_type == ATOM_NUMPARENT) {
1479 v->value = commit_list_count(commit->parents);
1480 v->s = xstrfmt("%lu", (unsigned long)v->value);
1481 }
1482 else if (atom_type == ATOM_PARENT) {
1483 struct commit_list *parents;
1484 struct strbuf s = STRBUF_INIT;
1485 for (parents = commit->parents; parents; parents = parents->next) {
1486 struct object_id *oid = &parents->item->object.oid;
1487 if (parents != commit->parents)
1488 strbuf_addch(&s, ' ');
1489 strbuf_addstr(&s, do_grab_oid("parent", oid, &used_atom[i]));
1490 }
1491 v->s = strbuf_detach(&s, NULL);
1492 }
1493 }
1494 }
1495
1496 static const char *find_wholine(const char *who, int wholen, const char *buf)
1497 {
1498 const char *eol;
1499 while (*buf) {
1500 if (!strncmp(buf, who, wholen) &&
1501 buf[wholen] == ' ')
1502 return buf + wholen + 1;
1503 eol = strchr(buf, '\n');
1504 if (!eol)
1505 return "";
1506 eol++;
1507 if (*eol == '\n')
1508 return ""; /* end of header */
1509 buf = eol;
1510 }
1511 return "";
1512 }
1513
1514 static const char *copy_line(const char *buf)
1515 {
1516 const char *eol = strchrnul(buf, '\n');
1517 return xmemdupz(buf, eol - buf);
1518 }
1519
1520 static const char *copy_name(const char *buf)
1521 {
1522 const char *cp;
1523 for (cp = buf; *cp && *cp != '\n'; cp++) {
1524 if (starts_with(cp, " <"))
1525 return xmemdupz(buf, cp - buf);
1526 }
1527 return xstrdup("");
1528 }
1529
1530 static const char *find_end_of_email(const char *email, int opt)
1531 {
1532 const char *eoemail;
1533
1534 if (opt & EO_LOCALPART) {
1535 eoemail = strchr(email, '@');
1536 if (eoemail)
1537 return eoemail;
1538 return strchr(email, '>');
1539 }
1540
1541 if (opt & EO_TRIM)
1542 return strchr(email, '>');
1543
1544 /*
1545 * The option here is either the raw email option or the raw
1546 * mailmap option (that is EO_RAW or EO_MAILMAP). In such cases,
1547 * we directly grab the whole email including the closing
1548 * angle brackets.
1549 *
1550 * If EO_MAILMAP was set with any other option (that is either
1551 * EO_TRIM or EO_LOCALPART), we already grab the end of email
1552 * above.
1553 */
1554 eoemail = strchr(email, '>');
1555 if (eoemail)
1556 eoemail++;
1557 return eoemail;
1558 }
1559
1560 static const char *copy_email(const char *buf, struct used_atom *atom)
1561 {
1562 const char *email = strchr(buf, '<');
1563 const char *eoemail;
1564 int opt = atom->u.email_option.option;
1565
1566 if (!email)
1567 return xstrdup("");
1568
1569 if (opt & (EO_LOCALPART | EO_TRIM))
1570 email++;
1571
1572 eoemail = find_end_of_email(email, opt);
1573 if (!eoemail)
1574 return xstrdup("");
1575 return xmemdupz(email, eoemail - email);
1576 }
1577
1578 static char *copy_subject(const char *buf, unsigned long len)
1579 {
1580 struct strbuf sb = STRBUF_INIT;
1581 int i;
1582
1583 for (i = 0; i < len; i++) {
1584 if (buf[i] == '\r' && i + 1 < len && buf[i + 1] == '\n')
1585 continue; /* ignore CR in CRLF */
1586
1587 if (buf[i] == '\n')
1588 strbuf_addch(&sb, ' ');
1589 else
1590 strbuf_addch(&sb, buf[i]);
1591 }
1592 return strbuf_detach(&sb, NULL);
1593 }
1594
1595 static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
1596 {
1597 const char *eoemail = strstr(buf, "> ");
1598 char *zone;
1599 timestamp_t timestamp;
1600 long tz;
1601 struct date_mode date_mode = DATE_MODE_INIT;
1602 const char *formatp;
1603
1604 /*
1605 * We got here because atomname ends in "date" or "date<something>";
1606 * it's not possible that <something> is not ":<format>" because
1607 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1608 * ":" means no format is specified, and use the default.
1609 */
1610 formatp = strchr(atomname, ':');
1611 if (formatp) {
1612 formatp++;
1613 parse_date_format(formatp, &date_mode);
1614 }
1615
1616 if (!eoemail)
1617 goto bad;
1618 timestamp = parse_timestamp(eoemail + 2, &zone, 10);
1619 if (timestamp == TIME_MAX)
1620 goto bad;
1621 tz = strtol(zone, NULL, 10);
1622 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
1623 goto bad;
1624 v->s = xstrdup(show_date(timestamp, tz, &date_mode));
1625 v->value = timestamp;
1626 date_mode_release(&date_mode);
1627 return;
1628 bad:
1629 v->s = xstrdup("");
1630 v->value = 0;
1631 }
1632
1633 static struct string_list mailmap = STRING_LIST_INIT_NODUP;
1634
1635 /* See grab_values */
1636 static void grab_person(const char *who, struct atom_value *val, int deref, void *buf)
1637 {
1638 int i;
1639 int wholen = strlen(who);
1640 const char *wholine = NULL;
1641 const char *headers[] = { "author ", "committer ",
1642 "tagger ", NULL };
1643
1644 for (i = 0; i < used_atom_cnt; i++) {
1645 struct used_atom *atom = &used_atom[i];
1646 const char *name = atom->name;
1647 struct atom_value *v = &val[i];
1648 struct strbuf mailmap_buf = STRBUF_INIT;
1649
1650 if (!!deref != (*name == '*'))
1651 continue;
1652 if (deref)
1653 name++;
1654 if (strncmp(who, name, wholen))
1655 continue;
1656 if (name[wholen] != 0 &&
1657 !starts_with(name + wholen, "name") &&
1658 !starts_with(name + wholen, "email") &&
1659 !starts_with(name + wholen, "date"))
1660 continue;
1661
1662 if ((starts_with(name + wholen, "name") &&
1663 (atom->u.name_option.option == N_MAILMAP)) ||
1664 (starts_with(name + wholen, "email") &&
1665 (atom->u.email_option.option & EO_MAILMAP))) {
1666 if (!mailmap.items)
1667 read_mailmap(&mailmap);
1668 strbuf_addstr(&mailmap_buf, buf);
1669 apply_mailmap_to_header(&mailmap_buf, headers, &mailmap);
1670 wholine = find_wholine(who, wholen, mailmap_buf.buf);
1671 } else {
1672 wholine = find_wholine(who, wholen, buf);
1673 }
1674
1675 if (!wholine)
1676 return; /* no point looking for it */
1677 if (name[wholen] == 0)
1678 v->s = copy_line(wholine);
1679 else if (starts_with(name + wholen, "name"))
1680 v->s = copy_name(wholine);
1681 else if (starts_with(name + wholen, "email"))
1682 v->s = copy_email(wholine, &used_atom[i]);
1683 else if (starts_with(name + wholen, "date"))
1684 grab_date(wholine, v, name);
1685
1686 strbuf_release(&mailmap_buf);
1687 }
1688
1689 /*
1690 * For a tag or a commit object, if "creator" or "creatordate" is
1691 * requested, do something special.
1692 */
1693 if (strcmp(who, "tagger") && strcmp(who, "committer"))
1694 return; /* "author" for commit object is not wanted */
1695 if (!wholine)
1696 wholine = find_wholine(who, wholen, buf);
1697 if (!wholine)
1698 return;
1699 for (i = 0; i < used_atom_cnt; i++) {
1700 const char *name = used_atom[i].name;
1701 enum atom_type atom_type = used_atom[i].atom_type;
1702 struct atom_value *v = &val[i];
1703 if (!!deref != (*name == '*'))
1704 continue;
1705 if (deref)
1706 name++;
1707
1708 if (atom_type == ATOM_CREATORDATE)
1709 grab_date(wholine, v, name);
1710 else if (atom_type == ATOM_CREATOR)
1711 v->s = copy_line(wholine);
1712 }
1713 }
1714
1715 static void grab_signature(struct atom_value *val, int deref, struct object *obj)
1716 {
1717 int i;
1718 struct commit *commit = (struct commit *) obj;
1719 struct signature_check sigc = { 0 };
1720 int signature_checked = 0;
1721
1722 for (i = 0; i < used_atom_cnt; i++) {
1723 struct used_atom *atom = &used_atom[i];
1724 const char *name = atom->name;
1725 struct atom_value *v = &val[i];
1726 int opt;
1727
1728 if (!!deref != (*name == '*'))
1729 continue;
1730 if (deref)
1731 name++;
1732
1733 if (!skip_prefix(name, "signature", &name) ||
1734 (*name && *name != ':'))
1735 continue;
1736 if (!*name)
1737 name = NULL;
1738 else
1739 name++;
1740
1741 opt = parse_signature_option(name);
1742 if (opt < 0)
1743 continue;
1744
1745 if (!signature_checked) {
1746 check_commit_signature(commit, &sigc);
1747 signature_checked = 1;
1748 }
1749
1750 switch (opt) {
1751 case S_BARE:
1752 v->s = xstrdup(sigc.output ? sigc.output: "");
1753 break;
1754 case S_SIGNER:
1755 v->s = xstrdup(sigc.signer ? sigc.signer : "");
1756 break;
1757 case S_GRADE:
1758 switch (sigc.result) {
1759 case 'G':
1760 switch (sigc.trust_level) {
1761 case TRUST_UNDEFINED:
1762 case TRUST_NEVER:
1763 v->s = xstrfmt("%c", (char)'U');
1764 break;
1765 default:
1766 v->s = xstrfmt("%c", (char)'G');
1767 break;
1768 }
1769 break;
1770 case 'B':
1771 case 'E':
1772 case 'N':
1773 case 'X':
1774 case 'Y':
1775 case 'R':
1776 v->s = xstrfmt("%c", (char)sigc.result);
1777 break;
1778 }
1779 break;
1780 case S_KEY:
1781 v->s = xstrdup(sigc.key ? sigc.key : "");
1782 break;
1783 case S_FINGERPRINT:
1784 v->s = xstrdup(sigc.fingerprint ?
1785 sigc.fingerprint : "");
1786 break;
1787 case S_PRI_KEY_FP:
1788 v->s = xstrdup(sigc.primary_key_fingerprint ?
1789 sigc.primary_key_fingerprint : "");
1790 break;
1791 case S_TRUST_LEVEL:
1792 v->s = xstrdup(gpg_trust_level_to_str(sigc.trust_level));
1793 break;
1794 }
1795 }
1796
1797 if (signature_checked)
1798 signature_check_clear(&sigc);
1799 }
1800
1801 static void find_subpos(const char *buf,
1802 const char **sub, size_t *sublen,
1803 const char **body, size_t *bodylen,
1804 size_t *nonsiglen,
1805 const char **sig, size_t *siglen)
1806 {
1807 struct strbuf payload = STRBUF_INIT;
1808 struct strbuf signature = STRBUF_INIT;
1809 const char *eol;
1810 const char *end = buf + strlen(buf);
1811 const char *sigstart;
1812
1813 /* parse signature first; we might not even have a subject line */
1814 parse_signature(buf, end - buf, &payload, &signature);
1815 strbuf_release(&payload);
1816
1817 /* skip past header until we hit empty line */
1818 while (*buf && *buf != '\n') {
1819 eol = strchrnul(buf, '\n');
1820 if (*eol)
1821 eol++;
1822 buf = eol;
1823 }
1824 /* skip any empty lines */
1825 while (*buf == '\n')
1826 buf++;
1827 *sig = strbuf_detach(&signature, siglen);
1828 sigstart = buf + parse_signed_buffer(buf, strlen(buf));
1829
1830 /* subject is first non-empty line */
1831 *sub = buf;
1832 /* subject goes to first empty line before signature begins */
1833 if ((eol = strstr(*sub, "\n\n")) ||
1834 (eol = strstr(*sub, "\r\n\r\n"))) {
1835 eol = eol < sigstart ? eol : sigstart;
1836 } else {
1837 /* treat whole message as subject */
1838 eol = sigstart;
1839 }
1840 buf = eol;
1841 *sublen = buf - *sub;
1842 /* drop trailing newline, if present */
1843 while (*sublen && ((*sub)[*sublen - 1] == '\n' ||
1844 (*sub)[*sublen - 1] == '\r'))
1845 *sublen -= 1;
1846
1847 /* skip any empty lines */
1848 while (*buf == '\n' || *buf == '\r')
1849 buf++;
1850 *body = buf;
1851 *bodylen = strlen(buf);
1852 *nonsiglen = sigstart - buf;
1853 }
1854
1855 /*
1856 * If 'lines' is greater than 0, append that many lines from the given
1857 * 'buf' of length 'size' to the given strbuf.
1858 */
1859 static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
1860 {
1861 int i;
1862 const char *sp, *eol;
1863 size_t len;
1864
1865 sp = buf;
1866
1867 for (i = 0; i < lines && sp < buf + size; i++) {
1868 if (i)
1869 strbuf_addstr(out, "\n ");
1870 eol = memchr(sp, '\n', size - (sp - buf));
1871 len = eol ? eol - sp : size - (sp - buf);
1872 strbuf_add(out, sp, len);
1873 if (!eol)
1874 break;
1875 sp = eol + 1;
1876 }
1877 }
1878
1879 static void grab_describe_values(struct atom_value *val, int deref,
1880 struct object *obj)
1881 {
1882 struct commit *commit = (struct commit *)obj;
1883 int i;
1884
1885 for (i = 0; i < used_atom_cnt; i++) {
1886 struct used_atom *atom = &used_atom[i];
1887 enum atom_type type = atom->atom_type;
1888 const char *name = atom->name;
1889 struct atom_value *v = &val[i];
1890
1891 struct child_process cmd = CHILD_PROCESS_INIT;
1892 struct strbuf out = STRBUF_INIT;
1893 struct strbuf err = STRBUF_INIT;
1894
1895 if (type != ATOM_DESCRIBE)
1896 continue;
1897
1898 if (!!deref != (*name == '*'))
1899 continue;
1900
1901 cmd.git_cmd = 1;
1902 strvec_push(&cmd.args, "describe");
1903 strvec_pushv(&cmd.args, atom->u.describe_args);
1904 strvec_push(&cmd.args, oid_to_hex(&commit->object.oid));
1905 if (pipe_command(&cmd, NULL, 0, &out, 0, &err, 0) < 0) {
1906 error(_("failed to run 'describe'"));
1907 v->s = xstrdup("");
1908 continue;
1909 }
1910 strbuf_rtrim(&out);
1911 v->s = strbuf_detach(&out, NULL);
1912
1913 strbuf_release(&err);
1914 }
1915 }
1916
1917 /* See grab_values */
1918 static void grab_sub_body_contents(struct atom_value *val, int deref, struct expand_data *data)
1919 {
1920 int i;
1921 const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
1922 size_t sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
1923 void *buf = data->content;
1924
1925 for (i = 0; i < used_atom_cnt; i++) {
1926 struct used_atom *atom = &used_atom[i];
1927 const char *name = atom->name;
1928 struct atom_value *v = &val[i];
1929 enum atom_type atom_type = atom->atom_type;
1930
1931 if (!!deref != (*name == '*'))
1932 continue;
1933 if (deref)
1934 name++;
1935
1936 if (atom_type == ATOM_RAW) {
1937 unsigned long buf_size = data->size;
1938
1939 if (atom->u.raw_data.option == RAW_BARE) {
1940 v->s = xmemdupz(buf, buf_size);
1941 v->s_size = buf_size;
1942 } else if (atom->u.raw_data.option == RAW_LENGTH) {
1943 v->value = buf_size;
1944 v->s = xstrfmt("%"PRIuMAX, v->value);
1945 }
1946 continue;
1947 }
1948
1949 if ((data->type != OBJ_TAG &&
1950 data->type != OBJ_COMMIT) ||
1951 (strcmp(name, "body") &&
1952 !starts_with(name, "subject") &&
1953 !starts_with(name, "trailers") &&
1954 !starts_with(name, "contents")))
1955 continue;
1956 if (!subpos)
1957 find_subpos(buf,
1958 &subpos, &sublen,
1959 &bodypos, &bodylen, &nonsiglen,
1960 &sigpos, &siglen);
1961
1962 if (atom->u.contents.option == C_SUB)
1963 v->s = copy_subject(subpos, sublen);
1964 else if (atom->u.contents.option == C_SUB_SANITIZE) {
1965 struct strbuf sb = STRBUF_INIT;
1966 format_sanitized_subject(&sb, subpos, sublen);
1967 v->s = strbuf_detach(&sb, NULL);
1968 } else if (atom->u.contents.option == C_BODY_DEP)
1969 v->s = xmemdupz(bodypos, bodylen);
1970 else if (atom->u.contents.option == C_LENGTH) {
1971 v->value = strlen(subpos);
1972 v->s = xstrfmt("%"PRIuMAX, v->value);
1973 } else if (atom->u.contents.option == C_BODY)
1974 v->s = xmemdupz(bodypos, nonsiglen);
1975 else if (atom->u.contents.option == C_SIG)
1976 v->s = xmemdupz(sigpos, siglen);
1977 else if (atom->u.contents.option == C_LINES) {
1978 struct strbuf s = STRBUF_INIT;
1979 const char *contents_end = bodypos + nonsiglen;
1980
1981 /* Size is the length of the message after removing the signature */
1982 append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
1983 v->s = strbuf_detach(&s, NULL);
1984 } else if (atom->u.contents.option == C_TRAILERS) {
1985 struct strbuf s = STRBUF_INIT;
1986
1987 /* Format the trailer info according to the trailer_opts given */
1988 format_trailers_from_commit(&s, subpos, &atom->u.contents.trailer_opts);
1989
1990 v->s = strbuf_detach(&s, NULL);
1991 } else if (atom->u.contents.option == C_BARE)
1992 v->s = xstrdup(subpos);
1993
1994 }
1995 free((void *)sigpos);
1996 }
1997
1998 /*
1999 * We want to have empty print-string for field requests
2000 * that do not apply (e.g. "authordate" for a tag object)
2001 */
2002 static void fill_missing_values(struct atom_value *val)
2003 {
2004 int i;
2005 for (i = 0; i < used_atom_cnt; i++) {
2006 struct atom_value *v = &val[i];
2007 if (!v->s)
2008 v->s = xstrdup("");
2009 }
2010 }
2011
2012 /*
2013 * val is a list of atom_value to hold returned values. Extract
2014 * the values for atoms in used_atom array out of (obj, buf, sz).
2015 * when deref is false, (obj, buf, sz) is the object that is
2016 * pointed at by the ref itself; otherwise it is the object the
2017 * ref (which is a tag) refers to.
2018 */
2019 static void grab_values(struct atom_value *val, int deref, struct object *obj, struct expand_data *data)
2020 {
2021 void *buf = data->content;
2022
2023 switch (obj->type) {
2024 case OBJ_TAG:
2025 grab_tag_values(val, deref, obj);
2026 grab_sub_body_contents(val, deref, data);
2027 grab_person("tagger", val, deref, buf);
2028 grab_describe_values(val, deref, obj);
2029 break;
2030 case OBJ_COMMIT:
2031 grab_commit_values(val, deref, obj);
2032 grab_sub_body_contents(val, deref, data);
2033 grab_person("author", val, deref, buf);
2034 grab_person("committer", val, deref, buf);
2035 grab_signature(val, deref, obj);
2036 grab_describe_values(val, deref, obj);
2037 break;
2038 case OBJ_TREE:
2039 /* grab_tree_values(val, deref, obj, buf, sz); */
2040 grab_sub_body_contents(val, deref, data);
2041 break;
2042 case OBJ_BLOB:
2043 /* grab_blob_values(val, deref, obj, buf, sz); */
2044 grab_sub_body_contents(val, deref, data);
2045 break;
2046 default:
2047 die("Eh? Object of type %d?", obj->type);
2048 }
2049 }
2050
2051 static inline char *copy_advance(char *dst, const char *src)
2052 {
2053 while (*src)
2054 *dst++ = *src++;
2055 return dst;
2056 }
2057
2058 static const char *lstrip_ref_components(const char *refname, int len)
2059 {
2060 long remaining = len;
2061 const char *start = xstrdup(refname);
2062 const char *to_free = start;
2063
2064 if (len < 0) {
2065 int i;
2066 const char *p = refname;
2067
2068 /* Find total no of '/' separated path-components */
2069 for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
2070 ;
2071 /*
2072 * The number of components we need to strip is now
2073 * the total minus the components to be left (Plus one
2074 * because we count the number of '/', but the number
2075 * of components is one more than the no of '/').
2076 */
2077 remaining = i + len + 1;
2078 }
2079
2080 while (remaining > 0) {
2081 switch (*start++) {
2082 case '\0':
2083 free((char *)to_free);
2084 return xstrdup("");
2085 case '/':
2086 remaining--;
2087 break;
2088 }
2089 }
2090
2091 start = xstrdup(start);
2092 free((char *)to_free);
2093 return start;
2094 }
2095
2096 static const char *rstrip_ref_components(const char *refname, int len)
2097 {
2098 long remaining = len;
2099 const char *start = xstrdup(refname);
2100 const char *to_free = start;
2101
2102 if (len < 0) {
2103 int i;
2104 const char *p = refname;
2105
2106 /* Find total no of '/' separated path-components */
2107 for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
2108 ;
2109 /*
2110 * The number of components we need to strip is now
2111 * the total minus the components to be left (Plus one
2112 * because we count the number of '/', but the number
2113 * of components is one more than the no of '/').
2114 */
2115 remaining = i + len + 1;
2116 }
2117
2118 while (remaining-- > 0) {
2119 char *p = strrchr(start, '/');
2120 if (!p) {
2121 free((char *)to_free);
2122 return xstrdup("");
2123 } else
2124 p[0] = '\0';
2125 }
2126 return start;
2127 }
2128
2129 static const char *show_ref(struct refname_atom *atom, const char *refname)
2130 {
2131 if (atom->option == R_SHORT)
2132 return shorten_unambiguous_ref(refname, warn_ambiguous_refs);
2133 else if (atom->option == R_LSTRIP)
2134 return lstrip_ref_components(refname, atom->lstrip);
2135 else if (atom->option == R_RSTRIP)
2136 return rstrip_ref_components(refname, atom->rstrip);
2137 else
2138 return xstrdup(refname);
2139 }
2140
2141 static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
2142 struct branch *branch, const char **s)
2143 {
2144 int num_ours, num_theirs;
2145 if (atom->u.remote_ref.option == RR_REF)
2146 *s = show_ref(&atom->u.remote_ref.refname, refname);
2147 else if (atom->u.remote_ref.option == RR_TRACK) {
2148 if (stat_tracking_info(branch, &num_ours, &num_theirs,
2149 NULL, atom->u.remote_ref.push,
2150 AHEAD_BEHIND_FULL) < 0) {
2151 *s = xstrdup(msgs.gone);
2152 } else if (!num_ours && !num_theirs)
2153 *s = xstrdup("");
2154 else if (!num_ours)
2155 *s = xstrfmt(msgs.behind, num_theirs);
2156 else if (!num_theirs)
2157 *s = xstrfmt(msgs.ahead, num_ours);
2158 else
2159 *s = xstrfmt(msgs.ahead_behind,
2160 num_ours, num_theirs);
2161 if (!atom->u.remote_ref.nobracket && *s[0]) {
2162 const char *to_free = *s;
2163 *s = xstrfmt("[%s]", *s);
2164 free((void *)to_free);
2165 }
2166 } else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
2167 if (stat_tracking_info(branch, &num_ours, &num_theirs,
2168 NULL, atom->u.remote_ref.push,
2169 AHEAD_BEHIND_FULL) < 0) {
2170 *s = xstrdup("");
2171 return;
2172 }
2173 if (!num_ours && !num_theirs)
2174 *s = xstrdup("=");
2175 else if (!num_ours)
2176 *s = xstrdup("<");
2177 else if (!num_theirs)
2178 *s = xstrdup(">");
2179 else
2180 *s = xstrdup("<>");
2181 } else if (atom->u.remote_ref.option == RR_REMOTE_NAME) {
2182 int explicit;
2183 const char *remote = atom->u.remote_ref.push ?
2184 pushremote_for_branch(branch, &explicit) :
2185 remote_for_branch(branch, &explicit);
2186 *s = xstrdup(explicit ? remote : "");
2187 } else if (atom->u.remote_ref.option == RR_REMOTE_REF) {
2188 const char *merge;
2189
2190 merge = remote_ref_for_branch(branch, atom->u.remote_ref.push);
2191 *s = xstrdup(merge ? merge : "");
2192 } else
2193 BUG("unhandled RR_* enum");
2194 }
2195
2196 char *get_head_description(void)
2197 {
2198 struct strbuf desc = STRBUF_INIT;
2199 struct wt_status_state state;
2200 memset(&state, 0, sizeof(state));
2201 wt_status_get_state(the_repository, &state, 1);
2202 if (state.rebase_in_progress ||
2203 state.rebase_interactive_in_progress) {
2204 if (state.branch)
2205 strbuf_addf(&desc, _("(no branch, rebasing %s)"),
2206 state.branch);
2207 else
2208 strbuf_addf(&desc, _("(no branch, rebasing detached HEAD %s)"),
2209 state.detached_from);
2210 } else if (state.bisect_in_progress)
2211 strbuf_addf(&desc, _("(no branch, bisect started on %s)"),
2212 state.bisecting_from);
2213 else if (state.detached_from) {
2214 if (state.detached_at)
2215 strbuf_addf(&desc, _("(HEAD detached at %s)"),
2216 state.detached_from);
2217 else
2218 strbuf_addf(&desc, _("(HEAD detached from %s)"),
2219 state.detached_from);
2220 } else
2221 strbuf_addstr(&desc, _("(no branch)"));
2222
2223 wt_status_state_free_buffers(&state);
2224
2225 return strbuf_detach(&desc, NULL);
2226 }
2227
2228 static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref)
2229 {
2230 if (!ref->symref)
2231 return xstrdup("");
2232 else
2233 return show_ref(&atom->u.refname, ref->symref);
2234 }
2235
2236 static const char *get_refname(struct used_atom *atom, struct ref_array_item *ref)
2237 {
2238 if (ref->kind & FILTER_REFS_DETACHED_HEAD)
2239 return get_head_description();
2240 return show_ref(&atom->u.refname, ref->refname);
2241 }
2242
2243 static int get_object(struct ref_array_item *ref, int deref, struct object **obj,
2244 struct expand_data *oi, struct strbuf *err)
2245 {
2246 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
2247 int eaten = 1;
2248 if (oi->info.contentp) {
2249 /* We need to know that to use parse_object_buffer properly */
2250 oi->info.sizep = &oi->size;
2251 oi->info.typep = &oi->type;
2252 }
2253 if (oid_object_info_extended(the_repository, &oi->oid, &oi->info,
2254 OBJECT_INFO_LOOKUP_REPLACE))
2255 return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
2256 oid_to_hex(&oi->oid), ref->refname);
2257 if (oi->info.disk_sizep && oi->disk_size < 0)
2258 BUG("Object size is less than zero.");
2259
2260 if (oi->info.contentp) {
2261 *obj = parse_object_buffer(the_repository, &oi->oid, oi->type, oi->size, oi->content, &eaten);
2262 if (!*obj) {
2263 if (!eaten)
2264 free(oi->content);
2265 return strbuf_addf_ret(err, -1, _("parse_object_buffer failed on %s for %s"),
2266 oid_to_hex(&oi->oid), ref->refname);
2267 }
2268 grab_values(ref->value, deref, *obj, oi);
2269 }
2270
2271 grab_common_values(ref->value, deref, oi);
2272 if (!eaten)
2273 free(oi->content);
2274 return 0;
2275 }
2276
2277 static void populate_worktree_map(struct hashmap *map, struct worktree **worktrees)
2278 {
2279 int i;
2280
2281 for (i = 0; worktrees[i]; i++) {
2282 if (worktrees[i]->head_ref) {
2283 struct ref_to_worktree_entry *entry;
2284 entry = xmalloc(sizeof(*entry));
2285 entry->wt = worktrees[i];
2286 hashmap_entry_init(&entry->ent,
2287 strhash(worktrees[i]->head_ref));
2288
2289 hashmap_add(map, &entry->ent);
2290 }
2291 }
2292 }
2293
2294 static void lazy_init_worktree_map(void)
2295 {
2296 if (ref_to_worktree_map.worktrees)
2297 return;
2298
2299 ref_to_worktree_map.worktrees = get_worktrees();
2300 hashmap_init(&(ref_to_worktree_map.map), ref_to_worktree_map_cmpfnc, NULL, 0);
2301 populate_worktree_map(&(ref_to_worktree_map.map), ref_to_worktree_map.worktrees);
2302 }
2303
2304 static char *get_worktree_path(const struct ref_array_item *ref)
2305 {
2306 struct hashmap_entry entry, *e;
2307 struct ref_to_worktree_entry *lookup_result;
2308
2309 lazy_init_worktree_map();
2310
2311 hashmap_entry_init(&entry, strhash(ref->refname));
2312 e = hashmap_get(&(ref_to_worktree_map.map), &entry, ref->refname);
2313
2314 if (!e)
2315 return xstrdup("");
2316
2317 lookup_result = container_of(e, struct ref_to_worktree_entry, ent);
2318
2319 return xstrdup(lookup_result->wt->path);
2320 }
2321
2322 /*
2323 * Parse the object referred by ref, and grab needed value.
2324 */
2325 static int populate_value(struct ref_array_item *ref, struct strbuf *err)
2326 {
2327 struct object *obj;
2328 int i;
2329 struct object_info empty = OBJECT_INFO_INIT;
2330 int ahead_behind_atoms = 0;
2331
2332 CALLOC_ARRAY(ref->value, used_atom_cnt);
2333
2334 if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
2335 ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
2336 NULL, NULL);
2337 if (!ref->symref)
2338 ref->symref = xstrdup("");
2339 }
2340
2341 /* Fill in specials first */
2342 for (i = 0; i < used_atom_cnt; i++) {
2343 struct used_atom *atom = &used_atom[i];
2344 enum atom_type atom_type = atom->atom_type;
2345 const char *name = used_atom[i].name;
2346 struct atom_value *v = &ref->value[i];
2347 int deref = 0;
2348 const char *refname;
2349 struct branch *branch = NULL;
2350
2351 v->s_size = ATOM_SIZE_UNSPECIFIED;
2352 v->handler = append_atom;
2353 v->value = 0;
2354 v->atom = atom;
2355
2356 if (*name == '*') {
2357 deref = 1;
2358 name++;
2359 }
2360
2361 if (atom_type == ATOM_REFNAME)
2362 refname = get_refname(atom, ref);
2363 else if (atom_type == ATOM_WORKTREEPATH) {
2364 if (ref->kind == FILTER_REFS_BRANCHES)
2365 v->s = get_worktree_path(ref);
2366 else
2367 v->s = xstrdup("");
2368 continue;
2369 }
2370 else if (atom_type == ATOM_SYMREF)
2371 refname = get_symref(atom, ref);
2372 else if (atom_type == ATOM_UPSTREAM) {
2373 const char *branch_name;
2374 /* only local branches may have an upstream */
2375 if (!skip_prefix(ref->refname, "refs/heads/",
2376 &branch_name)) {
2377 v->s = xstrdup("");
2378 continue;
2379 }
2380 branch = branch_get(branch_name);
2381
2382 refname = branch_get_upstream(branch, NULL);
2383 if (refname)
2384 fill_remote_ref_details(atom, refname, branch, &v->s);
2385 else
2386 v->s = xstrdup("");
2387 continue;
2388 } else if (atom_type == ATOM_PUSH && atom->u.remote_ref.push) {
2389 const char *branch_name;
2390 v->s = xstrdup("");
2391 if (!skip_prefix(ref->refname, "refs/heads/",
2392 &branch_name))
2393 continue;
2394 branch = branch_get(branch_name);
2395
2396 if (atom->u.remote_ref.push_remote)
2397 refname = NULL;
2398 else {
2399 refname = branch_get_push(branch, NULL);
2400 if (!refname)
2401 continue;
2402 }
2403 /* We will definitely re-init v->s on the next line. */
2404 free((char *)v->s);
2405 fill_remote_ref_details(atom, refname, branch, &v->s);
2406 continue;
2407 } else if (atom_type == ATOM_COLOR) {
2408 v->s = xstrdup(atom->u.color);
2409 continue;
2410 } else if (atom_type == ATOM_FLAG) {
2411 char buf[256], *cp = buf;
2412 if (ref->flag & REF_ISSYMREF)
2413 cp = copy_advance(cp, ",symref");
2414 if (ref->flag & REF_ISPACKED)
2415 cp = copy_advance(cp, ",packed");
2416 if (cp == buf)
2417 v->s = xstrdup("");
2418 else {
2419 *cp = '\0';
2420 v->s = xstrdup(buf + 1);
2421 }
2422 continue;
2423 } else if (!deref && atom_type == ATOM_OBJECTNAME &&
2424 grab_oid(name, "objectname", &ref->objectname, v, atom)) {
2425 continue;
2426 } else if (atom_type == ATOM_HEAD) {
2427 if (atom->u.head && !strcmp(ref->refname, atom->u.head))
2428 v->s = xstrdup("*");
2429 else
2430 v->s = xstrdup(" ");
2431 continue;
2432 } else if (atom_type == ATOM_ALIGN) {
2433 v->handler = align_atom_handler;
2434 v->s = xstrdup("");
2435 continue;
2436 } else if (atom_type == ATOM_END) {
2437 v->handler = end_atom_handler;
2438 v->s = xstrdup("");
2439 continue;
2440 } else if (atom_type == ATOM_IF) {
2441 const char *s;
2442 if (skip_prefix(name, "if:", &s))
2443 v->s = xstrdup(s);
2444 else
2445 v->s = xstrdup("");
2446 v->handler = if_atom_handler;
2447 continue;
2448 } else if (atom_type == ATOM_THEN) {
2449 v->handler = then_atom_handler;
2450 v->s = xstrdup("");
2451 continue;
2452 } else if (atom_type == ATOM_ELSE) {
2453 v->handler = else_atom_handler;
2454 v->s = xstrdup("");
2455 continue;
2456 } else if (atom_type == ATOM_REST) {
2457 if (ref->rest)
2458 v->s = xstrdup(ref->rest);
2459 else
2460 v->s = xstrdup("");
2461 continue;
2462 } else if (atom_type == ATOM_AHEADBEHIND) {
2463 if (ref->counts) {
2464 const struct ahead_behind_count *count;
2465 count = ref->counts[ahead_behind_atoms++];
2466 v->s = xstrfmt("%d %d", count->ahead, count->behind);
2467 } else {
2468 /* Not a commit. */
2469 v->s = xstrdup("");
2470 }
2471 continue;
2472 } else
2473 continue;
2474
2475 if (!deref)
2476 v->s = xstrdup(refname);
2477 else
2478 v->s = xstrfmt("%s^{}", refname);
2479 free((char *)refname);
2480 }
2481
2482 for (i = 0; i < used_atom_cnt; i++) {
2483 struct atom_value *v = &ref->value[i];
2484 if (v->s == NULL && used_atom[i].source == SOURCE_NONE)
2485 return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
2486 oid_to_hex(&ref->objectname), ref->refname);
2487 }
2488
2489 if (need_tagged)
2490 oi.info.contentp = &oi.content;
2491 if (!memcmp(&oi.info, &empty, sizeof(empty)) &&
2492 !memcmp(&oi_deref.info, &empty, sizeof(empty)))
2493 return 0;
2494
2495
2496 oi.oid = ref->objectname;
2497 if (get_object(ref, 0, &obj, &oi, err))
2498 return -1;
2499
2500 /*
2501 * If there is no atom that wants to know about tagged
2502 * object, we are done.
2503 */
2504 if (!need_tagged || (obj->type != OBJ_TAG))
2505 return 0;
2506
2507 /*
2508 * If it is a tag object, see if we use a value that derefs
2509 * the object, and if we do grab the object it refers to.
2510 */
2511 oi_deref.oid = *get_tagged_oid((struct tag *)obj);
2512
2513 /*
2514 * NEEDSWORK: This derefs tag only once, which
2515 * is good to deal with chains of trust, but
2516 * is not consistent with what deref_tag() does
2517 * which peels the onion to the core.
2518 */
2519 return get_object(ref, 1, &obj, &oi_deref, err);
2520 }
2521
2522 /*
2523 * Given a ref, return the value for the atom. This lazily gets value
2524 * out of the object by calling populate value.
2525 */
2526 static int get_ref_atom_value(struct ref_array_item *ref, int atom,
2527 struct atom_value **v, struct strbuf *err)
2528 {
2529 if (!ref->value) {
2530 if (populate_value(ref, err))
2531 return -1;
2532 fill_missing_values(ref->value);
2533 }
2534 *v = &ref->value[atom];
2535 return 0;
2536 }
2537
2538 /*
2539 * Return 1 if the refname matches one of the patterns, otherwise 0.
2540 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
2541 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
2542 * matches "refs/heads/mas*", too).
2543 */
2544 static int match_pattern(const char **patterns, const char *refname,
2545 int ignore_case)
2546 {
2547 unsigned flags = 0;
2548
2549 if (ignore_case)
2550 flags |= WM_CASEFOLD;
2551
2552 /*
2553 * When no '--format' option is given we need to skip the prefix
2554 * for matching refs of tags and branches.
2555 */
2556 (void)(skip_prefix(refname, "refs/tags/", &refname) ||
2557 skip_prefix(refname, "refs/heads/", &refname) ||
2558 skip_prefix(refname, "refs/remotes/", &refname) ||
2559 skip_prefix(refname, "refs/", &refname));
2560
2561 for (; *patterns; patterns++) {
2562 if (!wildmatch(*patterns, refname, flags))
2563 return 1;
2564 }
2565 return 0;
2566 }
2567
2568 /*
2569 * Return 1 if the refname matches one of the patterns, otherwise 0.
2570 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
2571 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
2572 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
2573 */
2574 static int match_name_as_path(const char **pattern, const char *refname,
2575 int ignore_case)
2576 {
2577 int namelen = strlen(refname);
2578 unsigned flags = WM_PATHNAME;
2579
2580 if (ignore_case)
2581 flags |= WM_CASEFOLD;
2582
2583 for (; *pattern; pattern++) {
2584 const char *p = *pattern;
2585 int plen = strlen(p);
2586
2587 if ((plen <= namelen) &&
2588 !strncmp(refname, p, plen) &&
2589 (refname[plen] == '\0' ||
2590 refname[plen] == '/' ||
2591 p[plen-1] == '/'))
2592 return 1;
2593 if (!wildmatch(p, refname, flags))
2594 return 1;
2595 }
2596 return 0;
2597 }
2598
2599 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
2600 static int filter_pattern_match(struct ref_filter *filter, const char *refname)
2601 {
2602 if (!*filter->name_patterns)
2603 return 1; /* No pattern always matches */
2604 if (filter->match_as_path)
2605 return match_name_as_path(filter->name_patterns, refname,
2606 filter->ignore_case);
2607 return match_pattern(filter->name_patterns, refname,
2608 filter->ignore_case);
2609 }
2610
2611 static int filter_exclude_match(struct ref_filter *filter, const char *refname)
2612 {
2613 if (!filter->exclude.nr)
2614 return 0;
2615 if (filter->match_as_path)
2616 return match_name_as_path(filter->exclude.v, refname,
2617 filter->ignore_case);
2618 return match_pattern(filter->exclude.v, refname, filter->ignore_case);
2619 }
2620
2621 /*
2622 * This is the same as for_each_fullref_in(), but it tries to iterate
2623 * only over the patterns we'll care about. Note that it _doesn't_ do a full
2624 * pattern match, so the callback still has to match each ref individually.
2625 */
2626 static int for_each_fullref_in_pattern(struct ref_filter *filter,
2627 each_ref_fn cb,
2628 void *cb_data)
2629 {
2630 if (!filter->match_as_path) {
2631 /*
2632 * in this case, the patterns are applied after
2633 * prefixes like "refs/heads/" etc. are stripped off,
2634 * so we have to look at everything:
2635 */
2636 return for_each_fullref_in("", cb, cb_data);
2637 }
2638
2639 if (filter->ignore_case) {
2640 /*
2641 * we can't handle case-insensitive comparisons,
2642 * so just return everything and let the caller
2643 * sort it out.
2644 */
2645 return for_each_fullref_in("", cb, cb_data);
2646 }
2647
2648 if (!filter->name_patterns[0]) {
2649 /* no patterns; we have to look at everything */
2650 return refs_for_each_fullref_in(get_main_ref_store(the_repository),
2651 "", filter->exclude.v, cb, cb_data);
2652 }
2653
2654 return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository),
2655 NULL, filter->name_patterns,
2656 filter->exclude.v,
2657 cb, cb_data);
2658 }
2659
2660 /*
2661 * Given a ref (oid, refname), check if the ref belongs to the array
2662 * of oids. If the given ref is a tag, check if the given tag points
2663 * at one of the oids in the given oid array. Returns non-zero if a
2664 * match is found.
2665 *
2666 * NEEDSWORK:
2667 * As the refs are cached we might know what refname peels to without
2668 * the need to parse the object via parse_object(). peel_ref() might be a
2669 * more efficient alternative to obtain the pointee.
2670 */
2671 static int match_points_at(struct oid_array *points_at,
2672 const struct object_id *oid,
2673 const char *refname)
2674 {
2675 struct object *obj;
2676
2677 if (oid_array_lookup(points_at, oid) >= 0)
2678 return 1;
2679 obj = parse_object_with_flags(the_repository, oid,
2680 PARSE_OBJECT_SKIP_HASH_CHECK);
2681 while (obj && obj->type == OBJ_TAG) {
2682 struct tag *tag = (struct tag *)obj;
2683
2684 if (parse_tag(tag) < 0) {
2685 obj = NULL;
2686 break;
2687 }
2688
2689 if (oid_array_lookup(points_at, get_tagged_oid(tag)) >= 0)
2690 return 1;
2691
2692 obj = tag->tagged;
2693 }
2694 if (!obj)
2695 die(_("malformed object at '%s'"), refname);
2696 return 0;
2697 }
2698
2699 /*
2700 * Allocate space for a new ref_array_item and copy the name and oid to it.
2701 *
2702 * Callers can then fill in other struct members at their leisure.
2703 */
2704 static struct ref_array_item *new_ref_array_item(const char *refname,
2705 const struct object_id *oid)
2706 {
2707 struct ref_array_item *ref;
2708
2709 FLEX_ALLOC_STR(ref, refname, refname);
2710 oidcpy(&ref->objectname, oid);
2711 ref->rest = NULL;
2712
2713 return ref;
2714 }
2715
2716 struct ref_array_item *ref_array_push(struct ref_array *array,
2717 const char *refname,
2718 const struct object_id *oid)
2719 {
2720 struct ref_array_item *ref = new_ref_array_item(refname, oid);
2721
2722 ALLOC_GROW(array->items, array->nr + 1, array->alloc);
2723 array->items[array->nr++] = ref;
2724
2725 return ref;
2726 }
2727
2728 static int ref_kind_from_refname(const char *refname)
2729 {
2730 unsigned int i;
2731
2732 static struct {
2733 const char *prefix;
2734 unsigned int kind;
2735 } ref_kind[] = {
2736 { "refs/heads/" , FILTER_REFS_BRANCHES },
2737 { "refs/remotes/" , FILTER_REFS_REMOTES },
2738 { "refs/tags/", FILTER_REFS_TAGS}
2739 };
2740
2741 if (!strcmp(refname, "HEAD"))
2742 return FILTER_REFS_DETACHED_HEAD;
2743
2744 for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
2745 if (starts_with(refname, ref_kind[i].prefix))
2746 return ref_kind[i].kind;
2747 }
2748
2749 return FILTER_REFS_OTHERS;
2750 }
2751
2752 static int filter_ref_kind(struct ref_filter *filter, const char *refname)
2753 {
2754 if (filter->kind == FILTER_REFS_BRANCHES ||
2755 filter->kind == FILTER_REFS_REMOTES ||
2756 filter->kind == FILTER_REFS_TAGS)
2757 return filter->kind;
2758 return ref_kind_from_refname(refname);
2759 }
2760
2761 struct ref_filter_cbdata {
2762 struct ref_array *array;
2763 struct ref_filter *filter;
2764 struct contains_cache contains_cache;
2765 struct contains_cache no_contains_cache;
2766 };
2767
2768 /*
2769 * A call-back given to for_each_ref(). Filter refs and keep them for
2770 * later object processing.
2771 */
2772 static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
2773 {
2774 struct ref_filter_cbdata *ref_cbdata = cb_data;
2775 struct ref_filter *filter = ref_cbdata->filter;
2776 struct ref_array_item *ref;
2777 struct commit *commit = NULL;
2778 unsigned int kind;
2779
2780 if (flag & REF_BAD_NAME) {
2781 warning(_("ignoring ref with broken name %s"), refname);
2782 return 0;
2783 }
2784
2785 if (flag & REF_ISBROKEN) {
2786 warning(_("ignoring broken ref %s"), refname);
2787 return 0;
2788 }
2789
2790 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2791 kind = filter_ref_kind(filter, refname);
2792 if (!(kind & filter->kind))
2793 return 0;
2794
2795 if (!filter_pattern_match(filter, refname))
2796 return 0;
2797
2798 if (filter_exclude_match(filter, refname))
2799 return 0;
2800
2801 if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname))
2802 return 0;
2803
2804 /*
2805 * A merge filter is applied on refs pointing to commits. Hence
2806 * obtain the commit using the 'oid' available and discard all
2807 * non-commits early. The actual filtering is done later.
2808 */
2809 if (filter->reachable_from || filter->unreachable_from ||
2810 filter->with_commit || filter->no_commit || filter->verbose) {
2811 commit = lookup_commit_reference_gently(the_repository, oid, 1);
2812 if (!commit)
2813 return 0;
2814 /* We perform the filtering for the '--contains' option... */
2815 if (filter->with_commit &&
2816 !commit_contains(filter, commit, filter->with_commit, &ref_cbdata->contains_cache))
2817 return 0;
2818 /* ...or for the `--no-contains' option */
2819 if (filter->no_commit &&
2820 commit_contains(filter, commit, filter->no_commit, &ref_cbdata->no_contains_cache))
2821 return 0;
2822 }
2823
2824 /*
2825 * We do not open the object yet; sort may only need refname
2826 * to do its job and the resulting list may yet to be pruned
2827 * by maxcount logic.
2828 */
2829 ref = ref_array_push(ref_cbdata->array, refname, oid);
2830 ref->commit = commit;
2831 ref->flag = flag;
2832 ref->kind = kind;
2833
2834 return 0;
2835 }
2836
2837 /* Free memory allocated for a ref_array_item */
2838 static void free_array_item(struct ref_array_item *item)
2839 {
2840 free((char *)item->symref);
2841 if (item->value) {
2842 int i;
2843 for (i = 0; i < used_atom_cnt; i++)
2844 free((char *)item->value[i].s);
2845 free(item->value);
2846 }
2847 free(item->counts);
2848 free(item);
2849 }
2850
2851 /* Free all memory allocated for ref_array */
2852 void ref_array_clear(struct ref_array *array)
2853 {
2854 int i;
2855
2856 for (i = 0; i < array->nr; i++)
2857 free_array_item(array->items[i]);
2858 FREE_AND_NULL(array->items);
2859 array->nr = array->alloc = 0;
2860
2861 for (i = 0; i < used_atom_cnt; i++) {
2862 struct used_atom *atom = &used_atom[i];
2863 if (atom->atom_type == ATOM_HEAD)
2864 free(atom->u.head);
2865 free((char *)atom->name);
2866 }
2867 FREE_AND_NULL(used_atom);
2868 used_atom_cnt = 0;
2869
2870 if (ref_to_worktree_map.worktrees) {
2871 hashmap_clear_and_free(&(ref_to_worktree_map.map),
2872 struct ref_to_worktree_entry, ent);
2873 free_worktrees(ref_to_worktree_map.worktrees);
2874 ref_to_worktree_map.worktrees = NULL;
2875 }
2876
2877 FREE_AND_NULL(array->counts);
2878 }
2879
2880 #define EXCLUDE_REACHED 0
2881 #define INCLUDE_REACHED 1
2882 static void reach_filter(struct ref_array *array,
2883 struct commit_list **check_reachable,
2884 int include_reached)
2885 {
2886 int i, old_nr;
2887 struct commit **to_clear;
2888
2889 if (!*check_reachable)
2890 return;
2891
2892 CALLOC_ARRAY(to_clear, array->nr);
2893 for (i = 0; i < array->nr; i++) {
2894 struct ref_array_item *item = array->items[i];
2895 to_clear[i] = item->commit;
2896 }
2897
2898 tips_reachable_from_bases(the_repository,
2899 *check_reachable,
2900 to_clear, array->nr,
2901 UNINTERESTING);
2902
2903 old_nr = array->nr;
2904 array->nr = 0;
2905
2906 for (i = 0; i < old_nr; i++) {
2907 struct ref_array_item *item = array->items[i];
2908 struct commit *commit = item->commit;
2909
2910 int is_merged = !!(commit->object.flags & UNINTERESTING);
2911
2912 if (is_merged == include_reached)
2913 array->items[array->nr++] = array->items[i];
2914 else
2915 free_array_item(item);
2916 }
2917
2918 clear_commit_marks_many(old_nr, to_clear, ALL_REV_FLAGS);
2919
2920 while (*check_reachable) {
2921 struct commit *merge_commit = pop_commit(check_reachable);
2922 clear_commit_marks(merge_commit, ALL_REV_FLAGS);
2923 }
2924
2925 free(to_clear);
2926 }
2927
2928 void filter_ahead_behind(struct repository *r,
2929 struct ref_format *format,
2930 struct ref_array *array)
2931 {
2932 struct commit **commits;
2933 size_t commits_nr = format->bases.nr + array->nr;
2934
2935 if (!format->bases.nr || !array->nr)
2936 return;
2937
2938 ALLOC_ARRAY(commits, commits_nr);
2939 for (size_t i = 0; i < format->bases.nr; i++)
2940 commits[i] = format->bases.items[i].util;
2941
2942 ALLOC_ARRAY(array->counts, st_mult(format->bases.nr, array->nr));
2943
2944 commits_nr = format->bases.nr;
2945 array->counts_nr = 0;
2946 for (size_t i = 0; i < array->nr; i++) {
2947 const char *name = array->items[i]->refname;
2948 commits[commits_nr] = lookup_commit_reference_by_name(name);
2949
2950 if (!commits[commits_nr])
2951 continue;
2952
2953 CALLOC_ARRAY(array->items[i]->counts, format->bases.nr);
2954 for (size_t j = 0; j < format->bases.nr; j++) {
2955 struct ahead_behind_count *count;
2956 count = &array->counts[array->counts_nr++];
2957 count->tip_index = commits_nr;
2958 count->base_index = j;
2959
2960 array->items[i]->counts[j] = count;
2961 }
2962 commits_nr++;
2963 }
2964
2965 ahead_behind(r, commits, commits_nr, array->counts, array->counts_nr);
2966 free(commits);
2967 }
2968
2969 /*
2970 * API for filtering a set of refs. Based on the type of refs the user
2971 * has requested, we iterate through those refs and apply filters
2972 * as per the given ref_filter structure and finally store the
2973 * filtered refs in the ref_array structure.
2974 */
2975 int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
2976 {
2977 struct ref_filter_cbdata ref_cbdata;
2978 int save_commit_buffer_orig;
2979 int ret = 0;
2980
2981 ref_cbdata.array = array;
2982 ref_cbdata.filter = filter;
2983
2984 filter->kind = type & FILTER_REFS_KIND_MASK;
2985
2986 save_commit_buffer_orig = save_commit_buffer;
2987 save_commit_buffer = 0;
2988
2989 init_contains_cache(&ref_cbdata.contains_cache);
2990 init_contains_cache(&ref_cbdata.no_contains_cache);
2991
2992 /* Simple per-ref filtering */
2993 if (!filter->kind)
2994 die("filter_refs: invalid type");
2995 else {
2996 /*
2997 * For common cases where we need only branches or remotes or tags,
2998 * we only iterate through those refs. If a mix of refs is needed,
2999 * we iterate over all refs and filter out required refs with the help
3000 * of filter_ref_kind().
3001 */
3002 if (filter->kind == FILTER_REFS_BRANCHES)
3003 ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata);
3004 else if (filter->kind == FILTER_REFS_REMOTES)
3005 ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata);
3006 else if (filter->kind == FILTER_REFS_TAGS)
3007 ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata);
3008 else if (filter->kind & FILTER_REFS_ALL)
3009 ret = for_each_fullref_in_pattern(filter, ref_filter_handler, &ref_cbdata);
3010 if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
3011 head_ref(ref_filter_handler, &ref_cbdata);
3012 }
3013
3014 clear_contains_cache(&ref_cbdata.contains_cache);
3015 clear_contains_cache(&ref_cbdata.no_contains_cache);
3016
3017 /* Filters that need revision walking */
3018 reach_filter(array, &filter->reachable_from, INCLUDE_REACHED);
3019 reach_filter(array, &filter->unreachable_from, EXCLUDE_REACHED);
3020
3021 save_commit_buffer = save_commit_buffer_orig;
3022 return ret;
3023 }
3024
3025 static int compare_detached_head(struct ref_array_item *a, struct ref_array_item *b)
3026 {
3027 if (!(a->kind ^ b->kind))
3028 BUG("ref_kind_from_refname() should only mark one ref as HEAD");
3029 if (a->kind & FILTER_REFS_DETACHED_HEAD)
3030 return -1;
3031 else if (b->kind & FILTER_REFS_DETACHED_HEAD)
3032 return 1;
3033 BUG("should have died in the xor check above");
3034 return 0;
3035 }
3036
3037 static int memcasecmp(const void *vs1, const void *vs2, size_t n)
3038 {
3039 const char *s1 = vs1, *s2 = vs2;
3040 const char *end = s1 + n;
3041
3042 for (; s1 < end; s1++, s2++) {
3043 int diff = tolower(*s1) - tolower(*s2);
3044 if (diff)
3045 return diff;
3046 }
3047 return 0;
3048 }
3049
3050 struct ref_sorting {
3051 struct ref_sorting *next;
3052 int atom; /* index into used_atom array (internal) */
3053 enum ref_sorting_order sort_flags;
3054 };
3055
3056 static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
3057 {
3058 struct atom_value *va, *vb;
3059 int cmp;
3060 int cmp_detached_head = 0;
3061 cmp_type cmp_type = used_atom[s->atom].type;
3062 struct strbuf err = STRBUF_INIT;
3063
3064 if (get_ref_atom_value(a, s->atom, &va, &err))
3065 die("%s", err.buf);
3066 if (get_ref_atom_value(b, s->atom, &vb, &err))
3067 die("%s", err.buf);
3068 strbuf_release(&err);
3069 if (s->sort_flags & REF_SORTING_DETACHED_HEAD_FIRST &&
3070 ((a->kind | b->kind) & FILTER_REFS_DETACHED_HEAD)) {
3071 cmp = compare_detached_head(a, b);
3072 cmp_detached_head = 1;
3073 } else if (s->sort_flags & REF_SORTING_VERSION) {
3074 cmp = versioncmp(va->s, vb->s);
3075 } else if (cmp_type == FIELD_STR) {
3076 if (va->s_size < 0 && vb->s_size < 0) {
3077 int (*cmp_fn)(const char *, const char *);
3078 cmp_fn = s->sort_flags & REF_SORTING_ICASE
3079 ? strcasecmp : strcmp;
3080 cmp = cmp_fn(va->s, vb->s);
3081 } else {
3082 size_t a_size = va->s_size < 0 ?
3083 strlen(va->s) : va->s_size;
3084 size_t b_size = vb->s_size < 0 ?
3085 strlen(vb->s) : vb->s_size;
3086 int (*cmp_fn)(const void *, const void *, size_t);
3087 cmp_fn = s->sort_flags & REF_SORTING_ICASE
3088 ? memcasecmp : memcmp;
3089
3090 cmp = cmp_fn(va->s, vb->s, b_size > a_size ?
3091 a_size : b_size);
3092 if (!cmp) {
3093 if (a_size > b_size)
3094 cmp = 1;
3095 else if (a_size < b_size)
3096 cmp = -1;
3097 }
3098 }
3099 } else {
3100 if (va->value < vb->value)
3101 cmp = -1;
3102 else if (va->value == vb->value)
3103 cmp = 0;
3104 else
3105 cmp = 1;
3106 }
3107
3108 return (s->sort_flags & REF_SORTING_REVERSE && !cmp_detached_head)
3109 ? -cmp : cmp;
3110 }
3111
3112 static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
3113 {
3114 struct ref_array_item *a = *((struct ref_array_item **)a_);
3115 struct ref_array_item *b = *((struct ref_array_item **)b_);
3116 struct ref_sorting *s;
3117
3118 for (s = ref_sorting; s; s = s->next) {
3119 int cmp = cmp_ref_sorting(s, a, b);
3120 if (cmp)
3121 return cmp;
3122 }
3123 s = ref_sorting;
3124 return s && s->sort_flags & REF_SORTING_ICASE ?
3125 strcasecmp(a->refname, b->refname) :
3126 strcmp(a->refname, b->refname);
3127 }
3128
3129 void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting,
3130 unsigned int mask, int on)
3131 {
3132 for (; sorting; sorting = sorting->next) {
3133 if (on)
3134 sorting->sort_flags |= mask;
3135 else
3136 sorting->sort_flags &= ~mask;
3137 }
3138 }
3139
3140 void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
3141 {
3142 QSORT_S(array->items, array->nr, compare_refs, sorting);
3143 }
3144
3145 static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
3146 {
3147 struct strbuf *s = &state->stack->output;
3148
3149 while (*cp && (!ep || cp < ep)) {
3150 if (*cp == '%') {
3151 if (cp[1] == '%')
3152 cp++;
3153 else {
3154 int ch = hex2chr(cp + 1);
3155 if (0 <= ch) {
3156 strbuf_addch(s, ch);
3157 cp += 3;
3158 continue;
3159 }
3160 }
3161 }
3162 strbuf_addch(s, *cp);
3163 cp++;
3164 }
3165 }
3166
3167 int format_ref_array_item(struct ref_array_item *info,
3168 struct ref_format *format,
3169 struct strbuf *final_buf,
3170 struct strbuf *error_buf)
3171 {
3172 const char *cp, *sp, *ep;
3173 struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
3174
3175 state.quote_style = format->quote_style;
3176 push_stack_element(&state.stack);
3177
3178 for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) {
3179 struct atom_value *atomv;
3180 int pos;
3181
3182 ep = strchr(sp, ')');
3183 if (cp < sp)
3184 append_literal(cp, sp, &state);
3185 pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf);
3186 if (pos < 0 || get_ref_atom_value(info, pos, &atomv, error_buf) ||
3187 atomv->handler(atomv, &state, error_buf)) {
3188 pop_stack_element(&state.stack);
3189 return -1;
3190 }
3191 }
3192 if (*cp) {
3193 sp = cp + strlen(cp);
3194 append_literal(cp, sp, &state);
3195 }
3196 if (format->need_color_reset_at_eol) {
3197 struct atom_value resetv = ATOM_VALUE_INIT;
3198 resetv.s = GIT_COLOR_RESET;
3199 if (append_atom(&resetv, &state, error_buf)) {
3200 pop_stack_element(&state.stack);
3201 return -1;
3202 }
3203 }
3204 if (state.stack->prev) {
3205 pop_stack_element(&state.stack);
3206 return strbuf_addf_ret(error_buf, -1, _("format: %%(end) atom missing"));
3207 }
3208 strbuf_addbuf(final_buf, &state.stack->output);
3209 pop_stack_element(&state.stack);
3210 return 0;
3211 }
3212
3213 void pretty_print_ref(const char *name, const struct object_id *oid,
3214 struct ref_format *format)
3215 {
3216 struct ref_array_item *ref_item;
3217 struct strbuf output = STRBUF_INIT;
3218 struct strbuf err = STRBUF_INIT;
3219
3220 ref_item = new_ref_array_item(name, oid);
3221 ref_item->kind = ref_kind_from_refname(name);
3222 if (format_ref_array_item(ref_item, format, &output, &err))
3223 die("%s", err.buf);
3224 fwrite(output.buf, 1, output.len, stdout);
3225 putchar('\n');
3226
3227 strbuf_release(&err);
3228 strbuf_release(&output);
3229 free_array_item(ref_item);
3230 }
3231
3232 static int parse_sorting_atom(const char *atom)
3233 {
3234 /*
3235 * This parses an atom using a dummy ref_format, since we don't
3236 * actually care about the formatting details.
3237 */
3238 struct ref_format dummy = REF_FORMAT_INIT;
3239 const char *end = atom + strlen(atom);
3240 struct strbuf err = STRBUF_INIT;
3241 int res = parse_ref_filter_atom(&dummy, atom, end, &err);
3242 if (res < 0)
3243 die("%s", err.buf);
3244 strbuf_release(&err);
3245 return res;
3246 }
3247
3248 /* If no sorting option is given, use refname to sort as default */
3249 static struct ref_sorting *ref_default_sorting(void)
3250 {
3251 static const char cstr_name[] = "refname";
3252
3253 struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
3254
3255 sorting->next = NULL;
3256 sorting->atom = parse_sorting_atom(cstr_name);
3257 return sorting;
3258 }
3259
3260 static void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
3261 {
3262 struct ref_sorting *s;
3263
3264 CALLOC_ARRAY(s, 1);
3265 s->next = *sorting_tail;
3266 *sorting_tail = s;
3267
3268 if (*arg == '-') {
3269 s->sort_flags |= REF_SORTING_REVERSE;
3270 arg++;
3271 }
3272 if (skip_prefix(arg, "version:", &arg) ||
3273 skip_prefix(arg, "v:", &arg))
3274 s->sort_flags |= REF_SORTING_VERSION;
3275 s->atom = parse_sorting_atom(arg);
3276 }
3277
3278 struct ref_sorting *ref_sorting_options(struct string_list *options)
3279 {
3280 struct string_list_item *item;
3281 struct ref_sorting *sorting = NULL, **tail = &sorting;
3282
3283 if (!options->nr) {
3284 sorting = ref_default_sorting();
3285 } else {
3286 for_each_string_list_item(item, options)
3287 parse_ref_sorting(tail, item->string);
3288 }
3289
3290 /*
3291 * From here on, the ref_sorting list should be used to talk
3292 * about the sort order used for the output. The caller
3293 * should not touch the string form anymore.
3294 */
3295 string_list_clear(options, 0);
3296 return sorting;
3297 }
3298
3299 void ref_sorting_release(struct ref_sorting *sorting)
3300 {
3301 while (sorting) {
3302 struct ref_sorting *next = sorting->next;
3303 free(sorting);
3304 sorting = next;
3305 }
3306 }
3307
3308 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
3309 {
3310 struct ref_filter *rf = opt->value;
3311 struct object_id oid;
3312 struct commit *merge_commit;
3313
3314 BUG_ON_OPT_NEG(unset);
3315
3316 if (repo_get_oid(the_repository, arg, &oid))
3317 die(_("malformed object name %s"), arg);
3318
3319 merge_commit = lookup_commit_reference_gently(the_repository, &oid, 0);
3320
3321 if (!merge_commit)
3322 return error(_("option `%s' must point to a commit"), opt->long_name);
3323
3324 if (starts_with(opt->long_name, "no"))
3325 commit_list_insert(merge_commit, &rf->unreachable_from);
3326 else
3327 commit_list_insert(merge_commit, &rf->reachable_from);
3328
3329 return 0;
3330 }
3331
3332 void ref_filter_init(struct ref_filter *filter)
3333 {
3334 struct ref_filter blank = REF_FILTER_INIT;
3335 memcpy(filter, &blank, sizeof(blank));
3336 }
3337
3338 void ref_filter_clear(struct ref_filter *filter)
3339 {
3340 strvec_clear(&filter->exclude);
3341 oid_array_clear(&filter->points_at);
3342 free_commit_list(filter->with_commit);
3343 free_commit_list(filter->no_commit);
3344 free_commit_list(filter->reachable_from);
3345 free_commit_list(filter->unreachable_from);
3346 ref_filter_init(filter);
3347 }