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