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 char *s
= xmalloc(41);
196 strcpy(s
, sha1_to_hex(sha1
));
200 if (!strcmp(name
, "objectname:short")) {
201 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
207 /* See grab_values */
208 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
212 for (i
= 0; i
< used_atom_cnt
; i
++) {
213 const char *name
= used_atom
[i
];
214 struct atom_value
*v
= &val
[i
];
215 if (!!deref
!= (*name
== '*'))
219 if (!strcmp(name
, "objecttype"))
220 v
->s
= typename(obj
->type
);
221 else if (!strcmp(name
, "objectsize")) {
222 char *s
= xmalloc(40);
223 sprintf(s
, "%lu", sz
);
228 grab_objectname(name
, obj
->sha1
, v
);
232 /* See grab_values */
233 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
236 struct tag
*tag
= (struct tag
*) obj
;
238 for (i
= 0; i
< used_atom_cnt
; i
++) {
239 const char *name
= used_atom
[i
];
240 struct atom_value
*v
= &val
[i
];
241 if (!!deref
!= (*name
== '*'))
245 if (!strcmp(name
, "tag"))
247 else if (!strcmp(name
, "type") && tag
->tagged
)
248 v
->s
= typename(tag
->tagged
->type
);
249 else if (!strcmp(name
, "object") && tag
->tagged
) {
250 char *s
= xmalloc(41);
251 strcpy(s
, sha1_to_hex(tag
->tagged
->sha1
));
257 /* See grab_values */
258 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
261 struct commit
*commit
= (struct commit
*) obj
;
263 for (i
= 0; i
< used_atom_cnt
; i
++) {
264 const char *name
= used_atom
[i
];
265 struct atom_value
*v
= &val
[i
];
266 if (!!deref
!= (*name
== '*'))
270 if (!strcmp(name
, "tree")) {
271 char *s
= xmalloc(41);
272 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
275 if (!strcmp(name
, "numparent")) {
276 char *s
= xmalloc(40);
277 v
->ul
= commit_list_count(commit
->parents
);
278 sprintf(s
, "%lu", v
->ul
);
281 else if (!strcmp(name
, "parent")) {
282 int num
= commit_list_count(commit
->parents
);
284 struct commit_list
*parents
;
285 char *s
= xmalloc(41 * num
+ 1);
287 for (i
= 0, parents
= commit
->parents
;
289 parents
= parents
->next
, i
= i
+ 41) {
290 struct commit
*parent
= parents
->item
;
291 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
301 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
305 if (!strncmp(buf
, who
, wholen
) &&
307 return buf
+ wholen
+ 1;
308 eol
= strchr(buf
, '\n');
313 return ""; /* end of header */
319 static const char *copy_line(const char *buf
)
321 const char *eol
= strchrnul(buf
, '\n');
322 return xmemdupz(buf
, eol
- buf
);
325 static const char *copy_name(const char *buf
)
328 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
329 if (!strncmp(cp
, " <", 2))
330 return xmemdupz(buf
, cp
- buf
);
335 static const char *copy_email(const char *buf
)
337 const char *email
= strchr(buf
, '<');
341 eoemail
= strchr(email
, '>');
344 return xmemdupz(email
, eoemail
+ 1 - email
);
347 static char *copy_subject(const char *buf
, unsigned long len
)
349 char *r
= xmemdupz(buf
, len
);
352 for (i
= 0; i
< len
; i
++)
359 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
361 const char *eoemail
= strstr(buf
, "> ");
363 unsigned long timestamp
;
365 struct date_mode date_mode
= { DATE_NORMAL
};
369 * We got here because atomname ends in "date" or "date<something>";
370 * it's not possible that <something> is not ":<format>" because
371 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
372 * ":" means no format is specified, and use the default.
374 formatp
= strchr(atomname
, ':');
375 if (formatp
!= NULL
) {
377 parse_date_format(formatp
, &date_mode
);
382 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
383 if (timestamp
== ULONG_MAX
)
385 tz
= strtol(zone
, NULL
, 10);
386 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
388 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
396 /* See grab_values */
397 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
400 int wholen
= strlen(who
);
401 const char *wholine
= NULL
;
403 for (i
= 0; i
< used_atom_cnt
; i
++) {
404 const char *name
= used_atom
[i
];
405 struct atom_value
*v
= &val
[i
];
406 if (!!deref
!= (*name
== '*'))
410 if (strncmp(who
, name
, wholen
))
412 if (name
[wholen
] != 0 &&
413 strcmp(name
+ wholen
, "name") &&
414 strcmp(name
+ wholen
, "email") &&
415 !starts_with(name
+ wholen
, "date"))
418 wholine
= find_wholine(who
, wholen
, buf
, sz
);
420 return; /* no point looking for it */
421 if (name
[wholen
] == 0)
422 v
->s
= copy_line(wholine
);
423 else if (!strcmp(name
+ wholen
, "name"))
424 v
->s
= copy_name(wholine
);
425 else if (!strcmp(name
+ wholen
, "email"))
426 v
->s
= copy_email(wholine
);
427 else if (starts_with(name
+ wholen
, "date"))
428 grab_date(wholine
, v
, name
);
432 * For a tag or a commit object, if "creator" or "creatordate" is
433 * requested, do something special.
435 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
436 return; /* "author" for commit object is not wanted */
438 wholine
= find_wholine(who
, wholen
, buf
, sz
);
441 for (i
= 0; i
< used_atom_cnt
; i
++) {
442 const char *name
= used_atom
[i
];
443 struct atom_value
*v
= &val
[i
];
444 if (!!deref
!= (*name
== '*'))
449 if (starts_with(name
, "creatordate"))
450 grab_date(wholine
, v
, name
);
451 else if (!strcmp(name
, "creator"))
452 v
->s
= copy_line(wholine
);
456 static void find_subpos(const char *buf
, unsigned long sz
,
457 const char **sub
, unsigned long *sublen
,
458 const char **body
, unsigned long *bodylen
,
459 unsigned long *nonsiglen
,
460 const char **sig
, unsigned long *siglen
)
463 /* skip past header until we hit empty line */
464 while (*buf
&& *buf
!= '\n') {
465 eol
= strchrnul(buf
, '\n');
470 /* skip any empty lines */
474 /* parse signature first; we might not even have a subject line */
475 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
476 *siglen
= strlen(*sig
);
478 /* subject is first non-empty line */
480 /* subject goes to first empty line */
481 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
482 eol
= strchrnul(buf
, '\n');
487 *sublen
= buf
- *sub
;
488 /* drop trailing newline, if present */
489 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
492 /* skip any empty lines */
496 *bodylen
= strlen(buf
);
497 *nonsiglen
= *sig
- buf
;
500 /* See grab_values */
501 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
504 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
505 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
507 for (i
= 0; i
< used_atom_cnt
; i
++) {
508 const char *name
= used_atom
[i
];
509 struct atom_value
*v
= &val
[i
];
510 if (!!deref
!= (*name
== '*'))
514 if (strcmp(name
, "subject") &&
515 strcmp(name
, "body") &&
516 strcmp(name
, "contents") &&
517 strcmp(name
, "contents:subject") &&
518 strcmp(name
, "contents:body") &&
519 strcmp(name
, "contents:signature"))
524 &bodypos
, &bodylen
, &nonsiglen
,
527 if (!strcmp(name
, "subject"))
528 v
->s
= copy_subject(subpos
, sublen
);
529 else if (!strcmp(name
, "contents:subject"))
530 v
->s
= copy_subject(subpos
, sublen
);
531 else if (!strcmp(name
, "body"))
532 v
->s
= xmemdupz(bodypos
, bodylen
);
533 else if (!strcmp(name
, "contents:body"))
534 v
->s
= xmemdupz(bodypos
, nonsiglen
);
535 else if (!strcmp(name
, "contents:signature"))
536 v
->s
= xmemdupz(sigpos
, siglen
);
537 else if (!strcmp(name
, "contents"))
538 v
->s
= xstrdup(subpos
);
543 * We want to have empty print-string for field requests
544 * that do not apply (e.g. "authordate" for a tag object)
546 static void fill_missing_values(struct atom_value
*val
)
549 for (i
= 0; i
< used_atom_cnt
; i
++) {
550 struct atom_value
*v
= &val
[i
];
557 * val is a list of atom_value to hold returned values. Extract
558 * the values for atoms in used_atom array out of (obj, buf, sz).
559 * when deref is false, (obj, buf, sz) is the object that is
560 * pointed at by the ref itself; otherwise it is the object the
561 * ref (which is a tag) refers to.
563 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
565 grab_common_values(val
, deref
, obj
, buf
, sz
);
568 grab_tag_values(val
, deref
, obj
, buf
, sz
);
569 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
570 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
573 grab_commit_values(val
, deref
, obj
, buf
, sz
);
574 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
575 grab_person("author", val
, deref
, obj
, buf
, sz
);
576 grab_person("committer", val
, deref
, obj
, buf
, sz
);
579 /* grab_tree_values(val, deref, obj, buf, sz); */
582 /* grab_blob_values(val, deref, obj, buf, sz); */
585 die("Eh? Object of type %d?", obj
->type
);
589 static inline char *copy_advance(char *dst
, const char *src
)
597 * Parse the object referred by ref, and grab needed value.
599 static void populate_value(struct ref_array_item
*ref
)
605 const unsigned char *tagged
;
607 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
609 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
610 unsigned char unused1
[20];
611 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
617 /* Fill in specials first */
618 for (i
= 0; i
< used_atom_cnt
; i
++) {
619 const char *name
= used_atom
[i
];
620 struct atom_value
*v
= &ref
->value
[i
];
624 struct branch
*branch
= NULL
;
631 if (starts_with(name
, "refname"))
632 refname
= ref
->refname
;
633 else if (starts_with(name
, "symref"))
634 refname
= ref
->symref
? ref
->symref
: "";
635 else if (starts_with(name
, "upstream")) {
636 const char *branch_name
;
637 /* only local branches may have an upstream */
638 if (!skip_prefix(ref
->refname
, "refs/heads/",
641 branch
= branch_get(branch_name
);
643 refname
= branch_get_upstream(branch
, NULL
);
646 } else if (starts_with(name
, "push")) {
647 const char *branch_name
;
648 if (!skip_prefix(ref
->refname
, "refs/heads/",
651 branch
= branch_get(branch_name
);
653 refname
= branch_get_push(branch
, NULL
);
656 } else if (starts_with(name
, "color:")) {
657 char color
[COLOR_MAXLEN
] = "";
659 if (color_parse(name
+ 6, color
) < 0)
660 die(_("unable to parse format"));
661 v
->s
= xstrdup(color
);
663 } else if (!strcmp(name
, "flag")) {
664 char buf
[256], *cp
= buf
;
665 if (ref
->flag
& REF_ISSYMREF
)
666 cp
= copy_advance(cp
, ",symref");
667 if (ref
->flag
& REF_ISPACKED
)
668 cp
= copy_advance(cp
, ",packed");
673 v
->s
= xstrdup(buf
+ 1);
676 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
)) {
678 } else if (!strcmp(name
, "HEAD")) {
680 unsigned char sha1
[20];
682 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
684 if (!strcmp(ref
->refname
, head
))
692 formatp
= strchr(name
, ':');
694 int num_ours
, num_theirs
;
697 if (!strcmp(formatp
, "short"))
698 refname
= shorten_unambiguous_ref(refname
,
699 warn_ambiguous_refs
);
700 else if (!strcmp(formatp
, "track") &&
701 (starts_with(name
, "upstream") ||
702 starts_with(name
, "push"))) {
705 if (stat_tracking_info(branch
, &num_ours
,
709 if (!num_ours
&& !num_theirs
)
711 else if (!num_ours
) {
712 sprintf(buf
, "[behind %d]", num_theirs
);
714 } else if (!num_theirs
) {
715 sprintf(buf
, "[ahead %d]", num_ours
);
718 sprintf(buf
, "[ahead %d, behind %d]",
719 num_ours
, num_theirs
);
723 } else if (!strcmp(formatp
, "trackshort") &&
724 (starts_with(name
, "upstream") ||
725 starts_with(name
, "push"))) {
728 if (stat_tracking_info(branch
, &num_ours
,
732 if (!num_ours
&& !num_theirs
)
736 else if (!num_theirs
)
742 die("unknown %.*s format %s",
743 (int)(formatp
- name
), name
, formatp
);
749 int len
= strlen(refname
);
750 char *s
= xmalloc(len
+ 4);
751 sprintf(s
, "%s^{}", refname
);
756 for (i
= 0; i
< used_atom_cnt
; i
++) {
757 struct atom_value
*v
= &ref
->value
[i
];
764 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
766 die("missing object %s for %s",
767 sha1_to_hex(ref
->objectname
), ref
->refname
);
769 die("parse_object_buffer failed on %s for %s",
770 sha1_to_hex(ref
->objectname
), ref
->refname
);
772 grab_values(ref
->value
, 0, obj
, buf
, size
);
777 * If there is no atom that wants to know about tagged
778 * object, we are done.
780 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
784 * If it is a tag object, see if we use a value that derefs
785 * the object, and if we do grab the object it refers to.
787 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
790 * NEEDSWORK: This derefs tag only once, which
791 * is good to deal with chains of trust, but
792 * is not consistent with what deref_tag() does
793 * which peels the onion to the core.
795 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
797 die("missing object %s for %s",
798 sha1_to_hex(tagged
), ref
->refname
);
800 die("parse_object_buffer failed on %s for %s",
801 sha1_to_hex(tagged
), ref
->refname
);
802 grab_values(ref
->value
, 1, obj
, buf
, size
);
808 * Given a ref, return the value for the atom. This lazily gets value
809 * out of the object by calling populate value.
811 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
815 fill_missing_values(ref
->value
);
817 *v
= &ref
->value
[atom
];
821 * Return 1 if the refname matches one of the patterns, otherwise 0.
822 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
823 * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
824 * matches "refs/heads/m*",too).
826 static int match_name_as_path(const char **pattern
, const char *refname
)
828 int namelen
= strlen(refname
);
829 for (; *pattern
; pattern
++) {
830 const char *p
= *pattern
;
831 int plen
= strlen(p
);
833 if ((plen
<= namelen
) &&
834 !strncmp(refname
, p
, plen
) &&
835 (refname
[plen
] == '\0' ||
836 refname
[plen
] == '/' ||
839 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
845 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
846 static struct ref_array_item
*new_ref_array_item(const char *refname
,
847 const unsigned char *objectname
,
850 size_t len
= strlen(refname
);
851 struct ref_array_item
*ref
= xcalloc(1, sizeof(struct ref_array_item
) + len
+ 1);
852 memcpy(ref
->refname
, refname
, len
);
853 ref
->refname
[len
] = '\0';
854 hashcpy(ref
->objectname
, objectname
);
861 * A call-back given to for_each_ref(). Filter refs and keep them for
862 * later object processing.
864 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
866 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
867 struct ref_filter
*filter
= ref_cbdata
->filter
;
868 struct ref_array_item
*ref
;
870 if (flag
& REF_BAD_NAME
) {
871 warning("ignoring ref with broken name %s", refname
);
875 if (flag
& REF_ISBROKEN
) {
876 warning("ignoring broken ref %s", refname
);
880 if (*filter
->name_patterns
&& !match_name_as_path(filter
->name_patterns
, refname
))
884 * We do not open the object yet; sort may only need refname
885 * to do its job and the resulting list may yet to be pruned
888 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
890 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
891 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
895 /* Free memory allocated for a ref_array_item */
896 static void free_array_item(struct ref_array_item
*item
)
898 free((char *)item
->symref
);
902 /* Free all memory allocated for ref_array */
903 void ref_array_clear(struct ref_array
*array
)
907 for (i
= 0; i
< array
->nr
; i
++)
908 free_array_item(array
->items
[i
]);
911 array
->nr
= array
->alloc
= 0;
915 * API for filtering a set of refs. Based on the type of refs the user
916 * has requested, we iterate through those refs and apply filters
917 * as per the given ref_filter structure and finally store the
918 * filtered refs in the ref_array structure.
920 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
922 struct ref_filter_cbdata ref_cbdata
;
924 ref_cbdata
.array
= array
;
925 ref_cbdata
.filter
= filter
;
927 if (type
& (FILTER_REFS_ALL
| FILTER_REFS_INCLUDE_BROKEN
))
928 return for_each_rawref(ref_filter_handler
, &ref_cbdata
);
929 else if (type
& FILTER_REFS_ALL
)
930 return for_each_ref(ref_filter_handler
, &ref_cbdata
);
932 die("filter_refs: invalid type");
936 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
938 struct atom_value
*va
, *vb
;
940 cmp_type cmp_type
= used_atom_type
[s
->atom
];
942 get_ref_atom_value(a
, s
->atom
, &va
);
943 get_ref_atom_value(b
, s
->atom
, &vb
);
946 cmp
= strcmp(va
->s
, vb
->s
);
951 else if (va
->ul
== vb
->ul
)
957 return (s
->reverse
) ? -cmp
: cmp
;
960 static struct ref_sorting
*ref_sorting
;
961 static int compare_refs(const void *a_
, const void *b_
)
963 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
964 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
965 struct ref_sorting
*s
;
967 for (s
= ref_sorting
; s
; s
= s
->next
) {
968 int cmp
= cmp_ref_sorting(s
, a
, b
);
975 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
977 ref_sorting
= sorting
;
978 qsort(array
->items
, array
->nr
, sizeof(struct ref_array_item
*), compare_refs
);
981 static void print_value(struct atom_value
*v
, int quote_style
)
983 struct strbuf sb
= STRBUF_INIT
;
984 switch (quote_style
) {
989 sq_quote_buf(&sb
, v
->s
);
992 perl_quote_buf(&sb
, v
->s
);
995 python_quote_buf(&sb
, v
->s
);
998 tcl_quote_buf(&sb
, v
->s
);
1001 if (quote_style
!= QUOTE_NONE
) {
1002 fputs(sb
.buf
, stdout
);
1003 strbuf_release(&sb
);
1007 static int hex1(char ch
)
1009 if ('0' <= ch
&& ch
<= '9')
1011 else if ('a' <= ch
&& ch
<= 'f')
1012 return ch
- 'a' + 10;
1013 else if ('A' <= ch
&& ch
<= 'F')
1014 return ch
- 'A' + 10;
1017 static int hex2(const char *cp
)
1020 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
1025 static void emit(const char *cp
, const char *ep
)
1027 while (*cp
&& (!ep
|| cp
< ep
)) {
1032 int ch
= hex2(cp
+ 1);
1045 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
1047 const char *cp
, *sp
, *ep
;
1049 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1050 struct atom_value
*atomv
;
1052 ep
= strchr(sp
, ')');
1055 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
1056 print_value(atomv
, quote_style
);
1059 sp
= cp
+ strlen(cp
);
1062 if (need_color_reset_at_eol
) {
1063 struct atom_value resetv
;
1064 char color
[COLOR_MAXLEN
] = "";
1066 if (color_parse("reset", color
) < 0)
1067 die("BUG: couldn't parse 'reset' as a color");
1069 print_value(&resetv
, quote_style
);
1074 /* If no sorting option is given, use refname to sort as default */
1075 struct ref_sorting
*ref_default_sorting(void)
1077 static const char cstr_name
[] = "refname";
1079 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
1081 sorting
->next
= NULL
;
1082 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1086 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
1088 struct ref_sorting
**sorting_tail
= opt
->value
;
1089 struct ref_sorting
*s
;
1092 if (!arg
) /* should --no-sort void the list ? */
1095 s
= xcalloc(1, sizeof(*s
));
1096 s
->next
= *sorting_tail
;
1104 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);