14 #define QUOTE_PYTHON 3
16 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
20 unsigned long ul
; /* used for sorting when not FIELD_STR */
24 struct ref_sort
*next
;
25 int atom
; /* index into used_atom array */
31 unsigned char objectname
[20];
32 struct atom_value
*value
;
41 { "objectsize", FIELD_ULONG
},
44 { "parent" }, /* NEEDSWORK: how to address 2nd and later parents? */
45 { "numparent", FIELD_ULONG
},
52 { "authordate", FIELD_TIME
},
56 { "committerdate", FIELD_TIME
},
60 { "taggerdate", FIELD_TIME
},
62 { "creatordate", FIELD_TIME
},
69 * An atom is a valid field atom listed above, possibly prefixed with
70 * a "*" to denote deref_tag().
72 * We parse given format string and sort specifiers, and make a list
73 * of properties that we need to extract out of objects. refinfo
74 * structure will hold an array of values extracted that can be
75 * indexed with the "atom number", which is an index into this
78 static const char **used_atom
;
79 static cmp_type
*used_atom_type
;
80 static int used_atom_cnt
, sort_atom_limit
, need_tagged
;
83 * Used to parse format string and sort specifiers
85 static int parse_atom(const char *atom
, const char *ep
)
92 if (*sp
== '*' && sp
< ep
)
95 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
97 /* Do we have the atom already used elsewhere? */
98 for (i
= 0; i
< used_atom_cnt
; i
++) {
99 int len
= strlen(used_atom
[i
]);
100 if (len
== ep
- atom
&& !memcmp(used_atom
[i
], atom
, len
))
104 /* Is the atom a valid one? */
105 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
106 int len
= strlen(valid_atom
[i
].name
);
107 if (len
== ep
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
111 if (ARRAY_SIZE(valid_atom
) <= i
)
112 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
114 /* Add it in, including the deref prefix */
117 used_atom
= xrealloc(used_atom
,
118 (sizeof *used_atom
) * used_atom_cnt
);
119 used_atom_type
= xrealloc(used_atom_type
,
120 (sizeof(*used_atom_type
) * used_atom_cnt
));
121 n
= xmalloc(ep
- atom
+ 1);
122 memcpy(n
, atom
, ep
- atom
);
125 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
130 * In a format string, find the next occurrence of %(atom).
132 static const char *find_next(const char *cp
)
136 /* %( is the start of an atom;
137 * %% is a quoteed per-cent.
141 else if (cp
[1] == '%')
142 cp
++; /* skip over two % */
143 /* otherwise this is a singleton, literal % */
151 * Make sure the format string is well formed, and parse out
154 static void verify_format(const char *format
)
157 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
158 const char *ep
= strchr(sp
, ')');
160 die("malformatted format string %s", sp
);
161 /* sp points at "%(" and ep points at the closing ")" */
162 parse_atom(sp
+ 2, ep
);
168 * Given an object name, read the object data and size, and return a
169 * "struct object". If the object data we are returning is also borrowed
170 * by the "struct object" representation, set *eaten as well---it is a
171 * signal from parse_object_buffer to us not to free the buffer.
173 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
176 void *buf
= read_sha1_file(sha1
, type
, sz
);
179 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
185 /* See grab_values */
186 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
190 for (i
= 0; i
< used_atom_cnt
; i
++) {
191 const char *name
= used_atom
[i
];
192 struct atom_value
*v
= &val
[i
];
193 if (!!deref
!= (*name
== '*'))
197 if (!strcmp(name
, "objecttype"))
198 v
->s
= type_names
[obj
->type
];
199 else if (!strcmp(name
, "objectsize")) {
200 char *s
= xmalloc(40);
201 sprintf(s
, "%lu", sz
);
205 else if (!strcmp(name
, "objectname")) {
206 char *s
= xmalloc(41);
207 strcpy(s
, sha1_to_hex(obj
->sha1
));
213 /* See grab_values */
214 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
217 struct tag
*tag
= (struct tag
*) obj
;
219 for (i
= 0; i
< used_atom_cnt
; i
++) {
220 const char *name
= used_atom
[i
];
221 struct atom_value
*v
= &val
[i
];
222 if (!!deref
!= (*name
== '*'))
226 if (!strcmp(name
, "tag"))
231 static int num_parents(struct commit
*commit
)
233 struct commit_list
*parents
;
236 for (i
= 0, parents
= commit
->parents
;
238 parents
= parents
->next
)
243 /* See grab_values */
244 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
247 struct commit
*commit
= (struct commit
*) obj
;
249 for (i
= 0; i
< used_atom_cnt
; i
++) {
250 const char *name
= used_atom
[i
];
251 struct atom_value
*v
= &val
[i
];
252 if (!!deref
!= (*name
== '*'))
256 if (!strcmp(name
, "tree")) {
257 char *s
= xmalloc(41);
258 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
261 if (!strcmp(name
, "numparent")) {
262 char *s
= xmalloc(40);
263 sprintf(s
, "%lu", v
->ul
);
265 v
->ul
= num_parents(commit
);
267 else if (!strcmp(name
, "parent")) {
268 int num
= num_parents(commit
);
270 struct commit_list
*parents
;
271 char *s
= xmalloc(42 * num
);
273 for (i
= 0, parents
= commit
->parents
;
275 parents
= parents
->next
, i
= i
+ 42) {
276 struct commit
*parent
= parents
->item
;
277 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
285 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
289 if (!strncmp(buf
, who
, wholen
) &&
291 return buf
+ wholen
+ 1;
292 eol
= strchr(buf
, '\n');
297 return ""; /* end of header */
303 static char *copy_line(const char *buf
)
305 const char *eol
= strchr(buf
, '\n');
311 line
= xmalloc(len
+ 1);
312 memcpy(line
, buf
, len
);
317 static char *copy_name(const char *buf
)
319 const char *eol
= strchr(buf
, '\n');
320 const char *eoname
= strstr(buf
, " <");
323 if (!(eoname
&& eol
&& eoname
< eol
))
326 line
= xmalloc(len
+ 1);
327 memcpy(line
, buf
, len
);
332 static char *copy_email(const char *buf
)
334 const char *email
= strchr(buf
, '<');
335 const char *eoemail
= strchr(email
, '>');
338 if (!email
|| !eoemail
)
341 len
= eoemail
- email
;
342 line
= xmalloc(len
+ 1);
343 memcpy(line
, email
, len
);
348 static void grab_date(const char *buf
, struct atom_value
*v
)
350 const char *eoemail
= strstr(buf
, "> ");
352 unsigned long timestamp
;
357 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
358 if (timestamp
== ULONG_MAX
)
360 tz
= strtol(zone
, NULL
, 10);
361 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
363 v
->s
= xstrdup(show_date(timestamp
, tz
, 0));
371 /* See grab_values */
372 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
375 int wholen
= strlen(who
);
376 const char *wholine
= NULL
;
378 for (i
= 0; i
< used_atom_cnt
; i
++) {
379 const char *name
= used_atom
[i
];
380 struct atom_value
*v
= &val
[i
];
381 if (!!deref
!= (*name
== '*'))
385 if (strncmp(who
, name
, wholen
))
387 if (name
[wholen
] != 0 &&
388 strcmp(name
+ wholen
, "name") &&
389 strcmp(name
+ wholen
, "email") &&
390 strcmp(name
+ wholen
, "date"))
393 wholine
= find_wholine(who
, wholen
, buf
, sz
);
395 return; /* no point looking for it */
396 if (name
[wholen
] == 0)
397 v
->s
= copy_line(wholine
);
398 else if (!strcmp(name
+ wholen
, "name"))
399 v
->s
= copy_name(wholine
);
400 else if (!strcmp(name
+ wholen
, "email"))
401 v
->s
= copy_email(wholine
);
402 else if (!strcmp(name
+ wholen
, "date"))
403 grab_date(wholine
, v
);
406 /* For a tag or a commit object, if "creator" or "creatordate" is
407 * requested, do something special.
409 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
410 return; /* "author" for commit object is not wanted */
412 wholine
= find_wholine(who
, wholen
, buf
, sz
);
415 for (i
= 0; i
< used_atom_cnt
; i
++) {
416 const char *name
= used_atom
[i
];
417 struct atom_value
*v
= &val
[i
];
418 if (!!deref
!= (*name
== '*'))
423 if (!strcmp(name
, "creatordate"))
424 grab_date(wholine
, v
);
425 else if (!strcmp(name
, "creator"))
426 v
->s
= copy_line(wholine
);
430 static void find_subpos(const char *buf
, unsigned long sz
, const char **sub
, const char **body
)
433 const char *eol
= strchr(buf
, '\n');
436 if (eol
[1] == '\n') {
438 break; /* found end of header */
446 *sub
= buf
; /* first non-empty line */
447 buf
= strchr(buf
, '\n');
449 return; /* no body */
451 buf
++; /* skip blank between subject and body */
455 /* See grab_values */
456 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
459 const char *subpos
= NULL
, *bodypos
= NULL
;
461 for (i
= 0; i
< used_atom_cnt
; i
++) {
462 const char *name
= used_atom
[i
];
463 struct atom_value
*v
= &val
[i
];
464 if (!!deref
!= (*name
== '*'))
468 if (strcmp(name
, "subject") &&
469 strcmp(name
, "body") &&
470 strcmp(name
, "contents"))
473 find_subpos(buf
, sz
, &subpos
, &bodypos
);
477 if (!strcmp(name
, "subject"))
478 v
->s
= copy_line(subpos
);
479 else if (!strcmp(name
, "body"))
480 v
->s
= xstrdup(bodypos
);
481 else if (!strcmp(name
, "contents"))
482 v
->s
= xstrdup(subpos
);
486 /* We want to have empty print-string for field requests
487 * that do not apply (e.g. "authordate" for a tag object)
489 static void fill_missing_values(struct atom_value
*val
)
492 for (i
= 0; i
< used_atom_cnt
; i
++) {
493 struct atom_value
*v
= &val
[i
];
500 * val is a list of atom_value to hold returned values. Extract
501 * the values for atoms in used_atom array out of (obj, buf, sz).
502 * when deref is false, (obj, buf, sz) is the object that is
503 * pointed at by the ref itself; otherwise it is the object the
504 * ref (which is a tag) refers to.
506 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
508 grab_common_values(val
, deref
, obj
, buf
, sz
);
511 grab_tag_values(val
, deref
, obj
, buf
, sz
);
512 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
513 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
516 grab_commit_values(val
, deref
, obj
, buf
, sz
);
517 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
518 grab_person("author", val
, deref
, obj
, buf
, sz
);
519 grab_person("committer", val
, deref
, obj
, buf
, sz
);
522 // grab_tree_values(val, deref, obj, buf, sz);
525 // grab_blob_values(val, deref, obj, buf, sz);
528 die("Eh? Object of type %d?", obj
->type
);
533 * Parse the object referred by ref, and grab needed value.
535 static void populate_value(struct refinfo
*ref
)
541 const unsigned char *tagged
;
543 ref
->value
= xcalloc(sizeof(struct atom_value
), used_atom_cnt
);
545 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
547 die("missing object %s for %s",
548 sha1_to_hex(ref
->objectname
), ref
->refname
);
550 die("parse_object_buffer failed on %s for %s",
551 sha1_to_hex(ref
->objectname
), ref
->refname
);
553 /* Fill in specials first */
554 for (i
= 0; i
< used_atom_cnt
; i
++) {
555 const char *name
= used_atom
[i
];
556 struct atom_value
*v
= &ref
->value
[i
];
557 if (!strcmp(name
, "refname"))
559 else if (!strcmp(name
, "*refname")) {
560 int len
= strlen(ref
->refname
);
561 char *s
= xmalloc(len
+ 4);
562 sprintf(s
, "%s^{}", ref
->refname
);
567 grab_values(ref
->value
, 0, obj
, buf
, size
);
571 /* If there is no atom that wants to know about tagged
572 * object, we are done.
574 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
577 /* If it is a tag object, see if we use a value that derefs
578 * the object, and if we do grab the object it refers to.
580 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
582 /* NEEDSWORK: This derefs tag only once, which
583 * is good to deal with chains of trust, but
584 * is not consistent with what deref_tag() does
585 * which peels the onion to the core.
587 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
589 die("missing object %s for %s",
590 sha1_to_hex(tagged
), ref
->refname
);
592 die("parse_object_buffer failed on %s for %s",
593 sha1_to_hex(tagged
), ref
->refname
);
594 grab_values(ref
->value
, 1, obj
, buf
, size
);
600 * Given a ref, return the value for the atom. This lazily gets value
601 * out of the object by calling populate value.
603 static void get_value(struct refinfo
*ref
, int atom
, struct atom_value
**v
)
607 fill_missing_values(ref
->value
);
609 *v
= &ref
->value
[atom
];
612 struct grab_ref_cbdata
{
613 struct refinfo
**grab_array
;
614 const char **grab_pattern
;
619 * A call-back given to for_each_ref(). It is unfortunate that we
620 * need to use global variables to pass extra information to this
623 static int grab_single_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
625 struct grab_ref_cbdata
*cb
= cb_data
;
629 if (*cb
->grab_pattern
) {
630 const char **pattern
;
631 int namelen
= strlen(refname
);
632 for (pattern
= cb
->grab_pattern
; *pattern
; pattern
++) {
633 const char *p
= *pattern
;
634 int plen
= strlen(p
);
636 if ((plen
<= namelen
) &&
637 !strncmp(refname
, p
, plen
) &&
638 (refname
[plen
] == '\0' ||
639 refname
[plen
] == '/'))
641 if (!fnmatch(p
, refname
, FNM_PATHNAME
))
648 /* We do not open the object yet; sort may only need refname
649 * to do its job and the resulting list may yet to be pruned
652 ref
= xcalloc(1, sizeof(*ref
));
653 ref
->refname
= xstrdup(refname
);
654 hashcpy(ref
->objectname
, sha1
);
657 cb
->grab_array
= xrealloc(cb
->grab_array
,
658 sizeof(*cb
->grab_array
) * (cnt
+ 1));
659 cb
->grab_array
[cnt
++] = ref
;
664 static int cmp_ref_sort(struct ref_sort
*s
, struct refinfo
*a
, struct refinfo
*b
)
666 struct atom_value
*va
, *vb
;
668 cmp_type cmp_type
= used_atom_type
[s
->atom
];
670 get_value(a
, s
->atom
, &va
);
671 get_value(b
, s
->atom
, &vb
);
674 cmp
= strcmp(va
->s
, vb
->s
);
679 else if (va
->ul
== vb
->ul
)
685 return (s
->reverse
) ? -cmp
: cmp
;
688 static struct ref_sort
*ref_sort
;
689 static int compare_refs(const void *a_
, const void *b_
)
691 struct refinfo
*a
= *((struct refinfo
**)a_
);
692 struct refinfo
*b
= *((struct refinfo
**)b_
);
695 for (s
= ref_sort
; s
; s
= s
->next
) {
696 int cmp
= cmp_ref_sort(s
, a
, b
);
703 static void sort_refs(struct ref_sort
*sort
, struct refinfo
**refs
, int num_refs
)
706 qsort(refs
, num_refs
, sizeof(struct refinfo
*), compare_refs
);
709 static void print_value(struct refinfo
*ref
, int atom
, int quote_style
)
711 struct atom_value
*v
;
712 get_value(ref
, atom
, &v
);
713 switch (quote_style
) {
718 sq_quote_print(stdout
, v
->s
);
721 perl_quote_print(stdout
, v
->s
);
724 python_quote_print(stdout
, v
->s
);
729 static int hex1(char ch
)
731 if ('0' <= ch
&& ch
<= '9')
733 else if ('a' <= ch
&& ch
<= 'f')
734 return ch
- 'a' + 10;
735 else if ('A' <= ch
&& ch
<= 'F')
736 return ch
- 'A' + 10;
739 static int hex2(const char *cp
)
742 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
747 static void emit(const char *cp
, const char *ep
)
749 while (*cp
&& (!ep
|| cp
< ep
)) {
754 int ch
= hex2(cp
+ 1);
767 static void show_ref(struct refinfo
*info
, const char *format
, int quote_style
)
769 const char *cp
, *sp
, *ep
;
771 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
772 ep
= strchr(sp
, ')');
775 print_value(info
, parse_atom(sp
+ 2, ep
), quote_style
);
778 sp
= cp
+ strlen(cp
);
784 static struct ref_sort
*default_sort(void)
786 static const char cstr_name
[] = "refname";
788 struct ref_sort
*sort
= xcalloc(1, sizeof(*sort
));
791 sort
->atom
= parse_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
795 int cmd_for_each_ref(int ac
, const char **av
, char *prefix
)
798 const char *format
= NULL
;
799 struct ref_sort
*sort
= NULL
, **sort_tail
= &sort
;
801 int quote_style
= -1; /* unspecified yet */
802 struct refinfo
**refs
;
803 struct grab_ref_cbdata cbdata
;
805 for (i
= 1; i
< ac
; i
++) {
806 const char *arg
= av
[i
];
809 if (!strcmp(arg
, "--")) {
813 if (!strncmp(arg
, "--format=", 9)) {
815 die("more than one --format?");
819 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--shell") ) {
820 if (0 <= quote_style
)
821 die("more than one quoting style?");
822 quote_style
= QUOTE_SHELL
;
825 if (!strcmp(arg
, "-p") || !strcmp(arg
, "--perl") ) {
826 if (0 <= quote_style
)
827 die("more than one quoting style?");
828 quote_style
= QUOTE_PERL
;
831 if (!strcmp(arg
, "--python") ) {
832 if (0 <= quote_style
)
833 die("more than one quoting style?");
834 quote_style
= QUOTE_PYTHON
;
837 if (!strncmp(arg
, "--count=", 8)) {
839 die("more than one --count?");
840 maxcount
= atoi(arg
+ 8);
842 die("The number %s did not parse", arg
);
845 if (!strncmp(arg
, "--sort=", 7)) {
846 struct ref_sort
*s
= xcalloc(1, sizeof(*s
));
851 sort_tail
= &s
->next
;
859 sort
->atom
= parse_atom(arg
, arg
+len
);
865 quote_style
= QUOTE_NONE
;
868 sort
= default_sort();
869 sort_atom_limit
= used_atom_cnt
;
871 format
= "%(objectname) %(objecttype)\t%(refname)";
873 verify_format(format
);
875 memset(&cbdata
, 0, sizeof(cbdata
));
876 cbdata
.grab_pattern
= av
+ i
;
877 for_each_ref(grab_single_ref
, &cbdata
);
878 refs
= cbdata
.grab_array
;
879 num_refs
= cbdata
.grab_cnt
;
881 for (i
= 0; i
< used_atom_cnt
; i
++) {
882 if (used_atom
[i
][0] == '*') {
888 sort_refs(sort
, refs
, num_refs
);
890 if (!maxcount
|| num_refs
< maxcount
)
892 for (i
= 0; i
< maxcount
; i
++)
893 show_ref(refs
[i
], format
, quote_style
);