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