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
},
78 * An atom is a valid field atom listed above, possibly prefixed with
79 * a "*" to denote deref_tag().
81 * We parse given format string and sort specifiers, and make a list
82 * of properties that we need to extract out of objects. refinfo
83 * structure will hold an array of values extracted that can be
84 * indexed with the "atom number", which is an index into this
87 static const char **used_atom
;
88 static cmp_type
*used_atom_type
;
89 static int used_atom_cnt
, sort_atom_limit
, need_tagged
, need_symref
;
92 * Used to parse format string and sort specifiers
94 static int parse_atom(const char *atom
, const char *ep
)
100 if (*sp
== '*' && sp
< ep
)
103 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
105 /* Do we have the atom already used elsewhere? */
106 for (i
= 0; i
< used_atom_cnt
; i
++) {
107 int len
= strlen(used_atom
[i
]);
108 if (len
== ep
- atom
&& !memcmp(used_atom
[i
], atom
, len
))
112 /* Is the atom a valid one? */
113 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
114 int len
= strlen(valid_atom
[i
].name
);
116 * If the atom name has a colon, strip it and everything after
117 * it off - it specifies the format for this entry, and
118 * shouldn't be used for checking against the valid_atom
121 const char *formatp
= strchr(sp
, ':');
122 if (!formatp
|| ep
< formatp
)
124 if (len
== formatp
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
128 if (ARRAY_SIZE(valid_atom
) <= i
)
129 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
131 /* Add it in, including the deref prefix */
134 used_atom
= xrealloc(used_atom
,
135 (sizeof *used_atom
) * used_atom_cnt
);
136 used_atom_type
= xrealloc(used_atom_type
,
137 (sizeof(*used_atom_type
) * used_atom_cnt
));
138 used_atom
[at
] = xmemdupz(atom
, ep
- atom
);
139 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
142 if (!strcmp(used_atom
[at
], "symref"))
148 * In a format string, find the next occurrence of %(atom).
150 static const char *find_next(const char *cp
)
155 * %( is the start of an atom;
156 * %% is a quoted per-cent.
160 else if (cp
[1] == '%')
161 cp
++; /* skip over two % */
162 /* otherwise this is a singleton, literal % */
170 * Make sure the format string is well formed, and parse out
173 static int verify_format(const char *format
)
176 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
177 const char *ep
= strchr(sp
, ')');
179 return error("malformed format string %s", sp
);
180 /* sp points at "%(" and ep points at the closing ")" */
181 parse_atom(sp
+ 2, ep
);
188 * Given an object name, read the object data and size, and return a
189 * "struct object". If the object data we are returning is also borrowed
190 * by the "struct object" representation, set *eaten as well---it is a
191 * signal from parse_object_buffer to us not to free the buffer.
193 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
195 enum object_type type
;
196 void *buf
= read_sha1_file(sha1
, &type
, sz
);
199 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
205 /* See grab_values */
206 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
210 for (i
= 0; i
< used_atom_cnt
; i
++) {
211 const char *name
= used_atom
[i
];
212 struct atom_value
*v
= &val
[i
];
213 if (!!deref
!= (*name
== '*'))
217 if (!strcmp(name
, "objecttype"))
218 v
->s
= typename(obj
->type
);
219 else if (!strcmp(name
, "objectsize")) {
220 char *s
= xmalloc(40);
221 sprintf(s
, "%lu", sz
);
225 else if (!strcmp(name
, "objectname")) {
226 char *s
= xmalloc(41);
227 strcpy(s
, sha1_to_hex(obj
->sha1
));
230 else if (!strcmp(name
, "objectname:short")) {
231 v
->s
= find_unique_abbrev(obj
->sha1
, DEFAULT_ABBREV
);
236 /* See grab_values */
237 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
240 struct tag
*tag
= (struct tag
*) obj
;
242 for (i
= 0; i
< used_atom_cnt
; i
++) {
243 const char *name
= used_atom
[i
];
244 struct atom_value
*v
= &val
[i
];
245 if (!!deref
!= (*name
== '*'))
249 if (!strcmp(name
, "tag"))
251 else if (!strcmp(name
, "type") && tag
->tagged
)
252 v
->s
= typename(tag
->tagged
->type
);
253 else if (!strcmp(name
, "object") && tag
->tagged
) {
254 char *s
= xmalloc(41);
255 strcpy(s
, sha1_to_hex(tag
->tagged
->sha1
));
261 static int num_parents(struct commit
*commit
)
263 struct commit_list
*parents
;
266 for (i
= 0, parents
= commit
->parents
;
268 parents
= parents
->next
)
273 /* See grab_values */
274 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
277 struct commit
*commit
= (struct commit
*) obj
;
279 for (i
= 0; i
< used_atom_cnt
; i
++) {
280 const char *name
= used_atom
[i
];
281 struct atom_value
*v
= &val
[i
];
282 if (!!deref
!= (*name
== '*'))
286 if (!strcmp(name
, "tree")) {
287 char *s
= xmalloc(41);
288 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
291 if (!strcmp(name
, "numparent")) {
292 char *s
= xmalloc(40);
293 v
->ul
= num_parents(commit
);
294 sprintf(s
, "%lu", v
->ul
);
297 else if (!strcmp(name
, "parent")) {
298 int num
= num_parents(commit
);
300 struct commit_list
*parents
;
301 char *s
= xmalloc(41 * num
+ 1);
303 for (i
= 0, parents
= commit
->parents
;
305 parents
= parents
->next
, i
= i
+ 41) {
306 struct commit
*parent
= parents
->item
;
307 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
317 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
321 if (!strncmp(buf
, who
, wholen
) &&
323 return buf
+ wholen
+ 1;
324 eol
= strchr(buf
, '\n');
329 return ""; /* end of header */
335 static const char *copy_line(const char *buf
)
337 const char *eol
= strchrnul(buf
, '\n');
338 return xmemdupz(buf
, eol
- buf
);
341 static const char *copy_name(const char *buf
)
344 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
345 if (!strncmp(cp
, " <", 2))
346 return xmemdupz(buf
, cp
- buf
);
351 static const char *copy_email(const char *buf
)
353 const char *email
= strchr(buf
, '<');
357 eoemail
= strchr(email
, '>');
360 return xmemdupz(email
, eoemail
+ 1 - email
);
363 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
365 const char *eoemail
= strstr(buf
, "> ");
367 unsigned long timestamp
;
369 enum date_mode date_mode
= DATE_NORMAL
;
373 * We got here because atomname ends in "date" or "date<something>";
374 * it's not possible that <something> is not ":<format>" because
375 * parse_atom() wouldn't have allowed it, so we can assume that no
376 * ":" means no format is specified, and use the default.
378 formatp
= strchr(atomname
, ':');
379 if (formatp
!= NULL
) {
381 date_mode
= parse_date_format(formatp
);
386 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
387 if (timestamp
== ULONG_MAX
)
389 tz
= strtol(zone
, NULL
, 10);
390 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
392 v
->s
= xstrdup(show_date(timestamp
, tz
, date_mode
));
400 /* See grab_values */
401 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
404 int wholen
= strlen(who
);
405 const char *wholine
= NULL
;
407 for (i
= 0; i
< used_atom_cnt
; i
++) {
408 const char *name
= used_atom
[i
];
409 struct atom_value
*v
= &val
[i
];
410 if (!!deref
!= (*name
== '*'))
414 if (strncmp(who
, name
, wholen
))
416 if (name
[wholen
] != 0 &&
417 strcmp(name
+ wholen
, "name") &&
418 strcmp(name
+ wholen
, "email") &&
419 prefixcmp(name
+ wholen
, "date"))
422 wholine
= find_wholine(who
, wholen
, buf
, sz
);
424 return; /* no point looking for it */
425 if (name
[wholen
] == 0)
426 v
->s
= copy_line(wholine
);
427 else if (!strcmp(name
+ wholen
, "name"))
428 v
->s
= copy_name(wholine
);
429 else if (!strcmp(name
+ wholen
, "email"))
430 v
->s
= copy_email(wholine
);
431 else if (!prefixcmp(name
+ wholen
, "date"))
432 grab_date(wholine
, v
, name
);
436 * For a tag or a commit object, if "creator" or "creatordate" is
437 * requested, do something special.
439 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
440 return; /* "author" for commit object is not wanted */
442 wholine
= find_wholine(who
, wholen
, buf
, sz
);
445 for (i
= 0; i
< used_atom_cnt
; i
++) {
446 const char *name
= used_atom
[i
];
447 struct atom_value
*v
= &val
[i
];
448 if (!!deref
!= (*name
== '*'))
453 if (!prefixcmp(name
, "creatordate"))
454 grab_date(wholine
, v
, name
);
455 else if (!strcmp(name
, "creator"))
456 v
->s
= copy_line(wholine
);
460 static void find_subpos(const char *buf
, unsigned long sz
, const char **sub
, const char **body
)
463 const char *eol
= strchr(buf
, '\n');
466 if (eol
[1] == '\n') {
468 break; /* found end of header */
476 *sub
= buf
; /* first non-empty line */
477 buf
= strchr(buf
, '\n');
480 return; /* no body */
483 buf
++; /* skip blank between subject and body */
487 /* See grab_values */
488 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
491 const char *subpos
= NULL
, *bodypos
= NULL
;
493 for (i
= 0; i
< used_atom_cnt
; i
++) {
494 const char *name
= used_atom
[i
];
495 struct atom_value
*v
= &val
[i
];
496 if (!!deref
!= (*name
== '*'))
500 if (strcmp(name
, "subject") &&
501 strcmp(name
, "body") &&
502 strcmp(name
, "contents"))
505 find_subpos(buf
, sz
, &subpos
, &bodypos
);
509 if (!strcmp(name
, "subject"))
510 v
->s
= copy_line(subpos
);
511 else if (!strcmp(name
, "body"))
512 v
->s
= xstrdup(bodypos
);
513 else if (!strcmp(name
, "contents"))
514 v
->s
= xstrdup(subpos
);
519 * We want to have empty print-string for field requests
520 * that do not apply (e.g. "authordate" for a tag object)
522 static void fill_missing_values(struct atom_value
*val
)
525 for (i
= 0; i
< used_atom_cnt
; i
++) {
526 struct atom_value
*v
= &val
[i
];
533 * val is a list of atom_value to hold returned values. Extract
534 * the values for atoms in used_atom array out of (obj, buf, sz).
535 * when deref is false, (obj, buf, sz) is the object that is
536 * pointed at by the ref itself; otherwise it is the object the
537 * ref (which is a tag) refers to.
539 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
541 grab_common_values(val
, deref
, obj
, buf
, sz
);
544 grab_tag_values(val
, deref
, obj
, buf
, sz
);
545 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
546 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
549 grab_commit_values(val
, deref
, obj
, buf
, sz
);
550 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
551 grab_person("author", val
, deref
, obj
, buf
, sz
);
552 grab_person("committer", val
, deref
, obj
, buf
, sz
);
555 /* grab_tree_values(val, deref, obj, buf, sz); */
558 /* grab_blob_values(val, deref, obj, buf, sz); */
561 die("Eh? Object of type %d?", obj
->type
);
565 static inline char *copy_advance(char *dst
, const char *src
)
573 * Parse the object referred by ref, and grab needed value.
575 static void populate_value(struct refinfo
*ref
)
581 const unsigned char *tagged
;
583 ref
->value
= xcalloc(sizeof(struct atom_value
), used_atom_cnt
);
585 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
586 unsigned char unused1
[20];
588 symref
= resolve_ref(ref
->refname
, unused1
, 1, NULL
);
590 ref
->symref
= xstrdup(symref
);
595 /* Fill in specials first */
596 for (i
= 0; i
< used_atom_cnt
; i
++) {
597 const char *name
= used_atom
[i
];
598 struct atom_value
*v
= &ref
->value
[i
];
608 if (!prefixcmp(name
, "refname"))
609 refname
= ref
->refname
;
610 else if (!prefixcmp(name
, "symref"))
611 refname
= ref
->symref
? ref
->symref
: "";
612 else if (!prefixcmp(name
, "upstream")) {
613 struct branch
*branch
;
614 /* only local branches may have an upstream */
615 if (prefixcmp(ref
->refname
, "refs/heads/"))
617 branch
= branch_get(ref
->refname
+ 11);
619 if (!branch
|| !branch
->merge
|| !branch
->merge
[0] ||
620 !branch
->merge
[0]->dst
)
622 refname
= branch
->merge
[0]->dst
;
624 else if (!strcmp(name
, "flag")) {
625 char buf
[256], *cp
= buf
;
626 if (ref
->flag
& REF_ISSYMREF
)
627 cp
= copy_advance(cp
, ",symref");
628 if (ref
->flag
& REF_ISPACKED
)
629 cp
= copy_advance(cp
, ",packed");
634 v
->s
= xstrdup(buf
+ 1);
641 formatp
= strchr(name
, ':');
642 /* look for "short" refname format */
645 if (!strcmp(formatp
, "short"))
646 refname
= shorten_unambiguous_ref(refname
,
647 warn_ambiguous_refs
);
649 die("unknown %.*s format %s",
650 (int)(formatp
- name
), name
, formatp
);
656 int len
= strlen(refname
);
657 char *s
= xmalloc(len
+ 4);
658 sprintf(s
, "%s^{}", refname
);
663 for (i
= 0; i
< used_atom_cnt
; i
++) {
664 struct atom_value
*v
= &ref
->value
[i
];
671 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
673 die("missing object %s for %s",
674 sha1_to_hex(ref
->objectname
), ref
->refname
);
676 die("parse_object_buffer failed on %s for %s",
677 sha1_to_hex(ref
->objectname
), ref
->refname
);
679 grab_values(ref
->value
, 0, obj
, buf
, size
);
684 * If there is no atom that wants to know about tagged
685 * object, we are done.
687 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
691 * If it is a tag object, see if we use a value that derefs
692 * the object, and if we do grab the object it refers to.
694 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
697 * NEEDSWORK: This derefs tag only once, which
698 * is good to deal with chains of trust, but
699 * is not consistent with what deref_tag() does
700 * which peels the onion to the core.
702 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
704 die("missing object %s for %s",
705 sha1_to_hex(tagged
), ref
->refname
);
707 die("parse_object_buffer failed on %s for %s",
708 sha1_to_hex(tagged
), ref
->refname
);
709 grab_values(ref
->value
, 1, obj
, buf
, size
);
715 * Given a ref, return the value for the atom. This lazily gets value
716 * out of the object by calling populate value.
718 static void get_value(struct refinfo
*ref
, int atom
, struct atom_value
**v
)
722 fill_missing_values(ref
->value
);
724 *v
= &ref
->value
[atom
];
727 struct grab_ref_cbdata
{
728 struct refinfo
**grab_array
;
729 const char **grab_pattern
;
734 * A call-back given to for_each_ref(). Filter refs and keep them for
735 * later object processing.
737 static int grab_single_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
739 struct grab_ref_cbdata
*cb
= cb_data
;
743 if (*cb
->grab_pattern
) {
744 const char **pattern
;
745 int namelen
= strlen(refname
);
746 for (pattern
= cb
->grab_pattern
; *pattern
; pattern
++) {
747 const char *p
= *pattern
;
748 int plen
= strlen(p
);
750 if ((plen
<= namelen
) &&
751 !strncmp(refname
, p
, plen
) &&
752 (refname
[plen
] == '\0' ||
753 refname
[plen
] == '/' ||
756 if (!fnmatch(p
, refname
, FNM_PATHNAME
))
764 * We do not open the object yet; sort may only need refname
765 * to do its job and the resulting list may yet to be pruned
768 ref
= xcalloc(1, sizeof(*ref
));
769 ref
->refname
= xstrdup(refname
);
770 hashcpy(ref
->objectname
, sha1
);
774 cb
->grab_array
= xrealloc(cb
->grab_array
,
775 sizeof(*cb
->grab_array
) * (cnt
+ 1));
776 cb
->grab_array
[cnt
++] = ref
;
781 static int cmp_ref_sort(struct ref_sort
*s
, struct refinfo
*a
, struct refinfo
*b
)
783 struct atom_value
*va
, *vb
;
785 cmp_type cmp_type
= used_atom_type
[s
->atom
];
787 get_value(a
, s
->atom
, &va
);
788 get_value(b
, s
->atom
, &vb
);
791 cmp
= strcmp(va
->s
, vb
->s
);
796 else if (va
->ul
== vb
->ul
)
802 return (s
->reverse
) ? -cmp
: cmp
;
805 static struct ref_sort
*ref_sort
;
806 static int compare_refs(const void *a_
, const void *b_
)
808 struct refinfo
*a
= *((struct refinfo
**)a_
);
809 struct refinfo
*b
= *((struct refinfo
**)b_
);
812 for (s
= ref_sort
; s
; s
= s
->next
) {
813 int cmp
= cmp_ref_sort(s
, a
, b
);
820 static void sort_refs(struct ref_sort
*sort
, struct refinfo
**refs
, int num_refs
)
823 qsort(refs
, num_refs
, sizeof(struct refinfo
*), compare_refs
);
826 static void print_value(struct refinfo
*ref
, int atom
, int quote_style
)
828 struct atom_value
*v
;
829 get_value(ref
, atom
, &v
);
830 switch (quote_style
) {
835 sq_quote_print(stdout
, v
->s
);
838 perl_quote_print(stdout
, v
->s
);
841 python_quote_print(stdout
, v
->s
);
844 tcl_quote_print(stdout
, v
->s
);
849 static int hex1(char ch
)
851 if ('0' <= ch
&& ch
<= '9')
853 else if ('a' <= ch
&& ch
<= 'f')
854 return ch
- 'a' + 10;
855 else if ('A' <= ch
&& ch
<= 'F')
856 return ch
- 'A' + 10;
859 static int hex2(const char *cp
)
862 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
867 static void emit(const char *cp
, const char *ep
)
869 while (*cp
&& (!ep
|| cp
< ep
)) {
874 int ch
= hex2(cp
+ 1);
887 static void show_ref(struct refinfo
*info
, const char *format
, int quote_style
)
889 const char *cp
, *sp
, *ep
;
891 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
892 ep
= strchr(sp
, ')');
895 print_value(info
, parse_atom(sp
+ 2, ep
), quote_style
);
898 sp
= cp
+ strlen(cp
);
904 static struct ref_sort
*default_sort(void)
906 static const char cstr_name
[] = "refname";
908 struct ref_sort
*sort
= xcalloc(1, sizeof(*sort
));
911 sort
->atom
= parse_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
915 static int opt_parse_sort(const struct option
*opt
, const char *arg
, int unset
)
917 struct ref_sort
**sort_tail
= opt
->value
;
921 if (!arg
) /* should --no-sort void the list ? */
924 *sort_tail
= s
= xcalloc(1, sizeof(*s
));
931 s
->atom
= parse_atom(arg
, arg
+len
);
935 static char const * const for_each_ref_usage
[] = {
936 "git for-each-ref [options] [<pattern>]",
940 int cmd_for_each_ref(int argc
, const char **argv
, const char *prefix
)
943 const char *format
= "%(objectname) %(objecttype)\t%(refname)";
944 struct ref_sort
*sort
= NULL
, **sort_tail
= &sort
;
945 int maxcount
= 0, quote_style
= 0;
946 struct refinfo
**refs
;
947 struct grab_ref_cbdata cbdata
;
949 struct option opts
[] = {
950 OPT_BIT('s', "shell", "e_style
,
951 "quote placeholders suitably for shells", QUOTE_SHELL
),
952 OPT_BIT('p', "perl", "e_style
,
953 "quote placeholders suitably for perl", QUOTE_PERL
),
954 OPT_BIT(0 , "python", "e_style
,
955 "quote placeholders suitably for python", QUOTE_PYTHON
),
956 OPT_BIT(0 , "tcl", "e_style
,
957 "quote placeholders suitably for tcl", QUOTE_TCL
),
960 OPT_INTEGER( 0 , "count", &maxcount
, "show only <n> matched refs"),
961 OPT_STRING( 0 , "format", &format
, "format", "format to use for the output"),
962 OPT_CALLBACK(0 , "sort", sort_tail
, "key",
963 "field name to sort on", &opt_parse_sort
),
967 parse_options(argc
, argv
, prefix
, opts
, for_each_ref_usage
, 0);
969 error("invalid --count argument: `%d'", maxcount
);
970 usage_with_options(for_each_ref_usage
, opts
);
972 if (HAS_MULTI_BITS(quote_style
)) {
973 error("more than one quoting style?");
974 usage_with_options(for_each_ref_usage
, opts
);
976 if (verify_format(format
))
977 usage_with_options(for_each_ref_usage
, opts
);
980 sort
= default_sort();
981 sort_atom_limit
= used_atom_cnt
;
983 /* for warn_ambiguous_refs */
984 git_config(git_default_config
, NULL
);
986 memset(&cbdata
, 0, sizeof(cbdata
));
987 cbdata
.grab_pattern
= argv
;
988 for_each_rawref(grab_single_ref
, &cbdata
);
989 refs
= cbdata
.grab_array
;
990 num_refs
= cbdata
.grab_cnt
;
992 sort_refs(sort
, refs
, num_refs
);
994 if (!maxcount
|| num_refs
< maxcount
)
996 for (i
= 0; i
< maxcount
; i
++)
997 show_ref(refs
[i
], format
, quote_style
);