3 #include "parse-options.h"
11 #include "ref-filter.h"
13 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
21 { "objectsize", FIELD_ULONG
},
25 { "numparent", FIELD_ULONG
},
32 { "authordate", FIELD_TIME
},
36 { "committerdate", FIELD_TIME
},
40 { "taggerdate", FIELD_TIME
},
42 { "creatordate", FIELD_TIME
},
46 { "contents:subject" },
48 { "contents:signature" },
58 * An atom is a valid field atom listed above, possibly prefixed with
59 * a "*" to denote deref_tag().
61 * We parse given format string and sort specifiers, and make a list
62 * of properties that we need to extract out of objects. ref_array_item
63 * structure will hold an array of values extracted that can be
64 * indexed with the "atom number", which is an index into this
67 static const char **used_atom
;
68 static cmp_type
*used_atom_type
;
69 static int used_atom_cnt
, need_tagged
, need_symref
;
70 static int need_color_reset_at_eol
;
73 * Used to parse format string and sort specifiers
75 int parse_ref_filter_atom(const char *atom
, const char *ep
)
81 if (*sp
== '*' && sp
< ep
)
84 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
86 /* Do we have the atom already used elsewhere? */
87 for (i
= 0; i
< used_atom_cnt
; i
++) {
88 int len
= strlen(used_atom
[i
]);
89 if (len
== ep
- atom
&& !memcmp(used_atom
[i
], atom
, len
))
93 /* Is the atom a valid one? */
94 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
95 int len
= strlen(valid_atom
[i
].name
);
97 * If the atom name has a colon, strip it and everything after
98 * it off - it specifies the format for this entry, and
99 * shouldn't be used for checking against the valid_atom
102 const char *formatp
= strchr(sp
, ':');
103 if (!formatp
|| ep
< formatp
)
105 if (len
== formatp
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
109 if (ARRAY_SIZE(valid_atom
) <= i
)
110 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
112 /* Add it in, including the deref prefix */
115 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
116 REALLOC_ARRAY(used_atom_type
, used_atom_cnt
);
117 used_atom
[at
] = xmemdupz(atom
, ep
- atom
);
118 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
121 if (!strcmp(used_atom
[at
], "symref"))
127 * In a format string, find the next occurrence of %(atom).
129 static const char *find_next(const char *cp
)
134 * %( is the start of an atom;
135 * %% is a quoted per-cent.
139 else if (cp
[1] == '%')
140 cp
++; /* skip over two % */
141 /* otherwise this is a singleton, literal % */
149 * Make sure the format string is well formed, and parse out
152 int verify_ref_format(const char *format
)
156 need_color_reset_at_eol
= 0;
157 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
158 const char *color
, *ep
= strchr(sp
, ')');
162 return error("malformed format string %s", sp
);
163 /* sp points at "%(" and ep points at the closing ")" */
164 at
= parse_ref_filter_atom(sp
+ 2, ep
);
167 if (skip_prefix(used_atom
[at
], "color:", &color
))
168 need_color_reset_at_eol
= !!strcmp(color
, "reset");
174 * Given an object name, read the object data and size, and return a
175 * "struct object". If the object data we are returning is also borrowed
176 * by the "struct object" representation, set *eaten as well---it is a
177 * signal from parse_object_buffer to us not to free the buffer.
179 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
181 enum object_type type
;
182 void *buf
= read_sha1_file(sha1
, &type
, sz
);
185 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
191 static int grab_objectname(const char *name
, const unsigned char *sha1
,
192 struct atom_value
*v
)
194 if (!strcmp(name
, "objectname")) {
195 v
->s
= xstrdup(sha1_to_hex(sha1
));
198 if (!strcmp(name
, "objectname:short")) {
199 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
205 /* See grab_values */
206 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
210 for (i
= 0; i
< used_atom_cnt
; i
++) {
211 const char *name
= used_atom
[i
];
212 struct atom_value
*v
= &val
[i
];
213 if (!!deref
!= (*name
== '*'))
217 if (!strcmp(name
, "objecttype"))
218 v
->s
= typename(obj
->type
);
219 else if (!strcmp(name
, "objectsize")) {
221 v
->s
= xstrfmt("%lu", sz
);
224 grab_objectname(name
, obj
->sha1
, v
);
228 /* See grab_values */
229 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
232 struct tag
*tag
= (struct tag
*) obj
;
234 for (i
= 0; i
< used_atom_cnt
; i
++) {
235 const char *name
= used_atom
[i
];
236 struct atom_value
*v
= &val
[i
];
237 if (!!deref
!= (*name
== '*'))
241 if (!strcmp(name
, "tag"))
243 else if (!strcmp(name
, "type") && tag
->tagged
)
244 v
->s
= typename(tag
->tagged
->type
);
245 else if (!strcmp(name
, "object") && tag
->tagged
)
246 v
->s
= xstrdup(sha1_to_hex(tag
->tagged
->sha1
));
250 /* See grab_values */
251 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
254 struct commit
*commit
= (struct commit
*) obj
;
256 for (i
= 0; i
< used_atom_cnt
; i
++) {
257 const char *name
= used_atom
[i
];
258 struct atom_value
*v
= &val
[i
];
259 if (!!deref
!= (*name
== '*'))
263 if (!strcmp(name
, "tree")) {
264 v
->s
= xstrdup(sha1_to_hex(commit
->tree
->object
.sha1
));
266 else if (!strcmp(name
, "numparent")) {
267 v
->ul
= commit_list_count(commit
->parents
);
268 v
->s
= xstrfmt("%lu", v
->ul
);
270 else if (!strcmp(name
, "parent")) {
271 struct commit_list
*parents
;
272 struct strbuf s
= STRBUF_INIT
;
273 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
274 struct commit
*parent
= parents
->item
;
275 if (parents
!= commit
->parents
)
276 strbuf_addch(&s
, ' ');
277 strbuf_addstr(&s
, sha1_to_hex(parent
->object
.sha1
));
279 v
->s
= strbuf_detach(&s
, NULL
);
284 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
288 if (!strncmp(buf
, who
, wholen
) &&
290 return buf
+ wholen
+ 1;
291 eol
= strchr(buf
, '\n');
296 return ""; /* end of header */
302 static const char *copy_line(const char *buf
)
304 const char *eol
= strchrnul(buf
, '\n');
305 return xmemdupz(buf
, eol
- buf
);
308 static const char *copy_name(const char *buf
)
311 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
312 if (!strncmp(cp
, " <", 2))
313 return xmemdupz(buf
, cp
- buf
);
318 static const char *copy_email(const char *buf
)
320 const char *email
= strchr(buf
, '<');
324 eoemail
= strchr(email
, '>');
327 return xmemdupz(email
, eoemail
+ 1 - email
);
330 static char *copy_subject(const char *buf
, unsigned long len
)
332 char *r
= xmemdupz(buf
, len
);
335 for (i
= 0; i
< len
; i
++)
342 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
344 const char *eoemail
= strstr(buf
, "> ");
346 unsigned long timestamp
;
348 struct date_mode date_mode
= { DATE_NORMAL
};
352 * We got here because atomname ends in "date" or "date<something>";
353 * it's not possible that <something> is not ":<format>" because
354 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
355 * ":" means no format is specified, and use the default.
357 formatp
= strchr(atomname
, ':');
358 if (formatp
!= NULL
) {
360 parse_date_format(formatp
, &date_mode
);
365 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
366 if (timestamp
== ULONG_MAX
)
368 tz
= strtol(zone
, NULL
, 10);
369 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
371 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
379 /* See grab_values */
380 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
383 int wholen
= strlen(who
);
384 const char *wholine
= NULL
;
386 for (i
= 0; i
< used_atom_cnt
; i
++) {
387 const char *name
= used_atom
[i
];
388 struct atom_value
*v
= &val
[i
];
389 if (!!deref
!= (*name
== '*'))
393 if (strncmp(who
, name
, wholen
))
395 if (name
[wholen
] != 0 &&
396 strcmp(name
+ wholen
, "name") &&
397 strcmp(name
+ wholen
, "email") &&
398 !starts_with(name
+ wholen
, "date"))
401 wholine
= find_wholine(who
, wholen
, buf
, sz
);
403 return; /* no point looking for it */
404 if (name
[wholen
] == 0)
405 v
->s
= copy_line(wholine
);
406 else if (!strcmp(name
+ wholen
, "name"))
407 v
->s
= copy_name(wholine
);
408 else if (!strcmp(name
+ wholen
, "email"))
409 v
->s
= copy_email(wholine
);
410 else if (starts_with(name
+ wholen
, "date"))
411 grab_date(wholine
, v
, name
);
415 * For a tag or a commit object, if "creator" or "creatordate" is
416 * requested, do something special.
418 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
419 return; /* "author" for commit object is not wanted */
421 wholine
= find_wholine(who
, wholen
, buf
, sz
);
424 for (i
= 0; i
< used_atom_cnt
; i
++) {
425 const char *name
= used_atom
[i
];
426 struct atom_value
*v
= &val
[i
];
427 if (!!deref
!= (*name
== '*'))
432 if (starts_with(name
, "creatordate"))
433 grab_date(wholine
, v
, name
);
434 else if (!strcmp(name
, "creator"))
435 v
->s
= copy_line(wholine
);
439 static void find_subpos(const char *buf
, unsigned long sz
,
440 const char **sub
, unsigned long *sublen
,
441 const char **body
, unsigned long *bodylen
,
442 unsigned long *nonsiglen
,
443 const char **sig
, unsigned long *siglen
)
446 /* skip past header until we hit empty line */
447 while (*buf
&& *buf
!= '\n') {
448 eol
= strchrnul(buf
, '\n');
453 /* skip any empty lines */
457 /* parse signature first; we might not even have a subject line */
458 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
459 *siglen
= strlen(*sig
);
461 /* subject is first non-empty line */
463 /* subject goes to first empty line */
464 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
465 eol
= strchrnul(buf
, '\n');
470 *sublen
= buf
- *sub
;
471 /* drop trailing newline, if present */
472 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
475 /* skip any empty lines */
479 *bodylen
= strlen(buf
);
480 *nonsiglen
= *sig
- buf
;
483 /* See grab_values */
484 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
487 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
488 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
490 for (i
= 0; i
< used_atom_cnt
; i
++) {
491 const char *name
= used_atom
[i
];
492 struct atom_value
*v
= &val
[i
];
493 if (!!deref
!= (*name
== '*'))
497 if (strcmp(name
, "subject") &&
498 strcmp(name
, "body") &&
499 strcmp(name
, "contents") &&
500 strcmp(name
, "contents:subject") &&
501 strcmp(name
, "contents:body") &&
502 strcmp(name
, "contents:signature"))
507 &bodypos
, &bodylen
, &nonsiglen
,
510 if (!strcmp(name
, "subject"))
511 v
->s
= copy_subject(subpos
, sublen
);
512 else if (!strcmp(name
, "contents:subject"))
513 v
->s
= copy_subject(subpos
, sublen
);
514 else if (!strcmp(name
, "body"))
515 v
->s
= xmemdupz(bodypos
, bodylen
);
516 else if (!strcmp(name
, "contents:body"))
517 v
->s
= xmemdupz(bodypos
, nonsiglen
);
518 else if (!strcmp(name
, "contents:signature"))
519 v
->s
= xmemdupz(sigpos
, siglen
);
520 else if (!strcmp(name
, "contents"))
521 v
->s
= xstrdup(subpos
);
526 * We want to have empty print-string for field requests
527 * that do not apply (e.g. "authordate" for a tag object)
529 static void fill_missing_values(struct atom_value
*val
)
532 for (i
= 0; i
< used_atom_cnt
; i
++) {
533 struct atom_value
*v
= &val
[i
];
540 * val is a list of atom_value to hold returned values. Extract
541 * the values for atoms in used_atom array out of (obj, buf, sz).
542 * when deref is false, (obj, buf, sz) is the object that is
543 * pointed at by the ref itself; otherwise it is the object the
544 * ref (which is a tag) refers to.
546 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
548 grab_common_values(val
, deref
, obj
, buf
, sz
);
551 grab_tag_values(val
, deref
, obj
, buf
, sz
);
552 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
553 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
556 grab_commit_values(val
, deref
, obj
, buf
, sz
);
557 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
558 grab_person("author", val
, deref
, obj
, buf
, sz
);
559 grab_person("committer", val
, deref
, obj
, buf
, sz
);
562 /* grab_tree_values(val, deref, obj, buf, sz); */
565 /* grab_blob_values(val, deref, obj, buf, sz); */
568 die("Eh? Object of type %d?", obj
->type
);
572 static inline char *copy_advance(char *dst
, const char *src
)
580 * Parse the object referred by ref, and grab needed value.
582 static void populate_value(struct ref_array_item
*ref
)
588 const unsigned char *tagged
;
590 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
592 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
593 unsigned char unused1
[20];
594 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
600 /* Fill in specials first */
601 for (i
= 0; i
< used_atom_cnt
; i
++) {
602 const char *name
= used_atom
[i
];
603 struct atom_value
*v
= &ref
->value
[i
];
607 struct branch
*branch
= NULL
;
614 if (starts_with(name
, "refname"))
615 refname
= ref
->refname
;
616 else if (starts_with(name
, "symref"))
617 refname
= ref
->symref
? ref
->symref
: "";
618 else if (starts_with(name
, "upstream")) {
619 const char *branch_name
;
620 /* only local branches may have an upstream */
621 if (!skip_prefix(ref
->refname
, "refs/heads/",
624 branch
= branch_get(branch_name
);
626 refname
= branch_get_upstream(branch
, NULL
);
629 } else if (starts_with(name
, "push")) {
630 const char *branch_name
;
631 if (!skip_prefix(ref
->refname
, "refs/heads/",
634 branch
= branch_get(branch_name
);
636 refname
= branch_get_push(branch
, NULL
);
639 } else if (starts_with(name
, "color:")) {
640 char color
[COLOR_MAXLEN
] = "";
642 if (color_parse(name
+ 6, color
) < 0)
643 die(_("unable to parse format"));
644 v
->s
= xstrdup(color
);
646 } else if (!strcmp(name
, "flag")) {
647 char buf
[256], *cp
= buf
;
648 if (ref
->flag
& REF_ISSYMREF
)
649 cp
= copy_advance(cp
, ",symref");
650 if (ref
->flag
& REF_ISPACKED
)
651 cp
= copy_advance(cp
, ",packed");
656 v
->s
= xstrdup(buf
+ 1);
659 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
)) {
661 } else if (!strcmp(name
, "HEAD")) {
663 unsigned char sha1
[20];
665 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
667 if (!strcmp(ref
->refname
, head
))
675 formatp
= strchr(name
, ':');
677 int num_ours
, num_theirs
;
680 if (!strcmp(formatp
, "short"))
681 refname
= shorten_unambiguous_ref(refname
,
682 warn_ambiguous_refs
);
683 else if (!strcmp(formatp
, "track") &&
684 (starts_with(name
, "upstream") ||
685 starts_with(name
, "push"))) {
687 if (stat_tracking_info(branch
, &num_ours
,
691 if (!num_ours
&& !num_theirs
)
694 v
->s
= xstrfmt("[behind %d]", num_theirs
);
695 else if (!num_theirs
)
696 v
->s
= xstrfmt("[ahead %d]", num_ours
);
698 v
->s
= xstrfmt("[ahead %d, behind %d]",
699 num_ours
, num_theirs
);
701 } else if (!strcmp(formatp
, "trackshort") &&
702 (starts_with(name
, "upstream") ||
703 starts_with(name
, "push"))) {
706 if (stat_tracking_info(branch
, &num_ours
,
710 if (!num_ours
&& !num_theirs
)
714 else if (!num_theirs
)
720 die("unknown %.*s format %s",
721 (int)(formatp
- name
), name
, formatp
);
727 v
->s
= xstrfmt("%s^{}", refname
);
730 for (i
= 0; i
< used_atom_cnt
; i
++) {
731 struct atom_value
*v
= &ref
->value
[i
];
738 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
740 die("missing object %s for %s",
741 sha1_to_hex(ref
->objectname
), ref
->refname
);
743 die("parse_object_buffer failed on %s for %s",
744 sha1_to_hex(ref
->objectname
), ref
->refname
);
746 grab_values(ref
->value
, 0, obj
, buf
, size
);
751 * If there is no atom that wants to know about tagged
752 * object, we are done.
754 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
758 * If it is a tag object, see if we use a value that derefs
759 * the object, and if we do grab the object it refers to.
761 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
764 * NEEDSWORK: This derefs tag only once, which
765 * is good to deal with chains of trust, but
766 * is not consistent with what deref_tag() does
767 * which peels the onion to the core.
769 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
771 die("missing object %s for %s",
772 sha1_to_hex(tagged
), ref
->refname
);
774 die("parse_object_buffer failed on %s for %s",
775 sha1_to_hex(tagged
), ref
->refname
);
776 grab_values(ref
->value
, 1, obj
, buf
, size
);
782 * Given a ref, return the value for the atom. This lazily gets value
783 * out of the object by calling populate value.
785 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
789 fill_missing_values(ref
->value
);
791 *v
= &ref
->value
[atom
];
795 * Return 1 if the refname matches one of the patterns, otherwise 0.
796 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
797 * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
798 * matches "refs/heads/m*",too).
800 static int match_name_as_path(const char **pattern
, const char *refname
)
802 int namelen
= strlen(refname
);
803 for (; *pattern
; pattern
++) {
804 const char *p
= *pattern
;
805 int plen
= strlen(p
);
807 if ((plen
<= namelen
) &&
808 !strncmp(refname
, p
, plen
) &&
809 (refname
[plen
] == '\0' ||
810 refname
[plen
] == '/' ||
813 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
819 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
820 static struct ref_array_item
*new_ref_array_item(const char *refname
,
821 const unsigned char *objectname
,
824 size_t len
= strlen(refname
);
825 struct ref_array_item
*ref
= xcalloc(1, sizeof(struct ref_array_item
) + len
+ 1);
826 memcpy(ref
->refname
, refname
, len
);
827 ref
->refname
[len
] = '\0';
828 hashcpy(ref
->objectname
, objectname
);
835 * A call-back given to for_each_ref(). Filter refs and keep them for
836 * later object processing.
838 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
840 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
841 struct ref_filter
*filter
= ref_cbdata
->filter
;
842 struct ref_array_item
*ref
;
844 if (flag
& REF_BAD_NAME
) {
845 warning("ignoring ref with broken name %s", refname
);
849 if (flag
& REF_ISBROKEN
) {
850 warning("ignoring broken ref %s", refname
);
854 if (*filter
->name_patterns
&& !match_name_as_path(filter
->name_patterns
, refname
))
858 * We do not open the object yet; sort may only need refname
859 * to do its job and the resulting list may yet to be pruned
862 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
864 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
865 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
869 /* Free memory allocated for a ref_array_item */
870 static void free_array_item(struct ref_array_item
*item
)
872 free((char *)item
->symref
);
876 /* Free all memory allocated for ref_array */
877 void ref_array_clear(struct ref_array
*array
)
881 for (i
= 0; i
< array
->nr
; i
++)
882 free_array_item(array
->items
[i
]);
885 array
->nr
= array
->alloc
= 0;
889 * API for filtering a set of refs. Based on the type of refs the user
890 * has requested, we iterate through those refs and apply filters
891 * as per the given ref_filter structure and finally store the
892 * filtered refs in the ref_array structure.
894 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
896 struct ref_filter_cbdata ref_cbdata
;
898 ref_cbdata
.array
= array
;
899 ref_cbdata
.filter
= filter
;
901 if (type
& (FILTER_REFS_ALL
| FILTER_REFS_INCLUDE_BROKEN
))
902 return for_each_rawref(ref_filter_handler
, &ref_cbdata
);
903 else if (type
& FILTER_REFS_ALL
)
904 return for_each_ref(ref_filter_handler
, &ref_cbdata
);
906 die("filter_refs: invalid type");
910 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
912 struct atom_value
*va
, *vb
;
914 cmp_type cmp_type
= used_atom_type
[s
->atom
];
916 get_ref_atom_value(a
, s
->atom
, &va
);
917 get_ref_atom_value(b
, s
->atom
, &vb
);
920 cmp
= strcmp(va
->s
, vb
->s
);
925 else if (va
->ul
== vb
->ul
)
931 return (s
->reverse
) ? -cmp
: cmp
;
934 static struct ref_sorting
*ref_sorting
;
935 static int compare_refs(const void *a_
, const void *b_
)
937 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
938 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
939 struct ref_sorting
*s
;
941 for (s
= ref_sorting
; s
; s
= s
->next
) {
942 int cmp
= cmp_ref_sorting(s
, a
, b
);
949 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
951 ref_sorting
= sorting
;
952 qsort(array
->items
, array
->nr
, sizeof(struct ref_array_item
*), compare_refs
);
955 static void print_value(struct atom_value
*v
, int quote_style
)
957 struct strbuf sb
= STRBUF_INIT
;
958 switch (quote_style
) {
963 sq_quote_buf(&sb
, v
->s
);
966 perl_quote_buf(&sb
, v
->s
);
969 python_quote_buf(&sb
, v
->s
);
972 tcl_quote_buf(&sb
, v
->s
);
975 if (quote_style
!= QUOTE_NONE
) {
976 fputs(sb
.buf
, stdout
);
981 static int hex1(char ch
)
983 if ('0' <= ch
&& ch
<= '9')
985 else if ('a' <= ch
&& ch
<= 'f')
986 return ch
- 'a' + 10;
987 else if ('A' <= ch
&& ch
<= 'F')
988 return ch
- 'A' + 10;
991 static int hex2(const char *cp
)
994 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
999 static void emit(const char *cp
, const char *ep
)
1001 while (*cp
&& (!ep
|| cp
< ep
)) {
1006 int ch
= hex2(cp
+ 1);
1019 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
1021 const char *cp
, *sp
, *ep
;
1023 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1024 struct atom_value
*atomv
;
1026 ep
= strchr(sp
, ')');
1029 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
1030 print_value(atomv
, quote_style
);
1033 sp
= cp
+ strlen(cp
);
1036 if (need_color_reset_at_eol
) {
1037 struct atom_value resetv
;
1038 char color
[COLOR_MAXLEN
] = "";
1040 if (color_parse("reset", color
) < 0)
1041 die("BUG: couldn't parse 'reset' as a color");
1043 print_value(&resetv
, quote_style
);
1048 /* If no sorting option is given, use refname to sort as default */
1049 struct ref_sorting
*ref_default_sorting(void)
1051 static const char cstr_name
[] = "refname";
1053 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
1055 sorting
->next
= NULL
;
1056 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1060 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
1062 struct ref_sorting
**sorting_tail
= opt
->value
;
1063 struct ref_sorting
*s
;
1066 if (!arg
) /* should --no-sort void the list ? */
1069 s
= xcalloc(1, sizeof(*s
));
1070 s
->next
= *sorting_tail
;
1078 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);