15 #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
},
68 * An atom is a valid field atom listed above, possibly prefixed with
69 * a "*" to denote deref_tag().
71 * We parse given format string and sort specifiers, and make a list
72 * of properties that we need to extract out of objects. refinfo
73 * structure will hold an array of values extracted that can be
74 * indexed with the "atom number", which is an index into this
77 static const char **used_atom
;
78 static cmp_type
*used_atom_type
;
79 static int used_atom_cnt
, sort_atom_limit
, need_tagged
;
82 * Used to parse format string and sort specifiers
84 static int parse_atom(const char *atom
, const char *ep
)
91 if (*sp
== '*' && sp
< ep
)
94 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
96 /* Do we have the atom already used elsewhere? */
97 for (i
= 0; i
< used_atom_cnt
; i
++) {
98 int len
= strlen(used_atom
[i
]);
99 if (len
== ep
- atom
&& !memcmp(used_atom
[i
], atom
, len
))
103 /* Is the atom a valid one? */
104 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
105 int len
= strlen(valid_atom
[i
].name
);
106 if (len
== ep
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
110 if (ARRAY_SIZE(valid_atom
) <= i
)
111 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
113 /* Add it in, including the deref prefix */
116 used_atom
= xrealloc(used_atom
,
117 (sizeof *used_atom
) * used_atom_cnt
);
118 used_atom_type
= xrealloc(used_atom_type
,
119 (sizeof(*used_atom_type
) * used_atom_cnt
));
120 n
= xmalloc(ep
- atom
+ 1);
121 memcpy(n
, atom
, ep
- atom
);
124 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
129 * In a format string, find the next occurrence of %(atom).
131 static const char *find_next(const char *cp
)
135 /* %( is the start of an atom;
136 * %% is a quoteed per-cent.
140 else if (cp
[1] == '%')
141 cp
++; /* skip over two % */
142 /* otherwise this is a singleton, literal % */
150 * Make sure the format string is well formed, and parse out
153 static void verify_format(const char *format
)
156 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
157 const char *ep
= strchr(sp
, ')');
159 die("malformatted format string %s", sp
);
160 /* sp points at "%(" and ep points at the closing ")" */
161 parse_atom(sp
+ 2, ep
);
167 * Given an object name, read the object data and size, and return a
168 * "struct object". If the object data we are returning is also borrowed
169 * by the "struct object" representation, set *eaten as well---it is a
170 * signal from parse_object_buffer to us not to free the buffer.
172 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
175 void *buf
= read_sha1_file(sha1
, type
, sz
);
178 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
184 /* See grab_values */
185 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
189 for (i
= 0; i
< used_atom_cnt
; i
++) {
190 const char *name
= used_atom
[i
];
191 struct atom_value
*v
= &val
[i
];
192 if (!!deref
!= (*name
== '*'))
196 if (!strcmp(name
, "objecttype"))
197 v
->s
= type_names
[obj
->type
];
198 else if (!strcmp(name
, "objectsize")) {
199 char *s
= xmalloc(40);
200 sprintf(s
, "%lu", sz
);
204 else if (!strcmp(name
, "objectname")) {
205 char *s
= xmalloc(41);
206 strcpy(s
, sha1_to_hex(obj
->sha1
));
212 /* See grab_values */
213 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
216 struct tag
*tag
= (struct tag
*) obj
;
218 for (i
= 0; i
< used_atom_cnt
; i
++) {
219 const char *name
= used_atom
[i
];
220 struct atom_value
*v
= &val
[i
];
221 if (!!deref
!= (*name
== '*'))
225 if (!strcmp(name
, "tag"))
230 static int num_parents(struct commit
*commit
)
232 struct commit_list
*parents
;
235 for (i
= 0, parents
= commit
->parents
;
237 parents
= parents
->next
)
242 /* See grab_values */
243 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
246 struct commit
*commit
= (struct commit
*) obj
;
248 for (i
= 0; i
< used_atom_cnt
; i
++) {
249 const char *name
= used_atom
[i
];
250 struct atom_value
*v
= &val
[i
];
251 if (!!deref
!= (*name
== '*'))
255 if (!strcmp(name
, "tree")) {
256 char *s
= xmalloc(41);
257 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
260 if (!strcmp(name
, "numparent")) {
261 char *s
= xmalloc(40);
262 sprintf(s
, "%lu", v
->ul
);
264 v
->ul
= num_parents(commit
);
266 else if (!strcmp(name
, "parent")) {
267 int num
= num_parents(commit
);
269 struct commit_list
*parents
;
270 char *s
= xmalloc(42 * num
);
272 for (i
= 0, parents
= commit
->parents
;
274 parents
= parents
->next
, i
= i
+ 42) {
275 struct commit
*parent
= parents
->item
;
276 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
284 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
288 if (!strncmp(buf
, who
, wholen
) &&
290 return buf
+ wholen
+ 1;
291 eol
= strchr(buf
, '\n');
296 return ""; /* end of header */
302 static char *copy_line(const char *buf
)
304 const char *eol
= strchr(buf
, '\n');
310 line
= xmalloc(len
+ 1);
311 memcpy(line
, buf
, len
);
316 static char *copy_name(const char *buf
)
318 const char *eol
= strchr(buf
, '\n');
319 const char *eoname
= strstr(buf
, " <");
322 if (!(eoname
&& eol
&& eoname
< eol
))
325 line
= xmalloc(len
+ 1);
326 memcpy(line
, buf
, len
);
331 static char *copy_email(const char *buf
)
333 const char *email
= strchr(buf
, '<');
334 const char *eoemail
= strchr(email
, '>');
337 if (!email
|| !eoemail
)
340 len
= eoemail
- email
;
341 line
= xmalloc(len
+ 1);
342 memcpy(line
, email
, len
);
347 static void grab_date(const char *buf
, struct atom_value
*v
)
349 const char *eoemail
= strstr(buf
, "> ");
351 unsigned long timestamp
;
356 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
357 if (timestamp
== ULONG_MAX
)
359 tz
= strtol(zone
, NULL
, 10);
360 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
362 v
->s
= xstrdup(show_date(timestamp
, tz
, 0));
370 /* See grab_values */
371 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
374 int wholen
= strlen(who
);
375 const char *wholine
= NULL
;
377 for (i
= 0; i
< used_atom_cnt
; i
++) {
378 const char *name
= used_atom
[i
];
379 struct atom_value
*v
= &val
[i
];
380 if (!!deref
!= (*name
== '*'))
384 if (strncmp(who
, name
, wholen
))
386 if (name
[wholen
] != 0 &&
387 strcmp(name
+ wholen
, "name") &&
388 strcmp(name
+ wholen
, "email") &&
389 strcmp(name
+ wholen
, "date"))
392 wholine
= find_wholine(who
, wholen
, buf
, sz
);
394 return; /* no point looking for it */
395 if (name
[wholen
] == 0)
396 v
->s
= copy_line(wholine
);
397 else if (!strcmp(name
+ wholen
, "name"))
398 v
->s
= copy_name(wholine
);
399 else if (!strcmp(name
+ wholen
, "email"))
400 v
->s
= copy_email(wholine
);
401 else if (!strcmp(name
+ wholen
, "date"))
402 grab_date(wholine
, v
);
406 static void find_subpos(const char *buf
, unsigned long sz
, const char **sub
, const char **body
)
409 const char *eol
= strchr(buf
, '\n');
412 if (eol
[1] == '\n') {
414 break; /* found end of header */
422 *sub
= buf
; /* first non-empty line */
423 buf
= strchr(buf
, '\n');
425 return; /* no body */
427 buf
++; /* skip blank between subject and body */
431 /* See grab_values */
432 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
435 const char *subpos
= NULL
, *bodypos
= NULL
;
437 for (i
= 0; i
< used_atom_cnt
; i
++) {
438 const char *name
= used_atom
[i
];
439 struct atom_value
*v
= &val
[i
];
440 if (!!deref
!= (*name
== '*'))
444 if (strcmp(name
, "subject") &&
445 strcmp(name
, "body") &&
446 strcmp(name
, "contents"))
449 find_subpos(buf
, sz
, &subpos
, &bodypos
);
453 if (!strcmp(name
, "subject"))
454 v
->s
= copy_line(subpos
);
455 else if (!strcmp(name
, "body"))
457 else if (!strcmp(name
, "contents"))
462 /* We want to have empty print-string for field requests
463 * that do not apply (e.g. "authordate" for a tag object)
465 static void fill_missing_values(struct atom_value
*val
)
468 for (i
= 0; i
< used_atom_cnt
; i
++) {
469 struct atom_value
*v
= &val
[i
];
476 * val is a list of atom_value to hold returned values. Extract
477 * the values for atoms in used_atom array out of (obj, buf, sz).
478 * when deref is false, (obj, buf, sz) is the object that is
479 * pointed at by the ref itself; otherwise it is the object the
480 * ref (which is a tag) refers to.
482 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
484 grab_common_values(val
, deref
, obj
, buf
, sz
);
487 grab_tag_values(val
, deref
, obj
, buf
, sz
);
488 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
489 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
492 grab_commit_values(val
, deref
, obj
, buf
, sz
);
493 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
494 grab_person("author", val
, deref
, obj
, buf
, sz
);
495 grab_person("committer", val
, deref
, obj
, buf
, sz
);
498 // grab_tree_values(val, deref, obj, buf, sz);
501 // grab_blob_values(val, deref, obj, buf, sz);
504 die("Eh? Object of type %d?", obj
->type
);
509 * Parse the object referred by ref, and grab needed value.
511 static void populate_value(struct refinfo
*ref
)
517 const unsigned char *tagged
;
519 ref
->value
= xcalloc(sizeof(struct atom_value
), used_atom_cnt
);
521 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
523 die("missing object %s for %s",
524 sha1_to_hex(ref
->objectname
), ref
->refname
);
526 die("parse_object_buffer failed on %s for %s",
527 sha1_to_hex(ref
->objectname
), ref
->refname
);
529 /* Fill in specials first */
530 for (i
= 0; i
< used_atom_cnt
; i
++) {
531 const char *name
= used_atom
[i
];
532 struct atom_value
*v
= &ref
->value
[i
];
533 if (!strcmp(name
, "refname"))
535 else if (!strcmp(name
, "*refname")) {
536 int len
= strlen(ref
->refname
);
537 char *s
= xmalloc(len
+ 4);
538 sprintf(s
, "%s^{}", ref
->refname
);
543 grab_values(ref
->value
, 0, obj
, buf
, size
);
547 /* If there is no atom that wants to know about tagged
548 * object, we are done.
550 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
553 /* If it is a tag object, see if we use a value that derefs
554 * the object, and if we do grab the object it refers to.
556 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
558 /* NEEDSWORK: This derefs tag only once, which
559 * is good to deal with chains of trust, but
560 * is not consistent with what deref_tag() does
561 * which peels the onion to the core.
563 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
565 die("missing object %s for %s",
566 sha1_to_hex(tagged
), ref
->refname
);
568 die("parse_object_buffer failed on %s for %s",
569 sha1_to_hex(tagged
), ref
->refname
);
570 grab_values(ref
->value
, 1, obj
, buf
, size
);
576 * Given a ref, return the value for the atom. This lazily gets value
577 * out of the object by calling populate value.
579 static void get_value(struct refinfo
*ref
, int atom
, struct atom_value
**v
)
583 fill_missing_values(ref
->value
);
585 *v
= &ref
->value
[atom
];
588 struct grab_ref_cbdata
{
589 struct refinfo
**grab_array
;
590 const char **grab_pattern
;
595 * A call-back given to for_each_ref(). It is unfortunate that we
596 * need to use global variables to pass extra information to this
599 static int grab_single_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
601 struct grab_ref_cbdata
*cb
= cb_data
;
605 if (*cb
->grab_pattern
) {
606 const char **pattern
;
607 int namelen
= strlen(refname
);
608 for (pattern
= cb
->grab_pattern
; *pattern
; pattern
++) {
609 const char *p
= *pattern
;
610 int plen
= strlen(p
);
612 if ((plen
<= namelen
) &&
613 !strncmp(refname
, p
, plen
) &&
614 (refname
[plen
] == '\0' ||
615 refname
[plen
] == '/'))
617 if (!fnmatch(p
, refname
, FNM_PATHNAME
))
624 /* We do not open the object yet; sort may only need refname
625 * to do its job and the resulting list may yet to be pruned
628 ref
= xcalloc(1, sizeof(*ref
));
629 ref
->refname
= xstrdup(refname
);
630 hashcpy(ref
->objectname
, sha1
);
633 cb
->grab_array
= xrealloc(cb
->grab_array
,
634 sizeof(*cb
->grab_array
) * (cnt
+ 1));
635 cb
->grab_array
[cnt
++] = ref
;
640 static int cmp_ref_sort(struct ref_sort
*s
, struct refinfo
*a
, struct refinfo
*b
)
642 struct atom_value
*va
, *vb
;
644 cmp_type cmp_type
= used_atom_type
[s
->atom
];
646 get_value(a
, s
->atom
, &va
);
647 get_value(b
, s
->atom
, &vb
);
650 cmp
= strcmp(va
->s
, vb
->s
);
655 else if (va
->ul
== vb
->ul
)
661 return (s
->reverse
) ? -cmp
: cmp
;
664 static struct ref_sort
*ref_sort
;
665 static int compare_refs(const void *a_
, const void *b_
)
667 struct refinfo
*a
= *((struct refinfo
**)a_
);
668 struct refinfo
*b
= *((struct refinfo
**)b_
);
671 for (s
= ref_sort
; s
; s
= s
->next
) {
672 int cmp
= cmp_ref_sort(s
, a
, b
);
679 static void sort_refs(struct ref_sort
*sort
, struct refinfo
**refs
, int num_refs
)
682 qsort(refs
, num_refs
, sizeof(struct refinfo
*), compare_refs
);
685 static void print_value(struct refinfo
*ref
, int atom
, int quote_style
)
687 struct atom_value
*v
;
688 get_value(ref
, atom
, &v
);
689 switch (quote_style
) {
694 sq_quote_print(stdout
, v
->s
);
697 perl_quote_print(stdout
, v
->s
);
700 python_quote_print(stdout
, v
->s
);
705 static int hex1(char ch
)
707 if ('0' <= ch
&& ch
<= '9')
709 else if ('a' <= ch
&& ch
<= 'f')
710 return ch
- 'a' + 10;
711 else if ('A' <= ch
&& ch
<= 'F')
712 return ch
- 'A' + 10;
715 static int hex2(const char *cp
)
718 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
723 static void emit(const char *cp
, const char *ep
)
725 while (*cp
&& (!ep
|| cp
< ep
)) {
730 int ch
= hex2(cp
+ 1);
743 static void show_ref(struct refinfo
*info
, const char *format
, int quote_style
)
745 const char *cp
, *sp
, *ep
;
747 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
748 ep
= strchr(sp
, ')');
751 print_value(info
, parse_atom(sp
+ 2, ep
), quote_style
);
754 sp
= cp
+ strlen(cp
);
760 static struct ref_sort
*default_sort(void)
762 static const char cstr_name
[] = "refname";
764 struct ref_sort
*sort
= xcalloc(1, sizeof(*sort
));
767 sort
->atom
= parse_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
771 int cmd_for_each_ref(int ac
, const char **av
, char *prefix
)
774 const char *format
= NULL
;
775 struct ref_sort
*sort
= NULL
, **sort_tail
= &sort
;
777 int quote_style
= -1; /* unspecified yet */
778 struct refinfo
**refs
;
779 struct grab_ref_cbdata cbdata
;
781 for (i
= 1; i
< ac
; i
++) {
782 const char *arg
= av
[i
];
785 if (!strcmp(arg
, "--")) {
789 if (!strncmp(arg
, "--format=", 9)) {
791 die("more than one --format?");
795 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--shell") ) {
796 if (0 <= quote_style
)
797 die("more than one quoting style?");
798 quote_style
= QUOTE_SHELL
;
801 if (!strcmp(arg
, "-p") || !strcmp(arg
, "--perl") ) {
802 if (0 <= quote_style
)
803 die("more than one quoting style?");
804 quote_style
= QUOTE_PERL
;
807 if (!strcmp(arg
, "--python") ) {
808 if (0 <= quote_style
)
809 die("more than one quoting style?");
810 quote_style
= QUOTE_PYTHON
;
813 if (!strncmp(arg
, "--count=", 8)) {
815 die("more than one --count?");
816 maxcount
= atoi(arg
+ 8);
818 die("The number %s did not parse", arg
);
821 if (!strncmp(arg
, "--sort=", 7)) {
822 struct ref_sort
*s
= xcalloc(1, sizeof(*s
));
827 sort_tail
= &s
->next
;
835 sort
->atom
= parse_atom(arg
, arg
+len
);
841 quote_style
= QUOTE_NONE
;
844 sort
= default_sort();
845 sort_atom_limit
= used_atom_cnt
;
847 format
= "%(objectname) %(objecttype)\t%(refname)";
849 verify_format(format
);
851 memset(&cbdata
, 0, sizeof(cbdata
));
852 cbdata
.grab_pattern
= av
+ i
;
853 for_each_ref(grab_single_ref
, &cbdata
);
854 refs
= cbdata
.grab_array
;
855 num_refs
= cbdata
.grab_cnt
;
857 for (i
= 0; i
< used_atom_cnt
; i
++) {
858 if (used_atom
[i
][0] == '*') {
864 sort_refs(sort
, refs
, num_refs
);
866 if (!maxcount
|| num_refs
< maxcount
)
868 for (i
= 0; i
< maxcount
; i
++)
869 show_ref(refs
[i
], format
, quote_style
);