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" },
85 * An atom is a valid field atom listed above, possibly prefixed with
86 * a "*" to denote deref_tag().
88 * We parse given format string and sort specifiers, and make a list
89 * of properties that we need to extract out of objects. refinfo
90 * structure will hold an array of values extracted that can be
91 * indexed with the "atom number", which is an index into this
94 static const char **used_atom
;
95 static cmp_type
*used_atom_type
;
96 static int used_atom_cnt
, need_tagged
, need_symref
;
97 static int need_color_reset_at_eol
;
100 * Used to parse format string and sort specifiers
102 static int parse_atom(const char *atom
, const char *ep
)
108 if (*sp
== '*' && sp
< ep
)
111 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
113 /* Do we have the atom already used elsewhere? */
114 for (i
= 0; i
< used_atom_cnt
; i
++) {
115 int len
= strlen(used_atom
[i
]);
116 if (len
== ep
- atom
&& !memcmp(used_atom
[i
], atom
, len
))
120 /* Is the atom a valid one? */
121 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
122 int len
= strlen(valid_atom
[i
].name
);
124 * If the atom name has a colon, strip it and everything after
125 * it off - it specifies the format for this entry, and
126 * shouldn't be used for checking against the valid_atom
129 const char *formatp
= strchr(sp
, ':');
130 if (!formatp
|| ep
< formatp
)
132 if (len
== formatp
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
136 if (ARRAY_SIZE(valid_atom
) <= i
)
137 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
139 /* Add it in, including the deref prefix */
142 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
143 REALLOC_ARRAY(used_atom_type
, used_atom_cnt
);
144 used_atom
[at
] = xmemdupz(atom
, ep
- atom
);
145 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
148 if (!strcmp(used_atom
[at
], "symref"))
154 * In a format string, find the next occurrence of %(atom).
156 static const char *find_next(const char *cp
)
161 * %( is the start of an atom;
162 * %% is a quoted per-cent.
166 else if (cp
[1] == '%')
167 cp
++; /* skip over two % */
168 /* otherwise this is a singleton, literal % */
176 * Make sure the format string is well formed, and parse out
179 static int verify_format(const char *format
)
183 need_color_reset_at_eol
= 0;
184 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
185 const char *color
, *ep
= strchr(sp
, ')');
189 return error("malformed format string %s", sp
);
190 /* sp points at "%(" and ep points at the closing ")" */
191 at
= parse_atom(sp
+ 2, ep
);
194 if (skip_prefix(used_atom
[at
], "color:", &color
))
195 need_color_reset_at_eol
= !!strcmp(color
, "reset");
201 * Given an object name, read the object data and size, and return a
202 * "struct object". If the object data we are returning is also borrowed
203 * by the "struct object" representation, set *eaten as well---it is a
204 * signal from parse_object_buffer to us not to free the buffer.
206 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
208 enum object_type type
;
209 void *buf
= read_sha1_file(sha1
, &type
, sz
);
212 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
218 static int grab_objectname(const char *name
, const unsigned char *sha1
,
219 struct atom_value
*v
)
221 if (!strcmp(name
, "objectname")) {
222 char *s
= xmalloc(41);
223 strcpy(s
, sha1_to_hex(sha1
));
227 if (!strcmp(name
, "objectname:short")) {
228 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
234 /* See grab_values */
235 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
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
, "objecttype"))
247 v
->s
= typename(obj
->type
);
248 else if (!strcmp(name
, "objectsize")) {
249 char *s
= xmalloc(40);
250 sprintf(s
, "%lu", sz
);
255 grab_objectname(name
, obj
->sha1
, v
);
259 /* See grab_values */
260 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
263 struct tag
*tag
= (struct tag
*) obj
;
265 for (i
= 0; i
< used_atom_cnt
; i
++) {
266 const char *name
= used_atom
[i
];
267 struct atom_value
*v
= &val
[i
];
268 if (!!deref
!= (*name
== '*'))
272 if (!strcmp(name
, "tag"))
274 else if (!strcmp(name
, "type") && tag
->tagged
)
275 v
->s
= typename(tag
->tagged
->type
);
276 else if (!strcmp(name
, "object") && tag
->tagged
) {
277 char *s
= xmalloc(41);
278 strcpy(s
, sha1_to_hex(tag
->tagged
->sha1
));
284 /* See grab_values */
285 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
288 struct commit
*commit
= (struct commit
*) obj
;
290 for (i
= 0; i
< used_atom_cnt
; i
++) {
291 const char *name
= used_atom
[i
];
292 struct atom_value
*v
= &val
[i
];
293 if (!!deref
!= (*name
== '*'))
297 if (!strcmp(name
, "tree")) {
298 char *s
= xmalloc(41);
299 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
302 if (!strcmp(name
, "numparent")) {
303 char *s
= xmalloc(40);
304 v
->ul
= commit_list_count(commit
->parents
);
305 sprintf(s
, "%lu", v
->ul
);
308 else if (!strcmp(name
, "parent")) {
309 int num
= commit_list_count(commit
->parents
);
311 struct commit_list
*parents
;
312 char *s
= xmalloc(41 * num
+ 1);
314 for (i
= 0, parents
= commit
->parents
;
316 parents
= parents
->next
, i
= i
+ 41) {
317 struct commit
*parent
= parents
->item
;
318 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
328 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
332 if (!strncmp(buf
, who
, wholen
) &&
334 return buf
+ wholen
+ 1;
335 eol
= strchr(buf
, '\n');
340 return ""; /* end of header */
346 static const char *copy_line(const char *buf
)
348 const char *eol
= strchrnul(buf
, '\n');
349 return xmemdupz(buf
, eol
- buf
);
352 static const char *copy_name(const char *buf
)
355 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
356 if (!strncmp(cp
, " <", 2))
357 return xmemdupz(buf
, cp
- buf
);
362 static const char *copy_email(const char *buf
)
364 const char *email
= strchr(buf
, '<');
368 eoemail
= strchr(email
, '>');
371 return xmemdupz(email
, eoemail
+ 1 - email
);
374 static char *copy_subject(const char *buf
, unsigned long len
)
376 char *r
= xmemdupz(buf
, len
);
379 for (i
= 0; i
< len
; i
++)
386 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
388 const char *eoemail
= strstr(buf
, "> ");
390 unsigned long timestamp
;
392 enum date_mode date_mode
= DATE_NORMAL
;
396 * We got here because atomname ends in "date" or "date<something>";
397 * it's not possible that <something> is not ":<format>" because
398 * parse_atom() wouldn't have allowed it, so we can assume that no
399 * ":" means no format is specified, and use the default.
401 formatp
= strchr(atomname
, ':');
402 if (formatp
!= NULL
) {
404 date_mode
= parse_date_format(formatp
);
409 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
410 if (timestamp
== ULONG_MAX
)
412 tz
= strtol(zone
, NULL
, 10);
413 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
415 v
->s
= xstrdup(show_date(timestamp
, tz
, date_mode
));
423 /* See grab_values */
424 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
427 int wholen
= strlen(who
);
428 const char *wholine
= NULL
;
430 for (i
= 0; i
< used_atom_cnt
; i
++) {
431 const char *name
= used_atom
[i
];
432 struct atom_value
*v
= &val
[i
];
433 if (!!deref
!= (*name
== '*'))
437 if (strncmp(who
, name
, wholen
))
439 if (name
[wholen
] != 0 &&
440 strcmp(name
+ wholen
, "name") &&
441 strcmp(name
+ wholen
, "email") &&
442 !starts_with(name
+ wholen
, "date"))
445 wholine
= find_wholine(who
, wholen
, buf
, sz
);
447 return; /* no point looking for it */
448 if (name
[wholen
] == 0)
449 v
->s
= copy_line(wholine
);
450 else if (!strcmp(name
+ wholen
, "name"))
451 v
->s
= copy_name(wholine
);
452 else if (!strcmp(name
+ wholen
, "email"))
453 v
->s
= copy_email(wholine
);
454 else if (starts_with(name
+ wholen
, "date"))
455 grab_date(wholine
, v
, name
);
459 * For a tag or a commit object, if "creator" or "creatordate" is
460 * requested, do something special.
462 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
463 return; /* "author" for commit object is not wanted */
465 wholine
= find_wholine(who
, wholen
, buf
, sz
);
468 for (i
= 0; i
< used_atom_cnt
; i
++) {
469 const char *name
= used_atom
[i
];
470 struct atom_value
*v
= &val
[i
];
471 if (!!deref
!= (*name
== '*'))
476 if (starts_with(name
, "creatordate"))
477 grab_date(wholine
, v
, name
);
478 else if (!strcmp(name
, "creator"))
479 v
->s
= copy_line(wholine
);
483 static void find_subpos(const char *buf
, unsigned long sz
,
484 const char **sub
, unsigned long *sublen
,
485 const char **body
, unsigned long *bodylen
,
486 unsigned long *nonsiglen
,
487 const char **sig
, unsigned long *siglen
)
490 /* skip past header until we hit empty line */
491 while (*buf
&& *buf
!= '\n') {
492 eol
= strchrnul(buf
, '\n');
497 /* skip any empty lines */
501 /* parse signature first; we might not even have a subject line */
502 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
503 *siglen
= strlen(*sig
);
505 /* subject is first non-empty line */
507 /* subject goes to first empty line */
508 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
509 eol
= strchrnul(buf
, '\n');
514 *sublen
= buf
- *sub
;
515 /* drop trailing newline, if present */
516 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
519 /* skip any empty lines */
523 *bodylen
= strlen(buf
);
524 *nonsiglen
= *sig
- buf
;
527 /* See grab_values */
528 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
531 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
532 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
534 for (i
= 0; i
< used_atom_cnt
; i
++) {
535 const char *name
= used_atom
[i
];
536 struct atom_value
*v
= &val
[i
];
537 if (!!deref
!= (*name
== '*'))
541 if (strcmp(name
, "subject") &&
542 strcmp(name
, "body") &&
543 strcmp(name
, "contents") &&
544 strcmp(name
, "contents:subject") &&
545 strcmp(name
, "contents:body") &&
546 strcmp(name
, "contents:signature"))
551 &bodypos
, &bodylen
, &nonsiglen
,
554 if (!strcmp(name
, "subject"))
555 v
->s
= copy_subject(subpos
, sublen
);
556 else if (!strcmp(name
, "contents:subject"))
557 v
->s
= copy_subject(subpos
, sublen
);
558 else if (!strcmp(name
, "body"))
559 v
->s
= xmemdupz(bodypos
, bodylen
);
560 else if (!strcmp(name
, "contents:body"))
561 v
->s
= xmemdupz(bodypos
, nonsiglen
);
562 else if (!strcmp(name
, "contents:signature"))
563 v
->s
= xmemdupz(sigpos
, siglen
);
564 else if (!strcmp(name
, "contents"))
565 v
->s
= xstrdup(subpos
);
570 * We want to have empty print-string for field requests
571 * that do not apply (e.g. "authordate" for a tag object)
573 static void fill_missing_values(struct atom_value
*val
)
576 for (i
= 0; i
< used_atom_cnt
; i
++) {
577 struct atom_value
*v
= &val
[i
];
584 * val is a list of atom_value to hold returned values. Extract
585 * the values for atoms in used_atom array out of (obj, buf, sz).
586 * when deref is false, (obj, buf, sz) is the object that is
587 * pointed at by the ref itself; otherwise it is the object the
588 * ref (which is a tag) refers to.
590 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
592 grab_common_values(val
, deref
, obj
, buf
, sz
);
595 grab_tag_values(val
, deref
, obj
, buf
, sz
);
596 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
597 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
600 grab_commit_values(val
, deref
, obj
, buf
, sz
);
601 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
602 grab_person("author", val
, deref
, obj
, buf
, sz
);
603 grab_person("committer", val
, deref
, obj
, buf
, sz
);
606 /* grab_tree_values(val, deref, obj, buf, sz); */
609 /* grab_blob_values(val, deref, obj, buf, sz); */
612 die("Eh? Object of type %d?", obj
->type
);
616 static inline char *copy_advance(char *dst
, const char *src
)
624 * Parse the object referred by ref, and grab needed value.
626 static void populate_value(struct refinfo
*ref
)
632 const unsigned char *tagged
;
634 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
636 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
637 unsigned char unused1
[20];
638 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
644 /* Fill in specials first */
645 for (i
= 0; i
< used_atom_cnt
; i
++) {
646 const char *name
= used_atom
[i
];
647 struct atom_value
*v
= &ref
->value
[i
];
651 struct branch
*branch
= NULL
;
658 if (starts_with(name
, "refname"))
659 refname
= ref
->refname
;
660 else if (starts_with(name
, "symref"))
661 refname
= ref
->symref
? ref
->symref
: "";
662 else if (starts_with(name
, "upstream")) {
663 const char *branch_name
;
664 /* only local branches may have an upstream */
665 if (!skip_prefix(ref
->refname
, "refs/heads/",
668 branch
= branch_get(branch_name
);
670 refname
= branch_get_upstream(branch
, NULL
);
673 } else if (starts_with(name
, "push")) {
674 const char *branch_name
;
675 if (!skip_prefix(ref
->refname
, "refs/heads/",
678 branch
= branch_get(branch_name
);
680 refname
= branch_get_push(branch
, NULL
);
683 } else if (starts_with(name
, "color:")) {
684 char color
[COLOR_MAXLEN
] = "";
686 if (color_parse(name
+ 6, color
) < 0)
687 die(_("unable to parse format"));
688 v
->s
= xstrdup(color
);
690 } else if (!strcmp(name
, "flag")) {
691 char buf
[256], *cp
= buf
;
692 if (ref
->flag
& REF_ISSYMREF
)
693 cp
= copy_advance(cp
, ",symref");
694 if (ref
->flag
& REF_ISPACKED
)
695 cp
= copy_advance(cp
, ",packed");
700 v
->s
= xstrdup(buf
+ 1);
703 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
)) {
705 } else if (!strcmp(name
, "HEAD")) {
707 unsigned char sha1
[20];
709 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
711 if (!strcmp(ref
->refname
, head
))
719 formatp
= strchr(name
, ':');
721 int num_ours
, num_theirs
;
724 if (!strcmp(formatp
, "short"))
725 refname
= shorten_unambiguous_ref(refname
,
726 warn_ambiguous_refs
);
727 else if (!strcmp(formatp
, "track") &&
728 (starts_with(name
, "upstream") ||
729 starts_with(name
, "push"))) {
732 if (stat_tracking_info(branch
, &num_ours
,
736 if (!num_ours
&& !num_theirs
)
738 else if (!num_ours
) {
739 sprintf(buf
, "[behind %d]", num_theirs
);
741 } else if (!num_theirs
) {
742 sprintf(buf
, "[ahead %d]", num_ours
);
745 sprintf(buf
, "[ahead %d, behind %d]",
746 num_ours
, num_theirs
);
750 } else if (!strcmp(formatp
, "trackshort") &&
751 (starts_with(name
, "upstream") ||
752 starts_with(name
, "push"))) {
755 if (stat_tracking_info(branch
, &num_ours
,
759 if (!num_ours
&& !num_theirs
)
763 else if (!num_theirs
)
769 die("unknown %.*s format %s",
770 (int)(formatp
- name
), name
, formatp
);
776 int len
= strlen(refname
);
777 char *s
= xmalloc(len
+ 4);
778 sprintf(s
, "%s^{}", refname
);
783 for (i
= 0; i
< used_atom_cnt
; i
++) {
784 struct atom_value
*v
= &ref
->value
[i
];
791 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
793 die("missing object %s for %s",
794 sha1_to_hex(ref
->objectname
), ref
->refname
);
796 die("parse_object_buffer failed on %s for %s",
797 sha1_to_hex(ref
->objectname
), ref
->refname
);
799 grab_values(ref
->value
, 0, obj
, buf
, size
);
804 * If there is no atom that wants to know about tagged
805 * object, we are done.
807 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
811 * If it is a tag object, see if we use a value that derefs
812 * the object, and if we do grab the object it refers to.
814 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
817 * NEEDSWORK: This derefs tag only once, which
818 * is good to deal with chains of trust, but
819 * is not consistent with what deref_tag() does
820 * which peels the onion to the core.
822 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
824 die("missing object %s for %s",
825 sha1_to_hex(tagged
), ref
->refname
);
827 die("parse_object_buffer failed on %s for %s",
828 sha1_to_hex(tagged
), ref
->refname
);
829 grab_values(ref
->value
, 1, obj
, buf
, size
);
835 * Given a ref, return the value for the atom. This lazily gets value
836 * out of the object by calling populate value.
838 static void get_value(struct refinfo
*ref
, int atom
, struct atom_value
**v
)
842 fill_missing_values(ref
->value
);
844 *v
= &ref
->value
[atom
];
847 struct grab_ref_cbdata
{
848 struct refinfo
**grab_array
;
849 const char **grab_pattern
;
854 * A call-back given to for_each_ref(). Filter refs and keep them for
855 * later object processing.
857 static int grab_single_ref(const char *refname
, const struct object_id
*oid
,
858 int flag
, void *cb_data
)
860 struct grab_ref_cbdata
*cb
= cb_data
;
864 if (flag
& REF_BAD_NAME
) {
865 warning("ignoring ref with broken name %s", refname
);
869 if (flag
& REF_ISBROKEN
) {
870 warning("ignoring broken ref %s", refname
);
874 if (*cb
->grab_pattern
) {
875 const char **pattern
;
876 int namelen
= strlen(refname
);
877 for (pattern
= cb
->grab_pattern
; *pattern
; pattern
++) {
878 const char *p
= *pattern
;
879 int plen
= strlen(p
);
881 if ((plen
<= namelen
) &&
882 !strncmp(refname
, p
, plen
) &&
883 (refname
[plen
] == '\0' ||
884 refname
[plen
] == '/' ||
887 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
895 * We do not open the object yet; sort may only need refname
896 * to do its job and the resulting list may yet to be pruned
899 ref
= xcalloc(1, sizeof(*ref
));
900 ref
->refname
= xstrdup(refname
);
901 hashcpy(ref
->objectname
, oid
->hash
);
905 REALLOC_ARRAY(cb
->grab_array
, cnt
+ 1);
906 cb
->grab_array
[cnt
++] = ref
;
911 static int cmp_ref_sort(struct ref_sort
*s
, struct refinfo
*a
, struct refinfo
*b
)
913 struct atom_value
*va
, *vb
;
915 cmp_type cmp_type
= used_atom_type
[s
->atom
];
917 get_value(a
, s
->atom
, &va
);
918 get_value(b
, s
->atom
, &vb
);
921 cmp
= strcmp(va
->s
, vb
->s
);
926 else if (va
->ul
== vb
->ul
)
932 return (s
->reverse
) ? -cmp
: cmp
;
935 static struct ref_sort
*ref_sort
;
936 static int compare_refs(const void *a_
, const void *b_
)
938 struct refinfo
*a
= *((struct refinfo
**)a_
);
939 struct refinfo
*b
= *((struct refinfo
**)b_
);
942 for (s
= ref_sort
; s
; s
= s
->next
) {
943 int cmp
= cmp_ref_sort(s
, a
, b
);
950 static void sort_refs(struct ref_sort
*sort
, struct refinfo
**refs
, int num_refs
)
953 qsort(refs
, num_refs
, sizeof(struct refinfo
*), compare_refs
);
956 static void print_value(struct atom_value
*v
, int quote_style
)
958 struct strbuf sb
= STRBUF_INIT
;
959 switch (quote_style
) {
964 sq_quote_buf(&sb
, v
->s
);
967 perl_quote_buf(&sb
, v
->s
);
970 python_quote_buf(&sb
, v
->s
);
973 tcl_quote_buf(&sb
, v
->s
);
976 if (quote_style
!= QUOTE_NONE
) {
977 fputs(sb
.buf
, stdout
);
982 static int hex1(char ch
)
984 if ('0' <= ch
&& ch
<= '9')
986 else if ('a' <= ch
&& ch
<= 'f')
987 return ch
- 'a' + 10;
988 else if ('A' <= ch
&& ch
<= 'F')
989 return ch
- 'A' + 10;
992 static int hex2(const char *cp
)
995 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
1000 static void emit(const char *cp
, const char *ep
)
1002 while (*cp
&& (!ep
|| cp
< ep
)) {
1007 int ch
= hex2(cp
+ 1);
1020 static void show_ref(struct refinfo
*info
, const char *format
, int quote_style
)
1022 const char *cp
, *sp
, *ep
;
1024 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1025 struct atom_value
*atomv
;
1027 ep
= strchr(sp
, ')');
1030 get_value(info
, parse_atom(sp
+ 2, ep
), &atomv
);
1031 print_value(atomv
, quote_style
);
1034 sp
= cp
+ strlen(cp
);
1037 if (need_color_reset_at_eol
) {
1038 struct atom_value resetv
;
1039 char color
[COLOR_MAXLEN
] = "";
1041 if (color_parse("reset", color
) < 0)
1042 die("BUG: couldn't parse 'reset' as a color");
1044 print_value(&resetv
, quote_style
);
1049 static struct ref_sort
*default_sort(void)
1051 static const char cstr_name
[] = "refname";
1053 struct ref_sort
*sort
= xcalloc(1, sizeof(*sort
));
1056 sort
->atom
= parse_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1060 static int opt_parse_sort(const struct option
*opt
, const char *arg
, int unset
)
1062 struct ref_sort
**sort_tail
= opt
->value
;
1066 if (!arg
) /* should --no-sort void the list ? */
1069 s
= xcalloc(1, sizeof(*s
));
1070 s
->next
= *sort_tail
;
1078 s
->atom
= parse_atom(arg
, arg
+len
);
1082 static char const * const for_each_ref_usage
[] = {
1083 N_("git for-each-ref [<options>] [<pattern>]"),
1087 int cmd_for_each_ref(int argc
, const char **argv
, const char *prefix
)
1090 const char *format
= "%(objectname) %(objecttype)\t%(refname)";
1091 struct ref_sort
*sort
= NULL
, **sort_tail
= &sort
;
1092 int maxcount
= 0, quote_style
= 0;
1093 struct refinfo
**refs
;
1094 struct grab_ref_cbdata cbdata
;
1096 struct option opts
[] = {
1097 OPT_BIT('s', "shell", "e_style
,
1098 N_("quote placeholders suitably for shells"), QUOTE_SHELL
),
1099 OPT_BIT('p', "perl", "e_style
,
1100 N_("quote placeholders suitably for perl"), QUOTE_PERL
),
1101 OPT_BIT(0 , "python", "e_style
,
1102 N_("quote placeholders suitably for python"), QUOTE_PYTHON
),
1103 OPT_BIT(0 , "tcl", "e_style
,
1104 N_("quote placeholders suitably for Tcl"), QUOTE_TCL
),
1107 OPT_INTEGER( 0 , "count", &maxcount
, N_("show only <n> matched refs")),
1108 OPT_STRING( 0 , "format", &format
, N_("format"), N_("format to use for the output")),
1109 OPT_CALLBACK(0 , "sort", sort_tail
, N_("key"),
1110 N_("field name to sort on"), &opt_parse_sort
),
1114 parse_options(argc
, argv
, prefix
, opts
, for_each_ref_usage
, 0);
1116 error("invalid --count argument: `%d'", maxcount
);
1117 usage_with_options(for_each_ref_usage
, opts
);
1119 if (HAS_MULTI_BITS(quote_style
)) {
1120 error("more than one quoting style?");
1121 usage_with_options(for_each_ref_usage
, opts
);
1123 if (verify_format(format
))
1124 usage_with_options(for_each_ref_usage
, opts
);
1127 sort
= default_sort();
1129 /* for warn_ambiguous_refs */
1130 git_config(git_default_config
, NULL
);
1132 memset(&cbdata
, 0, sizeof(cbdata
));
1133 cbdata
.grab_pattern
= argv
;
1134 for_each_rawref(grab_single_ref
, &cbdata
);
1135 refs
= cbdata
.grab_array
;
1136 num_refs
= cbdata
.grab_cnt
;
1138 sort_refs(sort
, refs
, num_refs
);
1140 if (!maxcount
|| num_refs
< maxcount
)
1141 maxcount
= num_refs
;
1142 for (i
= 0; i
< maxcount
; i
++)
1143 show_ref(refs
[i
], format
, quote_style
);