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)
93 if (*sp == '*' && sp < ep)
96 die("malformed field name: %.*s", (int)(ep-atom), atom);
98 /* Do we have the atom already used elsewhere? */
99 for (i = 0; i < used_atom_cnt; i++) {
100 int len = strlen(used_atom[i]);
101 if (len == ep - atom && !memcmp(used_atom[i], atom, len))
105 /* Is the atom a valid one? */
106 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
107 int len = strlen(valid_atom[i].name);
108 if (len == ep - sp && !memcmp(valid_atom[i].name, sp, len))
112 if (ARRAY_SIZE(valid_atom) <= i)
113 die("unknown field name: %.*s", (int)(ep-atom), atom);
115 /* Add it in, including the deref prefix */
118 used_atom = xrealloc(used_atom,
119 (sizeof *used_atom) * used_atom_cnt);
120 used_atom_type = xrealloc(used_atom_type,
121 (sizeof(*used_atom_type) * used_atom_cnt));
122 used_atom[at] = xmemdupz(atom, ep - atom);
123 used_atom_type[at] = valid_atom[i].cmp_type;
128 * In a format string, find the next occurrence of %(atom).
130 static const char *find_next(const char *cp)
134 /* %( is the start of an atom;
135 * %% is a quoted per-cent.
139 else if (cp[1] == '%')
140 cp++; /* skip over two % */
141 /* otherwise this is a singleton, literal % */
149 * Make sure the format string is well formed, and parse out
152 static void verify_format(const char *format)
155 for (cp = format; *cp && (sp = find_next(cp)); ) {
156 const char *ep = strchr(sp, ')');
158 die("malformatted format string %s", sp);
159 /* sp points at "%(" and ep points at the closing ")" */
160 parse_atom(sp + 2, ep);
166 * Given an object name, read the object data and size, and return a
167 * "struct object". If the object data we are returning is also borrowed
168 * by the "struct object" representation, set *eaten as well---it is a
169 * signal from parse_object_buffer to us not to free the buffer.
171 static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
173 enum object_type type;
174 void *buf = read_sha1_file(sha1, &type, sz);
177 *obj = parse_object_buffer(sha1, type, *sz, buf, eaten);
183 /* See grab_values */
184 static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
188 for (i = 0; i < used_atom_cnt; i++) {
189 const char *name = used_atom[i];
190 struct atom_value *v = &val[i];
191 if (!!deref != (*name == '*'))
195 if (!strcmp(name, "objecttype"))
196 v->s = typename(obj->type);
197 else if (!strcmp(name, "objectsize")) {
198 char *s = xmalloc(40);
199 sprintf(s, "%lu", sz);
203 else if (!strcmp(name, "objectname")) {
204 char *s = xmalloc(41);
205 strcpy(s, sha1_to_hex(obj->sha1));
211 /* See grab_values */
212 static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
215 struct tag *tag = (struct tag *) obj;
217 for (i = 0; i < used_atom_cnt; i++) {
218 const char *name = used_atom[i];
219 struct atom_value *v = &val[i];
220 if (!!deref != (*name == '*'))
224 if (!strcmp(name, "tag"))
229 static int num_parents(struct commit *commit)
231 struct commit_list *parents;
234 for (i = 0, parents = commit->parents;
236 parents = parents->next)
241 /* See grab_values */
242 static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
245 struct commit *commit = (struct commit *) 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, "tree")) {
255 char *s = xmalloc(41);
256 strcpy(s, sha1_to_hex(commit->tree->object.sha1));
259 if (!strcmp(name, "numparent")) {
260 char *s = xmalloc(40);
261 sprintf(s, "%lu", v->ul);
263 v->ul = num_parents(commit);
265 else if (!strcmp(name, "parent")) {
266 int num = num_parents(commit);
268 struct commit_list *parents;
269 char *s = xmalloc(42 * num);
271 for (i = 0, parents = commit->parents;
273 parents = parents->next, i = i + 42) {
274 struct commit *parent = parents->item;
275 strcpy(s+i, sha1_to_hex(parent->object.sha1));
283 static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz)
287 if (!strncmp(buf, who, wholen) &&
289 return buf + wholen + 1;
290 eol = strchr(buf, '\n');
295 return ""; /* end of header */
301 static const char *copy_line(const char *buf)
303 const char *eol = strchr(buf, '\n');
306 return xmemdupz(buf, eol - buf);
309 static const char *copy_name(const char *buf)
312 for (cp = buf; *cp != '\n'; cp++) {
313 if (!strncmp(cp, " <", 2))
314 return xmemdupz(buf, cp - buf);
319 static const char *copy_email(const char *buf)
321 const char *email = strchr(buf, '<');
322 const char *eoemail = strchr(email, '>');
323 if (!email || !eoemail)
325 return xmemdupz(email, eoemail + 1 - email);
328 static void grab_date(const char *buf, struct atom_value *v)
330 const char *eoemail = strstr(buf, "> ");
332 unsigned long timestamp;
337 timestamp = strtoul(eoemail + 2, &zone, 10);
338 if (timestamp == ULONG_MAX)
340 tz = strtol(zone, NULL, 10);
341 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
343 v->s = xstrdup(show_date(timestamp, tz, 0));
351 /* See grab_values */
352 static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
355 int wholen = strlen(who);
356 const char *wholine = NULL;
358 for (i = 0; i < used_atom_cnt; i++) {
359 const char *name = used_atom[i];
360 struct atom_value *v = &val[i];
361 if (!!deref != (*name == '*'))
365 if (strncmp(who, name, wholen))
367 if (name[wholen] != 0 &&
368 strcmp(name + wholen, "name") &&
369 strcmp(name + wholen, "email") &&
370 strcmp(name + wholen, "date"))
373 wholine = find_wholine(who, wholen, buf, sz);
375 return; /* no point looking for it */
376 if (name[wholen] == 0)
377 v->s = copy_line(wholine);
378 else if (!strcmp(name + wholen, "name"))
379 v->s = copy_name(wholine);
380 else if (!strcmp(name + wholen, "email"))
381 v->s = copy_email(wholine);
382 else if (!strcmp(name + wholen, "date"))
383 grab_date(wholine, v);
386 /* For a tag or a commit object, if "creator" or "creatordate" is
387 * requested, do something special.
389 if (strcmp(who, "tagger") && strcmp(who, "committer"))
390 return; /* "author" for commit object is not wanted */
392 wholine = find_wholine(who, wholen, buf, sz);
395 for (i = 0; i < used_atom_cnt; i++) {
396 const char *name = used_atom[i];
397 struct atom_value *v = &val[i];
398 if (!!deref != (*name == '*'))
403 if (!strcmp(name, "creatordate"))
404 grab_date(wholine, v);
405 else if (!strcmp(name, "creator"))
406 v->s = copy_line(wholine);
410 static void find_subpos(const char *buf, unsigned long sz, const char **sub, const char **body)
413 const char *eol = strchr(buf, '\n');
416 if (eol[1] == '\n') {
418 break; /* found end of header */
426 *sub = buf; /* first non-empty line */
427 buf = strchr(buf, '\n');
429 return; /* no body */
431 buf++; /* skip blank between subject and body */
435 /* See grab_values */
436 static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
439 const char *subpos = NULL, *bodypos = NULL;
441 for (i = 0; i < used_atom_cnt; i++) {
442 const char *name = used_atom[i];
443 struct atom_value *v = &val[i];
444 if (!!deref != (*name == '*'))
448 if (strcmp(name, "subject") &&
449 strcmp(name, "body") &&
450 strcmp(name, "contents"))
453 find_subpos(buf, sz, &subpos, &bodypos);
457 if (!strcmp(name, "subject"))
458 v->s = copy_line(subpos);
459 else if (!strcmp(name, "body"))
460 v->s = xstrdup(bodypos);
461 else if (!strcmp(name, "contents"))
462 v->s = xstrdup(subpos);
466 /* We want to have empty print-string for field requests
467 * that do not apply (e.g. "authordate" for a tag object)
469 static void fill_missing_values(struct atom_value *val)
472 for (i = 0; i < used_atom_cnt; i++) {
473 struct atom_value *v = &val[i];
480 * val is a list of atom_value to hold returned values. Extract
481 * the values for atoms in used_atom array out of (obj, buf, sz).
482 * when deref is false, (obj, buf, sz) is the object that is
483 * pointed at by the ref itself; otherwise it is the object the
484 * ref (which is a tag) refers to.
486 static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
488 grab_common_values(val, deref, obj, buf, sz);
491 grab_tag_values(val, deref, obj, buf, sz);
492 grab_sub_body_contents(val, deref, obj, buf, sz);
493 grab_person("tagger", val, deref, obj, buf, sz);
496 grab_commit_values(val, deref, obj, buf, sz);
497 grab_sub_body_contents(val, deref, obj, buf, sz);
498 grab_person("author", val, deref, obj, buf, sz);
499 grab_person("committer", val, deref, obj, buf, sz);
502 // grab_tree_values(val, deref, obj, buf, sz);
505 // grab_blob_values(val, deref, obj, buf, sz);
508 die("Eh? Object of type %d?", obj->type);
513 * Parse the object referred by ref, and grab needed value.
515 static void populate_value(struct refinfo *ref)
521 const unsigned char *tagged;
523 ref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt);
525 buf = get_obj(ref->objectname, &obj, &size, &eaten);
527 die("missing object %s for %s",
528 sha1_to_hex(ref->objectname), ref->refname);
530 die("parse_object_buffer failed on %s for %s",
531 sha1_to_hex(ref->objectname), ref->refname);
533 /* Fill in specials first */
534 for (i = 0; i < used_atom_cnt; i++) {
535 const char *name = used_atom[i];
536 struct atom_value *v = &ref->value[i];
537 if (!strcmp(name, "refname"))
539 else if (!strcmp(name, "*refname")) {
540 int len = strlen(ref->refname);
541 char *s = xmalloc(len + 4);
542 sprintf(s, "%s^{}", ref->refname);
547 grab_values(ref->value, 0, obj, buf, size);
551 /* If there is no atom that wants to know about tagged
552 * object, we are done.
554 if (!need_tagged || (obj->type != OBJ_TAG))
557 /* If it is a tag object, see if we use a value that derefs
558 * the object, and if we do grab the object it refers to.
560 tagged = ((struct tag *)obj)->tagged->sha1;
562 /* NEEDSWORK: This derefs tag only once, which
563 * is good to deal with chains of trust, but
564 * is not consistent with what deref_tag() does
565 * which peels the onion to the core.
567 buf = get_obj(tagged, &obj, &size, &eaten);
569 die("missing object %s for %s",
570 sha1_to_hex(tagged), ref->refname);
572 die("parse_object_buffer failed on %s for %s",
573 sha1_to_hex(tagged), ref->refname);
574 grab_values(ref->value, 1, obj, buf, size);
580 * Given a ref, return the value for the atom. This lazily gets value
581 * out of the object by calling populate value.
583 static void get_value(struct refinfo *ref, int atom, struct atom_value **v)
587 fill_missing_values(ref->value);
589 *v = &ref->value[atom];
592 struct grab_ref_cbdata {
593 struct refinfo **grab_array;
594 const char **grab_pattern;
599 * A call-back given to for_each_ref(). It is unfortunate that we
600 * need to use global variables to pass extra information to this
603 static int grab_single_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
605 struct grab_ref_cbdata *cb = cb_data;
609 if (*cb->grab_pattern) {
610 const char **pattern;
611 int namelen = strlen(refname);
612 for (pattern = cb->grab_pattern; *pattern; pattern++) {
613 const char *p = *pattern;
614 int plen = strlen(p);
616 if ((plen <= namelen) &&
617 !strncmp(refname, p, plen) &&
618 (refname[plen] == '\0' ||
619 refname[plen] == '/'))
621 if (!fnmatch(p, refname, FNM_PATHNAME))
628 /* We do not open the object yet; sort may only need refname
629 * to do its job and the resulting list may yet to be pruned
632 ref = xcalloc(1, sizeof(*ref));
633 ref->refname = xstrdup(refname);
634 hashcpy(ref->objectname, sha1);
637 cb->grab_array = xrealloc(cb->grab_array,
638 sizeof(*cb->grab_array) * (cnt + 1));
639 cb->grab_array[cnt++] = ref;
644 static int cmp_ref_sort(struct ref_sort *s, struct refinfo *a, struct refinfo *b)
646 struct atom_value *va, *vb;
648 cmp_type cmp_type = used_atom_type[s->atom];
650 get_value(a, s->atom, &va);
651 get_value(b, s->atom, &vb);
654 cmp = strcmp(va->s, vb->s);
659 else if (va->ul == vb->ul)
665 return (s->reverse) ? -cmp : cmp;
668 static struct ref_sort *ref_sort;
669 static int compare_refs(const void *a_, const void *b_)
671 struct refinfo *a = *((struct refinfo **)a_);
672 struct refinfo *b = *((struct refinfo **)b_);
675 for (s = ref_sort; s; s = s->next) {
676 int cmp = cmp_ref_sort(s, a, b);
683 static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs)
686 qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs);
689 static void print_value(struct refinfo *ref, int atom, int quote_style)
691 struct atom_value *v;
692 get_value(ref, atom, &v);
693 switch (quote_style) {
698 sq_quote_print(stdout, v->s);
701 perl_quote_print(stdout, v->s);
704 python_quote_print(stdout, v->s);
707 tcl_quote_print(stdout, v->s);
712 static int hex1(char ch)
714 if ('0' <= ch && ch <= '9')
716 else if ('a' <= ch && ch <= 'f')
717 return ch - 'a' + 10;
718 else if ('A' <= ch && ch <= 'F')
719 return ch - 'A' + 10;
722 static int hex2(const char *cp)
725 return (hex1(cp[0]) << 4) | hex1(cp[1]);
730 static void emit(const char *cp, const char *ep)
732 while (*cp && (!ep || cp < ep)) {
737 int ch = hex2(cp + 1);
750 static void show_ref(struct refinfo *info, const char *format, int quote_style)
752 const char *cp, *sp, *ep;
754 for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
755 ep = strchr(sp, ')');
758 print_value(info, parse_atom(sp + 2, ep), quote_style);
761 sp = cp + strlen(cp);
767 static struct ref_sort *default_sort(void)
769 static const char cstr_name[] = "refname";
771 struct ref_sort *sort = xcalloc(1, sizeof(*sort));
774 sort->atom = parse_atom(cstr_name, cstr_name + strlen(cstr_name));
778 int cmd_for_each_ref(int ac, const char **av, const char *prefix)
781 const char *format = NULL;
782 struct ref_sort *sort = NULL, **sort_tail = &sort;
784 int quote_style = -1; /* unspecified yet */
785 struct refinfo **refs;
786 struct grab_ref_cbdata cbdata;
788 for (i = 1; i < ac; i++) {
789 const char *arg = av[i];
792 if (!strcmp(arg, "--")) {
796 if (!prefixcmp(arg, "--format=")) {
798 die("more than one --format?");
802 if (!strcmp(arg, "-s") || !strcmp(arg, "--shell") ) {
803 if (0 <= quote_style)
804 die("more than one quoting style?");
805 quote_style = QUOTE_SHELL;
808 if (!strcmp(arg, "-p") || !strcmp(arg, "--perl") ) {
809 if (0 <= quote_style)
810 die("more than one quoting style?");
811 quote_style = QUOTE_PERL;
814 if (!strcmp(arg, "--python") ) {
815 if (0 <= quote_style)
816 die("more than one quoting style?");
817 quote_style = QUOTE_PYTHON;
820 if (!strcmp(arg, "--tcl") ) {
821 if (0 <= quote_style)
822 die("more than one quoting style?");
823 quote_style = QUOTE_TCL;
826 if (!prefixcmp(arg, "--count=")) {
828 die("more than one --count?");
829 maxcount = atoi(arg + 8);
831 die("The number %s did not parse", arg);
834 if (!prefixcmp(arg, "--sort=")) {
835 struct ref_sort *s = xcalloc(1, sizeof(*s));
840 sort_tail = &s->next;
848 sort->atom = parse_atom(arg, arg+len);
854 quote_style = QUOTE_NONE;
857 sort = default_sort();
858 sort_atom_limit = used_atom_cnt;
860 format = "%(objectname) %(objecttype)\t%(refname)";
862 verify_format(format);
864 memset(&cbdata, 0, sizeof(cbdata));
865 cbdata.grab_pattern = av + i;
866 for_each_ref(grab_single_ref, &cbdata);
867 refs = cbdata.grab_array;
868 num_refs = cbdata.grab_cnt;
870 for (i = 0; i < used_atom_cnt; i++) {
871 if (used_atom[i][0] == '*') {
877 sort_refs(sort, refs, num_refs);
879 if (!maxcount || num_refs < maxcount)
881 for (i = 0; i < maxcount; i++)
882 show_ref(refs[i], format, quote_style);