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