The second batch
[git.git] / builtin / cat-file.c
blob43a1d7ac4989a22023d7060baa9ae880d1dd7c9a
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
7 #include "builtin.h"
8 #include "config.h"
9 #include "convert.h"
10 #include "diff.h"
11 #include "environment.h"
12 #include "gettext.h"
13 #include "hex.h"
14 #include "ident.h"
15 #include "parse-options.h"
16 #include "userdiff.h"
17 #include "streaming.h"
18 #include "oid-array.h"
19 #include "packfile.h"
20 #include "object-file.h"
21 #include "object-name.h"
22 #include "object-store-ll.h"
23 #include "replace-object.h"
24 #include "promisor-remote.h"
25 #include "mailmap.h"
26 #include "write-or-die.h"
28 enum batch_mode {
29 BATCH_MODE_CONTENTS,
30 BATCH_MODE_INFO,
31 BATCH_MODE_QUEUE_AND_DISPATCH,
34 struct batch_options {
35 int enabled;
36 int follow_symlinks;
37 enum batch_mode batch_mode;
38 int buffer_output;
39 int all_objects;
40 int unordered;
41 int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */
42 char input_delim;
43 char output_delim;
44 const char *format;
47 static const char *force_path;
49 static struct string_list mailmap = STRING_LIST_INIT_NODUP;
50 static int use_mailmap;
52 static char *replace_idents_using_mailmap(char *, size_t *);
54 static char *replace_idents_using_mailmap(char *object_buf, size_t *size)
56 struct strbuf sb = STRBUF_INIT;
57 const char *headers[] = { "author ", "committer ", "tagger ", NULL };
59 strbuf_attach(&sb, object_buf, *size, *size + 1);
60 apply_mailmap_to_header(&sb, headers, &mailmap);
61 *size = sb.len;
62 return strbuf_detach(&sb, NULL);
65 static int filter_object(const char *path, unsigned mode,
66 const struct object_id *oid,
67 char **buf, unsigned long *size)
69 enum object_type type;
71 *buf = repo_read_object_file(the_repository, oid, &type, size);
72 if (!*buf)
73 return error(_("cannot read object %s '%s'"),
74 oid_to_hex(oid), path);
75 if ((type == OBJ_BLOB) && S_ISREG(mode)) {
76 struct strbuf strbuf = STRBUF_INIT;
77 struct checkout_metadata meta;
79 init_checkout_metadata(&meta, NULL, NULL, oid);
80 if (convert_to_working_tree(the_repository->index, path, *buf, *size, &strbuf, &meta)) {
81 free(*buf);
82 *size = strbuf.len;
83 *buf = strbuf_detach(&strbuf, NULL);
87 return 0;
90 static int stream_blob(const struct object_id *oid)
92 if (stream_blob_to_fd(1, oid, NULL, 0))
93 die("unable to stream %s to stdout", oid_to_hex(oid));
94 return 0;
97 static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
98 int unknown_type)
100 int ret;
101 struct object_id oid;
102 enum object_type type;
103 char *buf;
104 unsigned long size;
105 struct object_context obj_context;
106 struct object_info oi = OBJECT_INFO_INIT;
107 struct strbuf sb = STRBUF_INIT;
108 unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
109 unsigned get_oid_flags =
110 GET_OID_RECORD_PATH |
111 GET_OID_ONLY_TO_DIE |
112 GET_OID_HASH_ANY;
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;
228 if (!buffer)
229 die(_("unable to read %s"), oid_to_hex(&oid));
231 if (!skip_prefix(buffer, "object ", &target) ||
232 get_oid_hex_algop(target, &blob_oid,
233 &hash_algos[oid.algo]))
234 die("%s not a valid tag", oid_to_hex(&oid));
235 free(buffer);
236 } else
237 oidcpy(&blob_oid, &oid);
239 if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB) {
240 ret = stream_blob(&blob_oid);
241 goto cleanup;
244 * we attempted to dereference a tag to a blob
245 * and failed; there may be new dereference
246 * mechanisms this code is not aware of.
247 * fall-back to the usual case.
250 buf = read_object_with_reference(the_repository, &oid,
251 exp_type_id, &size, NULL);
253 if (use_mailmap) {
254 size_t s = size;
255 buf = replace_idents_using_mailmap(buf, &s);
256 size = cast_size_t_to_ulong(s);
258 break;
260 default:
261 die("git cat-file: unknown option: %s", exp_type);
264 if (!buf)
265 die("git cat-file %s: bad file", obj_name);
267 write_or_die(1, buf, size);
268 ret = 0;
269 cleanup:
270 free(buf);
271 free(obj_context.path);
272 return ret;
275 struct expand_data {
276 struct object_id oid;
277 enum object_type type;
278 unsigned long size;
279 off_t disk_size;
280 const char *rest;
281 struct object_id delta_base_oid;
284 * If mark_query is true, we do not expand anything, but rather
285 * just mark the object_info with items we wish to query.
287 int mark_query;
290 * Whether to split the input on whitespace before feeding it to
291 * get_sha1; this is decided during the mark_query phase based on
292 * whether we have a %(rest) token in our format.
294 int split_on_whitespace;
297 * After a mark_query run, this object_info is set up to be
298 * passed to oid_object_info_extended. It will point to the data
299 * elements above, so you can retrieve the response from there.
301 struct object_info info;
304 * This flag will be true if the requested batch format and options
305 * don't require us to call oid_object_info, which can then be
306 * optimized out.
308 unsigned skip_object_info : 1;
311 static int is_atom(const char *atom, const char *s, int slen)
313 int alen = strlen(atom);
314 return alen == slen && !memcmp(atom, s, alen);
317 static int expand_atom(struct strbuf *sb, const char *atom, int len,
318 struct expand_data *data)
320 if (is_atom("objectname", atom, len)) {
321 if (!data->mark_query)
322 strbuf_addstr(sb, oid_to_hex(&data->oid));
323 } else if (is_atom("objecttype", atom, len)) {
324 if (data->mark_query)
325 data->info.typep = &data->type;
326 else
327 strbuf_addstr(sb, type_name(data->type));
328 } else if (is_atom("objectsize", atom, len)) {
329 if (data->mark_query)
330 data->info.sizep = &data->size;
331 else
332 strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
333 } else if (is_atom("objectsize:disk", atom, len)) {
334 if (data->mark_query)
335 data->info.disk_sizep = &data->disk_size;
336 else
337 strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
338 } else if (is_atom("rest", atom, len)) {
339 if (data->mark_query)
340 data->split_on_whitespace = 1;
341 else if (data->rest)
342 strbuf_addstr(sb, data->rest);
343 } else if (is_atom("deltabase", atom, len)) {
344 if (data->mark_query)
345 data->info.delta_base_oid = &data->delta_base_oid;
346 else
347 strbuf_addstr(sb,
348 oid_to_hex(&data->delta_base_oid));
349 } else
350 return 0;
351 return 1;
354 static void expand_format(struct strbuf *sb, const char *start,
355 struct expand_data *data)
357 while (strbuf_expand_step(sb, &start)) {
358 const char *end;
360 if (skip_prefix(start, "%", &start) || *start != '(')
361 strbuf_addch(sb, '%');
362 else if ((end = strchr(start + 1, ')')) &&
363 expand_atom(sb, start + 1, end - start - 1, data))
364 start = end + 1;
365 else
366 strbuf_expand_bad_format(start, "cat-file");
370 static void batch_write(struct batch_options *opt, const void *data, int len)
372 if (opt->buffer_output) {
373 if (fwrite(data, 1, len, stdout) != len)
374 die_errno("unable to write to stdout");
375 } else
376 write_or_die(1, data, len);
379 static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
381 const struct object_id *oid = &data->oid;
383 assert(data->info.typep);
385 if (data->type == OBJ_BLOB) {
386 if (opt->buffer_output)
387 fflush(stdout);
388 if (opt->transform_mode) {
389 char *contents;
390 unsigned long size;
392 if (!data->rest)
393 die("missing path for '%s'", oid_to_hex(oid));
395 if (opt->transform_mode == 'w') {
396 if (filter_object(data->rest, 0100644, oid,
397 &contents, &size))
398 die("could not convert '%s' %s",
399 oid_to_hex(oid), data->rest);
400 } else if (opt->transform_mode == 'c') {
401 enum object_type type;
402 if (!textconv_object(the_repository,
403 data->rest, 0100644, oid,
404 1, &contents, &size))
405 contents = repo_read_object_file(the_repository,
406 oid,
407 &type,
408 &size);
409 if (!contents)
410 die("could not convert '%s' %s",
411 oid_to_hex(oid), data->rest);
412 } else
413 BUG("invalid transform_mode: %c", opt->transform_mode);
414 batch_write(opt, contents, size);
415 free(contents);
416 } else {
417 stream_blob(oid);
420 else {
421 enum object_type type;
422 unsigned long size;
423 void *contents;
425 contents = repo_read_object_file(the_repository, oid, &type,
426 &size);
427 if (!contents)
428 die("object %s disappeared", oid_to_hex(oid));
430 if (use_mailmap) {
431 size_t s = size;
432 contents = replace_idents_using_mailmap(contents, &s);
433 size = cast_size_t_to_ulong(s);
436 if (type != data->type)
437 die("object %s changed type!?", oid_to_hex(oid));
438 if (data->info.sizep && size != data->size && !use_mailmap)
439 die("object %s changed size!?", oid_to_hex(oid));
441 batch_write(opt, contents, size);
442 free(contents);
446 static void print_default_format(struct strbuf *scratch, struct expand_data *data,
447 struct batch_options *opt)
449 strbuf_addf(scratch, "%s %s %"PRIuMAX"%c", oid_to_hex(&data->oid),
450 type_name(data->type),
451 (uintmax_t)data->size, opt->output_delim);
455 * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
456 * which the object may be accessed (though note that we may also rely on
457 * data->oid, too). If "pack" is NULL, then offset is ignored.
459 static void batch_object_write(const char *obj_name,
460 struct strbuf *scratch,
461 struct batch_options *opt,
462 struct expand_data *data,
463 struct packed_git *pack,
464 off_t offset)
466 if (!data->skip_object_info) {
467 int ret;
469 if (use_mailmap)
470 data->info.typep = &data->type;
472 if (pack)
473 ret = packed_object_info(the_repository, pack, offset,
474 &data->info);
475 else
476 ret = oid_object_info_extended(the_repository,
477 &data->oid, &data->info,
478 OBJECT_INFO_LOOKUP_REPLACE);
479 if (ret < 0) {
480 printf("%s missing%c",
481 obj_name ? obj_name : oid_to_hex(&data->oid), opt->output_delim);
482 fflush(stdout);
483 return;
486 if (use_mailmap && (data->type == OBJ_COMMIT || data->type == OBJ_TAG)) {
487 size_t s = data->size;
488 char *buf = NULL;
490 buf = repo_read_object_file(the_repository, &data->oid, &data->type,
491 &data->size);
492 if (!buf)
493 die(_("unable to read %s"), oid_to_hex(&data->oid));
494 buf = replace_idents_using_mailmap(buf, &s);
495 data->size = cast_size_t_to_ulong(s);
497 free(buf);
501 strbuf_reset(scratch);
503 if (!opt->format) {
504 print_default_format(scratch, data, opt);
505 } else {
506 expand_format(scratch, opt->format, data);
507 strbuf_addch(scratch, opt->output_delim);
510 batch_write(opt, scratch->buf, scratch->len);
512 if (opt->batch_mode == BATCH_MODE_CONTENTS) {
513 print_object_or_die(opt, data);
514 batch_write(opt, &opt->output_delim, 1);
518 static void batch_one_object(const char *obj_name,
519 struct strbuf *scratch,
520 struct batch_options *opt,
521 struct expand_data *data)
523 struct object_context ctx;
524 int flags =
525 GET_OID_HASH_ANY |
526 (opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0);
527 enum get_oid_result result;
529 result = get_oid_with_context(the_repository, obj_name,
530 flags, &data->oid, &ctx);
531 if (result != FOUND) {
532 switch (result) {
533 case MISSING_OBJECT:
534 printf("%s missing%c", obj_name, opt->output_delim);
535 break;
536 case SHORT_NAME_AMBIGUOUS:
537 printf("%s ambiguous%c", obj_name, opt->output_delim);
538 break;
539 case DANGLING_SYMLINK:
540 printf("dangling %"PRIuMAX"%c%s%c",
541 (uintmax_t)strlen(obj_name),
542 opt->output_delim, obj_name, opt->output_delim);
543 break;
544 case SYMLINK_LOOP:
545 printf("loop %"PRIuMAX"%c%s%c",
546 (uintmax_t)strlen(obj_name),
547 opt->output_delim, obj_name, opt->output_delim);
548 break;
549 case NOT_DIR:
550 printf("notdir %"PRIuMAX"%c%s%c",
551 (uintmax_t)strlen(obj_name),
552 opt->output_delim, obj_name, opt->output_delim);
553 break;
554 default:
555 BUG("unknown get_sha1_with_context result %d\n",
556 result);
557 break;
559 fflush(stdout);
560 return;
563 if (ctx.mode == 0) {
564 printf("symlink %"PRIuMAX"%c%s%c",
565 (uintmax_t)ctx.symlink_path.len,
566 opt->output_delim, ctx.symlink_path.buf, opt->output_delim);
567 fflush(stdout);
568 return;
571 batch_object_write(obj_name, scratch, opt, data, NULL, 0);
574 struct object_cb_data {
575 struct batch_options *opt;
576 struct expand_data *expand;
577 struct oidset *seen;
578 struct strbuf *scratch;
581 static int batch_object_cb(const struct object_id *oid, void *vdata)
583 struct object_cb_data *data = vdata;
584 oidcpy(&data->expand->oid, oid);
585 batch_object_write(NULL, data->scratch, data->opt, data->expand,
586 NULL, 0);
587 return 0;
590 static int collect_loose_object(const struct object_id *oid,
591 const char *path UNUSED,
592 void *data)
594 oid_array_append(data, oid);
595 return 0;
598 static int collect_packed_object(const struct object_id *oid,
599 struct packed_git *pack UNUSED,
600 uint32_t pos UNUSED,
601 void *data)
603 oid_array_append(data, oid);
604 return 0;
607 static int batch_unordered_object(const struct object_id *oid,
608 struct packed_git *pack, off_t offset,
609 void *vdata)
611 struct object_cb_data *data = vdata;
613 if (oidset_insert(data->seen, oid))
614 return 0;
616 oidcpy(&data->expand->oid, oid);
617 batch_object_write(NULL, data->scratch, data->opt, data->expand,
618 pack, offset);
619 return 0;
622 static int batch_unordered_loose(const struct object_id *oid,
623 const char *path UNUSED,
624 void *data)
626 return batch_unordered_object(oid, NULL, 0, data);
629 static int batch_unordered_packed(const struct object_id *oid,
630 struct packed_git *pack,
631 uint32_t pos,
632 void *data)
634 return batch_unordered_object(oid, pack,
635 nth_packed_object_offset(pack, pos),
636 data);
639 typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *,
640 struct strbuf *, struct expand_data *);
642 struct queued_cmd {
643 parse_cmd_fn_t fn;
644 char *line;
647 static void parse_cmd_contents(struct batch_options *opt,
648 const char *line,
649 struct strbuf *output,
650 struct expand_data *data)
652 opt->batch_mode = BATCH_MODE_CONTENTS;
653 batch_one_object(line, output, opt, data);
656 static void parse_cmd_info(struct batch_options *opt,
657 const char *line,
658 struct strbuf *output,
659 struct expand_data *data)
661 opt->batch_mode = BATCH_MODE_INFO;
662 batch_one_object(line, output, opt, data);
665 static void dispatch_calls(struct batch_options *opt,
666 struct strbuf *output,
667 struct expand_data *data,
668 struct queued_cmd *cmd,
669 int nr)
671 int i;
673 if (!opt->buffer_output)
674 die(_("flush is only for --buffer mode"));
676 for (i = 0; i < nr; i++)
677 cmd[i].fn(opt, cmd[i].line, output, data);
679 fflush(stdout);
682 static void free_cmds(struct queued_cmd *cmd, size_t *nr)
684 size_t i;
686 for (i = 0; i < *nr; i++)
687 FREE_AND_NULL(cmd[i].line);
689 *nr = 0;
693 static const struct parse_cmd {
694 const char *name;
695 parse_cmd_fn_t fn;
696 unsigned takes_args;
697 } commands[] = {
698 { "contents", parse_cmd_contents, 1},
699 { "info", parse_cmd_info, 1},
700 { "flush", NULL, 0},
703 static void batch_objects_command(struct batch_options *opt,
704 struct strbuf *output,
705 struct expand_data *data)
707 struct strbuf input = STRBUF_INIT;
708 struct queued_cmd *queued_cmd = NULL;
709 size_t alloc = 0, nr = 0;
711 while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
712 int i;
713 const struct parse_cmd *cmd = NULL;
714 const char *p = NULL, *cmd_end;
715 struct queued_cmd call = {0};
717 if (!input.len)
718 die(_("empty command in input"));
719 if (isspace(*input.buf))
720 die(_("whitespace before command: '%s'"), input.buf);
722 for (i = 0; i < ARRAY_SIZE(commands); i++) {
723 if (!skip_prefix(input.buf, commands[i].name, &cmd_end))
724 continue;
726 cmd = &commands[i];
727 if (cmd->takes_args) {
728 if (*cmd_end != ' ')
729 die(_("%s requires arguments"),
730 commands[i].name);
732 p = cmd_end + 1;
733 } else if (*cmd_end) {
734 die(_("%s takes no arguments"),
735 commands[i].name);
738 break;
741 if (!cmd)
742 die(_("unknown command: '%s'"), input.buf);
744 if (!strcmp(cmd->name, "flush")) {
745 dispatch_calls(opt, output, data, queued_cmd, nr);
746 free_cmds(queued_cmd, &nr);
747 } else if (!opt->buffer_output) {
748 cmd->fn(opt, p, output, data);
749 } else {
750 ALLOC_GROW(queued_cmd, nr + 1, alloc);
751 call.fn = cmd->fn;
752 call.line = xstrdup_or_null(p);
753 queued_cmd[nr++] = call;
757 if (opt->buffer_output &&
758 nr &&
759 !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) {
760 dispatch_calls(opt, output, data, queued_cmd, nr);
761 free_cmds(queued_cmd, &nr);
764 free_cmds(queued_cmd, &nr);
765 free(queued_cmd);
766 strbuf_release(&input);
769 #define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
771 static int batch_objects(struct batch_options *opt)
773 struct strbuf input = STRBUF_INIT;
774 struct strbuf output = STRBUF_INIT;
775 struct expand_data data;
776 int save_warning;
777 int retval = 0;
780 * Expand once with our special mark_query flag, which will prime the
781 * object_info to be handed to oid_object_info_extended for each
782 * object.
784 memset(&data, 0, sizeof(data));
785 data.mark_query = 1;
786 expand_format(&output,
787 opt->format ? opt->format : DEFAULT_FORMAT,
788 &data);
789 data.mark_query = 0;
790 strbuf_release(&output);
791 if (opt->transform_mode)
792 data.split_on_whitespace = 1;
794 if (opt->format && !strcmp(opt->format, DEFAULT_FORMAT))
795 opt->format = NULL;
797 * If we are printing out the object, then always fill in the type,
798 * since we will want to decide whether or not to stream.
800 if (opt->batch_mode == BATCH_MODE_CONTENTS)
801 data.info.typep = &data.type;
803 if (opt->all_objects) {
804 struct object_cb_data cb;
805 struct object_info empty = OBJECT_INFO_INIT;
807 if (!memcmp(&data.info, &empty, sizeof(empty)))
808 data.skip_object_info = 1;
810 if (repo_has_promisor_remote(the_repository))
811 warning("This repository uses promisor remotes. Some objects may not be loaded.");
813 disable_replace_refs();
815 cb.opt = opt;
816 cb.expand = &data;
817 cb.scratch = &output;
819 if (opt->unordered) {
820 struct oidset seen = OIDSET_INIT;
822 cb.seen = &seen;
824 for_each_loose_object(batch_unordered_loose, &cb, 0);
825 for_each_packed_object(batch_unordered_packed, &cb,
826 FOR_EACH_OBJECT_PACK_ORDER);
828 oidset_clear(&seen);
829 } else {
830 struct oid_array sa = OID_ARRAY_INIT;
832 for_each_loose_object(collect_loose_object, &sa, 0);
833 for_each_packed_object(collect_packed_object, &sa, 0);
835 oid_array_for_each_unique(&sa, batch_object_cb, &cb);
837 oid_array_clear(&sa);
840 strbuf_release(&output);
841 return 0;
845 * We are going to call get_sha1 on a potentially very large number of
846 * objects. In most large cases, these will be actual object sha1s. The
847 * cost to double-check that each one is not also a ref (just so we can
848 * warn) ends up dwarfing the actual cost of the object lookups
849 * themselves. We can work around it by just turning off the warning.
851 save_warning = warn_on_object_refname_ambiguity;
852 warn_on_object_refname_ambiguity = 0;
854 if (opt->batch_mode == BATCH_MODE_QUEUE_AND_DISPATCH) {
855 batch_objects_command(opt, &output, &data);
856 goto cleanup;
859 while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
860 if (data.split_on_whitespace) {
862 * Split at first whitespace, tying off the beginning
863 * of the string and saving the remainder (or NULL) in
864 * data.rest.
866 char *p = strpbrk(input.buf, " \t");
867 if (p) {
868 while (*p && strchr(" \t", *p))
869 *p++ = '\0';
871 data.rest = p;
874 batch_one_object(input.buf, &output, opt, &data);
877 cleanup:
878 strbuf_release(&input);
879 strbuf_release(&output);
880 warn_on_object_refname_ambiguity = save_warning;
881 return retval;
884 static int git_cat_file_config(const char *var, const char *value,
885 const struct config_context *ctx, void *cb)
887 if (userdiff_config(var, value) < 0)
888 return -1;
890 return git_default_config(var, value, ctx, cb);
893 static int batch_option_callback(const struct option *opt,
894 const char *arg,
895 int unset)
897 struct batch_options *bo = opt->value;
899 BUG_ON_OPT_NEG(unset);
901 if (bo->enabled) {
902 return error(_("only one batch option may be specified"));
905 bo->enabled = 1;
907 if (!strcmp(opt->long_name, "batch"))
908 bo->batch_mode = BATCH_MODE_CONTENTS;
909 else if (!strcmp(opt->long_name, "batch-check"))
910 bo->batch_mode = BATCH_MODE_INFO;
911 else if (!strcmp(opt->long_name, "batch-command"))
912 bo->batch_mode = BATCH_MODE_QUEUE_AND_DISPATCH;
913 else
914 BUG("%s given to batch-option-callback", opt->long_name);
916 bo->format = arg;
918 return 0;
921 int cmd_cat_file(int argc, const char **argv, const char *prefix)
923 int opt = 0;
924 int opt_cw = 0;
925 int opt_epts = 0;
926 const char *exp_type = NULL, *obj_name = NULL;
927 struct batch_options batch = {0};
928 int unknown_type = 0;
929 int input_nul_terminated = 0;
930 int nul_terminated = 0;
932 const char * const usage[] = {
933 N_("git cat-file <type> <object>"),
934 N_("git cat-file (-e | -p) <object>"),
935 N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"),
936 N_("git cat-file (--textconv | --filters)\n"
937 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"),
938 N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
939 " [--buffer] [--follow-symlinks] [--unordered]\n"
940 " [--textconv | --filters] [-Z]"),
941 NULL
943 const struct option options[] = {
944 /* Simple queries */
945 OPT_GROUP(N_("Check object existence or emit object contents")),
946 OPT_CMDMODE('e', NULL, &opt,
947 N_("check if <object> exists"), 'e'),
948 OPT_CMDMODE('p', NULL, &opt, N_("pretty-print <object> content"), 'p'),
950 OPT_GROUP(N_("Emit [broken] object attributes")),
951 OPT_CMDMODE('t', NULL, &opt, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'),
952 OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
953 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
954 N_("allow -s and -t to work with broken/corrupt objects")),
955 OPT_BOOL(0, "use-mailmap", &use_mailmap, N_("use mail map file")),
956 OPT_ALIAS(0, "mailmap", "use-mailmap"),
957 /* Batch mode */
958 OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")),
959 OPT_CALLBACK_F(0, "batch", &batch, N_("format"),
960 N_("show full <object> or <rev> contents"),
961 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
962 batch_option_callback),
963 OPT_CALLBACK_F(0, "batch-check", &batch, N_("format"),
964 N_("like --batch, but don't emit <contents>"),
965 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
966 batch_option_callback),
967 OPT_BOOL_F('z', NULL, &input_nul_terminated, N_("stdin is NUL-terminated"),
968 PARSE_OPT_HIDDEN),
969 OPT_BOOL('Z', NULL, &nul_terminated, N_("stdin and stdout is NUL-terminated")),
970 OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
971 N_("read commands from stdin"),
972 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
973 batch_option_callback),
974 OPT_CMDMODE(0, "batch-all-objects", &opt,
975 N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'),
976 /* Batch-specific options */
977 OPT_GROUP(N_("Change or optimize batch output")),
978 OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
979 OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
980 N_("follow in-tree symlinks")),
981 OPT_BOOL(0, "unordered", &batch.unordered,
982 N_("do not order objects before emitting them")),
983 /* Textconv options, stand-ole*/
984 OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")),
985 OPT_CMDMODE(0, "textconv", &opt,
986 N_("run textconv on object's content"), 'c'),
987 OPT_CMDMODE(0, "filters", &opt,
988 N_("run filters on object's content"), 'w'),
989 OPT_STRING(0, "path", &force_path, N_("blob|tree"),
990 N_("use a <path> for (--textconv | --filters); Not with 'batch'")),
991 OPT_END()
994 git_config(git_cat_file_config, NULL);
996 batch.buffer_output = -1;
998 argc = parse_options(argc, argv, prefix, options, usage, 0);
999 opt_cw = (opt == 'c' || opt == 'w');
1000 opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's');
1002 if (use_mailmap)
1003 read_mailmap(&mailmap);
1005 /* --batch-all-objects? */
1006 if (opt == 'b')
1007 batch.all_objects = 1;
1009 /* Option compatibility */
1010 if (force_path && !opt_cw)
1011 usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
1012 usage, options,
1013 "--path", _("path|tree-ish"), "--filters",
1014 "--textconv");
1016 /* Option compatibility with batch mode */
1017 if (batch.enabled)
1019 else if (batch.follow_symlinks)
1020 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1021 "--follow-symlinks");
1022 else if (batch.buffer_output >= 0)
1023 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1024 "--buffer");
1025 else if (batch.all_objects)
1026 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1027 "--batch-all-objects");
1028 else if (input_nul_terminated)
1029 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1030 "-z");
1031 else if (nul_terminated)
1032 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1033 "-Z");
1035 batch.input_delim = batch.output_delim = '\n';
1036 if (input_nul_terminated)
1037 batch.input_delim = '\0';
1038 if (nul_terminated)
1039 batch.input_delim = batch.output_delim = '\0';
1041 /* Batch defaults */
1042 if (batch.buffer_output < 0)
1043 batch.buffer_output = batch.all_objects;
1045 /* Return early if we're in batch mode? */
1046 if (batch.enabled) {
1047 if (opt_cw)
1048 batch.transform_mode = opt;
1049 else if (opt && opt != 'b')
1050 usage_msg_optf(_("'-%c' is incompatible with batch mode"),
1051 usage, options, opt);
1052 else if (argc)
1053 usage_msg_opt(_("batch modes take no arguments"), usage,
1054 options);
1056 return batch_objects(&batch);
1059 if (opt) {
1060 if (!argc && opt == 'c')
1061 usage_msg_optf(_("<rev> required with '%s'"),
1062 usage, options, "--textconv");
1063 else if (!argc && opt == 'w')
1064 usage_msg_optf(_("<rev> required with '%s'"),
1065 usage, options, "--filters");
1066 else if (!argc && opt_epts)
1067 usage_msg_optf(_("<object> required with '-%c'"),
1068 usage, options, opt);
1069 else if (argc == 1)
1070 obj_name = argv[0];
1071 else
1072 usage_msg_opt(_("too many arguments"), usage, options);
1073 } else if (!argc) {
1074 usage_with_options(usage, options);
1075 } else if (argc != 2) {
1076 usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"),
1077 usage, options, argc);
1078 } else if (argc) {
1079 exp_type = argv[0];
1080 obj_name = argv[1];
1083 if (unknown_type && opt != 't' && opt != 's')
1084 die("git cat-file --allow-unknown-type: use with -s or -t");
1085 return cat_one_file(opt, exp_type, obj_name, unknown_type);