test-lib: add a GIT_TEST_PASSING_SANITIZE_LEAK=check mode
[git.git] / builtin / cat-file.c
blobf42782e955f35bdfa9a78c217337863364bce1d5
1 /*
2 * GIT - The information manager from hell
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"
20 enum batch_mode {
21 BATCH_MODE_CONTENTS,
22 BATCH_MODE_INFO,
23 BATCH_MODE_QUEUE_AND_DISPATCH,
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;
37 static const char *force_path;
39 static int filter_object(const char *path, unsigned mode,
40 const struct object_id *oid,
41 char **buf, unsigned long *size)
43 enum object_type type;
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;
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);
61 return 0;
64 static int stream_blob(const struct object_id *oid)
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;
71 static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
72 int unknown_type)
74 int ret;
75 struct object_id oid;
76 enum object_type type;
77 char *buf;
78 unsigned long size;
79 struct object_context obj_context;
80 struct object_info oi = OBJECT_INFO_INIT;
81 struct strbuf sb = STRBUF_INIT;
82 unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
83 unsigned get_oid_flags = GET_OID_RECORD_PATH | GET_OID_ONLY_TO_DIE;
84 const char *path = force_path;
85 const int opt_cw = (opt == 'c' || opt == 'w');
86 if (!path && opt_cw)
87 get_oid_flags |= GET_OID_REQUIRE_PATH;
89 if (unknown_type)
90 flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
92 if (get_oid_with_context(the_repository, obj_name, get_oid_flags, &oid,
93 &obj_context))
94 die("Not a valid object name %s", obj_name);
96 if (!path)
97 path = obj_context.path;
98 if (obj_context.mode == S_IFINVALID)
99 obj_context.mode = 0100644;
101 buf = NULL;
102 switch (opt) {
103 case 't':
104 oi.type_name = &sb;
105 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
106 die("git cat-file: could not get object info");
107 if (sb.len) {
108 printf("%s\n", sb.buf);
109 strbuf_release(&sb);
110 ret = 0;
111 goto cleanup;
113 break;
115 case 's':
116 oi.sizep = &size;
117 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
118 die("git cat-file: could not get object info");
119 printf("%"PRIuMAX"\n", (uintmax_t)size);
120 ret = 0;
121 goto cleanup;
123 case 'e':
124 return !has_object_file(&oid);
126 case 'w':
128 if (filter_object(path, obj_context.mode,
129 &oid, &buf, &size)) {
130 ret = -1;
131 goto cleanup;
133 break;
135 case 'c':
136 if (textconv_object(the_repository, path, obj_context.mode,
137 &oid, 1, &buf, &size))
138 break;
139 /* else fallthrough */
141 case 'p':
142 type = oid_object_info(the_repository, &oid, NULL);
143 if (type < 0)
144 die("Not a valid object name %s", obj_name);
146 /* custom pretty-print here */
147 if (type == OBJ_TREE) {
148 const char *ls_args[3] = { NULL };
149 ls_args[0] = "ls-tree";
150 ls_args[1] = obj_name;
151 ret = cmd_ls_tree(2, ls_args, NULL);
152 goto cleanup;
155 if (type == OBJ_BLOB) {
156 ret = stream_blob(&oid);
157 goto cleanup;
159 buf = read_object_file(&oid, &type, &size);
160 if (!buf)
161 die("Cannot read object %s", obj_name);
163 /* otherwise just spit out the data */
164 break;
166 case 0:
168 enum object_type exp_type_id = type_from_string(exp_type);
170 if (exp_type_id == OBJ_BLOB) {
171 struct object_id blob_oid;
172 if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
173 char *buffer = read_object_file(&oid, &type,
174 &size);
175 const char *target;
176 if (!skip_prefix(buffer, "object ", &target) ||
177 get_oid_hex(target, &blob_oid))
178 die("%s not a valid tag", oid_to_hex(&oid));
179 free(buffer);
180 } else
181 oidcpy(&blob_oid, &oid);
183 if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB) {
184 ret = stream_blob(&blob_oid);
185 goto cleanup;
188 * we attempted to dereference a tag to a blob
189 * and failed; there may be new dereference
190 * mechanisms this code is not aware of.
191 * fall-back to the usual case.
194 buf = read_object_with_reference(the_repository, &oid,
195 exp_type_id, &size, NULL);
196 break;
198 default:
199 die("git cat-file: unknown option: %s", exp_type);
202 if (!buf)
203 die("git cat-file %s: bad file", obj_name);
205 write_or_die(1, buf, size);
206 ret = 0;
207 cleanup:
208 free(buf);
209 free(obj_context.path);
210 return ret;
213 struct expand_data {
214 struct object_id oid;
215 enum object_type type;
216 unsigned long size;
217 off_t disk_size;
218 const char *rest;
219 struct object_id delta_base_oid;
222 * If mark_query is true, we do not expand anything, but rather
223 * just mark the object_info with items we wish to query.
225 int mark_query;
228 * Whether to split the input on whitespace before feeding it to
229 * get_sha1; this is decided during the mark_query phase based on
230 * whether we have a %(rest) token in our format.
232 int split_on_whitespace;
235 * After a mark_query run, this object_info is set up to be
236 * passed to oid_object_info_extended. It will point to the data
237 * elements above, so you can retrieve the response from there.
239 struct object_info info;
242 * This flag will be true if the requested batch format and options
243 * don't require us to call oid_object_info, which can then be
244 * optimized out.
246 unsigned skip_object_info : 1;
249 static int is_atom(const char *atom, const char *s, int slen)
251 int alen = strlen(atom);
252 return alen == slen && !memcmp(atom, s, alen);
255 static void expand_atom(struct strbuf *sb, const char *atom, int len,
256 void *vdata)
258 struct expand_data *data = vdata;
260 if (is_atom("objectname", atom, len)) {
261 if (!data->mark_query)
262 strbuf_addstr(sb, oid_to_hex(&data->oid));
263 } else if (is_atom("objecttype", atom, len)) {
264 if (data->mark_query)
265 data->info.typep = &data->type;
266 else
267 strbuf_addstr(sb, type_name(data->type));
268 } else if (is_atom("objectsize", atom, len)) {
269 if (data->mark_query)
270 data->info.sizep = &data->size;
271 else
272 strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
273 } else if (is_atom("objectsize:disk", atom, len)) {
274 if (data->mark_query)
275 data->info.disk_sizep = &data->disk_size;
276 else
277 strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
278 } else if (is_atom("rest", atom, len)) {
279 if (data->mark_query)
280 data->split_on_whitespace = 1;
281 else if (data->rest)
282 strbuf_addstr(sb, data->rest);
283 } else if (is_atom("deltabase", atom, len)) {
284 if (data->mark_query)
285 data->info.delta_base_oid = &data->delta_base_oid;
286 else
287 strbuf_addstr(sb,
288 oid_to_hex(&data->delta_base_oid));
289 } else
290 die("unknown format element: %.*s", len, atom);
293 static size_t expand_format(struct strbuf *sb, const char *start, void *data)
295 const char *end;
297 if (*start != '(')
298 return 0;
299 end = strchr(start + 1, ')');
300 if (!end)
301 die("format element '%s' does not end in ')'", start);
303 expand_atom(sb, start + 1, end - start - 1, data);
305 return end - start + 1;
308 static void batch_write(struct batch_options *opt, const void *data, int len)
310 if (opt->buffer_output) {
311 if (fwrite(data, 1, len, stdout) != len)
312 die_errno("unable to write to stdout");
313 } else
314 write_or_die(1, data, len);
317 static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
319 const struct object_id *oid = &data->oid;
321 assert(data->info.typep);
323 if (data->type == OBJ_BLOB) {
324 if (opt->buffer_output)
325 fflush(stdout);
326 if (opt->transform_mode) {
327 char *contents;
328 unsigned long size;
330 if (!data->rest)
331 die("missing path for '%s'", oid_to_hex(oid));
333 if (opt->transform_mode == 'w') {
334 if (filter_object(data->rest, 0100644, oid,
335 &contents, &size))
336 die("could not convert '%s' %s",
337 oid_to_hex(oid), data->rest);
338 } else if (opt->transform_mode == 'c') {
339 enum object_type type;
340 if (!textconv_object(the_repository,
341 data->rest, 0100644, oid,
342 1, &contents, &size))
343 contents = read_object_file(oid,
344 &type,
345 &size);
346 if (!contents)
347 die("could not convert '%s' %s",
348 oid_to_hex(oid), data->rest);
349 } else
350 BUG("invalid transform_mode: %c", opt->transform_mode);
351 batch_write(opt, contents, size);
352 free(contents);
353 } else {
354 stream_blob(oid);
357 else {
358 enum object_type type;
359 unsigned long size;
360 void *contents;
362 contents = read_object_file(oid, &type, &size);
363 if (!contents)
364 die("object %s disappeared", oid_to_hex(oid));
365 if (type != data->type)
366 die("object %s changed type!?", oid_to_hex(oid));
367 if (data->info.sizep && size != data->size)
368 die("object %s changed size!?", oid_to_hex(oid));
370 batch_write(opt, contents, size);
371 free(contents);
375 static void print_default_format(struct strbuf *scratch, struct expand_data *data)
377 strbuf_addf(scratch, "%s %s %"PRIuMAX"\n", oid_to_hex(&data->oid),
378 type_name(data->type),
379 (uintmax_t)data->size);
383 * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
384 * which the object may be accessed (though note that we may also rely on
385 * data->oid, too). If "pack" is NULL, then offset is ignored.
387 static void batch_object_write(const char *obj_name,
388 struct strbuf *scratch,
389 struct batch_options *opt,
390 struct expand_data *data,
391 struct packed_git *pack,
392 off_t offset)
394 if (!data->skip_object_info) {
395 int ret;
397 if (pack)
398 ret = packed_object_info(the_repository, pack, offset,
399 &data->info);
400 else
401 ret = oid_object_info_extended(the_repository,
402 &data->oid, &data->info,
403 OBJECT_INFO_LOOKUP_REPLACE);
404 if (ret < 0) {
405 printf("%s missing\n",
406 obj_name ? obj_name : oid_to_hex(&data->oid));
407 fflush(stdout);
408 return;
412 strbuf_reset(scratch);
414 if (!opt->format) {
415 print_default_format(scratch, data);
416 } else {
417 strbuf_expand(scratch, opt->format, expand_format, data);
418 strbuf_addch(scratch, '\n');
421 batch_write(opt, scratch->buf, scratch->len);
423 if (opt->batch_mode == BATCH_MODE_CONTENTS) {
424 print_object_or_die(opt, data);
425 batch_write(opt, "\n", 1);
429 static void batch_one_object(const char *obj_name,
430 struct strbuf *scratch,
431 struct batch_options *opt,
432 struct expand_data *data)
434 struct object_context ctx;
435 int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
436 enum get_oid_result result;
438 result = get_oid_with_context(the_repository, obj_name,
439 flags, &data->oid, &ctx);
440 if (result != FOUND) {
441 switch (result) {
442 case MISSING_OBJECT:
443 printf("%s missing\n", obj_name);
444 break;
445 case SHORT_NAME_AMBIGUOUS:
446 printf("%s ambiguous\n", obj_name);
447 break;
448 case DANGLING_SYMLINK:
449 printf("dangling %"PRIuMAX"\n%s\n",
450 (uintmax_t)strlen(obj_name), obj_name);
451 break;
452 case SYMLINK_LOOP:
453 printf("loop %"PRIuMAX"\n%s\n",
454 (uintmax_t)strlen(obj_name), obj_name);
455 break;
456 case NOT_DIR:
457 printf("notdir %"PRIuMAX"\n%s\n",
458 (uintmax_t)strlen(obj_name), obj_name);
459 break;
460 default:
461 BUG("unknown get_sha1_with_context result %d\n",
462 result);
463 break;
465 fflush(stdout);
466 return;
469 if (ctx.mode == 0) {
470 printf("symlink %"PRIuMAX"\n%s\n",
471 (uintmax_t)ctx.symlink_path.len,
472 ctx.symlink_path.buf);
473 fflush(stdout);
474 return;
477 batch_object_write(obj_name, scratch, opt, data, NULL, 0);
480 struct object_cb_data {
481 struct batch_options *opt;
482 struct expand_data *expand;
483 struct oidset *seen;
484 struct strbuf *scratch;
487 static int batch_object_cb(const struct object_id *oid, void *vdata)
489 struct object_cb_data *data = vdata;
490 oidcpy(&data->expand->oid, oid);
491 batch_object_write(NULL, data->scratch, data->opt, data->expand,
492 NULL, 0);
493 return 0;
496 static int collect_loose_object(const struct object_id *oid,
497 const char *path,
498 void *data)
500 oid_array_append(data, oid);
501 return 0;
504 static int collect_packed_object(const struct object_id *oid,
505 struct packed_git *pack,
506 uint32_t pos,
507 void *data)
509 oid_array_append(data, oid);
510 return 0;
513 static int batch_unordered_object(const struct object_id *oid,
514 struct packed_git *pack, off_t offset,
515 void *vdata)
517 struct object_cb_data *data = vdata;
519 if (oidset_insert(data->seen, oid))
520 return 0;
522 oidcpy(&data->expand->oid, oid);
523 batch_object_write(NULL, data->scratch, data->opt, data->expand,
524 pack, offset);
525 return 0;
528 static int batch_unordered_loose(const struct object_id *oid,
529 const char *path,
530 void *data)
532 return batch_unordered_object(oid, NULL, 0, data);
535 static int batch_unordered_packed(const struct object_id *oid,
536 struct packed_git *pack,
537 uint32_t pos,
538 void *data)
540 return batch_unordered_object(oid, pack,
541 nth_packed_object_offset(pack, pos),
542 data);
545 typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *,
546 struct strbuf *, struct expand_data *);
548 struct queued_cmd {
549 parse_cmd_fn_t fn;
550 char *line;
553 static void parse_cmd_contents(struct batch_options *opt,
554 const char *line,
555 struct strbuf *output,
556 struct expand_data *data)
558 opt->batch_mode = BATCH_MODE_CONTENTS;
559 batch_one_object(line, output, opt, data);
562 static void parse_cmd_info(struct batch_options *opt,
563 const char *line,
564 struct strbuf *output,
565 struct expand_data *data)
567 opt->batch_mode = BATCH_MODE_INFO;
568 batch_one_object(line, output, opt, data);
571 static void dispatch_calls(struct batch_options *opt,
572 struct strbuf *output,
573 struct expand_data *data,
574 struct queued_cmd *cmd,
575 int nr)
577 int i;
579 if (!opt->buffer_output)
580 die(_("flush is only for --buffer mode"));
582 for (i = 0; i < nr; i++)
583 cmd[i].fn(opt, cmd[i].line, output, data);
585 fflush(stdout);
588 static void free_cmds(struct queued_cmd *cmd, size_t *nr)
590 size_t i;
592 for (i = 0; i < *nr; i++)
593 FREE_AND_NULL(cmd[i].line);
595 *nr = 0;
599 static const struct parse_cmd {
600 const char *name;
601 parse_cmd_fn_t fn;
602 unsigned takes_args;
603 } commands[] = {
604 { "contents", parse_cmd_contents, 1},
605 { "info", parse_cmd_info, 1},
606 { "flush", NULL, 0},
609 static void batch_objects_command(struct batch_options *opt,
610 struct strbuf *output,
611 struct expand_data *data)
613 struct strbuf input = STRBUF_INIT;
614 struct queued_cmd *queued_cmd = NULL;
615 size_t alloc = 0, nr = 0;
617 while (!strbuf_getline(&input, stdin)) {
618 int i;
619 const struct parse_cmd *cmd = NULL;
620 const char *p = NULL, *cmd_end;
621 struct queued_cmd call = {0};
623 if (!input.len)
624 die(_("empty command in input"));
625 if (isspace(*input.buf))
626 die(_("whitespace before command: '%s'"), input.buf);
628 for (i = 0; i < ARRAY_SIZE(commands); i++) {
629 if (!skip_prefix(input.buf, commands[i].name, &cmd_end))
630 continue;
632 cmd = &commands[i];
633 if (cmd->takes_args) {
634 if (*cmd_end != ' ')
635 die(_("%s requires arguments"),
636 commands[i].name);
638 p = cmd_end + 1;
639 } else if (*cmd_end) {
640 die(_("%s takes no arguments"),
641 commands[i].name);
644 break;
647 if (!cmd)
648 die(_("unknown command: '%s'"), input.buf);
650 if (!strcmp(cmd->name, "flush")) {
651 dispatch_calls(opt, output, data, queued_cmd, nr);
652 free_cmds(queued_cmd, &nr);
653 } else if (!opt->buffer_output) {
654 cmd->fn(opt, p, output, data);
655 } else {
656 ALLOC_GROW(queued_cmd, nr + 1, alloc);
657 call.fn = cmd->fn;
658 call.line = xstrdup_or_null(p);
659 queued_cmd[nr++] = call;
663 if (opt->buffer_output &&
664 nr &&
665 !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) {
666 dispatch_calls(opt, output, data, queued_cmd, nr);
667 free_cmds(queued_cmd, &nr);
670 free_cmds(queued_cmd, &nr);
671 free(queued_cmd);
672 strbuf_release(&input);
675 #define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
677 static int batch_objects(struct batch_options *opt)
679 struct strbuf input = STRBUF_INIT;
680 struct strbuf output = STRBUF_INIT;
681 struct expand_data data;
682 int save_warning;
683 int retval = 0;
686 * Expand once with our special mark_query flag, which will prime the
687 * object_info to be handed to oid_object_info_extended for each
688 * object.
690 memset(&data, 0, sizeof(data));
691 data.mark_query = 1;
692 strbuf_expand(&output,
693 opt->format ? opt->format : DEFAULT_FORMAT,
694 expand_format,
695 &data);
696 data.mark_query = 0;
697 strbuf_release(&output);
698 if (opt->transform_mode)
699 data.split_on_whitespace = 1;
701 if (opt->format && !strcmp(opt->format, DEFAULT_FORMAT))
702 opt->format = NULL;
704 * If we are printing out the object, then always fill in the type,
705 * since we will want to decide whether or not to stream.
707 if (opt->batch_mode == BATCH_MODE_CONTENTS)
708 data.info.typep = &data.type;
710 if (opt->all_objects) {
711 struct object_cb_data cb;
712 struct object_info empty = OBJECT_INFO_INIT;
714 if (!memcmp(&data.info, &empty, sizeof(empty)))
715 data.skip_object_info = 1;
717 if (has_promisor_remote())
718 warning("This repository uses promisor remotes. Some objects may not be loaded.");
720 read_replace_refs = 0;
722 cb.opt = opt;
723 cb.expand = &data;
724 cb.scratch = &output;
726 if (opt->unordered) {
727 struct oidset seen = OIDSET_INIT;
729 cb.seen = &seen;
731 for_each_loose_object(batch_unordered_loose, &cb, 0);
732 for_each_packed_object(batch_unordered_packed, &cb,
733 FOR_EACH_OBJECT_PACK_ORDER);
735 oidset_clear(&seen);
736 } else {
737 struct oid_array sa = OID_ARRAY_INIT;
739 for_each_loose_object(collect_loose_object, &sa, 0);
740 for_each_packed_object(collect_packed_object, &sa, 0);
742 oid_array_for_each_unique(&sa, batch_object_cb, &cb);
744 oid_array_clear(&sa);
747 strbuf_release(&output);
748 return 0;
752 * We are going to call get_sha1 on a potentially very large number of
753 * objects. In most large cases, these will be actual object sha1s. The
754 * cost to double-check that each one is not also a ref (just so we can
755 * warn) ends up dwarfing the actual cost of the object lookups
756 * themselves. We can work around it by just turning off the warning.
758 save_warning = warn_on_object_refname_ambiguity;
759 warn_on_object_refname_ambiguity = 0;
761 if (opt->batch_mode == BATCH_MODE_QUEUE_AND_DISPATCH) {
762 batch_objects_command(opt, &output, &data);
763 goto cleanup;
766 while (strbuf_getline(&input, stdin) != EOF) {
767 if (data.split_on_whitespace) {
769 * Split at first whitespace, tying off the beginning
770 * of the string and saving the remainder (or NULL) in
771 * data.rest.
773 char *p = strpbrk(input.buf, " \t");
774 if (p) {
775 while (*p && strchr(" \t", *p))
776 *p++ = '\0';
778 data.rest = p;
781 batch_one_object(input.buf, &output, opt, &data);
784 cleanup:
785 strbuf_release(&input);
786 strbuf_release(&output);
787 warn_on_object_refname_ambiguity = save_warning;
788 return retval;
791 static int git_cat_file_config(const char *var, const char *value, void *cb)
793 if (userdiff_config(var, value) < 0)
794 return -1;
796 return git_default_config(var, value, cb);
799 static int batch_option_callback(const struct option *opt,
800 const char *arg,
801 int unset)
803 struct batch_options *bo = opt->value;
805 BUG_ON_OPT_NEG(unset);
807 if (bo->enabled) {
808 return error(_("only one batch option may be specified"));
811 bo->enabled = 1;
813 if (!strcmp(opt->long_name, "batch"))
814 bo->batch_mode = BATCH_MODE_CONTENTS;
815 else if (!strcmp(opt->long_name, "batch-check"))
816 bo->batch_mode = BATCH_MODE_INFO;
817 else if (!strcmp(opt->long_name, "batch-command"))
818 bo->batch_mode = BATCH_MODE_QUEUE_AND_DISPATCH;
819 else
820 BUG("%s given to batch-option-callback", opt->long_name);
822 bo->format = arg;
824 return 0;
827 int cmd_cat_file(int argc, const char **argv, const char *prefix)
829 int opt = 0;
830 int opt_cw = 0;
831 int opt_epts = 0;
832 const char *exp_type = NULL, *obj_name = NULL;
833 struct batch_options batch = {0};
834 int unknown_type = 0;
836 const char * const usage[] = {
837 N_("git cat-file <type> <object>"),
838 N_("git cat-file (-e | -p) <object>"),
839 N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"),
840 N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
841 " [--buffer] [--follow-symlinks] [--unordered]\n"
842 " [--textconv | --filters]"),
843 N_("git cat-file (--textconv | --filters)\n"
844 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"),
845 NULL
847 const struct option options[] = {
848 /* Simple queries */
849 OPT_GROUP(N_("Check object existence or emit object contents")),
850 OPT_CMDMODE('e', NULL, &opt,
851 N_("check if <object> exists"), 'e'),
852 OPT_CMDMODE('p', NULL, &opt, N_("pretty-print <object> content"), 'p'),
854 OPT_GROUP(N_("Emit [broken] object attributes")),
855 OPT_CMDMODE('t', NULL, &opt, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'),
856 OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
857 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
858 N_("allow -s and -t to work with broken/corrupt objects")),
859 /* Batch mode */
860 OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")),
861 OPT_CALLBACK_F(0, "batch", &batch, N_("format"),
862 N_("show full <object> or <rev> contents"),
863 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
864 batch_option_callback),
865 OPT_CALLBACK_F(0, "batch-check", &batch, N_("format"),
866 N_("like --batch, but don't emit <contents>"),
867 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
868 batch_option_callback),
869 OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
870 N_("read commands from stdin"),
871 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
872 batch_option_callback),
873 OPT_CMDMODE(0, "batch-all-objects", &opt,
874 N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'),
875 /* Batch-specific options */
876 OPT_GROUP(N_("Change or optimize batch output")),
877 OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
878 OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
879 N_("follow in-tree symlinks")),
880 OPT_BOOL(0, "unordered", &batch.unordered,
881 N_("do not order objects before emitting them")),
882 /* Textconv options, stand-ole*/
883 OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")),
884 OPT_CMDMODE(0, "textconv", &opt,
885 N_("run textconv on object's content"), 'c'),
886 OPT_CMDMODE(0, "filters", &opt,
887 N_("run filters on object's content"), 'w'),
888 OPT_STRING(0, "path", &force_path, N_("blob|tree"),
889 N_("use a <path> for (--textconv | --filters); Not with 'batch'")),
890 OPT_END()
893 git_config(git_cat_file_config, NULL);
895 batch.buffer_output = -1;
897 argc = parse_options(argc, argv, prefix, options, usage, 0);
898 opt_cw = (opt == 'c' || opt == 'w');
899 opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's');
901 /* --batch-all-objects? */
902 if (opt == 'b')
903 batch.all_objects = 1;
905 /* Option compatibility */
906 if (force_path && !opt_cw)
907 usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
908 usage, options,
909 "--path", _("path|tree-ish"), "--filters",
910 "--textconv");
912 /* Option compatibility with batch mode */
913 if (batch.enabled)
915 else if (batch.follow_symlinks)
916 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
917 "--follow-symlinks");
918 else if (batch.buffer_output >= 0)
919 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
920 "--buffer");
921 else if (batch.all_objects)
922 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
923 "--batch-all-objects");
925 /* Batch defaults */
926 if (batch.buffer_output < 0)
927 batch.buffer_output = batch.all_objects;
929 /* Return early if we're in batch mode? */
930 if (batch.enabled) {
931 if (opt_cw)
932 batch.transform_mode = opt;
933 else if (opt && opt != 'b')
934 usage_msg_optf(_("'-%c' is incompatible with batch mode"),
935 usage, options, opt);
936 else if (argc)
937 usage_msg_opt(_("batch modes take no arguments"), usage,
938 options);
940 return batch_objects(&batch);
943 if (opt) {
944 if (!argc && opt == 'c')
945 usage_msg_optf(_("<rev> required with '%s'"),
946 usage, options, "--textconv");
947 else if (!argc && opt == 'w')
948 usage_msg_optf(_("<rev> required with '%s'"),
949 usage, options, "--filters");
950 else if (!argc && opt_epts)
951 usage_msg_optf(_("<object> required with '-%c'"),
952 usage, options, opt);
953 else if (argc == 1)
954 obj_name = argv[0];
955 else
956 usage_msg_opt(_("too many arguments"), usage, options);
957 } else if (!argc) {
958 usage_with_options(usage, options);
959 } else if (argc != 2) {
960 usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"),
961 usage, options, argc);
962 } else if (argc) {
963 exp_type = argv[0];
964 obj_name = argv[1];
967 if (unknown_type && opt != 't' && opt != 's')
968 die("git cat-file --allow-unknown-type: use with -s or -t");
969 return cat_one_file(opt, exp_type, obj_name, unknown_type);