3 #include "parse-options.h"
6 #include "object-store.h"
7 #include "repository.h"
13 #include "ref-filter.h"
16 #include "git-compat-util.h"
19 #include "wt-status.h"
20 #include "commit-slab.h"
21 #include "commit-graph.h"
22 #include "commit-reach.h"
27 static struct ref_msg {
31 const char *ahead_behind;
33 /* Untranslated plumbing messages: */
40 void setup_ref_filter_porcelain_msg(void)
42 msgs.gone = _("gone");
43 msgs.ahead = _("ahead %d");
44 msgs.behind = _("behind %d");
45 msgs.ahead_behind = _("ahead %d, behind %d");
48 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
49 typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status;
50 typedef enum { SOURCE_NONE = 0, SOURCE_OBJ, SOURCE_OTHER } info_source;
58 cmp_status cmp_status;
60 unsigned int then_atom_seen : 1,
62 condition_satisfied : 1;
66 enum { R_NORMAL, R_SHORT, R_LSTRIP, R_RSTRIP } option;
70 static struct ref_trailer_buf {
71 struct string_list filter_list;
73 struct strbuf kvsepbuf;
74 } ref_trailer_buf = {STRING_LIST_INIT_NODUP, STRBUF_INIT, STRBUF_INIT};
76 static struct expand_data {
78 enum object_type type;
81 struct object_id delta_base_oid;
84 struct object_info info;
87 struct ref_to_worktree_entry {
88 struct hashmap_entry ent;
89 struct worktree *wt; /* key is wt->head_ref */
92 static int ref_to_worktree_map_cmpfnc(const void *unused_lookupdata,
93 const struct hashmap_entry *eptr,
94 const struct hashmap_entry *kptr,
95 const void *keydata_aka_refname)
97 const struct ref_to_worktree_entry *e, *k;
99 e = container_of(eptr, const struct ref_to_worktree_entry, ent);
100 k = container_of(kptr, const struct ref_to_worktree_entry, ent);
102 return strcmp(e->wt->head_ref,
103 keydata_aka_refname ? keydata_aka_refname : k->wt->head_ref);
106 static struct ref_to_worktree_map {
108 struct worktree **worktrees;
109 } ref_to_worktree_map;
112 * An atom is a valid field atom listed below, possibly prefixed with
113 * a "*" to denote deref_tag().
115 * We parse given format string and sort specifiers, and make a list
116 * of properties that we need to extract out of objects. ref_array_item
117 * structure will hold an array of values extracted that can be
118 * indexed with the "atom number", which is an index into this
121 static struct used_atom {
126 char color[COLOR_MAXLEN];
130 RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME, RR_REMOTE_REF
132 struct refname_atom refname;
133 unsigned int nobracket : 1, push : 1, push_remote : 1;
136 enum { C_BARE, C_BODY, C_BODY_DEP, C_LENGTH, C_LINES,
137 C_SIG, C_SUB, C_SUB_SANITIZE, C_TRAILERS } option;
138 struct process_trailer_options trailer_opts;
142 cmp_status cmp_status;
146 enum { O_FULL, O_LENGTH, O_SHORT } option;
150 enum { O_SIZE, O_SIZE_DISK } option;
152 struct email_option {
153 enum { EO_RAW, EO_TRIM, EO_LOCALPART } option;
155 struct refname_atom refname;
159 static int used_atom_cnt, need_tagged, need_symref;
162 * Expand string, append it to strbuf *sb, then return error code ret.
163 * Allow to save few lines of code.
165 static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...)
169 strbuf_vaddf(sb, fmt, ap);
174 static int color_atom_parser(const struct ref_format *format, struct used_atom *atom,
175 const char *color_value, struct strbuf *err)
178 return strbuf_addf_ret(err, -1, _("expected format: %%(color:<color>)"));
179 if (color_parse(color_value, atom->u.color) < 0)
180 return strbuf_addf_ret(err, -1, _("unrecognized color: %%(color:%s)"),
183 * We check this after we've parsed the color, which lets us complain
184 * about syntactically bogus color names even if they won't be used.
186 if (!want_color(format->use_color))
187 color_parse("", atom->u.color);
191 static int refname_atom_parser_internal(struct refname_atom *atom, const char *arg,
192 const char *name, struct strbuf *err)
195 atom->option = R_NORMAL;
196 else if (!strcmp(arg, "short"))
197 atom->option = R_SHORT;
198 else if (skip_prefix(arg, "lstrip=", &arg) ||
199 skip_prefix(arg, "strip=", &arg)) {
200 atom->option = R_LSTRIP;
201 if (strtol_i(arg, 10, &atom->lstrip))
202 return strbuf_addf_ret(err, -1, _("Integer value expected refname:lstrip=%s"), arg);
203 } else if (skip_prefix(arg, "rstrip=", &arg)) {
204 atom->option = R_RSTRIP;
205 if (strtol_i(arg, 10, &atom->rstrip))
206 return strbuf_addf_ret(err, -1, _("Integer value expected refname:rstrip=%s"), arg);
208 return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), name, arg);
212 static int remote_ref_atom_parser(const struct ref_format *format, struct used_atom *atom,
213 const char *arg, struct strbuf *err)
215 struct string_list params = STRING_LIST_INIT_DUP;
218 if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:"))
219 atom->u.remote_ref.push = 1;
222 atom->u.remote_ref.option = RR_REF;
223 return refname_atom_parser_internal(&atom->u.remote_ref.refname,
224 arg, atom->name, err);
227 atom->u.remote_ref.nobracket = 0;
228 string_list_split(¶ms, arg, ',', -1);
230 for (i = 0; i < params.nr; i++) {
231 const char *s = params.items[i].string;
233 if (!strcmp(s, "track"))
234 atom->u.remote_ref.option = RR_TRACK;
235 else if (!strcmp(s, "trackshort"))
236 atom->u.remote_ref.option = RR_TRACKSHORT;
237 else if (!strcmp(s, "nobracket"))
238 atom->u.remote_ref.nobracket = 1;
239 else if (!strcmp(s, "remotename")) {
240 atom->u.remote_ref.option = RR_REMOTE_NAME;
241 atom->u.remote_ref.push_remote = 1;
242 } else if (!strcmp(s, "remoteref")) {
243 atom->u.remote_ref.option = RR_REMOTE_REF;
244 atom->u.remote_ref.push_remote = 1;
246 atom->u.remote_ref.option = RR_REF;
247 if (refname_atom_parser_internal(&atom->u.remote_ref.refname,
248 arg, atom->name, err)) {
249 string_list_clear(¶ms, 0);
255 string_list_clear(¶ms, 0);
259 static int objecttype_atom_parser(const struct ref_format *format, struct used_atom *atom,
260 const char *arg, struct strbuf *err)
263 return strbuf_addf_ret(err, -1, _("%%(objecttype) does not take arguments"));
264 if (*atom->name == '*')
265 oi_deref.info.typep = &oi_deref.type;
267 oi.info.typep = &oi.type;
271 static int objectsize_atom_parser(const struct ref_format *format, struct used_atom *atom,
272 const char *arg, struct strbuf *err)
275 atom->u.objectsize.option = O_SIZE;
276 if (*atom->name == '*')
277 oi_deref.info.sizep = &oi_deref.size;
279 oi.info.sizep = &oi.size;
280 } else if (!strcmp(arg, "disk")) {
281 atom->u.objectsize.option = O_SIZE_DISK;
282 if (*atom->name == '*')
283 oi_deref.info.disk_sizep = &oi_deref.disk_size;
285 oi.info.disk_sizep = &oi.disk_size;
287 return strbuf_addf_ret(err, -1, _("unrecognized %%(objectsize) argument: %s"), arg);
291 static int deltabase_atom_parser(const struct ref_format *format, struct used_atom *atom,
292 const char *arg, struct strbuf *err)
295 return strbuf_addf_ret(err, -1, _("%%(deltabase) does not take arguments"));
296 if (*atom->name == '*')
297 oi_deref.info.delta_base_oid = &oi_deref.delta_base_oid;
299 oi.info.delta_base_oid = &oi.delta_base_oid;
303 static int body_atom_parser(const struct ref_format *format, struct used_atom *atom,
304 const char *arg, struct strbuf *err)
307 return strbuf_addf_ret(err, -1, _("%%(body) does not take arguments"));
308 atom->u.contents.option = C_BODY_DEP;
312 static int subject_atom_parser(const struct ref_format *format, struct used_atom *atom,
313 const char *arg, struct strbuf *err)
316 atom->u.contents.option = C_SUB;
317 else if (!strcmp(arg, "sanitize"))
318 atom->u.contents.option = C_SUB_SANITIZE;
320 return strbuf_addf_ret(err, -1, _("unrecognized %%(subject) argument: %s"), arg);
324 static int trailers_atom_parser(const struct ref_format *format, struct used_atom *atom,
325 const char *arg, struct strbuf *err)
327 atom->u.contents.trailer_opts.no_divider = 1;
330 const char *argbuf = xstrfmt("%s)", arg);
331 char *invalid_arg = NULL;
333 if (format_set_trailers_options(&atom->u.contents.trailer_opts,
334 &ref_trailer_buf.filter_list,
335 &ref_trailer_buf.sepbuf,
336 &ref_trailer_buf.kvsepbuf,
337 &argbuf, &invalid_arg)) {
339 strbuf_addf(err, _("expected %%(trailers:key=<value>)"));
341 strbuf_addf(err, _("unknown %%(trailers) argument: %s"), invalid_arg);
342 free((char *)invalid_arg);
346 atom->u.contents.option = C_TRAILERS;
350 static int contents_atom_parser(const struct ref_format *format, struct used_atom *atom,
351 const char *arg, struct strbuf *err)
354 atom->u.contents.option = C_BARE;
355 else if (!strcmp(arg, "body"))
356 atom->u.contents.option = C_BODY;
357 else if (!strcmp(arg, "size"))
358 atom->u.contents.option = C_LENGTH;
359 else if (!strcmp(arg, "signature"))
360 atom->u.contents.option = C_SIG;
361 else if (!strcmp(arg, "subject"))
362 atom->u.contents.option = C_SUB;
363 else if (!strcmp(arg, "trailers")) {
364 if (trailers_atom_parser(format, atom, NULL, err))
366 } else if (skip_prefix(arg, "trailers:", &arg)) {
367 if (trailers_atom_parser(format, atom, arg, err))
369 } else if (skip_prefix(arg, "lines=", &arg)) {
370 atom->u.contents.option = C_LINES;
371 if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
372 return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg);
374 return strbuf_addf_ret(err, -1, _("unrecognized %%(contents) argument: %s"), arg);
378 static int oid_atom_parser(const struct ref_format *format, struct used_atom *atom,
379 const char *arg, struct strbuf *err)
382 atom->u.oid.option = O_FULL;
383 else if (!strcmp(arg, "short"))
384 atom->u.oid.option = O_SHORT;
385 else if (skip_prefix(arg, "short=", &arg)) {
386 atom->u.oid.option = O_LENGTH;
387 if (strtoul_ui(arg, 10, &atom->u.oid.length) ||
388 atom->u.oid.length == 0)
389 return strbuf_addf_ret(err, -1, _("positive value expected '%s' in %%(%s)"), arg, atom->name);
390 if (atom->u.oid.length < MINIMUM_ABBREV)
391 atom->u.oid.length = MINIMUM_ABBREV;
393 return strbuf_addf_ret(err, -1, _("unrecognized argument '%s' in %%(%s)"), arg, atom->name);
397 static int person_email_atom_parser(const struct ref_format *format, struct used_atom *atom,
398 const char *arg, struct strbuf *err)
401 atom->u.email_option.option = EO_RAW;
402 else if (!strcmp(arg, "trim"))
403 atom->u.email_option.option = EO_TRIM;
404 else if (!strcmp(arg, "localpart"))
405 atom->u.email_option.option = EO_LOCALPART;
407 return strbuf_addf_ret(err, -1, _("unrecognized email option: %s"), arg);
411 static int refname_atom_parser(const struct ref_format *format, struct used_atom *atom,
412 const char *arg, struct strbuf *err)
414 return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err);
417 static align_type parse_align_position(const char *s)
419 if (!strcmp(s, "right"))
421 else if (!strcmp(s, "middle"))
423 else if (!strcmp(s, "left"))
428 static int align_atom_parser(const struct ref_format *format, struct used_atom *atom,
429 const char *arg, struct strbuf *err)
431 struct align *align = &atom->u.align;
432 struct string_list params = STRING_LIST_INIT_DUP;
434 unsigned int width = ~0U;
437 return strbuf_addf_ret(err, -1, _("expected format: %%(align:<width>,<position>)"));
439 align->position = ALIGN_LEFT;
441 string_list_split(¶ms, arg, ',', -1);
442 for (i = 0; i < params.nr; i++) {
443 const char *s = params.items[i].string;
446 if (skip_prefix(s, "position=", &s)) {
447 position = parse_align_position(s);
449 strbuf_addf(err, _("unrecognized position:%s"), s);
450 string_list_clear(¶ms, 0);
453 align->position = position;
454 } else if (skip_prefix(s, "width=", &s)) {
455 if (strtoul_ui(s, 10, &width)) {
456 strbuf_addf(err, _("unrecognized width:%s"), s);
457 string_list_clear(¶ms, 0);
460 } else if (!strtoul_ui(s, 10, &width))
462 else if ((position = parse_align_position(s)) >= 0)
463 align->position = position;
465 strbuf_addf(err, _("unrecognized %%(align) argument: %s"), s);
466 string_list_clear(¶ms, 0);
472 string_list_clear(¶ms, 0);
473 return strbuf_addf_ret(err, -1, _("positive width expected with the %%(align) atom"));
475 align->width = width;
476 string_list_clear(¶ms, 0);
480 static int if_atom_parser(const struct ref_format *format, struct used_atom *atom,
481 const char *arg, struct strbuf *err)
484 atom->u.if_then_else.cmp_status = COMPARE_NONE;
486 } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) {
487 atom->u.if_then_else.cmp_status = COMPARE_EQUAL;
488 } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) {
489 atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL;
491 return strbuf_addf_ret(err, -1, _("unrecognized %%(if) argument: %s"), arg);
495 static int head_atom_parser(const struct ref_format *format, struct used_atom *atom,
496 const char *arg, struct strbuf *unused_err)
498 atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
506 int (*parser)(const struct ref_format *format, struct used_atom *atom,
507 const char *arg, struct strbuf *err);
509 { "refname", SOURCE_NONE, FIELD_STR, refname_atom_parser },
510 { "objecttype", SOURCE_OTHER, FIELD_STR, objecttype_atom_parser },
511 { "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser },
512 { "objectname", SOURCE_OTHER, FIELD_STR, oid_atom_parser },
513 { "deltabase", SOURCE_OTHER, FIELD_STR, deltabase_atom_parser },
514 { "tree", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
515 { "parent", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
516 { "numparent", SOURCE_OBJ, FIELD_ULONG },
517 { "object", SOURCE_OBJ },
518 { "type", SOURCE_OBJ },
519 { "tag", SOURCE_OBJ },
520 { "author", SOURCE_OBJ },
521 { "authorname", SOURCE_OBJ },
522 { "authoremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
523 { "authordate", SOURCE_OBJ, FIELD_TIME },
524 { "committer", SOURCE_OBJ },
525 { "committername", SOURCE_OBJ },
526 { "committeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
527 { "committerdate", SOURCE_OBJ, FIELD_TIME },
528 { "tagger", SOURCE_OBJ },
529 { "taggername", SOURCE_OBJ },
530 { "taggeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
531 { "taggerdate", SOURCE_OBJ, FIELD_TIME },
532 { "creator", SOURCE_OBJ },
533 { "creatordate", SOURCE_OBJ, FIELD_TIME },
534 { "subject", SOURCE_OBJ, FIELD_STR, subject_atom_parser },
535 { "body", SOURCE_OBJ, FIELD_STR, body_atom_parser },
536 { "trailers", SOURCE_OBJ, FIELD_STR, trailers_atom_parser },
537 { "contents", SOURCE_OBJ, FIELD_STR, contents_atom_parser },
538 { "upstream", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
539 { "push", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
540 { "symref", SOURCE_NONE, FIELD_STR, refname_atom_parser },
541 { "flag", SOURCE_NONE },
542 { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser },
543 { "color", SOURCE_NONE, FIELD_STR, color_atom_parser },
544 { "worktreepath", SOURCE_NONE },
545 { "align", SOURCE_NONE, FIELD_STR, align_atom_parser },
546 { "end", SOURCE_NONE },
547 { "if", SOURCE_NONE, FIELD_STR, if_atom_parser },
548 { "then", SOURCE_NONE },
549 { "else", SOURCE_NONE },
551 * Please update $__git_ref_fieldlist in git-completion.bash
552 * when you add new atoms
556 #define REF_FORMATTING_STATE_INIT { 0, NULL }
558 struct ref_formatting_stack {
559 struct ref_formatting_stack *prev;
560 struct strbuf output;
561 void (*at_end)(struct ref_formatting_stack **stack);
565 struct ref_formatting_state {
567 struct ref_formatting_stack *stack;
572 int (*handler)(struct atom_value *atomv, struct ref_formatting_state *state,
574 uintmax_t value; /* used for sorting when not FIELD_STR */
575 struct used_atom *atom;
579 * Used to parse format string and sort specifiers
581 static int parse_ref_filter_atom(const struct ref_format *format,
582 const char *atom, const char *ep,
590 if (*sp == '*' && sp < ep)
593 return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"),
594 (int)(ep-atom), atom);
596 /* Do we have the atom already used elsewhere? */
597 for (i = 0; i < used_atom_cnt; i++) {
598 int len = strlen(used_atom[i].name);
599 if (len == ep - atom && !memcmp(used_atom[i].name, atom, len))
604 * If the atom name has a colon, strip it and everything after
605 * it off - it specifies the format for this entry, and
606 * shouldn't be used for checking against the valid_atom
609 arg = memchr(sp, ':', ep - sp);
610 atom_len = (arg ? arg : ep) - sp;
612 /* Is the atom a valid one? */
613 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
614 int len = strlen(valid_atom[i].name);
615 if (len == atom_len && !memcmp(valid_atom[i].name, sp, len))
619 if (ARRAY_SIZE(valid_atom) <= i)
620 return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"),
621 (int)(ep-atom), atom);
622 if (valid_atom[i].source != SOURCE_NONE && !have_git_dir())
623 return strbuf_addf_ret(err, -1,
624 _("not a git repository, but the field '%.*s' requires access to object data"),
625 (int)(ep-atom), atom);
627 /* Add it in, including the deref prefix */
630 REALLOC_ARRAY(used_atom, used_atom_cnt);
631 used_atom[at].name = xmemdupz(atom, ep - atom);
632 used_atom[at].type = valid_atom[i].cmp_type;
633 used_atom[at].source = valid_atom[i].source;
634 if (used_atom[at].source == SOURCE_OBJ) {
636 oi_deref.info.contentp = &oi_deref.content;
638 oi.info.contentp = &oi.content;
641 arg = used_atom[at].name + (arg - atom) + 1;
644 * Treat empty sub-arguments list as NULL (i.e.,
645 * "%(atom:)" is equivalent to "%(atom)").
650 memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
651 if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err))
655 if (!strcmp(valid_atom[i].name, "symref"))
660 static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
662 switch (quote_style) {
664 strbuf_addstr(s, str);
667 sq_quote_buf(s, str);
670 perl_quote_buf(s, str);
673 python_quote_buf(s, str);
676 tcl_quote_buf(s, str);
681 static int append_atom(struct atom_value *v, struct ref_formatting_state *state,
682 struct strbuf *unused_err)
685 * Quote formatting is only done when the stack has a single
686 * element. Otherwise quote formatting is done on the
687 * element's entire output strbuf when the %(end) atom is
690 if (!state->stack->prev)
691 quote_formatting(&state->stack->output, v->s, state->quote_style);
693 strbuf_addstr(&state->stack->output, v->s);
697 static void push_stack_element(struct ref_formatting_stack **stack)
699 struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
701 strbuf_init(&s->output, 0);
706 static void pop_stack_element(struct ref_formatting_stack **stack)
708 struct ref_formatting_stack *current = *stack;
709 struct ref_formatting_stack *prev = current->prev;
712 strbuf_addbuf(&prev->output, ¤t->output);
713 strbuf_release(¤t->output);
718 static void end_align_handler(struct ref_formatting_stack **stack)
720 struct ref_formatting_stack *cur = *stack;
721 struct align *align = (struct align *)cur->at_end_data;
722 struct strbuf s = STRBUF_INIT;
724 strbuf_utf8_align(&s, align->position, align->width, cur->output.buf);
725 strbuf_swap(&cur->output, &s);
729 static int align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
730 struct strbuf *unused_err)
732 struct ref_formatting_stack *new_stack;
734 push_stack_element(&state->stack);
735 new_stack = state->stack;
736 new_stack->at_end = end_align_handler;
737 new_stack->at_end_data = &atomv->atom->u.align;
741 static void if_then_else_handler(struct ref_formatting_stack **stack)
743 struct ref_formatting_stack *cur = *stack;
744 struct ref_formatting_stack *prev = cur->prev;
745 struct if_then_else *if_then_else = (struct if_then_else *)cur->at_end_data;
747 if (!if_then_else->then_atom_seen)
748 die(_("format: %%(if) atom used without a %%(then) atom"));
750 if (if_then_else->else_atom_seen) {
752 * There is an %(else) atom: we need to drop one state from the
753 * stack, either the %(else) branch if the condition is satisfied, or
754 * the %(then) branch if it isn't.
756 if (if_then_else->condition_satisfied) {
757 strbuf_reset(&cur->output);
758 pop_stack_element(&cur);
760 strbuf_swap(&cur->output, &prev->output);
761 strbuf_reset(&cur->output);
762 pop_stack_element(&cur);
764 } else if (!if_then_else->condition_satisfied) {
766 * No %(else) atom: just drop the %(then) branch if the
767 * condition is not satisfied.
769 strbuf_reset(&cur->output);
776 static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
777 struct strbuf *unused_err)
779 struct ref_formatting_stack *new_stack;
780 struct if_then_else *if_then_else = xcalloc(1,
781 sizeof(struct if_then_else));
783 if_then_else->str = atomv->atom->u.if_then_else.str;
784 if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status;
786 push_stack_element(&state->stack);
787 new_stack = state->stack;
788 new_stack->at_end = if_then_else_handler;
789 new_stack->at_end_data = if_then_else;
793 static int is_empty(const char *s)
803 static int then_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
806 struct ref_formatting_stack *cur = state->stack;
807 struct if_then_else *if_then_else = NULL;
809 if (cur->at_end == if_then_else_handler)
810 if_then_else = (struct if_then_else *)cur->at_end_data;
812 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used without an %%(if) atom"));
813 if (if_then_else->then_atom_seen)
814 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used more than once"));
815 if (if_then_else->else_atom_seen)
816 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used after %%(else)"));
817 if_then_else->then_atom_seen = 1;
819 * If the 'equals' or 'notequals' attribute is used then
820 * perform the required comparison. If not, only non-empty
821 * strings satisfy the 'if' condition.
823 if (if_then_else->cmp_status == COMPARE_EQUAL) {
824 if (!strcmp(if_then_else->str, cur->output.buf))
825 if_then_else->condition_satisfied = 1;
826 } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) {
827 if (strcmp(if_then_else->str, cur->output.buf))
828 if_then_else->condition_satisfied = 1;
829 } else if (cur->output.len && !is_empty(cur->output.buf))
830 if_then_else->condition_satisfied = 1;
831 strbuf_reset(&cur->output);
835 static int else_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
838 struct ref_formatting_stack *prev = state->stack;
839 struct if_then_else *if_then_else = NULL;
841 if (prev->at_end == if_then_else_handler)
842 if_then_else = (struct if_then_else *)prev->at_end_data;
844 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without an %%(if) atom"));
845 if (!if_then_else->then_atom_seen)
846 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without a %%(then) atom"));
847 if (if_then_else->else_atom_seen)
848 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used more than once"));
849 if_then_else->else_atom_seen = 1;
850 push_stack_element(&state->stack);
851 state->stack->at_end_data = prev->at_end_data;
852 state->stack->at_end = prev->at_end;
856 static int end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
859 struct ref_formatting_stack *current = state->stack;
860 struct strbuf s = STRBUF_INIT;
862 if (!current->at_end)
863 return strbuf_addf_ret(err, -1, _("format: %%(end) atom used without corresponding atom"));
864 current->at_end(&state->stack);
866 /* Stack may have been popped within at_end(), hence reset the current pointer */
867 current = state->stack;
870 * Perform quote formatting when the stack element is that of
871 * a supporting atom. If nested then perform quote formatting
872 * only on the topmost supporting atom.
874 if (!current->prev->prev) {
875 quote_formatting(&s, current->output.buf, state->quote_style);
876 strbuf_swap(¤t->output, &s);
879 pop_stack_element(&state->stack);
884 * In a format string, find the next occurrence of %(atom).
886 static const char *find_next(const char *cp)
891 * %( is the start of an atom;
892 * %% is a quoted per-cent.
896 else if (cp[1] == '%')
897 cp++; /* skip over two % */
898 /* otherwise this is a singleton, literal % */
906 * Make sure the format string is well formed, and parse out
909 int verify_ref_format(struct ref_format *format)
913 format->need_color_reset_at_eol = 0;
914 for (cp = format->format; *cp && (sp = find_next(cp)); ) {
915 struct strbuf err = STRBUF_INIT;
916 const char *color, *ep = strchr(sp, ')');
920 return error(_("malformed format string %s"), sp);
921 /* sp points at "%(" and ep points at the closing ")" */
922 at = parse_ref_filter_atom(format, sp + 2, ep, &err);
927 if (skip_prefix(used_atom[at].name, "color:", &color))
928 format->need_color_reset_at_eol = !!strcmp(color, "reset");
929 strbuf_release(&err);
931 if (format->need_color_reset_at_eol && !want_color(format->use_color))
932 format->need_color_reset_at_eol = 0;
936 static const char *do_grab_oid(const char *field, const struct object_id *oid,
937 struct used_atom *atom)
939 switch (atom->u.oid.option) {
941 return oid_to_hex(oid);
943 return find_unique_abbrev(oid, atom->u.oid.length);
945 return find_unique_abbrev(oid, DEFAULT_ABBREV);
947 BUG("unknown %%(%s) option", field);
951 static int grab_oid(const char *name, const char *field, const struct object_id *oid,
952 struct atom_value *v, struct used_atom *atom)
954 if (starts_with(name, field)) {
955 v->s = xstrdup(do_grab_oid(field, oid, atom));
961 /* See grab_values */
962 static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi)
966 for (i = 0; i < used_atom_cnt; i++) {
967 const char *name = used_atom[i].name;
968 struct atom_value *v = &val[i];
969 if (!!deref != (*name == '*'))
973 if (!strcmp(name, "objecttype"))
974 v->s = xstrdup(type_name(oi->type));
975 else if (starts_with(name, "objectsize")) {
976 if (used_atom[i].u.objectsize.option == O_SIZE_DISK) {
977 v->value = oi->disk_size;
978 v->s = xstrfmt("%"PRIuMAX, (uintmax_t)oi->disk_size);
979 } else if (used_atom[i].u.objectsize.option == O_SIZE) {
981 v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size);
983 } else if (!strcmp(name, "deltabase"))
984 v->s = xstrdup(oid_to_hex(&oi->delta_base_oid));
986 grab_oid(name, "objectname", &oi->oid, v, &used_atom[i]);
990 /* See grab_values */
991 static void grab_tag_values(struct atom_value *val, int deref, struct object *obj)
994 struct tag *tag = (struct tag *) obj;
996 for (i = 0; i < used_atom_cnt; i++) {
997 const char *name = used_atom[i].name;
998 struct atom_value *v = &val[i];
999 if (!!deref != (*name == '*'))
1003 if (!strcmp(name, "tag"))
1004 v->s = xstrdup(tag->tag);
1005 else if (!strcmp(name, "type") && tag->tagged)
1006 v->s = xstrdup(type_name(tag->tagged->type));
1007 else if (!strcmp(name, "object") && tag->tagged)
1008 v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
1012 /* See grab_values */
1013 static void grab_commit_values(struct atom_value *val, int deref, struct object *obj)
1016 struct commit *commit = (struct commit *) obj;
1018 for (i = 0; i < used_atom_cnt; i++) {
1019 const char *name = used_atom[i].name;
1020 struct atom_value *v = &val[i];
1021 if (!!deref != (*name == '*'))
1025 if (grab_oid(name, "tree", get_commit_tree_oid(commit), v, &used_atom[i]))
1027 if (!strcmp(name, "numparent")) {
1028 v->value = commit_list_count(commit->parents);
1029 v->s = xstrfmt("%lu", (unsigned long)v->value);
1031 else if (starts_with(name, "parent")) {
1032 struct commit_list *parents;
1033 struct strbuf s = STRBUF_INIT;
1034 for (parents = commit->parents; parents; parents = parents->next) {
1035 struct object_id *oid = &parents->item->object.oid;
1036 if (parents != commit->parents)
1037 strbuf_addch(&s, ' ');
1038 strbuf_addstr(&s, do_grab_oid("parent", oid, &used_atom[i]));
1040 v->s = strbuf_detach(&s, NULL);
1045 static const char *find_wholine(const char *who, int wholen, const char *buf)
1049 if (!strncmp(buf, who, wholen) &&
1051 return buf + wholen + 1;
1052 eol = strchr(buf, '\n');
1057 return ""; /* end of header */
1063 static const char *copy_line(const char *buf)
1065 const char *eol = strchrnul(buf, '\n');
1066 return xmemdupz(buf, eol - buf);
1069 static const char *copy_name(const char *buf)
1072 for (cp = buf; *cp && *cp != '\n'; cp++) {
1073 if (!strncmp(cp, " <", 2))
1074 return xmemdupz(buf, cp - buf);
1079 static const char *copy_email(const char *buf, struct used_atom *atom)
1081 const char *email = strchr(buf, '<');
1082 const char *eoemail;
1085 switch (atom->u.email_option.option) {
1087 eoemail = strchr(email, '>');
1093 eoemail = strchr(email, '>');
1097 eoemail = strchr(email, '@');
1099 eoemail = strchr(email, '>');
1102 BUG("unknown email option");
1107 return xmemdupz(email, eoemail - email);
1110 static char *copy_subject(const char *buf, unsigned long len)
1112 struct strbuf sb = STRBUF_INIT;
1115 for (i = 0; i < len; i++) {
1116 if (buf[i] == '\r' && i + 1 < len && buf[i + 1] == '\n')
1117 continue; /* ignore CR in CRLF */
1120 strbuf_addch(&sb, ' ');
1122 strbuf_addch(&sb, buf[i]);
1124 return strbuf_detach(&sb, NULL);
1127 static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
1129 const char *eoemail = strstr(buf, "> ");
1131 timestamp_t timestamp;
1133 struct date_mode date_mode = { DATE_NORMAL };
1134 const char *formatp;
1137 * We got here because atomname ends in "date" or "date<something>";
1138 * it's not possible that <something> is not ":<format>" because
1139 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1140 * ":" means no format is specified, and use the default.
1142 formatp = strchr(atomname, ':');
1143 if (formatp != NULL) {
1145 parse_date_format(formatp, &date_mode);
1150 timestamp = parse_timestamp(eoemail + 2, &zone, 10);
1151 if (timestamp == TIME_MAX)
1153 tz = strtol(zone, NULL, 10);
1154 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
1156 v->s = xstrdup(show_date(timestamp, tz, &date_mode));
1157 v->value = timestamp;
1164 /* See grab_values */
1165 static void grab_person(const char *who, struct atom_value *val, int deref, void *buf)
1168 int wholen = strlen(who);
1169 const char *wholine = NULL;
1171 for (i = 0; i < used_atom_cnt; i++) {
1172 const char *name = used_atom[i].name;
1173 struct atom_value *v = &val[i];
1174 if (!!deref != (*name == '*'))
1178 if (strncmp(who, name, wholen))
1180 if (name[wholen] != 0 &&
1181 strcmp(name + wholen, "name") &&
1182 !starts_with(name + wholen, "email") &&
1183 !starts_with(name + wholen, "date"))
1186 wholine = find_wholine(who, wholen, buf);
1188 return; /* no point looking for it */
1189 if (name[wholen] == 0)
1190 v->s = copy_line(wholine);
1191 else if (!strcmp(name + wholen, "name"))
1192 v->s = copy_name(wholine);
1193 else if (starts_with(name + wholen, "email"))
1194 v->s = copy_email(wholine, &used_atom[i]);
1195 else if (starts_with(name + wholen, "date"))
1196 grab_date(wholine, v, name);
1200 * For a tag or a commit object, if "creator" or "creatordate" is
1201 * requested, do something special.
1203 if (strcmp(who, "tagger") && strcmp(who, "committer"))
1204 return; /* "author" for commit object is not wanted */
1206 wholine = find_wholine(who, wholen, buf);
1209 for (i = 0; i < used_atom_cnt; i++) {
1210 const char *name = used_atom[i].name;
1211 struct atom_value *v = &val[i];
1212 if (!!deref != (*name == '*'))
1217 if (starts_with(name, "creatordate"))
1218 grab_date(wholine, v, name);
1219 else if (!strcmp(name, "creator"))
1220 v->s = copy_line(wholine);
1224 static void find_subpos(const char *buf,
1225 const char **sub, size_t *sublen,
1226 const char **body, size_t *bodylen,
1228 const char **sig, size_t *siglen)
1230 struct strbuf payload = STRBUF_INIT;
1231 struct strbuf signature = STRBUF_INIT;
1233 const char *end = buf + strlen(buf);
1234 const char *sigstart;
1236 /* parse signature first; we might not even have a subject line */
1237 parse_signature(buf, end - buf, &payload, &signature);
1239 /* skip past header until we hit empty line */
1240 while (*buf && *buf != '\n') {
1241 eol = strchrnul(buf, '\n');
1246 /* skip any empty lines */
1247 while (*buf == '\n')
1249 *sig = strbuf_detach(&signature, siglen);
1250 sigstart = buf + parse_signed_buffer(buf, strlen(buf));
1252 /* subject is first non-empty line */
1254 /* subject goes to first empty line before signature begins */
1255 if ((eol = strstr(*sub, "\n\n"))) {
1256 eol = eol < sigstart ? eol : sigstart;
1257 /* check if message uses CRLF */
1258 } else if (! (eol = strstr(*sub, "\r\n\r\n"))) {
1259 /* treat whole message as subject */
1260 eol = strrchr(*sub, '\0');
1263 *sublen = buf - *sub;
1264 /* drop trailing newline, if present */
1265 while (*sublen && ((*sub)[*sublen - 1] == '\n' ||
1266 (*sub)[*sublen - 1] == '\r'))
1269 /* skip any empty lines */
1270 while (*buf == '\n' || *buf == '\r')
1273 *bodylen = strlen(buf);
1274 *nonsiglen = sigstart - buf;
1278 * If 'lines' is greater than 0, append that many lines from the given
1279 * 'buf' of length 'size' to the given strbuf.
1281 static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
1284 const char *sp, *eol;
1289 for (i = 0; i < lines && sp < buf + size; i++) {
1291 strbuf_addstr(out, "\n ");
1292 eol = memchr(sp, '\n', size - (sp - buf));
1293 len = eol ? eol - sp : size - (sp - buf);
1294 strbuf_add(out, sp, len);
1301 /* See grab_values */
1302 static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
1305 const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
1306 size_t sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
1308 for (i = 0; i < used_atom_cnt; i++) {
1309 struct used_atom *atom = &used_atom[i];
1310 const char *name = atom->name;
1311 struct atom_value *v = &val[i];
1313 if (!!deref != (*name == '*'))
1317 if (strcmp(name, "body") &&
1318 !starts_with(name, "subject") &&
1319 !starts_with(name, "trailers") &&
1320 !starts_with(name, "contents"))
1325 &bodypos, &bodylen, &nonsiglen,
1328 if (atom->u.contents.option == C_SUB)
1329 v->s = copy_subject(subpos, sublen);
1330 else if (atom->u.contents.option == C_SUB_SANITIZE) {
1331 struct strbuf sb = STRBUF_INIT;
1332 format_sanitized_subject(&sb, subpos, sublen);
1333 v->s = strbuf_detach(&sb, NULL);
1334 } else if (atom->u.contents.option == C_BODY_DEP)
1335 v->s = xmemdupz(bodypos, bodylen);
1336 else if (atom->u.contents.option == C_LENGTH)
1337 v->s = xstrfmt("%"PRIuMAX, (uintmax_t)strlen(subpos));
1338 else if (atom->u.contents.option == C_BODY)
1339 v->s = xmemdupz(bodypos, nonsiglen);
1340 else if (atom->u.contents.option == C_SIG)
1341 v->s = xmemdupz(sigpos, siglen);
1342 else if (atom->u.contents.option == C_LINES) {
1343 struct strbuf s = STRBUF_INIT;
1344 const char *contents_end = bodypos + nonsiglen;
1346 /* Size is the length of the message after removing the signature */
1347 append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
1348 v->s = strbuf_detach(&s, NULL);
1349 } else if (atom->u.contents.option == C_TRAILERS) {
1350 struct strbuf s = STRBUF_INIT;
1352 /* Format the trailer info according to the trailer_opts given */
1353 format_trailers_from_commit(&s, subpos, &atom->u.contents.trailer_opts);
1355 v->s = strbuf_detach(&s, NULL);
1356 } else if (atom->u.contents.option == C_BARE)
1357 v->s = xstrdup(subpos);
1360 free((void *)sigpos);
1364 * We want to have empty print-string for field requests
1365 * that do not apply (e.g. "authordate" for a tag object)
1367 static void fill_missing_values(struct atom_value *val)
1370 for (i = 0; i < used_atom_cnt; i++) {
1371 struct atom_value *v = &val[i];
1378 * val is a list of atom_value to hold returned values. Extract
1379 * the values for atoms in used_atom array out of (obj, buf, sz).
1380 * when deref is false, (obj, buf, sz) is the object that is
1381 * pointed at by the ref itself; otherwise it is the object the
1382 * ref (which is a tag) refers to.
1384 static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf)
1386 switch (obj->type) {
1388 grab_tag_values(val, deref, obj);
1389 grab_sub_body_contents(val, deref, buf);
1390 grab_person("tagger", val, deref, buf);
1393 grab_commit_values(val, deref, obj);
1394 grab_sub_body_contents(val, deref, buf);
1395 grab_person("author", val, deref, buf);
1396 grab_person("committer", val, deref, buf);
1399 /* grab_tree_values(val, deref, obj, buf, sz); */
1402 /* grab_blob_values(val, deref, obj, buf, sz); */
1405 die("Eh? Object of type %d?", obj->type);
1409 static inline char *copy_advance(char *dst, const char *src)
1416 static const char *lstrip_ref_components(const char *refname, int len)
1418 long remaining = len;
1419 const char *start = xstrdup(refname);
1420 const char *to_free = start;
1424 const char *p = refname;
1426 /* Find total no of '/' separated path-components */
1427 for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
1430 * The number of components we need to strip is now
1431 * the total minus the components to be left (Plus one
1432 * because we count the number of '/', but the number
1433 * of components is one more than the no of '/').
1435 remaining = i + len + 1;
1438 while (remaining > 0) {
1441 free((char *)to_free);
1449 start = xstrdup(start);
1450 free((char *)to_free);
1454 static const char *rstrip_ref_components(const char *refname, int len)
1456 long remaining = len;
1457 const char *start = xstrdup(refname);
1458 const char *to_free = start;
1462 const char *p = refname;
1464 /* Find total no of '/' separated path-components */
1465 for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
1468 * The number of components we need to strip is now
1469 * the total minus the components to be left (Plus one
1470 * because we count the number of '/', but the number
1471 * of components is one more than the no of '/').
1473 remaining = i + len + 1;
1476 while (remaining-- > 0) {
1477 char *p = strrchr(start, '/');
1479 free((char *)to_free);
1487 static const char *show_ref(struct refname_atom *atom, const char *refname)
1489 if (atom->option == R_SHORT)
1490 return shorten_unambiguous_ref(refname, warn_ambiguous_refs);
1491 else if (atom->option == R_LSTRIP)
1492 return lstrip_ref_components(refname, atom->lstrip);
1493 else if (atom->option == R_RSTRIP)
1494 return rstrip_ref_components(refname, atom->rstrip);
1496 return xstrdup(refname);
1499 static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
1500 struct branch *branch, const char **s)
1502 int num_ours, num_theirs;
1503 if (atom->u.remote_ref.option == RR_REF)
1504 *s = show_ref(&atom->u.remote_ref.refname, refname);
1505 else if (atom->u.remote_ref.option == RR_TRACK) {
1506 if (stat_tracking_info(branch, &num_ours, &num_theirs,
1507 NULL, atom->u.remote_ref.push,
1508 AHEAD_BEHIND_FULL) < 0) {
1509 *s = xstrdup(msgs.gone);
1510 } else if (!num_ours && !num_theirs)
1513 *s = xstrfmt(msgs.behind, num_theirs);
1514 else if (!num_theirs)
1515 *s = xstrfmt(msgs.ahead, num_ours);
1517 *s = xstrfmt(msgs.ahead_behind,
1518 num_ours, num_theirs);
1519 if (!atom->u.remote_ref.nobracket && *s[0]) {
1520 const char *to_free = *s;
1521 *s = xstrfmt("[%s]", *s);
1522 free((void *)to_free);
1524 } else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
1525 if (stat_tracking_info(branch, &num_ours, &num_theirs,
1526 NULL, atom->u.remote_ref.push,
1527 AHEAD_BEHIND_FULL) < 0) {
1531 if (!num_ours && !num_theirs)
1535 else if (!num_theirs)
1539 } else if (atom->u.remote_ref.option == RR_REMOTE_NAME) {
1541 const char *remote = atom->u.remote_ref.push ?
1542 pushremote_for_branch(branch, &explicit) :
1543 remote_for_branch(branch, &explicit);
1544 *s = xstrdup(explicit ? remote : "");
1545 } else if (atom->u.remote_ref.option == RR_REMOTE_REF) {
1548 merge = remote_ref_for_branch(branch, atom->u.remote_ref.push);
1549 *s = xstrdup(merge ? merge : "");
1551 BUG("unhandled RR_* enum");
1554 char *get_head_description(void)
1556 struct strbuf desc = STRBUF_INIT;
1557 struct wt_status_state state;
1558 memset(&state, 0, sizeof(state));
1559 wt_status_get_state(the_repository, &state, 1);
1560 if (state.rebase_in_progress ||
1561 state.rebase_interactive_in_progress) {
1563 strbuf_addf(&desc, _("(no branch, rebasing %s)"),
1566 strbuf_addf(&desc, _("(no branch, rebasing detached HEAD %s)"),
1567 state.detached_from);
1568 } else if (state.bisect_in_progress)
1569 strbuf_addf(&desc, _("(no branch, bisect started on %s)"),
1571 else if (state.detached_from) {
1572 if (state.detached_at)
1573 strbuf_addf(&desc, _("(HEAD detached at %s)"),
1574 state.detached_from);
1576 strbuf_addf(&desc, _("(HEAD detached from %s)"),
1577 state.detached_from);
1579 strbuf_addstr(&desc, _("(no branch)"));
1581 return strbuf_detach(&desc, NULL);
1584 static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref)
1589 return show_ref(&atom->u.refname, ref->symref);
1592 static const char *get_refname(struct used_atom *atom, struct ref_array_item *ref)
1594 if (ref->kind & FILTER_REFS_DETACHED_HEAD)
1595 return get_head_description();
1596 return show_ref(&atom->u.refname, ref->refname);
1599 static int get_object(struct ref_array_item *ref, int deref, struct object **obj,
1600 struct expand_data *oi, struct strbuf *err)
1602 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
1604 if (oi->info.contentp) {
1605 /* We need to know that to use parse_object_buffer properly */
1606 oi->info.sizep = &oi->size;
1607 oi->info.typep = &oi->type;
1609 if (oid_object_info_extended(the_repository, &oi->oid, &oi->info,
1610 OBJECT_INFO_LOOKUP_REPLACE))
1611 return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
1612 oid_to_hex(&oi->oid), ref->refname);
1613 if (oi->info.disk_sizep && oi->disk_size < 0)
1614 BUG("Object size is less than zero.");
1616 if (oi->info.contentp) {
1617 *obj = parse_object_buffer(the_repository, &oi->oid, oi->type, oi->size, oi->content, &eaten);
1621 return strbuf_addf_ret(err, -1, _("parse_object_buffer failed on %s for %s"),
1622 oid_to_hex(&oi->oid), ref->refname);
1624 grab_values(ref->value, deref, *obj, oi->content);
1627 grab_common_values(ref->value, deref, oi);
1633 static void populate_worktree_map(struct hashmap *map, struct worktree **worktrees)
1637 for (i = 0; worktrees[i]; i++) {
1638 if (worktrees[i]->head_ref) {
1639 struct ref_to_worktree_entry *entry;
1640 entry = xmalloc(sizeof(*entry));
1641 entry->wt = worktrees[i];
1642 hashmap_entry_init(&entry->ent,
1643 strhash(worktrees[i]->head_ref));
1645 hashmap_add(map, &entry->ent);
1650 static void lazy_init_worktree_map(void)
1652 if (ref_to_worktree_map.worktrees)
1655 ref_to_worktree_map.worktrees = get_worktrees();
1656 hashmap_init(&(ref_to_worktree_map.map), ref_to_worktree_map_cmpfnc, NULL, 0);
1657 populate_worktree_map(&(ref_to_worktree_map.map), ref_to_worktree_map.worktrees);
1660 static char *get_worktree_path(const struct used_atom *atom, const struct ref_array_item *ref)
1662 struct hashmap_entry entry, *e;
1663 struct ref_to_worktree_entry *lookup_result;
1665 lazy_init_worktree_map();
1667 hashmap_entry_init(&entry, strhash(ref->refname));
1668 e = hashmap_get(&(ref_to_worktree_map.map), &entry, ref->refname);
1673 lookup_result = container_of(e, struct ref_to_worktree_entry, ent);
1675 return xstrdup(lookup_result->wt->path);
1679 * Parse the object referred by ref, and grab needed value.
1681 static int populate_value(struct ref_array_item *ref, struct strbuf *err)
1685 struct object_info empty = OBJECT_INFO_INIT;
1687 CALLOC_ARRAY(ref->value, used_atom_cnt);
1689 if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
1690 ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
1693 ref->symref = xstrdup("");
1696 /* Fill in specials first */
1697 for (i = 0; i < used_atom_cnt; i++) {
1698 struct used_atom *atom = &used_atom[i];
1699 const char *name = used_atom[i].name;
1700 struct atom_value *v = &ref->value[i];
1702 const char *refname;
1703 struct branch *branch = NULL;
1705 v->handler = append_atom;
1713 if (starts_with(name, "refname"))
1714 refname = get_refname(atom, ref);
1715 else if (!strcmp(name, "worktreepath")) {
1716 if (ref->kind == FILTER_REFS_BRANCHES)
1717 v->s = get_worktree_path(atom, ref);
1722 else if (starts_with(name, "symref"))
1723 refname = get_symref(atom, ref);
1724 else if (starts_with(name, "upstream")) {
1725 const char *branch_name;
1726 /* only local branches may have an upstream */
1727 if (!skip_prefix(ref->refname, "refs/heads/",
1732 branch = branch_get(branch_name);
1734 refname = branch_get_upstream(branch, NULL);
1736 fill_remote_ref_details(atom, refname, branch, &v->s);
1740 } else if (atom->u.remote_ref.push) {
1741 const char *branch_name;
1743 if (!skip_prefix(ref->refname, "refs/heads/",
1746 branch = branch_get(branch_name);
1748 if (atom->u.remote_ref.push_remote)
1751 refname = branch_get_push(branch, NULL);
1755 /* We will definitely re-init v->s on the next line. */
1757 fill_remote_ref_details(atom, refname, branch, &v->s);
1759 } else if (starts_with(name, "color:")) {
1760 v->s = xstrdup(atom->u.color);
1762 } else if (!strcmp(name, "flag")) {
1763 char buf[256], *cp = buf;
1764 if (ref->flag & REF_ISSYMREF)
1765 cp = copy_advance(cp, ",symref");
1766 if (ref->flag & REF_ISPACKED)
1767 cp = copy_advance(cp, ",packed");
1772 v->s = xstrdup(buf + 1);
1775 } else if (!deref && grab_oid(name, "objectname", &ref->objectname, v, atom)) {
1777 } else if (!strcmp(name, "HEAD")) {
1778 if (atom->u.head && !strcmp(ref->refname, atom->u.head))
1779 v->s = xstrdup("*");
1781 v->s = xstrdup(" ");
1783 } else if (starts_with(name, "align")) {
1784 v->handler = align_atom_handler;
1787 } else if (!strcmp(name, "end")) {
1788 v->handler = end_atom_handler;
1791 } else if (starts_with(name, "if")) {
1793 if (skip_prefix(name, "if:", &s))
1797 v->handler = if_atom_handler;
1799 } else if (!strcmp(name, "then")) {
1800 v->handler = then_atom_handler;
1803 } else if (!strcmp(name, "else")) {
1804 v->handler = else_atom_handler;
1811 v->s = xstrdup(refname);
1813 v->s = xstrfmt("%s^{}", refname);
1814 free((char *)refname);
1817 for (i = 0; i < used_atom_cnt; i++) {
1818 struct atom_value *v = &ref->value[i];
1819 if (v->s == NULL && used_atom[i].source == SOURCE_NONE)
1820 return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
1821 oid_to_hex(&ref->objectname), ref->refname);
1825 oi.info.contentp = &oi.content;
1826 if (!memcmp(&oi.info, &empty, sizeof(empty)) &&
1827 !memcmp(&oi_deref.info, &empty, sizeof(empty)))
1831 oi.oid = ref->objectname;
1832 if (get_object(ref, 0, &obj, &oi, err))
1836 * If there is no atom that wants to know about tagged
1837 * object, we are done.
1839 if (!need_tagged || (obj->type != OBJ_TAG))
1843 * If it is a tag object, see if we use a value that derefs
1844 * the object, and if we do grab the object it refers to.
1846 oi_deref.oid = *get_tagged_oid((struct tag *)obj);
1849 * NEEDSWORK: This derefs tag only once, which
1850 * is good to deal with chains of trust, but
1851 * is not consistent with what deref_tag() does
1852 * which peels the onion to the core.
1854 return get_object(ref, 1, &obj, &oi_deref, err);
1858 * Given a ref, return the value for the atom. This lazily gets value
1859 * out of the object by calling populate value.
1861 static int get_ref_atom_value(struct ref_array_item *ref, int atom,
1862 struct atom_value **v, struct strbuf *err)
1865 if (populate_value(ref, err))
1867 fill_missing_values(ref->value);
1869 *v = &ref->value[atom];
1874 * Return 1 if the refname matches one of the patterns, otherwise 0.
1875 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1876 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1877 * matches "refs/heads/mas*", too).
1879 static int match_pattern(const struct ref_filter *filter, const char *refname)
1881 const char **patterns = filter->name_patterns;
1884 if (filter->ignore_case)
1885 flags |= WM_CASEFOLD;
1888 * When no '--format' option is given we need to skip the prefix
1889 * for matching refs of tags and branches.
1891 (void)(skip_prefix(refname, "refs/tags/", &refname) ||
1892 skip_prefix(refname, "refs/heads/", &refname) ||
1893 skip_prefix(refname, "refs/remotes/", &refname) ||
1894 skip_prefix(refname, "refs/", &refname));
1896 for (; *patterns; patterns++) {
1897 if (!wildmatch(*patterns, refname, flags))
1904 * Return 1 if the refname matches one of the patterns, otherwise 0.
1905 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1906 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1907 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1909 static int match_name_as_path(const struct ref_filter *filter, const char *refname)
1911 const char **pattern = filter->name_patterns;
1912 int namelen = strlen(refname);
1913 unsigned flags = WM_PATHNAME;
1915 if (filter->ignore_case)
1916 flags |= WM_CASEFOLD;
1918 for (; *pattern; pattern++) {
1919 const char *p = *pattern;
1920 int plen = strlen(p);
1922 if ((plen <= namelen) &&
1923 !strncmp(refname, p, plen) &&
1924 (refname[plen] == '\0' ||
1925 refname[plen] == '/' ||
1928 if (!wildmatch(p, refname, flags))
1934 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1935 static int filter_pattern_match(struct ref_filter *filter, const char *refname)
1937 if (!*filter->name_patterns)
1938 return 1; /* No pattern always matches */
1939 if (filter->match_as_path)
1940 return match_name_as_path(filter, refname);
1941 return match_pattern(filter, refname);
1945 * This is the same as for_each_fullref_in(), but it tries to iterate
1946 * only over the patterns we'll care about. Note that it _doesn't_ do a full
1947 * pattern match, so the callback still has to match each ref individually.
1949 static int for_each_fullref_in_pattern(struct ref_filter *filter,
1954 if (!filter->match_as_path) {
1956 * in this case, the patterns are applied after
1957 * prefixes like "refs/heads/" etc. are stripped off,
1958 * so we have to look at everything:
1960 return for_each_fullref_in("", cb, cb_data, broken);
1963 if (filter->ignore_case) {
1965 * we can't handle case-insensitive comparisons,
1966 * so just return everything and let the caller
1969 return for_each_fullref_in("", cb, cb_data, broken);
1972 if (!filter->name_patterns[0]) {
1973 /* no patterns; we have to look at everything */
1974 return for_each_fullref_in("", cb, cb_data, broken);
1977 return for_each_fullref_in_prefixes(NULL, filter->name_patterns,
1978 cb, cb_data, broken);
1982 * Given a ref (oid, refname), check if the ref belongs to the array
1983 * of oids. If the given ref is a tag, check if the given tag points
1984 * at one of the oids in the given oid array.
1986 * 1. Only a single level of indirection is obtained, we might want to
1987 * change this to account for multiple levels (e.g. annotated tags
1988 * pointing to annotated tags pointing to a commit.)
1989 * 2. As the refs are cached we might know what refname peels to without
1990 * the need to parse the object via parse_object(). peel_ref() might be a
1991 * more efficient alternative to obtain the pointee.
1993 static const struct object_id *match_points_at(struct oid_array *points_at,
1994 const struct object_id *oid,
1995 const char *refname)
1997 const struct object_id *tagged_oid = NULL;
2000 if (oid_array_lookup(points_at, oid) >= 0)
2002 obj = parse_object(the_repository, oid);
2004 die(_("malformed object at '%s'"), refname);
2005 if (obj->type == OBJ_TAG)
2006 tagged_oid = get_tagged_oid((struct tag *)obj);
2007 if (tagged_oid && oid_array_lookup(points_at, tagged_oid) >= 0)
2013 * Allocate space for a new ref_array_item and copy the name and oid to it.
2015 * Callers can then fill in other struct members at their leisure.
2017 static struct ref_array_item *new_ref_array_item(const char *refname,
2018 const struct object_id *oid)
2020 struct ref_array_item *ref;
2022 FLEX_ALLOC_STR(ref, refname, refname);
2023 oidcpy(&ref->objectname, oid);
2028 struct ref_array_item *ref_array_push(struct ref_array *array,
2029 const char *refname,
2030 const struct object_id *oid)
2032 struct ref_array_item *ref = new_ref_array_item(refname, oid);
2034 ALLOC_GROW(array->items, array->nr + 1, array->alloc);
2035 array->items[array->nr++] = ref;
2040 static int ref_kind_from_refname(const char *refname)
2048 { "refs/heads/" , FILTER_REFS_BRANCHES },
2049 { "refs/remotes/" , FILTER_REFS_REMOTES },
2050 { "refs/tags/", FILTER_REFS_TAGS}
2053 if (!strcmp(refname, "HEAD"))
2054 return FILTER_REFS_DETACHED_HEAD;
2056 for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
2057 if (starts_with(refname, ref_kind[i].prefix))
2058 return ref_kind[i].kind;
2061 return FILTER_REFS_OTHERS;
2064 static int filter_ref_kind(struct ref_filter *filter, const char *refname)
2066 if (filter->kind == FILTER_REFS_BRANCHES ||
2067 filter->kind == FILTER_REFS_REMOTES ||
2068 filter->kind == FILTER_REFS_TAGS)
2069 return filter->kind;
2070 return ref_kind_from_refname(refname);
2073 struct ref_filter_cbdata {
2074 struct ref_array *array;
2075 struct ref_filter *filter;
2076 struct contains_cache contains_cache;
2077 struct contains_cache no_contains_cache;
2081 * A call-back given to for_each_ref(). Filter refs and keep them for
2082 * later object processing.
2084 static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
2086 struct ref_filter_cbdata *ref_cbdata = cb_data;
2087 struct ref_filter *filter = ref_cbdata->filter;
2088 struct ref_array_item *ref;
2089 struct commit *commit = NULL;
2092 if (flag & REF_BAD_NAME) {
2093 warning(_("ignoring ref with broken name %s"), refname);
2097 if (flag & REF_ISBROKEN) {
2098 warning(_("ignoring broken ref %s"), refname);
2102 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2103 kind = filter_ref_kind(filter, refname);
2104 if (!(kind & filter->kind))
2107 if (!filter_pattern_match(filter, refname))
2110 if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname))
2114 * A merge filter is applied on refs pointing to commits. Hence
2115 * obtain the commit using the 'oid' available and discard all
2116 * non-commits early. The actual filtering is done later.
2118 if (filter->reachable_from || filter->unreachable_from ||
2119 filter->with_commit || filter->no_commit || filter->verbose) {
2120 commit = lookup_commit_reference_gently(the_repository, oid, 1);
2123 /* We perform the filtering for the '--contains' option... */
2124 if (filter->with_commit &&
2125 !commit_contains(filter, commit, filter->with_commit, &ref_cbdata->contains_cache))
2127 /* ...or for the `--no-contains' option */
2128 if (filter->no_commit &&
2129 commit_contains(filter, commit, filter->no_commit, &ref_cbdata->no_contains_cache))
2134 * We do not open the object yet; sort may only need refname
2135 * to do its job and the resulting list may yet to be pruned
2136 * by maxcount logic.
2138 ref = ref_array_push(ref_cbdata->array, refname, oid);
2139 ref->commit = commit;
2146 /* Free memory allocated for a ref_array_item */
2147 static void free_array_item(struct ref_array_item *item)
2149 free((char *)item->symref);
2152 for (i = 0; i < used_atom_cnt; i++)
2153 free((char *)item->value[i].s);
2159 /* Free all memory allocated for ref_array */
2160 void ref_array_clear(struct ref_array *array)
2164 for (i = 0; i < array->nr; i++)
2165 free_array_item(array->items[i]);
2166 FREE_AND_NULL(array->items);
2167 array->nr = array->alloc = 0;
2169 for (i = 0; i < used_atom_cnt; i++)
2170 free((char *)used_atom[i].name);
2171 FREE_AND_NULL(used_atom);
2174 if (ref_to_worktree_map.worktrees) {
2175 hashmap_clear_and_free(&(ref_to_worktree_map.map),
2176 struct ref_to_worktree_entry, ent);
2177 free_worktrees(ref_to_worktree_map.worktrees);
2178 ref_to_worktree_map.worktrees = NULL;
2182 #define EXCLUDE_REACHED 0
2183 #define INCLUDE_REACHED 1
2184 static void reach_filter(struct ref_array *array,
2185 struct commit_list *check_reachable,
2186 int include_reached)
2188 struct rev_info revs;
2190 struct commit **to_clear;
2191 struct commit_list *cr;
2193 if (!check_reachable)
2196 CALLOC_ARRAY(to_clear, array->nr);
2198 repo_init_revisions(the_repository, &revs, NULL);
2200 for (i = 0; i < array->nr; i++) {
2201 struct ref_array_item *item = array->items[i];
2202 add_pending_object(&revs, &item->commit->object, item->refname);
2203 to_clear[i] = item->commit;
2206 for (cr = check_reachable; cr; cr = cr->next) {
2207 struct commit *merge_commit = cr->item;
2208 merge_commit->object.flags |= UNINTERESTING;
2209 add_pending_object(&revs, &merge_commit->object, "");
2213 if (prepare_revision_walk(&revs))
2214 die(_("revision walk setup failed"));
2219 for (i = 0; i < old_nr; i++) {
2220 struct ref_array_item *item = array->items[i];
2221 struct commit *commit = item->commit;
2223 int is_merged = !!(commit->object.flags & UNINTERESTING);
2225 if (is_merged == include_reached)
2226 array->items[array->nr++] = array->items[i];
2228 free_array_item(item);
2231 clear_commit_marks_many(old_nr, to_clear, ALL_REV_FLAGS);
2233 while (check_reachable) {
2234 struct commit *merge_commit = pop_commit(&check_reachable);
2235 clear_commit_marks(merge_commit, ALL_REV_FLAGS);
2242 * API for filtering a set of refs. Based on the type of refs the user
2243 * has requested, we iterate through those refs and apply filters
2244 * as per the given ref_filter structure and finally store the
2245 * filtered refs in the ref_array structure.
2247 int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
2249 struct ref_filter_cbdata ref_cbdata;
2251 unsigned int broken = 0;
2253 ref_cbdata.array = array;
2254 ref_cbdata.filter = filter;
2256 if (type & FILTER_REFS_INCLUDE_BROKEN)
2258 filter->kind = type & FILTER_REFS_KIND_MASK;
2260 init_contains_cache(&ref_cbdata.contains_cache);
2261 init_contains_cache(&ref_cbdata.no_contains_cache);
2263 /* Simple per-ref filtering */
2265 die("filter_refs: invalid type");
2268 * For common cases where we need only branches or remotes or tags,
2269 * we only iterate through those refs. If a mix of refs is needed,
2270 * we iterate over all refs and filter out required refs with the help
2271 * of filter_ref_kind().
2273 if (filter->kind == FILTER_REFS_BRANCHES)
2274 ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken);
2275 else if (filter->kind == FILTER_REFS_REMOTES)
2276 ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken);
2277 else if (filter->kind == FILTER_REFS_TAGS)
2278 ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken);
2279 else if (filter->kind & FILTER_REFS_ALL)
2280 ret = for_each_fullref_in_pattern(filter, ref_filter_handler, &ref_cbdata, broken);
2281 if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
2282 head_ref(ref_filter_handler, &ref_cbdata);
2285 clear_contains_cache(&ref_cbdata.contains_cache);
2286 clear_contains_cache(&ref_cbdata.no_contains_cache);
2288 /* Filters that need revision walking */
2289 reach_filter(array, filter->reachable_from, INCLUDE_REACHED);
2290 reach_filter(array, filter->unreachable_from, EXCLUDE_REACHED);
2295 static int compare_detached_head(struct ref_array_item *a, struct ref_array_item *b)
2297 if (!(a->kind ^ b->kind))
2298 BUG("ref_kind_from_refname() should only mark one ref as HEAD");
2299 if (a->kind & FILTER_REFS_DETACHED_HEAD)
2301 else if (b->kind & FILTER_REFS_DETACHED_HEAD)
2303 BUG("should have died in the xor check above");
2307 static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
2309 struct atom_value *va, *vb;
2311 int cmp_detached_head = 0;
2312 cmp_type cmp_type = used_atom[s->atom].type;
2313 struct strbuf err = STRBUF_INIT;
2315 if (get_ref_atom_value(a, s->atom, &va, &err))
2317 if (get_ref_atom_value(b, s->atom, &vb, &err))
2319 strbuf_release(&err);
2320 if (s->sort_flags & REF_SORTING_DETACHED_HEAD_FIRST &&
2321 ((a->kind | b->kind) & FILTER_REFS_DETACHED_HEAD)) {
2322 cmp = compare_detached_head(a, b);
2323 cmp_detached_head = 1;
2324 } else if (s->sort_flags & REF_SORTING_VERSION) {
2325 cmp = versioncmp(va->s, vb->s);
2326 } else if (cmp_type == FIELD_STR) {
2327 int (*cmp_fn)(const char *, const char *);
2328 cmp_fn = s->sort_flags & REF_SORTING_ICASE
2329 ? strcasecmp : strcmp;
2330 cmp = cmp_fn(va->s, vb->s);
2332 if (va->value < vb->value)
2334 else if (va->value == vb->value)
2340 return (s->sort_flags & REF_SORTING_REVERSE && !cmp_detached_head)
2344 static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
2346 struct ref_array_item *a = *((struct ref_array_item **)a_);
2347 struct ref_array_item *b = *((struct ref_array_item **)b_);
2348 struct ref_sorting *s;
2350 for (s = ref_sorting; s; s = s->next) {
2351 int cmp = cmp_ref_sorting(s, a, b);
2356 return s && s->sort_flags & REF_SORTING_ICASE ?
2357 strcasecmp(a->refname, b->refname) :
2358 strcmp(a->refname, b->refname);
2361 void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting,
2362 unsigned int mask, int on)
2364 for (; sorting; sorting = sorting->next) {
2366 sorting->sort_flags |= mask;
2368 sorting->sort_flags &= ~mask;
2372 void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
2374 QSORT_S(array->items, array->nr, compare_refs, sorting);
2377 static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
2379 struct strbuf *s = &state->stack->output;
2381 while (*cp && (!ep || cp < ep)) {
2386 int ch = hex2chr(cp + 1);
2388 strbuf_addch(s, ch);
2394 strbuf_addch(s, *cp);
2399 int format_ref_array_item(struct ref_array_item *info,
2400 const struct ref_format *format,
2401 struct strbuf *final_buf,
2402 struct strbuf *error_buf)
2404 const char *cp, *sp, *ep;
2405 struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
2407 state.quote_style = format->quote_style;
2408 push_stack_element(&state.stack);
2410 for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) {
2411 struct atom_value *atomv;
2414 ep = strchr(sp, ')');
2416 append_literal(cp, sp, &state);
2417 pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf);
2418 if (pos < 0 || get_ref_atom_value(info, pos, &atomv, error_buf) ||
2419 atomv->handler(atomv, &state, error_buf)) {
2420 pop_stack_element(&state.stack);
2425 sp = cp + strlen(cp);
2426 append_literal(cp, sp, &state);
2428 if (format->need_color_reset_at_eol) {
2429 struct atom_value resetv;
2430 resetv.s = GIT_COLOR_RESET;
2431 if (append_atom(&resetv, &state, error_buf)) {
2432 pop_stack_element(&state.stack);
2436 if (state.stack->prev) {
2437 pop_stack_element(&state.stack);
2438 return strbuf_addf_ret(error_buf, -1, _("format: %%(end) atom missing"));
2440 strbuf_addbuf(final_buf, &state.stack->output);
2441 pop_stack_element(&state.stack);
2445 void pretty_print_ref(const char *name, const struct object_id *oid,
2446 const struct ref_format *format)
2448 struct ref_array_item *ref_item;
2449 struct strbuf output = STRBUF_INIT;
2450 struct strbuf err = STRBUF_INIT;
2452 ref_item = new_ref_array_item(name, oid);
2453 ref_item->kind = ref_kind_from_refname(name);
2454 if (format_ref_array_item(ref_item, format, &output, &err))
2456 fwrite(output.buf, 1, output.len, stdout);
2459 strbuf_release(&err);
2460 strbuf_release(&output);
2461 free_array_item(ref_item);
2464 static int parse_sorting_atom(const char *atom)
2467 * This parses an atom using a dummy ref_format, since we don't
2468 * actually care about the formatting details.
2470 struct ref_format dummy = REF_FORMAT_INIT;
2471 const char *end = atom + strlen(atom);
2472 struct strbuf err = STRBUF_INIT;
2473 int res = parse_ref_filter_atom(&dummy, atom, end, &err);
2476 strbuf_release(&err);
2480 /* If no sorting option is given, use refname to sort as default */
2481 struct ref_sorting *ref_default_sorting(void)
2483 static const char cstr_name[] = "refname";
2485 struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
2487 sorting->next = NULL;
2488 sorting->atom = parse_sorting_atom(cstr_name);
2492 void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
2494 struct ref_sorting *s;
2497 s->next = *sorting_tail;
2501 s->sort_flags |= REF_SORTING_REVERSE;
2504 if (skip_prefix(arg, "version:", &arg) ||
2505 skip_prefix(arg, "v:", &arg))
2506 s->sort_flags |= REF_SORTING_VERSION;
2507 s->atom = parse_sorting_atom(arg);
2510 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
2513 * NEEDSWORK: We should probably clear the list in this case, but we've
2514 * already munged the global used_atoms list, which would need to be
2517 BUG_ON_OPT_NEG(unset);
2519 parse_ref_sorting(opt->value, arg);
2523 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
2525 struct ref_filter *rf = opt->value;
2526 struct object_id oid;
2527 struct commit *merge_commit;
2529 BUG_ON_OPT_NEG(unset);
2531 if (get_oid(arg, &oid))
2532 die(_("malformed object name %s"), arg);
2534 merge_commit = lookup_commit_reference_gently(the_repository, &oid, 0);
2537 return error(_("option `%s' must point to a commit"), opt->long_name);
2539 if (starts_with(opt->long_name, "no"))
2540 commit_list_insert(merge_commit, &rf->unreachable_from);
2542 commit_list_insert(merge_commit, &rf->reachable_from);