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