]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/cat-file.c
Merge branch 'jk/fsck-indices-in-worktrees'
[thirdparty/git.git] / builtin / cat-file.c
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define USE_THE_INDEX_VARIABLE
7 #include "cache.h"
8 #include "config.h"
9 #include "builtin.h"
10 #include "diff.h"
11 #include "parse-options.h"
12 #include "userdiff.h"
13 #include "streaming.h"
14 #include "tree-walk.h"
15 #include "oid-array.h"
16 #include "packfile.h"
17 #include "object-store.h"
18 #include "promisor-remote.h"
19 #include "mailmap.h"
20
21 enum batch_mode {
22 BATCH_MODE_CONTENTS,
23 BATCH_MODE_INFO,
24 BATCH_MODE_QUEUE_AND_DISPATCH,
25 };
26
27 struct batch_options {
28 int enabled;
29 int follow_symlinks;
30 enum batch_mode batch_mode;
31 int buffer_output;
32 int all_objects;
33 int unordered;
34 int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */
35 int nul_terminated;
36 const char *format;
37 };
38
39 static const char *force_path;
40
41 static struct string_list mailmap = STRING_LIST_INIT_NODUP;
42 static int use_mailmap;
43
44 static char *replace_idents_using_mailmap(char *, size_t *);
45
46 static char *replace_idents_using_mailmap(char *object_buf, size_t *size)
47 {
48 struct strbuf sb = STRBUF_INIT;
49 const char *headers[] = { "author ", "committer ", "tagger ", NULL };
50
51 strbuf_attach(&sb, object_buf, *size, *size + 1);
52 apply_mailmap_to_header(&sb, headers, &mailmap);
53 *size = sb.len;
54 return strbuf_detach(&sb, NULL);
55 }
56
57 static int filter_object(const char *path, unsigned mode,
58 const struct object_id *oid,
59 char **buf, unsigned long *size)
60 {
61 enum object_type type;
62
63 *buf = read_object_file(oid, &type, size);
64 if (!*buf)
65 return error(_("cannot read object %s '%s'"),
66 oid_to_hex(oid), path);
67 if ((type == OBJ_BLOB) && S_ISREG(mode)) {
68 struct strbuf strbuf = STRBUF_INIT;
69 struct checkout_metadata meta;
70
71 init_checkout_metadata(&meta, NULL, NULL, oid);
72 if (convert_to_working_tree(&the_index, path, *buf, *size, &strbuf, &meta)) {
73 free(*buf);
74 *size = strbuf.len;
75 *buf = strbuf_detach(&strbuf, NULL);
76 }
77 }
78
79 return 0;
80 }
81
82 static int stream_blob(const struct object_id *oid)
83 {
84 if (stream_blob_to_fd(1, oid, NULL, 0))
85 die("unable to stream %s to stdout", oid_to_hex(oid));
86 return 0;
87 }
88
89 static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
90 int unknown_type)
91 {
92 int ret;
93 struct object_id oid;
94 enum object_type type;
95 char *buf;
96 unsigned long size;
97 struct object_context obj_context;
98 struct object_info oi = OBJECT_INFO_INIT;
99 struct strbuf sb = STRBUF_INIT;
100 unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
101 unsigned get_oid_flags = GET_OID_RECORD_PATH | GET_OID_ONLY_TO_DIE;
102 const char *path = force_path;
103 const int opt_cw = (opt == 'c' || opt == 'w');
104 if (!path && opt_cw)
105 get_oid_flags |= GET_OID_REQUIRE_PATH;
106
107 if (unknown_type)
108 flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
109
110 if (get_oid_with_context(the_repository, obj_name, get_oid_flags, &oid,
111 &obj_context))
112 die("Not a valid object name %s", obj_name);
113
114 if (!path)
115 path = obj_context.path;
116 if (obj_context.mode == S_IFINVALID)
117 obj_context.mode = 0100644;
118
119 buf = NULL;
120 switch (opt) {
121 case 't':
122 oi.type_name = &sb;
123 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
124 die("git cat-file: could not get object info");
125 if (sb.len) {
126 printf("%s\n", sb.buf);
127 strbuf_release(&sb);
128 ret = 0;
129 goto cleanup;
130 }
131 break;
132
133 case 's':
134 oi.sizep = &size;
135
136 if (use_mailmap) {
137 oi.typep = &type;
138 oi.contentp = (void**)&buf;
139 }
140
141 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
142 die("git cat-file: could not get object info");
143
144 if (use_mailmap && (type == OBJ_COMMIT || type == OBJ_TAG)) {
145 size_t s = size;
146 buf = replace_idents_using_mailmap(buf, &s);
147 size = cast_size_t_to_ulong(s);
148 }
149
150 printf("%"PRIuMAX"\n", (uintmax_t)size);
151 ret = 0;
152 goto cleanup;
153
154 case 'e':
155 return !has_object_file(&oid);
156
157 case 'w':
158
159 if (filter_object(path, obj_context.mode,
160 &oid, &buf, &size)) {
161 ret = -1;
162 goto cleanup;
163 }
164 break;
165
166 case 'c':
167 if (textconv_object(the_repository, path, obj_context.mode,
168 &oid, 1, &buf, &size))
169 break;
170 /* else fallthrough */
171
172 case 'p':
173 type = oid_object_info(the_repository, &oid, NULL);
174 if (type < 0)
175 die("Not a valid object name %s", obj_name);
176
177 /* custom pretty-print here */
178 if (type == OBJ_TREE) {
179 const char *ls_args[3] = { NULL };
180 ls_args[0] = "ls-tree";
181 ls_args[1] = obj_name;
182 ret = cmd_ls_tree(2, ls_args, NULL);
183 goto cleanup;
184 }
185
186 if (type == OBJ_BLOB) {
187 ret = stream_blob(&oid);
188 goto cleanup;
189 }
190 buf = read_object_file(&oid, &type, &size);
191 if (!buf)
192 die("Cannot read object %s", obj_name);
193
194 if (use_mailmap) {
195 size_t s = size;
196 buf = replace_idents_using_mailmap(buf, &s);
197 size = cast_size_t_to_ulong(s);
198 }
199
200 /* otherwise just spit out the data */
201 break;
202
203 case 0:
204 {
205 enum object_type exp_type_id = type_from_string(exp_type);
206
207 if (exp_type_id == OBJ_BLOB) {
208 struct object_id blob_oid;
209 if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
210 char *buffer = read_object_file(&oid, &type,
211 &size);
212 const char *target;
213 if (!skip_prefix(buffer, "object ", &target) ||
214 get_oid_hex(target, &blob_oid))
215 die("%s not a valid tag", oid_to_hex(&oid));
216 free(buffer);
217 } else
218 oidcpy(&blob_oid, &oid);
219
220 if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB) {
221 ret = stream_blob(&blob_oid);
222 goto cleanup;
223 }
224 /*
225 * we attempted to dereference a tag to a blob
226 * and failed; there may be new dereference
227 * mechanisms this code is not aware of.
228 * fall-back to the usual case.
229 */
230 }
231 buf = read_object_with_reference(the_repository, &oid,
232 exp_type_id, &size, NULL);
233
234 if (use_mailmap) {
235 size_t s = size;
236 buf = replace_idents_using_mailmap(buf, &s);
237 size = cast_size_t_to_ulong(s);
238 }
239 break;
240 }
241 default:
242 die("git cat-file: unknown option: %s", exp_type);
243 }
244
245 if (!buf)
246 die("git cat-file %s: bad file", obj_name);
247
248 write_or_die(1, buf, size);
249 ret = 0;
250 cleanup:
251 free(buf);
252 free(obj_context.path);
253 return ret;
254 }
255
256 struct expand_data {
257 struct object_id oid;
258 enum object_type type;
259 unsigned long size;
260 off_t disk_size;
261 const char *rest;
262 struct object_id delta_base_oid;
263
264 /*
265 * If mark_query is true, we do not expand anything, but rather
266 * just mark the object_info with items we wish to query.
267 */
268 int mark_query;
269
270 /*
271 * Whether to split the input on whitespace before feeding it to
272 * get_sha1; this is decided during the mark_query phase based on
273 * whether we have a %(rest) token in our format.
274 */
275 int split_on_whitespace;
276
277 /*
278 * After a mark_query run, this object_info is set up to be
279 * passed to oid_object_info_extended. It will point to the data
280 * elements above, so you can retrieve the response from there.
281 */
282 struct object_info info;
283
284 /*
285 * This flag will be true if the requested batch format and options
286 * don't require us to call oid_object_info, which can then be
287 * optimized out.
288 */
289 unsigned skip_object_info : 1;
290 };
291
292 static int is_atom(const char *atom, const char *s, int slen)
293 {
294 int alen = strlen(atom);
295 return alen == slen && !memcmp(atom, s, alen);
296 }
297
298 static void expand_atom(struct strbuf *sb, const char *atom, int len,
299 void *vdata)
300 {
301 struct expand_data *data = vdata;
302
303 if (is_atom("objectname", atom, len)) {
304 if (!data->mark_query)
305 strbuf_addstr(sb, oid_to_hex(&data->oid));
306 } else if (is_atom("objecttype", atom, len)) {
307 if (data->mark_query)
308 data->info.typep = &data->type;
309 else
310 strbuf_addstr(sb, type_name(data->type));
311 } else if (is_atom("objectsize", atom, len)) {
312 if (data->mark_query)
313 data->info.sizep = &data->size;
314 else
315 strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
316 } else if (is_atom("objectsize:disk", atom, len)) {
317 if (data->mark_query)
318 data->info.disk_sizep = &data->disk_size;
319 else
320 strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
321 } else if (is_atom("rest", atom, len)) {
322 if (data->mark_query)
323 data->split_on_whitespace = 1;
324 else if (data->rest)
325 strbuf_addstr(sb, data->rest);
326 } else if (is_atom("deltabase", atom, len)) {
327 if (data->mark_query)
328 data->info.delta_base_oid = &data->delta_base_oid;
329 else
330 strbuf_addstr(sb,
331 oid_to_hex(&data->delta_base_oid));
332 } else
333 die("unknown format element: %.*s", len, atom);
334 }
335
336 static size_t expand_format(struct strbuf *sb, const char *start, void *data)
337 {
338 const char *end;
339
340 if (*start != '(')
341 return 0;
342 end = strchr(start + 1, ')');
343 if (!end)
344 die("format element '%s' does not end in ')'", start);
345
346 expand_atom(sb, start + 1, end - start - 1, data);
347
348 return end - start + 1;
349 }
350
351 static void batch_write(struct batch_options *opt, const void *data, int len)
352 {
353 if (opt->buffer_output) {
354 if (fwrite(data, 1, len, stdout) != len)
355 die_errno("unable to write to stdout");
356 } else
357 write_or_die(1, data, len);
358 }
359
360 static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
361 {
362 const struct object_id *oid = &data->oid;
363
364 assert(data->info.typep);
365
366 if (data->type == OBJ_BLOB) {
367 if (opt->buffer_output)
368 fflush(stdout);
369 if (opt->transform_mode) {
370 char *contents;
371 unsigned long size;
372
373 if (!data->rest)
374 die("missing path for '%s'", oid_to_hex(oid));
375
376 if (opt->transform_mode == 'w') {
377 if (filter_object(data->rest, 0100644, oid,
378 &contents, &size))
379 die("could not convert '%s' %s",
380 oid_to_hex(oid), data->rest);
381 } else if (opt->transform_mode == 'c') {
382 enum object_type type;
383 if (!textconv_object(the_repository,
384 data->rest, 0100644, oid,
385 1, &contents, &size))
386 contents = read_object_file(oid,
387 &type,
388 &size);
389 if (!contents)
390 die("could not convert '%s' %s",
391 oid_to_hex(oid), data->rest);
392 } else
393 BUG("invalid transform_mode: %c", opt->transform_mode);
394 batch_write(opt, contents, size);
395 free(contents);
396 } else {
397 stream_blob(oid);
398 }
399 }
400 else {
401 enum object_type type;
402 unsigned long size;
403 void *contents;
404
405 contents = read_object_file(oid, &type, &size);
406
407 if (use_mailmap) {
408 size_t s = size;
409 contents = replace_idents_using_mailmap(contents, &s);
410 size = cast_size_t_to_ulong(s);
411 }
412
413 if (!contents)
414 die("object %s disappeared", oid_to_hex(oid));
415 if (type != data->type)
416 die("object %s changed type!?", oid_to_hex(oid));
417 if (data->info.sizep && size != data->size && !use_mailmap)
418 die("object %s changed size!?", oid_to_hex(oid));
419
420 batch_write(opt, contents, size);
421 free(contents);
422 }
423 }
424
425 static void print_default_format(struct strbuf *scratch, struct expand_data *data)
426 {
427 strbuf_addf(scratch, "%s %s %"PRIuMAX"\n", oid_to_hex(&data->oid),
428 type_name(data->type),
429 (uintmax_t)data->size);
430 }
431
432 /*
433 * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
434 * which the object may be accessed (though note that we may also rely on
435 * data->oid, too). If "pack" is NULL, then offset is ignored.
436 */
437 static void batch_object_write(const char *obj_name,
438 struct strbuf *scratch,
439 struct batch_options *opt,
440 struct expand_data *data,
441 struct packed_git *pack,
442 off_t offset)
443 {
444 if (!data->skip_object_info) {
445 int ret;
446
447 if (use_mailmap)
448 data->info.typep = &data->type;
449
450 if (pack)
451 ret = packed_object_info(the_repository, pack, offset,
452 &data->info);
453 else
454 ret = oid_object_info_extended(the_repository,
455 &data->oid, &data->info,
456 OBJECT_INFO_LOOKUP_REPLACE);
457 if (ret < 0) {
458 printf("%s missing\n",
459 obj_name ? obj_name : oid_to_hex(&data->oid));
460 fflush(stdout);
461 return;
462 }
463
464 if (use_mailmap && (data->type == OBJ_COMMIT || data->type == OBJ_TAG)) {
465 size_t s = data->size;
466 char *buf = NULL;
467
468 buf = repo_read_object_file(the_repository, &data->oid, &data->type,
469 &data->size);
470 buf = replace_idents_using_mailmap(buf, &s);
471 data->size = cast_size_t_to_ulong(s);
472
473 free(buf);
474 }
475 }
476
477 strbuf_reset(scratch);
478
479 if (!opt->format) {
480 print_default_format(scratch, data);
481 } else {
482 strbuf_expand(scratch, opt->format, expand_format, data);
483 strbuf_addch(scratch, '\n');
484 }
485
486 batch_write(opt, scratch->buf, scratch->len);
487
488 if (opt->batch_mode == BATCH_MODE_CONTENTS) {
489 print_object_or_die(opt, data);
490 batch_write(opt, "\n", 1);
491 }
492 }
493
494 static void batch_one_object(const char *obj_name,
495 struct strbuf *scratch,
496 struct batch_options *opt,
497 struct expand_data *data)
498 {
499 struct object_context ctx;
500 int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
501 enum get_oid_result result;
502
503 result = get_oid_with_context(the_repository, obj_name,
504 flags, &data->oid, &ctx);
505 if (result != FOUND) {
506 switch (result) {
507 case MISSING_OBJECT:
508 printf("%s missing\n", obj_name);
509 break;
510 case SHORT_NAME_AMBIGUOUS:
511 printf("%s ambiguous\n", obj_name);
512 break;
513 case DANGLING_SYMLINK:
514 printf("dangling %"PRIuMAX"\n%s\n",
515 (uintmax_t)strlen(obj_name), obj_name);
516 break;
517 case SYMLINK_LOOP:
518 printf("loop %"PRIuMAX"\n%s\n",
519 (uintmax_t)strlen(obj_name), obj_name);
520 break;
521 case NOT_DIR:
522 printf("notdir %"PRIuMAX"\n%s\n",
523 (uintmax_t)strlen(obj_name), obj_name);
524 break;
525 default:
526 BUG("unknown get_sha1_with_context result %d\n",
527 result);
528 break;
529 }
530 fflush(stdout);
531 return;
532 }
533
534 if (ctx.mode == 0) {
535 printf("symlink %"PRIuMAX"\n%s\n",
536 (uintmax_t)ctx.symlink_path.len,
537 ctx.symlink_path.buf);
538 fflush(stdout);
539 return;
540 }
541
542 batch_object_write(obj_name, scratch, opt, data, NULL, 0);
543 }
544
545 struct object_cb_data {
546 struct batch_options *opt;
547 struct expand_data *expand;
548 struct oidset *seen;
549 struct strbuf *scratch;
550 };
551
552 static int batch_object_cb(const struct object_id *oid, void *vdata)
553 {
554 struct object_cb_data *data = vdata;
555 oidcpy(&data->expand->oid, oid);
556 batch_object_write(NULL, data->scratch, data->opt, data->expand,
557 NULL, 0);
558 return 0;
559 }
560
561 static int collect_loose_object(const struct object_id *oid,
562 const char *path,
563 void *data)
564 {
565 oid_array_append(data, oid);
566 return 0;
567 }
568
569 static int collect_packed_object(const struct object_id *oid,
570 struct packed_git *pack,
571 uint32_t pos,
572 void *data)
573 {
574 oid_array_append(data, oid);
575 return 0;
576 }
577
578 static int batch_unordered_object(const struct object_id *oid,
579 struct packed_git *pack, off_t offset,
580 void *vdata)
581 {
582 struct object_cb_data *data = vdata;
583
584 if (oidset_insert(data->seen, oid))
585 return 0;
586
587 oidcpy(&data->expand->oid, oid);
588 batch_object_write(NULL, data->scratch, data->opt, data->expand,
589 pack, offset);
590 return 0;
591 }
592
593 static int batch_unordered_loose(const struct object_id *oid,
594 const char *path,
595 void *data)
596 {
597 return batch_unordered_object(oid, NULL, 0, data);
598 }
599
600 static int batch_unordered_packed(const struct object_id *oid,
601 struct packed_git *pack,
602 uint32_t pos,
603 void *data)
604 {
605 return batch_unordered_object(oid, pack,
606 nth_packed_object_offset(pack, pos),
607 data);
608 }
609
610 typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *,
611 struct strbuf *, struct expand_data *);
612
613 struct queued_cmd {
614 parse_cmd_fn_t fn;
615 char *line;
616 };
617
618 static void parse_cmd_contents(struct batch_options *opt,
619 const char *line,
620 struct strbuf *output,
621 struct expand_data *data)
622 {
623 opt->batch_mode = BATCH_MODE_CONTENTS;
624 batch_one_object(line, output, opt, data);
625 }
626
627 static void parse_cmd_info(struct batch_options *opt,
628 const char *line,
629 struct strbuf *output,
630 struct expand_data *data)
631 {
632 opt->batch_mode = BATCH_MODE_INFO;
633 batch_one_object(line, output, opt, data);
634 }
635
636 static void dispatch_calls(struct batch_options *opt,
637 struct strbuf *output,
638 struct expand_data *data,
639 struct queued_cmd *cmd,
640 int nr)
641 {
642 int i;
643
644 if (!opt->buffer_output)
645 die(_("flush is only for --buffer mode"));
646
647 for (i = 0; i < nr; i++)
648 cmd[i].fn(opt, cmd[i].line, output, data);
649
650 fflush(stdout);
651 }
652
653 static void free_cmds(struct queued_cmd *cmd, size_t *nr)
654 {
655 size_t i;
656
657 for (i = 0; i < *nr; i++)
658 FREE_AND_NULL(cmd[i].line);
659
660 *nr = 0;
661 }
662
663
664 static const struct parse_cmd {
665 const char *name;
666 parse_cmd_fn_t fn;
667 unsigned takes_args;
668 } commands[] = {
669 { "contents", parse_cmd_contents, 1},
670 { "info", parse_cmd_info, 1},
671 { "flush", NULL, 0},
672 };
673
674 static void batch_objects_command(struct batch_options *opt,
675 struct strbuf *output,
676 struct expand_data *data)
677 {
678 struct strbuf input = STRBUF_INIT;
679 struct queued_cmd *queued_cmd = NULL;
680 size_t alloc = 0, nr = 0;
681
682 while (1) {
683 int i, ret;
684 const struct parse_cmd *cmd = NULL;
685 const char *p = NULL, *cmd_end;
686 struct queued_cmd call = {0};
687
688 if (opt->nul_terminated)
689 ret = strbuf_getline_nul(&input, stdin);
690 else
691 ret = strbuf_getline(&input, stdin);
692
693 if (ret)
694 break;
695
696 if (!input.len)
697 die(_("empty command in input"));
698 if (isspace(*input.buf))
699 die(_("whitespace before command: '%s'"), input.buf);
700
701 for (i = 0; i < ARRAY_SIZE(commands); i++) {
702 if (!skip_prefix(input.buf, commands[i].name, &cmd_end))
703 continue;
704
705 cmd = &commands[i];
706 if (cmd->takes_args) {
707 if (*cmd_end != ' ')
708 die(_("%s requires arguments"),
709 commands[i].name);
710
711 p = cmd_end + 1;
712 } else if (*cmd_end) {
713 die(_("%s takes no arguments"),
714 commands[i].name);
715 }
716
717 break;
718 }
719
720 if (!cmd)
721 die(_("unknown command: '%s'"), input.buf);
722
723 if (!strcmp(cmd->name, "flush")) {
724 dispatch_calls(opt, output, data, queued_cmd, nr);
725 free_cmds(queued_cmd, &nr);
726 } else if (!opt->buffer_output) {
727 cmd->fn(opt, p, output, data);
728 } else {
729 ALLOC_GROW(queued_cmd, nr + 1, alloc);
730 call.fn = cmd->fn;
731 call.line = xstrdup_or_null(p);
732 queued_cmd[nr++] = call;
733 }
734 }
735
736 if (opt->buffer_output &&
737 nr &&
738 !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) {
739 dispatch_calls(opt, output, data, queued_cmd, nr);
740 free_cmds(queued_cmd, &nr);
741 }
742
743 free_cmds(queued_cmd, &nr);
744 free(queued_cmd);
745 strbuf_release(&input);
746 }
747
748 #define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
749
750 static int batch_objects(struct batch_options *opt)
751 {
752 struct strbuf input = STRBUF_INIT;
753 struct strbuf output = STRBUF_INIT;
754 struct expand_data data;
755 int save_warning;
756 int retval = 0;
757
758 /*
759 * Expand once with our special mark_query flag, which will prime the
760 * object_info to be handed to oid_object_info_extended for each
761 * object.
762 */
763 memset(&data, 0, sizeof(data));
764 data.mark_query = 1;
765 strbuf_expand(&output,
766 opt->format ? opt->format : DEFAULT_FORMAT,
767 expand_format,
768 &data);
769 data.mark_query = 0;
770 strbuf_release(&output);
771 if (opt->transform_mode)
772 data.split_on_whitespace = 1;
773
774 if (opt->format && !strcmp(opt->format, DEFAULT_FORMAT))
775 opt->format = NULL;
776 /*
777 * If we are printing out the object, then always fill in the type,
778 * since we will want to decide whether or not to stream.
779 */
780 if (opt->batch_mode == BATCH_MODE_CONTENTS)
781 data.info.typep = &data.type;
782
783 if (opt->all_objects) {
784 struct object_cb_data cb;
785 struct object_info empty = OBJECT_INFO_INIT;
786
787 if (!memcmp(&data.info, &empty, sizeof(empty)))
788 data.skip_object_info = 1;
789
790 if (has_promisor_remote())
791 warning("This repository uses promisor remotes. Some objects may not be loaded.");
792
793 read_replace_refs = 0;
794
795 cb.opt = opt;
796 cb.expand = &data;
797 cb.scratch = &output;
798
799 if (opt->unordered) {
800 struct oidset seen = OIDSET_INIT;
801
802 cb.seen = &seen;
803
804 for_each_loose_object(batch_unordered_loose, &cb, 0);
805 for_each_packed_object(batch_unordered_packed, &cb,
806 FOR_EACH_OBJECT_PACK_ORDER);
807
808 oidset_clear(&seen);
809 } else {
810 struct oid_array sa = OID_ARRAY_INIT;
811
812 for_each_loose_object(collect_loose_object, &sa, 0);
813 for_each_packed_object(collect_packed_object, &sa, 0);
814
815 oid_array_for_each_unique(&sa, batch_object_cb, &cb);
816
817 oid_array_clear(&sa);
818 }
819
820 strbuf_release(&output);
821 return 0;
822 }
823
824 /*
825 * We are going to call get_sha1 on a potentially very large number of
826 * objects. In most large cases, these will be actual object sha1s. The
827 * cost to double-check that each one is not also a ref (just so we can
828 * warn) ends up dwarfing the actual cost of the object lookups
829 * themselves. We can work around it by just turning off the warning.
830 */
831 save_warning = warn_on_object_refname_ambiguity;
832 warn_on_object_refname_ambiguity = 0;
833
834 if (opt->batch_mode == BATCH_MODE_QUEUE_AND_DISPATCH) {
835 batch_objects_command(opt, &output, &data);
836 goto cleanup;
837 }
838
839 while (1) {
840 int ret;
841 if (opt->nul_terminated)
842 ret = strbuf_getline_nul(&input, stdin);
843 else
844 ret = strbuf_getline(&input, stdin);
845
846 if (ret == EOF)
847 break;
848
849 if (data.split_on_whitespace) {
850 /*
851 * Split at first whitespace, tying off the beginning
852 * of the string and saving the remainder (or NULL) in
853 * data.rest.
854 */
855 char *p = strpbrk(input.buf, " \t");
856 if (p) {
857 while (*p && strchr(" \t", *p))
858 *p++ = '\0';
859 }
860 data.rest = p;
861 }
862
863 batch_one_object(input.buf, &output, opt, &data);
864 }
865
866 cleanup:
867 strbuf_release(&input);
868 strbuf_release(&output);
869 warn_on_object_refname_ambiguity = save_warning;
870 return retval;
871 }
872
873 static int git_cat_file_config(const char *var, const char *value, void *cb)
874 {
875 if (userdiff_config(var, value) < 0)
876 return -1;
877
878 return git_default_config(var, value, cb);
879 }
880
881 static int batch_option_callback(const struct option *opt,
882 const char *arg,
883 int unset)
884 {
885 struct batch_options *bo = opt->value;
886
887 BUG_ON_OPT_NEG(unset);
888
889 if (bo->enabled) {
890 return error(_("only one batch option may be specified"));
891 }
892
893 bo->enabled = 1;
894
895 if (!strcmp(opt->long_name, "batch"))
896 bo->batch_mode = BATCH_MODE_CONTENTS;
897 else if (!strcmp(opt->long_name, "batch-check"))
898 bo->batch_mode = BATCH_MODE_INFO;
899 else if (!strcmp(opt->long_name, "batch-command"))
900 bo->batch_mode = BATCH_MODE_QUEUE_AND_DISPATCH;
901 else
902 BUG("%s given to batch-option-callback", opt->long_name);
903
904 bo->format = arg;
905
906 return 0;
907 }
908
909 int cmd_cat_file(int argc, const char **argv, const char *prefix)
910 {
911 int opt = 0;
912 int opt_cw = 0;
913 int opt_epts = 0;
914 const char *exp_type = NULL, *obj_name = NULL;
915 struct batch_options batch = {0};
916 int unknown_type = 0;
917
918 const char * const usage[] = {
919 N_("git cat-file <type> <object>"),
920 N_("git cat-file (-e | -p) <object>"),
921 N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"),
922 N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
923 " [--buffer] [--follow-symlinks] [--unordered]\n"
924 " [--textconv | --filters] [-z]"),
925 N_("git cat-file (--textconv | --filters)\n"
926 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"),
927 NULL
928 };
929 const struct option options[] = {
930 /* Simple queries */
931 OPT_GROUP(N_("Check object existence or emit object contents")),
932 OPT_CMDMODE('e', NULL, &opt,
933 N_("check if <object> exists"), 'e'),
934 OPT_CMDMODE('p', NULL, &opt, N_("pretty-print <object> content"), 'p'),
935
936 OPT_GROUP(N_("Emit [broken] object attributes")),
937 OPT_CMDMODE('t', NULL, &opt, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'),
938 OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
939 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
940 N_("allow -s and -t to work with broken/corrupt objects")),
941 OPT_BOOL(0, "use-mailmap", &use_mailmap, N_("use mail map file")),
942 OPT_ALIAS(0, "mailmap", "use-mailmap"),
943 /* Batch mode */
944 OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")),
945 OPT_CALLBACK_F(0, "batch", &batch, N_("format"),
946 N_("show full <object> or <rev> contents"),
947 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
948 batch_option_callback),
949 OPT_CALLBACK_F(0, "batch-check", &batch, N_("format"),
950 N_("like --batch, but don't emit <contents>"),
951 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
952 batch_option_callback),
953 OPT_BOOL('z', NULL, &batch.nul_terminated, N_("stdin is NUL-terminated")),
954 OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
955 N_("read commands from stdin"),
956 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
957 batch_option_callback),
958 OPT_CMDMODE(0, "batch-all-objects", &opt,
959 N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'),
960 /* Batch-specific options */
961 OPT_GROUP(N_("Change or optimize batch output")),
962 OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
963 OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
964 N_("follow in-tree symlinks")),
965 OPT_BOOL(0, "unordered", &batch.unordered,
966 N_("do not order objects before emitting them")),
967 /* Textconv options, stand-ole*/
968 OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")),
969 OPT_CMDMODE(0, "textconv", &opt,
970 N_("run textconv on object's content"), 'c'),
971 OPT_CMDMODE(0, "filters", &opt,
972 N_("run filters on object's content"), 'w'),
973 OPT_STRING(0, "path", &force_path, N_("blob|tree"),
974 N_("use a <path> for (--textconv | --filters); Not with 'batch'")),
975 OPT_END()
976 };
977
978 git_config(git_cat_file_config, NULL);
979
980 batch.buffer_output = -1;
981
982 argc = parse_options(argc, argv, prefix, options, usage, 0);
983 opt_cw = (opt == 'c' || opt == 'w');
984 opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's');
985
986 if (use_mailmap)
987 read_mailmap(&mailmap);
988
989 /* --batch-all-objects? */
990 if (opt == 'b')
991 batch.all_objects = 1;
992
993 /* Option compatibility */
994 if (force_path && !opt_cw)
995 usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
996 usage, options,
997 "--path", _("path|tree-ish"), "--filters",
998 "--textconv");
999
1000 /* Option compatibility with batch mode */
1001 if (batch.enabled)
1002 ;
1003 else if (batch.follow_symlinks)
1004 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1005 "--follow-symlinks");
1006 else if (batch.buffer_output >= 0)
1007 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1008 "--buffer");
1009 else if (batch.all_objects)
1010 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1011 "--batch-all-objects");
1012 else if (batch.nul_terminated)
1013 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1014 "-z");
1015
1016 /* Batch defaults */
1017 if (batch.buffer_output < 0)
1018 batch.buffer_output = batch.all_objects;
1019
1020 /* Return early if we're in batch mode? */
1021 if (batch.enabled) {
1022 if (opt_cw)
1023 batch.transform_mode = opt;
1024 else if (opt && opt != 'b')
1025 usage_msg_optf(_("'-%c' is incompatible with batch mode"),
1026 usage, options, opt);
1027 else if (argc)
1028 usage_msg_opt(_("batch modes take no arguments"), usage,
1029 options);
1030
1031 return batch_objects(&batch);
1032 }
1033
1034 if (opt) {
1035 if (!argc && opt == 'c')
1036 usage_msg_optf(_("<rev> required with '%s'"),
1037 usage, options, "--textconv");
1038 else if (!argc && opt == 'w')
1039 usage_msg_optf(_("<rev> required with '%s'"),
1040 usage, options, "--filters");
1041 else if (!argc && opt_epts)
1042 usage_msg_optf(_("<object> required with '-%c'"),
1043 usage, options, opt);
1044 else if (argc == 1)
1045 obj_name = argv[0];
1046 else
1047 usage_msg_opt(_("too many arguments"), usage, options);
1048 } else if (!argc) {
1049 usage_with_options(usage, options);
1050 } else if (argc != 2) {
1051 usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"),
1052 usage, options, argc);
1053 } else if (argc) {
1054 exp_type = argv[0];
1055 obj_name = argv[1];
1056 }
1057
1058 if (unknown_type && opt != 't' && opt != 's')
1059 die("git cat-file --allow-unknown-type: use with -s or -t");
1060 return cat_one_file(opt, exp_type, obj_name, unknown_type);
1061 }