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 used_atom
= xrealloc(used_atom
,
142 (sizeof *used_atom
) * used_atom_cnt
);
143 used_atom_type
= xrealloc(used_atom_type
,
144 (sizeof(*used_atom_type
) * used_atom_cnt
));
145 used_atom
[at
] = xmemdupz(atom
, ep
- atom
);
146 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
149 if (!strcmp(used_atom
[at
], "symref"))
155 * In a format string, find the next occurrence of %(atom).
157 static const char *find_next(const char *cp
)
162 * %( is the start of an atom;
163 * %% is a quoted per-cent.
167 else if (cp
[1] == '%')
168 cp
++; /* skip over two % */
169 /* otherwise this is a singleton, literal % */
177 * Make sure the format string is well formed, and parse out
180 static int verify_format(const char *format
)
183 static const char color_reset
[] = "color:reset";
185 need_color_reset_at_eol
= 0;
186 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
187 const char *ep
= strchr(sp
, ')');
191 return error("malformed format string %s", sp
);
192 /* sp points at "%(" and ep points at the closing ")" */
193 at
= parse_atom(sp
+ 2, ep
);
196 if (!memcmp(used_atom
[at
], "color:", 6))
197 need_color_reset_at_eol
= !!strcmp(used_atom
[at
], color_reset
);
203 * Given an object name, read the object data and size, and return a
204 * "struct object". If the object data we are returning is also borrowed
205 * by the "struct object" representation, set *eaten as well---it is a
206 * signal from parse_object_buffer to us not to free the buffer.
208 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
210 enum object_type type
;
211 void *buf
= read_sha1_file(sha1
, &type
, sz
);
214 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
220 static int grab_objectname(const char *name
, const unsigned char *sha1
,
221 struct atom_value
*v
)
223 if (!strcmp(name
, "objectname")) {
224 char *s
= xmalloc(41);
225 strcpy(s
, sha1_to_hex(sha1
));
229 if (!strcmp(name
, "objectname:short")) {
230 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
236 /* See grab_values */
237 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
241 for (i
= 0; i
< used_atom_cnt
; i
++) {
242 const char *name
= used_atom
[i
];
243 struct atom_value
*v
= &val
[i
];
244 if (!!deref
!= (*name
== '*'))
248 if (!strcmp(name
, "objecttype"))
249 v
->s
= typename(obj
->type
);
250 else if (!strcmp(name
, "objectsize")) {
251 char *s
= xmalloc(40);
252 sprintf(s
, "%lu", sz
);
257 grab_objectname(name
, obj
->sha1
, v
);
261 /* See grab_values */
262 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
265 struct tag
*tag
= (struct tag
*) obj
;
267 for (i
= 0; i
< used_atom_cnt
; i
++) {
268 const char *name
= used_atom
[i
];
269 struct atom_value
*v
= &val
[i
];
270 if (!!deref
!= (*name
== '*'))
274 if (!strcmp(name
, "tag"))
276 else if (!strcmp(name
, "type") && tag
->tagged
)
277 v
->s
= typename(tag
->tagged
->type
);
278 else if (!strcmp(name
, "object") && tag
->tagged
) {
279 char *s
= xmalloc(41);
280 strcpy(s
, sha1_to_hex(tag
->tagged
->sha1
));
286 static int num_parents(struct commit
*commit
)
288 struct commit_list
*parents
;
291 for (i
= 0, parents
= commit
->parents
;
293 parents
= parents
->next
)
298 /* See grab_values */
299 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
302 struct commit
*commit
= (struct commit
*) obj
;
304 for (i
= 0; i
< used_atom_cnt
; i
++) {
305 const char *name
= used_atom
[i
];
306 struct atom_value
*v
= &val
[i
];
307 if (!!deref
!= (*name
== '*'))
311 if (!strcmp(name
, "tree")) {
312 char *s
= xmalloc(41);
313 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
316 if (!strcmp(name
, "numparent")) {
317 char *s
= xmalloc(40);
318 v
->ul
= num_parents(commit
);
319 sprintf(s
, "%lu", v
->ul
);
322 else if (!strcmp(name
, "parent")) {
323 int num
= num_parents(commit
);
325 struct commit_list
*parents
;
326 char *s
= xmalloc(41 * num
+ 1);
328 for (i
= 0, parents
= commit
->parents
;
330 parents
= parents
->next
, i
= i
+ 41) {
331 struct commit
*parent
= parents
->item
;
332 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
342 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
346 if (!strncmp(buf
, who
, wholen
) &&
348 return buf
+ wholen
+ 1;
349 eol
= strchr(buf
, '\n');
354 return ""; /* end of header */
360 static const char *copy_line(const char *buf
)
362 const char *eol
= strchrnul(buf
, '\n');
363 return xmemdupz(buf
, eol
- buf
);
366 static const char *copy_name(const char *buf
)
369 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
370 if (!strncmp(cp
, " <", 2))
371 return xmemdupz(buf
, cp
- buf
);
376 static const char *copy_email(const char *buf
)
378 const char *email
= strchr(buf
, '<');
382 eoemail
= strchr(email
, '>');
385 return xmemdupz(email
, eoemail
+ 1 - email
);
388 static char *copy_subject(const char *buf
, unsigned long len
)
390 char *r
= xmemdupz(buf
, len
);
393 for (i
= 0; i
< len
; i
++)
400 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
402 const char *eoemail
= strstr(buf
, "> ");
404 unsigned long timestamp
;
406 enum date_mode date_mode
= DATE_NORMAL
;
410 * We got here because atomname ends in "date" or "date<something>";
411 * it's not possible that <something> is not ":<format>" because
412 * parse_atom() wouldn't have allowed it, so we can assume that no
413 * ":" means no format is specified, and use the default.
415 formatp
= strchr(atomname
, ':');
416 if (formatp
!= NULL
) {
418 date_mode
= parse_date_format(formatp
);
423 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
424 if (timestamp
== ULONG_MAX
)
426 tz
= strtol(zone
, NULL
, 10);
427 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
429 v
->s
= xstrdup(show_date(timestamp
, tz
, date_mode
));
437 /* See grab_values */
438 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
441 int wholen
= strlen(who
);
442 const char *wholine
= NULL
;
444 for (i
= 0; i
< used_atom_cnt
; i
++) {
445 const char *name
= used_atom
[i
];
446 struct atom_value
*v
= &val
[i
];
447 if (!!deref
!= (*name
== '*'))
451 if (strncmp(who
, name
, wholen
))
453 if (name
[wholen
] != 0 &&
454 strcmp(name
+ wholen
, "name") &&
455 strcmp(name
+ wholen
, "email") &&
456 !starts_with(name
+ wholen
, "date"))
459 wholine
= find_wholine(who
, wholen
, buf
, sz
);
461 return; /* no point looking for it */
462 if (name
[wholen
] == 0)
463 v
->s
= copy_line(wholine
);
464 else if (!strcmp(name
+ wholen
, "name"))
465 v
->s
= copy_name(wholine
);
466 else if (!strcmp(name
+ wholen
, "email"))
467 v
->s
= copy_email(wholine
);
468 else if (starts_with(name
+ wholen
, "date"))
469 grab_date(wholine
, v
, name
);
473 * For a tag or a commit object, if "creator" or "creatordate" is
474 * requested, do something special.
476 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
477 return; /* "author" for commit object is not wanted */
479 wholine
= find_wholine(who
, wholen
, buf
, sz
);
482 for (i
= 0; i
< used_atom_cnt
; i
++) {
483 const char *name
= used_atom
[i
];
484 struct atom_value
*v
= &val
[i
];
485 if (!!deref
!= (*name
== '*'))
490 if (starts_with(name
, "creatordate"))
491 grab_date(wholine
, v
, name
);
492 else if (!strcmp(name
, "creator"))
493 v
->s
= copy_line(wholine
);
497 static void find_subpos(const char *buf
, unsigned long sz
,
498 const char **sub
, unsigned long *sublen
,
499 const char **body
, unsigned long *bodylen
,
500 unsigned long *nonsiglen
,
501 const char **sig
, unsigned long *siglen
)
504 /* skip past header until we hit empty line */
505 while (*buf
&& *buf
!= '\n') {
506 eol
= strchrnul(buf
, '\n');
511 /* skip any empty lines */
515 /* parse signature first; we might not even have a subject line */
516 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
517 *siglen
= strlen(*sig
);
519 /* subject is first non-empty line */
521 /* subject goes to first empty line */
522 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
523 eol
= strchrnul(buf
, '\n');
528 *sublen
= buf
- *sub
;
529 /* drop trailing newline, if present */
530 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
533 /* skip any empty lines */
537 *bodylen
= strlen(buf
);
538 *nonsiglen
= *sig
- buf
;
541 /* See grab_values */
542 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
545 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
546 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
548 for (i
= 0; i
< used_atom_cnt
; i
++) {
549 const char *name
= used_atom
[i
];
550 struct atom_value
*v
= &val
[i
];
551 if (!!deref
!= (*name
== '*'))
555 if (strcmp(name
, "subject") &&
556 strcmp(name
, "body") &&
557 strcmp(name
, "contents") &&
558 strcmp(name
, "contents:subject") &&
559 strcmp(name
, "contents:body") &&
560 strcmp(name
, "contents:signature"))
565 &bodypos
, &bodylen
, &nonsiglen
,
568 if (!strcmp(name
, "subject"))
569 v
->s
= copy_subject(subpos
, sublen
);
570 else if (!strcmp(name
, "contents:subject"))
571 v
->s
= copy_subject(subpos
, sublen
);
572 else if (!strcmp(name
, "body"))
573 v
->s
= xmemdupz(bodypos
, bodylen
);
574 else if (!strcmp(name
, "contents:body"))
575 v
->s
= xmemdupz(bodypos
, nonsiglen
);
576 else if (!strcmp(name
, "contents:signature"))
577 v
->s
= xmemdupz(sigpos
, siglen
);
578 else if (!strcmp(name
, "contents"))
579 v
->s
= xstrdup(subpos
);
584 * We want to have empty print-string for field requests
585 * that do not apply (e.g. "authordate" for a tag object)
587 static void fill_missing_values(struct atom_value
*val
)
590 for (i
= 0; i
< used_atom_cnt
; i
++) {
591 struct atom_value
*v
= &val
[i
];
598 * val is a list of atom_value to hold returned values. Extract
599 * the values for atoms in used_atom array out of (obj, buf, sz).
600 * when deref is false, (obj, buf, sz) is the object that is
601 * pointed at by the ref itself; otherwise it is the object the
602 * ref (which is a tag) refers to.
604 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
606 grab_common_values(val
, deref
, obj
, buf
, sz
);
609 grab_tag_values(val
, deref
, obj
, buf
, sz
);
610 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
611 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
614 grab_commit_values(val
, deref
, obj
, buf
, sz
);
615 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
616 grab_person("author", val
, deref
, obj
, buf
, sz
);
617 grab_person("committer", val
, deref
, obj
, buf
, sz
);
620 /* grab_tree_values(val, deref, obj, buf, sz); */
623 /* grab_blob_values(val, deref, obj, buf, sz); */
626 die("Eh? Object of type %d?", obj
->type
);
630 static inline char *copy_advance(char *dst
, const char *src
)
638 * Parse the object referred by ref, and grab needed value.
640 static void populate_value(struct refinfo
*ref
)
646 const unsigned char *tagged
;
648 ref
->value
= xcalloc(sizeof(struct atom_value
), used_atom_cnt
);
650 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
651 unsigned char unused1
[20];
652 ref
->symref
= resolve_refdup(ref
->refname
, unused1
, 1, NULL
);
657 /* Fill in specials first */
658 for (i
= 0; i
< used_atom_cnt
; i
++) {
659 const char *name
= used_atom
[i
];
660 struct atom_value
*v
= &ref
->value
[i
];
664 struct branch
*branch
= NULL
;
671 if (starts_with(name
, "refname"))
672 refname
= ref
->refname
;
673 else if (starts_with(name
, "symref"))
674 refname
= ref
->symref
? ref
->symref
: "";
675 else if (starts_with(name
, "upstream")) {
676 /* only local branches may have an upstream */
677 if (!starts_with(ref
->refname
, "refs/heads/"))
679 branch
= branch_get(ref
->refname
+ 11);
681 if (!branch
|| !branch
->merge
|| !branch
->merge
[0] ||
682 !branch
->merge
[0]->dst
)
684 refname
= branch
->merge
[0]->dst
;
685 } else if (starts_with(name
, "color:")) {
686 char color
[COLOR_MAXLEN
] = "";
688 color_parse(name
+ 6, "--format", color
);
689 v
->s
= xstrdup(color
);
691 } else if (!strcmp(name
, "flag")) {
692 char buf
[256], *cp
= buf
;
693 if (ref
->flag
& REF_ISSYMREF
)
694 cp
= copy_advance(cp
, ",symref");
695 if (ref
->flag
& REF_ISPACKED
)
696 cp
= copy_advance(cp
, ",packed");
701 v
->s
= xstrdup(buf
+ 1);
704 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
)) {
706 } else if (!strcmp(name
, "HEAD")) {
708 unsigned char sha1
[20];
710 head
= resolve_ref_unsafe("HEAD", sha1
, 1, NULL
);
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")) {
731 if (stat_tracking_info(branch
, &num_ours
,
735 if (!num_ours
&& !num_theirs
)
737 else if (!num_ours
) {
738 sprintf(buf
, "[behind %d]", num_theirs
);
740 } else if (!num_theirs
) {
741 sprintf(buf
, "[ahead %d]", num_ours
);
744 sprintf(buf
, "[ahead %d, behind %d]",
745 num_ours
, num_theirs
);
749 } else if (!strcmp(formatp
, "trackshort") &&
750 starts_with(name
, "upstream")) {
753 if (stat_tracking_info(branch
, &num_ours
,
757 if (!num_ours
&& !num_theirs
)
761 else if (!num_theirs
)
767 die("unknown %.*s format %s",
768 (int)(formatp
- name
), name
, formatp
);
774 int len
= strlen(refname
);
775 char *s
= xmalloc(len
+ 4);
776 sprintf(s
, "%s^{}", refname
);
781 for (i
= 0; i
< used_atom_cnt
; i
++) {
782 struct atom_value
*v
= &ref
->value
[i
];
789 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
791 die("missing object %s for %s",
792 sha1_to_hex(ref
->objectname
), ref
->refname
);
794 die("parse_object_buffer failed on %s for %s",
795 sha1_to_hex(ref
->objectname
), ref
->refname
);
797 grab_values(ref
->value
, 0, obj
, buf
, size
);
802 * If there is no atom that wants to know about tagged
803 * object, we are done.
805 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
809 * If it is a tag object, see if we use a value that derefs
810 * the object, and if we do grab the object it refers to.
812 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
815 * NEEDSWORK: This derefs tag only once, which
816 * is good to deal with chains of trust, but
817 * is not consistent with what deref_tag() does
818 * which peels the onion to the core.
820 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
822 die("missing object %s for %s",
823 sha1_to_hex(tagged
), ref
->refname
);
825 die("parse_object_buffer failed on %s for %s",
826 sha1_to_hex(tagged
), ref
->refname
);
827 grab_values(ref
->value
, 1, obj
, buf
, size
);
833 * Given a ref, return the value for the atom. This lazily gets value
834 * out of the object by calling populate value.
836 static void get_value(struct refinfo
*ref
, int atom
, struct atom_value
**v
)
840 fill_missing_values(ref
->value
);
842 *v
= &ref
->value
[atom
];
845 struct grab_ref_cbdata
{
846 struct refinfo
**grab_array
;
847 const char **grab_pattern
;
852 * A call-back given to for_each_ref(). Filter refs and keep them for
853 * later object processing.
855 static int grab_single_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
857 struct grab_ref_cbdata
*cb
= cb_data
;
861 if (*cb
->grab_pattern
) {
862 const char **pattern
;
863 int namelen
= strlen(refname
);
864 for (pattern
= cb
->grab_pattern
; *pattern
; pattern
++) {
865 const char *p
= *pattern
;
866 int plen
= strlen(p
);
868 if ((plen
<= namelen
) &&
869 !strncmp(refname
, p
, plen
) &&
870 (refname
[plen
] == '\0' ||
871 refname
[plen
] == '/' ||
874 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
882 * We do not open the object yet; sort may only need refname
883 * to do its job and the resulting list may yet to be pruned
886 ref
= xcalloc(1, sizeof(*ref
));
887 ref
->refname
= xstrdup(refname
);
888 hashcpy(ref
->objectname
, sha1
);
892 cb
->grab_array
= xrealloc(cb
->grab_array
,
893 sizeof(*cb
->grab_array
) * (cnt
+ 1));
894 cb
->grab_array
[cnt
++] = ref
;
899 static int cmp_ref_sort(struct ref_sort
*s
, struct refinfo
*a
, struct refinfo
*b
)
901 struct atom_value
*va
, *vb
;
903 cmp_type cmp_type
= used_atom_type
[s
->atom
];
905 get_value(a
, s
->atom
, &va
);
906 get_value(b
, s
->atom
, &vb
);
909 cmp
= strcmp(va
->s
, vb
->s
);
914 else if (va
->ul
== vb
->ul
)
920 return (s
->reverse
) ? -cmp
: cmp
;
923 static struct ref_sort
*ref_sort
;
924 static int compare_refs(const void *a_
, const void *b_
)
926 struct refinfo
*a
= *((struct refinfo
**)a_
);
927 struct refinfo
*b
= *((struct refinfo
**)b_
);
930 for (s
= ref_sort
; s
; s
= s
->next
) {
931 int cmp
= cmp_ref_sort(s
, a
, b
);
938 static void sort_refs(struct ref_sort
*sort
, struct refinfo
**refs
, int num_refs
)
941 qsort(refs
, num_refs
, sizeof(struct refinfo
*), compare_refs
);
944 static void print_value(struct atom_value
*v
, int quote_style
)
946 struct strbuf sb
= STRBUF_INIT
;
947 switch (quote_style
) {
952 sq_quote_buf(&sb
, v
->s
);
955 perl_quote_buf(&sb
, v
->s
);
958 python_quote_buf(&sb
, v
->s
);
961 tcl_quote_buf(&sb
, v
->s
);
964 if (quote_style
!= QUOTE_NONE
) {
965 fputs(sb
.buf
, stdout
);
970 static int hex1(char ch
)
972 if ('0' <= ch
&& ch
<= '9')
974 else if ('a' <= ch
&& ch
<= 'f')
975 return ch
- 'a' + 10;
976 else if ('A' <= ch
&& ch
<= 'F')
977 return ch
- 'A' + 10;
980 static int hex2(const char *cp
)
983 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
988 static void emit(const char *cp
, const char *ep
)
990 while (*cp
&& (!ep
|| cp
< ep
)) {
995 int ch
= hex2(cp
+ 1);
1008 static void show_ref(struct refinfo
*info
, const char *format
, int quote_style
)
1010 const char *cp
, *sp
, *ep
;
1012 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1013 struct atom_value
*atomv
;
1015 ep
= strchr(sp
, ')');
1018 get_value(info
, parse_atom(sp
+ 2, ep
), &atomv
);
1019 print_value(atomv
, quote_style
);
1022 sp
= cp
+ strlen(cp
);
1025 if (need_color_reset_at_eol
) {
1026 struct atom_value resetv
;
1027 char color
[COLOR_MAXLEN
] = "";
1029 color_parse("reset", "--format", color
);
1031 print_value(&resetv
, quote_style
);
1036 static struct ref_sort
*default_sort(void)
1038 static const char cstr_name
[] = "refname";
1040 struct ref_sort
*sort
= xcalloc(1, sizeof(*sort
));
1043 sort
->atom
= parse_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1047 static int opt_parse_sort(const struct option
*opt
, const char *arg
, int unset
)
1049 struct ref_sort
**sort_tail
= opt
->value
;
1053 if (!arg
) /* should --no-sort void the list ? */
1056 s
= xcalloc(1, sizeof(*s
));
1057 s
->next
= *sort_tail
;
1065 s
->atom
= parse_atom(arg
, arg
+len
);
1069 static char const * const for_each_ref_usage
[] = {
1070 N_("git for-each-ref [options] [<pattern>]"),
1074 int cmd_for_each_ref(int argc
, const char **argv
, const char *prefix
)
1077 const char *format
= "%(objectname) %(objecttype)\t%(refname)";
1078 struct ref_sort
*sort
= NULL
, **sort_tail
= &sort
;
1079 int maxcount
= 0, quote_style
= 0;
1080 struct refinfo
**refs
;
1081 struct grab_ref_cbdata cbdata
;
1083 struct option opts
[] = {
1084 OPT_BIT('s', "shell", "e_style
,
1085 N_("quote placeholders suitably for shells"), QUOTE_SHELL
),
1086 OPT_BIT('p', "perl", "e_style
,
1087 N_("quote placeholders suitably for perl"), QUOTE_PERL
),
1088 OPT_BIT(0 , "python", "e_style
,
1089 N_("quote placeholders suitably for python"), QUOTE_PYTHON
),
1090 OPT_BIT(0 , "tcl", "e_style
,
1091 N_("quote placeholders suitably for tcl"), QUOTE_TCL
),
1094 OPT_INTEGER( 0 , "count", &maxcount
, N_("show only <n> matched refs")),
1095 OPT_STRING( 0 , "format", &format
, N_("format"), N_("format to use for the output")),
1096 OPT_CALLBACK(0 , "sort", sort_tail
, N_("key"),
1097 N_("field name to sort on"), &opt_parse_sort
),
1101 parse_options(argc
, argv
, prefix
, opts
, for_each_ref_usage
, 0);
1103 error("invalid --count argument: `%d'", maxcount
);
1104 usage_with_options(for_each_ref_usage
, opts
);
1106 if (HAS_MULTI_BITS(quote_style
)) {
1107 error("more than one quoting style?");
1108 usage_with_options(for_each_ref_usage
, opts
);
1110 if (verify_format(format
))
1111 usage_with_options(for_each_ref_usage
, opts
);
1114 sort
= default_sort();
1116 /* for warn_ambiguous_refs */
1117 git_config(git_default_config
, NULL
);
1119 memset(&cbdata
, 0, sizeof(cbdata
));
1120 cbdata
.grab_pattern
= argv
;
1121 for_each_rawref(grab_single_ref
, &cbdata
);
1122 refs
= cbdata
.grab_array
;
1123 num_refs
= cbdata
.grab_cnt
;
1125 sort_refs(sort
, refs
, num_refs
);
1127 if (!maxcount
|| num_refs
< maxcount
)
1128 maxcount
= num_refs
;
1129 for (i
= 0; i
< maxcount
; i
++)
1130 show_ref(refs
[i
], format
, quote_style
);