3 #include "parse-options.h"
11 #include "ref-filter.h"
14 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
22 { "objectsize", FIELD_ULONG },
26 { "numparent", FIELD_ULONG },
33 { "authordate", FIELD_TIME },
37 { "committerdate", FIELD_TIME },
41 { "taggerdate", FIELD_TIME },
43 { "creatordate", FIELD_TIME },
47 { "contents:subject" },
49 { "contents:signature" },
60 unsigned long ul; /* used for sorting when not FIELD_STR */
64 * An atom is a valid field atom listed above, possibly prefixed with
65 * a "*" to denote deref_tag().
67 * We parse given format string and sort specifiers, and make a list
68 * of properties that we need to extract out of objects. ref_array_item
69 * structure will hold an array of values extracted that can be
70 * indexed with the "atom number", which is an index into this
73 static const char **used_atom;
74 static cmp_type *used_atom_type;
75 static int used_atom_cnt, need_tagged, need_symref;
76 static int need_color_reset_at_eol;
79 * Used to parse format string and sort specifiers
81 int parse_ref_filter_atom(const char *atom, const char *ep)
87 if (*sp == '*' && sp < ep)
90 die("malformed field name: %.*s", (int)(ep-atom), atom);
92 /* Do we have the atom already used elsewhere? */
93 for (i = 0; i < used_atom_cnt; i++) {
94 int len = strlen(used_atom[i]);
95 if (len == ep - atom && !memcmp(used_atom[i], atom, len))
99 /* Is the atom a valid one? */
100 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
101 int len = strlen(valid_atom[i].name);
103 * If the atom name has a colon, strip it and everything after
104 * it off - it specifies the format for this entry, and
105 * shouldn't be used for checking against the valid_atom
108 const char *formatp = strchr(sp, ':');
109 if (!formatp || ep < formatp)
111 if (len == formatp - sp && !memcmp(valid_atom[i].name, sp, len))
115 if (ARRAY_SIZE(valid_atom) <= i)
116 die("unknown field name: %.*s", (int)(ep-atom), atom);
118 /* Add it in, including the deref prefix */
121 REALLOC_ARRAY(used_atom, used_atom_cnt);
122 REALLOC_ARRAY(used_atom_type, used_atom_cnt);
123 used_atom[at] = xmemdupz(atom, ep - atom);
124 used_atom_type[at] = valid_atom[i].cmp_type;
127 if (!strcmp(used_atom[at], "symref"))
133 * In a format string, find the next occurrence of %(atom).
135 static const char *find_next(const char *cp)
140 * %( is the start of an atom;
141 * %% is a quoted per-cent.
145 else if (cp[1] == '%')
146 cp++; /* skip over two % */
147 /* otherwise this is a singleton, literal % */
155 * Make sure the format string is well formed, and parse out
158 int verify_ref_format(const char *format)
162 need_color_reset_at_eol = 0;
163 for (cp = format; *cp && (sp = find_next(cp)); ) {
164 const char *color, *ep = strchr(sp, ')');
168 return error("malformed format string %s", sp);
169 /* sp points at "%(" and ep points at the closing ")" */
170 at = parse_ref_filter_atom(sp + 2, ep);
173 if (skip_prefix(used_atom[at], "color:", &color))
174 need_color_reset_at_eol = !!strcmp(color, "reset");
180 * Given an object name, read the object data and size, and return a
181 * "struct object". If the object data we are returning is also borrowed
182 * by the "struct object" representation, set *eaten as well---it is a
183 * signal from parse_object_buffer to us not to free the buffer.
185 static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
187 enum object_type type;
188 void *buf = read_sha1_file(sha1, &type, sz);
191 *obj = parse_object_buffer(sha1, type, *sz, buf, eaten);
197 static int grab_objectname(const char *name, const unsigned char *sha1,
198 struct atom_value *v)
200 if (!strcmp(name, "objectname")) {
201 char *s = xmalloc(41);
202 strcpy(s, sha1_to_hex(sha1));
206 if (!strcmp(name, "objectname:short")) {
207 v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
213 /* See grab_values */
214 static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
218 for (i = 0; i < used_atom_cnt; i++) {
219 const char *name = used_atom[i];
220 struct atom_value *v = &val[i];
221 if (!!deref != (*name == '*'))
225 if (!strcmp(name, "objecttype"))
226 v->s = typename(obj->type);
227 else if (!strcmp(name, "objectsize")) {
228 char *s = xmalloc(40);
229 sprintf(s, "%lu", sz);
234 grab_objectname(name, obj->sha1, v);
238 /* See grab_values */
239 static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
242 struct tag *tag = (struct tag *) obj;
244 for (i = 0; i < used_atom_cnt; i++) {
245 const char *name = used_atom[i];
246 struct atom_value *v = &val[i];
247 if (!!deref != (*name == '*'))
251 if (!strcmp(name, "tag"))
253 else if (!strcmp(name, "type") && tag->tagged)
254 v->s = typename(tag->tagged->type);
255 else if (!strcmp(name, "object") && tag->tagged) {
256 char *s = xmalloc(41);
257 strcpy(s, sha1_to_hex(tag->tagged->sha1));
263 /* See grab_values */
264 static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
267 struct commit *commit = (struct commit *) obj;
269 for (i = 0; i < used_atom_cnt; i++) {
270 const char *name = used_atom[i];
271 struct atom_value *v = &val[i];
272 if (!!deref != (*name == '*'))
276 if (!strcmp(name, "tree")) {
277 char *s = xmalloc(41);
278 strcpy(s, sha1_to_hex(commit->tree->object.sha1));
281 if (!strcmp(name, "numparent")) {
282 char *s = xmalloc(40);
283 v->ul = commit_list_count(commit->parents);
284 sprintf(s, "%lu", v->ul);
287 else if (!strcmp(name, "parent")) {
288 int num = commit_list_count(commit->parents);
290 struct commit_list *parents;
291 char *s = xmalloc(41 * num + 1);
293 for (i = 0, parents = commit->parents;
295 parents = parents->next, i = i + 41) {
296 struct commit *parent = parents->item;
297 strcpy(s+i, sha1_to_hex(parent->object.sha1));
307 static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz)
311 if (!strncmp(buf, who, wholen) &&
313 return buf + wholen + 1;
314 eol = strchr(buf, '\n');
319 return ""; /* end of header */
325 static const char *copy_line(const char *buf)
327 const char *eol = strchrnul(buf, '\n');
328 return xmemdupz(buf, eol - buf);
331 static const char *copy_name(const char *buf)
334 for (cp = buf; *cp && *cp != '\n'; cp++) {
335 if (!strncmp(cp, " <", 2))
336 return xmemdupz(buf, cp - buf);
341 static const char *copy_email(const char *buf)
343 const char *email = strchr(buf, '<');
347 eoemail = strchr(email, '>');
350 return xmemdupz(email, eoemail + 1 - email);
353 static char *copy_subject(const char *buf, unsigned long len)
355 char *r = xmemdupz(buf, len);
358 for (i = 0; i < len; i++)
365 static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
367 const char *eoemail = strstr(buf, "> ");
369 unsigned long timestamp;
371 struct date_mode date_mode = { DATE_NORMAL };
375 * We got here because atomname ends in "date" or "date<something>";
376 * it's not possible that <something> is not ":<format>" because
377 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
378 * ":" means no format is specified, and use the default.
380 formatp = strchr(atomname, ':');
381 if (formatp != NULL) {
383 parse_date_format(formatp, &date_mode);
388 timestamp = strtoul(eoemail + 2, &zone, 10);
389 if (timestamp == ULONG_MAX)
391 tz = strtol(zone, NULL, 10);
392 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
394 v->s = xstrdup(show_date(timestamp, tz, &date_mode));
402 /* See grab_values */
403 static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
406 int wholen = strlen(who);
407 const char *wholine = NULL;
409 for (i = 0; i < used_atom_cnt; i++) {
410 const char *name = used_atom[i];
411 struct atom_value *v = &val[i];
412 if (!!deref != (*name == '*'))
416 if (strncmp(who, name, wholen))
418 if (name[wholen] != 0 &&
419 strcmp(name + wholen, "name") &&
420 strcmp(name + wholen, "email") &&
421 !starts_with(name + wholen, "date"))
424 wholine = find_wholine(who, wholen, buf, sz);
426 return; /* no point looking for it */
427 if (name[wholen] == 0)
428 v->s = copy_line(wholine);
429 else if (!strcmp(name + wholen, "name"))
430 v->s = copy_name(wholine);
431 else if (!strcmp(name + wholen, "email"))
432 v->s = copy_email(wholine);
433 else if (starts_with(name + wholen, "date"))
434 grab_date(wholine, v, name);
438 * For a tag or a commit object, if "creator" or "creatordate" is
439 * requested, do something special.
441 if (strcmp(who, "tagger") && strcmp(who, "committer"))
442 return; /* "author" for commit object is not wanted */
444 wholine = find_wholine(who, wholen, buf, sz);
447 for (i = 0; i < used_atom_cnt; i++) {
448 const char *name = used_atom[i];
449 struct atom_value *v = &val[i];
450 if (!!deref != (*name == '*'))
455 if (starts_with(name, "creatordate"))
456 grab_date(wholine, v, name);
457 else if (!strcmp(name, "creator"))
458 v->s = copy_line(wholine);
462 static void find_subpos(const char *buf, unsigned long sz,
463 const char **sub, unsigned long *sublen,
464 const char **body, unsigned long *bodylen,
465 unsigned long *nonsiglen,
466 const char **sig, unsigned long *siglen)
469 /* skip past header until we hit empty line */
470 while (*buf && *buf != '\n') {
471 eol = strchrnul(buf, '\n');
476 /* skip any empty lines */
480 /* parse signature first; we might not even have a subject line */
481 *sig = buf + parse_signature(buf, strlen(buf));
482 *siglen = strlen(*sig);
484 /* subject is first non-empty line */
486 /* subject goes to first empty line */
487 while (buf < *sig && *buf && *buf != '\n') {
488 eol = strchrnul(buf, '\n');
493 *sublen = buf - *sub;
494 /* drop trailing newline, if present */
495 if (*sublen && (*sub)[*sublen - 1] == '\n')
498 /* skip any empty lines */
502 *bodylen = strlen(buf);
503 *nonsiglen = *sig - buf;
506 /* See grab_values */
507 static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
510 const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
511 unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
513 for (i = 0; i < used_atom_cnt; i++) {
514 const char *name = used_atom[i];
515 struct atom_value *v = &val[i];
516 if (!!deref != (*name == '*'))
520 if (strcmp(name, "subject") &&
521 strcmp(name, "body") &&
522 strcmp(name, "contents") &&
523 strcmp(name, "contents:subject") &&
524 strcmp(name, "contents:body") &&
525 strcmp(name, "contents:signature"))
530 &bodypos, &bodylen, &nonsiglen,
533 if (!strcmp(name, "subject"))
534 v->s = copy_subject(subpos, sublen);
535 else if (!strcmp(name, "contents:subject"))
536 v->s = copy_subject(subpos, sublen);
537 else if (!strcmp(name, "body"))
538 v->s = xmemdupz(bodypos, bodylen);
539 else if (!strcmp(name, "contents:body"))
540 v->s = xmemdupz(bodypos, nonsiglen);
541 else if (!strcmp(name, "contents:signature"))
542 v->s = xmemdupz(sigpos, siglen);
543 else if (!strcmp(name, "contents"))
544 v->s = xstrdup(subpos);
549 * We want to have empty print-string for field requests
550 * that do not apply (e.g. "authordate" for a tag object)
552 static void fill_missing_values(struct atom_value *val)
555 for (i = 0; i < used_atom_cnt; i++) {
556 struct atom_value *v = &val[i];
563 * val is a list of atom_value to hold returned values. Extract
564 * the values for atoms in used_atom array out of (obj, buf, sz).
565 * when deref is false, (obj, buf, sz) is the object that is
566 * pointed at by the ref itself; otherwise it is the object the
567 * ref (which is a tag) refers to.
569 static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
571 grab_common_values(val, deref, obj, buf, sz);
574 grab_tag_values(val, deref, obj, buf, sz);
575 grab_sub_body_contents(val, deref, obj, buf, sz);
576 grab_person("tagger", val, deref, obj, buf, sz);
579 grab_commit_values(val, deref, obj, buf, sz);
580 grab_sub_body_contents(val, deref, obj, buf, sz);
581 grab_person("author", val, deref, obj, buf, sz);
582 grab_person("committer", val, deref, obj, buf, sz);
585 /* grab_tree_values(val, deref, obj, buf, sz); */
588 /* grab_blob_values(val, deref, obj, buf, sz); */
591 die("Eh? Object of type %d?", obj->type);
595 static inline char *copy_advance(char *dst, const char *src)
603 * Parse the object referred by ref, and grab needed value.
605 static void populate_value(struct ref_array_item *ref)
611 const unsigned char *tagged;
613 ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
615 if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
616 unsigned char unused1[20];
617 ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
623 /* Fill in specials first */
624 for (i = 0; i < used_atom_cnt; i++) {
625 const char *name = used_atom[i];
626 struct atom_value *v = &ref->value[i];
630 struct branch *branch = NULL;
637 if (starts_with(name, "refname"))
638 refname = ref->refname;
639 else if (starts_with(name, "symref"))
640 refname = ref->symref ? ref->symref : "";
641 else if (starts_with(name, "upstream")) {
642 const char *branch_name;
643 /* only local branches may have an upstream */
644 if (!skip_prefix(ref->refname, "refs/heads/",
647 branch = branch_get(branch_name);
649 refname = branch_get_upstream(branch, NULL);
652 } else if (starts_with(name, "push")) {
653 const char *branch_name;
654 if (!skip_prefix(ref->refname, "refs/heads/",
657 branch = branch_get(branch_name);
659 refname = branch_get_push(branch, NULL);
662 } else if (starts_with(name, "color:")) {
663 char color[COLOR_MAXLEN] = "";
665 if (color_parse(name + 6, color) < 0)
666 die(_("unable to parse format"));
667 v->s = xstrdup(color);
669 } else if (!strcmp(name, "flag")) {
670 char buf[256], *cp = buf;
671 if (ref->flag & REF_ISSYMREF)
672 cp = copy_advance(cp, ",symref");
673 if (ref->flag & REF_ISPACKED)
674 cp = copy_advance(cp, ",packed");
679 v->s = xstrdup(buf + 1);
682 } else if (!deref && grab_objectname(name, ref->objectname, v)) {
684 } else if (!strcmp(name, "HEAD")) {
686 unsigned char sha1[20];
688 head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
690 if (!strcmp(ref->refname, head))
698 formatp = strchr(name, ':');
700 int num_ours, num_theirs;
703 if (!strcmp(formatp, "short"))
704 refname = shorten_unambiguous_ref(refname,
705 warn_ambiguous_refs);
706 else if (!strcmp(formatp, "track") &&
707 (starts_with(name, "upstream") ||
708 starts_with(name, "push"))) {
711 if (stat_tracking_info(branch, &num_ours,
715 if (!num_ours && !num_theirs)
717 else if (!num_ours) {
718 sprintf(buf, "[behind %d]", num_theirs);
720 } else if (!num_theirs) {
721 sprintf(buf, "[ahead %d]", num_ours);
724 sprintf(buf, "[ahead %d, behind %d]",
725 num_ours, num_theirs);
729 } else if (!strcmp(formatp, "trackshort") &&
730 (starts_with(name, "upstream") ||
731 starts_with(name, "push"))) {
734 if (stat_tracking_info(branch, &num_ours,
738 if (!num_ours && !num_theirs)
742 else if (!num_theirs)
748 die("unknown %.*s format %s",
749 (int)(formatp - name), name, formatp);
755 int len = strlen(refname);
756 char *s = xmalloc(len + 4);
757 sprintf(s, "%s^{}", refname);
762 for (i = 0; i < used_atom_cnt; i++) {
763 struct atom_value *v = &ref->value[i];
770 buf = get_obj(ref->objectname, &obj, &size, &eaten);
772 die("missing object %s for %s",
773 sha1_to_hex(ref->objectname), ref->refname);
775 die("parse_object_buffer failed on %s for %s",
776 sha1_to_hex(ref->objectname), ref->refname);
778 grab_values(ref->value, 0, obj, buf, size);
783 * If there is no atom that wants to know about tagged
784 * object, we are done.
786 if (!need_tagged || (obj->type != OBJ_TAG))
790 * If it is a tag object, see if we use a value that derefs
791 * the object, and if we do grab the object it refers to.
793 tagged = ((struct tag *)obj)->tagged->sha1;
796 * NEEDSWORK: This derefs tag only once, which
797 * is good to deal with chains of trust, but
798 * is not consistent with what deref_tag() does
799 * which peels the onion to the core.
801 buf = get_obj(tagged, &obj, &size, &eaten);
803 die("missing object %s for %s",
804 sha1_to_hex(tagged), ref->refname);
806 die("parse_object_buffer failed on %s for %s",
807 sha1_to_hex(tagged), ref->refname);
808 grab_values(ref->value, 1, obj, buf, size);
814 * Given a ref, return the value for the atom. This lazily gets value
815 * out of the object by calling populate value.
817 static void get_ref_atom_value(struct ref_array_item *ref, int atom, struct atom_value **v)
821 fill_missing_values(ref->value);
823 *v = &ref->value[atom];
826 enum contains_result {
827 CONTAINS_UNKNOWN = -1,
833 * Mimicking the real stack, this stack lives on the heap, avoiding stack
836 * At each recursion step, the stack items points to the commits whose
837 * ancestors are to be inspected.
839 struct contains_stack {
841 struct contains_stack_entry {
842 struct commit *commit;
843 struct commit_list *parents;
847 static int in_commit_list(const struct commit_list *want, struct commit *c)
849 for (; want; want = want->next)
850 if (!hashcmp(want->item->object.sha1, c->object.sha1))
856 * Test whether the candidate or one of its parents is contained in the list.
857 * Do not recurse to find out, though, but return -1 if inconclusive.
859 static enum contains_result contains_test(struct commit *candidate,
860 const struct commit_list *want)
862 /* was it previously marked as containing a want commit? */
863 if (candidate->object.flags & TMP_MARK)
865 /* or marked as not possibly containing a want commit? */
866 if (candidate->object.flags & UNINTERESTING)
869 if (in_commit_list(want, candidate)) {
870 candidate->object.flags |= TMP_MARK;
874 if (parse_commit(candidate) < 0)
880 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
882 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
883 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
884 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
887 static enum contains_result contains_tag_algo(struct commit *candidate,
888 const struct commit_list *want)
890 struct contains_stack contains_stack = { 0, 0, NULL };
891 int result = contains_test(candidate, want);
893 if (result != CONTAINS_UNKNOWN)
896 push_to_contains_stack(candidate, &contains_stack);
897 while (contains_stack.nr) {
898 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
899 struct commit *commit = entry->commit;
900 struct commit_list *parents = entry->parents;
903 commit->object.flags |= UNINTERESTING;
907 * If we just popped the stack, parents->item has been marked,
908 * therefore contains_test will return a meaningful 0 or 1.
910 else switch (contains_test(parents->item, want)) {
912 commit->object.flags |= TMP_MARK;
916 entry->parents = parents->next;
918 case CONTAINS_UNKNOWN:
919 push_to_contains_stack(parents->item, &contains_stack);
923 free(contains_stack.contains_stack);
924 return contains_test(candidate, want);
927 static int commit_contains(struct ref_filter *filter, struct commit *commit)
929 if (filter->with_commit_tag_algo)
930 return contains_tag_algo(commit, filter->with_commit);
931 return is_descendant_of(commit, filter->with_commit);
935 * Return 1 if the refname matches one of the patterns, otherwise 0.
936 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
937 * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
938 * matches "refs/heads/m*",too).
940 static int match_name_as_path(const char **pattern, const char *refname)
942 int namelen = strlen(refname);
943 for (; *pattern; pattern++) {
944 const char *p = *pattern;
945 int plen = strlen(p);
947 if ((plen <= namelen) &&
948 !strncmp(refname, p, plen) &&
949 (refname[plen] == '\0' ||
950 refname[plen] == '/' ||
953 if (!wildmatch(p, refname, WM_PATHNAME, NULL))
960 * Given a ref (sha1, refname), check if the ref belongs to the array
961 * of sha1s. If the given ref is a tag, check if the given tag points
962 * at one of the sha1s in the given sha1 array.
963 * the given sha1_array.
965 * 1. Only a single level of inderection is obtained, we might want to
966 * change this to account for multiple levels (e.g. annotated tags
967 * pointing to annotated tags pointing to a commit.)
968 * 2. As the refs are cached we might know what refname peels to without
969 * the need to parse the object via parse_object(). peel_ref() might be a
970 * more efficient alternative to obtain the pointee.
972 static const unsigned char *match_points_at(struct sha1_array *points_at,
973 const unsigned char *sha1,
976 const unsigned char *tagged_sha1 = NULL;
979 if (sha1_array_lookup(points_at, sha1) >= 0)
981 obj = parse_object(sha1);
983 die(_("malformed object at '%s'"), refname);
984 if (obj->type == OBJ_TAG)
985 tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
986 if (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)
991 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
992 static struct ref_array_item *new_ref_array_item(const char *refname,
993 const unsigned char *objectname,
996 size_t len = strlen(refname);
997 struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item) + len + 1);
998 memcpy(ref->refname, refname, len);
999 ref->refname[len] = '\0';
1000 hashcpy(ref->objectname, objectname);
1007 * A call-back given to for_each_ref(). Filter refs and keep them for
1008 * later object processing.
1010 static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
1012 struct ref_filter_cbdata *ref_cbdata = cb_data;
1013 struct ref_filter *filter = ref_cbdata->filter;
1014 struct ref_array_item *ref;
1015 struct commit *commit = NULL;
1017 if (flag & REF_BAD_NAME) {
1018 warning("ignoring ref with broken name %s", refname);
1022 if (flag & REF_ISBROKEN) {
1023 warning("ignoring broken ref %s", refname);
1027 if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))
1030 if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
1034 * A merge filter is applied on refs pointing to commits. Hence
1035 * obtain the commit using the 'oid' available and discard all
1036 * non-commits early. The actual filtering is done later.
1038 if (filter->merge_commit || filter->with_commit) {
1039 commit = lookup_commit_reference_gently(oid->hash, 1);
1042 /* We perform the filtering for the '--contains' option */
1043 if (filter->with_commit &&
1044 !commit_contains(filter, commit))
1049 * We do not open the object yet; sort may only need refname
1050 * to do its job and the resulting list may yet to be pruned
1051 * by maxcount logic.
1053 ref = new_ref_array_item(refname, oid->hash, flag);
1054 ref->commit = commit;
1056 REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);
1057 ref_cbdata->array->items[ref_cbdata->array->nr++] = ref;
1061 /* Free memory allocated for a ref_array_item */
1062 static void free_array_item(struct ref_array_item *item)
1064 free((char *)item->symref);
1068 /* Free all memory allocated for ref_array */
1069 void ref_array_clear(struct ref_array *array)
1073 for (i = 0; i < array->nr; i++)
1074 free_array_item(array->items[i]);
1076 array->items = NULL;
1077 array->nr = array->alloc = 0;
1080 static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata)
1082 struct rev_info revs;
1084 struct ref_filter *filter = ref_cbdata->filter;
1085 struct ref_array *array = ref_cbdata->array;
1086 struct commit **to_clear = xcalloc(sizeof(struct commit *), array->nr);
1088 init_revisions(&revs, NULL);
1090 for (i = 0; i < array->nr; i++) {
1091 struct ref_array_item *item = array->items[i];
1092 add_pending_object(&revs, &item->commit->object, item->refname);
1093 to_clear[i] = item->commit;
1096 filter->merge_commit->object.flags |= UNINTERESTING;
1097 add_pending_object(&revs, &filter->merge_commit->object, "");
1100 if (prepare_revision_walk(&revs))
1101 die(_("revision walk setup failed"));
1106 for (i = 0; i < old_nr; i++) {
1107 struct ref_array_item *item = array->items[i];
1108 struct commit *commit = item->commit;
1110 int is_merged = !!(commit->object.flags & UNINTERESTING);
1112 if (is_merged == (filter->merge == REF_FILTER_MERGED_INCLUDE))
1113 array->items[array->nr++] = array->items[i];
1115 free_array_item(item);
1118 for (i = 0; i < old_nr; i++)
1119 clear_commit_marks(to_clear[i], ALL_REV_FLAGS);
1120 clear_commit_marks(filter->merge_commit, ALL_REV_FLAGS);
1125 * API for filtering a set of refs. Based on the type of refs the user
1126 * has requested, we iterate through those refs and apply filters
1127 * as per the given ref_filter structure and finally store the
1128 * filtered refs in the ref_array structure.
1130 int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
1132 struct ref_filter_cbdata ref_cbdata;
1135 ref_cbdata.array = array;
1136 ref_cbdata.filter = filter;
1138 /* Simple per-ref filtering */
1139 if (type & (FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN))
1140 ret = for_each_rawref(ref_filter_handler, &ref_cbdata);
1141 else if (type & FILTER_REFS_ALL)
1142 ret = for_each_ref(ref_filter_handler, &ref_cbdata);
1144 die("filter_refs: invalid type");
1146 /* Filters that need revision walking */
1147 if (filter->merge_commit)
1148 do_merge_filter(&ref_cbdata);
1153 static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
1155 struct atom_value *va, *vb;
1157 cmp_type cmp_type = used_atom_type[s->atom];
1159 get_ref_atom_value(a, s->atom, &va);
1160 get_ref_atom_value(b, s->atom, &vb);
1163 cmp = strcmp(va->s, vb->s);
1166 if (va->ul < vb->ul)
1168 else if (va->ul == vb->ul)
1174 return (s->reverse) ? -cmp : cmp;
1177 static struct ref_sorting *ref_sorting;
1178 static int compare_refs(const void *a_, const void *b_)
1180 struct ref_array_item *a = *((struct ref_array_item **)a_);
1181 struct ref_array_item *b = *((struct ref_array_item **)b_);
1182 struct ref_sorting *s;
1184 for (s = ref_sorting; s; s = s->next) {
1185 int cmp = cmp_ref_sorting(s, a, b);
1192 void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
1194 ref_sorting = sorting;
1195 qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
1198 static void print_value(struct atom_value *v, int quote_style)
1200 struct strbuf sb = STRBUF_INIT;
1201 switch (quote_style) {
1203 fputs(v->s, stdout);
1206 sq_quote_buf(&sb, v->s);
1209 perl_quote_buf(&sb, v->s);
1212 python_quote_buf(&sb, v->s);
1215 tcl_quote_buf(&sb, v->s);
1218 if (quote_style != QUOTE_NONE) {
1219 fputs(sb.buf, stdout);
1220 strbuf_release(&sb);
1224 static int hex1(char ch)
1226 if ('0' <= ch && ch <= '9')
1228 else if ('a' <= ch && ch <= 'f')
1229 return ch - 'a' + 10;
1230 else if ('A' <= ch && ch <= 'F')
1231 return ch - 'A' + 10;
1234 static int hex2(const char *cp)
1237 return (hex1(cp[0]) << 4) | hex1(cp[1]);
1242 static void emit(const char *cp, const char *ep)
1244 while (*cp && (!ep || cp < ep)) {
1249 int ch = hex2(cp + 1);
1262 void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
1264 const char *cp, *sp, *ep;
1266 for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
1267 struct atom_value *atomv;
1269 ep = strchr(sp, ')');
1272 get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
1273 print_value(atomv, quote_style);
1276 sp = cp + strlen(cp);
1279 if (need_color_reset_at_eol) {
1280 struct atom_value resetv;
1281 char color[COLOR_MAXLEN] = "";
1283 if (color_parse("reset", color) < 0)
1284 die("BUG: couldn't parse 'reset' as a color");
1286 print_value(&resetv, quote_style);
1291 /* If no sorting option is given, use refname to sort as default */
1292 struct ref_sorting *ref_default_sorting(void)
1294 static const char cstr_name[] = "refname";
1296 struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
1298 sorting->next = NULL;
1299 sorting->atom = parse_ref_filter_atom(cstr_name, cstr_name + strlen(cstr_name));
1303 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
1305 struct ref_sorting **sorting_tail = opt->value;
1306 struct ref_sorting *s;
1309 if (!arg) /* should --no-sort void the list ? */
1312 s = xcalloc(1, sizeof(*s));
1313 s->next = *sorting_tail;
1321 s->atom = parse_ref_filter_atom(arg, arg+len);
1325 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
1327 struct ref_filter *rf = opt->value;
1328 unsigned char sha1[20];
1330 rf->merge = starts_with(opt->long_name, "no")
1331 ? REF_FILTER_MERGED_OMIT
1332 : REF_FILTER_MERGED_INCLUDE;
1334 if (get_sha1(arg, sha1))
1335 die(_("malformed object name %s"), arg);
1337 rf->merge_commit = lookup_commit_reference_gently(sha1, 0);
1338 if (!rf->merge_commit)
1339 return opterror(opt, "must point to a commit", 0);