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 static struct refinfo
**grab_array
;
589 static const char **grab_pattern
;
590 static int *grab_cnt
;
593 * A call-back given to for_each_ref(). It is unfortunate that we
594 * need to use global variables to pass extra information to this
597 static int grab_single_ref(const char *refname
, const unsigned char *sha1
)
603 const char **pattern
;
604 int namelen
= strlen(refname
);
605 for (pattern
= grab_pattern
; *pattern
; pattern
++) {
606 const char *p
= *pattern
;
607 int plen
= strlen(p
);
609 if ((plen
<= namelen
) &&
610 !strncmp(refname
, p
, plen
) &&
611 (refname
[plen
] == '\0' ||
612 refname
[plen
] == '/'))
614 if (!fnmatch(p
, refname
, FNM_PATHNAME
))
621 /* We do not open the object yet; sort may only need refname
622 * to do its job and the resulting list may yet to be pruned
625 ref
= xcalloc(1, sizeof(*ref
));
626 ref
->refname
= xstrdup(refname
);
627 hashcpy(ref
->objectname
, sha1
);
630 grab_array
= xrealloc(grab_array
, sizeof(*grab_array
) * (cnt
+ 1));
631 grab_array
[cnt
++] = ref
;
636 static struct refinfo
**grab_refs(const char **pattern
, int *cnt
)
638 /* Sheesh, we really should make for-each-ref to take
642 grab_pattern
= pattern
;
644 for_each_ref(grab_single_ref
);
648 static int cmp_ref_sort(struct ref_sort
*s
, struct refinfo
*a
, struct refinfo
*b
)
650 struct atom_value
*va
, *vb
;
652 cmp_type cmp_type
= used_atom_type
[s
->atom
];
654 get_value(a
, s
->atom
, &va
);
655 get_value(b
, s
->atom
, &vb
);
658 cmp
= strcmp(va
->s
, vb
->s
);
663 else if (va
->ul
== vb
->ul
)
669 return (s
->reverse
) ? -cmp
: cmp
;
672 static struct ref_sort
*ref_sort
;
673 static int compare_refs(const void *a_
, const void *b_
)
675 struct refinfo
*a
= *((struct refinfo
**)a_
);
676 struct refinfo
*b
= *((struct refinfo
**)b_
);
679 for (s
= ref_sort
; s
; s
= s
->next
) {
680 int cmp
= cmp_ref_sort(s
, a
, b
);
687 static void sort_refs(struct ref_sort
*sort
, struct refinfo
**refs
, int num_refs
)
690 qsort(refs
, num_refs
, sizeof(struct refinfo
*), compare_refs
);
693 static void print_value(struct refinfo
*ref
, int atom
, int quote_style
)
695 struct atom_value
*v
;
696 get_value(ref
, atom
, &v
);
697 switch (quote_style
) {
702 sq_quote_print(stdout
, v
->s
);
705 perl_quote_print(stdout
, v
->s
);
708 python_quote_print(stdout
, v
->s
);
713 static int hex1(char ch
)
715 if ('0' <= ch
&& ch
<= '9')
717 else if ('a' <= ch
&& ch
<= 'f')
718 return ch
- 'a' + 10;
719 else if ('A' <= ch
&& ch
<= 'F')
720 return ch
- 'A' + 10;
723 static int hex2(const char *cp
)
726 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
731 static void emit(const char *cp
, const char *ep
)
733 while (*cp
&& (!ep
|| cp
< ep
)) {
738 int ch
= hex2(cp
+ 1);
751 static void show_ref(struct refinfo
*info
, const char *format
, int quote_style
)
753 const char *cp
, *sp
, *ep
;
755 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
756 ep
= strchr(sp
, ')');
759 print_value(info
, parse_atom(sp
+ 2, ep
), quote_style
);
762 sp
= cp
+ strlen(cp
);
768 static struct ref_sort
*default_sort(void)
770 static const char cstr_name
[] = "refname";
772 struct ref_sort
*sort
= xcalloc(1, sizeof(*sort
));
775 sort
->atom
= parse_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
779 int cmd_for_each_ref(int ac
, const char **av
, char *prefix
)
782 const char *format
= NULL
;
783 struct ref_sort
*sort
= NULL
, **sort_tail
= &sort
;
785 int quote_style
= -1; /* unspecified yet */
786 struct refinfo
**refs
;
788 for (i
= 1; i
< ac
; i
++) {
789 const char *arg
= av
[i
];
792 if (!strcmp(arg
, "--")) {
796 if (!strncmp(arg
, "--format=", 9)) {
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 (!strncmp(arg
, "--count=", 8)) {
822 die("more than one --count?");
823 maxcount
= atoi(arg
+ 8);
825 die("The number %s did not parse", arg
);
828 if (!strncmp(arg
, "--sort=", 7)) {
829 struct ref_sort
*s
= xcalloc(1, sizeof(*s
));
834 sort_tail
= &s
->next
;
842 sort
->atom
= parse_atom(arg
, arg
+len
);
848 quote_style
= QUOTE_NONE
;
851 sort
= default_sort();
852 sort_atom_limit
= used_atom_cnt
;
854 format
= "%(objectname) %(objecttype)\t%(refname)";
856 verify_format(format
);
858 refs
= grab_refs(av
+ i
, &num_refs
);
860 for (i
= 0; i
< used_atom_cnt
; i
++) {
861 if (used_atom
[i
][0] == '*') {
867 sort_refs(sort
, refs
, num_refs
);
869 if (!maxcount
|| num_refs
< maxcount
)
871 for (i
= 0; i
< maxcount
; i
++)
872 show_ref(refs
[i
], format
, quote_style
);