10 #include "parse-options.h"
17 #define QUOTE_PYTHON 4
20 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
24 unsigned long ul
; /* used for sorting when not FIELD_STR */
28 struct ref_sort
*next
;
29 int atom
; /* index into used_atom array */
35 unsigned char objectname
[20];
38 struct atom_value
*value
;
47 { "objectsize", FIELD_ULONG
},
51 { "numparent", FIELD_ULONG
},
58 { "authordate", FIELD_TIME
},
62 { "committerdate", FIELD_TIME
},
66 { "taggerdate", FIELD_TIME
},
68 { "creatordate", FIELD_TIME
},
72 { "contents:subject" },
74 { "contents:signature" },
82 * An atom is a valid field atom listed above, possibly prefixed with
83 * a "*" to denote deref_tag().
85 * We parse given format string and sort specifiers, and make a list
86 * of properties that we need to extract out of objects. refinfo
87 * structure will hold an array of values extracted that can be
88 * indexed with the "atom number", which is an index into this
91 static const char **used_atom
;
92 static cmp_type
*used_atom_type
;
93 static int used_atom_cnt
, sort_atom_limit
, need_tagged
, need_symref
;
96 * Used to parse format string and sort specifiers
98 static int parse_atom(const char *atom
, const char *ep
)
104 if (*sp
== '*' && sp
< ep
)
107 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
109 /* Do we have the atom already used elsewhere? */
110 for (i
= 0; i
< used_atom_cnt
; i
++) {
111 int len
= strlen(used_atom
[i
]);
112 if (len
== ep
- atom
&& !memcmp(used_atom
[i
], atom
, len
))
116 /* Is the atom a valid one? */
117 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
118 int len
= strlen(valid_atom
[i
].name
);
120 * If the atom name has a colon, strip it and everything after
121 * it off - it specifies the format for this entry, and
122 * shouldn't be used for checking against the valid_atom
125 const char *formatp
= strchr(sp
, ':');
126 if (!formatp
|| ep
< formatp
)
128 if (len
== formatp
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
132 if (ARRAY_SIZE(valid_atom
) <= i
)
133 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
135 /* Add it in, including the deref prefix */
138 used_atom
= xrealloc(used_atom
,
139 (sizeof *used_atom
) * used_atom_cnt
);
140 used_atom_type
= xrealloc(used_atom_type
,
141 (sizeof(*used_atom_type
) * used_atom_cnt
));
142 used_atom
[at
] = xmemdupz(atom
, ep
- atom
);
143 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
146 if (!strcmp(used_atom
[at
], "symref"))
152 * In a format string, find the next occurrence of %(atom).
154 static const char *find_next(const char *cp
)
159 * %( is the start of an atom;
160 * %% is a quoted per-cent.
164 else if (cp
[1] == '%')
165 cp
++; /* skip over two % */
166 /* otherwise this is a singleton, literal % */
174 * Make sure the format string is well formed, and parse out
177 static int verify_format(const char *format
)
180 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
181 const char *ep
= strchr(sp
, ')');
183 return error("malformed format string %s", sp
);
184 /* sp points at "%(" and ep points at the closing ")" */
185 parse_atom(sp
+ 2, ep
);
192 * Given an object name, read the object data and size, and return a
193 * "struct object". If the object data we are returning is also borrowed
194 * by the "struct object" representation, set *eaten as well---it is a
195 * signal from parse_object_buffer to us not to free the buffer.
197 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
199 enum object_type type
;
200 void *buf
= read_sha1_file(sha1
, &type
, sz
);
203 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
209 /* See grab_values */
210 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
214 for (i
= 0; i
< used_atom_cnt
; i
++) {
215 const char *name
= used_atom
[i
];
216 struct atom_value
*v
= &val
[i
];
217 if (!!deref
!= (*name
== '*'))
221 if (!strcmp(name
, "objecttype"))
222 v
->s
= typename(obj
->type
);
223 else if (!strcmp(name
, "objectsize")) {
224 char *s
= xmalloc(40);
225 sprintf(s
, "%lu", sz
);
229 else if (!strcmp(name
, "objectname")) {
230 char *s
= xmalloc(41);
231 strcpy(s
, sha1_to_hex(obj
->sha1
));
234 else if (!strcmp(name
, "objectname:short")) {
235 v
->s
= xstrdup(find_unique_abbrev(obj
->sha1
,
241 /* See grab_values */
242 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
245 struct tag
*tag
= (struct tag
*) obj
;
247 for (i
= 0; i
< used_atom_cnt
; i
++) {
248 const char *name
= used_atom
[i
];
249 struct atom_value
*v
= &val
[i
];
250 if (!!deref
!= (*name
== '*'))
254 if (!strcmp(name
, "tag"))
256 else if (!strcmp(name
, "type") && tag
->tagged
)
257 v
->s
= typename(tag
->tagged
->type
);
258 else if (!strcmp(name
, "object") && tag
->tagged
) {
259 char *s
= xmalloc(41);
260 strcpy(s
, sha1_to_hex(tag
->tagged
->sha1
));
266 static int num_parents(struct commit
*commit
)
268 struct commit_list
*parents
;
271 for (i
= 0, parents
= commit
->parents
;
273 parents
= parents
->next
)
278 /* See grab_values */
279 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
282 struct commit
*commit
= (struct commit
*) obj
;
284 for (i
= 0; i
< used_atom_cnt
; i
++) {
285 const char *name
= used_atom
[i
];
286 struct atom_value
*v
= &val
[i
];
287 if (!!deref
!= (*name
== '*'))
291 if (!strcmp(name
, "tree")) {
292 char *s
= xmalloc(41);
293 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
296 if (!strcmp(name
, "numparent")) {
297 char *s
= xmalloc(40);
298 v
->ul
= num_parents(commit
);
299 sprintf(s
, "%lu", v
->ul
);
302 else if (!strcmp(name
, "parent")) {
303 int num
= num_parents(commit
);
305 struct commit_list
*parents
;
306 char *s
= xmalloc(41 * num
+ 1);
308 for (i
= 0, parents
= commit
->parents
;
310 parents
= parents
->next
, i
= i
+ 41) {
311 struct commit
*parent
= parents
->item
;
312 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
322 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
326 if (!strncmp(buf
, who
, wholen
) &&
328 return buf
+ wholen
+ 1;
329 eol
= strchr(buf
, '\n');
334 return ""; /* end of header */
340 static const char *copy_line(const char *buf
)
342 const char *eol
= strchrnul(buf
, '\n');
343 return xmemdupz(buf
, eol
- buf
);
346 static const char *copy_name(const char *buf
)
349 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
350 if (!strncmp(cp
, " <", 2))
351 return xmemdupz(buf
, cp
- buf
);
356 static const char *copy_email(const char *buf
)
358 const char *email
= strchr(buf
, '<');
362 eoemail
= strchr(email
, '>');
365 return xmemdupz(email
, eoemail
+ 1 - email
);
368 static char *copy_subject(const char *buf
, unsigned long len
)
370 char *r
= xmemdupz(buf
, len
);
373 for (i
= 0; i
< len
; i
++)
380 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
382 const char *eoemail
= strstr(buf
, "> ");
384 unsigned long timestamp
;
386 enum date_mode date_mode
= DATE_NORMAL
;
390 * We got here because atomname ends in "date" or "date<something>";
391 * it's not possible that <something> is not ":<format>" because
392 * parse_atom() wouldn't have allowed it, so we can assume that no
393 * ":" means no format is specified, and use the default.
395 formatp
= strchr(atomname
, ':');
396 if (formatp
!= NULL
) {
398 date_mode
= parse_date_format(formatp
);
403 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
404 if (timestamp
== ULONG_MAX
)
406 tz
= strtol(zone
, NULL
, 10);
407 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
409 v
->s
= xstrdup(show_date(timestamp
, tz
, date_mode
));
417 /* See grab_values */
418 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
421 int wholen
= strlen(who
);
422 const char *wholine
= NULL
;
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
== '*'))
431 if (strncmp(who
, name
, wholen
))
433 if (name
[wholen
] != 0 &&
434 strcmp(name
+ wholen
, "name") &&
435 strcmp(name
+ wholen
, "email") &&
436 prefixcmp(name
+ wholen
, "date"))
439 wholine
= find_wholine(who
, wholen
, buf
, sz
);
441 return; /* no point looking for it */
442 if (name
[wholen
] == 0)
443 v
->s
= copy_line(wholine
);
444 else if (!strcmp(name
+ wholen
, "name"))
445 v
->s
= copy_name(wholine
);
446 else if (!strcmp(name
+ wholen
, "email"))
447 v
->s
= copy_email(wholine
);
448 else if (!prefixcmp(name
+ wholen
, "date"))
449 grab_date(wholine
, v
, name
);
453 * For a tag or a commit object, if "creator" or "creatordate" is
454 * requested, do something special.
456 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
457 return; /* "author" for commit object is not wanted */
459 wholine
= find_wholine(who
, wholen
, buf
, sz
);
462 for (i
= 0; i
< used_atom_cnt
; i
++) {
463 const char *name
= used_atom
[i
];
464 struct atom_value
*v
= &val
[i
];
465 if (!!deref
!= (*name
== '*'))
470 if (!prefixcmp(name
, "creatordate"))
471 grab_date(wholine
, v
, name
);
472 else if (!strcmp(name
, "creator"))
473 v
->s
= copy_line(wholine
);
477 static void find_subpos(const char *buf
, unsigned long sz
,
478 const char **sub
, unsigned long *sublen
,
479 const char **body
, unsigned long *bodylen
,
480 unsigned long *nonsiglen
,
481 const char **sig
, unsigned long *siglen
)
484 /* skip past header until we hit empty line */
485 while (*buf
&& *buf
!= '\n') {
486 eol
= strchrnul(buf
, '\n');
491 /* skip any empty lines */
495 /* parse signature first; we might not even have a subject line */
496 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
497 *siglen
= strlen(*sig
);
499 /* subject is first non-empty line */
501 /* subject goes to first empty line */
502 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
503 eol
= strchrnul(buf
, '\n');
508 *sublen
= buf
- *sub
;
509 /* drop trailing newline, if present */
510 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
513 /* skip any empty lines */
517 *bodylen
= strlen(buf
);
518 *nonsiglen
= *sig
- buf
;
521 /* See grab_values */
522 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
525 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
526 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
528 for (i
= 0; i
< used_atom_cnt
; i
++) {
529 const char *name
= used_atom
[i
];
530 struct atom_value
*v
= &val
[i
];
531 if (!!deref
!= (*name
== '*'))
535 if (strcmp(name
, "subject") &&
536 strcmp(name
, "body") &&
537 strcmp(name
, "contents") &&
538 strcmp(name
, "contents:subject") &&
539 strcmp(name
, "contents:body") &&
540 strcmp(name
, "contents:signature"))
545 &bodypos
, &bodylen
, &nonsiglen
,
548 if (!strcmp(name
, "subject"))
549 v
->s
= copy_subject(subpos
, sublen
);
550 else if (!strcmp(name
, "contents:subject"))
551 v
->s
= copy_subject(subpos
, sublen
);
552 else if (!strcmp(name
, "body"))
553 v
->s
= xmemdupz(bodypos
, bodylen
);
554 else if (!strcmp(name
, "contents:body"))
555 v
->s
= xmemdupz(bodypos
, nonsiglen
);
556 else if (!strcmp(name
, "contents:signature"))
557 v
->s
= xmemdupz(sigpos
, siglen
);
558 else if (!strcmp(name
, "contents"))
559 v
->s
= xstrdup(subpos
);
564 * We want to have empty print-string for field requests
565 * that do not apply (e.g. "authordate" for a tag object)
567 static void fill_missing_values(struct atom_value
*val
)
570 for (i
= 0; i
< used_atom_cnt
; i
++) {
571 struct atom_value
*v
= &val
[i
];
578 * val is a list of atom_value to hold returned values. Extract
579 * the values for atoms in used_atom array out of (obj, buf, sz).
580 * when deref is false, (obj, buf, sz) is the object that is
581 * pointed at by the ref itself; otherwise it is the object the
582 * ref (which is a tag) refers to.
584 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
586 grab_common_values(val
, deref
, obj
, buf
, sz
);
589 grab_tag_values(val
, deref
, obj
, buf
, sz
);
590 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
591 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
594 grab_commit_values(val
, deref
, obj
, buf
, sz
);
595 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
596 grab_person("author", val
, deref
, obj
, buf
, sz
);
597 grab_person("committer", val
, deref
, obj
, buf
, sz
);
600 /* grab_tree_values(val, deref, obj, buf, sz); */
603 /* grab_blob_values(val, deref, obj, buf, sz); */
606 die("Eh? Object of type %d?", obj
->type
);
610 static inline char *copy_advance(char *dst
, const char *src
)
618 * Parse the object referred by ref, and grab needed value.
620 static void populate_value(struct refinfo
*ref
)
626 const unsigned char *tagged
;
628 ref
->value
= xcalloc(sizeof(struct atom_value
), used_atom_cnt
);
630 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
631 unsigned char unused1
[20];
632 ref
->symref
= resolve_refdup(ref
->refname
, unused1
, 1, NULL
);
637 /* Fill in specials first */
638 for (i
= 0; i
< used_atom_cnt
; i
++) {
639 const char *name
= used_atom
[i
];
640 struct atom_value
*v
= &ref
->value
[i
];
644 struct branch
*branch
= NULL
;
651 if (!prefixcmp(name
, "refname"))
652 refname
= ref
->refname
;
653 else if (!prefixcmp(name
, "symref"))
654 refname
= ref
->symref
? ref
->symref
: "";
655 else if (!prefixcmp(name
, "upstream")) {
656 /* only local branches may have an upstream */
657 if (prefixcmp(ref
->refname
, "refs/heads/"))
659 branch
= branch_get(ref
->refname
+ 11);
661 if (!branch
|| !branch
->merge
|| !branch
->merge
[0] ||
662 !branch
->merge
[0]->dst
)
664 refname
= branch
->merge
[0]->dst
;
666 else if (!strcmp(name
, "flag")) {
667 char buf
[256], *cp
= buf
;
668 if (ref
->flag
& REF_ISSYMREF
)
669 cp
= copy_advance(cp
, ",symref");
670 if (ref
->flag
& REF_ISPACKED
)
671 cp
= copy_advance(cp
, ",packed");
676 v
->s
= xstrdup(buf
+ 1);
679 } else if (!strcmp(name
, "HEAD")) {
681 unsigned char sha1
[20];
683 head
= resolve_ref_unsafe("HEAD", sha1
, 1, NULL
);
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 !prefixcmp(name
, "upstream")) {
704 stat_tracking_info(branch
, &num_ours
, &num_theirs
);
705 if (!num_ours
&& !num_theirs
)
707 else if (!num_ours
) {
708 sprintf(buf
, "[behind %d]", num_theirs
);
710 } else if (!num_theirs
) {
711 sprintf(buf
, "[ahead %d]", num_ours
);
714 sprintf(buf
, "[ahead %d, behind %d]",
715 num_ours
, num_theirs
);
719 } else if (!strcmp(formatp
, "trackshort") &&
720 !prefixcmp(name
, "upstream")) {
722 stat_tracking_info(branch
, &num_ours
, &num_theirs
);
723 if (!num_ours
&& !num_theirs
)
727 else if (!num_theirs
)
733 die("unknown %.*s format %s",
734 (int)(formatp
- name
), name
, formatp
);
740 int len
= strlen(refname
);
741 char *s
= xmalloc(len
+ 4);
742 sprintf(s
, "%s^{}", refname
);
747 for (i
= 0; i
< used_atom_cnt
; i
++) {
748 struct atom_value
*v
= &ref
->value
[i
];
755 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
757 die("missing object %s for %s",
758 sha1_to_hex(ref
->objectname
), ref
->refname
);
760 die("parse_object_buffer failed on %s for %s",
761 sha1_to_hex(ref
->objectname
), ref
->refname
);
763 grab_values(ref
->value
, 0, obj
, buf
, size
);
768 * If there is no atom that wants to know about tagged
769 * object, we are done.
771 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
775 * If it is a tag object, see if we use a value that derefs
776 * the object, and if we do grab the object it refers to.
778 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
781 * NEEDSWORK: This derefs tag only once, which
782 * is good to deal with chains of trust, but
783 * is not consistent with what deref_tag() does
784 * which peels the onion to the core.
786 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
788 die("missing object %s for %s",
789 sha1_to_hex(tagged
), ref
->refname
);
791 die("parse_object_buffer failed on %s for %s",
792 sha1_to_hex(tagged
), ref
->refname
);
793 grab_values(ref
->value
, 1, obj
, buf
, size
);
799 * Given a ref, return the value for the atom. This lazily gets value
800 * out of the object by calling populate value.
802 static void get_value(struct refinfo
*ref
, int atom
, struct atom_value
**v
)
806 fill_missing_values(ref
->value
);
808 *v
= &ref
->value
[atom
];
811 struct grab_ref_cbdata
{
812 struct refinfo
**grab_array
;
813 const char **grab_pattern
;
818 * A call-back given to for_each_ref(). Filter refs and keep them for
819 * later object processing.
821 static int grab_single_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
823 struct grab_ref_cbdata
*cb
= cb_data
;
827 if (*cb
->grab_pattern
) {
828 const char **pattern
;
829 int namelen
= strlen(refname
);
830 for (pattern
= cb
->grab_pattern
; *pattern
; pattern
++) {
831 const char *p
= *pattern
;
832 int plen
= strlen(p
);
834 if ((plen
<= namelen
) &&
835 !strncmp(refname
, p
, plen
) &&
836 (refname
[plen
] == '\0' ||
837 refname
[plen
] == '/' ||
840 if (!fnmatch(p
, refname
, FNM_PATHNAME
))
848 * We do not open the object yet; sort may only need refname
849 * to do its job and the resulting list may yet to be pruned
852 ref
= xcalloc(1, sizeof(*ref
));
853 ref
->refname
= xstrdup(refname
);
854 hashcpy(ref
->objectname
, sha1
);
858 cb
->grab_array
= xrealloc(cb
->grab_array
,
859 sizeof(*cb
->grab_array
) * (cnt
+ 1));
860 cb
->grab_array
[cnt
++] = ref
;
865 static int cmp_ref_sort(struct ref_sort
*s
, struct refinfo
*a
, struct refinfo
*b
)
867 struct atom_value
*va
, *vb
;
869 cmp_type cmp_type
= used_atom_type
[s
->atom
];
871 get_value(a
, s
->atom
, &va
);
872 get_value(b
, s
->atom
, &vb
);
875 cmp
= strcmp(va
->s
, vb
->s
);
880 else if (va
->ul
== vb
->ul
)
886 return (s
->reverse
) ? -cmp
: cmp
;
889 static struct ref_sort
*ref_sort
;
890 static int compare_refs(const void *a_
, const void *b_
)
892 struct refinfo
*a
= *((struct refinfo
**)a_
);
893 struct refinfo
*b
= *((struct refinfo
**)b_
);
896 for (s
= ref_sort
; s
; s
= s
->next
) {
897 int cmp
= cmp_ref_sort(s
, a
, b
);
904 static void sort_refs(struct ref_sort
*sort
, struct refinfo
**refs
, int num_refs
)
907 qsort(refs
, num_refs
, sizeof(struct refinfo
*), compare_refs
);
910 static void print_value(struct refinfo
*ref
, int atom
, int quote_style
)
912 struct atom_value
*v
;
913 struct strbuf sb
= STRBUF_INIT
;
914 get_value(ref
, atom
, &v
);
915 switch (quote_style
) {
920 sq_quote_buf(&sb
, v
->s
);
923 perl_quote_buf(&sb
, v
->s
);
926 python_quote_buf(&sb
, v
->s
);
929 tcl_quote_buf(&sb
, v
->s
);
932 if (quote_style
!= QUOTE_NONE
) {
933 fputs(sb
.buf
, stdout
);
938 static int hex1(char ch
)
940 if ('0' <= ch
&& ch
<= '9')
942 else if ('a' <= ch
&& ch
<= 'f')
943 return ch
- 'a' + 10;
944 else if ('A' <= ch
&& ch
<= 'F')
945 return ch
- 'A' + 10;
948 static int hex2(const char *cp
)
951 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
956 static void emit(const char *cp
, const char *ep
)
958 while (*cp
&& (!ep
|| cp
< ep
)) {
963 int ch
= hex2(cp
+ 1);
976 static void show_ref(struct refinfo
*info
, const char *format
, int quote_style
)
978 const char *cp
, *sp
, *ep
;
980 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
981 ep
= strchr(sp
, ')');
984 print_value(info
, parse_atom(sp
+ 2, ep
), quote_style
);
987 sp
= cp
+ strlen(cp
);
993 static struct ref_sort
*default_sort(void)
995 static const char cstr_name
[] = "refname";
997 struct ref_sort
*sort
= xcalloc(1, sizeof(*sort
));
1000 sort
->atom
= parse_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1004 static int opt_parse_sort(const struct option
*opt
, const char *arg
, int unset
)
1006 struct ref_sort
**sort_tail
= opt
->value
;
1010 if (!arg
) /* should --no-sort void the list ? */
1013 s
= xcalloc(1, sizeof(*s
));
1014 s
->next
= *sort_tail
;
1022 s
->atom
= parse_atom(arg
, arg
+len
);
1026 static char const * const for_each_ref_usage
[] = {
1027 N_("git for-each-ref [options] [<pattern>]"),
1031 int cmd_for_each_ref(int argc
, const char **argv
, const char *prefix
)
1034 const char *format
= "%(objectname) %(objecttype)\t%(refname)";
1035 struct ref_sort
*sort
= NULL
, **sort_tail
= &sort
;
1036 int maxcount
= 0, quote_style
= 0;
1037 struct refinfo
**refs
;
1038 struct grab_ref_cbdata cbdata
;
1040 struct option opts
[] = {
1041 OPT_BIT('s', "shell", "e_style
,
1042 N_("quote placeholders suitably for shells"), QUOTE_SHELL
),
1043 OPT_BIT('p', "perl", "e_style
,
1044 N_("quote placeholders suitably for perl"), QUOTE_PERL
),
1045 OPT_BIT(0 , "python", "e_style
,
1046 N_("quote placeholders suitably for python"), QUOTE_PYTHON
),
1047 OPT_BIT(0 , "tcl", "e_style
,
1048 N_("quote placeholders suitably for tcl"), QUOTE_TCL
),
1051 OPT_INTEGER( 0 , "count", &maxcount
, N_("show only <n> matched refs")),
1052 OPT_STRING( 0 , "format", &format
, N_("format"), N_("format to use for the output")),
1053 OPT_CALLBACK(0 , "sort", sort_tail
, N_("key"),
1054 N_("field name to sort on"), &opt_parse_sort
),
1058 parse_options(argc
, argv
, prefix
, opts
, for_each_ref_usage
, 0);
1060 error("invalid --count argument: `%d'", maxcount
);
1061 usage_with_options(for_each_ref_usage
, opts
);
1063 if (HAS_MULTI_BITS(quote_style
)) {
1064 error("more than one quoting style?");
1065 usage_with_options(for_each_ref_usage
, opts
);
1067 if (verify_format(format
))
1068 usage_with_options(for_each_ref_usage
, opts
);
1071 sort
= default_sort();
1072 sort_atom_limit
= used_atom_cnt
;
1074 /* for warn_ambiguous_refs */
1075 git_config(git_default_config
, NULL
);
1077 memset(&cbdata
, 0, sizeof(cbdata
));
1078 cbdata
.grab_pattern
= argv
;
1079 for_each_rawref(grab_single_ref
, &cbdata
);
1080 refs
= cbdata
.grab_array
;
1081 num_refs
= cbdata
.grab_cnt
;
1083 sort_refs(sort
, refs
, num_refs
);
1085 if (!maxcount
|| num_refs
< maxcount
)
1086 maxcount
= num_refs
;
1087 for (i
= 0; i
< maxcount
; i
++)
1088 show_ref(refs
[i
], format
, quote_style
);