]>
Commit | Line | Data |
---|---|---|
1 | #include "builtin.h" | |
2 | #include "cache.h" | |
3 | #include "refs.h" | |
4 | #include "object.h" | |
5 | #include "tag.h" | |
6 | #include "commit.h" | |
7 | #include "tree.h" | |
8 | #include "blob.h" | |
9 | #include "quote.h" | |
10 | ||
11 | /* Quoting styles */ | |
12 | #define QUOTE_NONE 0 | |
13 | #define QUOTE_SHELL 1 | |
14 | #define QUOTE_PERL 2 | |
15 | #define QUOTE_PYTHON 3 | |
16 | #define QUOTE_TCL 4 | |
17 | ||
18 | typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type; | |
19 | ||
20 | struct atom_value { | |
21 | const char *s; | |
22 | unsigned long ul; /* used for sorting when not FIELD_STR */ | |
23 | }; | |
24 | ||
25 | struct ref_sort { | |
26 | struct ref_sort *next; | |
27 | int atom; /* index into used_atom array */ | |
28 | unsigned reverse : 1; | |
29 | }; | |
30 | ||
31 | struct refinfo { | |
32 | char *refname; | |
33 | unsigned char objectname[20]; | |
34 | struct atom_value *value; | |
35 | }; | |
36 | ||
37 | static struct { | |
38 | const char *name; | |
39 | cmp_type cmp_type; | |
40 | } valid_atom[] = { | |
41 | { "refname" }, | |
42 | { "objecttype" }, | |
43 | { "objectsize", FIELD_ULONG }, | |
44 | { "objectname" }, | |
45 | { "tree" }, | |
46 | { "parent" }, /* NEEDSWORK: how to address 2nd and later parents? */ | |
47 | { "numparent", FIELD_ULONG }, | |
48 | { "object" }, | |
49 | { "type" }, | |
50 | { "tag" }, | |
51 | { "author" }, | |
52 | { "authorname" }, | |
53 | { "authoremail" }, | |
54 | { "authordate", FIELD_TIME }, | |
55 | { "committer" }, | |
56 | { "committername" }, | |
57 | { "committeremail" }, | |
58 | { "committerdate", FIELD_TIME }, | |
59 | { "tagger" }, | |
60 | { "taggername" }, | |
61 | { "taggeremail" }, | |
62 | { "taggerdate", FIELD_TIME }, | |
63 | { "creator" }, | |
64 | { "creatordate", FIELD_TIME }, | |
65 | { "subject" }, | |
66 | { "body" }, | |
67 | { "contents" }, | |
68 | }; | |
69 | ||
70 | /* | |
71 | * An atom is a valid field atom listed above, possibly prefixed with | |
72 | * a "*" to denote deref_tag(). | |
73 | * | |
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 | |
78 | * array. | |
79 | */ | |
80 | static const char **used_atom; | |
81 | static cmp_type *used_atom_type; | |
82 | static int used_atom_cnt, sort_atom_limit, need_tagged; | |
83 | ||
84 | /* | |
85 | * Used to parse format string and sort specifiers | |
86 | */ | |
87 | static int parse_atom(const char *atom, const char *ep) | |
88 | { | |
89 | const char *sp; | |
90 | int i, at; | |
91 | ||
92 | sp = atom; | |
93 | if (*sp == '*' && sp < ep) | |
94 | sp++; /* deref */ | |
95 | if (ep <= sp) | |
96 | die("malformed field name: %.*s", (int)(ep-atom), atom); | |
97 | ||
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)) | |
102 | return i; | |
103 | } | |
104 | ||
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)) | |
109 | break; | |
110 | } | |
111 | ||
112 | if (ARRAY_SIZE(valid_atom) <= i) | |
113 | die("unknown field name: %.*s", (int)(ep-atom), atom); | |
114 | ||
115 | /* Add it in, including the deref prefix */ | |
116 | at = used_atom_cnt; | |
117 | used_atom_cnt++; | |
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 | used_atom[at] = xmemdupz(atom, ep - atom); | |
123 | used_atom_type[at] = valid_atom[i].cmp_type; | |
124 | return at; | |
125 | } | |
126 | ||
127 | /* | |
128 | * In a format string, find the next occurrence of %(atom). | |
129 | */ | |
130 | static const char *find_next(const char *cp) | |
131 | { | |
132 | while (*cp) { | |
133 | if (*cp == '%') { | |
134 | /* %( is the start of an atom; | |
135 | * %% is a quoted per-cent. | |
136 | */ | |
137 | if (cp[1] == '(') | |
138 | return cp; | |
139 | else if (cp[1] == '%') | |
140 | cp++; /* skip over two % */ | |
141 | /* otherwise this is a singleton, literal % */ | |
142 | } | |
143 | cp++; | |
144 | } | |
145 | return NULL; | |
146 | } | |
147 | ||
148 | /* | |
149 | * Make sure the format string is well formed, and parse out | |
150 | * the used atoms. | |
151 | */ | |
152 | static void verify_format(const char *format) | |
153 | { | |
154 | const char *cp, *sp; | |
155 | for (cp = format; *cp && (sp = find_next(cp)); ) { | |
156 | const char *ep = strchr(sp, ')'); | |
157 | if (!ep) | |
158 | die("malformatted format string %s", sp); | |
159 | /* sp points at "%(" and ep points at the closing ")" */ | |
160 | parse_atom(sp + 2, ep); | |
161 | cp = ep + 1; | |
162 | } | |
163 | } | |
164 | ||
165 | /* | |
166 | * Given an object name, read the object data and size, and return a | |
167 | * "struct object". If the object data we are returning is also borrowed | |
168 | * by the "struct object" representation, set *eaten as well---it is a | |
169 | * signal from parse_object_buffer to us not to free the buffer. | |
170 | */ | |
171 | static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten) | |
172 | { | |
173 | enum object_type type; | |
174 | void *buf = read_sha1_file(sha1, &type, sz); | |
175 | ||
176 | if (buf) | |
177 | *obj = parse_object_buffer(sha1, type, *sz, buf, eaten); | |
178 | else | |
179 | *obj = NULL; | |
180 | return buf; | |
181 | } | |
182 | ||
183 | /* See grab_values */ | |
184 | static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
185 | { | |
186 | int i; | |
187 | ||
188 | for (i = 0; i < used_atom_cnt; i++) { | |
189 | const char *name = used_atom[i]; | |
190 | struct atom_value *v = &val[i]; | |
191 | if (!!deref != (*name == '*')) | |
192 | continue; | |
193 | if (deref) | |
194 | name++; | |
195 | if (!strcmp(name, "objecttype")) | |
196 | v->s = typename(obj->type); | |
197 | else if (!strcmp(name, "objectsize")) { | |
198 | char *s = xmalloc(40); | |
199 | sprintf(s, "%lu", sz); | |
200 | v->ul = sz; | |
201 | v->s = s; | |
202 | } | |
203 | else if (!strcmp(name, "objectname")) { | |
204 | char *s = xmalloc(41); | |
205 | strcpy(s, sha1_to_hex(obj->sha1)); | |
206 | v->s = s; | |
207 | } | |
208 | } | |
209 | } | |
210 | ||
211 | /* See grab_values */ | |
212 | static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
213 | { | |
214 | int i; | |
215 | struct tag *tag = (struct tag *) obj; | |
216 | ||
217 | for (i = 0; i < used_atom_cnt; i++) { | |
218 | const char *name = used_atom[i]; | |
219 | struct atom_value *v = &val[i]; | |
220 | if (!!deref != (*name == '*')) | |
221 | continue; | |
222 | if (deref) | |
223 | name++; | |
224 | if (!strcmp(name, "tag")) | |
225 | v->s = tag->tag; | |
226 | } | |
227 | } | |
228 | ||
229 | static int num_parents(struct commit *commit) | |
230 | { | |
231 | struct commit_list *parents; | |
232 | int i; | |
233 | ||
234 | for (i = 0, parents = commit->parents; | |
235 | parents; | |
236 | parents = parents->next) | |
237 | i++; | |
238 | return i; | |
239 | } | |
240 | ||
241 | /* See grab_values */ | |
242 | static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
243 | { | |
244 | int i; | |
245 | struct commit *commit = (struct commit *) obj; | |
246 | ||
247 | for (i = 0; i < used_atom_cnt; i++) { | |
248 | const char *name = used_atom[i]; | |
249 | struct atom_value *v = &val[i]; | |
250 | if (!!deref != (*name == '*')) | |
251 | continue; | |
252 | if (deref) | |
253 | name++; | |
254 | if (!strcmp(name, "tree")) { | |
255 | char *s = xmalloc(41); | |
256 | strcpy(s, sha1_to_hex(commit->tree->object.sha1)); | |
257 | v->s = s; | |
258 | } | |
259 | if (!strcmp(name, "numparent")) { | |
260 | char *s = xmalloc(40); | |
261 | sprintf(s, "%lu", v->ul); | |
262 | v->s = s; | |
263 | v->ul = num_parents(commit); | |
264 | } | |
265 | else if (!strcmp(name, "parent")) { | |
266 | int num = num_parents(commit); | |
267 | int i; | |
268 | struct commit_list *parents; | |
269 | char *s = xmalloc(42 * num); | |
270 | v->s = s; | |
271 | for (i = 0, parents = commit->parents; | |
272 | parents; | |
273 | parents = parents->next, i = i + 42) { | |
274 | struct commit *parent = parents->item; | |
275 | strcpy(s+i, sha1_to_hex(parent->object.sha1)); | |
276 | if (parents->next) | |
277 | s[i+40] = ' '; | |
278 | } | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
283 | static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz) | |
284 | { | |
285 | const char *eol; | |
286 | while (*buf) { | |
287 | if (!strncmp(buf, who, wholen) && | |
288 | buf[wholen] == ' ') | |
289 | return buf + wholen + 1; | |
290 | eol = strchr(buf, '\n'); | |
291 | if (!eol) | |
292 | return ""; | |
293 | eol++; | |
294 | if (eol[1] == '\n') | |
295 | return ""; /* end of header */ | |
296 | buf = eol; | |
297 | } | |
298 | return ""; | |
299 | } | |
300 | ||
301 | static const char *copy_line(const char *buf) | |
302 | { | |
303 | const char *eol = strchr(buf, '\n'); | |
304 | if (!eol) | |
305 | return ""; | |
306 | return xmemdupz(buf, eol - buf); | |
307 | } | |
308 | ||
309 | static const char *copy_name(const char *buf) | |
310 | { | |
311 | const char *cp; | |
312 | for (cp = buf; *cp != '\n'; cp++) { | |
313 | if (!strncmp(cp, " <", 2)) | |
314 | return xmemdupz(buf, cp - buf); | |
315 | } | |
316 | return ""; | |
317 | } | |
318 | ||
319 | static const char *copy_email(const char *buf) | |
320 | { | |
321 | const char *email = strchr(buf, '<'); | |
322 | const char *eoemail = strchr(email, '>'); | |
323 | if (!email || !eoemail) | |
324 | return ""; | |
325 | return xmemdupz(email, eoemail + 1 - email); | |
326 | } | |
327 | ||
328 | static void grab_date(const char *buf, struct atom_value *v) | |
329 | { | |
330 | const char *eoemail = strstr(buf, "> "); | |
331 | char *zone; | |
332 | unsigned long timestamp; | |
333 | long tz; | |
334 | ||
335 | if (!eoemail) | |
336 | goto bad; | |
337 | timestamp = strtoul(eoemail + 2, &zone, 10); | |
338 | if (timestamp == ULONG_MAX) | |
339 | goto bad; | |
340 | tz = strtol(zone, NULL, 10); | |
341 | if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE) | |
342 | goto bad; | |
343 | v->s = xstrdup(show_date(timestamp, tz, 0)); | |
344 | v->ul = timestamp; | |
345 | return; | |
346 | bad: | |
347 | v->s = ""; | |
348 | v->ul = 0; | |
349 | } | |
350 | ||
351 | /* See grab_values */ | |
352 | static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
353 | { | |
354 | int i; | |
355 | int wholen = strlen(who); | |
356 | const char *wholine = NULL; | |
357 | ||
358 | for (i = 0; i < used_atom_cnt; i++) { | |
359 | const char *name = used_atom[i]; | |
360 | struct atom_value *v = &val[i]; | |
361 | if (!!deref != (*name == '*')) | |
362 | continue; | |
363 | if (deref) | |
364 | name++; | |
365 | if (strncmp(who, name, wholen)) | |
366 | continue; | |
367 | if (name[wholen] != 0 && | |
368 | strcmp(name + wholen, "name") && | |
369 | strcmp(name + wholen, "email") && | |
370 | strcmp(name + wholen, "date")) | |
371 | continue; | |
372 | if (!wholine) | |
373 | wholine = find_wholine(who, wholen, buf, sz); | |
374 | if (!wholine) | |
375 | return; /* no point looking for it */ | |
376 | if (name[wholen] == 0) | |
377 | v->s = copy_line(wholine); | |
378 | else if (!strcmp(name + wholen, "name")) | |
379 | v->s = copy_name(wholine); | |
380 | else if (!strcmp(name + wholen, "email")) | |
381 | v->s = copy_email(wholine); | |
382 | else if (!strcmp(name + wholen, "date")) | |
383 | grab_date(wholine, v); | |
384 | } | |
385 | ||
386 | /* For a tag or a commit object, if "creator" or "creatordate" is | |
387 | * requested, do something special. | |
388 | */ | |
389 | if (strcmp(who, "tagger") && strcmp(who, "committer")) | |
390 | return; /* "author" for commit object is not wanted */ | |
391 | if (!wholine) | |
392 | wholine = find_wholine(who, wholen, buf, sz); | |
393 | if (!wholine) | |
394 | return; | |
395 | for (i = 0; i < used_atom_cnt; i++) { | |
396 | const char *name = used_atom[i]; | |
397 | struct atom_value *v = &val[i]; | |
398 | if (!!deref != (*name == '*')) | |
399 | continue; | |
400 | if (deref) | |
401 | name++; | |
402 | ||
403 | if (!strcmp(name, "creatordate")) | |
404 | grab_date(wholine, v); | |
405 | else if (!strcmp(name, "creator")) | |
406 | v->s = copy_line(wholine); | |
407 | } | |
408 | } | |
409 | ||
410 | static void find_subpos(const char *buf, unsigned long sz, const char **sub, const char **body) | |
411 | { | |
412 | while (*buf) { | |
413 | const char *eol = strchr(buf, '\n'); | |
414 | if (!eol) | |
415 | return; | |
416 | if (eol[1] == '\n') { | |
417 | buf = eol + 1; | |
418 | break; /* found end of header */ | |
419 | } | |
420 | buf = eol + 1; | |
421 | } | |
422 | while (*buf == '\n') | |
423 | buf++; | |
424 | if (!*buf) | |
425 | return; | |
426 | *sub = buf; /* first non-empty line */ | |
427 | buf = strchr(buf, '\n'); | |
428 | if (!buf) | |
429 | return; /* no body */ | |
430 | while (*buf == '\n') | |
431 | buf++; /* skip blank between subject and body */ | |
432 | *body = buf; | |
433 | } | |
434 | ||
435 | /* See grab_values */ | |
436 | static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
437 | { | |
438 | int i; | |
439 | const char *subpos = NULL, *bodypos = NULL; | |
440 | ||
441 | for (i = 0; i < used_atom_cnt; i++) { | |
442 | const char *name = used_atom[i]; | |
443 | struct atom_value *v = &val[i]; | |
444 | if (!!deref != (*name == '*')) | |
445 | continue; | |
446 | if (deref) | |
447 | name++; | |
448 | if (strcmp(name, "subject") && | |
449 | strcmp(name, "body") && | |
450 | strcmp(name, "contents")) | |
451 | continue; | |
452 | if (!subpos) | |
453 | find_subpos(buf, sz, &subpos, &bodypos); | |
454 | if (!subpos) | |
455 | return; | |
456 | ||
457 | if (!strcmp(name, "subject")) | |
458 | v->s = copy_line(subpos); | |
459 | else if (!strcmp(name, "body")) | |
460 | v->s = xstrdup(bodypos); | |
461 | else if (!strcmp(name, "contents")) | |
462 | v->s = xstrdup(subpos); | |
463 | } | |
464 | } | |
465 | ||
466 | /* We want to have empty print-string for field requests | |
467 | * that do not apply (e.g. "authordate" for a tag object) | |
468 | */ | |
469 | static void fill_missing_values(struct atom_value *val) | |
470 | { | |
471 | int i; | |
472 | for (i = 0; i < used_atom_cnt; i++) { | |
473 | struct atom_value *v = &val[i]; | |
474 | if (v->s == NULL) | |
475 | v->s = ""; | |
476 | } | |
477 | } | |
478 | ||
479 | /* | |
480 | * val is a list of atom_value to hold returned values. Extract | |
481 | * the values for atoms in used_atom array out of (obj, buf, sz). | |
482 | * when deref is false, (obj, buf, sz) is the object that is | |
483 | * pointed at by the ref itself; otherwise it is the object the | |
484 | * ref (which is a tag) refers to. | |
485 | */ | |
486 | static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
487 | { | |
488 | grab_common_values(val, deref, obj, buf, sz); | |
489 | switch (obj->type) { | |
490 | case OBJ_TAG: | |
491 | grab_tag_values(val, deref, obj, buf, sz); | |
492 | grab_sub_body_contents(val, deref, obj, buf, sz); | |
493 | grab_person("tagger", val, deref, obj, buf, sz); | |
494 | break; | |
495 | case OBJ_COMMIT: | |
496 | grab_commit_values(val, deref, obj, buf, sz); | |
497 | grab_sub_body_contents(val, deref, obj, buf, sz); | |
498 | grab_person("author", val, deref, obj, buf, sz); | |
499 | grab_person("committer", val, deref, obj, buf, sz); | |
500 | break; | |
501 | case OBJ_TREE: | |
502 | // grab_tree_values(val, deref, obj, buf, sz); | |
503 | break; | |
504 | case OBJ_BLOB: | |
505 | // grab_blob_values(val, deref, obj, buf, sz); | |
506 | break; | |
507 | default: | |
508 | die("Eh? Object of type %d?", obj->type); | |
509 | } | |
510 | } | |
511 | ||
512 | /* | |
513 | * Parse the object referred by ref, and grab needed value. | |
514 | */ | |
515 | static void populate_value(struct refinfo *ref) | |
516 | { | |
517 | void *buf; | |
518 | struct object *obj; | |
519 | int eaten, i; | |
520 | unsigned long size; | |
521 | const unsigned char *tagged; | |
522 | ||
523 | ref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt); | |
524 | ||
525 | buf = get_obj(ref->objectname, &obj, &size, &eaten); | |
526 | if (!buf) | |
527 | die("missing object %s for %s", | |
528 | sha1_to_hex(ref->objectname), ref->refname); | |
529 | if (!obj) | |
530 | die("parse_object_buffer failed on %s for %s", | |
531 | sha1_to_hex(ref->objectname), ref->refname); | |
532 | ||
533 | /* Fill in specials first */ | |
534 | for (i = 0; i < used_atom_cnt; i++) { | |
535 | const char *name = used_atom[i]; | |
536 | struct atom_value *v = &ref->value[i]; | |
537 | if (!strcmp(name, "refname")) | |
538 | v->s = ref->refname; | |
539 | else if (!strcmp(name, "*refname")) { | |
540 | int len = strlen(ref->refname); | |
541 | char *s = xmalloc(len + 4); | |
542 | sprintf(s, "%s^{}", ref->refname); | |
543 | v->s = s; | |
544 | } | |
545 | } | |
546 | ||
547 | grab_values(ref->value, 0, obj, buf, size); | |
548 | if (!eaten) | |
549 | free(buf); | |
550 | ||
551 | /* If there is no atom that wants to know about tagged | |
552 | * object, we are done. | |
553 | */ | |
554 | if (!need_tagged || (obj->type != OBJ_TAG)) | |
555 | return; | |
556 | ||
557 | /* If it is a tag object, see if we use a value that derefs | |
558 | * the object, and if we do grab the object it refers to. | |
559 | */ | |
560 | tagged = ((struct tag *)obj)->tagged->sha1; | |
561 | ||
562 | /* NEEDSWORK: This derefs tag only once, which | |
563 | * is good to deal with chains of trust, but | |
564 | * is not consistent with what deref_tag() does | |
565 | * which peels the onion to the core. | |
566 | */ | |
567 | buf = get_obj(tagged, &obj, &size, &eaten); | |
568 | if (!buf) | |
569 | die("missing object %s for %s", | |
570 | sha1_to_hex(tagged), ref->refname); | |
571 | if (!obj) | |
572 | die("parse_object_buffer failed on %s for %s", | |
573 | sha1_to_hex(tagged), ref->refname); | |
574 | grab_values(ref->value, 1, obj, buf, size); | |
575 | if (!eaten) | |
576 | free(buf); | |
577 | } | |
578 | ||
579 | /* | |
580 | * Given a ref, return the value for the atom. This lazily gets value | |
581 | * out of the object by calling populate value. | |
582 | */ | |
583 | static void get_value(struct refinfo *ref, int atom, struct atom_value **v) | |
584 | { | |
585 | if (!ref->value) { | |
586 | populate_value(ref); | |
587 | fill_missing_values(ref->value); | |
588 | } | |
589 | *v = &ref->value[atom]; | |
590 | } | |
591 | ||
592 | struct grab_ref_cbdata { | |
593 | struct refinfo **grab_array; | |
594 | const char **grab_pattern; | |
595 | int grab_cnt; | |
596 | }; | |
597 | ||
598 | /* | |
599 | * A call-back given to for_each_ref(). It is unfortunate that we | |
600 | * need to use global variables to pass extra information to this | |
601 | * function. | |
602 | */ | |
603 | static int grab_single_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) | |
604 | { | |
605 | struct grab_ref_cbdata *cb = cb_data; | |
606 | struct refinfo *ref; | |
607 | int cnt; | |
608 | ||
609 | if (*cb->grab_pattern) { | |
610 | const char **pattern; | |
611 | int namelen = strlen(refname); | |
612 | for (pattern = cb->grab_pattern; *pattern; pattern++) { | |
613 | const char *p = *pattern; | |
614 | int plen = strlen(p); | |
615 | ||
616 | if ((plen <= namelen) && | |
617 | !strncmp(refname, p, plen) && | |
618 | (refname[plen] == '\0' || | |
619 | refname[plen] == '/')) | |
620 | break; | |
621 | if (!fnmatch(p, refname, FNM_PATHNAME)) | |
622 | break; | |
623 | } | |
624 | if (!*pattern) | |
625 | return 0; | |
626 | } | |
627 | ||
628 | /* We do not open the object yet; sort may only need refname | |
629 | * to do its job and the resulting list may yet to be pruned | |
630 | * by maxcount logic. | |
631 | */ | |
632 | ref = xcalloc(1, sizeof(*ref)); | |
633 | ref->refname = xstrdup(refname); | |
634 | hashcpy(ref->objectname, sha1); | |
635 | ||
636 | cnt = cb->grab_cnt; | |
637 | cb->grab_array = xrealloc(cb->grab_array, | |
638 | sizeof(*cb->grab_array) * (cnt + 1)); | |
639 | cb->grab_array[cnt++] = ref; | |
640 | cb->grab_cnt = cnt; | |
641 | return 0; | |
642 | } | |
643 | ||
644 | static int cmp_ref_sort(struct ref_sort *s, struct refinfo *a, struct refinfo *b) | |
645 | { | |
646 | struct atom_value *va, *vb; | |
647 | int cmp; | |
648 | cmp_type cmp_type = used_atom_type[s->atom]; | |
649 | ||
650 | get_value(a, s->atom, &va); | |
651 | get_value(b, s->atom, &vb); | |
652 | switch (cmp_type) { | |
653 | case FIELD_STR: | |
654 | cmp = strcmp(va->s, vb->s); | |
655 | break; | |
656 | default: | |
657 | if (va->ul < vb->ul) | |
658 | cmp = -1; | |
659 | else if (va->ul == vb->ul) | |
660 | cmp = 0; | |
661 | else | |
662 | cmp = 1; | |
663 | break; | |
664 | } | |
665 | return (s->reverse) ? -cmp : cmp; | |
666 | } | |
667 | ||
668 | static struct ref_sort *ref_sort; | |
669 | static int compare_refs(const void *a_, const void *b_) | |
670 | { | |
671 | struct refinfo *a = *((struct refinfo **)a_); | |
672 | struct refinfo *b = *((struct refinfo **)b_); | |
673 | struct ref_sort *s; | |
674 | ||
675 | for (s = ref_sort; s; s = s->next) { | |
676 | int cmp = cmp_ref_sort(s, a, b); | |
677 | if (cmp) | |
678 | return cmp; | |
679 | } | |
680 | return 0; | |
681 | } | |
682 | ||
683 | static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs) | |
684 | { | |
685 | ref_sort = sort; | |
686 | qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs); | |
687 | } | |
688 | ||
689 | static void print_value(struct refinfo *ref, int atom, int quote_style) | |
690 | { | |
691 | struct atom_value *v; | |
692 | get_value(ref, atom, &v); | |
693 | switch (quote_style) { | |
694 | case QUOTE_NONE: | |
695 | fputs(v->s, stdout); | |
696 | break; | |
697 | case QUOTE_SHELL: | |
698 | sq_quote_print(stdout, v->s); | |
699 | break; | |
700 | case QUOTE_PERL: | |
701 | perl_quote_print(stdout, v->s); | |
702 | break; | |
703 | case QUOTE_PYTHON: | |
704 | python_quote_print(stdout, v->s); | |
705 | break; | |
706 | case QUOTE_TCL: | |
707 | tcl_quote_print(stdout, v->s); | |
708 | break; | |
709 | } | |
710 | } | |
711 | ||
712 | static int hex1(char ch) | |
713 | { | |
714 | if ('0' <= ch && ch <= '9') | |
715 | return ch - '0'; | |
716 | else if ('a' <= ch && ch <= 'f') | |
717 | return ch - 'a' + 10; | |
718 | else if ('A' <= ch && ch <= 'F') | |
719 | return ch - 'A' + 10; | |
720 | return -1; | |
721 | } | |
722 | static int hex2(const char *cp) | |
723 | { | |
724 | if (cp[0] && cp[1]) | |
725 | return (hex1(cp[0]) << 4) | hex1(cp[1]); | |
726 | else | |
727 | return -1; | |
728 | } | |
729 | ||
730 | static void emit(const char *cp, const char *ep) | |
731 | { | |
732 | while (*cp && (!ep || cp < ep)) { | |
733 | if (*cp == '%') { | |
734 | if (cp[1] == '%') | |
735 | cp++; | |
736 | else { | |
737 | int ch = hex2(cp + 1); | |
738 | if (0 <= ch) { | |
739 | putchar(ch); | |
740 | cp += 3; | |
741 | continue; | |
742 | } | |
743 | } | |
744 | } | |
745 | putchar(*cp); | |
746 | cp++; | |
747 | } | |
748 | } | |
749 | ||
750 | static void show_ref(struct refinfo *info, const char *format, int quote_style) | |
751 | { | |
752 | const char *cp, *sp, *ep; | |
753 | ||
754 | for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) { | |
755 | ep = strchr(sp, ')'); | |
756 | if (cp < sp) | |
757 | emit(cp, sp); | |
758 | print_value(info, parse_atom(sp + 2, ep), quote_style); | |
759 | } | |
760 | if (*cp) { | |
761 | sp = cp + strlen(cp); | |
762 | emit(cp, sp); | |
763 | } | |
764 | putchar('\n'); | |
765 | } | |
766 | ||
767 | static struct ref_sort *default_sort(void) | |
768 | { | |
769 | static const char cstr_name[] = "refname"; | |
770 | ||
771 | struct ref_sort *sort = xcalloc(1, sizeof(*sort)); | |
772 | ||
773 | sort->next = NULL; | |
774 | sort->atom = parse_atom(cstr_name, cstr_name + strlen(cstr_name)); | |
775 | return sort; | |
776 | } | |
777 | ||
778 | int cmd_for_each_ref(int ac, const char **av, const char *prefix) | |
779 | { | |
780 | int i, num_refs; | |
781 | const char *format = NULL; | |
782 | struct ref_sort *sort = NULL, **sort_tail = &sort; | |
783 | int maxcount = 0; | |
784 | int quote_style = -1; /* unspecified yet */ | |
785 | struct refinfo **refs; | |
786 | struct grab_ref_cbdata cbdata; | |
787 | ||
788 | for (i = 1; i < ac; i++) { | |
789 | const char *arg = av[i]; | |
790 | if (arg[0] != '-') | |
791 | break; | |
792 | if (!strcmp(arg, "--")) { | |
793 | i++; | |
794 | break; | |
795 | } | |
796 | if (!prefixcmp(arg, "--format=")) { | |
797 | if (format) | |
798 | die("more than one --format?"); | |
799 | format = arg + 9; | |
800 | continue; | |
801 | } | |
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; | |
806 | continue; | |
807 | } | |
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; | |
812 | continue; | |
813 | } | |
814 | if (!strcmp(arg, "--python") ) { | |
815 | if (0 <= quote_style) | |
816 | die("more than one quoting style?"); | |
817 | quote_style = QUOTE_PYTHON; | |
818 | continue; | |
819 | } | |
820 | if (!strcmp(arg, "--tcl") ) { | |
821 | if (0 <= quote_style) | |
822 | die("more than one quoting style?"); | |
823 | quote_style = QUOTE_TCL; | |
824 | continue; | |
825 | } | |
826 | if (!prefixcmp(arg, "--count=")) { | |
827 | if (maxcount) | |
828 | die("more than one --count?"); | |
829 | maxcount = atoi(arg + 8); | |
830 | if (maxcount <= 0) | |
831 | die("The number %s did not parse", arg); | |
832 | continue; | |
833 | } | |
834 | if (!prefixcmp(arg, "--sort=")) { | |
835 | struct ref_sort *s = xcalloc(1, sizeof(*s)); | |
836 | int len; | |
837 | ||
838 | s->next = NULL; | |
839 | *sort_tail = s; | |
840 | sort_tail = &s->next; | |
841 | ||
842 | arg += 7; | |
843 | if (*arg == '-') { | |
844 | s->reverse = 1; | |
845 | arg++; | |
846 | } | |
847 | len = strlen(arg); | |
848 | sort->atom = parse_atom(arg, arg+len); | |
849 | continue; | |
850 | } | |
851 | break; | |
852 | } | |
853 | if (quote_style < 0) | |
854 | quote_style = QUOTE_NONE; | |
855 | ||
856 | if (!sort) | |
857 | sort = default_sort(); | |
858 | sort_atom_limit = used_atom_cnt; | |
859 | if (!format) | |
860 | format = "%(objectname) %(objecttype)\t%(refname)"; | |
861 | ||
862 | verify_format(format); | |
863 | ||
864 | memset(&cbdata, 0, sizeof(cbdata)); | |
865 | cbdata.grab_pattern = av + i; | |
866 | for_each_ref(grab_single_ref, &cbdata); | |
867 | refs = cbdata.grab_array; | |
868 | num_refs = cbdata.grab_cnt; | |
869 | ||
870 | for (i = 0; i < used_atom_cnt; i++) { | |
871 | if (used_atom[i][0] == '*') { | |
872 | need_tagged = 1; | |
873 | break; | |
874 | } | |
875 | } | |
876 | ||
877 | sort_refs(sort, refs, num_refs); | |
878 | ||
879 | if (!maxcount || num_refs < maxcount) | |
880 | maxcount = num_refs; | |
881 | for (i = 0; i < maxcount; i++) | |
882 | show_ref(refs[i], format, quote_style); | |
883 | return 0; | |
884 | } |