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];
36 struct atom_value
*value
;
45 { "objectsize", FIELD_ULONG
},
49 { "numparent", FIELD_ULONG
},
56 { "authordate", FIELD_TIME
},
60 { "committerdate", FIELD_TIME
},
64 { "taggerdate", FIELD_TIME
},
66 { "creatordate", FIELD_TIME
},
74 * An atom is a valid field atom listed above, possibly prefixed with
75 * a "*" to denote deref_tag().
77 * We parse given format string and sort specifiers, and make a list
78 * of properties that we need to extract out of objects. refinfo
79 * structure will hold an array of values extracted that can be
80 * indexed with the "atom number", which is an index into this
83 static const char **used_atom
;
84 static cmp_type
*used_atom_type
;
85 static int used_atom_cnt
, sort_atom_limit
, need_tagged
;
88 * Used to parse format string and sort specifiers
90 static int parse_atom(const char *atom
, const char *ep
)
96 if (*sp
== '*' && sp
< ep
)
99 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
101 /* Do we have the atom already used elsewhere? */
102 for (i
= 0; i
< used_atom_cnt
; i
++) {
103 int len
= strlen(used_atom
[i
]);
104 if (len
== ep
- atom
&& !memcmp(used_atom
[i
], atom
, len
))
108 /* Is the atom a valid one? */
109 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
110 int len
= strlen(valid_atom
[i
].name
);
112 * If the atom name has a colon, strip it and everything after
113 * it off - it specifies the format for this entry, and
114 * shouldn't be used for checking against the valid_atom
117 const char *formatp
= strchr(sp
, ':');
118 if (!formatp
|| ep
< formatp
)
120 if (len
== formatp
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
124 if (ARRAY_SIZE(valid_atom
) <= i
)
125 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
127 /* Add it in, including the deref prefix */
130 used_atom
= xrealloc(used_atom
,
131 (sizeof *used_atom
) * used_atom_cnt
);
132 used_atom_type
= xrealloc(used_atom_type
,
133 (sizeof(*used_atom_type
) * used_atom_cnt
));
134 used_atom
[at
] = xmemdupz(atom
, ep
- atom
);
135 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
140 * In a format string, find the next occurrence of %(atom).
142 static const char *find_next(const char *cp
)
146 /* %( is the start of an atom;
147 * %% is a quoted per-cent.
151 else if (cp
[1] == '%')
152 cp
++; /* skip over two % */
153 /* otherwise this is a singleton, literal % */
161 * Make sure the format string is well formed, and parse out
164 static int verify_format(const char *format
)
167 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
168 const char *ep
= strchr(sp
, ')');
170 return error("malformed format string %s", sp
);
171 /* sp points at "%(" and ep points at the closing ")" */
172 parse_atom(sp
+ 2, ep
);
179 * Given an object name, read the object data and size, and return a
180 * "struct object". If the object data we are returning is also borrowed
181 * by the "struct object" representation, set *eaten as well---it is a
182 * signal from parse_object_buffer to us not to free the buffer.
184 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
186 enum object_type type
;
187 void *buf
= read_sha1_file(sha1
, &type
, sz
);
190 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
196 /* See grab_values */
197 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
201 for (i
= 0; i
< used_atom_cnt
; i
++) {
202 const char *name
= used_atom
[i
];
203 struct atom_value
*v
= &val
[i
];
204 if (!!deref
!= (*name
== '*'))
208 if (!strcmp(name
, "objecttype"))
209 v
->s
= typename(obj
->type
);
210 else if (!strcmp(name
, "objectsize")) {
211 char *s
= xmalloc(40);
212 sprintf(s
, "%lu", sz
);
216 else if (!strcmp(name
, "objectname")) {
217 char *s
= xmalloc(41);
218 strcpy(s
, sha1_to_hex(obj
->sha1
));
224 /* See grab_values */
225 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
228 struct tag
*tag
= (struct tag
*) obj
;
230 for (i
= 0; i
< used_atom_cnt
; i
++) {
231 const char *name
= used_atom
[i
];
232 struct atom_value
*v
= &val
[i
];
233 if (!!deref
!= (*name
== '*'))
237 if (!strcmp(name
, "tag"))
239 else if (!strcmp(name
, "type") && tag
->tagged
)
240 v
->s
= typename(tag
->tagged
->type
);
241 else if (!strcmp(name
, "object") && tag
->tagged
) {
242 char *s
= xmalloc(41);
243 strcpy(s
, sha1_to_hex(tag
->tagged
->sha1
));
249 static int num_parents(struct commit
*commit
)
251 struct commit_list
*parents
;
254 for (i
= 0, parents
= commit
->parents
;
256 parents
= parents
->next
)
261 /* See grab_values */
262 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
265 struct commit
*commit
= (struct commit
*) 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
, "tree")) {
275 char *s
= xmalloc(41);
276 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
279 if (!strcmp(name
, "numparent")) {
280 char *s
= xmalloc(40);
281 v
->ul
= num_parents(commit
);
282 sprintf(s
, "%lu", v
->ul
);
285 else if (!strcmp(name
, "parent")) {
286 int num
= num_parents(commit
);
288 struct commit_list
*parents
;
289 char *s
= xmalloc(41 * num
+ 1);
291 for (i
= 0, parents
= commit
->parents
;
293 parents
= parents
->next
, i
= i
+ 41) {
294 struct commit
*parent
= parents
->item
;
295 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
305 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
309 if (!strncmp(buf
, who
, wholen
) &&
311 return buf
+ wholen
+ 1;
312 eol
= strchr(buf
, '\n');
317 return ""; /* end of header */
323 static const char *copy_line(const char *buf
)
325 const char *eol
= strchrnul(buf
, '\n');
326 return xmemdupz(buf
, eol
- buf
);
329 static const char *copy_name(const char *buf
)
332 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
333 if (!strncmp(cp
, " <", 2))
334 return xmemdupz(buf
, cp
- buf
);
339 static const char *copy_email(const char *buf
)
341 const char *email
= strchr(buf
, '<');
342 const char *eoemail
= strchr(email
, '>');
343 if (!email
|| !eoemail
)
345 return xmemdupz(email
, eoemail
+ 1 - email
);
348 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
350 const char *eoemail
= strstr(buf
, "> ");
352 unsigned long timestamp
;
354 enum date_mode date_mode
= DATE_NORMAL
;
358 * We got here because atomname ends in "date" or "date<something>";
359 * it's not possible that <something> is not ":<format>" because
360 * parse_atom() wouldn't have allowed it, so we can assume that no
361 * ":" means no format is specified, and use the default.
363 formatp
= strchr(atomname
, ':');
364 if (formatp
!= NULL
) {
366 date_mode
= parse_date_format(formatp
);
371 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
372 if (timestamp
== ULONG_MAX
)
374 tz
= strtol(zone
, NULL
, 10);
375 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
377 v
->s
= xstrdup(show_date(timestamp
, tz
, date_mode
));
385 /* See grab_values */
386 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
389 int wholen
= strlen(who
);
390 const char *wholine
= NULL
;
392 for (i
= 0; i
< used_atom_cnt
; i
++) {
393 const char *name
= used_atom
[i
];
394 struct atom_value
*v
= &val
[i
];
395 if (!!deref
!= (*name
== '*'))
399 if (strncmp(who
, name
, wholen
))
401 if (name
[wholen
] != 0 &&
402 strcmp(name
+ wholen
, "name") &&
403 strcmp(name
+ wholen
, "email") &&
404 prefixcmp(name
+ wholen
, "date"))
407 wholine
= find_wholine(who
, wholen
, buf
, sz
);
409 return; /* no point looking for it */
410 if (name
[wholen
] == 0)
411 v
->s
= copy_line(wholine
);
412 else if (!strcmp(name
+ wholen
, "name"))
413 v
->s
= copy_name(wholine
);
414 else if (!strcmp(name
+ wholen
, "email"))
415 v
->s
= copy_email(wholine
);
416 else if (!prefixcmp(name
+ wholen
, "date"))
417 grab_date(wholine
, v
, name
);
420 /* For a tag or a commit object, if "creator" or "creatordate" is
421 * requested, do something special.
423 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
424 return; /* "author" for commit object is not wanted */
426 wholine
= find_wholine(who
, wholen
, buf
, sz
);
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
== '*'))
437 if (!prefixcmp(name
, "creatordate"))
438 grab_date(wholine
, v
, name
);
439 else if (!strcmp(name
, "creator"))
440 v
->s
= copy_line(wholine
);
444 static void find_subpos(const char *buf
, unsigned long sz
, const char **sub
, const char **body
)
447 const char *eol
= strchr(buf
, '\n');
450 if (eol
[1] == '\n') {
452 break; /* found end of header */
460 *sub
= buf
; /* first non-empty line */
461 buf
= strchr(buf
, '\n');
464 return; /* no body */
467 buf
++; /* skip blank between subject and body */
471 /* See grab_values */
472 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
475 const char *subpos
= NULL
, *bodypos
= NULL
;
477 for (i
= 0; i
< used_atom_cnt
; i
++) {
478 const char *name
= used_atom
[i
];
479 struct atom_value
*v
= &val
[i
];
480 if (!!deref
!= (*name
== '*'))
484 if (strcmp(name
, "subject") &&
485 strcmp(name
, "body") &&
486 strcmp(name
, "contents"))
489 find_subpos(buf
, sz
, &subpos
, &bodypos
);
493 if (!strcmp(name
, "subject"))
494 v
->s
= copy_line(subpos
);
495 else if (!strcmp(name
, "body"))
496 v
->s
= xstrdup(bodypos
);
497 else if (!strcmp(name
, "contents"))
498 v
->s
= xstrdup(subpos
);
502 /* We want to have empty print-string for field requests
503 * that do not apply (e.g. "authordate" for a tag object)
505 static void fill_missing_values(struct atom_value
*val
)
508 for (i
= 0; i
< used_atom_cnt
; i
++) {
509 struct atom_value
*v
= &val
[i
];
516 * val is a list of atom_value to hold returned values. Extract
517 * the values for atoms in used_atom array out of (obj, buf, sz).
518 * when deref is false, (obj, buf, sz) is the object that is
519 * pointed at by the ref itself; otherwise it is the object the
520 * ref (which is a tag) refers to.
522 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
524 grab_common_values(val
, deref
, obj
, buf
, sz
);
527 grab_tag_values(val
, deref
, obj
, buf
, sz
);
528 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
529 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
532 grab_commit_values(val
, deref
, obj
, buf
, sz
);
533 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
534 grab_person("author", val
, deref
, obj
, buf
, sz
);
535 grab_person("committer", val
, deref
, obj
, buf
, sz
);
538 // grab_tree_values(val, deref, obj, buf, sz);
541 // grab_blob_values(val, deref, obj, buf, sz);
544 die("Eh? Object of type %d?", obj
->type
);
549 * Parse the object referred by ref, and grab needed value.
551 static void populate_value(struct refinfo
*ref
)
557 const unsigned char *tagged
;
559 ref
->value
= xcalloc(sizeof(struct atom_value
), used_atom_cnt
);
561 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
563 die("missing object %s for %s",
564 sha1_to_hex(ref
->objectname
), ref
->refname
);
566 die("parse_object_buffer failed on %s for %s",
567 sha1_to_hex(ref
->objectname
), ref
->refname
);
569 /* Fill in specials first */
570 for (i
= 0; i
< used_atom_cnt
; i
++) {
571 const char *name
= used_atom
[i
];
572 struct atom_value
*v
= &ref
->value
[i
];
582 if (!prefixcmp(name
, "refname"))
583 refname
= ref
->refname
;
584 else if(!prefixcmp(name
, "upstream")) {
585 struct branch
*branch
;
586 /* only local branches may have an upstream */
587 if (prefixcmp(ref
->refname
, "refs/heads/"))
589 branch
= branch_get(ref
->refname
+ 11);
591 if (!branch
|| !branch
->merge
|| !branch
->merge
[0] ||
592 !branch
->merge
[0]->dst
)
594 refname
= branch
->merge
[0]->dst
;
599 formatp
= strchr(name
, ':');
600 /* look for "short" refname format */
603 if (!strcmp(formatp
, "short"))
604 refname
= shorten_unambiguous_ref(refname
,
605 warn_ambiguous_refs
);
607 die("unknown %.*s format %s",
608 (int)(formatp
- name
), name
, formatp
);
614 int len
= strlen(refname
);
615 char *s
= xmalloc(len
+ 4);
616 sprintf(s
, "%s^{}", refname
);
621 grab_values(ref
->value
, 0, obj
, buf
, size
);
625 /* If there is no atom that wants to know about tagged
626 * object, we are done.
628 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
631 /* If it is a tag object, see if we use a value that derefs
632 * the object, and if we do grab the object it refers to.
634 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
636 /* NEEDSWORK: This derefs tag only once, which
637 * is good to deal with chains of trust, but
638 * is not consistent with what deref_tag() does
639 * which peels the onion to the core.
641 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
643 die("missing object %s for %s",
644 sha1_to_hex(tagged
), ref
->refname
);
646 die("parse_object_buffer failed on %s for %s",
647 sha1_to_hex(tagged
), ref
->refname
);
648 grab_values(ref
->value
, 1, obj
, buf
, size
);
654 * Given a ref, return the value for the atom. This lazily gets value
655 * out of the object by calling populate value.
657 static void get_value(struct refinfo
*ref
, int atom
, struct atom_value
**v
)
661 fill_missing_values(ref
->value
);
663 *v
= &ref
->value
[atom
];
666 struct grab_ref_cbdata
{
667 struct refinfo
**grab_array
;
668 const char **grab_pattern
;
673 * A call-back given to for_each_ref(). It is unfortunate that we
674 * need to use global variables to pass extra information to this
677 static int grab_single_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
679 struct grab_ref_cbdata
*cb
= cb_data
;
683 if (*cb
->grab_pattern
) {
684 const char **pattern
;
685 int namelen
= strlen(refname
);
686 for (pattern
= cb
->grab_pattern
; *pattern
; pattern
++) {
687 const char *p
= *pattern
;
688 int plen
= strlen(p
);
690 if ((plen
<= namelen
) &&
691 !strncmp(refname
, p
, plen
) &&
692 (refname
[plen
] == '\0' ||
693 refname
[plen
] == '/' ||
696 if (!fnmatch(p
, refname
, FNM_PATHNAME
))
703 /* We do not open the object yet; sort may only need refname
704 * to do its job and the resulting list may yet to be pruned
707 ref
= xcalloc(1, sizeof(*ref
));
708 ref
->refname
= xstrdup(refname
);
709 hashcpy(ref
->objectname
, sha1
);
712 cb
->grab_array
= xrealloc(cb
->grab_array
,
713 sizeof(*cb
->grab_array
) * (cnt
+ 1));
714 cb
->grab_array
[cnt
++] = ref
;
719 static int cmp_ref_sort(struct ref_sort
*s
, struct refinfo
*a
, struct refinfo
*b
)
721 struct atom_value
*va
, *vb
;
723 cmp_type cmp_type
= used_atom_type
[s
->atom
];
725 get_value(a
, s
->atom
, &va
);
726 get_value(b
, s
->atom
, &vb
);
729 cmp
= strcmp(va
->s
, vb
->s
);
734 else if (va
->ul
== vb
->ul
)
740 return (s
->reverse
) ? -cmp
: cmp
;
743 static struct ref_sort
*ref_sort
;
744 static int compare_refs(const void *a_
, const void *b_
)
746 struct refinfo
*a
= *((struct refinfo
**)a_
);
747 struct refinfo
*b
= *((struct refinfo
**)b_
);
750 for (s
= ref_sort
; s
; s
= s
->next
) {
751 int cmp
= cmp_ref_sort(s
, a
, b
);
758 static void sort_refs(struct ref_sort
*sort
, struct refinfo
**refs
, int num_refs
)
761 qsort(refs
, num_refs
, sizeof(struct refinfo
*), compare_refs
);
764 static void print_value(struct refinfo
*ref
, int atom
, int quote_style
)
766 struct atom_value
*v
;
767 get_value(ref
, atom
, &v
);
768 switch (quote_style
) {
773 sq_quote_print(stdout
, v
->s
);
776 perl_quote_print(stdout
, v
->s
);
779 python_quote_print(stdout
, v
->s
);
782 tcl_quote_print(stdout
, v
->s
);
787 static int hex1(char ch
)
789 if ('0' <= ch
&& ch
<= '9')
791 else if ('a' <= ch
&& ch
<= 'f')
792 return ch
- 'a' + 10;
793 else if ('A' <= ch
&& ch
<= 'F')
794 return ch
- 'A' + 10;
797 static int hex2(const char *cp
)
800 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
805 static void emit(const char *cp
, const char *ep
)
807 while (*cp
&& (!ep
|| cp
< ep
)) {
812 int ch
= hex2(cp
+ 1);
825 static void show_ref(struct refinfo
*info
, const char *format
, int quote_style
)
827 const char *cp
, *sp
, *ep
;
829 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
830 ep
= strchr(sp
, ')');
833 print_value(info
, parse_atom(sp
+ 2, ep
), quote_style
);
836 sp
= cp
+ strlen(cp
);
842 static struct ref_sort
*default_sort(void)
844 static const char cstr_name
[] = "refname";
846 struct ref_sort
*sort
= xcalloc(1, sizeof(*sort
));
849 sort
->atom
= parse_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
853 static int opt_parse_sort(const struct option
*opt
, const char *arg
, int unset
)
855 struct ref_sort
**sort_tail
= opt
->value
;
859 if (!arg
) /* should --no-sort void the list ? */
862 *sort_tail
= s
= xcalloc(1, sizeof(*s
));
869 s
->atom
= parse_atom(arg
, arg
+len
);
873 static char const * const for_each_ref_usage
[] = {
874 "git for-each-ref [options] [<pattern>]",
878 int cmd_for_each_ref(int argc
, const char **argv
, const char *prefix
)
881 const char *format
= "%(objectname) %(objecttype)\t%(refname)";
882 struct ref_sort
*sort
= NULL
, **sort_tail
= &sort
;
883 int maxcount
= 0, quote_style
= 0;
884 struct refinfo
**refs
;
885 struct grab_ref_cbdata cbdata
;
887 struct option opts
[] = {
888 OPT_BIT('s', "shell", "e_style
,
889 "quote placeholders suitably for shells", QUOTE_SHELL
),
890 OPT_BIT('p', "perl", "e_style
,
891 "quote placeholders suitably for perl", QUOTE_PERL
),
892 OPT_BIT(0 , "python", "e_style
,
893 "quote placeholders suitably for python", QUOTE_PYTHON
),
894 OPT_BIT(0 , "tcl", "e_style
,
895 "quote placeholders suitably for tcl", QUOTE_TCL
),
898 OPT_INTEGER( 0 , "count", &maxcount
, "show only <n> matched refs"),
899 OPT_STRING( 0 , "format", &format
, "format", "format to use for the output"),
900 OPT_CALLBACK(0 , "sort", sort_tail
, "key",
901 "field name to sort on", &opt_parse_sort
),
905 parse_options(argc
, argv
, opts
, for_each_ref_usage
, 0);
907 error("invalid --count argument: `%d'", maxcount
);
908 usage_with_options(for_each_ref_usage
, opts
);
910 if (HAS_MULTI_BITS(quote_style
)) {
911 error("more than one quoting style?");
912 usage_with_options(for_each_ref_usage
, opts
);
914 if (verify_format(format
))
915 usage_with_options(for_each_ref_usage
, opts
);
918 sort
= default_sort();
919 sort_atom_limit
= used_atom_cnt
;
921 /* for warn_ambiguous_refs */
922 git_config(git_default_config
, NULL
);
924 memset(&cbdata
, 0, sizeof(cbdata
));
925 cbdata
.grab_pattern
= argv
;
926 for_each_ref(grab_single_ref
, &cbdata
);
927 refs
= cbdata
.grab_array
;
928 num_refs
= cbdata
.grab_cnt
;
930 for (i
= 0; i
< used_atom_cnt
; i
++) {
931 if (used_atom
[i
][0] == '*') {
937 sort_refs(sort
, refs
, num_refs
);
939 if (!maxcount
|| num_refs
< maxcount
)
941 for (i
= 0; i
< maxcount
; i
++)
942 show_ref(refs
[i
], format
, quote_style
);