14 #define QUOTE_PYTHON 3
17 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
21 unsigned long ul
; /* used for sorting when not FIELD_STR */
25 struct ref_sort
*next
;
26 int atom
; /* index into used_atom array */
32 unsigned char objectname
[20];
33 struct atom_value
*value
;
42 { "objectsize", FIELD_ULONG
},
45 { "parent" }, /* NEEDSWORK: how to address 2nd and later parents? */
46 { "numparent", FIELD_ULONG
},
53 { "authordate", FIELD_TIME
},
57 { "committerdate", FIELD_TIME
},
61 { "taggerdate", FIELD_TIME
},
63 { "creatordate", FIELD_TIME
},
70 * An atom is a valid field atom listed above, possibly prefixed with
71 * a "*" to denote deref_tag().
73 * We parse given format string and sort specifiers, and make a list
74 * of properties that we need to extract out of objects. refinfo
75 * structure will hold an array of values extracted that can be
76 * indexed with the "atom number", which is an index into this
79 static const char **used_atom
;
80 static cmp_type
*used_atom_type
;
81 static int used_atom_cnt
, sort_atom_limit
, need_tagged
;
84 * Used to parse format string and sort specifiers
86 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 n
= xmalloc(ep
- atom
+ 1);
123 memcpy(n
, atom
, ep
- atom
);
126 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
131 * In a format string, find the next occurrence of %(atom).
133 static const char *find_next(const char *cp
)
137 /* %( is the start of an atom;
138 * %% is a quoted per-cent.
142 else if (cp
[1] == '%')
143 cp
++; /* skip over two % */
144 /* otherwise this is a singleton, literal % */
152 * Make sure the format string is well formed, and parse out
155 static void verify_format(const char *format
)
158 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
159 const char *ep
= strchr(sp
, ')');
161 die("malformatted format string %s", sp
);
162 /* sp points at "%(" and ep points at the closing ")" */
163 parse_atom(sp
+ 2, ep
);
169 * Given an object name, read the object data and size, and return a
170 * "struct object". If the object data we are returning is also borrowed
171 * by the "struct object" representation, set *eaten as well---it is a
172 * signal from parse_object_buffer to us not to free the buffer.
174 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
177 void *buf
= read_sha1_file(sha1
, type
, sz
);
180 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
186 /* See grab_values */
187 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
191 for (i
= 0; i
< used_atom_cnt
; i
++) {
192 const char *name
= used_atom
[i
];
193 struct atom_value
*v
= &val
[i
];
194 if (!!deref
!= (*name
== '*'))
198 if (!strcmp(name
, "objecttype"))
199 v
->s
= type_names
[obj
->type
];
200 else if (!strcmp(name
, "objectsize")) {
201 char *s
= xmalloc(40);
202 sprintf(s
, "%lu", sz
);
206 else if (!strcmp(name
, "objectname")) {
207 char *s
= xmalloc(41);
208 strcpy(s
, sha1_to_hex(obj
->sha1
));
214 /* See grab_values */
215 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
218 struct tag
*tag
= (struct tag
*) obj
;
220 for (i
= 0; i
< used_atom_cnt
; i
++) {
221 const char *name
= used_atom
[i
];
222 struct atom_value
*v
= &val
[i
];
223 if (!!deref
!= (*name
== '*'))
227 if (!strcmp(name
, "tag"))
232 static int num_parents(struct commit
*commit
)
234 struct commit_list
*parents
;
237 for (i
= 0, parents
= commit
->parents
;
239 parents
= parents
->next
)
244 /* See grab_values */
245 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
248 struct commit
*commit
= (struct commit
*) obj
;
250 for (i
= 0; i
< used_atom_cnt
; i
++) {
251 const char *name
= used_atom
[i
];
252 struct atom_value
*v
= &val
[i
];
253 if (!!deref
!= (*name
== '*'))
257 if (!strcmp(name
, "tree")) {
258 char *s
= xmalloc(41);
259 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
262 if (!strcmp(name
, "numparent")) {
263 char *s
= xmalloc(40);
264 sprintf(s
, "%lu", v
->ul
);
266 v
->ul
= num_parents(commit
);
268 else if (!strcmp(name
, "parent")) {
269 int num
= num_parents(commit
);
271 struct commit_list
*parents
;
272 char *s
= xmalloc(42 * num
);
274 for (i
= 0, parents
= commit
->parents
;
276 parents
= parents
->next
, i
= i
+ 42) {
277 struct commit
*parent
= parents
->item
;
278 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
286 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
290 if (!strncmp(buf
, who
, wholen
) &&
292 return buf
+ wholen
+ 1;
293 eol
= strchr(buf
, '\n');
298 return ""; /* end of header */
304 static char *copy_line(const char *buf
)
306 const char *eol
= strchr(buf
, '\n');
312 line
= xmalloc(len
+ 1);
313 memcpy(line
, buf
, len
);
318 static char *copy_name(const char *buf
)
320 const char *eol
= strchr(buf
, '\n');
321 const char *eoname
= strstr(buf
, " <");
324 if (!(eoname
&& eol
&& eoname
< eol
))
327 line
= xmalloc(len
+ 1);
328 memcpy(line
, buf
, len
);
333 static char *copy_email(const char *buf
)
335 const char *email
= strchr(buf
, '<');
336 const char *eoemail
= strchr(email
, '>');
339 if (!email
|| !eoemail
)
342 len
= eoemail
- email
;
343 line
= xmalloc(len
+ 1);
344 memcpy(line
, email
, len
);
349 static void grab_date(const char *buf
, struct atom_value
*v
)
351 const char *eoemail
= strstr(buf
, "> ");
353 unsigned long timestamp
;
358 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
359 if (timestamp
== ULONG_MAX
)
361 tz
= strtol(zone
, NULL
, 10);
362 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
364 v
->s
= xstrdup(show_date(timestamp
, tz
, 0));
372 /* See grab_values */
373 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
376 int wholen
= strlen(who
);
377 const char *wholine
= NULL
;
379 for (i
= 0; i
< used_atom_cnt
; i
++) {
380 const char *name
= used_atom
[i
];
381 struct atom_value
*v
= &val
[i
];
382 if (!!deref
!= (*name
== '*'))
386 if (strncmp(who
, name
, wholen
))
388 if (name
[wholen
] != 0 &&
389 strcmp(name
+ wholen
, "name") &&
390 strcmp(name
+ wholen
, "email") &&
391 strcmp(name
+ wholen
, "date"))
394 wholine
= find_wholine(who
, wholen
, buf
, sz
);
396 return; /* no point looking for it */
397 if (name
[wholen
] == 0)
398 v
->s
= copy_line(wholine
);
399 else if (!strcmp(name
+ wholen
, "name"))
400 v
->s
= copy_name(wholine
);
401 else if (!strcmp(name
+ wholen
, "email"))
402 v
->s
= copy_email(wholine
);
403 else if (!strcmp(name
+ wholen
, "date"))
404 grab_date(wholine
, v
);
407 /* For a tag or a commit object, if "creator" or "creatordate" is
408 * requested, do something special.
410 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
411 return; /* "author" for commit object is not wanted */
413 wholine
= find_wholine(who
, wholen
, buf
, sz
);
416 for (i
= 0; i
< used_atom_cnt
; i
++) {
417 const char *name
= used_atom
[i
];
418 struct atom_value
*v
= &val
[i
];
419 if (!!deref
!= (*name
== '*'))
424 if (!strcmp(name
, "creatordate"))
425 grab_date(wholine
, v
);
426 else if (!strcmp(name
, "creator"))
427 v
->s
= copy_line(wholine
);
431 static void find_subpos(const char *buf
, unsigned long sz
, const char **sub
, const char **body
)
434 const char *eol
= strchr(buf
, '\n');
437 if (eol
[1] == '\n') {
439 break; /* found end of header */
447 *sub
= buf
; /* first non-empty line */
448 buf
= strchr(buf
, '\n');
450 return; /* no body */
452 buf
++; /* skip blank between subject and body */
456 /* See grab_values */
457 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
460 const char *subpos
= NULL
, *bodypos
= NULL
;
462 for (i
= 0; i
< used_atom_cnt
; i
++) {
463 const char *name
= used_atom
[i
];
464 struct atom_value
*v
= &val
[i
];
465 if (!!deref
!= (*name
== '*'))
469 if (strcmp(name
, "subject") &&
470 strcmp(name
, "body") &&
471 strcmp(name
, "contents"))
474 find_subpos(buf
, sz
, &subpos
, &bodypos
);
478 if (!strcmp(name
, "subject"))
479 v
->s
= copy_line(subpos
);
480 else if (!strcmp(name
, "body"))
481 v
->s
= xstrdup(bodypos
);
482 else if (!strcmp(name
, "contents"))
483 v
->s
= xstrdup(subpos
);
487 /* We want to have empty print-string for field requests
488 * that do not apply (e.g. "authordate" for a tag object)
490 static void fill_missing_values(struct atom_value
*val
)
493 for (i
= 0; i
< used_atom_cnt
; i
++) {
494 struct atom_value
*v
= &val
[i
];
501 * val is a list of atom_value to hold returned values. Extract
502 * the values for atoms in used_atom array out of (obj, buf, sz).
503 * when deref is false, (obj, buf, sz) is the object that is
504 * pointed at by the ref itself; otherwise it is the object the
505 * ref (which is a tag) refers to.
507 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
509 grab_common_values(val
, deref
, obj
, buf
, sz
);
512 grab_tag_values(val
, deref
, obj
, buf
, sz
);
513 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
514 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
517 grab_commit_values(val
, deref
, obj
, buf
, sz
);
518 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
519 grab_person("author", val
, deref
, obj
, buf
, sz
);
520 grab_person("committer", val
, deref
, obj
, buf
, sz
);
523 // grab_tree_values(val, deref, obj, buf, sz);
526 // grab_blob_values(val, deref, obj, buf, sz);
529 die("Eh? Object of type %d?", obj
->type
);
534 * Parse the object referred by ref, and grab needed value.
536 static void populate_value(struct refinfo
*ref
)
542 const unsigned char *tagged
;
544 ref
->value
= xcalloc(sizeof(struct atom_value
), used_atom_cnt
);
546 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
548 die("missing object %s for %s",
549 sha1_to_hex(ref
->objectname
), ref
->refname
);
551 die("parse_object_buffer failed on %s for %s",
552 sha1_to_hex(ref
->objectname
), ref
->refname
);
554 /* Fill in specials first */
555 for (i
= 0; i
< used_atom_cnt
; i
++) {
556 const char *name
= used_atom
[i
];
557 struct atom_value
*v
= &ref
->value
[i
];
558 if (!strcmp(name
, "refname"))
560 else if (!strcmp(name
, "*refname")) {
561 int len
= strlen(ref
->refname
);
562 char *s
= xmalloc(len
+ 4);
563 sprintf(s
, "%s^{}", ref
->refname
);
568 grab_values(ref
->value
, 0, obj
, buf
, size
);
572 /* If there is no atom that wants to know about tagged
573 * object, we are done.
575 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
578 /* If it is a tag object, see if we use a value that derefs
579 * the object, and if we do grab the object it refers to.
581 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
583 /* NEEDSWORK: This derefs tag only once, which
584 * is good to deal with chains of trust, but
585 * is not consistent with what deref_tag() does
586 * which peels the onion to the core.
588 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
590 die("missing object %s for %s",
591 sha1_to_hex(tagged
), ref
->refname
);
593 die("parse_object_buffer failed on %s for %s",
594 sha1_to_hex(tagged
), ref
->refname
);
595 grab_values(ref
->value
, 1, obj
, buf
, size
);
601 * Given a ref, return the value for the atom. This lazily gets value
602 * out of the object by calling populate value.
604 static void get_value(struct refinfo
*ref
, int atom
, struct atom_value
**v
)
608 fill_missing_values(ref
->value
);
610 *v
= &ref
->value
[atom
];
613 struct grab_ref_cbdata
{
614 struct refinfo
**grab_array
;
615 const char **grab_pattern
;
620 * A call-back given to for_each_ref(). It is unfortunate that we
621 * need to use global variables to pass extra information to this
624 static int grab_single_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
626 struct grab_ref_cbdata
*cb
= cb_data
;
630 if (*cb
->grab_pattern
) {
631 const char **pattern
;
632 int namelen
= strlen(refname
);
633 for (pattern
= cb
->grab_pattern
; *pattern
; pattern
++) {
634 const char *p
= *pattern
;
635 int plen
= strlen(p
);
637 if ((plen
<= namelen
) &&
638 !strncmp(refname
, p
, plen
) &&
639 (refname
[plen
] == '\0' ||
640 refname
[plen
] == '/'))
642 if (!fnmatch(p
, refname
, FNM_PATHNAME
))
649 /* We do not open the object yet; sort may only need refname
650 * to do its job and the resulting list may yet to be pruned
653 ref
= xcalloc(1, sizeof(*ref
));
654 ref
->refname
= xstrdup(refname
);
655 hashcpy(ref
->objectname
, sha1
);
658 cb
->grab_array
= xrealloc(cb
->grab_array
,
659 sizeof(*cb
->grab_array
) * (cnt
+ 1));
660 cb
->grab_array
[cnt
++] = ref
;
665 static int cmp_ref_sort(struct ref_sort
*s
, struct refinfo
*a
, struct refinfo
*b
)
667 struct atom_value
*va
, *vb
;
669 cmp_type cmp_type
= used_atom_type
[s
->atom
];
671 get_value(a
, s
->atom
, &va
);
672 get_value(b
, s
->atom
, &vb
);
675 cmp
= strcmp(va
->s
, vb
->s
);
680 else if (va
->ul
== vb
->ul
)
686 return (s
->reverse
) ? -cmp
: cmp
;
689 static struct ref_sort
*ref_sort
;
690 static int compare_refs(const void *a_
, const void *b_
)
692 struct refinfo
*a
= *((struct refinfo
**)a_
);
693 struct refinfo
*b
= *((struct refinfo
**)b_
);
696 for (s
= ref_sort
; s
; s
= s
->next
) {
697 int cmp
= cmp_ref_sort(s
, a
, b
);
704 static void sort_refs(struct ref_sort
*sort
, struct refinfo
**refs
, int num_refs
)
707 qsort(refs
, num_refs
, sizeof(struct refinfo
*), compare_refs
);
710 static void print_value(struct refinfo
*ref
, int atom
, int quote_style
)
712 struct atom_value
*v
;
713 get_value(ref
, atom
, &v
);
714 switch (quote_style
) {
719 sq_quote_print(stdout
, v
->s
);
722 perl_quote_print(stdout
, v
->s
);
725 python_quote_print(stdout
, v
->s
);
728 tcl_quote_print(stdout
, v
->s
);
733 static int hex1(char ch
)
735 if ('0' <= ch
&& ch
<= '9')
737 else if ('a' <= ch
&& ch
<= 'f')
738 return ch
- 'a' + 10;
739 else if ('A' <= ch
&& ch
<= 'F')
740 return ch
- 'A' + 10;
743 static int hex2(const char *cp
)
746 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
751 static void emit(const char *cp
, const char *ep
)
753 while (*cp
&& (!ep
|| cp
< ep
)) {
758 int ch
= hex2(cp
+ 1);
771 static void show_ref(struct refinfo
*info
, const char *format
, int quote_style
)
773 const char *cp
, *sp
, *ep
;
775 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
776 ep
= strchr(sp
, ')');
779 print_value(info
, parse_atom(sp
+ 2, ep
), quote_style
);
782 sp
= cp
+ strlen(cp
);
788 static struct ref_sort
*default_sort(void)
790 static const char cstr_name
[] = "refname";
792 struct ref_sort
*sort
= xcalloc(1, sizeof(*sort
));
795 sort
->atom
= parse_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
799 int cmd_for_each_ref(int ac
, const char **av
, char *prefix
)
802 const char *format
= NULL
;
803 struct ref_sort
*sort
= NULL
, **sort_tail
= &sort
;
805 int quote_style
= -1; /* unspecified yet */
806 struct refinfo
**refs
;
807 struct grab_ref_cbdata cbdata
;
809 for (i
= 1; i
< ac
; i
++) {
810 const char *arg
= av
[i
];
813 if (!strcmp(arg
, "--")) {
817 if (!strncmp(arg
, "--format=", 9)) {
819 die("more than one --format?");
823 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--shell") ) {
824 if (0 <= quote_style
)
825 die("more than one quoting style?");
826 quote_style
= QUOTE_SHELL
;
829 if (!strcmp(arg
, "-p") || !strcmp(arg
, "--perl") ) {
830 if (0 <= quote_style
)
831 die("more than one quoting style?");
832 quote_style
= QUOTE_PERL
;
835 if (!strcmp(arg
, "--python") ) {
836 if (0 <= quote_style
)
837 die("more than one quoting style?");
838 quote_style
= QUOTE_PYTHON
;
841 if (!strcmp(arg
, "--tcl") ) {
842 if (0 <= quote_style
)
843 die("more than one quoting style?");
844 quote_style
= QUOTE_TCL
;
847 if (!strncmp(arg
, "--count=", 8)) {
849 die("more than one --count?");
850 maxcount
= atoi(arg
+ 8);
852 die("The number %s did not parse", arg
);
855 if (!strncmp(arg
, "--sort=", 7)) {
856 struct ref_sort
*s
= xcalloc(1, sizeof(*s
));
861 sort_tail
= &s
->next
;
869 sort
->atom
= parse_atom(arg
, arg
+len
);
875 quote_style
= QUOTE_NONE
;
878 sort
= default_sort();
879 sort_atom_limit
= used_atom_cnt
;
881 format
= "%(objectname) %(objecttype)\t%(refname)";
883 verify_format(format
);
885 memset(&cbdata
, 0, sizeof(cbdata
));
886 cbdata
.grab_pattern
= av
+ i
;
887 for_each_ref(grab_single_ref
, &cbdata
);
888 refs
= cbdata
.grab_array
;
889 num_refs
= cbdata
.grab_cnt
;
891 for (i
= 0; i
< used_atom_cnt
; i
++) {
892 if (used_atom
[i
][0] == '*') {
898 sort_refs(sort
, refs
, num_refs
);
900 if (!maxcount
|| num_refs
< maxcount
)
902 for (i
= 0; i
< maxcount
; i
++)
903 show_ref(refs
[i
], format
, quote_style
);