15 #define QUOTE_PYTHON 3
18 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
22 unsigned long ul; /* used for sorting when not FIELD_STR */
26 struct ref_sort *next;
27 int atom; /* index into used_atom array */
33 unsigned char objectname[20];
34 struct atom_value *value;
43 { "objectsize", FIELD_ULONG },
46 { "parent" }, /* NEEDSWORK: how to address 2nd and later parents? */
47 { "numparent", FIELD_ULONG },
54 { "authordate", FIELD_TIME },
58 { "committerdate", FIELD_TIME },
62 { "taggerdate", FIELD_TIME },
64 { "creatordate", FIELD_TIME },
71 * An atom is a valid field atom listed above, possibly prefixed with
72 * a "*" to denote deref_tag().
74 * We parse given format string and sort specifiers, and make a list
75 * of properties that we need to extract out of objects. refinfo
76 * structure will hold an array of values extracted that can be
77 * indexed with the "atom number", which is an index into this
80 static const char **used_atom;
81 static cmp_type *used_atom_type;
82 static int used_atom_cnt, sort_atom_limit, need_tagged;
85 * Used to parse format string and sort specifiers
87 static int parse_atom(const char *atom, const char *ep)
94 if (*sp == '*' && sp < ep)
97 die("malformed field name: %.*s", (int)(ep-atom), atom);
99 /* Do we have the atom already used elsewhere? */
100 for (i = 0; i < used_atom_cnt; i++) {
101 int len = strlen(used_atom[i]);
102 if (len == ep - atom && !memcmp(used_atom[i], atom, len))
106 /* Is the atom a valid one? */
107 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
108 int len = strlen(valid_atom[i].name);
109 if (len == ep - sp && !memcmp(valid_atom[i].name, sp, len))
113 if (ARRAY_SIZE(valid_atom) <= i)
114 die("unknown field name: %.*s", (int)(ep-atom), atom);
116 /* Add it in, including the deref prefix */
119 used_atom = xrealloc(used_atom,
120 (sizeof *used_atom) * used_atom_cnt);
121 used_atom_type = xrealloc(used_atom_type,
122 (sizeof(*used_atom_type) * used_atom_cnt));
123 n = xmalloc(ep - atom + 1);
124 memcpy(n, atom, ep - atom);
127 used_atom_type[at] = valid_atom[i].cmp_type;
132 * In a format string, find the next occurrence of %(atom).
134 static const char *find_next(const char *cp)
138 /* %( is the start of an atom;
139 * %% is a quoted per-cent.
143 else if (cp[1] == '%')
144 cp++; /* skip over two % */
145 /* otherwise this is a singleton, literal % */
153 * Make sure the format string is well formed, and parse out
156 static void verify_format(const char *format)
159 for (cp = format; *cp && (sp = find_next(cp)); ) {
160 const char *ep = strchr(sp, ')');
162 die("malformatted format string %s", sp);
163 /* sp points at "%(" and ep points at the closing ")" */
164 parse_atom(sp + 2, ep);
170 * Given an object name, read the object data and size, and return a
171 * "struct object". If the object data we are returning is also borrowed
172 * by the "struct object" representation, set *eaten as well---it is a
173 * signal from parse_object_buffer to us not to free the buffer.
175 static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
177 enum object_type type;
178 void *buf = read_sha1_file(sha1, &type, sz);
181 *obj = parse_object_buffer(sha1, type, *sz, buf, eaten);
187 /* See grab_values */
188 static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
192 for (i = 0; i < used_atom_cnt; i++) {
193 const char *name = used_atom[i];
194 struct atom_value *v = &val[i];
195 if (!!deref != (*name == '*'))
199 if (!strcmp(name, "objecttype"))
200 v->s = typename(obj->type);
201 else if (!strcmp(name, "objectsize")) {
202 char *s = xmalloc(40);
203 sprintf(s, "%lu", sz);
207 else if (!strcmp(name, "objectname")) {
208 char *s = xmalloc(41);
209 strcpy(s, sha1_to_hex(obj->sha1));
215 /* See grab_values */
216 static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
219 struct tag *tag = (struct tag *) obj;
221 for (i = 0; i < used_atom_cnt; i++) {
222 const char *name = used_atom[i];
223 struct atom_value *v = &val[i];
224 if (!!deref != (*name == '*'))
228 if (!strcmp(name, "tag"))
233 static int num_parents(struct commit *commit)
235 struct commit_list *parents;
238 for (i = 0, parents = commit->parents;
240 parents = parents->next)
245 /* See grab_values */
246 static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
249 struct commit *commit = (struct commit *) obj;
251 for (i = 0; i < used_atom_cnt; i++) {
252 const char *name = used_atom[i];
253 struct atom_value *v = &val[i];
254 if (!!deref != (*name == '*'))
258 if (!strcmp(name, "tree")) {
259 char *s = xmalloc(41);
260 strcpy(s, sha1_to_hex(commit->tree->object.sha1));
263 if (!strcmp(name, "numparent")) {
264 char *s = xmalloc(40);
265 sprintf(s, "%lu", v->ul);
267 v->ul = num_parents(commit);
269 else if (!strcmp(name, "parent")) {
270 int num = num_parents(commit);
272 struct commit_list *parents;
273 char *s = xmalloc(42 * num);
275 for (i = 0, parents = commit->parents;
277 parents = parents->next, i = i + 42) {
278 struct commit *parent = parents->item;
279 strcpy(s+i, sha1_to_hex(parent->object.sha1));
287 static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz)
291 if (!strncmp(buf, who, wholen) &&
293 return buf + wholen + 1;
294 eol = strchr(buf, '\n');
299 return ""; /* end of header */
305 static const char *copy_line(const char *buf)
307 const char *eol = strchr(buf, '\n');
313 line = xmalloc(len + 1);
314 memcpy(line, buf, len);
319 static const char *copy_name(const char *buf)
321 const char *eol = strchr(buf, '\n');
322 const char *eoname = strstr(buf, " <");
325 if (!(eoname && eol && eoname < eol))
328 line = xmalloc(len + 1);
329 memcpy(line, buf, len);
334 static const char *copy_email(const char *buf)
336 const char *email = strchr(buf, '<');
337 const char *eoemail = strchr(email, '>');
340 if (!email || !eoemail)
343 len = eoemail - email;
344 line = xmalloc(len + 1);
345 memcpy(line, email, len);
350 static void grab_date(const char *buf, struct atom_value *v)
352 const char *eoemail = strstr(buf, "> ");
354 unsigned long timestamp;
359 timestamp = strtoul(eoemail + 2, &zone, 10);
360 if (timestamp == ULONG_MAX)
362 tz = strtol(zone, NULL, 10);
363 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
365 v->s = xstrdup(show_date(timestamp, tz, 0));
373 /* See grab_values */
374 static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
377 int wholen = strlen(who);
378 const char *wholine = NULL;
380 for (i = 0; i < used_atom_cnt; i++) {
381 const char *name = used_atom[i];
382 struct atom_value *v = &val[i];
383 if (!!deref != (*name == '*'))
387 if (strncmp(who, name, wholen))
389 if (name[wholen] != 0 &&
390 strcmp(name + wholen, "name") &&
391 strcmp(name + wholen, "email") &&
392 strcmp(name + wholen, "date"))
395 wholine = find_wholine(who, wholen, buf, sz);
397 return; /* no point looking for it */
398 if (name[wholen] == 0)
399 v->s = copy_line(wholine);
400 else if (!strcmp(name + wholen, "name"))
401 v->s = copy_name(wholine);
402 else if (!strcmp(name + wholen, "email"))
403 v->s = copy_email(wholine);
404 else if (!strcmp(name + wholen, "date"))
405 grab_date(wholine, v);
408 /* For a tag or a commit object, if "creator" or "creatordate" is
409 * requested, do something special.
411 if (strcmp(who, "tagger") && strcmp(who, "committer"))
412 return; /* "author" for commit object is not wanted */
414 wholine = find_wholine(who, wholen, buf, sz);
417 for (i = 0; i < used_atom_cnt; i++) {
418 const char *name = used_atom[i];
419 struct atom_value *v = &val[i];
420 if (!!deref != (*name == '*'))
425 if (!strcmp(name, "creatordate"))
426 grab_date(wholine, v);
427 else if (!strcmp(name, "creator"))
428 v->s = copy_line(wholine);
432 static void find_subpos(const char *buf, unsigned long sz, const char **sub, const char **body)
435 const char *eol = strchr(buf, '\n');
438 if (eol[1] == '\n') {
440 break; /* found end of header */
448 *sub = buf; /* first non-empty line */
449 buf = strchr(buf, '\n');
451 return; /* no body */
453 buf++; /* skip blank between subject and body */
457 /* See grab_values */
458 static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
461 const char *subpos = NULL, *bodypos = NULL;
463 for (i = 0; i < used_atom_cnt; i++) {
464 const char *name = used_atom[i];
465 struct atom_value *v = &val[i];
466 if (!!deref != (*name == '*'))
470 if (strcmp(name, "subject") &&
471 strcmp(name, "body") &&
472 strcmp(name, "contents"))
475 find_subpos(buf, sz, &subpos, &bodypos);
479 if (!strcmp(name, "subject"))
480 v->s = copy_line(subpos);
481 else if (!strcmp(name, "body"))
482 v->s = xstrdup(bodypos);
483 else if (!strcmp(name, "contents"))
484 v->s = xstrdup(subpos);
488 /* We want to have empty print-string for field requests
489 * that do not apply (e.g. "authordate" for a tag object)
491 static void fill_missing_values(struct atom_value *val)
494 for (i = 0; i < used_atom_cnt; i++) {
495 struct atom_value *v = &val[i];
502 * val is a list of atom_value to hold returned values. Extract
503 * the values for atoms in used_atom array out of (obj, buf, sz).
504 * when deref is false, (obj, buf, sz) is the object that is
505 * pointed at by the ref itself; otherwise it is the object the
506 * ref (which is a tag) refers to.
508 static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
510 grab_common_values(val, deref, obj, buf, sz);
513 grab_tag_values(val, deref, obj, buf, sz);
514 grab_sub_body_contents(val, deref, obj, buf, sz);
515 grab_person("tagger", val, deref, obj, buf, sz);
518 grab_commit_values(val, deref, obj, buf, sz);
519 grab_sub_body_contents(val, deref, obj, buf, sz);
520 grab_person("author", val, deref, obj, buf, sz);
521 grab_person("committer", val, deref, obj, buf, sz);
524 // grab_tree_values(val, deref, obj, buf, sz);
527 // grab_blob_values(val, deref, obj, buf, sz);
530 die("Eh? Object of type %d?", obj->type);
535 * Parse the object referred by ref, and grab needed value.
537 static void populate_value(struct refinfo *ref)
543 const unsigned char *tagged;
545 ref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt);
547 buf = get_obj(ref->objectname, &obj, &size, &eaten);
549 die("missing object %s for %s",
550 sha1_to_hex(ref->objectname), ref->refname);
552 die("parse_object_buffer failed on %s for %s",
553 sha1_to_hex(ref->objectname), ref->refname);
555 /* Fill in specials first */
556 for (i = 0; i < used_atom_cnt; i++) {
557 const char *name = used_atom[i];
558 struct atom_value *v = &ref->value[i];
559 if (!strcmp(name, "refname"))
561 else if (!strcmp(name, "*refname")) {
562 int len = strlen(ref->refname);
563 char *s = xmalloc(len + 4);
564 sprintf(s, "%s^{}", ref->refname);
569 grab_values(ref->value, 0, obj, buf, size);
573 /* If there is no atom that wants to know about tagged
574 * object, we are done.
576 if (!need_tagged || (obj->type != OBJ_TAG))
579 /* If it is a tag object, see if we use a value that derefs
580 * the object, and if we do grab the object it refers to.
582 tagged = ((struct tag *)obj)->tagged->sha1;
584 /* NEEDSWORK: This derefs tag only once, which
585 * is good to deal with chains of trust, but
586 * is not consistent with what deref_tag() does
587 * which peels the onion to the core.
589 buf = get_obj(tagged, &obj, &size, &eaten);
591 die("missing object %s for %s",
592 sha1_to_hex(tagged), ref->refname);
594 die("parse_object_buffer failed on %s for %s",
595 sha1_to_hex(tagged), ref->refname);
596 grab_values(ref->value, 1, obj, buf, size);
602 * Given a ref, return the value for the atom. This lazily gets value
603 * out of the object by calling populate value.
605 static void get_value(struct refinfo *ref, int atom, struct atom_value **v)
609 fill_missing_values(ref->value);
611 *v = &ref->value[atom];
614 struct grab_ref_cbdata {
615 struct refinfo **grab_array;
616 const char **grab_pattern;
621 * A call-back given to for_each_ref(). It is unfortunate that we
622 * need to use global variables to pass extra information to this
625 static int grab_single_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
627 struct grab_ref_cbdata *cb = cb_data;
631 if (*cb->grab_pattern) {
632 const char **pattern;
633 int namelen = strlen(refname);
634 for (pattern = cb->grab_pattern; *pattern; pattern++) {
635 const char *p = *pattern;
636 int plen = strlen(p);
638 if ((plen <= namelen) &&
639 !strncmp(refname, p, plen) &&
640 (refname[plen] == '\0' ||
641 refname[plen] == '/'))
643 if (!fnmatch(p, refname, FNM_PATHNAME))
650 /* We do not open the object yet; sort may only need refname
651 * to do its job and the resulting list may yet to be pruned
654 ref = xcalloc(1, sizeof(*ref));
655 ref->refname = xstrdup(refname);
656 hashcpy(ref->objectname, sha1);
659 cb->grab_array = xrealloc(cb->grab_array,
660 sizeof(*cb->grab_array) * (cnt + 1));
661 cb->grab_array[cnt++] = ref;
666 static int cmp_ref_sort(struct ref_sort *s, struct refinfo *a, struct refinfo *b)
668 struct atom_value *va, *vb;
670 cmp_type cmp_type = used_atom_type[s->atom];
672 get_value(a, s->atom, &va);
673 get_value(b, s->atom, &vb);
676 cmp = strcmp(va->s, vb->s);
681 else if (va->ul == vb->ul)
687 return (s->reverse) ? -cmp : cmp;
690 static struct ref_sort *ref_sort;
691 static int compare_refs(const void *a_, const void *b_)
693 struct refinfo *a = *((struct refinfo **)a_);
694 struct refinfo *b = *((struct refinfo **)b_);
697 for (s = ref_sort; s; s = s->next) {
698 int cmp = cmp_ref_sort(s, a, b);
705 static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs)
708 qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs);
711 static void print_value(struct refinfo *ref, int atom, int quote_style)
713 struct atom_value *v;
714 get_value(ref, atom, &v);
715 switch (quote_style) {
720 sq_quote_print(stdout, v->s);
723 perl_quote_print(stdout, v->s);
726 python_quote_print(stdout, v->s);
729 tcl_quote_print(stdout, v->s);
734 static int hex1(char ch)
736 if ('0' <= ch && ch <= '9')
738 else if ('a' <= ch && ch <= 'f')
739 return ch - 'a' + 10;
740 else if ('A' <= ch && ch <= 'F')
741 return ch - 'A' + 10;
744 static int hex2(const char *cp)
747 return (hex1(cp[0]) << 4) | hex1(cp[1]);
752 static void emit(const char *cp, const char *ep)
754 while (*cp && (!ep || cp < ep)) {
759 int ch = hex2(cp + 1);
772 static void show_ref(struct refinfo *info, const char *format, int quote_style)
774 const char *cp, *sp, *ep;
776 for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
777 ep = strchr(sp, ')');
780 print_value(info, parse_atom(sp + 2, ep), quote_style);
783 sp = cp + strlen(cp);
789 static struct ref_sort *default_sort(void)
791 static const char cstr_name[] = "refname";
793 struct ref_sort *sort = xcalloc(1, sizeof(*sort));
796 sort->atom = parse_atom(cstr_name, cstr_name + strlen(cstr_name));
800 int cmd_for_each_ref(int ac, const char **av, const char *prefix)
803 const char *format = NULL;
804 struct ref_sort *sort = NULL, **sort_tail = &sort;
806 int quote_style = -1; /* unspecified yet */
807 struct refinfo **refs;
808 struct grab_ref_cbdata cbdata;
810 for (i = 1; i < ac; i++) {
811 const char *arg = av[i];
814 if (!strcmp(arg, "--")) {
818 if (!prefixcmp(arg, "--format=")) {
820 die("more than one --format?");
824 if (!strcmp(arg, "-s") || !strcmp(arg, "--shell") ) {
825 if (0 <= quote_style)
826 die("more than one quoting style?");
827 quote_style = QUOTE_SHELL;
830 if (!strcmp(arg, "-p") || !strcmp(arg, "--perl") ) {
831 if (0 <= quote_style)
832 die("more than one quoting style?");
833 quote_style = QUOTE_PERL;
836 if (!strcmp(arg, "--python") ) {
837 if (0 <= quote_style)
838 die("more than one quoting style?");
839 quote_style = QUOTE_PYTHON;
842 if (!strcmp(arg, "--tcl") ) {
843 if (0 <= quote_style)
844 die("more than one quoting style?");
845 quote_style = QUOTE_TCL;
848 if (!prefixcmp(arg, "--count=")) {
850 die("more than one --count?");
851 maxcount = atoi(arg + 8);
853 die("The number %s did not parse", arg);
856 if (!prefixcmp(arg, "--sort=")) {
857 struct ref_sort *s = xcalloc(1, sizeof(*s));
862 sort_tail = &s->next;
870 sort->atom = parse_atom(arg, arg+len);
876 quote_style = QUOTE_NONE;
879 sort = default_sort();
880 sort_atom_limit = used_atom_cnt;
882 format = "%(objectname) %(objecttype)\t%(refname)";
884 verify_format(format);
886 memset(&cbdata, 0, sizeof(cbdata));
887 cbdata.grab_pattern = av + i;
888 for_each_ref(grab_single_ref, &cbdata);
889 refs = cbdata.grab_array;
890 num_refs = cbdata.grab_cnt;
892 for (i = 0; i < used_atom_cnt; i++) {
893 if (used_atom[i][0] == '*') {
899 sort_refs(sort, refs, num_refs);
901 if (!maxcount || num_refs < maxcount)
903 for (i = 0; i < maxcount; i++)
904 show_ref(refs[i], format, quote_style);