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