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 },
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 v->ul = num_parents(commit);
266 sprintf(s, "%lu", v->ul);
269 else if (!strcmp(name, "parent")) {
270 int num = num_parents(commit);
272 struct commit_list *parents;
273 char *s = xmalloc(41 * num + 1);
275 for (i = 0, parents = commit->parents;
277 parents = parents->next, i = i + 41) {
278 struct commit *parent = parents->item;
279 strcpy(s+i, sha1_to_hex(parent->object.sha1));
289 static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz)
293 if (!strncmp(buf, who, wholen) &&
295 return buf + wholen + 1;
296 eol = strchr(buf, '\n');
301 return ""; /* end of header */
307 static const char *copy_line(const char *buf)
309 const char *eol = strchr(buf, '\n');
315 line = xmalloc(len + 1);
316 memcpy(line, buf, len);
321 static const char *copy_name(const char *buf)
323 const char *eol = strchr(buf, '\n');
324 const char *eoname = strstr(buf, " <");
327 if (!(eoname && eol && eoname < eol))
330 line = xmalloc(len + 1);
331 memcpy(line, buf, len);
336 static const char *copy_email(const char *buf)
338 const char *email = strchr(buf, '<');
339 const char *eoemail = strchr(email, '>');
342 if (!email || !eoemail)
345 len = eoemail - email;
346 line = xmalloc(len + 1);
347 memcpy(line, email, len);
352 static void grab_date(const char *buf, struct atom_value *v)
354 const char *eoemail = strstr(buf, "> ");
356 unsigned long timestamp;
361 timestamp = strtoul(eoemail + 2, &zone, 10);
362 if (timestamp == ULONG_MAX)
364 tz = strtol(zone, NULL, 10);
365 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
367 v->s = xstrdup(show_date(timestamp, tz, 0));
375 /* See grab_values */
376 static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
379 int wholen = strlen(who);
380 const char *wholine = NULL;
382 for (i = 0; i < used_atom_cnt; i++) {
383 const char *name = used_atom[i];
384 struct atom_value *v = &val[i];
385 if (!!deref != (*name == '*'))
389 if (strncmp(who, name, wholen))
391 if (name[wholen] != 0 &&
392 strcmp(name + wholen, "name") &&
393 strcmp(name + wholen, "email") &&
394 strcmp(name + wholen, "date"))
397 wholine = find_wholine(who, wholen, buf, sz);
399 return; /* no point looking for it */
400 if (name[wholen] == 0)
401 v->s = copy_line(wholine);
402 else if (!strcmp(name + wholen, "name"))
403 v->s = copy_name(wholine);
404 else if (!strcmp(name + wholen, "email"))
405 v->s = copy_email(wholine);
406 else if (!strcmp(name + wholen, "date"))
407 grab_date(wholine, v);
410 /* For a tag or a commit object, if "creator" or "creatordate" is
411 * requested, do something special.
413 if (strcmp(who, "tagger") && strcmp(who, "committer"))
414 return; /* "author" for commit object is not wanted */
416 wholine = find_wholine(who, wholen, buf, sz);
419 for (i = 0; i < used_atom_cnt; i++) {
420 const char *name = used_atom[i];
421 struct atom_value *v = &val[i];
422 if (!!deref != (*name == '*'))
427 if (!strcmp(name, "creatordate"))
428 grab_date(wholine, v);
429 else if (!strcmp(name, "creator"))
430 v->s = copy_line(wholine);
434 static void find_subpos(const char *buf, unsigned long sz, const char **sub, const char **body)
437 const char *eol = strchr(buf, '\n');
440 if (eol[1] == '\n') {
442 break; /* found end of header */
450 *sub = buf; /* first non-empty line */
451 buf = strchr(buf, '\n');
453 return; /* no body */
455 buf++; /* skip blank between subject and body */
459 /* See grab_values */
460 static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
463 const char *subpos = NULL, *bodypos = NULL;
465 for (i = 0; i < used_atom_cnt; i++) {
466 const char *name = used_atom[i];
467 struct atom_value *v = &val[i];
468 if (!!deref != (*name == '*'))
472 if (strcmp(name, "subject") &&
473 strcmp(name, "body") &&
474 strcmp(name, "contents"))
477 find_subpos(buf, sz, &subpos, &bodypos);
481 if (!strcmp(name, "subject"))
482 v->s = copy_line(subpos);
483 else if (!strcmp(name, "body"))
484 v->s = xstrdup(bodypos);
485 else if (!strcmp(name, "contents"))
486 v->s = xstrdup(subpos);
490 /* We want to have empty print-string for field requests
491 * that do not apply (e.g. "authordate" for a tag object)
493 static void fill_missing_values(struct atom_value *val)
496 for (i = 0; i < used_atom_cnt; i++) {
497 struct atom_value *v = &val[i];
504 * val is a list of atom_value to hold returned values. Extract
505 * the values for atoms in used_atom array out of (obj, buf, sz).
506 * when deref is false, (obj, buf, sz) is the object that is
507 * pointed at by the ref itself; otherwise it is the object the
508 * ref (which is a tag) refers to.
510 static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
512 grab_common_values(val, deref, obj, buf, sz);
515 grab_tag_values(val, deref, obj, buf, sz);
516 grab_sub_body_contents(val, deref, obj, buf, sz);
517 grab_person("tagger", val, deref, obj, buf, sz);
520 grab_commit_values(val, deref, obj, buf, sz);
521 grab_sub_body_contents(val, deref, obj, buf, sz);
522 grab_person("author", val, deref, obj, buf, sz);
523 grab_person("committer", val, deref, obj, buf, sz);
526 // grab_tree_values(val, deref, obj, buf, sz);
529 // grab_blob_values(val, deref, obj, buf, sz);
532 die("Eh? Object of type %d?", obj->type);
537 * Parse the object referred by ref, and grab needed value.
539 static void populate_value(struct refinfo *ref)
545 const unsigned char *tagged;
547 ref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt);
549 buf = get_obj(ref->objectname, &obj, &size, &eaten);
551 die("missing object %s for %s",
552 sha1_to_hex(ref->objectname), ref->refname);
554 die("parse_object_buffer failed on %s for %s",
555 sha1_to_hex(ref->objectname), ref->refname);
557 /* Fill in specials first */
558 for (i = 0; i < used_atom_cnt; i++) {
559 const char *name = used_atom[i];
560 struct atom_value *v = &ref->value[i];
561 if (!strcmp(name, "refname"))
563 else if (!strcmp(name, "*refname")) {
564 int len = strlen(ref->refname);
565 char *s = xmalloc(len + 4);
566 sprintf(s, "%s^{}", ref->refname);
571 grab_values(ref->value, 0, obj, buf, size);
575 /* If there is no atom that wants to know about tagged
576 * object, we are done.
578 if (!need_tagged || (obj->type != OBJ_TAG))
581 /* If it is a tag object, see if we use a value that derefs
582 * the object, and if we do grab the object it refers to.
584 tagged = ((struct tag *)obj)->tagged->sha1;
586 /* NEEDSWORK: This derefs tag only once, which
587 * is good to deal with chains of trust, but
588 * is not consistent with what deref_tag() does
589 * which peels the onion to the core.
591 buf = get_obj(tagged, &obj, &size, &eaten);
593 die("missing object %s for %s",
594 sha1_to_hex(tagged), ref->refname);
596 die("parse_object_buffer failed on %s for %s",
597 sha1_to_hex(tagged), ref->refname);
598 grab_values(ref->value, 1, obj, buf, size);
604 * Given a ref, return the value for the atom. This lazily gets value
605 * out of the object by calling populate value.
607 static void get_value(struct refinfo *ref, int atom, struct atom_value **v)
611 fill_missing_values(ref->value);
613 *v = &ref->value[atom];
616 struct grab_ref_cbdata {
617 struct refinfo **grab_array;
618 const char **grab_pattern;
623 * A call-back given to for_each_ref(). It is unfortunate that we
624 * need to use global variables to pass extra information to this
627 static int grab_single_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
629 struct grab_ref_cbdata *cb = cb_data;
633 if (*cb->grab_pattern) {
634 const char **pattern;
635 int namelen = strlen(refname);
636 for (pattern = cb->grab_pattern; *pattern; pattern++) {
637 const char *p = *pattern;
638 int plen = strlen(p);
640 if ((plen <= namelen) &&
641 !strncmp(refname, p, plen) &&
642 (refname[plen] == '\0' ||
643 refname[plen] == '/'))
645 if (!fnmatch(p, refname, FNM_PATHNAME))
652 /* We do not open the object yet; sort may only need refname
653 * to do its job and the resulting list may yet to be pruned
656 ref = xcalloc(1, sizeof(*ref));
657 ref->refname = xstrdup(refname);
658 hashcpy(ref->objectname, sha1);
661 cb->grab_array = xrealloc(cb->grab_array,
662 sizeof(*cb->grab_array) * (cnt + 1));
663 cb->grab_array[cnt++] = ref;
668 static int cmp_ref_sort(struct ref_sort *s, struct refinfo *a, struct refinfo *b)
670 struct atom_value *va, *vb;
672 cmp_type cmp_type = used_atom_type[s->atom];
674 get_value(a, s->atom, &va);
675 get_value(b, s->atom, &vb);
678 cmp = strcmp(va->s, vb->s);
683 else if (va->ul == vb->ul)
689 return (s->reverse) ? -cmp : cmp;
692 static struct ref_sort *ref_sort;
693 static int compare_refs(const void *a_, const void *b_)
695 struct refinfo *a = *((struct refinfo **)a_);
696 struct refinfo *b = *((struct refinfo **)b_);
699 for (s = ref_sort; s; s = s->next) {
700 int cmp = cmp_ref_sort(s, a, b);
707 static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs)
710 qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs);
713 static void print_value(struct refinfo *ref, int atom, int quote_style)
715 struct atom_value *v;
716 get_value(ref, atom, &v);
717 switch (quote_style) {
722 sq_quote_print(stdout, v->s);
725 perl_quote_print(stdout, v->s);
728 python_quote_print(stdout, v->s);
731 tcl_quote_print(stdout, v->s);
736 static int hex1(char ch)
738 if ('0' <= ch && ch <= '9')
740 else if ('a' <= ch && ch <= 'f')
741 return ch - 'a' + 10;
742 else if ('A' <= ch && ch <= 'F')
743 return ch - 'A' + 10;
746 static int hex2(const char *cp)
749 return (hex1(cp[0]) << 4) | hex1(cp[1]);
754 static void emit(const char *cp, const char *ep)
756 while (*cp && (!ep || cp < ep)) {
761 int ch = hex2(cp + 1);
774 static void show_ref(struct refinfo *info, const char *format, int quote_style)
776 const char *cp, *sp, *ep;
778 for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
779 ep = strchr(sp, ')');
782 print_value(info, parse_atom(sp + 2, ep), quote_style);
785 sp = cp + strlen(cp);
791 static struct ref_sort *default_sort(void)
793 static const char cstr_name[] = "refname";
795 struct ref_sort *sort = xcalloc(1, sizeof(*sort));
798 sort->atom = parse_atom(cstr_name, cstr_name + strlen(cstr_name));
802 int cmd_for_each_ref(int ac, const char **av, const char *prefix)
805 const char *format = NULL;
806 struct ref_sort *sort = NULL, **sort_tail = &sort;
808 int quote_style = -1; /* unspecified yet */
809 struct refinfo **refs;
810 struct grab_ref_cbdata cbdata;
812 for (i = 1; i < ac; i++) {
813 const char *arg = av[i];
816 if (!strcmp(arg, "--")) {
820 if (!prefixcmp(arg, "--format=")) {
822 die("more than one --format?");
826 if (!strcmp(arg, "-s") || !strcmp(arg, "--shell") ) {
827 if (0 <= quote_style)
828 die("more than one quoting style?");
829 quote_style = QUOTE_SHELL;
832 if (!strcmp(arg, "-p") || !strcmp(arg, "--perl") ) {
833 if (0 <= quote_style)
834 die("more than one quoting style?");
835 quote_style = QUOTE_PERL;
838 if (!strcmp(arg, "--python") ) {
839 if (0 <= quote_style)
840 die("more than one quoting style?");
841 quote_style = QUOTE_PYTHON;
844 if (!strcmp(arg, "--tcl") ) {
845 if (0 <= quote_style)
846 die("more than one quoting style?");
847 quote_style = QUOTE_TCL;
850 if (!prefixcmp(arg, "--count=")) {
852 die("more than one --count?");
853 maxcount = atoi(arg + 8);
855 die("The number %s did not parse", arg);
858 if (!prefixcmp(arg, "--sort=")) {
859 struct ref_sort *s = xcalloc(1, sizeof(*s));
864 sort_tail = &s->next;
872 sort->atom = parse_atom(arg, arg+len);
878 quote_style = QUOTE_NONE;
881 sort = default_sort();
882 sort_atom_limit = used_atom_cnt;
884 format = "%(objectname) %(objecttype)\t%(refname)";
886 verify_format(format);
888 memset(&cbdata, 0, sizeof(cbdata));
889 cbdata.grab_pattern = av + i;
890 for_each_ref(grab_single_ref, &cbdata);
891 refs = cbdata.grab_array;
892 num_refs = cbdata.grab_cnt;
894 for (i = 0; i < used_atom_cnt; i++) {
895 if (used_atom[i][0] == '*') {
901 sort_refs(sort, refs, num_refs);
903 if (!maxcount || num_refs < maxcount)
905 for (i = 0; i < maxcount; i++)
906 show_ref(refs[i], format, quote_style);