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" },
59 * An atom is a valid field atom listed above, possibly prefixed with
60 * a "*" to denote deref_tag().
62 * We parse given format string and sort specifiers, and make a list
63 * of properties that we need to extract out of objects. ref_array_item
64 * structure will hold an array of values extracted that can be
65 * indexed with the "atom number", which is an index into this
68 static const char **used_atom
;
69 static cmp_type
*used_atom_type
;
70 static int used_atom_cnt
, need_tagged
, need_symref
;
71 static int need_color_reset_at_eol
;
74 * Used to parse format string and sort specifiers
76 int parse_ref_filter_atom(const char *atom
, const char *ep
)
82 if (*sp
== '*' && sp
< ep
)
85 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
87 /* Do we have the atom already used elsewhere? */
88 for (i
= 0; i
< used_atom_cnt
; i
++) {
89 int len
= strlen(used_atom
[i
]);
90 if (len
== ep
- atom
&& !memcmp(used_atom
[i
], atom
, len
))
94 /* Is the atom a valid one? */
95 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
96 int len
= strlen(valid_atom
[i
].name
);
98 * If the atom name has a colon, strip it and everything after
99 * it off - it specifies the format for this entry, and
100 * shouldn't be used for checking against the valid_atom
103 const char *formatp
= strchr(sp
, ':');
104 if (!formatp
|| ep
< formatp
)
106 if (len
== formatp
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
110 if (ARRAY_SIZE(valid_atom
) <= i
)
111 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
113 /* Add it in, including the deref prefix */
116 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
117 REALLOC_ARRAY(used_atom_type
, used_atom_cnt
);
118 used_atom
[at
] = xmemdupz(atom
, ep
- atom
);
119 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
122 if (!strcmp(used_atom
[at
], "symref"))
128 * In a format string, find the next occurrence of %(atom).
130 static const char *find_next(const char *cp
)
135 * %( is the start of an atom;
136 * %% is a quoted per-cent.
140 else if (cp
[1] == '%')
141 cp
++; /* skip over two % */
142 /* otherwise this is a singleton, literal % */
150 * Make sure the format string is well formed, and parse out
153 int verify_ref_format(const char *format
)
157 need_color_reset_at_eol
= 0;
158 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
159 const char *color
, *ep
= strchr(sp
, ')');
163 return error("malformed format string %s", sp
);
164 /* sp points at "%(" and ep points at the closing ")" */
165 at
= parse_ref_filter_atom(sp
+ 2, ep
);
168 if (skip_prefix(used_atom
[at
], "color:", &color
))
169 need_color_reset_at_eol
= !!strcmp(color
, "reset");
175 * Given an object name, read the object data and size, and return a
176 * "struct object". If the object data we are returning is also borrowed
177 * by the "struct object" representation, set *eaten as well---it is a
178 * signal from parse_object_buffer to us not to free the buffer.
180 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
182 enum object_type type
;
183 void *buf
= read_sha1_file(sha1
, &type
, sz
);
186 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
192 static int grab_objectname(const char *name
, const unsigned char *sha1
,
193 struct atom_value
*v
)
195 if (!strcmp(name
, "objectname")) {
196 char *s
= xmalloc(41);
197 strcpy(s
, sha1_to_hex(sha1
));
201 if (!strcmp(name
, "objectname:short")) {
202 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
208 /* See grab_values */
209 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
213 for (i
= 0; i
< used_atom_cnt
; i
++) {
214 const char *name
= used_atom
[i
];
215 struct atom_value
*v
= &val
[i
];
216 if (!!deref
!= (*name
== '*'))
220 if (!strcmp(name
, "objecttype"))
221 v
->s
= typename(obj
->type
);
222 else if (!strcmp(name
, "objectsize")) {
223 char *s
= xmalloc(40);
224 sprintf(s
, "%lu", sz
);
229 grab_objectname(name
, obj
->sha1
, v
);
233 /* See grab_values */
234 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
237 struct tag
*tag
= (struct tag
*) obj
;
239 for (i
= 0; i
< used_atom_cnt
; i
++) {
240 const char *name
= used_atom
[i
];
241 struct atom_value
*v
= &val
[i
];
242 if (!!deref
!= (*name
== '*'))
246 if (!strcmp(name
, "tag"))
248 else if (!strcmp(name
, "type") && tag
->tagged
)
249 v
->s
= typename(tag
->tagged
->type
);
250 else if (!strcmp(name
, "object") && tag
->tagged
) {
251 char *s
= xmalloc(41);
252 strcpy(s
, sha1_to_hex(tag
->tagged
->sha1
));
258 /* See grab_values */
259 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
262 struct commit
*commit
= (struct commit
*) obj
;
264 for (i
= 0; i
< used_atom_cnt
; i
++) {
265 const char *name
= used_atom
[i
];
266 struct atom_value
*v
= &val
[i
];
267 if (!!deref
!= (*name
== '*'))
271 if (!strcmp(name
, "tree")) {
272 char *s
= xmalloc(41);
273 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
276 if (!strcmp(name
, "numparent")) {
277 char *s
= xmalloc(40);
278 v
->ul
= commit_list_count(commit
->parents
);
279 sprintf(s
, "%lu", v
->ul
);
282 else if (!strcmp(name
, "parent")) {
283 int num
= commit_list_count(commit
->parents
);
285 struct commit_list
*parents
;
286 char *s
= xmalloc(41 * num
+ 1);
288 for (i
= 0, parents
= commit
->parents
;
290 parents
= parents
->next
, i
= i
+ 41) {
291 struct commit
*parent
= parents
->item
;
292 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
302 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
306 if (!strncmp(buf
, who
, wholen
) &&
308 return buf
+ wholen
+ 1;
309 eol
= strchr(buf
, '\n');
314 return ""; /* end of header */
320 static const char *copy_line(const char *buf
)
322 const char *eol
= strchrnul(buf
, '\n');
323 return xmemdupz(buf
, eol
- buf
);
326 static const char *copy_name(const char *buf
)
329 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
330 if (!strncmp(cp
, " <", 2))
331 return xmemdupz(buf
, cp
- buf
);
336 static const char *copy_email(const char *buf
)
338 const char *email
= strchr(buf
, '<');
342 eoemail
= strchr(email
, '>');
345 return xmemdupz(email
, eoemail
+ 1 - email
);
348 static char *copy_subject(const char *buf
, unsigned long len
)
350 char *r
= xmemdupz(buf
, len
);
353 for (i
= 0; i
< len
; i
++)
360 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
362 const char *eoemail
= strstr(buf
, "> ");
364 unsigned long timestamp
;
366 enum date_mode date_mode
= DATE_NORMAL
;
370 * We got here because atomname ends in "date" or "date<something>";
371 * it's not possible that <something> is not ":<format>" because
372 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
373 * ":" means no format is specified, and use the default.
375 formatp
= strchr(atomname
, ':');
376 if (formatp
!= NULL
) {
378 date_mode
= parse_date_format(formatp
);
383 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
384 if (timestamp
== ULONG_MAX
)
386 tz
= strtol(zone
, NULL
, 10);
387 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
389 v
->s
= xstrdup(show_date(timestamp
, tz
, date_mode
));
397 /* See grab_values */
398 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
401 int wholen
= strlen(who
);
402 const char *wholine
= NULL
;
404 for (i
= 0; i
< used_atom_cnt
; i
++) {
405 const char *name
= used_atom
[i
];
406 struct atom_value
*v
= &val
[i
];
407 if (!!deref
!= (*name
== '*'))
411 if (strncmp(who
, name
, wholen
))
413 if (name
[wholen
] != 0 &&
414 strcmp(name
+ wholen
, "name") &&
415 strcmp(name
+ wholen
, "email") &&
416 !starts_with(name
+ wholen
, "date"))
419 wholine
= find_wholine(who
, wholen
, buf
, sz
);
421 return; /* no point looking for it */
422 if (name
[wholen
] == 0)
423 v
->s
= copy_line(wholine
);
424 else if (!strcmp(name
+ wholen
, "name"))
425 v
->s
= copy_name(wholine
);
426 else if (!strcmp(name
+ wholen
, "email"))
427 v
->s
= copy_email(wholine
);
428 else if (starts_with(name
+ wholen
, "date"))
429 grab_date(wholine
, v
, name
);
433 * For a tag or a commit object, if "creator" or "creatordate" is
434 * requested, do something special.
436 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
437 return; /* "author" for commit object is not wanted */
439 wholine
= find_wholine(who
, wholen
, buf
, sz
);
442 for (i
= 0; i
< used_atom_cnt
; i
++) {
443 const char *name
= used_atom
[i
];
444 struct atom_value
*v
= &val
[i
];
445 if (!!deref
!= (*name
== '*'))
450 if (starts_with(name
, "creatordate"))
451 grab_date(wholine
, v
, name
);
452 else if (!strcmp(name
, "creator"))
453 v
->s
= copy_line(wholine
);
457 static void find_subpos(const char *buf
, unsigned long sz
,
458 const char **sub
, unsigned long *sublen
,
459 const char **body
, unsigned long *bodylen
,
460 unsigned long *nonsiglen
,
461 const char **sig
, unsigned long *siglen
)
464 /* skip past header until we hit empty line */
465 while (*buf
&& *buf
!= '\n') {
466 eol
= strchrnul(buf
, '\n');
471 /* skip any empty lines */
475 /* parse signature first; we might not even have a subject line */
476 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
477 *siglen
= strlen(*sig
);
479 /* subject is first non-empty line */
481 /* subject goes to first empty line */
482 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
483 eol
= strchrnul(buf
, '\n');
488 *sublen
= buf
- *sub
;
489 /* drop trailing newline, if present */
490 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
493 /* skip any empty lines */
497 *bodylen
= strlen(buf
);
498 *nonsiglen
= *sig
- buf
;
501 /* See grab_values */
502 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
505 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
506 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
508 for (i
= 0; i
< used_atom_cnt
; i
++) {
509 const char *name
= used_atom
[i
];
510 struct atom_value
*v
= &val
[i
];
511 if (!!deref
!= (*name
== '*'))
515 if (strcmp(name
, "subject") &&
516 strcmp(name
, "body") &&
517 strcmp(name
, "contents") &&
518 strcmp(name
, "contents:subject") &&
519 strcmp(name
, "contents:body") &&
520 strcmp(name
, "contents:signature"))
525 &bodypos
, &bodylen
, &nonsiglen
,
528 if (!strcmp(name
, "subject"))
529 v
->s
= copy_subject(subpos
, sublen
);
530 else if (!strcmp(name
, "contents:subject"))
531 v
->s
= copy_subject(subpos
, sublen
);
532 else if (!strcmp(name
, "body"))
533 v
->s
= xmemdupz(bodypos
, bodylen
);
534 else if (!strcmp(name
, "contents:body"))
535 v
->s
= xmemdupz(bodypos
, nonsiglen
);
536 else if (!strcmp(name
, "contents:signature"))
537 v
->s
= xmemdupz(sigpos
, siglen
);
538 else if (!strcmp(name
, "contents"))
539 v
->s
= xstrdup(subpos
);
544 * We want to have empty print-string for field requests
545 * that do not apply (e.g. "authordate" for a tag object)
547 static void fill_missing_values(struct atom_value
*val
)
550 for (i
= 0; i
< used_atom_cnt
; i
++) {
551 struct atom_value
*v
= &val
[i
];
558 * val is a list of atom_value to hold returned values. Extract
559 * the values for atoms in used_atom array out of (obj, buf, sz).
560 * when deref is false, (obj, buf, sz) is the object that is
561 * pointed at by the ref itself; otherwise it is the object the
562 * ref (which is a tag) refers to.
564 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
566 grab_common_values(val
, deref
, obj
, buf
, sz
);
569 grab_tag_values(val
, deref
, obj
, buf
, sz
);
570 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
571 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
574 grab_commit_values(val
, deref
, obj
, buf
, sz
);
575 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
576 grab_person("author", val
, deref
, obj
, buf
, sz
);
577 grab_person("committer", val
, deref
, obj
, buf
, sz
);
580 /* grab_tree_values(val, deref, obj, buf, sz); */
583 /* grab_blob_values(val, deref, obj, buf, sz); */
586 die("Eh? Object of type %d?", obj
->type
);
590 static inline char *copy_advance(char *dst
, const char *src
)
598 * Parse the object referred by ref, and grab needed value.
600 static void populate_value(struct ref_array_item
*ref
)
606 const unsigned char *tagged
;
608 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
610 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
611 unsigned char unused1
[20];
612 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
618 /* Fill in specials first */
619 for (i
= 0; i
< used_atom_cnt
; i
++) {
620 const char *name
= used_atom
[i
];
621 struct atom_value
*v
= &ref
->value
[i
];
625 struct branch
*branch
= NULL
;
632 if (starts_with(name
, "refname"))
633 refname
= ref
->refname
;
634 else if (starts_with(name
, "symref"))
635 refname
= ref
->symref
? ref
->symref
: "";
636 else if (starts_with(name
, "upstream")) {
637 const char *branch_name
;
638 /* only local branches may have an upstream */
639 if (!skip_prefix(ref
->refname
, "refs/heads/",
642 branch
= branch_get(branch_name
);
644 refname
= branch_get_upstream(branch
, NULL
);
647 } else if (starts_with(name
, "push")) {
648 const char *branch_name
;
649 if (!skip_prefix(ref
->refname
, "refs/heads/",
652 branch
= branch_get(branch_name
);
654 refname
= branch_get_push(branch
, NULL
);
657 } else if (starts_with(name
, "color:")) {
658 char color
[COLOR_MAXLEN
] = "";
660 if (color_parse(name
+ 6, color
) < 0)
661 die(_("unable to parse format"));
662 v
->s
= xstrdup(color
);
664 } else if (!strcmp(name
, "flag")) {
665 char buf
[256], *cp
= buf
;
666 if (ref
->flag
& REF_ISSYMREF
)
667 cp
= copy_advance(cp
, ",symref");
668 if (ref
->flag
& REF_ISPACKED
)
669 cp
= copy_advance(cp
, ",packed");
674 v
->s
= xstrdup(buf
+ 1);
677 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
)) {
679 } else if (!strcmp(name
, "HEAD")) {
681 unsigned char sha1
[20];
683 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
685 if (!strcmp(ref
->refname
, head
))
693 formatp
= strchr(name
, ':');
695 int num_ours
, num_theirs
;
698 if (!strcmp(formatp
, "short"))
699 refname
= shorten_unambiguous_ref(refname
,
700 warn_ambiguous_refs
);
701 else if (!strcmp(formatp
, "track") &&
702 (starts_with(name
, "upstream") ||
703 starts_with(name
, "push"))) {
706 if (stat_tracking_info(branch
, &num_ours
,
710 if (!num_ours
&& !num_theirs
)
712 else if (!num_ours
) {
713 sprintf(buf
, "[behind %d]", num_theirs
);
715 } else if (!num_theirs
) {
716 sprintf(buf
, "[ahead %d]", num_ours
);
719 sprintf(buf
, "[ahead %d, behind %d]",
720 num_ours
, num_theirs
);
724 } else if (!strcmp(formatp
, "trackshort") &&
725 (starts_with(name
, "upstream") ||
726 starts_with(name
, "push"))) {
729 if (stat_tracking_info(branch
, &num_ours
,
733 if (!num_ours
&& !num_theirs
)
737 else if (!num_theirs
)
743 die("unknown %.*s format %s",
744 (int)(formatp
- name
), name
, formatp
);
750 int len
= strlen(refname
);
751 char *s
= xmalloc(len
+ 4);
752 sprintf(s
, "%s^{}", refname
);
757 for (i
= 0; i
< used_atom_cnt
; i
++) {
758 struct atom_value
*v
= &ref
->value
[i
];
765 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
767 die("missing object %s for %s",
768 sha1_to_hex(ref
->objectname
), ref
->refname
);
770 die("parse_object_buffer failed on %s for %s",
771 sha1_to_hex(ref
->objectname
), ref
->refname
);
773 grab_values(ref
->value
, 0, obj
, buf
, size
);
778 * If there is no atom that wants to know about tagged
779 * object, we are done.
781 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
785 * If it is a tag object, see if we use a value that derefs
786 * the object, and if we do grab the object it refers to.
788 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
791 * NEEDSWORK: This derefs tag only once, which
792 * is good to deal with chains of trust, but
793 * is not consistent with what deref_tag() does
794 * which peels the onion to the core.
796 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
798 die("missing object %s for %s",
799 sha1_to_hex(tagged
), ref
->refname
);
801 die("parse_object_buffer failed on %s for %s",
802 sha1_to_hex(tagged
), ref
->refname
);
803 grab_values(ref
->value
, 1, obj
, buf
, size
);
809 * Given a ref, return the value for the atom. This lazily gets value
810 * out of the object by calling populate value.
812 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
816 fill_missing_values(ref
->value
);
818 *v
= &ref
->value
[atom
];
822 * Return 1 if the refname matches one of the patterns, otherwise 0.
823 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
824 * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
825 * matches "refs/heads/m*",too).
827 static int match_name_as_path(const char **pattern
, const char *refname
)
829 int namelen
= strlen(refname
);
830 for (; *pattern
; pattern
++) {
831 const char *p
= *pattern
;
832 int plen
= strlen(p
);
834 if ((plen
<= namelen
) &&
835 !strncmp(refname
, p
, plen
) &&
836 (refname
[plen
] == '\0' ||
837 refname
[plen
] == '/' ||
840 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
847 * Given a ref (sha1, refname), check if the ref belongs to the array
848 * of sha1s. If the given ref is a tag, check if the given tag points
849 * at one of the sha1s in the given sha1 array.
850 * the given sha1_array.
852 * 1. Only a single level of inderection is obtained, we might want to
853 * change this to account for multiple levels (e.g. annotated tags
854 * pointing to annotated tags pointing to a commit.)
855 * 2. As the refs are cached we might know what refname peels to without
856 * the need to parse the object via parse_object(). peel_ref() might be a
857 * more efficient alternative to obtain the pointee.
859 static const unsigned char *match_points_at(struct sha1_array
*points_at
,
860 const unsigned char *sha1
,
863 const unsigned char *tagged_sha1
= NULL
;
866 if (sha1_array_lookup(points_at
, sha1
) >= 0)
868 obj
= parse_object(sha1
);
870 die(_("malformed object at '%s'"), refname
);
871 if (obj
->type
== OBJ_TAG
)
872 tagged_sha1
= ((struct tag
*)obj
)->tagged
->sha1
;
873 if (tagged_sha1
&& sha1_array_lookup(points_at
, tagged_sha1
) >= 0)
878 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
879 static struct ref_array_item
*new_ref_array_item(const char *refname
,
880 const unsigned char *objectname
,
883 size_t len
= strlen(refname
);
884 struct ref_array_item
*ref
= xcalloc(1, sizeof(struct ref_array_item
) + len
+ 1);
885 memcpy(ref
->refname
, refname
, len
);
886 ref
->refname
[len
] = '\0';
887 hashcpy(ref
->objectname
, objectname
);
894 * A call-back given to for_each_ref(). Filter refs and keep them for
895 * later object processing.
897 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
899 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
900 struct ref_filter
*filter
= ref_cbdata
->filter
;
901 struct ref_array_item
*ref
;
902 struct commit
*commit
= NULL
;
904 if (flag
& REF_BAD_NAME
) {
905 warning("ignoring ref with broken name %s", refname
);
909 if (*filter
->name_patterns
&& !match_name_as_path(filter
->name_patterns
, refname
))
912 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
->hash
, refname
))
916 * A merge filter is applied on refs pointing to commits. Hence
917 * obtain the commit using the 'oid' available and discard all
918 * non-commits early. The actual filtering is done later.
920 if (filter
->merge_commit
) {
921 commit
= lookup_commit_reference_gently(oid
->hash
, 1);
927 * We do not open the object yet; sort may only need refname
928 * to do its job and the resulting list may yet to be pruned
931 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
932 ref
->commit
= commit
;
934 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
935 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
939 /* Free memory allocated for a ref_array_item */
940 static void free_array_item(struct ref_array_item
*item
)
942 free((char *)item
->symref
);
946 /* Free all memory allocated for ref_array */
947 void ref_array_clear(struct ref_array
*array
)
951 for (i
= 0; i
< array
->nr
; i
++)
952 free_array_item(array
->items
[i
]);
955 array
->nr
= array
->alloc
= 0;
958 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
960 struct rev_info revs
;
962 struct ref_filter
*filter
= ref_cbdata
->filter
;
963 struct ref_array
*array
= ref_cbdata
->array
;
964 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
966 init_revisions(&revs
, NULL
);
968 for (i
= 0; i
< array
->nr
; i
++) {
969 struct ref_array_item
*item
= array
->items
[i
];
970 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
971 to_clear
[i
] = item
->commit
;
974 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
975 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
978 if (prepare_revision_walk(&revs
))
979 die(_("revision walk setup failed"));
984 for (i
= 0; i
< old_nr
; i
++) {
985 struct ref_array_item
*item
= array
->items
[i
];
986 struct commit
*commit
= item
->commit
;
988 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
990 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
991 array
->items
[array
->nr
++] = array
->items
[i
];
993 free_array_item(item
);
996 for (i
= 0; i
< old_nr
; i
++)
997 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
998 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1003 * API for filtering a set of refs. Based on the type of refs the user
1004 * has requested, we iterate through those refs and apply filters
1005 * as per the given ref_filter structure and finally store the
1006 * filtered refs in the ref_array structure.
1008 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1010 struct ref_filter_cbdata ref_cbdata
;
1013 ref_cbdata
.array
= array
;
1014 ref_cbdata
.filter
= filter
;
1016 /* Simple per-ref filtering */
1017 if (type
& (FILTER_REFS_ALL
| FILTER_REFS_INCLUDE_BROKEN
))
1018 ret
= for_each_rawref(ref_filter_handler
, &ref_cbdata
);
1019 else if (type
& FILTER_REFS_ALL
)
1020 ret
= for_each_ref(ref_filter_handler
, &ref_cbdata
);
1022 die("filter_refs: invalid type");
1024 /* Filters that need revision walking */
1025 if (filter
->merge_commit
)
1026 do_merge_filter(&ref_cbdata
);
1031 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
1033 struct atom_value
*va
, *vb
;
1035 cmp_type cmp_type
= used_atom_type
[s
->atom
];
1037 get_ref_atom_value(a
, s
->atom
, &va
);
1038 get_ref_atom_value(b
, s
->atom
, &vb
);
1041 cmp
= strcmp(va
->s
, vb
->s
);
1044 if (va
->ul
< vb
->ul
)
1046 else if (va
->ul
== vb
->ul
)
1052 return (s
->reverse
) ? -cmp
: cmp
;
1055 static struct ref_sorting
*ref_sorting
;
1056 static int compare_refs(const void *a_
, const void *b_
)
1058 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
1059 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
1060 struct ref_sorting
*s
;
1062 for (s
= ref_sorting
; s
; s
= s
->next
) {
1063 int cmp
= cmp_ref_sorting(s
, a
, b
);
1070 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
1072 ref_sorting
= sorting
;
1073 qsort(array
->items
, array
->nr
, sizeof(struct ref_array_item
*), compare_refs
);
1076 static void print_value(struct atom_value
*v
, int quote_style
)
1078 struct strbuf sb
= STRBUF_INIT
;
1079 switch (quote_style
) {
1081 fputs(v
->s
, stdout
);
1084 sq_quote_buf(&sb
, v
->s
);
1087 perl_quote_buf(&sb
, v
->s
);
1090 python_quote_buf(&sb
, v
->s
);
1093 tcl_quote_buf(&sb
, v
->s
);
1096 if (quote_style
!= QUOTE_NONE
) {
1097 fputs(sb
.buf
, stdout
);
1098 strbuf_release(&sb
);
1102 static int hex1(char ch
)
1104 if ('0' <= ch
&& ch
<= '9')
1106 else if ('a' <= ch
&& ch
<= 'f')
1107 return ch
- 'a' + 10;
1108 else if ('A' <= ch
&& ch
<= 'F')
1109 return ch
- 'A' + 10;
1112 static int hex2(const char *cp
)
1115 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
1120 static void emit(const char *cp
, const char *ep
)
1122 while (*cp
&& (!ep
|| cp
< ep
)) {
1127 int ch
= hex2(cp
+ 1);
1140 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
1142 const char *cp
, *sp
, *ep
;
1144 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1145 struct atom_value
*atomv
;
1147 ep
= strchr(sp
, ')');
1150 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
1151 print_value(atomv
, quote_style
);
1154 sp
= cp
+ strlen(cp
);
1157 if (need_color_reset_at_eol
) {
1158 struct atom_value resetv
;
1159 char color
[COLOR_MAXLEN
] = "";
1161 if (color_parse("reset", color
) < 0)
1162 die("BUG: couldn't parse 'reset' as a color");
1164 print_value(&resetv
, quote_style
);
1169 /* If no sorting option is given, use refname to sort as default */
1170 struct ref_sorting
*ref_default_sorting(void)
1172 static const char cstr_name
[] = "refname";
1174 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
1176 sorting
->next
= NULL
;
1177 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1181 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
1183 struct ref_sorting
**sorting_tail
= opt
->value
;
1184 struct ref_sorting
*s
;
1187 if (!arg
) /* should --no-sort void the list ? */
1190 s
= xcalloc(1, sizeof(*s
));
1191 s
->next
= *sorting_tail
;
1199 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);
1203 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
1205 struct ref_filter
*rf
= opt
->value
;
1206 unsigned char sha1
[20];
1208 rf
->merge
= starts_with(opt
->long_name
, "no")
1209 ? REF_FILTER_MERGED_OMIT
1210 : REF_FILTER_MERGED_INCLUDE
;
1212 if (get_sha1(arg
, sha1
))
1213 die(_("malformed object name %s"), arg
);
1215 rf
->merge_commit
= lookup_commit_reference_gently(sha1
, 0);
1216 if (!rf
->merge_commit
)
1217 return opterror(opt
, "must point to a commit", 0);