3 #include "parse-options.h"
11 #include "ref-filter.h"
14 #include "git-compat-util.h"
18 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
26 * An atom is a valid field atom listed below, possibly prefixed with
27 * a "*" to denote deref_tag().
29 * We parse given format string and sort specifiers, and make a list
30 * of properties that we need to extract out of objects. ref_array_item
31 * structure will hold an array of values extracted that can be
32 * indexed with the "atom number", which is an index into this
35 static struct used_atom {
39 char color[COLOR_MAXLEN];
41 enum { RR_NORMAL, RR_SHORTEN, RR_TRACK, RR_TRACKSHORT }
44 enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
47 enum { O_FULL, O_SHORT } objectname;
50 static int used_atom_cnt, need_tagged, need_symref;
51 static int need_color_reset_at_eol;
53 static void color_atom_parser(struct used_atom *atom, const char *color_value)
56 die(_("expected format: %%(color:<color>)"));
57 if (color_parse(color_value, atom->u.color) < 0)
58 die(_("unrecognized color: %%(color:%s)"), color_value);
61 static void remote_ref_atom_parser(struct used_atom *atom, const char *arg)
64 atom->u.remote_ref = RR_NORMAL;
65 else if (!strcmp(arg, "short"))
66 atom->u.remote_ref = RR_SHORTEN;
67 else if (!strcmp(arg, "track"))
68 atom->u.remote_ref = RR_TRACK;
69 else if (!strcmp(arg, "trackshort"))
70 atom->u.remote_ref = RR_TRACKSHORT;
72 die(_("unrecognized format: %%(%s)"), atom->name);
75 static void body_atom_parser(struct used_atom *atom, const char *arg)
78 die(_("%%(body) does not take arguments"));
79 atom->u.contents.option = C_BODY_DEP;
82 static void subject_atom_parser(struct used_atom *atom, const char *arg)
85 die(_("%%(subject) does not take arguments"));
86 atom->u.contents.option = C_SUB;
89 static void trailers_atom_parser(struct used_atom *atom, const char *arg)
92 die(_("%%(trailers) does not take arguments"));
93 atom->u.contents.option = C_TRAILERS;
96 static void contents_atom_parser(struct used_atom *atom, const char *arg)
99 atom->u.contents.option = C_BARE;
100 else if (!strcmp(arg, "body"))
101 atom->u.contents.option = C_BODY;
102 else if (!strcmp(arg, "signature"))
103 atom->u.contents.option = C_SIG;
104 else if (!strcmp(arg, "subject"))
105 atom->u.contents.option = C_SUB;
106 else if (!strcmp(arg, "trailers"))
107 atom->u.contents.option = C_TRAILERS;
108 else if (skip_prefix(arg, "lines=", &arg)) {
109 atom->u.contents.option = C_LINES;
110 if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
111 die(_("positive value expected contents:lines=%s"), arg);
113 die(_("unrecognized %%(contents) argument: %s"), arg);
116 static void objectname_atom_parser(struct used_atom *atom, const char *arg)
119 atom->u.objectname = O_FULL;
120 else if (!strcmp(arg, "short"))
121 atom->u.objectname = O_SHORT;
123 die(_("unrecognized %%(objectname) argument: %s"), arg);
126 static align_type parse_align_position(const char *s)
128 if (!strcmp(s, "right"))
130 else if (!strcmp(s, "middle"))
132 else if (!strcmp(s, "left"))
137 static void align_atom_parser(struct used_atom *atom, const char *arg)
139 struct align *align = &atom->u.align;
140 struct string_list params = STRING_LIST_INIT_DUP;
142 unsigned int width = ~0U;
145 die(_("expected format: %%(align:<width>,<position>)"));
147 align->position = ALIGN_LEFT;
149 string_list_split(¶ms, arg, ',', -1);
150 for (i = 0; i < params.nr; i++) {
151 const char *s = params.items[i].string;
154 if (skip_prefix(s, "position=", &s)) {
155 position = parse_align_position(s);
157 die(_("unrecognized position:%s"), s);
158 align->position = position;
159 } else if (skip_prefix(s, "width=", &s)) {
160 if (strtoul_ui(s, 10, &width))
161 die(_("unrecognized width:%s"), s);
162 } else if (!strtoul_ui(s, 10, &width))
164 else if ((position = parse_align_position(s)) >= 0)
165 align->position = position;
167 die(_("unrecognized %%(align) argument: %s"), s);
171 die(_("positive width expected with the %%(align) atom"));
172 align->width = width;
173 string_list_clear(¶ms, 0);
179 void (*parser)(struct used_atom *atom, const char *arg);
183 { "objectsize", FIELD_ULONG },
184 { "objectname", FIELD_STR, objectname_atom_parser },
187 { "numparent", FIELD_ULONG },
194 { "authordate", FIELD_TIME },
197 { "committeremail" },
198 { "committerdate", FIELD_TIME },
202 { "taggerdate", FIELD_TIME },
204 { "creatordate", FIELD_TIME },
205 { "subject", FIELD_STR, subject_atom_parser },
206 { "body", FIELD_STR, body_atom_parser },
207 { "trailers", FIELD_STR, trailers_atom_parser },
208 { "contents", FIELD_STR, contents_atom_parser },
209 { "upstream", FIELD_STR, remote_ref_atom_parser },
210 { "push", FIELD_STR, remote_ref_atom_parser },
214 { "color", FIELD_STR, color_atom_parser },
215 { "align", FIELD_STR, align_atom_parser },
219 #define REF_FORMATTING_STATE_INIT { 0, NULL }
221 struct ref_formatting_stack {
222 struct ref_formatting_stack *prev;
223 struct strbuf output;
224 void (*at_end)(struct ref_formatting_stack *stack);
228 struct ref_formatting_state {
230 struct ref_formatting_stack *stack;
238 void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
239 unsigned long ul; /* used for sorting when not FIELD_STR */
243 * Used to parse format string and sort specifiers
245 int parse_ref_filter_atom(const char *atom, const char *ep)
252 if (*sp == '*' && sp < ep)
255 die(_("malformed field name: %.*s"), (int)(ep-atom), atom);
257 /* Do we have the atom already used elsewhere? */
258 for (i = 0; i < used_atom_cnt; i++) {
259 int len = strlen(used_atom[i].name);
260 if (len == ep - atom && !memcmp(used_atom[i].name, atom, len))
265 * If the atom name has a colon, strip it and everything after
266 * it off - it specifies the format for this entry, and
267 * shouldn't be used for checking against the valid_atom
270 arg = memchr(sp, ':', ep - sp);
271 atom_len = (arg ? arg : ep) - sp;
273 /* Is the atom a valid one? */
274 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
275 int len = strlen(valid_atom[i].name);
276 if (len == atom_len && !memcmp(valid_atom[i].name, sp, len))
280 if (ARRAY_SIZE(valid_atom) <= i)
281 die(_("unknown field name: %.*s"), (int)(ep-atom), atom);
283 /* Add it in, including the deref prefix */
286 REALLOC_ARRAY(used_atom, used_atom_cnt);
287 used_atom[at].name = xmemdupz(atom, ep - atom);
288 used_atom[at].type = valid_atom[i].cmp_type;
290 arg = used_atom[at].name + (arg - atom) + 1;
291 memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
292 if (valid_atom[i].parser)
293 valid_atom[i].parser(&used_atom[at], arg);
296 if (!strcmp(used_atom[at].name, "symref"))
301 static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
303 switch (quote_style) {
305 strbuf_addstr(s, str);
308 sq_quote_buf(s, str);
311 perl_quote_buf(s, str);
314 python_quote_buf(s, str);
317 tcl_quote_buf(s, str);
322 static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
325 * Quote formatting is only done when the stack has a single
326 * element. Otherwise quote formatting is done on the
327 * element's entire output strbuf when the %(end) atom is
330 if (!state->stack->prev)
331 quote_formatting(&state->stack->output, v->s, state->quote_style);
333 strbuf_addstr(&state->stack->output, v->s);
336 static void push_stack_element(struct ref_formatting_stack **stack)
338 struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
340 strbuf_init(&s->output, 0);
345 static void pop_stack_element(struct ref_formatting_stack **stack)
347 struct ref_formatting_stack *current = *stack;
348 struct ref_formatting_stack *prev = current->prev;
351 strbuf_addbuf(&prev->output, ¤t->output);
352 strbuf_release(¤t->output);
357 static void end_align_handler(struct ref_formatting_stack *stack)
359 struct align *align = (struct align *)stack->at_end_data;
360 struct strbuf s = STRBUF_INIT;
362 strbuf_utf8_align(&s, align->position, align->width, stack->output.buf);
363 strbuf_swap(&stack->output, &s);
367 static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
369 struct ref_formatting_stack *new;
371 push_stack_element(&state->stack);
373 new->at_end = end_align_handler;
374 new->at_end_data = &atomv->u.align;
377 static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
379 struct ref_formatting_stack *current = state->stack;
380 struct strbuf s = STRBUF_INIT;
382 if (!current->at_end)
383 die(_("format: %%(end) atom used without corresponding atom"));
384 current->at_end(current);
387 * Perform quote formatting when the stack element is that of
388 * a supporting atom. If nested then perform quote formatting
389 * only on the topmost supporting atom.
391 if (!state->stack->prev->prev) {
392 quote_formatting(&s, current->output.buf, state->quote_style);
393 strbuf_swap(¤t->output, &s);
396 pop_stack_element(&state->stack);
400 * In a format string, find the next occurrence of %(atom).
402 static const char *find_next(const char *cp)
407 * %( is the start of an atom;
408 * %% is a quoted per-cent.
412 else if (cp[1] == '%')
413 cp++; /* skip over two % */
414 /* otherwise this is a singleton, literal % */
422 * Make sure the format string is well formed, and parse out
425 int verify_ref_format(const char *format)
429 need_color_reset_at_eol = 0;
430 for (cp = format; *cp && (sp = find_next(cp)); ) {
431 const char *color, *ep = strchr(sp, ')');
435 return error(_("malformed format string %s"), sp);
436 /* sp points at "%(" and ep points at the closing ")" */
437 at = parse_ref_filter_atom(sp + 2, ep);
440 if (skip_prefix(used_atom[at].name, "color:", &color))
441 need_color_reset_at_eol = !!strcmp(color, "reset");
447 * Given an object name, read the object data and size, and return a
448 * "struct object". If the object data we are returning is also borrowed
449 * by the "struct object" representation, set *eaten as well---it is a
450 * signal from parse_object_buffer to us not to free the buffer.
452 static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
454 enum object_type type;
455 void *buf = read_sha1_file(sha1, &type, sz);
458 *obj = parse_object_buffer(sha1, type, *sz, buf, eaten);
464 static int grab_objectname(const char *name, const unsigned char *sha1,
465 struct atom_value *v, struct used_atom *atom)
467 if (starts_with(name, "objectname")) {
468 if (atom->u.objectname == O_SHORT) {
469 v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
471 } else if (atom->u.objectname == O_FULL) {
472 v->s = xstrdup(sha1_to_hex(sha1));
475 die("BUG: unknown %%(objectname) option");
480 /* See grab_values */
481 static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
485 for (i = 0; i < used_atom_cnt; i++) {
486 const char *name = used_atom[i].name;
487 struct atom_value *v = &val[i];
488 if (!!deref != (*name == '*'))
492 if (!strcmp(name, "objecttype"))
493 v->s = typename(obj->type);
494 else if (!strcmp(name, "objectsize")) {
496 v->s = xstrfmt("%lu", sz);
499 grab_objectname(name, obj->oid.hash, v, &used_atom[i]);
503 /* See grab_values */
504 static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
507 struct tag *tag = (struct tag *) obj;
509 for (i = 0; i < used_atom_cnt; i++) {
510 const char *name = used_atom[i].name;
511 struct atom_value *v = &val[i];
512 if (!!deref != (*name == '*'))
516 if (!strcmp(name, "tag"))
518 else if (!strcmp(name, "type") && tag->tagged)
519 v->s = typename(tag->tagged->type);
520 else if (!strcmp(name, "object") && tag->tagged)
521 v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
525 /* See grab_values */
526 static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
529 struct commit *commit = (struct commit *) obj;
531 for (i = 0; i < used_atom_cnt; i++) {
532 const char *name = used_atom[i].name;
533 struct atom_value *v = &val[i];
534 if (!!deref != (*name == '*'))
538 if (!strcmp(name, "tree")) {
539 v->s = xstrdup(oid_to_hex(&commit->tree->object.oid));
541 else if (!strcmp(name, "numparent")) {
542 v->ul = commit_list_count(commit->parents);
543 v->s = xstrfmt("%lu", v->ul);
545 else if (!strcmp(name, "parent")) {
546 struct commit_list *parents;
547 struct strbuf s = STRBUF_INIT;
548 for (parents = commit->parents; parents; parents = parents->next) {
549 struct commit *parent = parents->item;
550 if (parents != commit->parents)
551 strbuf_addch(&s, ' ');
552 strbuf_addstr(&s, oid_to_hex(&parent->object.oid));
554 v->s = strbuf_detach(&s, NULL);
559 static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz)
563 if (!strncmp(buf, who, wholen) &&
565 return buf + wholen + 1;
566 eol = strchr(buf, '\n');
571 return ""; /* end of header */
577 static const char *copy_line(const char *buf)
579 const char *eol = strchrnul(buf, '\n');
580 return xmemdupz(buf, eol - buf);
583 static const char *copy_name(const char *buf)
586 for (cp = buf; *cp && *cp != '\n'; cp++) {
587 if (!strncmp(cp, " <", 2))
588 return xmemdupz(buf, cp - buf);
593 static const char *copy_email(const char *buf)
595 const char *email = strchr(buf, '<');
599 eoemail = strchr(email, '>');
602 return xmemdupz(email, eoemail + 1 - email);
605 static char *copy_subject(const char *buf, unsigned long len)
607 char *r = xmemdupz(buf, len);
610 for (i = 0; i < len; i++)
617 static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
619 const char *eoemail = strstr(buf, "> ");
621 unsigned long timestamp;
623 struct date_mode date_mode = { DATE_NORMAL };
627 * We got here because atomname ends in "date" or "date<something>";
628 * it's not possible that <something> is not ":<format>" because
629 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
630 * ":" means no format is specified, and use the default.
632 formatp = strchr(atomname, ':');
633 if (formatp != NULL) {
635 parse_date_format(formatp, &date_mode);
640 timestamp = strtoul(eoemail + 2, &zone, 10);
641 if (timestamp == ULONG_MAX)
643 tz = strtol(zone, NULL, 10);
644 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
646 v->s = xstrdup(show_date(timestamp, tz, &date_mode));
654 /* See grab_values */
655 static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
658 int wholen = strlen(who);
659 const char *wholine = NULL;
661 for (i = 0; i < used_atom_cnt; i++) {
662 const char *name = used_atom[i].name;
663 struct atom_value *v = &val[i];
664 if (!!deref != (*name == '*'))
668 if (strncmp(who, name, wholen))
670 if (name[wholen] != 0 &&
671 strcmp(name + wholen, "name") &&
672 strcmp(name + wholen, "email") &&
673 !starts_with(name + wholen, "date"))
676 wholine = find_wholine(who, wholen, buf, sz);
678 return; /* no point looking for it */
679 if (name[wholen] == 0)
680 v->s = copy_line(wholine);
681 else if (!strcmp(name + wholen, "name"))
682 v->s = copy_name(wholine);
683 else if (!strcmp(name + wholen, "email"))
684 v->s = copy_email(wholine);
685 else if (starts_with(name + wholen, "date"))
686 grab_date(wholine, v, name);
690 * For a tag or a commit object, if "creator" or "creatordate" is
691 * requested, do something special.
693 if (strcmp(who, "tagger") && strcmp(who, "committer"))
694 return; /* "author" for commit object is not wanted */
696 wholine = find_wholine(who, wholen, buf, sz);
699 for (i = 0; i < used_atom_cnt; i++) {
700 const char *name = used_atom[i].name;
701 struct atom_value *v = &val[i];
702 if (!!deref != (*name == '*'))
707 if (starts_with(name, "creatordate"))
708 grab_date(wholine, v, name);
709 else if (!strcmp(name, "creator"))
710 v->s = copy_line(wholine);
714 static void find_subpos(const char *buf, unsigned long sz,
715 const char **sub, unsigned long *sublen,
716 const char **body, unsigned long *bodylen,
717 unsigned long *nonsiglen,
718 const char **sig, unsigned long *siglen)
721 /* skip past header until we hit empty line */
722 while (*buf && *buf != '\n') {
723 eol = strchrnul(buf, '\n');
728 /* skip any empty lines */
732 /* parse signature first; we might not even have a subject line */
733 *sig = buf + parse_signature(buf, strlen(buf));
734 *siglen = strlen(*sig);
736 /* subject is first non-empty line */
738 /* subject goes to first empty line */
739 while (buf < *sig && *buf && *buf != '\n') {
740 eol = strchrnul(buf, '\n');
745 *sublen = buf - *sub;
746 /* drop trailing newline, if present */
747 if (*sublen && (*sub)[*sublen - 1] == '\n')
750 /* skip any empty lines */
754 *bodylen = strlen(buf);
755 *nonsiglen = *sig - buf;
759 * If 'lines' is greater than 0, append that many lines from the given
760 * 'buf' of length 'size' to the given strbuf.
762 static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
765 const char *sp, *eol;
770 for (i = 0; i < lines && sp < buf + size; i++) {
772 strbuf_addstr(out, "\n ");
773 eol = memchr(sp, '\n', size - (sp - buf));
774 len = eol ? eol - sp : size - (sp - buf);
775 strbuf_add(out, sp, len);
782 /* See grab_values */
783 static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
786 const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
787 unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
789 for (i = 0; i < used_atom_cnt; i++) {
790 struct used_atom *atom = &used_atom[i];
791 const char *name = atom->name;
792 struct atom_value *v = &val[i];
793 if (!!deref != (*name == '*'))
797 if (strcmp(name, "subject") &&
798 strcmp(name, "body") &&
799 strcmp(name, "trailers") &&
800 !starts_with(name, "contents"))
805 &bodypos, &bodylen, &nonsiglen,
808 if (atom->u.contents.option == C_SUB)
809 v->s = copy_subject(subpos, sublen);
810 else if (atom->u.contents.option == C_BODY_DEP)
811 v->s = xmemdupz(bodypos, bodylen);
812 else if (atom->u.contents.option == C_BODY)
813 v->s = xmemdupz(bodypos, nonsiglen);
814 else if (atom->u.contents.option == C_SIG)
815 v->s = xmemdupz(sigpos, siglen);
816 else if (atom->u.contents.option == C_LINES) {
817 struct strbuf s = STRBUF_INIT;
818 const char *contents_end = bodylen + bodypos - siglen;
820 /* Size is the length of the message after removing the signature */
821 append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
822 v->s = strbuf_detach(&s, NULL);
823 } else if (atom->u.contents.option == C_TRAILERS) {
824 struct trailer_info info;
826 /* Search for trailer info */
827 trailer_info_get(&info, subpos);
828 v->s = xmemdupz(info.trailer_start,
829 info.trailer_end - info.trailer_start);
830 trailer_info_release(&info);
831 } else if (atom->u.contents.option == C_BARE)
832 v->s = xstrdup(subpos);
837 * We want to have empty print-string for field requests
838 * that do not apply (e.g. "authordate" for a tag object)
840 static void fill_missing_values(struct atom_value *val)
843 for (i = 0; i < used_atom_cnt; i++) {
844 struct atom_value *v = &val[i];
851 * val is a list of atom_value to hold returned values. Extract
852 * the values for atoms in used_atom array out of (obj, buf, sz).
853 * when deref is false, (obj, buf, sz) is the object that is
854 * pointed at by the ref itself; otherwise it is the object the
855 * ref (which is a tag) refers to.
857 static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
859 grab_common_values(val, deref, obj, buf, sz);
862 grab_tag_values(val, deref, obj, buf, sz);
863 grab_sub_body_contents(val, deref, obj, buf, sz);
864 grab_person("tagger", val, deref, obj, buf, sz);
867 grab_commit_values(val, deref, obj, buf, sz);
868 grab_sub_body_contents(val, deref, obj, buf, sz);
869 grab_person("author", val, deref, obj, buf, sz);
870 grab_person("committer", val, deref, obj, buf, sz);
873 /* grab_tree_values(val, deref, obj, buf, sz); */
876 /* grab_blob_values(val, deref, obj, buf, sz); */
879 die("Eh? Object of type %d?", obj->type);
883 static inline char *copy_advance(char *dst, const char *src)
890 static const char *strip_ref_components(const char *refname, const char *nr_arg)
893 long nr = strtol(nr_arg, &end, 10);
895 const char *start = refname;
897 if (nr < 1 || *end != '\0')
898 die(_(":strip= requires a positive integer argument"));
903 die(_("ref '%s' does not have %ld components to :strip"),
913 static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
914 struct branch *branch, const char **s)
916 int num_ours, num_theirs;
917 if (atom->u.remote_ref == RR_SHORTEN)
918 *s = shorten_unambiguous_ref(refname, warn_ambiguous_refs);
919 else if (atom->u.remote_ref == RR_TRACK) {
920 if (stat_tracking_info(branch, &num_ours,
924 if (!num_ours && !num_theirs)
927 *s = xstrfmt("[behind %d]", num_theirs);
928 else if (!num_theirs)
929 *s = xstrfmt("[ahead %d]", num_ours);
931 *s = xstrfmt("[ahead %d, behind %d]",
932 num_ours, num_theirs);
933 } else if (atom->u.remote_ref == RR_TRACKSHORT) {
934 if (stat_tracking_info(branch, &num_ours,
938 if (!num_ours && !num_theirs)
942 else if (!num_theirs)
946 } else /* RR_NORMAL */
951 * Parse the object referred by ref, and grab needed value.
953 static void populate_value(struct ref_array_item *ref)
959 const unsigned char *tagged;
961 ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
963 if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
964 unsigned char unused1[20];
965 ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
971 /* Fill in specials first */
972 for (i = 0; i < used_atom_cnt; i++) {
973 struct used_atom *atom = &used_atom[i];
974 const char *name = used_atom[i].name;
975 struct atom_value *v = &ref->value[i];
979 struct branch *branch = NULL;
981 v->handler = append_atom;
988 if (starts_with(name, "refname"))
989 refname = ref->refname;
990 else if (starts_with(name, "symref"))
991 refname = ref->symref ? ref->symref : "";
992 else if (starts_with(name, "upstream")) {
993 const char *branch_name;
994 /* only local branches may have an upstream */
995 if (!skip_prefix(ref->refname, "refs/heads/",
998 branch = branch_get(branch_name);
1000 refname = branch_get_upstream(branch, NULL);
1002 fill_remote_ref_details(atom, refname, branch, &v->s);
1004 } else if (starts_with(name, "push")) {
1005 const char *branch_name;
1006 if (!skip_prefix(ref->refname, "refs/heads/",
1009 branch = branch_get(branch_name);
1011 refname = branch_get_push(branch, NULL);
1014 fill_remote_ref_details(atom, refname, branch, &v->s);
1016 } else if (starts_with(name, "color:")) {
1017 v->s = atom->u.color;
1019 } else if (!strcmp(name, "flag")) {
1020 char buf[256], *cp = buf;
1021 if (ref->flag & REF_ISSYMREF)
1022 cp = copy_advance(cp, ",symref");
1023 if (ref->flag & REF_ISPACKED)
1024 cp = copy_advance(cp, ",packed");
1029 v->s = xstrdup(buf + 1);
1032 } else if (!deref && grab_objectname(name, ref->objectname, v, atom)) {
1034 } else if (!strcmp(name, "HEAD")) {
1036 unsigned char sha1[20];
1038 head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
1040 if (head && !strcmp(ref->refname, head))
1045 } else if (starts_with(name, "align")) {
1046 v->u.align = atom->u.align;
1047 v->handler = align_atom_handler;
1049 } else if (!strcmp(name, "end")) {
1050 v->handler = end_atom_handler;
1055 formatp = strchr(name, ':');
1060 if (!strcmp(formatp, "short"))
1061 refname = shorten_unambiguous_ref(refname,
1062 warn_ambiguous_refs);
1063 else if (skip_prefix(formatp, "strip=", &arg))
1064 refname = strip_ref_components(refname, arg);
1066 die(_("unknown %.*s format %s"),
1067 (int)(formatp - name), name, formatp);
1073 v->s = xstrfmt("%s^{}", refname);
1076 for (i = 0; i < used_atom_cnt; i++) {
1077 struct atom_value *v = &ref->value[i];
1084 buf = get_obj(ref->objectname, &obj, &size, &eaten);
1086 die(_("missing object %s for %s"),
1087 sha1_to_hex(ref->objectname), ref->refname);
1089 die(_("parse_object_buffer failed on %s for %s"),
1090 sha1_to_hex(ref->objectname), ref->refname);
1092 grab_values(ref->value, 0, obj, buf, size);
1097 * If there is no atom that wants to know about tagged
1098 * object, we are done.
1100 if (!need_tagged || (obj->type != OBJ_TAG))
1104 * If it is a tag object, see if we use a value that derefs
1105 * the object, and if we do grab the object it refers to.
1107 tagged = ((struct tag *)obj)->tagged->oid.hash;
1110 * NEEDSWORK: This derefs tag only once, which
1111 * is good to deal with chains of trust, but
1112 * is not consistent with what deref_tag() does
1113 * which peels the onion to the core.
1115 buf = get_obj(tagged, &obj, &size, &eaten);
1117 die(_("missing object %s for %s"),
1118 sha1_to_hex(tagged), ref->refname);
1120 die(_("parse_object_buffer failed on %s for %s"),
1121 sha1_to_hex(tagged), ref->refname);
1122 grab_values(ref->value, 1, obj, buf, size);
1128 * Given a ref, return the value for the atom. This lazily gets value
1129 * out of the object by calling populate value.
1131 static void get_ref_atom_value(struct ref_array_item *ref, int atom, struct atom_value **v)
1134 populate_value(ref);
1135 fill_missing_values(ref->value);
1137 *v = &ref->value[atom];
1140 enum contains_result {
1141 CONTAINS_UNKNOWN = -1,
1147 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1150 * At each recursion step, the stack items points to the commits whose
1151 * ancestors are to be inspected.
1153 struct contains_stack {
1155 struct contains_stack_entry {
1156 struct commit *commit;
1157 struct commit_list *parents;
1161 static int in_commit_list(const struct commit_list *want, struct commit *c)
1163 for (; want; want = want->next)
1164 if (!oidcmp(&want->item->object.oid, &c->object.oid))
1170 * Test whether the candidate or one of its parents is contained in the list.
1171 * Do not recurse to find out, though, but return -1 if inconclusive.
1173 static enum contains_result contains_test(struct commit *candidate,
1174 const struct commit_list *want)
1176 /* was it previously marked as containing a want commit? */
1177 if (candidate->object.flags & TMP_MARK)
1179 /* or marked as not possibly containing a want commit? */
1180 if (candidate->object.flags & UNINTERESTING)
1183 if (in_commit_list(want, candidate)) {
1184 candidate->object.flags |= TMP_MARK;
1188 if (parse_commit(candidate) < 0)
1194 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
1196 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
1197 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
1198 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
1201 static enum contains_result contains_tag_algo(struct commit *candidate,
1202 const struct commit_list *want)
1204 struct contains_stack contains_stack = { 0, 0, NULL };
1205 int result = contains_test(candidate, want);
1207 if (result != CONTAINS_UNKNOWN)
1210 push_to_contains_stack(candidate, &contains_stack);
1211 while (contains_stack.nr) {
1212 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
1213 struct commit *commit = entry->commit;
1214 struct commit_list *parents = entry->parents;
1217 commit->object.flags |= UNINTERESTING;
1218 contains_stack.nr--;
1221 * If we just popped the stack, parents->item has been marked,
1222 * therefore contains_test will return a meaningful 0 or 1.
1224 else switch (contains_test(parents->item, want)) {
1226 commit->object.flags |= TMP_MARK;
1227 contains_stack.nr--;
1230 entry->parents = parents->next;
1232 case CONTAINS_UNKNOWN:
1233 push_to_contains_stack(parents->item, &contains_stack);
1237 free(contains_stack.contains_stack);
1238 return contains_test(candidate, want);
1241 static int commit_contains(struct ref_filter *filter, struct commit *commit)
1243 if (filter->with_commit_tag_algo)
1244 return contains_tag_algo(commit, filter->with_commit);
1245 return is_descendant_of(commit, filter->with_commit);
1249 * Return 1 if the refname matches one of the patterns, otherwise 0.
1250 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1251 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1252 * matches "refs/heads/mas*", too).
1254 static int match_pattern(const struct ref_filter *filter, const char *refname)
1256 const char **patterns = filter->name_patterns;
1259 if (filter->ignore_case)
1260 flags |= WM_CASEFOLD;
1263 * When no '--format' option is given we need to skip the prefix
1264 * for matching refs of tags and branches.
1266 (void)(skip_prefix(refname, "refs/tags/", &refname) ||
1267 skip_prefix(refname, "refs/heads/", &refname) ||
1268 skip_prefix(refname, "refs/remotes/", &refname) ||
1269 skip_prefix(refname, "refs/", &refname));
1271 for (; *patterns; patterns++) {
1272 if (!wildmatch(*patterns, refname, flags, NULL))
1279 * Return 1 if the refname matches one of the patterns, otherwise 0.
1280 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1281 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1282 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1284 static int match_name_as_path(const struct ref_filter *filter, const char *refname)
1286 const char **pattern = filter->name_patterns;
1287 int namelen = strlen(refname);
1288 unsigned flags = WM_PATHNAME;
1290 if (filter->ignore_case)
1291 flags |= WM_CASEFOLD;
1293 for (; *pattern; pattern++) {
1294 const char *p = *pattern;
1295 int plen = strlen(p);
1297 if ((plen <= namelen) &&
1298 !strncmp(refname, p, plen) &&
1299 (refname[plen] == '\0' ||
1300 refname[plen] == '/' ||
1303 if (!wildmatch(p, refname, WM_PATHNAME, NULL))
1309 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1310 static int filter_pattern_match(struct ref_filter *filter, const char *refname)
1312 if (!*filter->name_patterns)
1313 return 1; /* No pattern always matches */
1314 if (filter->match_as_path)
1315 return match_name_as_path(filter, refname);
1316 return match_pattern(filter, refname);
1320 * Given a ref (sha1, refname), check if the ref belongs to the array
1321 * of sha1s. If the given ref is a tag, check if the given tag points
1322 * at one of the sha1s in the given sha1 array.
1323 * the given sha1_array.
1325 * 1. Only a single level of inderection is obtained, we might want to
1326 * change this to account for multiple levels (e.g. annotated tags
1327 * pointing to annotated tags pointing to a commit.)
1328 * 2. As the refs are cached we might know what refname peels to without
1329 * the need to parse the object via parse_object(). peel_ref() might be a
1330 * more efficient alternative to obtain the pointee.
1332 static const unsigned char *match_points_at(struct sha1_array *points_at,
1333 const unsigned char *sha1,
1334 const char *refname)
1336 const unsigned char *tagged_sha1 = NULL;
1339 if (sha1_array_lookup(points_at, sha1) >= 0)
1341 obj = parse_object(sha1);
1343 die(_("malformed object at '%s'"), refname);
1344 if (obj->type == OBJ_TAG)
1345 tagged_sha1 = ((struct tag *)obj)->tagged->oid.hash;
1346 if (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)
1351 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1352 static struct ref_array_item *new_ref_array_item(const char *refname,
1353 const unsigned char *objectname,
1356 struct ref_array_item *ref;
1357 FLEX_ALLOC_STR(ref, refname, refname);
1358 hashcpy(ref->objectname, objectname);
1364 static int ref_kind_from_refname(const char *refname)
1372 { "refs/heads/" , FILTER_REFS_BRANCHES },
1373 { "refs/remotes/" , FILTER_REFS_REMOTES },
1374 { "refs/tags/", FILTER_REFS_TAGS}
1377 if (!strcmp(refname, "HEAD"))
1378 return FILTER_REFS_DETACHED_HEAD;
1380 for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
1381 if (starts_with(refname, ref_kind[i].prefix))
1382 return ref_kind[i].kind;
1385 return FILTER_REFS_OTHERS;
1388 static int filter_ref_kind(struct ref_filter *filter, const char *refname)
1390 if (filter->kind == FILTER_REFS_BRANCHES ||
1391 filter->kind == FILTER_REFS_REMOTES ||
1392 filter->kind == FILTER_REFS_TAGS)
1393 return filter->kind;
1394 return ref_kind_from_refname(refname);
1398 * A call-back given to for_each_ref(). Filter refs and keep them for
1399 * later object processing.
1401 static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
1403 struct ref_filter_cbdata *ref_cbdata = cb_data;
1404 struct ref_filter *filter = ref_cbdata->filter;
1405 struct ref_array_item *ref;
1406 struct commit *commit = NULL;
1409 if (flag & REF_BAD_NAME) {
1410 warning(_("ignoring ref with broken name %s"), refname);
1414 if (flag & REF_ISBROKEN) {
1415 warning(_("ignoring broken ref %s"), refname);
1419 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1420 kind = filter_ref_kind(filter, refname);
1421 if (!(kind & filter->kind))
1424 if (!filter_pattern_match(filter, refname))
1427 if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
1431 * A merge filter is applied on refs pointing to commits. Hence
1432 * obtain the commit using the 'oid' available and discard all
1433 * non-commits early. The actual filtering is done later.
1435 if (filter->merge_commit || filter->with_commit || filter->verbose) {
1436 commit = lookup_commit_reference_gently(oid->hash, 1);
1439 /* We perform the filtering for the '--contains' option */
1440 if (filter->with_commit &&
1441 !commit_contains(filter, commit))
1446 * We do not open the object yet; sort may only need refname
1447 * to do its job and the resulting list may yet to be pruned
1448 * by maxcount logic.
1450 ref = new_ref_array_item(refname, oid->hash, flag);
1451 ref->commit = commit;
1453 REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);
1454 ref_cbdata->array->items[ref_cbdata->array->nr++] = ref;
1459 /* Free memory allocated for a ref_array_item */
1460 static void free_array_item(struct ref_array_item *item)
1462 free((char *)item->symref);
1466 /* Free all memory allocated for ref_array */
1467 void ref_array_clear(struct ref_array *array)
1471 for (i = 0; i < array->nr; i++)
1472 free_array_item(array->items[i]);
1474 array->items = NULL;
1475 array->nr = array->alloc = 0;
1478 static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata)
1480 struct rev_info revs;
1482 struct ref_filter *filter = ref_cbdata->filter;
1483 struct ref_array *array = ref_cbdata->array;
1484 struct commit **to_clear = xcalloc(sizeof(struct commit *), array->nr);
1486 init_revisions(&revs, NULL);
1488 for (i = 0; i < array->nr; i++) {
1489 struct ref_array_item *item = array->items[i];
1490 add_pending_object(&revs, &item->commit->object, item->refname);
1491 to_clear[i] = item->commit;
1494 filter->merge_commit->object.flags |= UNINTERESTING;
1495 add_pending_object(&revs, &filter->merge_commit->object, "");
1498 if (prepare_revision_walk(&revs))
1499 die(_("revision walk setup failed"));
1504 for (i = 0; i < old_nr; i++) {
1505 struct ref_array_item *item = array->items[i];
1506 struct commit *commit = item->commit;
1508 int is_merged = !!(commit->object.flags & UNINTERESTING);
1510 if (is_merged == (filter->merge == REF_FILTER_MERGED_INCLUDE))
1511 array->items[array->nr++] = array->items[i];
1513 free_array_item(item);
1516 for (i = 0; i < old_nr; i++)
1517 clear_commit_marks(to_clear[i], ALL_REV_FLAGS);
1518 clear_commit_marks(filter->merge_commit, ALL_REV_FLAGS);
1523 * API for filtering a set of refs. Based on the type of refs the user
1524 * has requested, we iterate through those refs and apply filters
1525 * as per the given ref_filter structure and finally store the
1526 * filtered refs in the ref_array structure.
1528 int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
1530 struct ref_filter_cbdata ref_cbdata;
1532 unsigned int broken = 0;
1534 ref_cbdata.array = array;
1535 ref_cbdata.filter = filter;
1537 if (type & FILTER_REFS_INCLUDE_BROKEN)
1539 filter->kind = type & FILTER_REFS_KIND_MASK;
1541 /* Simple per-ref filtering */
1543 die("filter_refs: invalid type");
1546 * For common cases where we need only branches or remotes or tags,
1547 * we only iterate through those refs. If a mix of refs is needed,
1548 * we iterate over all refs and filter out required refs with the help
1549 * of filter_ref_kind().
1551 if (filter->kind == FILTER_REFS_BRANCHES)
1552 ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken);
1553 else if (filter->kind == FILTER_REFS_REMOTES)
1554 ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken);
1555 else if (filter->kind == FILTER_REFS_TAGS)
1556 ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken);
1557 else if (filter->kind & FILTER_REFS_ALL)
1558 ret = for_each_fullref_in("", ref_filter_handler, &ref_cbdata, broken);
1559 if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
1560 head_ref(ref_filter_handler, &ref_cbdata);
1564 /* Filters that need revision walking */
1565 if (filter->merge_commit)
1566 do_merge_filter(&ref_cbdata);
1571 static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
1573 struct atom_value *va, *vb;
1575 cmp_type cmp_type = used_atom[s->atom].type;
1576 int (*cmp_fn)(const char *, const char *);
1578 get_ref_atom_value(a, s->atom, &va);
1579 get_ref_atom_value(b, s->atom, &vb);
1580 cmp_fn = s->ignore_case ? strcasecmp : strcmp;
1582 cmp = versioncmp(va->s, vb->s);
1583 else if (cmp_type == FIELD_STR)
1584 cmp = cmp_fn(va->s, vb->s);
1586 if (va->ul < vb->ul)
1588 else if (va->ul == vb->ul)
1589 cmp = cmp_fn(a->refname, b->refname);
1594 return (s->reverse) ? -cmp : cmp;
1597 static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
1599 struct ref_array_item *a = *((struct ref_array_item **)a_);
1600 struct ref_array_item *b = *((struct ref_array_item **)b_);
1601 struct ref_sorting *s;
1603 for (s = ref_sorting; s; s = s->next) {
1604 int cmp = cmp_ref_sorting(s, a, b);
1611 void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
1613 QSORT_S(array->items, array->nr, compare_refs, sorting);
1616 static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
1618 struct strbuf *s = &state->stack->output;
1620 while (*cp && (!ep || cp < ep)) {
1625 int ch = hex2chr(cp + 1);
1627 strbuf_addch(s, ch);
1633 strbuf_addch(s, *cp);
1638 void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
1640 const char *cp, *sp, *ep;
1641 struct strbuf *final_buf;
1642 struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
1644 state.quote_style = quote_style;
1645 push_stack_element(&state.stack);
1647 for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
1648 struct atom_value *atomv;
1650 ep = strchr(sp, ')');
1652 append_literal(cp, sp, &state);
1653 get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
1654 atomv->handler(atomv, &state);
1657 sp = cp + strlen(cp);
1658 append_literal(cp, sp, &state);
1660 if (need_color_reset_at_eol) {
1661 struct atom_value resetv;
1662 char color[COLOR_MAXLEN] = "";
1664 if (color_parse("reset", color) < 0)
1665 die("BUG: couldn't parse 'reset' as a color");
1667 append_atom(&resetv, &state);
1669 if (state.stack->prev)
1670 die(_("format: %%(end) atom missing"));
1671 final_buf = &state.stack->output;
1672 fwrite(final_buf->buf, 1, final_buf->len, stdout);
1673 pop_stack_element(&state.stack);
1677 void pretty_print_ref(const char *name, const unsigned char *sha1,
1680 struct ref_array_item *ref_item;
1681 ref_item = new_ref_array_item(name, sha1, 0);
1682 ref_item->kind = ref_kind_from_refname(name);
1683 show_ref_array_item(ref_item, format, 0);
1684 free_array_item(ref_item);
1687 /* If no sorting option is given, use refname to sort as default */
1688 struct ref_sorting *ref_default_sorting(void)
1690 static const char cstr_name[] = "refname";
1692 struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
1694 sorting->next = NULL;
1695 sorting->atom = parse_ref_filter_atom(cstr_name, cstr_name + strlen(cstr_name));
1699 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
1701 struct ref_sorting **sorting_tail = opt->value;
1702 struct ref_sorting *s;
1705 if (!arg) /* should --no-sort void the list ? */
1708 s = xcalloc(1, sizeof(*s));
1709 s->next = *sorting_tail;
1716 if (skip_prefix(arg, "version:", &arg) ||
1717 skip_prefix(arg, "v:", &arg))
1720 s->atom = parse_ref_filter_atom(arg, arg+len);
1724 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
1726 struct ref_filter *rf = opt->value;
1727 unsigned char sha1[20];
1729 rf->merge = starts_with(opt->long_name, "no")
1730 ? REF_FILTER_MERGED_OMIT
1731 : REF_FILTER_MERGED_INCLUDE;
1733 if (get_sha1(arg, sha1))
1734 die(_("malformed object name %s"), arg);
1736 rf->merge_commit = lookup_commit_reference_gently(sha1, 0);
1737 if (!rf->merge_commit)
1738 return opterror(opt, "must point to a commit", 0);