diff --no-index: fix -R with stdin
[git/debian.git] / builtin / cat-file.c
blob7ff56d5a7815e24e2a206b60e9370138bc876b7a
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 char input_delim;
46 char output_delim;
47 const char *format;
50 static const char *force_path;
52 static struct string_list mailmap = STRING_LIST_INIT_NODUP;
53 static int use_mailmap;
55 static char *replace_idents_using_mailmap(char *, size_t *);
57 static char *replace_idents_using_mailmap(char *object_buf, size_t *size)
59 struct strbuf sb = STRBUF_INIT;
60 const char *headers[] = { "author ", "committer ", "tagger ", NULL };
62 strbuf_attach(&sb, object_buf, *size, *size + 1);
63 apply_mailmap_to_header(&sb, headers, &mailmap);
64 *size = sb.len;
65 return strbuf_detach(&sb, NULL);
68 static int filter_object(const char *path, unsigned mode,
69 const struct object_id *oid,
70 char **buf, unsigned long *size)
72 enum object_type type;
74 *buf = repo_read_object_file(the_repository, oid, &type, size);
75 if (!*buf)
76 return error(_("cannot read object %s '%s'"),
77 oid_to_hex(oid), path);
78 if ((type == OBJ_BLOB) && S_ISREG(mode)) {
79 struct strbuf strbuf = STRBUF_INIT;
80 struct checkout_metadata meta;
82 init_checkout_metadata(&meta, NULL, NULL, oid);
83 if (convert_to_working_tree(&the_index, path, *buf, *size, &strbuf, &meta)) {
84 free(*buf);
85 *size = strbuf.len;
86 *buf = strbuf_detach(&strbuf, NULL);
90 return 0;
93 static int stream_blob(const struct object_id *oid)
95 if (stream_blob_to_fd(1, oid, NULL, 0))
96 die("unable to stream %s to stdout", oid_to_hex(oid));
97 return 0;
100 static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
101 int unknown_type)
103 int ret;
104 struct object_id oid;
105 enum object_type type;
106 char *buf;
107 unsigned long size;
108 struct object_context obj_context;
109 struct object_info oi = OBJECT_INFO_INIT;
110 struct strbuf sb = STRBUF_INIT;
111 unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
112 unsigned get_oid_flags = GET_OID_RECORD_PATH | GET_OID_ONLY_TO_DIE;
113 const char *path = force_path;
114 const int opt_cw = (opt == 'c' || opt == 'w');
115 if (!path && opt_cw)
116 get_oid_flags |= GET_OID_REQUIRE_PATH;
118 if (unknown_type)
119 flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
121 if (get_oid_with_context(the_repository, obj_name, get_oid_flags, &oid,
122 &obj_context))
123 die("Not a valid object name %s", obj_name);
125 if (!path)
126 path = obj_context.path;
127 if (obj_context.mode == S_IFINVALID)
128 obj_context.mode = 0100644;
130 buf = NULL;
131 switch (opt) {
132 case 't':
133 oi.type_name = &sb;
134 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
135 die("git cat-file: could not get object info");
136 if (sb.len) {
137 printf("%s\n", sb.buf);
138 strbuf_release(&sb);
139 ret = 0;
140 goto cleanup;
142 break;
144 case 's':
145 oi.sizep = &size;
147 if (use_mailmap) {
148 oi.typep = &type;
149 oi.contentp = (void**)&buf;
152 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
153 die("git cat-file: could not get object info");
155 if (use_mailmap && (type == OBJ_COMMIT || type == OBJ_TAG)) {
156 size_t s = size;
157 buf = replace_idents_using_mailmap(buf, &s);
158 size = cast_size_t_to_ulong(s);
161 printf("%"PRIuMAX"\n", (uintmax_t)size);
162 ret = 0;
163 goto cleanup;
165 case 'e':
166 return !repo_has_object_file(the_repository, &oid);
168 case 'w':
170 if (filter_object(path, obj_context.mode,
171 &oid, &buf, &size)) {
172 ret = -1;
173 goto cleanup;
175 break;
177 case 'c':
178 if (textconv_object(the_repository, path, obj_context.mode,
179 &oid, 1, &buf, &size))
180 break;
181 /* else fallthrough */
183 case 'p':
184 type = oid_object_info(the_repository, &oid, NULL);
185 if (type < 0)
186 die("Not a valid object name %s", obj_name);
188 /* custom pretty-print here */
189 if (type == OBJ_TREE) {
190 const char *ls_args[3] = { NULL };
191 ls_args[0] = "ls-tree";
192 ls_args[1] = obj_name;
193 ret = cmd_ls_tree(2, ls_args, NULL);
194 goto cleanup;
197 if (type == OBJ_BLOB) {
198 ret = stream_blob(&oid);
199 goto cleanup;
201 buf = repo_read_object_file(the_repository, &oid, &type,
202 &size);
203 if (!buf)
204 die("Cannot read object %s", obj_name);
206 if (use_mailmap) {
207 size_t s = size;
208 buf = replace_idents_using_mailmap(buf, &s);
209 size = cast_size_t_to_ulong(s);
212 /* otherwise just spit out the data */
213 break;
215 case 0:
217 enum object_type exp_type_id = type_from_string(exp_type);
219 if (exp_type_id == OBJ_BLOB) {
220 struct object_id blob_oid;
221 if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
222 char *buffer = repo_read_object_file(the_repository,
223 &oid,
224 &type,
225 &size);
226 const char *target;
227 if (!skip_prefix(buffer, "object ", &target) ||
228 get_oid_hex(target, &blob_oid))
229 die("%s not a valid tag", oid_to_hex(&oid));
230 free(buffer);
231 } else
232 oidcpy(&blob_oid, &oid);
234 if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB) {
235 ret = stream_blob(&blob_oid);
236 goto cleanup;
239 * we attempted to dereference a tag to a blob
240 * and failed; there may be new dereference
241 * mechanisms this code is not aware of.
242 * fall-back to the usual case.
245 buf = read_object_with_reference(the_repository, &oid,
246 exp_type_id, &size, NULL);
248 if (use_mailmap) {
249 size_t s = size;
250 buf = replace_idents_using_mailmap(buf, &s);
251 size = cast_size_t_to_ulong(s);
253 break;
255 default:
256 die("git cat-file: unknown option: %s", exp_type);
259 if (!buf)
260 die("git cat-file %s: bad file", obj_name);
262 write_or_die(1, buf, size);
263 ret = 0;
264 cleanup:
265 free(buf);
266 free(obj_context.path);
267 return ret;
270 struct expand_data {
271 struct object_id oid;
272 enum object_type type;
273 unsigned long size;
274 off_t disk_size;
275 const char *rest;
276 struct object_id delta_base_oid;
279 * If mark_query is true, we do not expand anything, but rather
280 * just mark the object_info with items we wish to query.
282 int mark_query;
285 * Whether to split the input on whitespace before feeding it to
286 * get_sha1; this is decided during the mark_query phase based on
287 * whether we have a %(rest) token in our format.
289 int split_on_whitespace;
292 * After a mark_query run, this object_info is set up to be
293 * passed to oid_object_info_extended. It will point to the data
294 * elements above, so you can retrieve the response from there.
296 struct object_info info;
299 * This flag will be true if the requested batch format and options
300 * don't require us to call oid_object_info, which can then be
301 * optimized out.
303 unsigned skip_object_info : 1;
306 static int is_atom(const char *atom, const char *s, int slen)
308 int alen = strlen(atom);
309 return alen == slen && !memcmp(atom, s, alen);
312 static void expand_atom(struct strbuf *sb, const char *atom, int len,
313 void *vdata)
315 struct expand_data *data = vdata;
317 if (is_atom("objectname", atom, len)) {
318 if (!data->mark_query)
319 strbuf_addstr(sb, oid_to_hex(&data->oid));
320 } else if (is_atom("objecttype", atom, len)) {
321 if (data->mark_query)
322 data->info.typep = &data->type;
323 else
324 strbuf_addstr(sb, type_name(data->type));
325 } else if (is_atom("objectsize", atom, len)) {
326 if (data->mark_query)
327 data->info.sizep = &data->size;
328 else
329 strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
330 } else if (is_atom("objectsize:disk", atom, len)) {
331 if (data->mark_query)
332 data->info.disk_sizep = &data->disk_size;
333 else
334 strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
335 } else if (is_atom("rest", atom, len)) {
336 if (data->mark_query)
337 data->split_on_whitespace = 1;
338 else if (data->rest)
339 strbuf_addstr(sb, data->rest);
340 } else if (is_atom("deltabase", atom, len)) {
341 if (data->mark_query)
342 data->info.delta_base_oid = &data->delta_base_oid;
343 else
344 strbuf_addstr(sb,
345 oid_to_hex(&data->delta_base_oid));
346 } else
347 die("unknown format element: %.*s", len, atom);
350 static size_t expand_format(struct strbuf *sb, const char *start, void *data)
352 const char *end;
354 if (*start != '(')
355 return 0;
356 end = strchr(start + 1, ')');
357 if (!end)
358 die("format element '%s' does not end in ')'", start);
360 expand_atom(sb, start + 1, end - start - 1, data);
362 return end - start + 1;
365 static void batch_write(struct batch_options *opt, const void *data, int len)
367 if (opt->buffer_output) {
368 if (fwrite(data, 1, len, stdout) != len)
369 die_errno("unable to write to stdout");
370 } else
371 write_or_die(1, data, len);
374 static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
376 const struct object_id *oid = &data->oid;
378 assert(data->info.typep);
380 if (data->type == OBJ_BLOB) {
381 if (opt->buffer_output)
382 fflush(stdout);
383 if (opt->transform_mode) {
384 char *contents;
385 unsigned long size;
387 if (!data->rest)
388 die("missing path for '%s'", oid_to_hex(oid));
390 if (opt->transform_mode == 'w') {
391 if (filter_object(data->rest, 0100644, oid,
392 &contents, &size))
393 die("could not convert '%s' %s",
394 oid_to_hex(oid), data->rest);
395 } else if (opt->transform_mode == 'c') {
396 enum object_type type;
397 if (!textconv_object(the_repository,
398 data->rest, 0100644, oid,
399 1, &contents, &size))
400 contents = repo_read_object_file(the_repository,
401 oid,
402 &type,
403 &size);
404 if (!contents)
405 die("could not convert '%s' %s",
406 oid_to_hex(oid), data->rest);
407 } else
408 BUG("invalid transform_mode: %c", opt->transform_mode);
409 batch_write(opt, contents, size);
410 free(contents);
411 } else {
412 stream_blob(oid);
415 else {
416 enum object_type type;
417 unsigned long size;
418 void *contents;
420 contents = repo_read_object_file(the_repository, oid, &type,
421 &size);
423 if (use_mailmap) {
424 size_t s = size;
425 contents = replace_idents_using_mailmap(contents, &s);
426 size = cast_size_t_to_ulong(s);
429 if (!contents)
430 die("object %s disappeared", oid_to_hex(oid));
431 if (type != data->type)
432 die("object %s changed type!?", oid_to_hex(oid));
433 if (data->info.sizep && size != data->size && !use_mailmap)
434 die("object %s changed size!?", oid_to_hex(oid));
436 batch_write(opt, contents, size);
437 free(contents);
441 static void print_default_format(struct strbuf *scratch, struct expand_data *data,
442 struct batch_options *opt)
444 strbuf_addf(scratch, "%s %s %"PRIuMAX"%c", oid_to_hex(&data->oid),
445 type_name(data->type),
446 (uintmax_t)data->size, opt->output_delim);
450 * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
451 * which the object may be accessed (though note that we may also rely on
452 * data->oid, too). If "pack" is NULL, then offset is ignored.
454 static void batch_object_write(const char *obj_name,
455 struct strbuf *scratch,
456 struct batch_options *opt,
457 struct expand_data *data,
458 struct packed_git *pack,
459 off_t offset)
461 if (!data->skip_object_info) {
462 int ret;
464 if (use_mailmap)
465 data->info.typep = &data->type;
467 if (pack)
468 ret = packed_object_info(the_repository, pack, offset,
469 &data->info);
470 else
471 ret = oid_object_info_extended(the_repository,
472 &data->oid, &data->info,
473 OBJECT_INFO_LOOKUP_REPLACE);
474 if (ret < 0) {
475 printf("%s missing%c",
476 obj_name ? obj_name : oid_to_hex(&data->oid), opt->output_delim);
477 fflush(stdout);
478 return;
481 if (use_mailmap && (data->type == OBJ_COMMIT || data->type == OBJ_TAG)) {
482 size_t s = data->size;
483 char *buf = NULL;
485 buf = repo_read_object_file(the_repository, &data->oid, &data->type,
486 &data->size);
487 buf = replace_idents_using_mailmap(buf, &s);
488 data->size = cast_size_t_to_ulong(s);
490 free(buf);
494 strbuf_reset(scratch);
496 if (!opt->format) {
497 print_default_format(scratch, data, opt);
498 } else {
499 strbuf_expand(scratch, opt->format, expand_format, data);
500 strbuf_addch(scratch, opt->output_delim);
503 batch_write(opt, scratch->buf, scratch->len);
505 if (opt->batch_mode == BATCH_MODE_CONTENTS) {
506 print_object_or_die(opt, data);
507 batch_write(opt, &opt->output_delim, 1);
511 static void batch_one_object(const char *obj_name,
512 struct strbuf *scratch,
513 struct batch_options *opt,
514 struct expand_data *data)
516 struct object_context ctx;
517 int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
518 enum get_oid_result result;
520 result = get_oid_with_context(the_repository, obj_name,
521 flags, &data->oid, &ctx);
522 if (result != FOUND) {
523 switch (result) {
524 case MISSING_OBJECT:
525 printf("%s missing%c", obj_name, opt->output_delim);
526 break;
527 case SHORT_NAME_AMBIGUOUS:
528 printf("%s ambiguous%c", obj_name, opt->output_delim);
529 break;
530 case DANGLING_SYMLINK:
531 printf("dangling %"PRIuMAX"%c%s%c",
532 (uintmax_t)strlen(obj_name),
533 opt->output_delim, obj_name, opt->output_delim);
534 break;
535 case SYMLINK_LOOP:
536 printf("loop %"PRIuMAX"%c%s%c",
537 (uintmax_t)strlen(obj_name),
538 opt->output_delim, obj_name, opt->output_delim);
539 break;
540 case NOT_DIR:
541 printf("notdir %"PRIuMAX"%c%s%c",
542 (uintmax_t)strlen(obj_name),
543 opt->output_delim, obj_name, opt->output_delim);
544 break;
545 default:
546 BUG("unknown get_sha1_with_context result %d\n",
547 result);
548 break;
550 fflush(stdout);
551 return;
554 if (ctx.mode == 0) {
555 printf("symlink %"PRIuMAX"%c%s%c",
556 (uintmax_t)ctx.symlink_path.len,
557 opt->output_delim, ctx.symlink_path.buf, opt->output_delim);
558 fflush(stdout);
559 return;
562 batch_object_write(obj_name, scratch, opt, data, NULL, 0);
565 struct object_cb_data {
566 struct batch_options *opt;
567 struct expand_data *expand;
568 struct oidset *seen;
569 struct strbuf *scratch;
572 static int batch_object_cb(const struct object_id *oid, void *vdata)
574 struct object_cb_data *data = vdata;
575 oidcpy(&data->expand->oid, oid);
576 batch_object_write(NULL, data->scratch, data->opt, data->expand,
577 NULL, 0);
578 return 0;
581 static int collect_loose_object(const struct object_id *oid,
582 const char *path UNUSED,
583 void *data)
585 oid_array_append(data, oid);
586 return 0;
589 static int collect_packed_object(const struct object_id *oid,
590 struct packed_git *pack UNUSED,
591 uint32_t pos UNUSED,
592 void *data)
594 oid_array_append(data, oid);
595 return 0;
598 static int batch_unordered_object(const struct object_id *oid,
599 struct packed_git *pack, off_t offset,
600 void *vdata)
602 struct object_cb_data *data = vdata;
604 if (oidset_insert(data->seen, oid))
605 return 0;
607 oidcpy(&data->expand->oid, oid);
608 batch_object_write(NULL, data->scratch, data->opt, data->expand,
609 pack, offset);
610 return 0;
613 static int batch_unordered_loose(const struct object_id *oid,
614 const char *path UNUSED,
615 void *data)
617 return batch_unordered_object(oid, NULL, 0, data);
620 static int batch_unordered_packed(const struct object_id *oid,
621 struct packed_git *pack,
622 uint32_t pos,
623 void *data)
625 return batch_unordered_object(oid, pack,
626 nth_packed_object_offset(pack, pos),
627 data);
630 typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *,
631 struct strbuf *, struct expand_data *);
633 struct queued_cmd {
634 parse_cmd_fn_t fn;
635 char *line;
638 static void parse_cmd_contents(struct batch_options *opt,
639 const char *line,
640 struct strbuf *output,
641 struct expand_data *data)
643 opt->batch_mode = BATCH_MODE_CONTENTS;
644 batch_one_object(line, output, opt, data);
647 static void parse_cmd_info(struct batch_options *opt,
648 const char *line,
649 struct strbuf *output,
650 struct expand_data *data)
652 opt->batch_mode = BATCH_MODE_INFO;
653 batch_one_object(line, output, opt, data);
656 static void dispatch_calls(struct batch_options *opt,
657 struct strbuf *output,
658 struct expand_data *data,
659 struct queued_cmd *cmd,
660 int nr)
662 int i;
664 if (!opt->buffer_output)
665 die(_("flush is only for --buffer mode"));
667 for (i = 0; i < nr; i++)
668 cmd[i].fn(opt, cmd[i].line, output, data);
670 fflush(stdout);
673 static void free_cmds(struct queued_cmd *cmd, size_t *nr)
675 size_t i;
677 for (i = 0; i < *nr; i++)
678 FREE_AND_NULL(cmd[i].line);
680 *nr = 0;
684 static const struct parse_cmd {
685 const char *name;
686 parse_cmd_fn_t fn;
687 unsigned takes_args;
688 } commands[] = {
689 { "contents", parse_cmd_contents, 1},
690 { "info", parse_cmd_info, 1},
691 { "flush", NULL, 0},
694 static void batch_objects_command(struct batch_options *opt,
695 struct strbuf *output,
696 struct expand_data *data)
698 struct strbuf input = STRBUF_INIT;
699 struct queued_cmd *queued_cmd = NULL;
700 size_t alloc = 0, nr = 0;
702 while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
703 int i;
704 const struct parse_cmd *cmd = NULL;
705 const char *p = NULL, *cmd_end;
706 struct queued_cmd call = {0};
708 if (!input.len)
709 die(_("empty command in input"));
710 if (isspace(*input.buf))
711 die(_("whitespace before command: '%s'"), input.buf);
713 for (i = 0; i < ARRAY_SIZE(commands); i++) {
714 if (!skip_prefix(input.buf, commands[i].name, &cmd_end))
715 continue;
717 cmd = &commands[i];
718 if (cmd->takes_args) {
719 if (*cmd_end != ' ')
720 die(_("%s requires arguments"),
721 commands[i].name);
723 p = cmd_end + 1;
724 } else if (*cmd_end) {
725 die(_("%s takes no arguments"),
726 commands[i].name);
729 break;
732 if (!cmd)
733 die(_("unknown command: '%s'"), input.buf);
735 if (!strcmp(cmd->name, "flush")) {
736 dispatch_calls(opt, output, data, queued_cmd, nr);
737 free_cmds(queued_cmd, &nr);
738 } else if (!opt->buffer_output) {
739 cmd->fn(opt, p, output, data);
740 } else {
741 ALLOC_GROW(queued_cmd, nr + 1, alloc);
742 call.fn = cmd->fn;
743 call.line = xstrdup_or_null(p);
744 queued_cmd[nr++] = call;
748 if (opt->buffer_output &&
749 nr &&
750 !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) {
751 dispatch_calls(opt, output, data, queued_cmd, nr);
752 free_cmds(queued_cmd, &nr);
755 free_cmds(queued_cmd, &nr);
756 free(queued_cmd);
757 strbuf_release(&input);
760 #define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
762 static int batch_objects(struct batch_options *opt)
764 struct strbuf input = STRBUF_INIT;
765 struct strbuf output = STRBUF_INIT;
766 struct expand_data data;
767 int save_warning;
768 int retval = 0;
771 * Expand once with our special mark_query flag, which will prime the
772 * object_info to be handed to oid_object_info_extended for each
773 * object.
775 memset(&data, 0, sizeof(data));
776 data.mark_query = 1;
777 strbuf_expand(&output,
778 opt->format ? opt->format : DEFAULT_FORMAT,
779 expand_format,
780 &data);
781 data.mark_query = 0;
782 strbuf_release(&output);
783 if (opt->transform_mode)
784 data.split_on_whitespace = 1;
786 if (opt->format && !strcmp(opt->format, DEFAULT_FORMAT))
787 opt->format = NULL;
789 * If we are printing out the object, then always fill in the type,
790 * since we will want to decide whether or not to stream.
792 if (opt->batch_mode == BATCH_MODE_CONTENTS)
793 data.info.typep = &data.type;
795 if (opt->all_objects) {
796 struct object_cb_data cb;
797 struct object_info empty = OBJECT_INFO_INIT;
799 if (!memcmp(&data.info, &empty, sizeof(empty)))
800 data.skip_object_info = 1;
802 if (repo_has_promisor_remote(the_repository))
803 warning("This repository uses promisor remotes. Some objects may not be loaded.");
805 disable_replace_refs();
807 cb.opt = opt;
808 cb.expand = &data;
809 cb.scratch = &output;
811 if (opt->unordered) {
812 struct oidset seen = OIDSET_INIT;
814 cb.seen = &seen;
816 for_each_loose_object(batch_unordered_loose, &cb, 0);
817 for_each_packed_object(batch_unordered_packed, &cb,
818 FOR_EACH_OBJECT_PACK_ORDER);
820 oidset_clear(&seen);
821 } else {
822 struct oid_array sa = OID_ARRAY_INIT;
824 for_each_loose_object(collect_loose_object, &sa, 0);
825 for_each_packed_object(collect_packed_object, &sa, 0);
827 oid_array_for_each_unique(&sa, batch_object_cb, &cb);
829 oid_array_clear(&sa);
832 strbuf_release(&output);
833 return 0;
837 * We are going to call get_sha1 on a potentially very large number of
838 * objects. In most large cases, these will be actual object sha1s. The
839 * cost to double-check that each one is not also a ref (just so we can
840 * warn) ends up dwarfing the actual cost of the object lookups
841 * themselves. We can work around it by just turning off the warning.
843 save_warning = warn_on_object_refname_ambiguity;
844 warn_on_object_refname_ambiguity = 0;
846 if (opt->batch_mode == BATCH_MODE_QUEUE_AND_DISPATCH) {
847 batch_objects_command(opt, &output, &data);
848 goto cleanup;
851 while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
852 if (data.split_on_whitespace) {
854 * Split at first whitespace, tying off the beginning
855 * of the string and saving the remainder (or NULL) in
856 * data.rest.
858 char *p = strpbrk(input.buf, " \t");
859 if (p) {
860 while (*p && strchr(" \t", *p))
861 *p++ = '\0';
863 data.rest = p;
866 batch_one_object(input.buf, &output, opt, &data);
869 cleanup:
870 strbuf_release(&input);
871 strbuf_release(&output);
872 warn_on_object_refname_ambiguity = save_warning;
873 return retval;
876 static int git_cat_file_config(const char *var, const char *value, void *cb)
878 if (userdiff_config(var, value) < 0)
879 return -1;
881 return git_default_config(var, value, cb);
884 static int batch_option_callback(const struct option *opt,
885 const char *arg,
886 int unset)
888 struct batch_options *bo = opt->value;
890 BUG_ON_OPT_NEG(unset);
892 if (bo->enabled) {
893 return error(_("only one batch option may be specified"));
896 bo->enabled = 1;
898 if (!strcmp(opt->long_name, "batch"))
899 bo->batch_mode = BATCH_MODE_CONTENTS;
900 else if (!strcmp(opt->long_name, "batch-check"))
901 bo->batch_mode = BATCH_MODE_INFO;
902 else if (!strcmp(opt->long_name, "batch-command"))
903 bo->batch_mode = BATCH_MODE_QUEUE_AND_DISPATCH;
904 else
905 BUG("%s given to batch-option-callback", opt->long_name);
907 bo->format = arg;
909 return 0;
912 int cmd_cat_file(int argc, const char **argv, const char *prefix)
914 int opt = 0;
915 int opt_cw = 0;
916 int opt_epts = 0;
917 const char *exp_type = NULL, *obj_name = NULL;
918 struct batch_options batch = {0};
919 int unknown_type = 0;
920 int input_nul_terminated = 0;
921 int nul_terminated = 0;
923 const char * const usage[] = {
924 N_("git cat-file <type> <object>"),
925 N_("git cat-file (-e | -p) <object>"),
926 N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"),
927 N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
928 " [--buffer] [--follow-symlinks] [--unordered]\n"
929 " [--textconv | --filters] [-Z]"),
930 N_("git cat-file (--textconv | --filters)\n"
931 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"),
932 NULL
934 const struct option options[] = {
935 /* Simple queries */
936 OPT_GROUP(N_("Check object existence or emit object contents")),
937 OPT_CMDMODE('e', NULL, &opt,
938 N_("check if <object> exists"), 'e'),
939 OPT_CMDMODE('p', NULL, &opt, N_("pretty-print <object> content"), 'p'),
941 OPT_GROUP(N_("Emit [broken] object attributes")),
942 OPT_CMDMODE('t', NULL, &opt, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'),
943 OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
944 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
945 N_("allow -s and -t to work with broken/corrupt objects")),
946 OPT_BOOL(0, "use-mailmap", &use_mailmap, N_("use mail map file")),
947 OPT_ALIAS(0, "mailmap", "use-mailmap"),
948 /* Batch mode */
949 OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")),
950 OPT_CALLBACK_F(0, "batch", &batch, N_("format"),
951 N_("show full <object> or <rev> contents"),
952 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
953 batch_option_callback),
954 OPT_CALLBACK_F(0, "batch-check", &batch, N_("format"),
955 N_("like --batch, but don't emit <contents>"),
956 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
957 batch_option_callback),
958 OPT_BOOL_F('z', NULL, &input_nul_terminated, N_("stdin is NUL-terminated"),
959 PARSE_OPT_HIDDEN),
960 OPT_BOOL('Z', NULL, &nul_terminated, N_("stdin and stdout is NUL-terminated")),
961 OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
962 N_("read commands from stdin"),
963 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
964 batch_option_callback),
965 OPT_CMDMODE(0, "batch-all-objects", &opt,
966 N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'),
967 /* Batch-specific options */
968 OPT_GROUP(N_("Change or optimize batch output")),
969 OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
970 OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
971 N_("follow in-tree symlinks")),
972 OPT_BOOL(0, "unordered", &batch.unordered,
973 N_("do not order objects before emitting them")),
974 /* Textconv options, stand-ole*/
975 OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")),
976 OPT_CMDMODE(0, "textconv", &opt,
977 N_("run textconv on object's content"), 'c'),
978 OPT_CMDMODE(0, "filters", &opt,
979 N_("run filters on object's content"), 'w'),
980 OPT_STRING(0, "path", &force_path, N_("blob|tree"),
981 N_("use a <path> for (--textconv | --filters); Not with 'batch'")),
982 OPT_END()
985 git_config(git_cat_file_config, NULL);
987 batch.buffer_output = -1;
989 argc = parse_options(argc, argv, prefix, options, usage, 0);
990 opt_cw = (opt == 'c' || opt == 'w');
991 opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's');
993 if (use_mailmap)
994 read_mailmap(&mailmap);
996 /* --batch-all-objects? */
997 if (opt == 'b')
998 batch.all_objects = 1;
1000 /* Option compatibility */
1001 if (force_path && !opt_cw)
1002 usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
1003 usage, options,
1004 "--path", _("path|tree-ish"), "--filters",
1005 "--textconv");
1007 /* Option compatibility with batch mode */
1008 if (batch.enabled)
1010 else if (batch.follow_symlinks)
1011 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1012 "--follow-symlinks");
1013 else if (batch.buffer_output >= 0)
1014 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1015 "--buffer");
1016 else if (batch.all_objects)
1017 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1018 "--batch-all-objects");
1019 else if (input_nul_terminated)
1020 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1021 "-z");
1022 else if (nul_terminated)
1023 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1024 "-Z");
1026 batch.input_delim = batch.output_delim = '\n';
1027 if (input_nul_terminated)
1028 batch.input_delim = '\0';
1029 if (nul_terminated)
1030 batch.input_delim = batch.output_delim = '\0';
1032 /* Batch defaults */
1033 if (batch.buffer_output < 0)
1034 batch.buffer_output = batch.all_objects;
1036 /* Return early if we're in batch mode? */
1037 if (batch.enabled) {
1038 if (opt_cw)
1039 batch.transform_mode = opt;
1040 else if (opt && opt != 'b')
1041 usage_msg_optf(_("'-%c' is incompatible with batch mode"),
1042 usage, options, opt);
1043 else if (argc)
1044 usage_msg_opt(_("batch modes take no arguments"), usage,
1045 options);
1047 return batch_objects(&batch);
1050 if (opt) {
1051 if (!argc && opt == 'c')
1052 usage_msg_optf(_("<rev> required with '%s'"),
1053 usage, options, "--textconv");
1054 else if (!argc && opt == 'w')
1055 usage_msg_optf(_("<rev> required with '%s'"),
1056 usage, options, "--filters");
1057 else if (!argc && opt_epts)
1058 usage_msg_optf(_("<object> required with '-%c'"),
1059 usage, options, opt);
1060 else if (argc == 1)
1061 obj_name = argv[0];
1062 else
1063 usage_msg_opt(_("too many arguments"), usage, options);
1064 } else if (!argc) {
1065 usage_with_options(usage, options);
1066 } else if (argc != 2) {
1067 usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"),
1068 usage, options, argc);
1069 } else if (argc) {
1070 exp_type = argv[0];
1071 obj_name = argv[1];
1074 if (unknown_type && opt != 't' && opt != 's')
1075 die("git cat-file --allow-unknown-type: use with -s or -t");
1076 return cat_one_file(opt, exp_type, obj_name, unknown_type);