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;
149 struct email_option {
150 enum { EO_RAW, EO_TRIM, EO_LOCALPART } option;
152 struct refname_atom refname;
156 static int used_atom_cnt, need_tagged, need_symref;
159 * Expand string, append it to strbuf *sb, then return error code ret.
160 * Allow to save few lines of code.
162 static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...)
166 strbuf_vaddf(sb, fmt, ap);
171 static int color_atom_parser(const struct ref_format *format, struct used_atom *atom,
172 const char *color_value, struct strbuf *err)
175 return strbuf_addf_ret(err, -1, _("expected format: %%(color:<color>)"));
176 if (color_parse(color_value, atom->u.color) < 0)
177 return strbuf_addf_ret(err, -1, _("unrecognized color: %%(color:%s)"),
180 * We check this after we've parsed the color, which lets us complain
181 * about syntactically bogus color names even if they won't be used.
183 if (!want_color(format->use_color))
184 color_parse("", atom->u.color);
188 static int refname_atom_parser_internal(struct refname_atom *atom, const char *arg,
189 const char *name, struct strbuf *err)
192 atom->option = R_NORMAL;
193 else if (!strcmp(arg, "short"))
194 atom->option = R_SHORT;
195 else if (skip_prefix(arg, "lstrip=", &arg) ||
196 skip_prefix(arg, "strip=", &arg)) {
197 atom->option = R_LSTRIP;
198 if (strtol_i(arg, 10, &atom->lstrip))
199 return strbuf_addf_ret(err, -1, _("Integer value expected refname:lstrip=%s"), arg);
200 } else if (skip_prefix(arg, "rstrip=", &arg)) {
201 atom->option = R_RSTRIP;
202 if (strtol_i(arg, 10, &atom->rstrip))
203 return strbuf_addf_ret(err, -1, _("Integer value expected refname:rstrip=%s"), arg);
205 return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), name, arg);
209 static int remote_ref_atom_parser(const struct ref_format *format, struct used_atom *atom,
210 const char *arg, struct strbuf *err)
212 struct string_list params = STRING_LIST_INIT_DUP;
215 if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:"))
216 atom->u.remote_ref.push = 1;
219 atom->u.remote_ref.option = RR_REF;
220 return refname_atom_parser_internal(&atom->u.remote_ref.refname,
221 arg, atom->name, err);
224 atom->u.remote_ref.nobracket = 0;
225 string_list_split(¶ms, arg, ',', -1);
227 for (i = 0; i < params.nr; i++) {
228 const char *s = params.items[i].string;
230 if (!strcmp(s, "track"))
231 atom->u.remote_ref.option = RR_TRACK;
232 else if (!strcmp(s, "trackshort"))
233 atom->u.remote_ref.option = RR_TRACKSHORT;
234 else if (!strcmp(s, "nobracket"))
235 atom->u.remote_ref.nobracket = 1;
236 else if (!strcmp(s, "remotename")) {
237 atom->u.remote_ref.option = RR_REMOTE_NAME;
238 atom->u.remote_ref.push_remote = 1;
239 } else if (!strcmp(s, "remoteref")) {
240 atom->u.remote_ref.option = RR_REMOTE_REF;
241 atom->u.remote_ref.push_remote = 1;
243 atom->u.remote_ref.option = RR_REF;
244 if (refname_atom_parser_internal(&atom->u.remote_ref.refname,
245 arg, atom->name, err)) {
246 string_list_clear(¶ms, 0);
252 string_list_clear(¶ms, 0);
256 static int objecttype_atom_parser(const struct ref_format *format, struct used_atom *atom,
257 const char *arg, struct strbuf *err)
260 return strbuf_addf_ret(err, -1, _("%%(objecttype) does not take arguments"));
261 if (*atom->name == '*')
262 oi_deref.info.typep = &oi_deref.type;
264 oi.info.typep = &oi.type;
268 static int objectsize_atom_parser(const struct ref_format *format, struct used_atom *atom,
269 const char *arg, struct strbuf *err)
272 if (*atom->name == '*')
273 oi_deref.info.sizep = &oi_deref.size;
275 oi.info.sizep = &oi.size;
276 } else if (!strcmp(arg, "disk")) {
277 if (*atom->name == '*')
278 oi_deref.info.disk_sizep = &oi_deref.disk_size;
280 oi.info.disk_sizep = &oi.disk_size;
282 return strbuf_addf_ret(err, -1, _("unrecognized %%(objectsize) argument: %s"), arg);
286 static int deltabase_atom_parser(const struct ref_format *format, struct used_atom *atom,
287 const char *arg, struct strbuf *err)
290 return strbuf_addf_ret(err, -1, _("%%(deltabase) does not take arguments"));
291 if (*atom->name == '*')
292 oi_deref.info.delta_base_oid = &oi_deref.delta_base_oid;
294 oi.info.delta_base_oid = &oi.delta_base_oid;
298 static int body_atom_parser(const struct ref_format *format, struct used_atom *atom,
299 const char *arg, struct strbuf *err)
302 return strbuf_addf_ret(err, -1, _("%%(body) does not take arguments"));
303 atom->u.contents.option = C_BODY_DEP;
307 static int subject_atom_parser(const struct ref_format *format, struct used_atom *atom,
308 const char *arg, struct strbuf *err)
311 atom->u.contents.option = C_SUB;
312 else if (!strcmp(arg, "sanitize"))
313 atom->u.contents.option = C_SUB_SANITIZE;
315 return strbuf_addf_ret(err, -1, _("unrecognized %%(subject) argument: %s"), arg);
319 static int trailers_atom_parser(const struct ref_format *format, struct used_atom *atom,
320 const char *arg, struct strbuf *err)
322 atom->u.contents.trailer_opts.no_divider = 1;
325 const char *argbuf = xstrfmt("%s)", arg);
326 char *invalid_arg = NULL;
328 if (format_set_trailers_options(&atom->u.contents.trailer_opts,
329 &ref_trailer_buf.filter_list,
330 &ref_trailer_buf.sepbuf,
331 &ref_trailer_buf.kvsepbuf,
332 &argbuf, &invalid_arg)) {
334 strbuf_addf(err, _("expected %%(trailers:key=<value>)"));
336 strbuf_addf(err, _("unknown %%(trailers) argument: %s"), invalid_arg);
337 free((char *)invalid_arg);
341 atom->u.contents.option = C_TRAILERS;
345 static int contents_atom_parser(const struct ref_format *format, struct used_atom *atom,
346 const char *arg, struct strbuf *err)
349 atom->u.contents.option = C_BARE;
350 else if (!strcmp(arg, "body"))
351 atom->u.contents.option = C_BODY;
352 else if (!strcmp(arg, "size"))
353 atom->u.contents.option = C_LENGTH;
354 else if (!strcmp(arg, "signature"))
355 atom->u.contents.option = C_SIG;
356 else if (!strcmp(arg, "subject"))
357 atom->u.contents.option = C_SUB;
358 else if (!strcmp(arg, "trailers")) {
359 if (trailers_atom_parser(format, atom, NULL, err))
361 } else if (skip_prefix(arg, "trailers:", &arg)) {
362 if (trailers_atom_parser(format, atom, arg, err))
364 } else if (skip_prefix(arg, "lines=", &arg)) {
365 atom->u.contents.option = C_LINES;
366 if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
367 return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg);
369 return strbuf_addf_ret(err, -1, _("unrecognized %%(contents) argument: %s"), arg);
373 static int oid_atom_parser(const struct ref_format *format, struct used_atom *atom,
374 const char *arg, struct strbuf *err)
377 atom->u.oid.option = O_FULL;
378 else if (!strcmp(arg, "short"))
379 atom->u.oid.option = O_SHORT;
380 else if (skip_prefix(arg, "short=", &arg)) {
381 atom->u.oid.option = O_LENGTH;
382 if (strtoul_ui(arg, 10, &atom->u.oid.length) ||
383 atom->u.oid.length == 0)
384 return strbuf_addf_ret(err, -1, _("positive value expected '%s' in %%(%s)"), arg, atom->name);
385 if (atom->u.oid.length < MINIMUM_ABBREV)
386 atom->u.oid.length = MINIMUM_ABBREV;
388 return strbuf_addf_ret(err, -1, _("unrecognized argument '%s' in %%(%s)"), arg, atom->name);
392 static int person_email_atom_parser(const struct ref_format *format, struct used_atom *atom,
393 const char *arg, struct strbuf *err)
396 atom->u.email_option.option = EO_RAW;
397 else if (!strcmp(arg, "trim"))
398 atom->u.email_option.option = EO_TRIM;
399 else if (!strcmp(arg, "localpart"))
400 atom->u.email_option.option = EO_LOCALPART;
402 return strbuf_addf_ret(err, -1, _("unrecognized email option: %s"), arg);
406 static int refname_atom_parser(const struct ref_format *format, struct used_atom *atom,
407 const char *arg, struct strbuf *err)
409 return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err);
412 static align_type parse_align_position(const char *s)
414 if (!strcmp(s, "right"))
416 else if (!strcmp(s, "middle"))
418 else if (!strcmp(s, "left"))
423 static int align_atom_parser(const struct ref_format *format, struct used_atom *atom,
424 const char *arg, struct strbuf *err)
426 struct align *align = &atom->u.align;
427 struct string_list params = STRING_LIST_INIT_DUP;
429 unsigned int width = ~0U;
432 return strbuf_addf_ret(err, -1, _("expected format: %%(align:<width>,<position>)"));
434 align->position = ALIGN_LEFT;
436 string_list_split(¶ms, arg, ',', -1);
437 for (i = 0; i < params.nr; i++) {
438 const char *s = params.items[i].string;
441 if (skip_prefix(s, "position=", &s)) {
442 position = parse_align_position(s);
444 strbuf_addf(err, _("unrecognized position:%s"), s);
445 string_list_clear(¶ms, 0);
448 align->position = position;
449 } else if (skip_prefix(s, "width=", &s)) {
450 if (strtoul_ui(s, 10, &width)) {
451 strbuf_addf(err, _("unrecognized width:%s"), s);
452 string_list_clear(¶ms, 0);
455 } else if (!strtoul_ui(s, 10, &width))
457 else if ((position = parse_align_position(s)) >= 0)
458 align->position = position;
460 strbuf_addf(err, _("unrecognized %%(align) argument: %s"), s);
461 string_list_clear(¶ms, 0);
467 string_list_clear(¶ms, 0);
468 return strbuf_addf_ret(err, -1, _("positive width expected with the %%(align) atom"));
470 align->width = width;
471 string_list_clear(¶ms, 0);
475 static int if_atom_parser(const struct ref_format *format, struct used_atom *atom,
476 const char *arg, struct strbuf *err)
479 atom->u.if_then_else.cmp_status = COMPARE_NONE;
481 } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) {
482 atom->u.if_then_else.cmp_status = COMPARE_EQUAL;
483 } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) {
484 atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL;
486 return strbuf_addf_ret(err, -1, _("unrecognized %%(if) argument: %s"), arg);
490 static int head_atom_parser(const struct ref_format *format, struct used_atom *atom,
491 const char *arg, struct strbuf *unused_err)
493 atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
501 int (*parser)(const struct ref_format *format, struct used_atom *atom,
502 const char *arg, struct strbuf *err);
504 { "refname", SOURCE_NONE, FIELD_STR, refname_atom_parser },
505 { "objecttype", SOURCE_OTHER, FIELD_STR, objecttype_atom_parser },
506 { "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser },
507 { "objectname", SOURCE_OTHER, FIELD_STR, oid_atom_parser },
508 { "deltabase", SOURCE_OTHER, FIELD_STR, deltabase_atom_parser },
509 { "tree", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
510 { "parent", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
511 { "numparent", SOURCE_OBJ, FIELD_ULONG },
512 { "object", SOURCE_OBJ },
513 { "type", SOURCE_OBJ },
514 { "tag", SOURCE_OBJ },
515 { "author", SOURCE_OBJ },
516 { "authorname", SOURCE_OBJ },
517 { "authoremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
518 { "authordate", SOURCE_OBJ, FIELD_TIME },
519 { "committer", SOURCE_OBJ },
520 { "committername", SOURCE_OBJ },
521 { "committeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
522 { "committerdate", SOURCE_OBJ, FIELD_TIME },
523 { "tagger", SOURCE_OBJ },
524 { "taggername", SOURCE_OBJ },
525 { "taggeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
526 { "taggerdate", SOURCE_OBJ, FIELD_TIME },
527 { "creator", SOURCE_OBJ },
528 { "creatordate", SOURCE_OBJ, FIELD_TIME },
529 { "subject", SOURCE_OBJ, FIELD_STR, subject_atom_parser },
530 { "body", SOURCE_OBJ, FIELD_STR, body_atom_parser },
531 { "trailers", SOURCE_OBJ, FIELD_STR, trailers_atom_parser },
532 { "contents", SOURCE_OBJ, FIELD_STR, contents_atom_parser },
533 { "upstream", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
534 { "push", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
535 { "symref", SOURCE_NONE, FIELD_STR, refname_atom_parser },
536 { "flag", SOURCE_NONE },
537 { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser },
538 { "color", SOURCE_NONE, FIELD_STR, color_atom_parser },
539 { "worktreepath", SOURCE_NONE },
540 { "align", SOURCE_NONE, FIELD_STR, align_atom_parser },
541 { "end", SOURCE_NONE },
542 { "if", SOURCE_NONE, FIELD_STR, if_atom_parser },
543 { "then", SOURCE_NONE },
544 { "else", SOURCE_NONE },
546 * Please update $__git_ref_fieldlist in git-completion.bash
547 * when you add new atoms
551 #define REF_FORMATTING_STATE_INIT { 0, NULL }
553 struct ref_formatting_stack {
554 struct ref_formatting_stack *prev;
555 struct strbuf output;
556 void (*at_end)(struct ref_formatting_stack **stack);
560 struct ref_formatting_state {
562 struct ref_formatting_stack *stack;
567 int (*handler)(struct atom_value *atomv, struct ref_formatting_state *state,
569 uintmax_t value; /* used for sorting when not FIELD_STR */
570 struct used_atom *atom;
574 * Used to parse format string and sort specifiers
576 static int parse_ref_filter_atom(const struct ref_format *format,
577 const char *atom, const char *ep,
585 if (*sp == '*' && sp < ep)
588 return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"),
589 (int)(ep-atom), atom);
591 /* Do we have the atom already used elsewhere? */
592 for (i = 0; i < used_atom_cnt; i++) {
593 int len = strlen(used_atom[i].name);
594 if (len == ep - atom && !memcmp(used_atom[i].name, atom, len))
599 * If the atom name has a colon, strip it and everything after
600 * it off - it specifies the format for this entry, and
601 * shouldn't be used for checking against the valid_atom
604 arg = memchr(sp, ':', ep - sp);
605 atom_len = (arg ? arg : ep) - sp;
607 /* Is the atom a valid one? */
608 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
609 int len = strlen(valid_atom[i].name);
610 if (len == atom_len && !memcmp(valid_atom[i].name, sp, len))
614 if (ARRAY_SIZE(valid_atom) <= i)
615 return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"),
616 (int)(ep-atom), atom);
617 if (valid_atom[i].source != SOURCE_NONE && !have_git_dir())
618 return strbuf_addf_ret(err, -1,
619 _("not a git repository, but the field '%.*s' requires access to object data"),
620 (int)(ep-atom), atom);
622 /* Add it in, including the deref prefix */
625 REALLOC_ARRAY(used_atom, used_atom_cnt);
626 used_atom[at].name = xmemdupz(atom, ep - atom);
627 used_atom[at].type = valid_atom[i].cmp_type;
628 used_atom[at].source = valid_atom[i].source;
629 if (used_atom[at].source == SOURCE_OBJ) {
631 oi_deref.info.contentp = &oi_deref.content;
633 oi.info.contentp = &oi.content;
636 arg = used_atom[at].name + (arg - atom) + 1;
639 * Treat empty sub-arguments list as NULL (i.e.,
640 * "%(atom:)" is equivalent to "%(atom)").
645 memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
646 if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err))
650 if (!strcmp(valid_atom[i].name, "symref"))
655 static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
657 switch (quote_style) {
659 strbuf_addstr(s, str);
662 sq_quote_buf(s, str);
665 perl_quote_buf(s, str);
668 python_quote_buf(s, str);
671 tcl_quote_buf(s, str);
676 static int append_atom(struct atom_value *v, struct ref_formatting_state *state,
677 struct strbuf *unused_err)
680 * Quote formatting is only done when the stack has a single
681 * element. Otherwise quote formatting is done on the
682 * element's entire output strbuf when the %(end) atom is
685 if (!state->stack->prev)
686 quote_formatting(&state->stack->output, v->s, state->quote_style);
688 strbuf_addstr(&state->stack->output, v->s);
692 static void push_stack_element(struct ref_formatting_stack **stack)
694 struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
696 strbuf_init(&s->output, 0);
701 static void pop_stack_element(struct ref_formatting_stack **stack)
703 struct ref_formatting_stack *current = *stack;
704 struct ref_formatting_stack *prev = current->prev;
707 strbuf_addbuf(&prev->output, ¤t->output);
708 strbuf_release(¤t->output);
713 static void end_align_handler(struct ref_formatting_stack **stack)
715 struct ref_formatting_stack *cur = *stack;
716 struct align *align = (struct align *)cur->at_end_data;
717 struct strbuf s = STRBUF_INIT;
719 strbuf_utf8_align(&s, align->position, align->width, cur->output.buf);
720 strbuf_swap(&cur->output, &s);
724 static int align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
725 struct strbuf *unused_err)
727 struct ref_formatting_stack *new_stack;
729 push_stack_element(&state->stack);
730 new_stack = state->stack;
731 new_stack->at_end = end_align_handler;
732 new_stack->at_end_data = &atomv->atom->u.align;
736 static void if_then_else_handler(struct ref_formatting_stack **stack)
738 struct ref_formatting_stack *cur = *stack;
739 struct ref_formatting_stack *prev = cur->prev;
740 struct if_then_else *if_then_else = (struct if_then_else *)cur->at_end_data;
742 if (!if_then_else->then_atom_seen)
743 die(_("format: %%(if) atom used without a %%(then) atom"));
745 if (if_then_else->else_atom_seen) {
747 * There is an %(else) atom: we need to drop one state from the
748 * stack, either the %(else) branch if the condition is satisfied, or
749 * the %(then) branch if it isn't.
751 if (if_then_else->condition_satisfied) {
752 strbuf_reset(&cur->output);
753 pop_stack_element(&cur);
755 strbuf_swap(&cur->output, &prev->output);
756 strbuf_reset(&cur->output);
757 pop_stack_element(&cur);
759 } else if (!if_then_else->condition_satisfied) {
761 * No %(else) atom: just drop the %(then) branch if the
762 * condition is not satisfied.
764 strbuf_reset(&cur->output);
771 static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
772 struct strbuf *unused_err)
774 struct ref_formatting_stack *new_stack;
775 struct if_then_else *if_then_else = xcalloc(sizeof(struct if_then_else), 1);
777 if_then_else->str = atomv->atom->u.if_then_else.str;
778 if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status;
780 push_stack_element(&state->stack);
781 new_stack = state->stack;
782 new_stack->at_end = if_then_else_handler;
783 new_stack->at_end_data = if_then_else;
787 static int is_empty(const char *s)
797 static int then_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
800 struct ref_formatting_stack *cur = state->stack;
801 struct if_then_else *if_then_else = NULL;
803 if (cur->at_end == if_then_else_handler)
804 if_then_else = (struct if_then_else *)cur->at_end_data;
806 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used without an %%(if) atom"));
807 if (if_then_else->then_atom_seen)
808 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used more than once"));
809 if (if_then_else->else_atom_seen)
810 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used after %%(else)"));
811 if_then_else->then_atom_seen = 1;
813 * If the 'equals' or 'notequals' attribute is used then
814 * perform the required comparison. If not, only non-empty
815 * strings satisfy the 'if' condition.
817 if (if_then_else->cmp_status == COMPARE_EQUAL) {
818 if (!strcmp(if_then_else->str, cur->output.buf))
819 if_then_else->condition_satisfied = 1;
820 } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) {
821 if (strcmp(if_then_else->str, cur->output.buf))
822 if_then_else->condition_satisfied = 1;
823 } else if (cur->output.len && !is_empty(cur->output.buf))
824 if_then_else->condition_satisfied = 1;
825 strbuf_reset(&cur->output);
829 static int else_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
832 struct ref_formatting_stack *prev = state->stack;
833 struct if_then_else *if_then_else = NULL;
835 if (prev->at_end == if_then_else_handler)
836 if_then_else = (struct if_then_else *)prev->at_end_data;
838 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without an %%(if) atom"));
839 if (!if_then_else->then_atom_seen)
840 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without a %%(then) atom"));
841 if (if_then_else->else_atom_seen)
842 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used more than once"));
843 if_then_else->else_atom_seen = 1;
844 push_stack_element(&state->stack);
845 state->stack->at_end_data = prev->at_end_data;
846 state->stack->at_end = prev->at_end;
850 static int end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
853 struct ref_formatting_stack *current = state->stack;
854 struct strbuf s = STRBUF_INIT;
856 if (!current->at_end)
857 return strbuf_addf_ret(err, -1, _("format: %%(end) atom used without corresponding atom"));
858 current->at_end(&state->stack);
860 /* Stack may have been popped within at_end(), hence reset the current pointer */
861 current = state->stack;
864 * Perform quote formatting when the stack element is that of
865 * a supporting atom. If nested then perform quote formatting
866 * only on the topmost supporting atom.
868 if (!current->prev->prev) {
869 quote_formatting(&s, current->output.buf, state->quote_style);
870 strbuf_swap(¤t->output, &s);
873 pop_stack_element(&state->stack);
878 * In a format string, find the next occurrence of %(atom).
880 static const char *find_next(const char *cp)
885 * %( is the start of an atom;
886 * %% is a quoted per-cent.
890 else if (cp[1] == '%')
891 cp++; /* skip over two % */
892 /* otherwise this is a singleton, literal % */
900 * Make sure the format string is well formed, and parse out
903 int verify_ref_format(struct ref_format *format)
907 format->need_color_reset_at_eol = 0;
908 for (cp = format->format; *cp && (sp = find_next(cp)); ) {
909 struct strbuf err = STRBUF_INIT;
910 const char *color, *ep = strchr(sp, ')');
914 return error(_("malformed format string %s"), sp);
915 /* sp points at "%(" and ep points at the closing ")" */
916 at = parse_ref_filter_atom(format, sp + 2, ep, &err);
921 if (skip_prefix(used_atom[at].name, "color:", &color))
922 format->need_color_reset_at_eol = !!strcmp(color, "reset");
923 strbuf_release(&err);
925 if (format->need_color_reset_at_eol && !want_color(format->use_color))
926 format->need_color_reset_at_eol = 0;
930 static const char *do_grab_oid(const char *field, const struct object_id *oid,
931 struct used_atom *atom)
933 switch (atom->u.oid.option) {
935 return oid_to_hex(oid);
937 return find_unique_abbrev(oid, atom->u.oid.length);
939 return find_unique_abbrev(oid, DEFAULT_ABBREV);
941 BUG("unknown %%(%s) option", field);
945 static int grab_oid(const char *name, const char *field, const struct object_id *oid,
946 struct atom_value *v, struct used_atom *atom)
948 if (starts_with(name, field)) {
949 v->s = xstrdup(do_grab_oid(field, oid, atom));
955 /* See grab_values */
956 static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi)
960 for (i = 0; i < used_atom_cnt; i++) {
961 const char *name = used_atom[i].name;
962 struct atom_value *v = &val[i];
963 if (!!deref != (*name == '*'))
967 if (!strcmp(name, "objecttype"))
968 v->s = xstrdup(type_name(oi->type));
969 else if (!strcmp(name, "objectsize:disk")) {
970 v->value = oi->disk_size;
971 v->s = xstrfmt("%"PRIuMAX, (uintmax_t)oi->disk_size);
972 } else if (!strcmp(name, "objectsize")) {
974 v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size);
975 } else if (!strcmp(name, "deltabase"))
976 v->s = xstrdup(oid_to_hex(&oi->delta_base_oid));
978 grab_oid(name, "objectname", &oi->oid, v, &used_atom[i]);
982 /* See grab_values */
983 static void grab_tag_values(struct atom_value *val, int deref, struct object *obj)
986 struct tag *tag = (struct tag *) obj;
988 for (i = 0; i < used_atom_cnt; i++) {
989 const char *name = used_atom[i].name;
990 struct atom_value *v = &val[i];
991 if (!!deref != (*name == '*'))
995 if (!strcmp(name, "tag"))
996 v->s = xstrdup(tag->tag);
997 else if (!strcmp(name, "type") && tag->tagged)
998 v->s = xstrdup(type_name(tag->tagged->type));
999 else if (!strcmp(name, "object") && tag->tagged)
1000 v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
1004 /* See grab_values */
1005 static void grab_commit_values(struct atom_value *val, int deref, struct object *obj)
1008 struct commit *commit = (struct commit *) obj;
1010 for (i = 0; i < used_atom_cnt; i++) {
1011 const char *name = used_atom[i].name;
1012 struct atom_value *v = &val[i];
1013 if (!!deref != (*name == '*'))
1017 if (grab_oid(name, "tree", get_commit_tree_oid(commit), v, &used_atom[i]))
1019 if (!strcmp(name, "numparent")) {
1020 v->value = commit_list_count(commit->parents);
1021 v->s = xstrfmt("%lu", (unsigned long)v->value);
1023 else if (starts_with(name, "parent")) {
1024 struct commit_list *parents;
1025 struct strbuf s = STRBUF_INIT;
1026 for (parents = commit->parents; parents; parents = parents->next) {
1027 struct object_id *oid = &parents->item->object.oid;
1028 if (parents != commit->parents)
1029 strbuf_addch(&s, ' ');
1030 strbuf_addstr(&s, do_grab_oid("parent", oid, &used_atom[i]));
1032 v->s = strbuf_detach(&s, NULL);
1037 static const char *find_wholine(const char *who, int wholen, const char *buf)
1041 if (!strncmp(buf, who, wholen) &&
1043 return buf + wholen + 1;
1044 eol = strchr(buf, '\n');
1049 return ""; /* end of header */
1055 static const char *copy_line(const char *buf)
1057 const char *eol = strchrnul(buf, '\n');
1058 return xmemdupz(buf, eol - buf);
1061 static const char *copy_name(const char *buf)
1064 for (cp = buf; *cp && *cp != '\n'; cp++) {
1065 if (!strncmp(cp, " <", 2))
1066 return xmemdupz(buf, cp - buf);
1071 static const char *copy_email(const char *buf, struct used_atom *atom)
1073 const char *email = strchr(buf, '<');
1074 const char *eoemail;
1077 switch (atom->u.email_option.option) {
1079 eoemail = strchr(email, '>');
1085 eoemail = strchr(email, '>');
1089 eoemail = strchr(email, '@');
1091 eoemail = strchr(email, '>');
1094 BUG("unknown email option");
1099 return xmemdupz(email, eoemail - email);
1102 static char *copy_subject(const char *buf, unsigned long len)
1104 struct strbuf sb = STRBUF_INIT;
1107 for (i = 0; i < len; i++) {
1108 if (buf[i] == '\r' && i + 1 < len && buf[i + 1] == '\n')
1109 continue; /* ignore CR in CRLF */
1112 strbuf_addch(&sb, ' ');
1114 strbuf_addch(&sb, buf[i]);
1116 return strbuf_detach(&sb, NULL);
1119 static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
1121 const char *eoemail = strstr(buf, "> ");
1123 timestamp_t timestamp;
1125 struct date_mode date_mode = { DATE_NORMAL };
1126 const char *formatp;
1129 * We got here because atomname ends in "date" or "date<something>";
1130 * it's not possible that <something> is not ":<format>" because
1131 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1132 * ":" means no format is specified, and use the default.
1134 formatp = strchr(atomname, ':');
1135 if (formatp != NULL) {
1137 parse_date_format(formatp, &date_mode);
1142 timestamp = parse_timestamp(eoemail + 2, &zone, 10);
1143 if (timestamp == TIME_MAX)
1145 tz = strtol(zone, NULL, 10);
1146 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
1148 v->s = xstrdup(show_date(timestamp, tz, &date_mode));
1149 v->value = timestamp;
1156 /* See grab_values */
1157 static void grab_person(const char *who, struct atom_value *val, int deref, void *buf)
1160 int wholen = strlen(who);
1161 const char *wholine = NULL;
1163 for (i = 0; i < used_atom_cnt; i++) {
1164 const char *name = used_atom[i].name;
1165 struct atom_value *v = &val[i];
1166 if (!!deref != (*name == '*'))
1170 if (strncmp(who, name, wholen))
1172 if (name[wholen] != 0 &&
1173 strcmp(name + wholen, "name") &&
1174 !starts_with(name + wholen, "email") &&
1175 !starts_with(name + wholen, "date"))
1178 wholine = find_wholine(who, wholen, buf);
1180 return; /* no point looking for it */
1181 if (name[wholen] == 0)
1182 v->s = copy_line(wholine);
1183 else if (!strcmp(name + wholen, "name"))
1184 v->s = copy_name(wholine);
1185 else if (starts_with(name + wholen, "email"))
1186 v->s = copy_email(wholine, &used_atom[i]);
1187 else if (starts_with(name + wholen, "date"))
1188 grab_date(wholine, v, name);
1192 * For a tag or a commit object, if "creator" or "creatordate" is
1193 * requested, do something special.
1195 if (strcmp(who, "tagger") && strcmp(who, "committer"))
1196 return; /* "author" for commit object is not wanted */
1198 wholine = find_wholine(who, wholen, buf);
1201 for (i = 0; i < used_atom_cnt; i++) {
1202 const char *name = used_atom[i].name;
1203 struct atom_value *v = &val[i];
1204 if (!!deref != (*name == '*'))
1209 if (starts_with(name, "creatordate"))
1210 grab_date(wholine, v, name);
1211 else if (!strcmp(name, "creator"))
1212 v->s = copy_line(wholine);
1216 static void find_subpos(const char *buf,
1217 const char **sub, unsigned long *sublen,
1218 const char **body, unsigned long *bodylen,
1219 unsigned long *nonsiglen,
1220 const char **sig, unsigned long *siglen)
1223 /* skip past header until we hit empty line */
1224 while (*buf && *buf != '\n') {
1225 eol = strchrnul(buf, '\n');
1230 /* skip any empty lines */
1231 while (*buf == '\n')
1234 /* parse signature first; we might not even have a subject line */
1235 *sig = buf + parse_signature(buf, strlen(buf));
1236 *siglen = strlen(*sig);
1238 /* subject is first non-empty line */
1240 /* subject goes to first empty line before signature begins */
1241 if ((eol = strstr(*sub, "\n\n"))) {
1242 eol = eol < *sig ? eol : *sig;
1243 /* check if message uses CRLF */
1244 } else if (! (eol = strstr(*sub, "\r\n\r\n"))) {
1245 /* treat whole message as subject */
1246 eol = strrchr(*sub, '\0');
1249 *sublen = buf - *sub;
1250 /* drop trailing newline, if present */
1251 while (*sublen && ((*sub)[*sublen - 1] == '\n' ||
1252 (*sub)[*sublen - 1] == '\r'))
1255 /* skip any empty lines */
1256 while (*buf == '\n' || *buf == '\r')
1259 *bodylen = strlen(buf);
1260 *nonsiglen = *sig - buf;
1264 * If 'lines' is greater than 0, append that many lines from the given
1265 * 'buf' of length 'size' to the given strbuf.
1267 static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
1270 const char *sp, *eol;
1275 for (i = 0; i < lines && sp < buf + size; i++) {
1277 strbuf_addstr(out, "\n ");
1278 eol = memchr(sp, '\n', size - (sp - buf));
1279 len = eol ? eol - sp : size - (sp - buf);
1280 strbuf_add(out, sp, len);
1287 /* See grab_values */
1288 static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
1291 const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
1292 unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
1294 for (i = 0; i < used_atom_cnt; i++) {
1295 struct used_atom *atom = &used_atom[i];
1296 const char *name = atom->name;
1297 struct atom_value *v = &val[i];
1298 if (!!deref != (*name == '*'))
1302 if (strcmp(name, "body") &&
1303 !starts_with(name, "subject") &&
1304 !starts_with(name, "trailers") &&
1305 !starts_with(name, "contents"))
1310 &bodypos, &bodylen, &nonsiglen,
1313 if (atom->u.contents.option == C_SUB)
1314 v->s = copy_subject(subpos, sublen);
1315 else if (atom->u.contents.option == C_SUB_SANITIZE) {
1316 struct strbuf sb = STRBUF_INIT;
1317 format_sanitized_subject(&sb, subpos, sublen);
1318 v->s = strbuf_detach(&sb, NULL);
1319 } else if (atom->u.contents.option == C_BODY_DEP)
1320 v->s = xmemdupz(bodypos, bodylen);
1321 else if (atom->u.contents.option == C_LENGTH)
1322 v->s = xstrfmt("%"PRIuMAX, (uintmax_t)strlen(subpos));
1323 else if (atom->u.contents.option == C_BODY)
1324 v->s = xmemdupz(bodypos, nonsiglen);
1325 else if (atom->u.contents.option == C_SIG)
1326 v->s = xmemdupz(sigpos, siglen);
1327 else if (atom->u.contents.option == C_LINES) {
1328 struct strbuf s = STRBUF_INIT;
1329 const char *contents_end = bodylen + bodypos - siglen;
1331 /* Size is the length of the message after removing the signature */
1332 append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
1333 v->s = strbuf_detach(&s, NULL);
1334 } else if (atom->u.contents.option == C_TRAILERS) {
1335 struct strbuf s = STRBUF_INIT;
1337 /* Format the trailer info according to the trailer_opts given */
1338 format_trailers_from_commit(&s, subpos, &atom->u.contents.trailer_opts);
1340 v->s = strbuf_detach(&s, NULL);
1341 } else if (atom->u.contents.option == C_BARE)
1342 v->s = xstrdup(subpos);
1347 * We want to have empty print-string for field requests
1348 * that do not apply (e.g. "authordate" for a tag object)
1350 static void fill_missing_values(struct atom_value *val)
1353 for (i = 0; i < used_atom_cnt; i++) {
1354 struct atom_value *v = &val[i];
1361 * val is a list of atom_value to hold returned values. Extract
1362 * the values for atoms in used_atom array out of (obj, buf, sz).
1363 * when deref is false, (obj, buf, sz) is the object that is
1364 * pointed at by the ref itself; otherwise it is the object the
1365 * ref (which is a tag) refers to.
1367 static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf)
1369 switch (obj->type) {
1371 grab_tag_values(val, deref, obj);
1372 grab_sub_body_contents(val, deref, buf);
1373 grab_person("tagger", val, deref, buf);
1376 grab_commit_values(val, deref, obj);
1377 grab_sub_body_contents(val, deref, buf);
1378 grab_person("author", val, deref, buf);
1379 grab_person("committer", val, deref, buf);
1382 /* grab_tree_values(val, deref, obj, buf, sz); */
1385 /* grab_blob_values(val, deref, obj, buf, sz); */
1388 die("Eh? Object of type %d?", obj->type);
1392 static inline char *copy_advance(char *dst, const char *src)
1399 static const char *lstrip_ref_components(const char *refname, int len)
1401 long remaining = len;
1402 const char *start = xstrdup(refname);
1403 const char *to_free = start;
1407 const char *p = refname;
1409 /* Find total no of '/' separated path-components */
1410 for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
1413 * The number of components we need to strip is now
1414 * the total minus the components to be left (Plus one
1415 * because we count the number of '/', but the number
1416 * of components is one more than the no of '/').
1418 remaining = i + len + 1;
1421 while (remaining > 0) {
1424 free((char *)to_free);
1432 start = xstrdup(start);
1433 free((char *)to_free);
1437 static const char *rstrip_ref_components(const char *refname, int len)
1439 long remaining = len;
1440 const char *start = xstrdup(refname);
1441 const char *to_free = start;
1445 const char *p = refname;
1447 /* Find total no of '/' separated path-components */
1448 for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
1451 * The number of components we need to strip is now
1452 * the total minus the components to be left (Plus one
1453 * because we count the number of '/', but the number
1454 * of components is one more than the no of '/').
1456 remaining = i + len + 1;
1459 while (remaining-- > 0) {
1460 char *p = strrchr(start, '/');
1462 free((char *)to_free);
1470 static const char *show_ref(struct refname_atom *atom, const char *refname)
1472 if (atom->option == R_SHORT)
1473 return shorten_unambiguous_ref(refname, warn_ambiguous_refs);
1474 else if (atom->option == R_LSTRIP)
1475 return lstrip_ref_components(refname, atom->lstrip);
1476 else if (atom->option == R_RSTRIP)
1477 return rstrip_ref_components(refname, atom->rstrip);
1479 return xstrdup(refname);
1482 static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
1483 struct branch *branch, const char **s)
1485 int num_ours, num_theirs;
1486 if (atom->u.remote_ref.option == RR_REF)
1487 *s = show_ref(&atom->u.remote_ref.refname, refname);
1488 else if (atom->u.remote_ref.option == RR_TRACK) {
1489 if (stat_tracking_info(branch, &num_ours, &num_theirs,
1490 NULL, atom->u.remote_ref.push,
1491 AHEAD_BEHIND_FULL) < 0) {
1492 *s = xstrdup(msgs.gone);
1493 } else if (!num_ours && !num_theirs)
1496 *s = xstrfmt(msgs.behind, num_theirs);
1497 else if (!num_theirs)
1498 *s = xstrfmt(msgs.ahead, num_ours);
1500 *s = xstrfmt(msgs.ahead_behind,
1501 num_ours, num_theirs);
1502 if (!atom->u.remote_ref.nobracket && *s[0]) {
1503 const char *to_free = *s;
1504 *s = xstrfmt("[%s]", *s);
1505 free((void *)to_free);
1507 } else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
1508 if (stat_tracking_info(branch, &num_ours, &num_theirs,
1509 NULL, atom->u.remote_ref.push,
1510 AHEAD_BEHIND_FULL) < 0) {
1514 if (!num_ours && !num_theirs)
1518 else if (!num_theirs)
1522 } else if (atom->u.remote_ref.option == RR_REMOTE_NAME) {
1524 const char *remote = atom->u.remote_ref.push ?
1525 pushremote_for_branch(branch, &explicit) :
1526 remote_for_branch(branch, &explicit);
1527 *s = xstrdup(explicit ? remote : "");
1528 } else if (atom->u.remote_ref.option == RR_REMOTE_REF) {
1531 merge = remote_ref_for_branch(branch, atom->u.remote_ref.push);
1532 *s = xstrdup(merge ? merge : "");
1534 BUG("unhandled RR_* enum");
1537 char *get_head_description(void)
1539 struct strbuf desc = STRBUF_INIT;
1540 struct wt_status_state state;
1541 memset(&state, 0, sizeof(state));
1542 wt_status_get_state(the_repository, &state, 1);
1543 if (state.rebase_in_progress ||
1544 state.rebase_interactive_in_progress) {
1546 strbuf_addf(&desc, _("(no branch, rebasing %s)"),
1549 strbuf_addf(&desc, _("(no branch, rebasing detached HEAD %s)"),
1550 state.detached_from);
1551 } else if (state.bisect_in_progress)
1552 strbuf_addf(&desc, _("(no branch, bisect started on %s)"),
1554 else if (state.detached_from) {
1555 if (state.detached_at)
1556 strbuf_addf(&desc, _("(HEAD detached at %s)"),
1557 state.detached_from);
1559 strbuf_addf(&desc, _("(HEAD detached from %s)"),
1560 state.detached_from);
1562 strbuf_addstr(&desc, _("(no branch)"));
1564 return strbuf_detach(&desc, NULL);
1567 static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref)
1572 return show_ref(&atom->u.refname, ref->symref);
1575 static const char *get_refname(struct used_atom *atom, struct ref_array_item *ref)
1577 if (ref->kind & FILTER_REFS_DETACHED_HEAD)
1578 return get_head_description();
1579 return show_ref(&atom->u.refname, ref->refname);
1582 static int get_object(struct ref_array_item *ref, int deref, struct object **obj,
1583 struct expand_data *oi, struct strbuf *err)
1585 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
1587 if (oi->info.contentp) {
1588 /* We need to know that to use parse_object_buffer properly */
1589 oi->info.sizep = &oi->size;
1590 oi->info.typep = &oi->type;
1592 if (oid_object_info_extended(the_repository, &oi->oid, &oi->info,
1593 OBJECT_INFO_LOOKUP_REPLACE))
1594 return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
1595 oid_to_hex(&oi->oid), ref->refname);
1596 if (oi->info.disk_sizep && oi->disk_size < 0)
1597 BUG("Object size is less than zero.");
1599 if (oi->info.contentp) {
1600 *obj = parse_object_buffer(the_repository, &oi->oid, oi->type, oi->size, oi->content, &eaten);
1604 return strbuf_addf_ret(err, -1, _("parse_object_buffer failed on %s for %s"),
1605 oid_to_hex(&oi->oid), ref->refname);
1607 grab_values(ref->value, deref, *obj, oi->content);
1610 grab_common_values(ref->value, deref, oi);
1616 static void populate_worktree_map(struct hashmap *map, struct worktree **worktrees)
1620 for (i = 0; worktrees[i]; i++) {
1621 if (worktrees[i]->head_ref) {
1622 struct ref_to_worktree_entry *entry;
1623 entry = xmalloc(sizeof(*entry));
1624 entry->wt = worktrees[i];
1625 hashmap_entry_init(&entry->ent,
1626 strhash(worktrees[i]->head_ref));
1628 hashmap_add(map, &entry->ent);
1633 static void lazy_init_worktree_map(void)
1635 if (ref_to_worktree_map.worktrees)
1638 ref_to_worktree_map.worktrees = get_worktrees();
1639 hashmap_init(&(ref_to_worktree_map.map), ref_to_worktree_map_cmpfnc, NULL, 0);
1640 populate_worktree_map(&(ref_to_worktree_map.map), ref_to_worktree_map.worktrees);
1643 static char *get_worktree_path(const struct used_atom *atom, const struct ref_array_item *ref)
1645 struct hashmap_entry entry, *e;
1646 struct ref_to_worktree_entry *lookup_result;
1648 lazy_init_worktree_map();
1650 hashmap_entry_init(&entry, strhash(ref->refname));
1651 e = hashmap_get(&(ref_to_worktree_map.map), &entry, ref->refname);
1656 lookup_result = container_of(e, struct ref_to_worktree_entry, ent);
1658 return xstrdup(lookup_result->wt->path);
1662 * Parse the object referred by ref, and grab needed value.
1664 static int populate_value(struct ref_array_item *ref, struct strbuf *err)
1668 struct object_info empty = OBJECT_INFO_INIT;
1670 ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
1672 if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
1673 ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
1676 ref->symref = xstrdup("");
1679 /* Fill in specials first */
1680 for (i = 0; i < used_atom_cnt; i++) {
1681 struct used_atom *atom = &used_atom[i];
1682 const char *name = used_atom[i].name;
1683 struct atom_value *v = &ref->value[i];
1685 const char *refname;
1686 struct branch *branch = NULL;
1688 v->handler = append_atom;
1696 if (starts_with(name, "refname"))
1697 refname = get_refname(atom, ref);
1698 else if (!strcmp(name, "worktreepath")) {
1699 if (ref->kind == FILTER_REFS_BRANCHES)
1700 v->s = get_worktree_path(atom, ref);
1705 else if (starts_with(name, "symref"))
1706 refname = get_symref(atom, ref);
1707 else if (starts_with(name, "upstream")) {
1708 const char *branch_name;
1709 /* only local branches may have an upstream */
1710 if (!skip_prefix(ref->refname, "refs/heads/",
1715 branch = branch_get(branch_name);
1717 refname = branch_get_upstream(branch, NULL);
1719 fill_remote_ref_details(atom, refname, branch, &v->s);
1723 } else if (atom->u.remote_ref.push) {
1724 const char *branch_name;
1726 if (!skip_prefix(ref->refname, "refs/heads/",
1729 branch = branch_get(branch_name);
1731 if (atom->u.remote_ref.push_remote)
1734 refname = branch_get_push(branch, NULL);
1738 /* We will definitely re-init v->s on the next line. */
1740 fill_remote_ref_details(atom, refname, branch, &v->s);
1742 } else if (starts_with(name, "color:")) {
1743 v->s = xstrdup(atom->u.color);
1745 } else if (!strcmp(name, "flag")) {
1746 char buf[256], *cp = buf;
1747 if (ref->flag & REF_ISSYMREF)
1748 cp = copy_advance(cp, ",symref");
1749 if (ref->flag & REF_ISPACKED)
1750 cp = copy_advance(cp, ",packed");
1755 v->s = xstrdup(buf + 1);
1758 } else if (!deref && grab_oid(name, "objectname", &ref->objectname, v, atom)) {
1760 } else if (!strcmp(name, "HEAD")) {
1761 if (atom->u.head && !strcmp(ref->refname, atom->u.head))
1762 v->s = xstrdup("*");
1764 v->s = xstrdup(" ");
1766 } else if (starts_with(name, "align")) {
1767 v->handler = align_atom_handler;
1770 } else if (!strcmp(name, "end")) {
1771 v->handler = end_atom_handler;
1774 } else if (starts_with(name, "if")) {
1776 if (skip_prefix(name, "if:", &s))
1780 v->handler = if_atom_handler;
1782 } else if (!strcmp(name, "then")) {
1783 v->handler = then_atom_handler;
1786 } else if (!strcmp(name, "else")) {
1787 v->handler = else_atom_handler;
1794 v->s = xstrdup(refname);
1796 v->s = xstrfmt("%s^{}", refname);
1797 free((char *)refname);
1800 for (i = 0; i < used_atom_cnt; i++) {
1801 struct atom_value *v = &ref->value[i];
1802 if (v->s == NULL && used_atom[i].source == SOURCE_NONE)
1803 return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
1804 oid_to_hex(&ref->objectname), ref->refname);
1808 oi.info.contentp = &oi.content;
1809 if (!memcmp(&oi.info, &empty, sizeof(empty)) &&
1810 !memcmp(&oi_deref.info, &empty, sizeof(empty)))
1814 oi.oid = ref->objectname;
1815 if (get_object(ref, 0, &obj, &oi, err))
1819 * If there is no atom that wants to know about tagged
1820 * object, we are done.
1822 if (!need_tagged || (obj->type != OBJ_TAG))
1826 * If it is a tag object, see if we use a value that derefs
1827 * the object, and if we do grab the object it refers to.
1829 oi_deref.oid = *get_tagged_oid((struct tag *)obj);
1832 * NEEDSWORK: This derefs tag only once, which
1833 * is good to deal with chains of trust, but
1834 * is not consistent with what deref_tag() does
1835 * which peels the onion to the core.
1837 return get_object(ref, 1, &obj, &oi_deref, err);
1841 * Given a ref, return the value for the atom. This lazily gets value
1842 * out of the object by calling populate value.
1844 static int get_ref_atom_value(struct ref_array_item *ref, int atom,
1845 struct atom_value **v, struct strbuf *err)
1848 if (populate_value(ref, err))
1850 fill_missing_values(ref->value);
1852 *v = &ref->value[atom];
1857 * Return 1 if the refname matches one of the patterns, otherwise 0.
1858 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1859 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1860 * matches "refs/heads/mas*", too).
1862 static int match_pattern(const struct ref_filter *filter, const char *refname)
1864 const char **patterns = filter->name_patterns;
1867 if (filter->ignore_case)
1868 flags |= WM_CASEFOLD;
1871 * When no '--format' option is given we need to skip the prefix
1872 * for matching refs of tags and branches.
1874 (void)(skip_prefix(refname, "refs/tags/", &refname) ||
1875 skip_prefix(refname, "refs/heads/", &refname) ||
1876 skip_prefix(refname, "refs/remotes/", &refname) ||
1877 skip_prefix(refname, "refs/", &refname));
1879 for (; *patterns; patterns++) {
1880 if (!wildmatch(*patterns, refname, flags))
1887 * Return 1 if the refname matches one of the patterns, otherwise 0.
1888 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1889 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1890 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1892 static int match_name_as_path(const struct ref_filter *filter, const char *refname)
1894 const char **pattern = filter->name_patterns;
1895 int namelen = strlen(refname);
1896 unsigned flags = WM_PATHNAME;
1898 if (filter->ignore_case)
1899 flags |= WM_CASEFOLD;
1901 for (; *pattern; pattern++) {
1902 const char *p = *pattern;
1903 int plen = strlen(p);
1905 if ((plen <= namelen) &&
1906 !strncmp(refname, p, plen) &&
1907 (refname[plen] == '\0' ||
1908 refname[plen] == '/' ||
1911 if (!wildmatch(p, refname, flags))
1917 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1918 static int filter_pattern_match(struct ref_filter *filter, const char *refname)
1920 if (!*filter->name_patterns)
1921 return 1; /* No pattern always matches */
1922 if (filter->match_as_path)
1923 return match_name_as_path(filter, refname);
1924 return match_pattern(filter, refname);
1927 static int qsort_strcmp(const void *va, const void *vb)
1929 const char *a = *(const char **)va;
1930 const char *b = *(const char **)vb;
1932 return strcmp(a, b);
1935 static void find_longest_prefixes_1(struct string_list *out,
1936 struct strbuf *prefix,
1937 const char **patterns, size_t nr)
1941 for (i = 0; i < nr; i++) {
1942 char c = patterns[i][prefix->len];
1943 if (!c || is_glob_special(c)) {
1944 string_list_append(out, prefix->buf);
1954 * Set "end" to the index of the element _after_ the last one
1957 for (end = i + 1; end < nr; end++) {
1958 if (patterns[i][prefix->len] != patterns[end][prefix->len])
1962 strbuf_addch(prefix, patterns[i][prefix->len]);
1963 find_longest_prefixes_1(out, prefix, patterns + i, end - i);
1964 strbuf_setlen(prefix, prefix->len - 1);
1970 static void find_longest_prefixes(struct string_list *out,
1971 const char **patterns)
1973 struct strvec sorted = STRVEC_INIT;
1974 struct strbuf prefix = STRBUF_INIT;
1976 strvec_pushv(&sorted, patterns);
1977 QSORT(sorted.v, sorted.nr, qsort_strcmp);
1979 find_longest_prefixes_1(out, &prefix, sorted.v, sorted.nr);
1981 strvec_clear(&sorted);
1982 strbuf_release(&prefix);
1986 * This is the same as for_each_fullref_in(), but it tries to iterate
1987 * only over the patterns we'll care about. Note that it _doesn't_ do a full
1988 * pattern match, so the callback still has to match each ref individually.
1990 static int for_each_fullref_in_pattern(struct ref_filter *filter,
1995 struct string_list prefixes = STRING_LIST_INIT_DUP;
1996 struct string_list_item *prefix;
1999 if (!filter->match_as_path) {
2001 * in this case, the patterns are applied after
2002 * prefixes like "refs/heads/" etc. are stripped off,
2003 * so we have to look at everything:
2005 return for_each_fullref_in("", cb, cb_data, broken);
2008 if (filter->ignore_case) {
2010 * we can't handle case-insensitive comparisons,
2011 * so just return everything and let the caller
2014 return for_each_fullref_in("", cb, cb_data, broken);
2017 if (!filter->name_patterns[0]) {
2018 /* no patterns; we have to look at everything */
2019 return for_each_fullref_in("", cb, cb_data, broken);
2022 find_longest_prefixes(&prefixes, filter->name_patterns);
2024 for_each_string_list_item(prefix, &prefixes) {
2025 ret = for_each_fullref_in(prefix->string, cb, cb_data, broken);
2030 string_list_clear(&prefixes, 0);
2035 * Given a ref (oid, refname), check if the ref belongs to the array
2036 * of oids. If the given ref is a tag, check if the given tag points
2037 * at one of the oids in the given oid array.
2039 * 1. Only a single level of indirection is obtained, we might want to
2040 * change this to account for multiple levels (e.g. annotated tags
2041 * pointing to annotated tags pointing to a commit.)
2042 * 2. As the refs are cached we might know what refname peels to without
2043 * the need to parse the object via parse_object(). peel_ref() might be a
2044 * more efficient alternative to obtain the pointee.
2046 static const struct object_id *match_points_at(struct oid_array *points_at,
2047 const struct object_id *oid,
2048 const char *refname)
2050 const struct object_id *tagged_oid = NULL;
2053 if (oid_array_lookup(points_at, oid) >= 0)
2055 obj = parse_object(the_repository, oid);
2057 die(_("malformed object at '%s'"), refname);
2058 if (obj->type == OBJ_TAG)
2059 tagged_oid = get_tagged_oid((struct tag *)obj);
2060 if (tagged_oid && oid_array_lookup(points_at, tagged_oid) >= 0)
2066 * Allocate space for a new ref_array_item and copy the name and oid to it.
2068 * Callers can then fill in other struct members at their leisure.
2070 static struct ref_array_item *new_ref_array_item(const char *refname,
2071 const struct object_id *oid)
2073 struct ref_array_item *ref;
2075 FLEX_ALLOC_STR(ref, refname, refname);
2076 oidcpy(&ref->objectname, oid);
2081 struct ref_array_item *ref_array_push(struct ref_array *array,
2082 const char *refname,
2083 const struct object_id *oid)
2085 struct ref_array_item *ref = new_ref_array_item(refname, oid);
2087 ALLOC_GROW(array->items, array->nr + 1, array->alloc);
2088 array->items[array->nr++] = ref;
2093 static int ref_kind_from_refname(const char *refname)
2101 { "refs/heads/" , FILTER_REFS_BRANCHES },
2102 { "refs/remotes/" , FILTER_REFS_REMOTES },
2103 { "refs/tags/", FILTER_REFS_TAGS}
2106 if (!strcmp(refname, "HEAD"))
2107 return FILTER_REFS_DETACHED_HEAD;
2109 for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
2110 if (starts_with(refname, ref_kind[i].prefix))
2111 return ref_kind[i].kind;
2114 return FILTER_REFS_OTHERS;
2117 static int filter_ref_kind(struct ref_filter *filter, const char *refname)
2119 if (filter->kind == FILTER_REFS_BRANCHES ||
2120 filter->kind == FILTER_REFS_REMOTES ||
2121 filter->kind == FILTER_REFS_TAGS)
2122 return filter->kind;
2123 return ref_kind_from_refname(refname);
2126 struct ref_filter_cbdata {
2127 struct ref_array *array;
2128 struct ref_filter *filter;
2129 struct contains_cache contains_cache;
2130 struct contains_cache no_contains_cache;
2134 * A call-back given to for_each_ref(). Filter refs and keep them for
2135 * later object processing.
2137 static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
2139 struct ref_filter_cbdata *ref_cbdata = cb_data;
2140 struct ref_filter *filter = ref_cbdata->filter;
2141 struct ref_array_item *ref;
2142 struct commit *commit = NULL;
2145 if (flag & REF_BAD_NAME) {
2146 warning(_("ignoring ref with broken name %s"), refname);
2150 if (flag & REF_ISBROKEN) {
2151 warning(_("ignoring broken ref %s"), refname);
2155 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2156 kind = filter_ref_kind(filter, refname);
2157 if (!(kind & filter->kind))
2160 if (!filter_pattern_match(filter, refname))
2163 if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname))
2167 * A merge filter is applied on refs pointing to commits. Hence
2168 * obtain the commit using the 'oid' available and discard all
2169 * non-commits early. The actual filtering is done later.
2171 if (filter->reachable_from || filter->unreachable_from ||
2172 filter->with_commit || filter->no_commit || filter->verbose) {
2173 commit = lookup_commit_reference_gently(the_repository, oid, 1);
2176 /* We perform the filtering for the '--contains' option... */
2177 if (filter->with_commit &&
2178 !commit_contains(filter, commit, filter->with_commit, &ref_cbdata->contains_cache))
2180 /* ...or for the `--no-contains' option */
2181 if (filter->no_commit &&
2182 commit_contains(filter, commit, filter->no_commit, &ref_cbdata->no_contains_cache))
2187 * We do not open the object yet; sort may only need refname
2188 * to do its job and the resulting list may yet to be pruned
2189 * by maxcount logic.
2191 ref = ref_array_push(ref_cbdata->array, refname, oid);
2192 ref->commit = commit;
2199 /* Free memory allocated for a ref_array_item */
2200 static void free_array_item(struct ref_array_item *item)
2202 free((char *)item->symref);
2205 for (i = 0; i < used_atom_cnt; i++)
2206 free((char *)item->value[i].s);
2212 /* Free all memory allocated for ref_array */
2213 void ref_array_clear(struct ref_array *array)
2217 for (i = 0; i < array->nr; i++)
2218 free_array_item(array->items[i]);
2219 FREE_AND_NULL(array->items);
2220 array->nr = array->alloc = 0;
2222 for (i = 0; i < used_atom_cnt; i++)
2223 free((char *)used_atom[i].name);
2224 FREE_AND_NULL(used_atom);
2227 if (ref_to_worktree_map.worktrees) {
2228 hashmap_clear_and_free(&(ref_to_worktree_map.map),
2229 struct ref_to_worktree_entry, ent);
2230 free_worktrees(ref_to_worktree_map.worktrees);
2231 ref_to_worktree_map.worktrees = NULL;
2235 #define EXCLUDE_REACHED 0
2236 #define INCLUDE_REACHED 1
2237 static void reach_filter(struct ref_array *array,
2238 struct commit_list *check_reachable,
2239 int include_reached)
2241 struct rev_info revs;
2243 struct commit **to_clear;
2244 struct commit_list *cr;
2246 if (!check_reachable)
2249 to_clear = xcalloc(sizeof(struct commit *), array->nr);
2251 repo_init_revisions(the_repository, &revs, NULL);
2253 for (i = 0; i < array->nr; i++) {
2254 struct ref_array_item *item = array->items[i];
2255 add_pending_object(&revs, &item->commit->object, item->refname);
2256 to_clear[i] = item->commit;
2259 for (cr = check_reachable; cr; cr = cr->next) {
2260 struct commit *merge_commit = cr->item;
2261 merge_commit->object.flags |= UNINTERESTING;
2262 add_pending_object(&revs, &merge_commit->object, "");
2266 if (prepare_revision_walk(&revs))
2267 die(_("revision walk setup failed"));
2272 for (i = 0; i < old_nr; i++) {
2273 struct ref_array_item *item = array->items[i];
2274 struct commit *commit = item->commit;
2276 int is_merged = !!(commit->object.flags & UNINTERESTING);
2278 if (is_merged == include_reached)
2279 array->items[array->nr++] = array->items[i];
2281 free_array_item(item);
2284 clear_commit_marks_many(old_nr, to_clear, ALL_REV_FLAGS);
2286 while (check_reachable) {
2287 struct commit *merge_commit = pop_commit(&check_reachable);
2288 clear_commit_marks(merge_commit, ALL_REV_FLAGS);
2295 * API for filtering a set of refs. Based on the type of refs the user
2296 * has requested, we iterate through those refs and apply filters
2297 * as per the given ref_filter structure and finally store the
2298 * filtered refs in the ref_array structure.
2300 int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
2302 struct ref_filter_cbdata ref_cbdata;
2304 unsigned int broken = 0;
2306 ref_cbdata.array = array;
2307 ref_cbdata.filter = filter;
2309 if (type & FILTER_REFS_INCLUDE_BROKEN)
2311 filter->kind = type & FILTER_REFS_KIND_MASK;
2313 init_contains_cache(&ref_cbdata.contains_cache);
2314 init_contains_cache(&ref_cbdata.no_contains_cache);
2316 /* Simple per-ref filtering */
2318 die("filter_refs: invalid type");
2321 * For common cases where we need only branches or remotes or tags,
2322 * we only iterate through those refs. If a mix of refs is needed,
2323 * we iterate over all refs and filter out required refs with the help
2324 * of filter_ref_kind().
2326 if (filter->kind == FILTER_REFS_BRANCHES)
2327 ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken);
2328 else if (filter->kind == FILTER_REFS_REMOTES)
2329 ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken);
2330 else if (filter->kind == FILTER_REFS_TAGS)
2331 ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken);
2332 else if (filter->kind & FILTER_REFS_ALL)
2333 ret = for_each_fullref_in_pattern(filter, ref_filter_handler, &ref_cbdata, broken);
2334 if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
2335 head_ref(ref_filter_handler, &ref_cbdata);
2338 clear_contains_cache(&ref_cbdata.contains_cache);
2339 clear_contains_cache(&ref_cbdata.no_contains_cache);
2341 /* Filters that need revision walking */
2342 reach_filter(array, filter->reachable_from, INCLUDE_REACHED);
2343 reach_filter(array, filter->unreachable_from, EXCLUDE_REACHED);
2348 static int compare_detached_head(struct ref_array_item *a, struct ref_array_item *b)
2350 if (!(a->kind ^ b->kind))
2351 BUG("ref_kind_from_refname() should only mark one ref as HEAD");
2352 if (a->kind & FILTER_REFS_DETACHED_HEAD)
2354 else if (b->kind & FILTER_REFS_DETACHED_HEAD)
2356 BUG("should have died in the xor check above");
2360 static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
2362 struct atom_value *va, *vb;
2364 int cmp_detached_head = 0;
2365 cmp_type cmp_type = used_atom[s->atom].type;
2366 struct strbuf err = STRBUF_INIT;
2368 if (get_ref_atom_value(a, s->atom, &va, &err))
2370 if (get_ref_atom_value(b, s->atom, &vb, &err))
2372 strbuf_release(&err);
2373 if (s->sort_flags & REF_SORTING_DETACHED_HEAD_FIRST &&
2374 ((a->kind | b->kind) & FILTER_REFS_DETACHED_HEAD)) {
2375 cmp = compare_detached_head(a, b);
2376 cmp_detached_head = 1;
2377 } else if (s->sort_flags & REF_SORTING_VERSION) {
2378 cmp = versioncmp(va->s, vb->s);
2379 } else if (cmp_type == FIELD_STR) {
2380 int (*cmp_fn)(const char *, const char *);
2381 cmp_fn = s->sort_flags & REF_SORTING_ICASE
2382 ? strcasecmp : strcmp;
2383 cmp = cmp_fn(va->s, vb->s);
2385 if (va->value < vb->value)
2387 else if (va->value == vb->value)
2393 return (s->sort_flags & REF_SORTING_REVERSE && !cmp_detached_head)
2397 static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
2399 struct ref_array_item *a = *((struct ref_array_item **)a_);
2400 struct ref_array_item *b = *((struct ref_array_item **)b_);
2401 struct ref_sorting *s;
2403 for (s = ref_sorting; s; s = s->next) {
2404 int cmp = cmp_ref_sorting(s, a, b);
2409 return s && s->sort_flags & REF_SORTING_ICASE ?
2410 strcasecmp(a->refname, b->refname) :
2411 strcmp(a->refname, b->refname);
2414 void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting,
2415 unsigned int mask, int on)
2417 for (; sorting; sorting = sorting->next) {
2419 sorting->sort_flags |= mask;
2421 sorting->sort_flags &= ~mask;
2425 void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
2427 QSORT_S(array->items, array->nr, compare_refs, sorting);
2430 static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
2432 struct strbuf *s = &state->stack->output;
2434 while (*cp && (!ep || cp < ep)) {
2439 int ch = hex2chr(cp + 1);
2441 strbuf_addch(s, ch);
2447 strbuf_addch(s, *cp);
2452 int format_ref_array_item(struct ref_array_item *info,
2453 const struct ref_format *format,
2454 struct strbuf *final_buf,
2455 struct strbuf *error_buf)
2457 const char *cp, *sp, *ep;
2458 struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
2460 state.quote_style = format->quote_style;
2461 push_stack_element(&state.stack);
2463 for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) {
2464 struct atom_value *atomv;
2467 ep = strchr(sp, ')');
2469 append_literal(cp, sp, &state);
2470 pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf);
2471 if (pos < 0 || get_ref_atom_value(info, pos, &atomv, error_buf) ||
2472 atomv->handler(atomv, &state, error_buf)) {
2473 pop_stack_element(&state.stack);
2478 sp = cp + strlen(cp);
2479 append_literal(cp, sp, &state);
2481 if (format->need_color_reset_at_eol) {
2482 struct atom_value resetv;
2483 resetv.s = GIT_COLOR_RESET;
2484 if (append_atom(&resetv, &state, error_buf)) {
2485 pop_stack_element(&state.stack);
2489 if (state.stack->prev) {
2490 pop_stack_element(&state.stack);
2491 return strbuf_addf_ret(error_buf, -1, _("format: %%(end) atom missing"));
2493 strbuf_addbuf(final_buf, &state.stack->output);
2494 pop_stack_element(&state.stack);
2498 void show_ref_array_item(struct ref_array_item *info,
2499 const struct ref_format *format)
2501 struct strbuf final_buf = STRBUF_INIT;
2502 struct strbuf error_buf = STRBUF_INIT;
2504 if (format_ref_array_item(info, format, &final_buf, &error_buf))
2505 die("%s", error_buf.buf);
2506 fwrite(final_buf.buf, 1, final_buf.len, stdout);
2507 strbuf_release(&error_buf);
2508 strbuf_release(&final_buf);
2512 void pretty_print_ref(const char *name, const struct object_id *oid,
2513 const struct ref_format *format)
2515 struct ref_array_item *ref_item;
2516 ref_item = new_ref_array_item(name, oid);
2517 ref_item->kind = ref_kind_from_refname(name);
2518 show_ref_array_item(ref_item, format);
2519 free_array_item(ref_item);
2522 static int parse_sorting_atom(const char *atom)
2525 * This parses an atom using a dummy ref_format, since we don't
2526 * actually care about the formatting details.
2528 struct ref_format dummy = REF_FORMAT_INIT;
2529 const char *end = atom + strlen(atom);
2530 struct strbuf err = STRBUF_INIT;
2531 int res = parse_ref_filter_atom(&dummy, atom, end, &err);
2534 strbuf_release(&err);
2538 /* If no sorting option is given, use refname to sort as default */
2539 struct ref_sorting *ref_default_sorting(void)
2541 static const char cstr_name[] = "refname";
2543 struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
2545 sorting->next = NULL;
2546 sorting->atom = parse_sorting_atom(cstr_name);
2550 void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
2552 struct ref_sorting *s;
2554 s = xcalloc(1, sizeof(*s));
2555 s->next = *sorting_tail;
2559 s->sort_flags |= REF_SORTING_REVERSE;
2562 if (skip_prefix(arg, "version:", &arg) ||
2563 skip_prefix(arg, "v:", &arg))
2564 s->sort_flags |= REF_SORTING_VERSION;
2565 s->atom = parse_sorting_atom(arg);
2568 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
2571 * NEEDSWORK: We should probably clear the list in this case, but we've
2572 * already munged the global used_atoms list, which would need to be
2575 BUG_ON_OPT_NEG(unset);
2577 parse_ref_sorting(opt->value, arg);
2581 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
2583 struct ref_filter *rf = opt->value;
2584 struct object_id oid;
2585 struct commit *merge_commit;
2587 BUG_ON_OPT_NEG(unset);
2589 if (get_oid(arg, &oid))
2590 die(_("malformed object name %s"), arg);
2592 merge_commit = lookup_commit_reference_gently(the_repository, &oid, 0);
2595 return error(_("option `%s' must point to a commit"), opt->long_name);
2597 if (starts_with(opt->long_name, "no"))
2598 commit_list_insert(merge_commit, &rf->unreachable_from);
2600 commit_list_insert(merge_commit, &rf->reachable_from);