3 #include "parse-options.h"
11 #include "ref-filter.h"
14 #include "git-compat-util.h"
17 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
25 { "objectsize", FIELD_ULONG },
29 { "numparent", FIELD_ULONG },
36 { "authordate", FIELD_TIME },
40 { "committerdate", FIELD_TIME },
44 { "taggerdate", FIELD_TIME },
46 { "creatordate", FIELD_TIME },
60 #define REF_FORMATTING_STATE_INIT { 0, NULL }
72 struct ref_formatting_stack {
73 struct ref_formatting_stack *prev;
75 void (*at_end)(struct ref_formatting_stack *stack);
79 struct ref_formatting_state {
81 struct ref_formatting_stack *stack;
88 struct contents contents;
90 void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
91 unsigned long ul; /* used for sorting when not FIELD_STR */
95 * An atom is a valid field atom listed above, possibly prefixed with
96 * a "*" to denote deref_tag().
98 * We parse given format string and sort specifiers, and make a list
99 * of properties that we need to extract out of objects. ref_array_item
100 * structure will hold an array of values extracted that can be
101 * indexed with the "atom number", which is an index into this
104 static const char **used_atom;
105 static cmp_type *used_atom_type;
106 static int used_atom_cnt, need_tagged, need_symref;
107 static int need_color_reset_at_eol;
110 * Used to parse format string and sort specifiers
112 int parse_ref_filter_atom(const char *atom, const char *ep)
118 if (*sp == '*' && sp < ep)
121 die("malformed field name: %.*s", (int)(ep-atom), atom);
123 /* Do we have the atom already used elsewhere? */
124 for (i = 0; i < used_atom_cnt; i++) {
125 int len = strlen(used_atom[i]);
126 if (len == ep - atom && !memcmp(used_atom[i], atom, len))
130 /* Is the atom a valid one? */
131 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
132 int len = strlen(valid_atom[i].name);
134 * If the atom name has a colon, strip it and everything after
135 * it off - it specifies the format for this entry, and
136 * shouldn't be used for checking against the valid_atom
139 const char *formatp = strchr(sp, ':');
140 if (!formatp || ep < formatp)
142 if (len == formatp - sp && !memcmp(valid_atom[i].name, sp, len))
146 if (ARRAY_SIZE(valid_atom) <= i)
147 die("unknown field name: %.*s", (int)(ep-atom), atom);
149 /* Add it in, including the deref prefix */
152 REALLOC_ARRAY(used_atom, used_atom_cnt);
153 REALLOC_ARRAY(used_atom_type, used_atom_cnt);
154 used_atom[at] = xmemdupz(atom, ep - atom);
155 used_atom_type[at] = valid_atom[i].cmp_type;
158 if (!strcmp(used_atom[at], "symref"))
163 static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
165 switch (quote_style) {
167 strbuf_addstr(s, str);
170 sq_quote_buf(s, str);
173 perl_quote_buf(s, str);
176 python_quote_buf(s, str);
179 tcl_quote_buf(s, str);
184 static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
187 * Quote formatting is only done when the stack has a single
188 * element. Otherwise quote formatting is done on the
189 * element's entire output strbuf when the %(end) atom is
192 if (!state->stack->prev)
193 quote_formatting(&state->stack->output, v->s, state->quote_style);
195 strbuf_addstr(&state->stack->output, v->s);
198 static void push_stack_element(struct ref_formatting_stack **stack)
200 struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
202 strbuf_init(&s->output, 0);
207 static void pop_stack_element(struct ref_formatting_stack **stack)
209 struct ref_formatting_stack *current = *stack;
210 struct ref_formatting_stack *prev = current->prev;
213 strbuf_addbuf(&prev->output, ¤t->output);
214 strbuf_release(¤t->output);
219 static void end_align_handler(struct ref_formatting_stack *stack)
221 struct align *align = (struct align *)stack->at_end_data;
222 struct strbuf s = STRBUF_INIT;
224 strbuf_utf8_align(&s, align->position, align->width, stack->output.buf);
225 strbuf_swap(&stack->output, &s);
229 static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
231 struct ref_formatting_stack *new;
233 push_stack_element(&state->stack);
235 new->at_end = end_align_handler;
236 new->at_end_data = &atomv->u.align;
239 static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
241 struct ref_formatting_stack *current = state->stack;
242 struct strbuf s = STRBUF_INIT;
244 if (!current->at_end)
245 die(_("format: %%(end) atom used without corresponding atom"));
246 current->at_end(current);
249 * Perform quote formatting when the stack element is that of
250 * a supporting atom. If nested then perform quote formatting
251 * only on the topmost supporting atom.
253 if (!state->stack->prev->prev) {
254 quote_formatting(&s, current->output.buf, state->quote_style);
255 strbuf_swap(¤t->output, &s);
258 pop_stack_element(&state->stack);
261 static int match_atom_name(const char *name, const char *atom_name, const char **val)
265 if (!skip_prefix(name, atom_name, &body))
266 return 0; /* doesn't even begin with "atom_name" */
268 *val = NULL; /* %(atom_name) and no customization */
272 return 0; /* "atom_namefoo" is not "atom_name" or "atom_name:..." */
273 *val = body + 1; /* "atom_name:val" */
278 * In a format string, find the next occurrence of %(atom).
280 static const char *find_next(const char *cp)
285 * %( is the start of an atom;
286 * %% is a quoted per-cent.
290 else if (cp[1] == '%')
291 cp++; /* skip over two % */
292 /* otherwise this is a singleton, literal % */
300 * Make sure the format string is well formed, and parse out
303 int verify_ref_format(const char *format)
307 need_color_reset_at_eol = 0;
308 for (cp = format; *cp && (sp = find_next(cp)); ) {
309 const char *color, *ep = strchr(sp, ')');
313 return error("malformed format string %s", sp);
314 /* sp points at "%(" and ep points at the closing ")" */
315 at = parse_ref_filter_atom(sp + 2, ep);
318 if (skip_prefix(used_atom[at], "color:", &color))
319 need_color_reset_at_eol = !!strcmp(color, "reset");
325 * Given an object name, read the object data and size, and return a
326 * "struct object". If the object data we are returning is also borrowed
327 * by the "struct object" representation, set *eaten as well---it is a
328 * signal from parse_object_buffer to us not to free the buffer.
330 static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
332 enum object_type type;
333 void *buf = read_sha1_file(sha1, &type, sz);
336 *obj = parse_object_buffer(sha1, type, *sz, buf, eaten);
342 static int grab_objectname(const char *name, const unsigned char *sha1,
343 struct atom_value *v)
345 if (!strcmp(name, "objectname")) {
346 v->s = xstrdup(sha1_to_hex(sha1));
349 if (!strcmp(name, "objectname:short")) {
350 v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
356 /* See grab_values */
357 static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
361 for (i = 0; i < used_atom_cnt; i++) {
362 const char *name = used_atom[i];
363 struct atom_value *v = &val[i];
364 if (!!deref != (*name == '*'))
368 if (!strcmp(name, "objecttype"))
369 v->s = typename(obj->type);
370 else if (!strcmp(name, "objectsize")) {
372 v->s = xstrfmt("%lu", sz);
375 grab_objectname(name, get_object_hash(*obj), v);
379 /* See grab_values */
380 static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
383 struct tag *tag = (struct tag *) obj;
385 for (i = 0; i < used_atom_cnt; i++) {
386 const char *name = used_atom[i];
387 struct atom_value *v = &val[i];
388 if (!!deref != (*name == '*'))
392 if (!strcmp(name, "tag"))
394 else if (!strcmp(name, "type") && tag->tagged)
395 v->s = typename(tag->tagged->type);
396 else if (!strcmp(name, "object") && tag->tagged)
397 v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
401 /* See grab_values */
402 static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
405 struct commit *commit = (struct commit *) obj;
407 for (i = 0; i < used_atom_cnt; i++) {
408 const char *name = used_atom[i];
409 struct atom_value *v = &val[i];
410 if (!!deref != (*name == '*'))
414 if (!strcmp(name, "tree")) {
415 v->s = xstrdup(oid_to_hex(&commit->tree->object.oid));
417 else if (!strcmp(name, "numparent")) {
418 v->ul = commit_list_count(commit->parents);
419 v->s = xstrfmt("%lu", v->ul);
421 else if (!strcmp(name, "parent")) {
422 struct commit_list *parents;
423 struct strbuf s = STRBUF_INIT;
424 for (parents = commit->parents; parents; parents = parents->next) {
425 struct commit *parent = parents->item;
426 if (parents != commit->parents)
427 strbuf_addch(&s, ' ');
428 strbuf_addstr(&s, oid_to_hex(&parent->object.oid));
430 v->s = strbuf_detach(&s, NULL);
435 static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz)
439 if (!strncmp(buf, who, wholen) &&
441 return buf + wholen + 1;
442 eol = strchr(buf, '\n');
447 return ""; /* end of header */
453 static const char *copy_line(const char *buf)
455 const char *eol = strchrnul(buf, '\n');
456 return xmemdupz(buf, eol - buf);
459 static const char *copy_name(const char *buf)
462 for (cp = buf; *cp && *cp != '\n'; cp++) {
463 if (!strncmp(cp, " <", 2))
464 return xmemdupz(buf, cp - buf);
469 static const char *copy_email(const char *buf)
471 const char *email = strchr(buf, '<');
475 eoemail = strchr(email, '>');
478 return xmemdupz(email, eoemail + 1 - email);
481 static char *copy_subject(const char *buf, unsigned long len)
483 char *r = xmemdupz(buf, len);
486 for (i = 0; i < len; i++)
493 static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
495 const char *eoemail = strstr(buf, "> ");
497 unsigned long timestamp;
499 struct date_mode date_mode = { DATE_NORMAL };
503 * We got here because atomname ends in "date" or "date<something>";
504 * it's not possible that <something> is not ":<format>" because
505 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
506 * ":" means no format is specified, and use the default.
508 formatp = strchr(atomname, ':');
509 if (formatp != NULL) {
511 parse_date_format(formatp, &date_mode);
516 timestamp = strtoul(eoemail + 2, &zone, 10);
517 if (timestamp == ULONG_MAX)
519 tz = strtol(zone, NULL, 10);
520 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
522 v->s = xstrdup(show_date(timestamp, tz, &date_mode));
530 /* See grab_values */
531 static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
534 int wholen = strlen(who);
535 const char *wholine = NULL;
537 for (i = 0; i < used_atom_cnt; i++) {
538 const char *name = used_atom[i];
539 struct atom_value *v = &val[i];
540 if (!!deref != (*name == '*'))
544 if (strncmp(who, name, wholen))
546 if (name[wholen] != 0 &&
547 strcmp(name + wholen, "name") &&
548 strcmp(name + wholen, "email") &&
549 !starts_with(name + wholen, "date"))
552 wholine = find_wholine(who, wholen, buf, sz);
554 return; /* no point looking for it */
555 if (name[wholen] == 0)
556 v->s = copy_line(wholine);
557 else if (!strcmp(name + wholen, "name"))
558 v->s = copy_name(wholine);
559 else if (!strcmp(name + wholen, "email"))
560 v->s = copy_email(wholine);
561 else if (starts_with(name + wholen, "date"))
562 grab_date(wholine, v, name);
566 * For a tag or a commit object, if "creator" or "creatordate" is
567 * requested, do something special.
569 if (strcmp(who, "tagger") && strcmp(who, "committer"))
570 return; /* "author" for commit object is not wanted */
572 wholine = find_wholine(who, wholen, buf, sz);
575 for (i = 0; i < used_atom_cnt; i++) {
576 const char *name = used_atom[i];
577 struct atom_value *v = &val[i];
578 if (!!deref != (*name == '*'))
583 if (starts_with(name, "creatordate"))
584 grab_date(wholine, v, name);
585 else if (!strcmp(name, "creator"))
586 v->s = copy_line(wholine);
590 static void find_subpos(const char *buf, unsigned long sz,
591 const char **sub, unsigned long *sublen,
592 const char **body, unsigned long *bodylen,
593 unsigned long *nonsiglen,
594 const char **sig, unsigned long *siglen)
597 /* skip past header until we hit empty line */
598 while (*buf && *buf != '\n') {
599 eol = strchrnul(buf, '\n');
604 /* skip any empty lines */
608 /* parse signature first; we might not even have a subject line */
609 *sig = buf + parse_signature(buf, strlen(buf));
610 *siglen = strlen(*sig);
612 /* subject is first non-empty line */
614 /* subject goes to first empty line */
615 while (buf < *sig && *buf && *buf != '\n') {
616 eol = strchrnul(buf, '\n');
621 *sublen = buf - *sub;
622 /* drop trailing newline, if present */
623 if (*sublen && (*sub)[*sublen - 1] == '\n')
626 /* skip any empty lines */
630 *bodylen = strlen(buf);
631 *nonsiglen = *sig - buf;
635 * If 'lines' is greater than 0, append that many lines from the given
636 * 'buf' of length 'size' to the given strbuf.
638 static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
641 const char *sp, *eol;
646 for (i = 0; i < lines && sp < buf + size; i++) {
648 strbuf_addstr(out, "\n ");
649 eol = memchr(sp, '\n', size - (sp - buf));
650 len = eol ? eol - sp : size - (sp - buf);
651 strbuf_add(out, sp, len);
658 /* See grab_values */
659 static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
662 const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
663 unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
665 for (i = 0; i < used_atom_cnt; i++) {
666 const char *name = used_atom[i];
667 struct atom_value *v = &val[i];
668 const char *valp = NULL;
669 if (!!deref != (*name == '*'))
673 if (strcmp(name, "subject") &&
674 strcmp(name, "body") &&
675 strcmp(name, "contents") &&
676 strcmp(name, "contents:subject") &&
677 strcmp(name, "contents:body") &&
678 strcmp(name, "contents:signature") &&
679 !starts_with(name, "contents:lines="))
684 &bodypos, &bodylen, &nonsiglen,
687 if (!strcmp(name, "subject"))
688 v->s = copy_subject(subpos, sublen);
689 else if (!strcmp(name, "contents:subject"))
690 v->s = copy_subject(subpos, sublen);
691 else if (!strcmp(name, "body"))
692 v->s = xmemdupz(bodypos, bodylen);
693 else if (!strcmp(name, "contents:body"))
694 v->s = xmemdupz(bodypos, nonsiglen);
695 else if (!strcmp(name, "contents:signature"))
696 v->s = xmemdupz(sigpos, siglen);
697 else if (!strcmp(name, "contents"))
698 v->s = xstrdup(subpos);
699 else if (skip_prefix(name, "contents:lines=", &valp)) {
700 struct strbuf s = STRBUF_INIT;
701 const char *contents_end = bodylen + bodypos - siglen;
703 if (strtoul_ui(valp, 10, &v->u.contents.lines))
704 die(_("positive value expected contents:lines=%s"), valp);
705 /* Size is the length of the message after removing the signature */
706 append_lines(&s, subpos, contents_end - subpos, v->u.contents.lines);
707 v->s = strbuf_detach(&s, NULL);
713 * We want to have empty print-string for field requests
714 * that do not apply (e.g. "authordate" for a tag object)
716 static void fill_missing_values(struct atom_value *val)
719 for (i = 0; i < used_atom_cnt; i++) {
720 struct atom_value *v = &val[i];
727 * val is a list of atom_value to hold returned values. Extract
728 * the values for atoms in used_atom array out of (obj, buf, sz).
729 * when deref is false, (obj, buf, sz) is the object that is
730 * pointed at by the ref itself; otherwise it is the object the
731 * ref (which is a tag) refers to.
733 static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
735 grab_common_values(val, deref, obj, buf, sz);
738 grab_tag_values(val, deref, obj, buf, sz);
739 grab_sub_body_contents(val, deref, obj, buf, sz);
740 grab_person("tagger", val, deref, obj, buf, sz);
743 grab_commit_values(val, deref, obj, buf, sz);
744 grab_sub_body_contents(val, deref, obj, buf, sz);
745 grab_person("author", val, deref, obj, buf, sz);
746 grab_person("committer", val, deref, obj, buf, sz);
749 /* grab_tree_values(val, deref, obj, buf, sz); */
752 /* grab_blob_values(val, deref, obj, buf, sz); */
755 die("Eh? Object of type %d?", obj->type);
759 static inline char *copy_advance(char *dst, const char *src)
767 * Parse the object referred by ref, and grab needed value.
769 static void populate_value(struct ref_array_item *ref)
775 const unsigned char *tagged;
777 ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
779 if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
780 unsigned char unused1[20];
781 ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
787 /* Fill in specials first */
788 for (i = 0; i < used_atom_cnt; i++) {
789 const char *name = used_atom[i];
790 struct atom_value *v = &ref->value[i];
795 struct branch *branch = NULL;
797 v->handler = append_atom;
804 if (starts_with(name, "refname"))
805 refname = ref->refname;
806 else if (starts_with(name, "symref"))
807 refname = ref->symref ? ref->symref : "";
808 else if (starts_with(name, "upstream")) {
809 const char *branch_name;
810 /* only local branches may have an upstream */
811 if (!skip_prefix(ref->refname, "refs/heads/",
814 branch = branch_get(branch_name);
816 refname = branch_get_upstream(branch, NULL);
819 } else if (starts_with(name, "push")) {
820 const char *branch_name;
821 if (!skip_prefix(ref->refname, "refs/heads/",
824 branch = branch_get(branch_name);
826 refname = branch_get_push(branch, NULL);
829 } else if (match_atom_name(name, "color", &valp)) {
830 char color[COLOR_MAXLEN] = "";
833 die(_("expected format: %%(color:<color>)"));
834 if (color_parse(valp, color) < 0)
835 die(_("unable to parse format"));
836 v->s = xstrdup(color);
838 } else if (!strcmp(name, "flag")) {
839 char buf[256], *cp = buf;
840 if (ref->flag & REF_ISSYMREF)
841 cp = copy_advance(cp, ",symref");
842 if (ref->flag & REF_ISPACKED)
843 cp = copy_advance(cp, ",packed");
848 v->s = xstrdup(buf + 1);
851 } else if (!deref && grab_objectname(name, ref->objectname, v)) {
853 } else if (!strcmp(name, "HEAD")) {
855 unsigned char sha1[20];
857 head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
859 if (!strcmp(ref->refname, head))
864 } else if (match_atom_name(name, "align", &valp)) {
865 struct align *align = &v->u.align;
866 struct strbuf **s, **to_free;
870 die(_("expected format: %%(align:<width>,<position>)"));
873 * TODO: Implement a function similar to strbuf_split_str()
874 * which would omit the separator from the end of each value.
876 s = to_free = strbuf_split_str(valp, ',', 0);
878 align->position = ALIGN_LEFT;
881 /* Strip trailing comma */
883 strbuf_setlen(s[0], s[0]->len - 1);
884 if (!strtoul_ui(s[0]->buf, 10, (unsigned int *)&width))
886 else if (!strcmp(s[0]->buf, "left"))
887 align->position = ALIGN_LEFT;
888 else if (!strcmp(s[0]->buf, "right"))
889 align->position = ALIGN_RIGHT;
890 else if (!strcmp(s[0]->buf, "middle"))
891 align->position = ALIGN_MIDDLE;
893 die(_("improper format entered align:%s"), s[0]->buf);
898 die(_("positive width expected with the %%(align) atom"));
899 align->width = width;
900 strbuf_list_free(to_free);
901 v->handler = align_atom_handler;
903 } else if (!strcmp(name, "end")) {
904 v->handler = end_atom_handler;
909 formatp = strchr(name, ':');
911 int num_ours, num_theirs;
914 if (!strcmp(formatp, "short"))
915 refname = shorten_unambiguous_ref(refname,
916 warn_ambiguous_refs);
917 else if (!strcmp(formatp, "track") &&
918 (starts_with(name, "upstream") ||
919 starts_with(name, "push"))) {
921 if (stat_tracking_info(branch, &num_ours,
925 if (!num_ours && !num_theirs)
928 v->s = xstrfmt("[behind %d]", num_theirs);
929 else if (!num_theirs)
930 v->s = xstrfmt("[ahead %d]", num_ours);
932 v->s = xstrfmt("[ahead %d, behind %d]",
933 num_ours, num_theirs);
935 } else if (!strcmp(formatp, "trackshort") &&
936 (starts_with(name, "upstream") ||
937 starts_with(name, "push"))) {
940 if (stat_tracking_info(branch, &num_ours,
944 if (!num_ours && !num_theirs)
948 else if (!num_theirs)
954 die("unknown %.*s format %s",
955 (int)(formatp - name), name, formatp);
961 v->s = xstrfmt("%s^{}", refname);
964 for (i = 0; i < used_atom_cnt; i++) {
965 struct atom_value *v = &ref->value[i];
972 buf = get_obj(ref->objectname, &obj, &size, &eaten);
974 die("missing object %s for %s",
975 sha1_to_hex(ref->objectname), ref->refname);
977 die("parse_object_buffer failed on %s for %s",
978 sha1_to_hex(ref->objectname), ref->refname);
980 grab_values(ref->value, 0, obj, buf, size);
985 * If there is no atom that wants to know about tagged
986 * object, we are done.
988 if (!need_tagged || (obj->type != OBJ_TAG))
992 * If it is a tag object, see if we use a value that derefs
993 * the object, and if we do grab the object it refers to.
995 tagged = get_object_hash(*((struct tag *)obj)->tagged);
998 * NEEDSWORK: This derefs tag only once, which
999 * is good to deal with chains of trust, but
1000 * is not consistent with what deref_tag() does
1001 * which peels the onion to the core.
1003 buf = get_obj(tagged, &obj, &size, &eaten);
1005 die("missing object %s for %s",
1006 sha1_to_hex(tagged), ref->refname);
1008 die("parse_object_buffer failed on %s for %s",
1009 sha1_to_hex(tagged), ref->refname);
1010 grab_values(ref->value, 1, obj, buf, size);
1016 * Given a ref, return the value for the atom. This lazily gets value
1017 * out of the object by calling populate value.
1019 static void get_ref_atom_value(struct ref_array_item *ref, int atom, struct atom_value **v)
1022 populate_value(ref);
1023 fill_missing_values(ref->value);
1025 *v = &ref->value[atom];
1028 enum contains_result {
1029 CONTAINS_UNKNOWN = -1,
1035 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1038 * At each recursion step, the stack items points to the commits whose
1039 * ancestors are to be inspected.
1041 struct contains_stack {
1043 struct contains_stack_entry {
1044 struct commit *commit;
1045 struct commit_list *parents;
1049 static int in_commit_list(const struct commit_list *want, struct commit *c)
1051 for (; want; want = want->next)
1052 if (!oidcmp(&want->item->object.oid, &c->object.oid))
1058 * Test whether the candidate or one of its parents is contained in the list.
1059 * Do not recurse to find out, though, but return -1 if inconclusive.
1061 static enum contains_result contains_test(struct commit *candidate,
1062 const struct commit_list *want)
1064 /* was it previously marked as containing a want commit? */
1065 if (candidate->object.flags & TMP_MARK)
1067 /* or marked as not possibly containing a want commit? */
1068 if (candidate->object.flags & UNINTERESTING)
1071 if (in_commit_list(want, candidate)) {
1072 candidate->object.flags |= TMP_MARK;
1076 if (parse_commit(candidate) < 0)
1082 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
1084 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
1085 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
1086 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
1089 static enum contains_result contains_tag_algo(struct commit *candidate,
1090 const struct commit_list *want)
1092 struct contains_stack contains_stack = { 0, 0, NULL };
1093 int result = contains_test(candidate, want);
1095 if (result != CONTAINS_UNKNOWN)
1098 push_to_contains_stack(candidate, &contains_stack);
1099 while (contains_stack.nr) {
1100 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
1101 struct commit *commit = entry->commit;
1102 struct commit_list *parents = entry->parents;
1105 commit->object.flags |= UNINTERESTING;
1106 contains_stack.nr--;
1109 * If we just popped the stack, parents->item has been marked,
1110 * therefore contains_test will return a meaningful 0 or 1.
1112 else switch (contains_test(parents->item, want)) {
1114 commit->object.flags |= TMP_MARK;
1115 contains_stack.nr--;
1118 entry->parents = parents->next;
1120 case CONTAINS_UNKNOWN:
1121 push_to_contains_stack(parents->item, &contains_stack);
1125 free(contains_stack.contains_stack);
1126 return contains_test(candidate, want);
1129 static int commit_contains(struct ref_filter *filter, struct commit *commit)
1131 if (filter->with_commit_tag_algo)
1132 return contains_tag_algo(commit, filter->with_commit);
1133 return is_descendant_of(commit, filter->with_commit);
1137 * Return 1 if the refname matches one of the patterns, otherwise 0.
1138 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1139 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1140 * matches "refs/heads/mas*", too).
1142 static int match_pattern(const char **patterns, const char *refname)
1145 * When no '--format' option is given we need to skip the prefix
1146 * for matching refs of tags and branches.
1148 (void)(skip_prefix(refname, "refs/tags/", &refname) ||
1149 skip_prefix(refname, "refs/heads/", &refname) ||
1150 skip_prefix(refname, "refs/remotes/", &refname) ||
1151 skip_prefix(refname, "refs/", &refname));
1153 for (; *patterns; patterns++) {
1154 if (!wildmatch(*patterns, refname, 0, NULL))
1161 * Return 1 if the refname matches one of the patterns, otherwise 0.
1162 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1163 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1164 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1166 static int match_name_as_path(const char **pattern, const char *refname)
1168 int namelen = strlen(refname);
1169 for (; *pattern; pattern++) {
1170 const char *p = *pattern;
1171 int plen = strlen(p);
1173 if ((plen <= namelen) &&
1174 !strncmp(refname, p, plen) &&
1175 (refname[plen] == '\0' ||
1176 refname[plen] == '/' ||
1179 if (!wildmatch(p, refname, WM_PATHNAME, NULL))
1185 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1186 static int filter_pattern_match(struct ref_filter *filter, const char *refname)
1188 if (!*filter->name_patterns)
1189 return 1; /* No pattern always matches */
1190 if (filter->match_as_path)
1191 return match_name_as_path(filter->name_patterns, refname);
1192 return match_pattern(filter->name_patterns, refname);
1196 * Given a ref (sha1, refname), check if the ref belongs to the array
1197 * of sha1s. If the given ref is a tag, check if the given tag points
1198 * at one of the sha1s in the given sha1 array.
1199 * the given sha1_array.
1201 * 1. Only a single level of inderection is obtained, we might want to
1202 * change this to account for multiple levels (e.g. annotated tags
1203 * pointing to annotated tags pointing to a commit.)
1204 * 2. As the refs are cached we might know what refname peels to without
1205 * the need to parse the object via parse_object(). peel_ref() might be a
1206 * more efficient alternative to obtain the pointee.
1208 static const unsigned char *match_points_at(struct sha1_array *points_at,
1209 const unsigned char *sha1,
1210 const char *refname)
1212 const unsigned char *tagged_sha1 = NULL;
1215 if (sha1_array_lookup(points_at, sha1) >= 0)
1217 obj = parse_object(sha1);
1219 die(_("malformed object at '%s'"), refname);
1220 if (obj->type == OBJ_TAG)
1221 tagged_sha1 = get_object_hash(*((struct tag *)obj)->tagged);
1222 if (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)
1227 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1228 static struct ref_array_item *new_ref_array_item(const char *refname,
1229 const unsigned char *objectname,
1232 size_t len = strlen(refname);
1233 struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item) + len + 1);
1234 memcpy(ref->refname, refname, len);
1235 ref->refname[len] = '\0';
1236 hashcpy(ref->objectname, objectname);
1242 static int filter_ref_kind(struct ref_filter *filter, const char *refname)
1250 { "refs/heads/" , FILTER_REFS_BRANCHES },
1251 { "refs/remotes/" , FILTER_REFS_REMOTES },
1252 { "refs/tags/", FILTER_REFS_TAGS}
1255 if (filter->kind == FILTER_REFS_BRANCHES ||
1256 filter->kind == FILTER_REFS_REMOTES ||
1257 filter->kind == FILTER_REFS_TAGS)
1258 return filter->kind;
1259 else if (!strcmp(refname, "HEAD"))
1260 return FILTER_REFS_DETACHED_HEAD;
1262 for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
1263 if (starts_with(refname, ref_kind[i].prefix))
1264 return ref_kind[i].kind;
1267 return FILTER_REFS_OTHERS;
1271 * A call-back given to for_each_ref(). Filter refs and keep them for
1272 * later object processing.
1274 static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
1276 struct ref_filter_cbdata *ref_cbdata = cb_data;
1277 struct ref_filter *filter = ref_cbdata->filter;
1278 struct ref_array_item *ref;
1279 struct commit *commit = NULL;
1282 if (flag & REF_BAD_NAME) {
1283 warning("ignoring ref with broken name %s", refname);
1287 if (flag & REF_ISBROKEN) {
1288 warning("ignoring broken ref %s", refname);
1292 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1293 kind = filter_ref_kind(filter, refname);
1294 if (!(kind & filter->kind))
1297 if (!filter_pattern_match(filter, refname))
1300 if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
1304 * A merge filter is applied on refs pointing to commits. Hence
1305 * obtain the commit using the 'oid' available and discard all
1306 * non-commits early. The actual filtering is done later.
1308 if (filter->merge_commit || filter->with_commit || filter->verbose) {
1309 commit = lookup_commit_reference_gently(oid->hash, 1);
1312 /* We perform the filtering for the '--contains' option */
1313 if (filter->with_commit &&
1314 !commit_contains(filter, commit))
1319 * We do not open the object yet; sort may only need refname
1320 * to do its job and the resulting list may yet to be pruned
1321 * by maxcount logic.
1323 ref = new_ref_array_item(refname, oid->hash, flag);
1324 ref->commit = commit;
1326 REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);
1327 ref_cbdata->array->items[ref_cbdata->array->nr++] = ref;
1332 /* Free memory allocated for a ref_array_item */
1333 static void free_array_item(struct ref_array_item *item)
1335 free((char *)item->symref);
1339 /* Free all memory allocated for ref_array */
1340 void ref_array_clear(struct ref_array *array)
1344 for (i = 0; i < array->nr; i++)
1345 free_array_item(array->items[i]);
1347 array->items = NULL;
1348 array->nr = array->alloc = 0;
1351 static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata)
1353 struct rev_info revs;
1355 struct ref_filter *filter = ref_cbdata->filter;
1356 struct ref_array *array = ref_cbdata->array;
1357 struct commit **to_clear = xcalloc(sizeof(struct commit *), array->nr);
1359 init_revisions(&revs, NULL);
1361 for (i = 0; i < array->nr; i++) {
1362 struct ref_array_item *item = array->items[i];
1363 add_pending_object(&revs, &item->commit->object, item->refname);
1364 to_clear[i] = item->commit;
1367 filter->merge_commit->object.flags |= UNINTERESTING;
1368 add_pending_object(&revs, &filter->merge_commit->object, "");
1371 if (prepare_revision_walk(&revs))
1372 die(_("revision walk setup failed"));
1377 for (i = 0; i < old_nr; i++) {
1378 struct ref_array_item *item = array->items[i];
1379 struct commit *commit = item->commit;
1381 int is_merged = !!(commit->object.flags & UNINTERESTING);
1383 if (is_merged == (filter->merge == REF_FILTER_MERGED_INCLUDE))
1384 array->items[array->nr++] = array->items[i];
1386 free_array_item(item);
1389 for (i = 0; i < old_nr; i++)
1390 clear_commit_marks(to_clear[i], ALL_REV_FLAGS);
1391 clear_commit_marks(filter->merge_commit, ALL_REV_FLAGS);
1396 * API for filtering a set of refs. Based on the type of refs the user
1397 * has requested, we iterate through those refs and apply filters
1398 * as per the given ref_filter structure and finally store the
1399 * filtered refs in the ref_array structure.
1401 int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
1403 struct ref_filter_cbdata ref_cbdata;
1405 unsigned int broken = 0;
1407 ref_cbdata.array = array;
1408 ref_cbdata.filter = filter;
1410 if (type & FILTER_REFS_INCLUDE_BROKEN)
1412 filter->kind = type & FILTER_REFS_KIND_MASK;
1414 /* Simple per-ref filtering */
1416 die("filter_refs: invalid type");
1419 * For common cases where we need only branches or remotes or tags,
1420 * we only iterate through those refs. If a mix of refs is needed,
1421 * we iterate over all refs and filter out required refs with the help
1422 * of filter_ref_kind().
1424 if (filter->kind == FILTER_REFS_BRANCHES)
1425 ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken);
1426 else if (filter->kind == FILTER_REFS_REMOTES)
1427 ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken);
1428 else if (filter->kind == FILTER_REFS_TAGS)
1429 ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken);
1430 else if (filter->kind & FILTER_REFS_ALL)
1431 ret = for_each_fullref_in("", ref_filter_handler, &ref_cbdata, broken);
1432 if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
1433 head_ref(ref_filter_handler, &ref_cbdata);
1437 /* Filters that need revision walking */
1438 if (filter->merge_commit)
1439 do_merge_filter(&ref_cbdata);
1444 static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
1446 struct atom_value *va, *vb;
1448 cmp_type cmp_type = used_atom_type[s->atom];
1450 get_ref_atom_value(a, s->atom, &va);
1451 get_ref_atom_value(b, s->atom, &vb);
1453 cmp = versioncmp(va->s, vb->s);
1454 else if (cmp_type == FIELD_STR)
1455 cmp = strcmp(va->s, vb->s);
1457 if (va->ul < vb->ul)
1459 else if (va->ul == vb->ul)
1460 cmp = strcmp(a->refname, b->refname);
1465 return (s->reverse) ? -cmp : cmp;
1468 static struct ref_sorting *ref_sorting;
1469 static int compare_refs(const void *a_, const void *b_)
1471 struct ref_array_item *a = *((struct ref_array_item **)a_);
1472 struct ref_array_item *b = *((struct ref_array_item **)b_);
1473 struct ref_sorting *s;
1475 for (s = ref_sorting; s; s = s->next) {
1476 int cmp = cmp_ref_sorting(s, a, b);
1483 void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
1485 ref_sorting = sorting;
1486 qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
1489 static int hex1(char ch)
1491 if ('0' <= ch && ch <= '9')
1493 else if ('a' <= ch && ch <= 'f')
1494 return ch - 'a' + 10;
1495 else if ('A' <= ch && ch <= 'F')
1496 return ch - 'A' + 10;
1499 static int hex2(const char *cp)
1502 return (hex1(cp[0]) << 4) | hex1(cp[1]);
1507 static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
1509 struct strbuf *s = &state->stack->output;
1511 while (*cp && (!ep || cp < ep)) {
1516 int ch = hex2(cp + 1);
1518 strbuf_addch(s, ch);
1524 strbuf_addch(s, *cp);
1529 void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
1531 const char *cp, *sp, *ep;
1532 struct strbuf *final_buf;
1533 struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
1535 state.quote_style = quote_style;
1536 push_stack_element(&state.stack);
1538 for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
1539 struct atom_value *atomv;
1541 ep = strchr(sp, ')');
1543 append_literal(cp, sp, &state);
1544 get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
1545 atomv->handler(atomv, &state);
1548 sp = cp + strlen(cp);
1549 append_literal(cp, sp, &state);
1551 if (need_color_reset_at_eol) {
1552 struct atom_value resetv;
1553 char color[COLOR_MAXLEN] = "";
1555 if (color_parse("reset", color) < 0)
1556 die("BUG: couldn't parse 'reset' as a color");
1558 append_atom(&resetv, &state);
1560 if (state.stack->prev)
1561 die(_("format: %%(end) atom missing"));
1562 final_buf = &state.stack->output;
1563 fwrite(final_buf->buf, 1, final_buf->len, stdout);
1564 pop_stack_element(&state.stack);
1568 /* If no sorting option is given, use refname to sort as default */
1569 struct ref_sorting *ref_default_sorting(void)
1571 static const char cstr_name[] = "refname";
1573 struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
1575 sorting->next = NULL;
1576 sorting->atom = parse_ref_filter_atom(cstr_name, cstr_name + strlen(cstr_name));
1580 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
1582 struct ref_sorting **sorting_tail = opt->value;
1583 struct ref_sorting *s;
1586 if (!arg) /* should --no-sort void the list ? */
1589 s = xcalloc(1, sizeof(*s));
1590 s->next = *sorting_tail;
1597 if (skip_prefix(arg, "version:", &arg) ||
1598 skip_prefix(arg, "v:", &arg))
1601 s->atom = parse_ref_filter_atom(arg, arg+len);
1605 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
1607 struct ref_filter *rf = opt->value;
1608 unsigned char sha1[20];
1610 rf->merge = starts_with(opt->long_name, "no")
1611 ? REF_FILTER_MERGED_OMIT
1612 : REF_FILTER_MERGED_INCLUDE;
1614 if (get_sha1(arg, sha1))
1615 die(_("malformed object name %s"), arg);
1617 rf->merge_commit = lookup_commit_reference_gently(sha1, 0);
1618 if (!rf->merge_commit)
1619 return opterror(opt, "must point to a commit", 0);