10 #include "parse-options.h"
18 #define QUOTE_PYTHON 4
21 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
25 unsigned long ul
; /* used for sorting when not FIELD_STR */
29 struct ref_sort
*next
;
30 int atom
; /* index into used_atom array */
36 unsigned char objectname
[20];
39 struct atom_value
*value
;
48 { "objectsize", FIELD_ULONG
},
52 { "numparent", FIELD_ULONG
},
59 { "authordate", FIELD_TIME
},
63 { "committerdate", FIELD_TIME
},
67 { "taggerdate", FIELD_TIME
},
69 { "creatordate", FIELD_TIME
},
73 { "contents:subject" },
75 { "contents:signature" },
84 * An atom is a valid field atom listed above, possibly prefixed with
85 * a "*" to denote deref_tag().
87 * We parse given format string and sort specifiers, and make a list
88 * of properties that we need to extract out of objects. refinfo
89 * structure will hold an array of values extracted that can be
90 * indexed with the "atom number", which is an index into this
93 static const char **used_atom
;
94 static cmp_type
*used_atom_type
;
95 static int used_atom_cnt
, need_tagged
, need_symref
;
96 static int need_color_reset_at_eol
;
99 * Used to parse format string and sort specifiers
101 static int parse_atom(const char *atom
, const char *ep
)
107 if (*sp
== '*' && sp
< ep
)
110 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
112 /* Do we have the atom already used elsewhere? */
113 for (i
= 0; i
< used_atom_cnt
; i
++) {
114 int len
= strlen(used_atom
[i
]);
115 if (len
== ep
- atom
&& !memcmp(used_atom
[i
], atom
, len
))
119 /* Is the atom a valid one? */
120 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
121 int len
= strlen(valid_atom
[i
].name
);
123 * If the atom name has a colon, strip it and everything after
124 * it off - it specifies the format for this entry, and
125 * shouldn't be used for checking against the valid_atom
128 const char *formatp
= strchr(sp
, ':');
129 if (!formatp
|| ep
< formatp
)
131 if (len
== formatp
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
135 if (ARRAY_SIZE(valid_atom
) <= i
)
136 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
138 /* Add it in, including the deref prefix */
141 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
142 REALLOC_ARRAY(used_atom_type
, used_atom_cnt
);
143 used_atom
[at
] = xmemdupz(atom
, ep
- atom
);
144 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
147 if (!strcmp(used_atom
[at
], "symref"))
153 * In a format string, find the next occurrence of %(atom).
155 static const char *find_next(const char *cp
)
160 * %( is the start of an atom;
161 * %% is a quoted per-cent.
165 else if (cp
[1] == '%')
166 cp
++; /* skip over two % */
167 /* otherwise this is a singleton, literal % */
175 * Make sure the format string is well formed, and parse out
178 static int verify_format(const char *format
)
182 need_color_reset_at_eol
= 0;
183 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
184 const char *color
, *ep
= strchr(sp
, ')');
188 return error("malformed format string %s", sp
);
189 /* sp points at "%(" and ep points at the closing ")" */
190 at
= parse_atom(sp
+ 2, ep
);
193 if (skip_prefix(used_atom
[at
], "color:", &color
))
194 need_color_reset_at_eol
= !!strcmp(color
, "reset");
200 * Given an object name, read the object data and size, and return a
201 * "struct object". If the object data we are returning is also borrowed
202 * by the "struct object" representation, set *eaten as well---it is a
203 * signal from parse_object_buffer to us not to free the buffer.
205 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
207 enum object_type type
;
208 void *buf
= read_sha1_file(sha1
, &type
, sz
);
211 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
217 static int grab_objectname(const char *name
, const unsigned char *sha1
,
218 struct atom_value
*v
)
220 if (!strcmp(name
, "objectname")) {
221 char *s
= xmalloc(41);
222 strcpy(s
, sha1_to_hex(sha1
));
226 if (!strcmp(name
, "objectname:short")) {
227 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
233 /* See grab_values */
234 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
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
, "objecttype"))
246 v
->s
= typename(obj
->type
);
247 else if (!strcmp(name
, "objectsize")) {
248 char *s
= xmalloc(40);
249 sprintf(s
, "%lu", sz
);
254 grab_objectname(name
, obj
->sha1
, v
);
258 /* See grab_values */
259 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
262 struct tag
*tag
= (struct tag
*) 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
, "tag"))
273 else if (!strcmp(name
, "type") && tag
->tagged
)
274 v
->s
= typename(tag
->tagged
->type
);
275 else if (!strcmp(name
, "object") && tag
->tagged
) {
276 char *s
= xmalloc(41);
277 strcpy(s
, sha1_to_hex(tag
->tagged
->sha1
));
283 /* See grab_values */
284 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
287 struct commit
*commit
= (struct commit
*) obj
;
289 for (i
= 0; i
< used_atom_cnt
; i
++) {
290 const char *name
= used_atom
[i
];
291 struct atom_value
*v
= &val
[i
];
292 if (!!deref
!= (*name
== '*'))
296 if (!strcmp(name
, "tree")) {
297 char *s
= xmalloc(41);
298 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
301 if (!strcmp(name
, "numparent")) {
302 char *s
= xmalloc(40);
303 v
->ul
= commit_list_count(commit
->parents
);
304 sprintf(s
, "%lu", v
->ul
);
307 else if (!strcmp(name
, "parent")) {
308 int num
= commit_list_count(commit
->parents
);
310 struct commit_list
*parents
;
311 char *s
= xmalloc(41 * num
+ 1);
313 for (i
= 0, parents
= commit
->parents
;
315 parents
= parents
->next
, i
= i
+ 41) {
316 struct commit
*parent
= parents
->item
;
317 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
327 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
331 if (!strncmp(buf
, who
, wholen
) &&
333 return buf
+ wholen
+ 1;
334 eol
= strchr(buf
, '\n');
339 return ""; /* end of header */
345 static const char *copy_line(const char *buf
)
347 const char *eol
= strchrnul(buf
, '\n');
348 return xmemdupz(buf
, eol
- buf
);
351 static const char *copy_name(const char *buf
)
354 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
355 if (!strncmp(cp
, " <", 2))
356 return xmemdupz(buf
, cp
- buf
);
361 static const char *copy_email(const char *buf
)
363 const char *email
= strchr(buf
, '<');
367 eoemail
= strchr(email
, '>');
370 return xmemdupz(email
, eoemail
+ 1 - email
);
373 static char *copy_subject(const char *buf
, unsigned long len
)
375 char *r
= xmemdupz(buf
, len
);
378 for (i
= 0; i
< len
; i
++)
385 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
387 const char *eoemail
= strstr(buf
, "> ");
389 unsigned long timestamp
;
391 enum date_mode date_mode
= DATE_NORMAL
;
395 * We got here because atomname ends in "date" or "date<something>";
396 * it's not possible that <something> is not ":<format>" because
397 * parse_atom() wouldn't have allowed it, so we can assume that no
398 * ":" means no format is specified, and use the default.
400 formatp
= strchr(atomname
, ':');
401 if (formatp
!= NULL
) {
403 date_mode
= parse_date_format(formatp
);
408 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
409 if (timestamp
== ULONG_MAX
)
411 tz
= strtol(zone
, NULL
, 10);
412 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
414 v
->s
= xstrdup(show_date(timestamp
, tz
, date_mode
));
422 /* See grab_values */
423 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
426 int wholen
= strlen(who
);
427 const char *wholine
= NULL
;
429 for (i
= 0; i
< used_atom_cnt
; i
++) {
430 const char *name
= used_atom
[i
];
431 struct atom_value
*v
= &val
[i
];
432 if (!!deref
!= (*name
== '*'))
436 if (strncmp(who
, name
, wholen
))
438 if (name
[wholen
] != 0 &&
439 strcmp(name
+ wholen
, "name") &&
440 strcmp(name
+ wholen
, "email") &&
441 !starts_with(name
+ wholen
, "date"))
444 wholine
= find_wholine(who
, wholen
, buf
, sz
);
446 return; /* no point looking for it */
447 if (name
[wholen
] == 0)
448 v
->s
= copy_line(wholine
);
449 else if (!strcmp(name
+ wholen
, "name"))
450 v
->s
= copy_name(wholine
);
451 else if (!strcmp(name
+ wholen
, "email"))
452 v
->s
= copy_email(wholine
);
453 else if (starts_with(name
+ wholen
, "date"))
454 grab_date(wholine
, v
, name
);
458 * For a tag or a commit object, if "creator" or "creatordate" is
459 * requested, do something special.
461 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
462 return; /* "author" for commit object is not wanted */
464 wholine
= find_wholine(who
, wholen
, buf
, sz
);
467 for (i
= 0; i
< used_atom_cnt
; i
++) {
468 const char *name
= used_atom
[i
];
469 struct atom_value
*v
= &val
[i
];
470 if (!!deref
!= (*name
== '*'))
475 if (starts_with(name
, "creatordate"))
476 grab_date(wholine
, v
, name
);
477 else if (!strcmp(name
, "creator"))
478 v
->s
= copy_line(wholine
);
482 static void find_subpos(const char *buf
, unsigned long sz
,
483 const char **sub
, unsigned long *sublen
,
484 const char **body
, unsigned long *bodylen
,
485 unsigned long *nonsiglen
,
486 const char **sig
, unsigned long *siglen
)
489 /* skip past header until we hit empty line */
490 while (*buf
&& *buf
!= '\n') {
491 eol
= strchrnul(buf
, '\n');
496 /* skip any empty lines */
500 /* parse signature first; we might not even have a subject line */
501 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
502 *siglen
= strlen(*sig
);
504 /* subject is first non-empty line */
506 /* subject goes to first empty line */
507 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
508 eol
= strchrnul(buf
, '\n');
513 *sublen
= buf
- *sub
;
514 /* drop trailing newline, if present */
515 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
518 /* skip any empty lines */
522 *bodylen
= strlen(buf
);
523 *nonsiglen
= *sig
- buf
;
526 /* See grab_values */
527 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
530 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
531 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
533 for (i
= 0; i
< used_atom_cnt
; i
++) {
534 const char *name
= used_atom
[i
];
535 struct atom_value
*v
= &val
[i
];
536 if (!!deref
!= (*name
== '*'))
540 if (strcmp(name
, "subject") &&
541 strcmp(name
, "body") &&
542 strcmp(name
, "contents") &&
543 strcmp(name
, "contents:subject") &&
544 strcmp(name
, "contents:body") &&
545 strcmp(name
, "contents:signature"))
550 &bodypos
, &bodylen
, &nonsiglen
,
553 if (!strcmp(name
, "subject"))
554 v
->s
= copy_subject(subpos
, sublen
);
555 else if (!strcmp(name
, "contents:subject"))
556 v
->s
= copy_subject(subpos
, sublen
);
557 else if (!strcmp(name
, "body"))
558 v
->s
= xmemdupz(bodypos
, bodylen
);
559 else if (!strcmp(name
, "contents:body"))
560 v
->s
= xmemdupz(bodypos
, nonsiglen
);
561 else if (!strcmp(name
, "contents:signature"))
562 v
->s
= xmemdupz(sigpos
, siglen
);
563 else if (!strcmp(name
, "contents"))
564 v
->s
= xstrdup(subpos
);
569 * We want to have empty print-string for field requests
570 * that do not apply (e.g. "authordate" for a tag object)
572 static void fill_missing_values(struct atom_value
*val
)
575 for (i
= 0; i
< used_atom_cnt
; i
++) {
576 struct atom_value
*v
= &val
[i
];
583 * val is a list of atom_value to hold returned values. Extract
584 * the values for atoms in used_atom array out of (obj, buf, sz).
585 * when deref is false, (obj, buf, sz) is the object that is
586 * pointed at by the ref itself; otherwise it is the object the
587 * ref (which is a tag) refers to.
589 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
591 grab_common_values(val
, deref
, obj
, buf
, sz
);
594 grab_tag_values(val
, deref
, obj
, buf
, sz
);
595 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
596 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
599 grab_commit_values(val
, deref
, obj
, buf
, sz
);
600 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
601 grab_person("author", val
, deref
, obj
, buf
, sz
);
602 grab_person("committer", val
, deref
, obj
, buf
, sz
);
605 /* grab_tree_values(val, deref, obj, buf, sz); */
608 /* grab_blob_values(val, deref, obj, buf, sz); */
611 die("Eh? Object of type %d?", obj
->type
);
615 static inline char *copy_advance(char *dst
, const char *src
)
623 * Parse the object referred by ref, and grab needed value.
625 static void populate_value(struct refinfo
*ref
)
631 const unsigned char *tagged
;
633 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
635 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
636 unsigned char unused1
[20];
637 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
643 /* Fill in specials first */
644 for (i
= 0; i
< used_atom_cnt
; i
++) {
645 const char *name
= used_atom
[i
];
646 struct atom_value
*v
= &ref
->value
[i
];
650 struct branch
*branch
= NULL
;
657 if (starts_with(name
, "refname"))
658 refname
= ref
->refname
;
659 else if (starts_with(name
, "symref"))
660 refname
= ref
->symref
? ref
->symref
: "";
661 else if (starts_with(name
, "upstream")) {
662 /* only local branches may have an upstream */
663 if (!starts_with(ref
->refname
, "refs/heads/"))
665 branch
= branch_get(ref
->refname
+ 11);
667 if (!branch
|| !branch
->merge
|| !branch
->merge
[0] ||
668 !branch
->merge
[0]->dst
)
670 refname
= branch
->merge
[0]->dst
;
671 } else if (starts_with(name
, "color:")) {
672 char color
[COLOR_MAXLEN
] = "";
674 if (color_parse(name
+ 6, color
) < 0)
675 die(_("unable to parse format"));
676 v
->s
= xstrdup(color
);
678 } else if (!strcmp(name
, "flag")) {
679 char buf
[256], *cp
= buf
;
680 if (ref
->flag
& REF_ISSYMREF
)
681 cp
= copy_advance(cp
, ",symref");
682 if (ref
->flag
& REF_ISPACKED
)
683 cp
= copy_advance(cp
, ",packed");
688 v
->s
= xstrdup(buf
+ 1);
691 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
)) {
693 } else if (!strcmp(name
, "HEAD")) {
695 unsigned char sha1
[20];
697 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
699 if (!strcmp(ref
->refname
, head
))
707 formatp
= strchr(name
, ':');
709 int num_ours
, num_theirs
;
712 if (!strcmp(formatp
, "short"))
713 refname
= shorten_unambiguous_ref(refname
,
714 warn_ambiguous_refs
);
715 else if (!strcmp(formatp
, "track") &&
716 starts_with(name
, "upstream")) {
719 if (stat_tracking_info(branch
, &num_ours
,
723 if (!num_ours
&& !num_theirs
)
725 else if (!num_ours
) {
726 sprintf(buf
, "[behind %d]", num_theirs
);
728 } else if (!num_theirs
) {
729 sprintf(buf
, "[ahead %d]", num_ours
);
732 sprintf(buf
, "[ahead %d, behind %d]",
733 num_ours
, num_theirs
);
737 } else if (!strcmp(formatp
, "trackshort") &&
738 starts_with(name
, "upstream")) {
741 if (stat_tracking_info(branch
, &num_ours
,
745 if (!num_ours
&& !num_theirs
)
749 else if (!num_theirs
)
755 die("unknown %.*s format %s",
756 (int)(formatp
- name
), name
, formatp
);
762 int len
= strlen(refname
);
763 char *s
= xmalloc(len
+ 4);
764 sprintf(s
, "%s^{}", refname
);
769 for (i
= 0; i
< used_atom_cnt
; i
++) {
770 struct atom_value
*v
= &ref
->value
[i
];
777 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
779 die("missing object %s for %s",
780 sha1_to_hex(ref
->objectname
), ref
->refname
);
782 die("parse_object_buffer failed on %s for %s",
783 sha1_to_hex(ref
->objectname
), ref
->refname
);
785 grab_values(ref
->value
, 0, obj
, buf
, size
);
790 * If there is no atom that wants to know about tagged
791 * object, we are done.
793 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
797 * If it is a tag object, see if we use a value that derefs
798 * the object, and if we do grab the object it refers to.
800 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
803 * NEEDSWORK: This derefs tag only once, which
804 * is good to deal with chains of trust, but
805 * is not consistent with what deref_tag() does
806 * which peels the onion to the core.
808 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
810 die("missing object %s for %s",
811 sha1_to_hex(tagged
), ref
->refname
);
813 die("parse_object_buffer failed on %s for %s",
814 sha1_to_hex(tagged
), ref
->refname
);
815 grab_values(ref
->value
, 1, obj
, buf
, size
);
821 * Given a ref, return the value for the atom. This lazily gets value
822 * out of the object by calling populate value.
824 static void get_value(struct refinfo
*ref
, int atom
, struct atom_value
**v
)
828 fill_missing_values(ref
->value
);
830 *v
= &ref
->value
[atom
];
833 struct grab_ref_cbdata
{
834 struct refinfo
**grab_array
;
835 const char **grab_pattern
;
840 * A call-back given to for_each_ref(). Filter refs and keep them for
841 * later object processing.
843 static int grab_single_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
845 struct grab_ref_cbdata
*cb
= cb_data
;
849 if (flag
& REF_BAD_NAME
) {
850 warning("ignoring ref with broken name %s", refname
);
854 if (*cb
->grab_pattern
) {
855 const char **pattern
;
856 int namelen
= strlen(refname
);
857 for (pattern
= cb
->grab_pattern
; *pattern
; pattern
++) {
858 const char *p
= *pattern
;
859 int plen
= strlen(p
);
861 if ((plen
<= namelen
) &&
862 !strncmp(refname
, p
, plen
) &&
863 (refname
[plen
] == '\0' ||
864 refname
[plen
] == '/' ||
867 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
875 * We do not open the object yet; sort may only need refname
876 * to do its job and the resulting list may yet to be pruned
879 ref
= xcalloc(1, sizeof(*ref
));
880 ref
->refname
= xstrdup(refname
);
881 hashcpy(ref
->objectname
, sha1
);
885 REALLOC_ARRAY(cb
->grab_array
, cnt
+ 1);
886 cb
->grab_array
[cnt
++] = ref
;
891 static int cmp_ref_sort(struct ref_sort
*s
, struct refinfo
*a
, struct refinfo
*b
)
893 struct atom_value
*va
, *vb
;
895 cmp_type cmp_type
= used_atom_type
[s
->atom
];
897 get_value(a
, s
->atom
, &va
);
898 get_value(b
, s
->atom
, &vb
);
901 cmp
= strcmp(va
->s
, vb
->s
);
906 else if (va
->ul
== vb
->ul
)
912 return (s
->reverse
) ? -cmp
: cmp
;
915 static struct ref_sort
*ref_sort
;
916 static int compare_refs(const void *a_
, const void *b_
)
918 struct refinfo
*a
= *((struct refinfo
**)a_
);
919 struct refinfo
*b
= *((struct refinfo
**)b_
);
922 for (s
= ref_sort
; s
; s
= s
->next
) {
923 int cmp
= cmp_ref_sort(s
, a
, b
);
930 static void sort_refs(struct ref_sort
*sort
, struct refinfo
**refs
, int num_refs
)
933 qsort(refs
, num_refs
, sizeof(struct refinfo
*), compare_refs
);
936 static void print_value(struct atom_value
*v
, int quote_style
)
938 struct strbuf sb
= STRBUF_INIT
;
939 switch (quote_style
) {
944 sq_quote_buf(&sb
, v
->s
);
947 perl_quote_buf(&sb
, v
->s
);
950 python_quote_buf(&sb
, v
->s
);
953 tcl_quote_buf(&sb
, v
->s
);
956 if (quote_style
!= QUOTE_NONE
) {
957 fputs(sb
.buf
, stdout
);
962 static int hex1(char ch
)
964 if ('0' <= ch
&& ch
<= '9')
966 else if ('a' <= ch
&& ch
<= 'f')
967 return ch
- 'a' + 10;
968 else if ('A' <= ch
&& ch
<= 'F')
969 return ch
- 'A' + 10;
972 static int hex2(const char *cp
)
975 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
980 static void emit(const char *cp
, const char *ep
)
982 while (*cp
&& (!ep
|| cp
< ep
)) {
987 int ch
= hex2(cp
+ 1);
1000 static void show_ref(struct refinfo
*info
, const char *format
, int quote_style
)
1002 const char *cp
, *sp
, *ep
;
1004 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1005 struct atom_value
*atomv
;
1007 ep
= strchr(sp
, ')');
1010 get_value(info
, parse_atom(sp
+ 2, ep
), &atomv
);
1011 print_value(atomv
, quote_style
);
1014 sp
= cp
+ strlen(cp
);
1017 if (need_color_reset_at_eol
) {
1018 struct atom_value resetv
;
1019 char color
[COLOR_MAXLEN
] = "";
1021 if (color_parse("reset", color
) < 0)
1022 die("BUG: couldn't parse 'reset' as a color");
1024 print_value(&resetv
, quote_style
);
1029 static struct ref_sort
*default_sort(void)
1031 static const char cstr_name
[] = "refname";
1033 struct ref_sort
*sort
= xcalloc(1, sizeof(*sort
));
1036 sort
->atom
= parse_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1040 static int opt_parse_sort(const struct option
*opt
, const char *arg
, int unset
)
1042 struct ref_sort
**sort_tail
= opt
->value
;
1046 if (!arg
) /* should --no-sort void the list ? */
1049 s
= xcalloc(1, sizeof(*s
));
1050 s
->next
= *sort_tail
;
1058 s
->atom
= parse_atom(arg
, arg
+len
);
1062 static char const * const for_each_ref_usage
[] = {
1063 N_("git for-each-ref [options] [<pattern>]"),
1067 int cmd_for_each_ref(int argc
, const char **argv
, const char *prefix
)
1070 const char *format
= "%(objectname) %(objecttype)\t%(refname)";
1071 struct ref_sort
*sort
= NULL
, **sort_tail
= &sort
;
1072 int maxcount
= 0, quote_style
= 0;
1073 struct refinfo
**refs
;
1074 struct grab_ref_cbdata cbdata
;
1076 struct option opts
[] = {
1077 OPT_BIT('s', "shell", "e_style
,
1078 N_("quote placeholders suitably for shells"), QUOTE_SHELL
),
1079 OPT_BIT('p', "perl", "e_style
,
1080 N_("quote placeholders suitably for perl"), QUOTE_PERL
),
1081 OPT_BIT(0 , "python", "e_style
,
1082 N_("quote placeholders suitably for python"), QUOTE_PYTHON
),
1083 OPT_BIT(0 , "tcl", "e_style
,
1084 N_("quote placeholders suitably for Tcl"), QUOTE_TCL
),
1087 OPT_INTEGER( 0 , "count", &maxcount
, N_("show only <n> matched refs")),
1088 OPT_STRING( 0 , "format", &format
, N_("format"), N_("format to use for the output")),
1089 OPT_CALLBACK(0 , "sort", sort_tail
, N_("key"),
1090 N_("field name to sort on"), &opt_parse_sort
),
1094 parse_options(argc
, argv
, prefix
, opts
, for_each_ref_usage
, 0);
1096 error("invalid --count argument: `%d'", maxcount
);
1097 usage_with_options(for_each_ref_usage
, opts
);
1099 if (HAS_MULTI_BITS(quote_style
)) {
1100 error("more than one quoting style?");
1101 usage_with_options(for_each_ref_usage
, opts
);
1103 if (verify_format(format
))
1104 usage_with_options(for_each_ref_usage
, opts
);
1107 sort
= default_sort();
1109 /* for warn_ambiguous_refs */
1110 git_config(git_default_config
, NULL
);
1112 memset(&cbdata
, 0, sizeof(cbdata
));
1113 cbdata
.grab_pattern
= argv
;
1114 for_each_rawref(grab_single_ref
, &cbdata
);
1115 refs
= cbdata
.grab_array
;
1116 num_refs
= cbdata
.grab_cnt
;
1118 sort_refs(sort
, refs
, num_refs
);
1120 if (!maxcount
|| num_refs
< maxcount
)
1121 maxcount
= num_refs
;
1122 for (i
= 0; i
< maxcount
; i
++)
1123 show_ref(refs
[i
], format
, quote_style
);