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