]>
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" |
8cae19d9 | 11 | #include "remote.h" |
fddb74c9 | 12 | #include "color.h" |
9f613ddd JH |
13 | |
14 | /* Quoting styles */ | |
15 | #define QUOTE_NONE 0 | |
16 | #define QUOTE_SHELL 1 | |
17 | #define QUOTE_PERL 2 | |
c9ecf4f1 JS |
18 | #define QUOTE_PYTHON 4 |
19 | #define QUOTE_TCL 8 | |
9f613ddd JH |
20 | |
21 | typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type; | |
22 | ||
23 | struct atom_value { | |
24 | const char *s; | |
25 | unsigned long ul; /* used for sorting when not FIELD_STR */ | |
26 | }; | |
27 | ||
28 | struct ref_sort { | |
29 | struct ref_sort *next; | |
30 | int atom; /* index into used_atom array */ | |
31 | unsigned reverse : 1; | |
32 | }; | |
33 | ||
34 | struct refinfo { | |
35 | char *refname; | |
36 | unsigned char objectname[20]; | |
5cdd628c JH |
37 | int flag; |
38 | const char *symref; | |
9f613ddd JH |
39 | struct atom_value *value; |
40 | }; | |
41 | ||
42 | static struct { | |
43 | const char *name; | |
44 | cmp_type cmp_type; | |
45 | } valid_atom[] = { | |
46 | { "refname" }, | |
47 | { "objecttype" }, | |
48 | { "objectsize", FIELD_ULONG }, | |
49 | { "objectname" }, | |
50 | { "tree" }, | |
9e1a2acf | 51 | { "parent" }, |
9f613ddd JH |
52 | { "numparent", FIELD_ULONG }, |
53 | { "object" }, | |
54 | { "type" }, | |
55 | { "tag" }, | |
56 | { "author" }, | |
57 | { "authorname" }, | |
58 | { "authoremail" }, | |
59 | { "authordate", FIELD_TIME }, | |
60 | { "committer" }, | |
61 | { "committername" }, | |
62 | { "committeremail" }, | |
63 | { "committerdate", FIELD_TIME }, | |
64 | { "tagger" }, | |
65 | { "taggername" }, | |
66 | { "taggeremail" }, | |
67 | { "taggerdate", FIELD_TIME }, | |
3175aa1e JH |
68 | { "creator" }, |
69 | { "creatordate", FIELD_TIME }, | |
9f613ddd JH |
70 | { "subject" }, |
71 | { "body" }, | |
72 | { "contents" }, | |
e2b23972 MG |
73 | { "contents:subject" }, |
74 | { "contents:body" }, | |
75 | { "contents:signature" }, | |
8cae19d9 | 76 | { "upstream" }, |
5cdd628c | 77 | { "symref" }, |
88fb7f27 | 78 | { "flag" }, |
7a48b832 | 79 | { "HEAD" }, |
fddb74c9 | 80 | { "color" }, |
9f613ddd JH |
81 | }; |
82 | ||
83 | /* | |
84 | * An atom is a valid field atom listed above, possibly prefixed with | |
85 | * a "*" to denote deref_tag(). | |
86 | * | |
87 | * We parse given format string and sort specifiers, and make a list | |
88 | * of properties that we need to extract out of objects. refinfo | |
89 | * structure will hold an array of values extracted that can be | |
90 | * indexed with the "atom number", which is an index into this | |
91 | * array. | |
92 | */ | |
93 | static const char **used_atom; | |
94 | static cmp_type *used_atom_type; | |
b9cf14d4 | 95 | static int used_atom_cnt, need_tagged, need_symref; |
db64eb65 | 96 | static int need_color_reset_at_eol; |
9f613ddd JH |
97 | |
98 | /* | |
99 | * Used to parse format string and sort specifiers | |
100 | */ | |
101 | static int parse_atom(const char *atom, const char *ep) | |
102 | { | |
103 | const char *sp; | |
9f613ddd JH |
104 | int i, at; |
105 | ||
106 | sp = atom; | |
107 | if (*sp == '*' && sp < ep) | |
108 | sp++; /* deref */ | |
109 | if (ep <= sp) | |
110 | die("malformed field name: %.*s", (int)(ep-atom), atom); | |
111 | ||
112 | /* Do we have the atom already used elsewhere? */ | |
113 | for (i = 0; i < used_atom_cnt; i++) { | |
114 | int len = strlen(used_atom[i]); | |
115 | if (len == ep - atom && !memcmp(used_atom[i], atom, len)) | |
116 | return i; | |
117 | } | |
118 | ||
119 | /* Is the atom a valid one? */ | |
120 | for (i = 0; i < ARRAY_SIZE(valid_atom); i++) { | |
121 | int len = strlen(valid_atom[i].name); | |
b64265ca AP |
122 | /* |
123 | * If the atom name has a colon, strip it and everything after | |
124 | * it off - it specifies the format for this entry, and | |
125 | * shouldn't be used for checking against the valid_atom | |
126 | * table. | |
127 | */ | |
128 | const char *formatp = strchr(sp, ':'); | |
070f6918 | 129 | if (!formatp || ep < formatp) |
b64265ca AP |
130 | formatp = ep; |
131 | if (len == formatp - sp && !memcmp(valid_atom[i].name, sp, len)) | |
9f613ddd JH |
132 | break; |
133 | } | |
134 | ||
135 | if (ARRAY_SIZE(valid_atom) <= i) | |
136 | die("unknown field name: %.*s", (int)(ep-atom), atom); | |
137 | ||
138 | /* Add it in, including the deref prefix */ | |
139 | at = used_atom_cnt; | |
140 | used_atom_cnt++; | |
2756ca43 RS |
141 | REALLOC_ARRAY(used_atom, used_atom_cnt); |
142 | REALLOC_ARRAY(used_atom_type, used_atom_cnt); | |
182af834 | 143 | used_atom[at] = xmemdupz(atom, ep - atom); |
9f613ddd | 144 | used_atom_type[at] = valid_atom[i].cmp_type; |
20322e0b JH |
145 | if (*atom == '*') |
146 | need_tagged = 1; | |
5cdd628c JH |
147 | if (!strcmp(used_atom[at], "symref")) |
148 | need_symref = 1; | |
9f613ddd JH |
149 | return at; |
150 | } | |
151 | ||
152 | /* | |
153 | * In a format string, find the next occurrence of %(atom). | |
154 | */ | |
155 | static const char *find_next(const char *cp) | |
156 | { | |
157 | while (*cp) { | |
158 | if (*cp == '%') { | |
40dae309 JH |
159 | /* |
160 | * %( is the start of an atom; | |
3dff5379 | 161 | * %% is a quoted per-cent. |
9f613ddd JH |
162 | */ |
163 | if (cp[1] == '(') | |
164 | return cp; | |
165 | else if (cp[1] == '%') | |
166 | cp++; /* skip over two % */ | |
167 | /* otherwise this is a singleton, literal % */ | |
168 | } | |
169 | cp++; | |
170 | } | |
171 | return NULL; | |
172 | } | |
173 | ||
174 | /* | |
175 | * Make sure the format string is well formed, and parse out | |
176 | * the used atoms. | |
177 | */ | |
c3428da8 | 178 | static int verify_format(const char *format) |
9f613ddd JH |
179 | { |
180 | const char *cp, *sp; | |
db64eb65 RR |
181 | |
182 | need_color_reset_at_eol = 0; | |
9f613ddd | 183 | for (cp = format; *cp && (sp = find_next(cp)); ) { |
008d5d00 | 184 | const char *color, *ep = strchr(sp, ')'); |
db64eb65 RR |
185 | int at; |
186 | ||
9f613ddd | 187 | if (!ep) |
8e0fbe67 | 188 | return error("malformed format string %s", sp); |
9f613ddd | 189 | /* sp points at "%(" and ep points at the closing ")" */ |
db64eb65 | 190 | at = parse_atom(sp + 2, ep); |
9f613ddd | 191 | cp = ep + 1; |
db64eb65 | 192 | |
008d5d00 RS |
193 | if (skip_prefix(used_atom[at], "color:", &color)) |
194 | need_color_reset_at_eol = !!strcmp(color, "reset"); | |
9f613ddd | 195 | } |
c3428da8 | 196 | return 0; |
9f613ddd JH |
197 | } |
198 | ||
199 | /* | |
200 | * Given an object name, read the object data and size, and return a | |
201 | * "struct object". If the object data we are returning is also borrowed | |
202 | * by the "struct object" representation, set *eaten as well---it is a | |
203 | * signal from parse_object_buffer to us not to free the buffer. | |
204 | */ | |
205 | static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten) | |
206 | { | |
21666f1a NP |
207 | enum object_type type; |
208 | void *buf = read_sha1_file(sha1, &type, sz); | |
9f613ddd JH |
209 | |
210 | if (buf) | |
211 | *obj = parse_object_buffer(sha1, type, *sz, buf, eaten); | |
212 | else | |
213 | *obj = NULL; | |
214 | return buf; | |
215 | } | |
216 | ||
b74cf648 JK |
217 | static int grab_objectname(const char *name, const unsigned char *sha1, |
218 | struct atom_value *v) | |
219 | { | |
220 | if (!strcmp(name, "objectname")) { | |
221 | char *s = xmalloc(41); | |
222 | strcpy(s, sha1_to_hex(sha1)); | |
223 | v->s = s; | |
224 | return 1; | |
225 | } | |
226 | if (!strcmp(name, "objectname:short")) { | |
227 | v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); | |
228 | return 1; | |
229 | } | |
230 | return 0; | |
231 | } | |
232 | ||
9f613ddd JH |
233 | /* See grab_values */ |
234 | static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
235 | { | |
236 | int i; | |
237 | ||
238 | for (i = 0; i < used_atom_cnt; i++) { | |
239 | const char *name = used_atom[i]; | |
240 | struct atom_value *v = &val[i]; | |
241 | if (!!deref != (*name == '*')) | |
242 | continue; | |
243 | if (deref) | |
244 | name++; | |
245 | if (!strcmp(name, "objecttype")) | |
df843662 | 246 | v->s = typename(obj->type); |
9f613ddd JH |
247 | else if (!strcmp(name, "objectsize")) { |
248 | char *s = xmalloc(40); | |
249 | sprintf(s, "%lu", sz); | |
250 | v->ul = sz; | |
251 | v->s = s; | |
252 | } | |
b74cf648 JK |
253 | else if (deref) |
254 | grab_objectname(name, obj->sha1, v); | |
9f613ddd JH |
255 | } |
256 | } | |
257 | ||
258 | /* See grab_values */ | |
259 | static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
260 | { | |
261 | int i; | |
262 | struct tag *tag = (struct tag *) obj; | |
263 | ||
264 | for (i = 0; i < used_atom_cnt; i++) { | |
265 | const char *name = used_atom[i]; | |
266 | struct atom_value *v = &val[i]; | |
267 | if (!!deref != (*name == '*')) | |
268 | continue; | |
269 | if (deref) | |
270 | name++; | |
271 | if (!strcmp(name, "tag")) | |
272 | v->s = tag->tag; | |
87412ec1 JK |
273 | else if (!strcmp(name, "type") && tag->tagged) |
274 | v->s = typename(tag->tagged->type); | |
275 | else if (!strcmp(name, "object") && tag->tagged) { | |
276 | char *s = xmalloc(41); | |
277 | strcpy(s, sha1_to_hex(tag->tagged->sha1)); | |
278 | v->s = s; | |
279 | } | |
9f613ddd JH |
280 | } |
281 | } | |
282 | ||
9f613ddd JH |
283 | /* See grab_values */ |
284 | static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
285 | { | |
286 | int i; | |
287 | struct commit *commit = (struct commit *) obj; | |
288 | ||
289 | for (i = 0; i < used_atom_cnt; i++) { | |
290 | const char *name = used_atom[i]; | |
291 | struct atom_value *v = &val[i]; | |
292 | if (!!deref != (*name == '*')) | |
293 | continue; | |
294 | if (deref) | |
295 | name++; | |
296 | if (!strcmp(name, "tree")) { | |
297 | char *s = xmalloc(41); | |
298 | strcpy(s, sha1_to_hex(commit->tree->object.sha1)); | |
299 | v->s = s; | |
300 | } | |
301 | if (!strcmp(name, "numparent")) { | |
302 | char *s = xmalloc(40); | |
4bbaa1eb | 303 | v->ul = commit_list_count(commit->parents); |
9f613ddd JH |
304 | sprintf(s, "%lu", v->ul); |
305 | v->s = s; | |
9f613ddd JH |
306 | } |
307 | else if (!strcmp(name, "parent")) { | |
4bbaa1eb | 308 | int num = commit_list_count(commit->parents); |
9f613ddd JH |
309 | int i; |
310 | struct commit_list *parents; | |
9e1a2acf | 311 | char *s = xmalloc(41 * num + 1); |
9f613ddd JH |
312 | v->s = s; |
313 | for (i = 0, parents = commit->parents; | |
314 | parents; | |
9e1a2acf | 315 | parents = parents->next, i = i + 41) { |
9f613ddd JH |
316 | struct commit *parent = parents->item; |
317 | strcpy(s+i, sha1_to_hex(parent->object.sha1)); | |
318 | if (parents->next) | |
319 | s[i+40] = ' '; | |
320 | } | |
9e1a2acf JH |
321 | if (!i) |
322 | *s = '\0'; | |
9f613ddd JH |
323 | } |
324 | } | |
325 | } | |
326 | ||
327 | static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz) | |
328 | { | |
329 | const char *eol; | |
330 | while (*buf) { | |
331 | if (!strncmp(buf, who, wholen) && | |
332 | buf[wholen] == ' ') | |
333 | return buf + wholen + 1; | |
334 | eol = strchr(buf, '\n'); | |
335 | if (!eol) | |
336 | return ""; | |
337 | eol++; | |
a74fa110 | 338 | if (*eol == '\n') |
9f613ddd JH |
339 | return ""; /* end of header */ |
340 | buf = eol; | |
341 | } | |
342 | return ""; | |
343 | } | |
344 | ||
3a55602e | 345 | static const char *copy_line(const char *buf) |
9f613ddd | 346 | { |
94e02e7f | 347 | const char *eol = strchrnul(buf, '\n'); |
182af834 | 348 | return xmemdupz(buf, eol - buf); |
9f613ddd JH |
349 | } |
350 | ||
3a55602e | 351 | static const char *copy_name(const char *buf) |
9f613ddd | 352 | { |
182af834 | 353 | const char *cp; |
6b30852d | 354 | for (cp = buf; *cp && *cp != '\n'; cp++) { |
182af834 PH |
355 | if (!strncmp(cp, " <", 2)) |
356 | return xmemdupz(buf, cp - buf); | |
357 | } | |
358 | return ""; | |
9f613ddd JH |
359 | } |
360 | ||
3a55602e | 361 | static const char *copy_email(const char *buf) |
9f613ddd JH |
362 | { |
363 | const char *email = strchr(buf, '<'); | |
e64c1b00 JK |
364 | const char *eoemail; |
365 | if (!email) | |
366 | return ""; | |
367 | eoemail = strchr(email, '>'); | |
368 | if (!eoemail) | |
9f613ddd | 369 | return ""; |
182af834 | 370 | return xmemdupz(email, eoemail + 1 - email); |
9f613ddd JH |
371 | } |
372 | ||
7f6e275b JK |
373 | static char *copy_subject(const char *buf, unsigned long len) |
374 | { | |
375 | char *r = xmemdupz(buf, len); | |
376 | int i; | |
377 | ||
378 | for (i = 0; i < len; i++) | |
379 | if (r[i] == '\n') | |
380 | r[i] = ' '; | |
381 | ||
382 | return r; | |
383 | } | |
384 | ||
d392e712 | 385 | static void grab_date(const char *buf, struct atom_value *v, const char *atomname) |
9f613ddd JH |
386 | { |
387 | const char *eoemail = strstr(buf, "> "); | |
388 | char *zone; | |
389 | unsigned long timestamp; | |
390 | long tz; | |
d392e712 AP |
391 | enum date_mode date_mode = DATE_NORMAL; |
392 | const char *formatp; | |
393 | ||
394 | /* | |
395 | * We got here because atomname ends in "date" or "date<something>"; | |
396 | * it's not possible that <something> is not ":<format>" because | |
397 | * parse_atom() wouldn't have allowed it, so we can assume that no | |
398 | * ":" means no format is specified, and use the default. | |
399 | */ | |
400 | formatp = strchr(atomname, ':'); | |
401 | if (formatp != NULL) { | |
402 | formatp++; | |
403 | date_mode = parse_date_format(formatp); | |
404 | } | |
9f613ddd JH |
405 | |
406 | if (!eoemail) | |
407 | goto bad; | |
408 | timestamp = strtoul(eoemail + 2, &zone, 10); | |
409 | if (timestamp == ULONG_MAX) | |
410 | goto bad; | |
411 | tz = strtol(zone, NULL, 10); | |
412 | if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE) | |
413 | goto bad; | |
d392e712 | 414 | v->s = xstrdup(show_date(timestamp, tz, date_mode)); |
9f613ddd JH |
415 | v->ul = timestamp; |
416 | return; | |
417 | bad: | |
418 | v->s = ""; | |
419 | v->ul = 0; | |
420 | } | |
421 | ||
422 | /* See grab_values */ | |
423 | static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
424 | { | |
425 | int i; | |
426 | int wholen = strlen(who); | |
427 | const char *wholine = NULL; | |
428 | ||
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 | if (strncmp(who, name, wholen)) | |
437 | continue; | |
438 | if (name[wholen] != 0 && | |
439 | strcmp(name + wholen, "name") && | |
440 | strcmp(name + wholen, "email") && | |
59556548 | 441 | !starts_with(name + wholen, "date")) |
9f613ddd JH |
442 | continue; |
443 | if (!wholine) | |
444 | wholine = find_wholine(who, wholen, buf, sz); | |
445 | if (!wholine) | |
446 | return; /* no point looking for it */ | |
447 | if (name[wholen] == 0) | |
448 | v->s = copy_line(wholine); | |
449 | else if (!strcmp(name + wholen, "name")) | |
450 | v->s = copy_name(wholine); | |
451 | else if (!strcmp(name + wholen, "email")) | |
452 | v->s = copy_email(wholine); | |
59556548 | 453 | else if (starts_with(name + wholen, "date")) |
d392e712 | 454 | grab_date(wholine, v, name); |
9f613ddd | 455 | } |
3175aa1e | 456 | |
40dae309 JH |
457 | /* |
458 | * For a tag or a commit object, if "creator" or "creatordate" is | |
3175aa1e JH |
459 | * requested, do something special. |
460 | */ | |
461 | if (strcmp(who, "tagger") && strcmp(who, "committer")) | |
462 | return; /* "author" for commit object is not wanted */ | |
463 | if (!wholine) | |
464 | wholine = find_wholine(who, wholen, buf, sz); | |
465 | if (!wholine) | |
466 | return; | |
467 | for (i = 0; i < used_atom_cnt; i++) { | |
468 | const char *name = used_atom[i]; | |
469 | struct atom_value *v = &val[i]; | |
470 | if (!!deref != (*name == '*')) | |
471 | continue; | |
472 | if (deref) | |
473 | name++; | |
474 | ||
59556548 | 475 | if (starts_with(name, "creatordate")) |
d392e712 | 476 | grab_date(wholine, v, name); |
3175aa1e JH |
477 | else if (!strcmp(name, "creator")) |
478 | v->s = copy_line(wholine); | |
479 | } | |
9f613ddd JH |
480 | } |
481 | ||
7ec0f31e JK |
482 | static void find_subpos(const char *buf, unsigned long sz, |
483 | const char **sub, unsigned long *sublen, | |
e2b23972 MG |
484 | const char **body, unsigned long *bodylen, |
485 | unsigned long *nonsiglen, | |
486 | const char **sig, unsigned long *siglen) | |
9f613ddd | 487 | { |
7ec0f31e JK |
488 | const char *eol; |
489 | /* skip past header until we hit empty line */ | |
490 | while (*buf && *buf != '\n') { | |
491 | eol = strchrnul(buf, '\n'); | |
492 | if (*eol) | |
493 | eol++; | |
494 | buf = eol; | |
9f613ddd | 495 | } |
7ec0f31e | 496 | /* skip any empty lines */ |
9f613ddd JH |
497 | while (*buf == '\n') |
498 | buf++; | |
7ec0f31e | 499 | |
e2b23972 MG |
500 | /* parse signature first; we might not even have a subject line */ |
501 | *sig = buf + parse_signature(buf, strlen(buf)); | |
502 | *siglen = strlen(*sig); | |
503 | ||
7ec0f31e JK |
504 | /* subject is first non-empty line */ |
505 | *sub = buf; | |
7f6e275b | 506 | /* subject goes to first empty line */ |
e2b23972 | 507 | while (buf < *sig && *buf && *buf != '\n') { |
7f6e275b JK |
508 | eol = strchrnul(buf, '\n'); |
509 | if (*eol) | |
510 | eol++; | |
511 | buf = eol; | |
512 | } | |
513 | *sublen = buf - *sub; | |
514 | /* drop trailing newline, if present */ | |
515 | if (*sublen && (*sub)[*sublen - 1] == '\n') | |
516 | *sublen -= 1; | |
7ec0f31e JK |
517 | |
518 | /* skip any empty lines */ | |
9f613ddd | 519 | while (*buf == '\n') |
7ec0f31e | 520 | buf++; |
9f613ddd | 521 | *body = buf; |
7ec0f31e | 522 | *bodylen = strlen(buf); |
e2b23972 | 523 | *nonsiglen = *sig - buf; |
9f613ddd JH |
524 | } |
525 | ||
526 | /* See grab_values */ | |
527 | static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
528 | { | |
529 | int i; | |
e2b23972 MG |
530 | const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL; |
531 | unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0; | |
9f613ddd JH |
532 | |
533 | for (i = 0; i < used_atom_cnt; i++) { | |
534 | const char *name = used_atom[i]; | |
535 | struct atom_value *v = &val[i]; | |
536 | if (!!deref != (*name == '*')) | |
537 | continue; | |
538 | if (deref) | |
539 | name++; | |
540 | if (strcmp(name, "subject") && | |
541 | strcmp(name, "body") && | |
e2b23972 MG |
542 | strcmp(name, "contents") && |
543 | strcmp(name, "contents:subject") && | |
544 | strcmp(name, "contents:body") && | |
545 | strcmp(name, "contents:signature")) | |
9f613ddd JH |
546 | continue; |
547 | if (!subpos) | |
7ec0f31e JK |
548 | find_subpos(buf, sz, |
549 | &subpos, &sublen, | |
e2b23972 MG |
550 | &bodypos, &bodylen, &nonsiglen, |
551 | &sigpos, &siglen); | |
9f613ddd JH |
552 | |
553 | if (!strcmp(name, "subject")) | |
7f6e275b | 554 | v->s = copy_subject(subpos, sublen); |
e2b23972 MG |
555 | else if (!strcmp(name, "contents:subject")) |
556 | v->s = copy_subject(subpos, sublen); | |
9f613ddd | 557 | else if (!strcmp(name, "body")) |
7ec0f31e | 558 | v->s = xmemdupz(bodypos, bodylen); |
e2b23972 MG |
559 | else if (!strcmp(name, "contents:body")) |
560 | v->s = xmemdupz(bodypos, nonsiglen); | |
561 | else if (!strcmp(name, "contents:signature")) | |
562 | v->s = xmemdupz(sigpos, siglen); | |
9f613ddd | 563 | else if (!strcmp(name, "contents")) |
f8290630 | 564 | v->s = xstrdup(subpos); |
9f613ddd JH |
565 | } |
566 | } | |
567 | ||
40dae309 JH |
568 | /* |
569 | * We want to have empty print-string for field requests | |
9f613ddd JH |
570 | * that do not apply (e.g. "authordate" for a tag object) |
571 | */ | |
572 | static void fill_missing_values(struct atom_value *val) | |
573 | { | |
574 | int i; | |
575 | for (i = 0; i < used_atom_cnt; i++) { | |
576 | struct atom_value *v = &val[i]; | |
577 | if (v->s == NULL) | |
578 | v->s = ""; | |
579 | } | |
580 | } | |
581 | ||
582 | /* | |
583 | * val is a list of atom_value to hold returned values. Extract | |
584 | * the values for atoms in used_atom array out of (obj, buf, sz). | |
585 | * when deref is false, (obj, buf, sz) is the object that is | |
586 | * pointed at by the ref itself; otherwise it is the object the | |
587 | * ref (which is a tag) refers to. | |
588 | */ | |
589 | static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
590 | { | |
591 | grab_common_values(val, deref, obj, buf, sz); | |
592 | switch (obj->type) { | |
593 | case OBJ_TAG: | |
594 | grab_tag_values(val, deref, obj, buf, sz); | |
595 | grab_sub_body_contents(val, deref, obj, buf, sz); | |
596 | grab_person("tagger", val, deref, obj, buf, sz); | |
597 | break; | |
598 | case OBJ_COMMIT: | |
599 | grab_commit_values(val, deref, obj, buf, sz); | |
600 | grab_sub_body_contents(val, deref, obj, buf, sz); | |
601 | grab_person("author", val, deref, obj, buf, sz); | |
602 | grab_person("committer", val, deref, obj, buf, sz); | |
603 | break; | |
604 | case OBJ_TREE: | |
2543d9b6 | 605 | /* grab_tree_values(val, deref, obj, buf, sz); */ |
9f613ddd JH |
606 | break; |
607 | case OBJ_BLOB: | |
2543d9b6 | 608 | /* grab_blob_values(val, deref, obj, buf, sz); */ |
9f613ddd JH |
609 | break; |
610 | default: | |
611 | die("Eh? Object of type %d?", obj->type); | |
612 | } | |
613 | } | |
614 | ||
88fb7f27 JH |
615 | static inline char *copy_advance(char *dst, const char *src) |
616 | { | |
617 | while (*src) | |
618 | *dst++ = *src++; | |
619 | return dst; | |
620 | } | |
621 | ||
9f613ddd JH |
622 | /* |
623 | * Parse the object referred by ref, and grab needed value. | |
624 | */ | |
625 | static void populate_value(struct refinfo *ref) | |
626 | { | |
627 | void *buf; | |
628 | struct object *obj; | |
629 | int eaten, i; | |
630 | unsigned long size; | |
631 | const unsigned char *tagged; | |
632 | ||
693eb02a | 633 | ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value)); |
9f613ddd | 634 | |
5cdd628c JH |
635 | if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) { |
636 | unsigned char unused1[20]; | |
7695d118 RS |
637 | ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING, |
638 | unused1, NULL); | |
96ec7b1e | 639 | if (!ref->symref) |
5cdd628c JH |
640 | ref->symref = ""; |
641 | } | |
642 | ||
9f613ddd JH |
643 | /* Fill in specials first */ |
644 | for (i = 0; i < used_atom_cnt; i++) { | |
645 | const char *name = used_atom[i]; | |
646 | struct atom_value *v = &ref->value[i]; | |
7d66f21a | 647 | int deref = 0; |
8db9a4b8 JK |
648 | const char *refname; |
649 | const char *formatp; | |
b28061ce | 650 | struct branch *branch = NULL; |
8db9a4b8 | 651 | |
7d66f21a BW |
652 | if (*name == '*') { |
653 | deref = 1; | |
654 | name++; | |
655 | } | |
7d66f21a | 656 | |
59556548 | 657 | if (starts_with(name, "refname")) |
8db9a4b8 | 658 | refname = ref->refname; |
59556548 | 659 | else if (starts_with(name, "symref")) |
5cdd628c | 660 | refname = ref->symref ? ref->symref : ""; |
59556548 | 661 | else if (starts_with(name, "upstream")) { |
8cae19d9 | 662 | /* only local branches may have an upstream */ |
59556548 | 663 | if (!starts_with(ref->refname, "refs/heads/")) |
8cae19d9 JK |
664 | continue; |
665 | branch = branch_get(ref->refname + 11); | |
666 | ||
667 | if (!branch || !branch->merge || !branch->merge[0] || | |
668 | !branch->merge[0]->dst) | |
669 | continue; | |
670 | refname = branch->merge[0]->dst; | |
ad704485 | 671 | } else if (starts_with(name, "color:")) { |
fddb74c9 RR |
672 | char color[COLOR_MAXLEN] = ""; |
673 | ||
f6c5a296 JK |
674 | if (color_parse(name + 6, color) < 0) |
675 | die(_("unable to parse format")); | |
fddb74c9 RR |
676 | v->s = xstrdup(color); |
677 | continue; | |
678 | } else if (!strcmp(name, "flag")) { | |
88fb7f27 JH |
679 | char buf[256], *cp = buf; |
680 | if (ref->flag & REF_ISSYMREF) | |
681 | cp = copy_advance(cp, ",symref"); | |
682 | if (ref->flag & REF_ISPACKED) | |
683 | cp = copy_advance(cp, ",packed"); | |
684 | if (cp == buf) | |
685 | v->s = ""; | |
686 | else { | |
687 | *cp = '\0'; | |
688 | v->s = xstrdup(buf + 1); | |
689 | } | |
690 | continue; | |
cb6bd572 | 691 | } else if (!deref && grab_objectname(name, ref->objectname, v)) { |
b74cf648 | 692 | continue; |
7a48b832 RR |
693 | } else if (!strcmp(name, "HEAD")) { |
694 | const char *head; | |
695 | unsigned char sha1[20]; | |
b28061ce | 696 | |
7695d118 RS |
697 | head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, |
698 | sha1, NULL); | |
7a48b832 RR |
699 | if (!strcmp(ref->refname, head)) |
700 | v->s = "*"; | |
701 | else | |
702 | v->s = " "; | |
703 | continue; | |
704 | } else | |
8db9a4b8 JK |
705 | continue; |
706 | ||
707 | formatp = strchr(name, ':'); | |
8db9a4b8 | 708 | if (formatp) { |
b28061ce RR |
709 | int num_ours, num_theirs; |
710 | ||
8db9a4b8 JK |
711 | formatp++; |
712 | if (!strcmp(formatp, "short")) | |
2bb98169 BW |
713 | refname = shorten_unambiguous_ref(refname, |
714 | warn_ambiguous_refs); | |
b28061ce | 715 | else if (!strcmp(formatp, "track") && |
ad704485 | 716 | starts_with(name, "upstream")) { |
b28061ce RR |
717 | char buf[40]; |
718 | ||
b6160d95 RKC |
719 | if (stat_tracking_info(branch, &num_ours, |
720 | &num_theirs) != 1) | |
721 | continue; | |
722 | ||
b28061ce RR |
723 | if (!num_ours && !num_theirs) |
724 | v->s = ""; | |
725 | else if (!num_ours) { | |
726 | sprintf(buf, "[behind %d]", num_theirs); | |
727 | v->s = xstrdup(buf); | |
728 | } else if (!num_theirs) { | |
729 | sprintf(buf, "[ahead %d]", num_ours); | |
730 | v->s = xstrdup(buf); | |
731 | } else { | |
732 | sprintf(buf, "[ahead %d, behind %d]", | |
733 | num_ours, num_theirs); | |
734 | v->s = xstrdup(buf); | |
735 | } | |
736 | continue; | |
737 | } else if (!strcmp(formatp, "trackshort") && | |
ad704485 | 738 | starts_with(name, "upstream")) { |
b28061ce | 739 | assert(branch); |
b6160d95 RKC |
740 | |
741 | if (stat_tracking_info(branch, &num_ours, | |
742 | &num_theirs) != 1) | |
743 | continue; | |
744 | ||
b28061ce RR |
745 | if (!num_ours && !num_theirs) |
746 | v->s = "="; | |
747 | else if (!num_ours) | |
748 | v->s = "<"; | |
749 | else if (!num_theirs) | |
750 | v->s = ">"; | |
751 | else | |
752 | v->s = "<>"; | |
753 | continue; | |
754 | } else | |
8db9a4b8 JK |
755 | die("unknown %.*s format %s", |
756 | (int)(formatp - name), name, formatp); | |
757 | } | |
758 | ||
759 | if (!deref) | |
760 | v->s = refname; | |
761 | else { | |
762 | int len = strlen(refname); | |
763 | char *s = xmalloc(len + 4); | |
764 | sprintf(s, "%s^{}", refname); | |
765 | v->s = s; | |
9f613ddd JH |
766 | } |
767 | } | |
768 | ||
b7dd2d20 AK |
769 | for (i = 0; i < used_atom_cnt; i++) { |
770 | struct atom_value *v = &ref->value[i]; | |
771 | if (v->s == NULL) | |
772 | goto need_obj; | |
773 | } | |
774 | return; | |
775 | ||
776 | need_obj: | |
777 | buf = get_obj(ref->objectname, &obj, &size, &eaten); | |
778 | if (!buf) | |
779 | die("missing object %s for %s", | |
780 | sha1_to_hex(ref->objectname), ref->refname); | |
781 | if (!obj) | |
782 | die("parse_object_buffer failed on %s for %s", | |
783 | sha1_to_hex(ref->objectname), ref->refname); | |
784 | ||
9f613ddd JH |
785 | grab_values(ref->value, 0, obj, buf, size); |
786 | if (!eaten) | |
787 | free(buf); | |
788 | ||
40dae309 JH |
789 | /* |
790 | * If there is no atom that wants to know about tagged | |
9f613ddd JH |
791 | * object, we are done. |
792 | */ | |
793 | if (!need_tagged || (obj->type != OBJ_TAG)) | |
794 | return; | |
795 | ||
40dae309 JH |
796 | /* |
797 | * If it is a tag object, see if we use a value that derefs | |
9f613ddd JH |
798 | * the object, and if we do grab the object it refers to. |
799 | */ | |
800 | tagged = ((struct tag *)obj)->tagged->sha1; | |
801 | ||
40dae309 JH |
802 | /* |
803 | * NEEDSWORK: This derefs tag only once, which | |
9f613ddd JH |
804 | * is good to deal with chains of trust, but |
805 | * is not consistent with what deref_tag() does | |
806 | * which peels the onion to the core. | |
807 | */ | |
808 | buf = get_obj(tagged, &obj, &size, &eaten); | |
809 | if (!buf) | |
810 | die("missing object %s for %s", | |
811 | sha1_to_hex(tagged), ref->refname); | |
812 | if (!obj) | |
813 | die("parse_object_buffer failed on %s for %s", | |
814 | sha1_to_hex(tagged), ref->refname); | |
815 | grab_values(ref->value, 1, obj, buf, size); | |
816 | if (!eaten) | |
817 | free(buf); | |
818 | } | |
819 | ||
820 | /* | |
821 | * Given a ref, return the value for the atom. This lazily gets value | |
822 | * out of the object by calling populate value. | |
823 | */ | |
824 | static void get_value(struct refinfo *ref, int atom, struct atom_value **v) | |
825 | { | |
826 | if (!ref->value) { | |
827 | populate_value(ref); | |
828 | fill_missing_values(ref->value); | |
829 | } | |
830 | *v = &ref->value[atom]; | |
831 | } | |
832 | ||
340adb8b JH |
833 | struct grab_ref_cbdata { |
834 | struct refinfo **grab_array; | |
835 | const char **grab_pattern; | |
836 | int grab_cnt; | |
837 | }; | |
9f613ddd JH |
838 | |
839 | /* | |
40dae309 JH |
840 | * A call-back given to for_each_ref(). Filter refs and keep them for |
841 | * later object processing. | |
9f613ddd | 842 | */ |
340adb8b | 843 | static int grab_single_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
9f613ddd | 844 | { |
340adb8b | 845 | struct grab_ref_cbdata *cb = cb_data; |
9f613ddd JH |
846 | struct refinfo *ref; |
847 | int cnt; | |
848 | ||
971c41c7 RS |
849 | if (flag & REF_BAD_NAME) { |
850 | warning("ignoring ref with broken name %s", refname); | |
851 | return 0; | |
852 | } | |
853 | ||
340adb8b | 854 | if (*cb->grab_pattern) { |
9f613ddd JH |
855 | const char **pattern; |
856 | int namelen = strlen(refname); | |
340adb8b | 857 | for (pattern = cb->grab_pattern; *pattern; pattern++) { |
9f613ddd JH |
858 | const char *p = *pattern; |
859 | int plen = strlen(p); | |
860 | ||
861 | if ((plen <= namelen) && | |
862 | !strncmp(refname, p, plen) && | |
863 | (refname[plen] == '\0' || | |
114ef908 BS |
864 | refname[plen] == '/' || |
865 | p[plen-1] == '/')) | |
9f613ddd | 866 | break; |
eb07894f | 867 | if (!wildmatch(p, refname, WM_PATHNAME, NULL)) |
9f613ddd JH |
868 | break; |
869 | } | |
870 | if (!*pattern) | |
871 | return 0; | |
872 | } | |
873 | ||
40dae309 JH |
874 | /* |
875 | * We do not open the object yet; sort may only need refname | |
9f613ddd JH |
876 | * to do its job and the resulting list may yet to be pruned |
877 | * by maxcount logic. | |
878 | */ | |
879 | ref = xcalloc(1, sizeof(*ref)); | |
880 | ref->refname = xstrdup(refname); | |
881 | hashcpy(ref->objectname, sha1); | |
5cdd628c | 882 | ref->flag = flag; |
9f613ddd | 883 | |
340adb8b | 884 | cnt = cb->grab_cnt; |
2756ca43 | 885 | REALLOC_ARRAY(cb->grab_array, cnt + 1); |
340adb8b JH |
886 | cb->grab_array[cnt++] = ref; |
887 | cb->grab_cnt = cnt; | |
9f613ddd JH |
888 | return 0; |
889 | } | |
890 | ||
9f613ddd JH |
891 | static int cmp_ref_sort(struct ref_sort *s, struct refinfo *a, struct refinfo *b) |
892 | { | |
893 | struct atom_value *va, *vb; | |
894 | int cmp; | |
895 | cmp_type cmp_type = used_atom_type[s->atom]; | |
896 | ||
897 | get_value(a, s->atom, &va); | |
898 | get_value(b, s->atom, &vb); | |
899 | switch (cmp_type) { | |
900 | case FIELD_STR: | |
901 | cmp = strcmp(va->s, vb->s); | |
902 | break; | |
903 | default: | |
904 | if (va->ul < vb->ul) | |
905 | cmp = -1; | |
906 | else if (va->ul == vb->ul) | |
907 | cmp = 0; | |
908 | else | |
909 | cmp = 1; | |
910 | break; | |
911 | } | |
912 | return (s->reverse) ? -cmp : cmp; | |
913 | } | |
914 | ||
915 | static struct ref_sort *ref_sort; | |
916 | static int compare_refs(const void *a_, const void *b_) | |
917 | { | |
918 | struct refinfo *a = *((struct refinfo **)a_); | |
919 | struct refinfo *b = *((struct refinfo **)b_); | |
920 | struct ref_sort *s; | |
921 | ||
922 | for (s = ref_sort; s; s = s->next) { | |
923 | int cmp = cmp_ref_sort(s, a, b); | |
924 | if (cmp) | |
925 | return cmp; | |
926 | } | |
927 | return 0; | |
928 | } | |
929 | ||
930 | static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs) | |
931 | { | |
932 | ref_sort = sort; | |
933 | qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs); | |
934 | } | |
935 | ||
db64eb65 | 936 | static void print_value(struct atom_value *v, int quote_style) |
9f613ddd | 937 | { |
10d0167f | 938 | struct strbuf sb = STRBUF_INIT; |
9f613ddd JH |
939 | switch (quote_style) { |
940 | case QUOTE_NONE: | |
941 | fputs(v->s, stdout); | |
942 | break; | |
943 | case QUOTE_SHELL: | |
10d0167f | 944 | sq_quote_buf(&sb, v->s); |
9f613ddd JH |
945 | break; |
946 | case QUOTE_PERL: | |
10d0167f | 947 | perl_quote_buf(&sb, v->s); |
9f613ddd JH |
948 | break; |
949 | case QUOTE_PYTHON: | |
10d0167f | 950 | python_quote_buf(&sb, v->s); |
9f613ddd | 951 | break; |
5558e55c | 952 | case QUOTE_TCL: |
10d0167f | 953 | tcl_quote_buf(&sb, v->s); |
5558e55c | 954 | break; |
9f613ddd | 955 | } |
10d0167f NTND |
956 | if (quote_style != QUOTE_NONE) { |
957 | fputs(sb.buf, stdout); | |
958 | strbuf_release(&sb); | |
959 | } | |
9f613ddd JH |
960 | } |
961 | ||
962 | static int hex1(char ch) | |
963 | { | |
964 | if ('0' <= ch && ch <= '9') | |
965 | return ch - '0'; | |
966 | else if ('a' <= ch && ch <= 'f') | |
967 | return ch - 'a' + 10; | |
968 | else if ('A' <= ch && ch <= 'F') | |
969 | return ch - 'A' + 10; | |
970 | return -1; | |
971 | } | |
972 | static int hex2(const char *cp) | |
973 | { | |
974 | if (cp[0] && cp[1]) | |
975 | return (hex1(cp[0]) << 4) | hex1(cp[1]); | |
976 | else | |
977 | return -1; | |
978 | } | |
979 | ||
980 | static void emit(const char *cp, const char *ep) | |
981 | { | |
982 | while (*cp && (!ep || cp < ep)) { | |
983 | if (*cp == '%') { | |
984 | if (cp[1] == '%') | |
985 | cp++; | |
986 | else { | |
987 | int ch = hex2(cp + 1); | |
988 | if (0 <= ch) { | |
989 | putchar(ch); | |
990 | cp += 3; | |
991 | continue; | |
992 | } | |
993 | } | |
994 | } | |
995 | putchar(*cp); | |
996 | cp++; | |
997 | } | |
998 | } | |
999 | ||
1000 | static void show_ref(struct refinfo *info, const char *format, int quote_style) | |
1001 | { | |
1002 | const char *cp, *sp, *ep; | |
1003 | ||
1004 | for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) { | |
db64eb65 RR |
1005 | struct atom_value *atomv; |
1006 | ||
9f613ddd JH |
1007 | ep = strchr(sp, ')'); |
1008 | if (cp < sp) | |
1009 | emit(cp, sp); | |
db64eb65 RR |
1010 | get_value(info, parse_atom(sp + 2, ep), &atomv); |
1011 | print_value(atomv, quote_style); | |
9f613ddd JH |
1012 | } |
1013 | if (*cp) { | |
1014 | sp = cp + strlen(cp); | |
1015 | emit(cp, sp); | |
1016 | } | |
db64eb65 RR |
1017 | if (need_color_reset_at_eol) { |
1018 | struct atom_value resetv; | |
1019 | char color[COLOR_MAXLEN] = ""; | |
1020 | ||
f6c5a296 JK |
1021 | if (color_parse("reset", color) < 0) |
1022 | die("BUG: couldn't parse 'reset' as a color"); | |
db64eb65 RR |
1023 | resetv.s = color; |
1024 | print_value(&resetv, quote_style); | |
1025 | } | |
9f613ddd JH |
1026 | putchar('\n'); |
1027 | } | |
1028 | ||
1029 | static struct ref_sort *default_sort(void) | |
1030 | { | |
1031 | static const char cstr_name[] = "refname"; | |
1032 | ||
1033 | struct ref_sort *sort = xcalloc(1, sizeof(*sort)); | |
1034 | ||
1035 | sort->next = NULL; | |
1036 | sort->atom = parse_atom(cstr_name, cstr_name + strlen(cstr_name)); | |
1037 | return sort; | |
1038 | } | |
1039 | ||
186458b1 | 1040 | static int opt_parse_sort(const struct option *opt, const char *arg, int unset) |
c3428da8 PH |
1041 | { |
1042 | struct ref_sort **sort_tail = opt->value; | |
1043 | struct ref_sort *s; | |
1044 | int len; | |
1045 | ||
1046 | if (!arg) /* should --no-sort void the list ? */ | |
1047 | return -1; | |
1048 | ||
3b51222c KK |
1049 | s = xcalloc(1, sizeof(*s)); |
1050 | s->next = *sort_tail; | |
1051 | *sort_tail = s; | |
c3428da8 PH |
1052 | |
1053 | if (*arg == '-') { | |
1054 | s->reverse = 1; | |
1055 | arg++; | |
1056 | } | |
1057 | len = strlen(arg); | |
1058 | s->atom = parse_atom(arg, arg+len); | |
1059 | return 0; | |
1060 | } | |
1061 | ||
1062 | static char const * const for_each_ref_usage[] = { | |
9c9b4f2f | 1063 | N_("git for-each-ref [<options>] [<pattern>]"), |
c3428da8 PH |
1064 | NULL |
1065 | }; | |
1066 | ||
1067 | int cmd_for_each_ref(int argc, const char **argv, const char *prefix) | |
9f613ddd JH |
1068 | { |
1069 | int i, num_refs; | |
c3428da8 | 1070 | const char *format = "%(objectname) %(objecttype)\t%(refname)"; |
9f613ddd | 1071 | struct ref_sort *sort = NULL, **sort_tail = &sort; |
9fac800c | 1072 | int maxcount = 0, quote_style = 0; |
9f613ddd | 1073 | struct refinfo **refs; |
340adb8b | 1074 | struct grab_ref_cbdata cbdata; |
2b2a5be3 MH |
1075 | struct each_ref_fn_sha1_adapter wrapped_grab_single_ref = |
1076 | {grab_single_ref, &cbdata}; | |
9f613ddd | 1077 | |
c3428da8 | 1078 | struct option opts[] = { |
9fac800c | 1079 | OPT_BIT('s', "shell", "e_style, |
18913521 | 1080 | N_("quote placeholders suitably for shells"), QUOTE_SHELL), |
9fac800c | 1081 | OPT_BIT('p', "perl", "e_style, |
18913521 | 1082 | N_("quote placeholders suitably for perl"), QUOTE_PERL), |
9fac800c | 1083 | OPT_BIT(0 , "python", "e_style, |
18913521 | 1084 | N_("quote placeholders suitably for python"), QUOTE_PYTHON), |
9fac800c | 1085 | OPT_BIT(0 , "tcl", "e_style, |
b799a696 | 1086 | N_("quote placeholders suitably for Tcl"), QUOTE_TCL), |
c3428da8 PH |
1087 | |
1088 | OPT_GROUP(""), | |
18913521 NTND |
1089 | OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")), |
1090 | OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")), | |
1091 | OPT_CALLBACK(0 , "sort", sort_tail, N_("key"), | |
1092 | N_("field name to sort on"), &opt_parse_sort), | |
c3428da8 PH |
1093 | OPT_END(), |
1094 | }; | |
1095 | ||
37782920 | 1096 | parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0); |
c3428da8 PH |
1097 | if (maxcount < 0) { |
1098 | error("invalid --count argument: `%d'", maxcount); | |
1099 | usage_with_options(for_each_ref_usage, opts); | |
9f613ddd | 1100 | } |
9fac800c | 1101 | if (HAS_MULTI_BITS(quote_style)) { |
c9ecf4f1 | 1102 | error("more than one quoting style?"); |
c3428da8 PH |
1103 | usage_with_options(for_each_ref_usage, opts); |
1104 | } | |
1105 | if (verify_format(format)) | |
1106 | usage_with_options(for_each_ref_usage, opts); | |
9f613ddd JH |
1107 | |
1108 | if (!sort) | |
1109 | sort = default_sort(); | |
9f613ddd | 1110 | |
2bb98169 BW |
1111 | /* for warn_ambiguous_refs */ |
1112 | git_config(git_default_config, NULL); | |
1113 | ||
340adb8b | 1114 | memset(&cbdata, 0, sizeof(cbdata)); |
c3428da8 | 1115 | cbdata.grab_pattern = argv; |
2b2a5be3 | 1116 | for_each_rawref(each_ref_fn_adapter, &wrapped_grab_single_ref); |
340adb8b JH |
1117 | refs = cbdata.grab_array; |
1118 | num_refs = cbdata.grab_cnt; | |
9f613ddd | 1119 | |
9f613ddd JH |
1120 | sort_refs(sort, refs, num_refs); |
1121 | ||
1122 | if (!maxcount || num_refs < maxcount) | |
1123 | maxcount = num_refs; | |
1124 | for (i = 0; i < maxcount; i++) | |
1125 | show_ref(refs[i], format, quote_style); | |
1126 | return 0; | |
1127 | } |