upload-pack: drop separate v2 "haves" array
[alt-git.git] / builtin / cat-file.c
blobbbf851138ec40875367aba94bc5b5de29b8dbe77
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define USE_THE_INDEX_VARIABLE
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_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 = GET_OID_RECORD_PATH | GET_OID_ONLY_TO_DIE;
110 const char *path = force_path;
111 const int opt_cw = (opt == 'c' || opt == 'w');
112 if (!path && opt_cw)
113 get_oid_flags |= GET_OID_REQUIRE_PATH;
115 if (unknown_type)
116 flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
118 if (get_oid_with_context(the_repository, obj_name, get_oid_flags, &oid,
119 &obj_context))
120 die("Not a valid object name %s", obj_name);
122 if (!path)
123 path = obj_context.path;
124 if (obj_context.mode == S_IFINVALID)
125 obj_context.mode = 0100644;
127 buf = NULL;
128 switch (opt) {
129 case 't':
130 oi.type_name = &sb;
131 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
132 die("git cat-file: could not get object info");
133 if (sb.len) {
134 printf("%s\n", sb.buf);
135 strbuf_release(&sb);
136 ret = 0;
137 goto cleanup;
139 break;
141 case 's':
142 oi.sizep = &size;
144 if (use_mailmap) {
145 oi.typep = &type;
146 oi.contentp = (void**)&buf;
149 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
150 die("git cat-file: could not get object info");
152 if (use_mailmap && (type == OBJ_COMMIT || type == OBJ_TAG)) {
153 size_t s = size;
154 buf = replace_idents_using_mailmap(buf, &s);
155 size = cast_size_t_to_ulong(s);
158 printf("%"PRIuMAX"\n", (uintmax_t)size);
159 ret = 0;
160 goto cleanup;
162 case 'e':
163 return !repo_has_object_file(the_repository, &oid);
165 case 'w':
167 if (filter_object(path, obj_context.mode,
168 &oid, &buf, &size)) {
169 ret = -1;
170 goto cleanup;
172 break;
174 case 'c':
175 if (textconv_object(the_repository, path, obj_context.mode,
176 &oid, 1, &buf, &size))
177 break;
178 /* else fallthrough */
180 case 'p':
181 type = oid_object_info(the_repository, &oid, NULL);
182 if (type < 0)
183 die("Not a valid object name %s", obj_name);
185 /* custom pretty-print here */
186 if (type == OBJ_TREE) {
187 const char *ls_args[3] = { NULL };
188 ls_args[0] = "ls-tree";
189 ls_args[1] = obj_name;
190 ret = cmd_ls_tree(2, ls_args, NULL);
191 goto cleanup;
194 if (type == OBJ_BLOB) {
195 ret = stream_blob(&oid);
196 goto cleanup;
198 buf = repo_read_object_file(the_repository, &oid, &type,
199 &size);
200 if (!buf)
201 die("Cannot read object %s", obj_name);
203 if (use_mailmap) {
204 size_t s = size;
205 buf = replace_idents_using_mailmap(buf, &s);
206 size = cast_size_t_to_ulong(s);
209 /* otherwise just spit out the data */
210 break;
212 case 0:
214 enum object_type exp_type_id = type_from_string(exp_type);
216 if (exp_type_id == OBJ_BLOB) {
217 struct object_id blob_oid;
218 if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
219 char *buffer = repo_read_object_file(the_repository,
220 &oid,
221 &type,
222 &size);
223 const char *target;
225 if (!buffer)
226 die(_("unable to read %s"), oid_to_hex(&oid));
228 if (!skip_prefix(buffer, "object ", &target) ||
229 get_oid_hex(target, &blob_oid))
230 die("%s not a valid tag", oid_to_hex(&oid));
231 free(buffer);
232 } else
233 oidcpy(&blob_oid, &oid);
235 if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB) {
236 ret = stream_blob(&blob_oid);
237 goto cleanup;
240 * we attempted to dereference a tag to a blob
241 * and failed; there may be new dereference
242 * mechanisms this code is not aware of.
243 * fall-back to the usual case.
246 buf = read_object_with_reference(the_repository, &oid,
247 exp_type_id, &size, NULL);
249 if (use_mailmap) {
250 size_t s = size;
251 buf = replace_idents_using_mailmap(buf, &s);
252 size = cast_size_t_to_ulong(s);
254 break;
256 default:
257 die("git cat-file: unknown option: %s", exp_type);
260 if (!buf)
261 die("git cat-file %s: bad file", obj_name);
263 write_or_die(1, buf, size);
264 ret = 0;
265 cleanup:
266 free(buf);
267 free(obj_context.path);
268 return ret;
271 struct expand_data {
272 struct object_id oid;
273 enum object_type type;
274 unsigned long size;
275 off_t disk_size;
276 const char *rest;
277 struct object_id delta_base_oid;
280 * If mark_query is true, we do not expand anything, but rather
281 * just mark the object_info with items we wish to query.
283 int mark_query;
286 * Whether to split the input on whitespace before feeding it to
287 * get_sha1; this is decided during the mark_query phase based on
288 * whether we have a %(rest) token in our format.
290 int split_on_whitespace;
293 * After a mark_query run, this object_info is set up to be
294 * passed to oid_object_info_extended. It will point to the data
295 * elements above, so you can retrieve the response from there.
297 struct object_info info;
300 * This flag will be true if the requested batch format and options
301 * don't require us to call oid_object_info, which can then be
302 * optimized out.
304 unsigned skip_object_info : 1;
307 static int is_atom(const char *atom, const char *s, int slen)
309 int alen = strlen(atom);
310 return alen == slen && !memcmp(atom, s, alen);
313 static void expand_atom(struct strbuf *sb, const char *atom, int len,
314 struct expand_data *data)
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 void expand_format(struct strbuf *sb, const char *start,
350 struct expand_data *data)
352 while (strbuf_expand_step(sb, &start)) {
353 const char *end;
355 if (skip_prefix(start, "%", &start) || *start != '(')
356 strbuf_addch(sb, '%');
357 else if (!(end = strchr(start + 1, ')')))
358 die("format element '%s' does not end in ')'", start);
359 else {
360 expand_atom(sb, start + 1, end - start - 1, data);
361 start = end + 1;
366 static void batch_write(struct batch_options *opt, const void *data, int len)
368 if (opt->buffer_output) {
369 if (fwrite(data, 1, len, stdout) != len)
370 die_errno("unable to write to stdout");
371 } else
372 write_or_die(1, data, len);
375 static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
377 const struct object_id *oid = &data->oid;
379 assert(data->info.typep);
381 if (data->type == OBJ_BLOB) {
382 if (opt->buffer_output)
383 fflush(stdout);
384 if (opt->transform_mode) {
385 char *contents;
386 unsigned long size;
388 if (!data->rest)
389 die("missing path for '%s'", oid_to_hex(oid));
391 if (opt->transform_mode == 'w') {
392 if (filter_object(data->rest, 0100644, oid,
393 &contents, &size))
394 die("could not convert '%s' %s",
395 oid_to_hex(oid), data->rest);
396 } else if (opt->transform_mode == 'c') {
397 enum object_type type;
398 if (!textconv_object(the_repository,
399 data->rest, 0100644, oid,
400 1, &contents, &size))
401 contents = repo_read_object_file(the_repository,
402 oid,
403 &type,
404 &size);
405 if (!contents)
406 die("could not convert '%s' %s",
407 oid_to_hex(oid), data->rest);
408 } else
409 BUG("invalid transform_mode: %c", opt->transform_mode);
410 batch_write(opt, contents, size);
411 free(contents);
412 } else {
413 stream_blob(oid);
416 else {
417 enum object_type type;
418 unsigned long size;
419 void *contents;
421 contents = repo_read_object_file(the_repository, oid, &type,
422 &size);
423 if (!contents)
424 die("object %s disappeared", oid_to_hex(oid));
426 if (use_mailmap) {
427 size_t s = size;
428 contents = replace_idents_using_mailmap(contents, &s);
429 size = cast_size_t_to_ulong(s);
432 if (type != data->type)
433 die("object %s changed type!?", oid_to_hex(oid));
434 if (data->info.sizep && size != data->size && !use_mailmap)
435 die("object %s changed size!?", oid_to_hex(oid));
437 batch_write(opt, contents, size);
438 free(contents);
442 static void print_default_format(struct strbuf *scratch, struct expand_data *data,
443 struct batch_options *opt)
445 strbuf_addf(scratch, "%s %s %"PRIuMAX"%c", oid_to_hex(&data->oid),
446 type_name(data->type),
447 (uintmax_t)data->size, opt->output_delim);
451 * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
452 * which the object may be accessed (though note that we may also rely on
453 * data->oid, too). If "pack" is NULL, then offset is ignored.
455 static void batch_object_write(const char *obj_name,
456 struct strbuf *scratch,
457 struct batch_options *opt,
458 struct expand_data *data,
459 struct packed_git *pack,
460 off_t offset)
462 if (!data->skip_object_info) {
463 int ret;
465 if (use_mailmap)
466 data->info.typep = &data->type;
468 if (pack)
469 ret = packed_object_info(the_repository, pack, offset,
470 &data->info);
471 else
472 ret = oid_object_info_extended(the_repository,
473 &data->oid, &data->info,
474 OBJECT_INFO_LOOKUP_REPLACE);
475 if (ret < 0) {
476 printf("%s missing%c",
477 obj_name ? obj_name : oid_to_hex(&data->oid), opt->output_delim);
478 fflush(stdout);
479 return;
482 if (use_mailmap && (data->type == OBJ_COMMIT || data->type == OBJ_TAG)) {
483 size_t s = data->size;
484 char *buf = NULL;
486 buf = repo_read_object_file(the_repository, &data->oid, &data->type,
487 &data->size);
488 if (!buf)
489 die(_("unable to read %s"), oid_to_hex(&data->oid));
490 buf = replace_idents_using_mailmap(buf, &s);
491 data->size = cast_size_t_to_ulong(s);
493 free(buf);
497 strbuf_reset(scratch);
499 if (!opt->format) {
500 print_default_format(scratch, data, opt);
501 } else {
502 expand_format(scratch, opt->format, data);
503 strbuf_addch(scratch, opt->output_delim);
506 batch_write(opt, scratch->buf, scratch->len);
508 if (opt->batch_mode == BATCH_MODE_CONTENTS) {
509 print_object_or_die(opt, data);
510 batch_write(opt, &opt->output_delim, 1);
514 static void batch_one_object(const char *obj_name,
515 struct strbuf *scratch,
516 struct batch_options *opt,
517 struct expand_data *data)
519 struct object_context ctx;
520 int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
521 enum get_oid_result result;
523 result = get_oid_with_context(the_repository, obj_name,
524 flags, &data->oid, &ctx);
525 if (result != FOUND) {
526 switch (result) {
527 case MISSING_OBJECT:
528 printf("%s missing%c", obj_name, opt->output_delim);
529 break;
530 case SHORT_NAME_AMBIGUOUS:
531 printf("%s ambiguous%c", obj_name, opt->output_delim);
532 break;
533 case DANGLING_SYMLINK:
534 printf("dangling %"PRIuMAX"%c%s%c",
535 (uintmax_t)strlen(obj_name),
536 opt->output_delim, obj_name, opt->output_delim);
537 break;
538 case SYMLINK_LOOP:
539 printf("loop %"PRIuMAX"%c%s%c",
540 (uintmax_t)strlen(obj_name),
541 opt->output_delim, obj_name, opt->output_delim);
542 break;
543 case NOT_DIR:
544 printf("notdir %"PRIuMAX"%c%s%c",
545 (uintmax_t)strlen(obj_name),
546 opt->output_delim, obj_name, opt->output_delim);
547 break;
548 default:
549 BUG("unknown get_sha1_with_context result %d\n",
550 result);
551 break;
553 fflush(stdout);
554 return;
557 if (ctx.mode == 0) {
558 printf("symlink %"PRIuMAX"%c%s%c",
559 (uintmax_t)ctx.symlink_path.len,
560 opt->output_delim, ctx.symlink_path.buf, opt->output_delim);
561 fflush(stdout);
562 return;
565 batch_object_write(obj_name, scratch, opt, data, NULL, 0);
568 struct object_cb_data {
569 struct batch_options *opt;
570 struct expand_data *expand;
571 struct oidset *seen;
572 struct strbuf *scratch;
575 static int batch_object_cb(const struct object_id *oid, void *vdata)
577 struct object_cb_data *data = vdata;
578 oidcpy(&data->expand->oid, oid);
579 batch_object_write(NULL, data->scratch, data->opt, data->expand,
580 NULL, 0);
581 return 0;
584 static int collect_loose_object(const struct object_id *oid,
585 const char *path UNUSED,
586 void *data)
588 oid_array_append(data, oid);
589 return 0;
592 static int collect_packed_object(const struct object_id *oid,
593 struct packed_git *pack UNUSED,
594 uint32_t pos UNUSED,
595 void *data)
597 oid_array_append(data, oid);
598 return 0;
601 static int batch_unordered_object(const struct object_id *oid,
602 struct packed_git *pack, off_t offset,
603 void *vdata)
605 struct object_cb_data *data = vdata;
607 if (oidset_insert(data->seen, oid))
608 return 0;
610 oidcpy(&data->expand->oid, oid);
611 batch_object_write(NULL, data->scratch, data->opt, data->expand,
612 pack, offset);
613 return 0;
616 static int batch_unordered_loose(const struct object_id *oid,
617 const char *path UNUSED,
618 void *data)
620 return batch_unordered_object(oid, NULL, 0, data);
623 static int batch_unordered_packed(const struct object_id *oid,
624 struct packed_git *pack,
625 uint32_t pos,
626 void *data)
628 return batch_unordered_object(oid, pack,
629 nth_packed_object_offset(pack, pos),
630 data);
633 typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *,
634 struct strbuf *, struct expand_data *);
636 struct queued_cmd {
637 parse_cmd_fn_t fn;
638 char *line;
641 static void parse_cmd_contents(struct batch_options *opt,
642 const char *line,
643 struct strbuf *output,
644 struct expand_data *data)
646 opt->batch_mode = BATCH_MODE_CONTENTS;
647 batch_one_object(line, output, opt, data);
650 static void parse_cmd_info(struct batch_options *opt,
651 const char *line,
652 struct strbuf *output,
653 struct expand_data *data)
655 opt->batch_mode = BATCH_MODE_INFO;
656 batch_one_object(line, output, opt, data);
659 static void dispatch_calls(struct batch_options *opt,
660 struct strbuf *output,
661 struct expand_data *data,
662 struct queued_cmd *cmd,
663 int nr)
665 int i;
667 if (!opt->buffer_output)
668 die(_("flush is only for --buffer mode"));
670 for (i = 0; i < nr; i++)
671 cmd[i].fn(opt, cmd[i].line, output, data);
673 fflush(stdout);
676 static void free_cmds(struct queued_cmd *cmd, size_t *nr)
678 size_t i;
680 for (i = 0; i < *nr; i++)
681 FREE_AND_NULL(cmd[i].line);
683 *nr = 0;
687 static const struct parse_cmd {
688 const char *name;
689 parse_cmd_fn_t fn;
690 unsigned takes_args;
691 } commands[] = {
692 { "contents", parse_cmd_contents, 1},
693 { "info", parse_cmd_info, 1},
694 { "flush", NULL, 0},
697 static void batch_objects_command(struct batch_options *opt,
698 struct strbuf *output,
699 struct expand_data *data)
701 struct strbuf input = STRBUF_INIT;
702 struct queued_cmd *queued_cmd = NULL;
703 size_t alloc = 0, nr = 0;
705 while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
706 int i;
707 const struct parse_cmd *cmd = NULL;
708 const char *p = NULL, *cmd_end;
709 struct queued_cmd call = {0};
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 expand_format(&output,
781 opt->format ? opt->format : DEFAULT_FORMAT,
782 &data);
783 data.mark_query = 0;
784 strbuf_release(&output);
785 if (opt->transform_mode)
786 data.split_on_whitespace = 1;
788 if (opt->format && !strcmp(opt->format, DEFAULT_FORMAT))
789 opt->format = NULL;
791 * If we are printing out the object, then always fill in the type,
792 * since we will want to decide whether or not to stream.
794 if (opt->batch_mode == BATCH_MODE_CONTENTS)
795 data.info.typep = &data.type;
797 if (opt->all_objects) {
798 struct object_cb_data cb;
799 struct object_info empty = OBJECT_INFO_INIT;
801 if (!memcmp(&data.info, &empty, sizeof(empty)))
802 data.skip_object_info = 1;
804 if (repo_has_promisor_remote(the_repository))
805 warning("This repository uses promisor remotes. Some objects may not be loaded.");
807 disable_replace_refs();
809 cb.opt = opt;
810 cb.expand = &data;
811 cb.scratch = &output;
813 if (opt->unordered) {
814 struct oidset seen = OIDSET_INIT;
816 cb.seen = &seen;
818 for_each_loose_object(batch_unordered_loose, &cb, 0);
819 for_each_packed_object(batch_unordered_packed, &cb,
820 FOR_EACH_OBJECT_PACK_ORDER);
822 oidset_clear(&seen);
823 } else {
824 struct oid_array sa = OID_ARRAY_INIT;
826 for_each_loose_object(collect_loose_object, &sa, 0);
827 for_each_packed_object(collect_packed_object, &sa, 0);
829 oid_array_for_each_unique(&sa, batch_object_cb, &cb);
831 oid_array_clear(&sa);
834 strbuf_release(&output);
835 return 0;
839 * We are going to call get_sha1 on a potentially very large number of
840 * objects. In most large cases, these will be actual object sha1s. The
841 * cost to double-check that each one is not also a ref (just so we can
842 * warn) ends up dwarfing the actual cost of the object lookups
843 * themselves. We can work around it by just turning off the warning.
845 save_warning = warn_on_object_refname_ambiguity;
846 warn_on_object_refname_ambiguity = 0;
848 if (opt->batch_mode == BATCH_MODE_QUEUE_AND_DISPATCH) {
849 batch_objects_command(opt, &output, &data);
850 goto cleanup;
853 while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
854 if (data.split_on_whitespace) {
856 * Split at first whitespace, tying off the beginning
857 * of the string and saving the remainder (or NULL) in
858 * data.rest.
860 char *p = strpbrk(input.buf, " \t");
861 if (p) {
862 while (*p && strchr(" \t", *p))
863 *p++ = '\0';
865 data.rest = p;
868 batch_one_object(input.buf, &output, opt, &data);
871 cleanup:
872 strbuf_release(&input);
873 strbuf_release(&output);
874 warn_on_object_refname_ambiguity = save_warning;
875 return retval;
878 static int git_cat_file_config(const char *var, const char *value,
879 const struct config_context *ctx, void *cb)
881 if (userdiff_config(var, value) < 0)
882 return -1;
884 return git_default_config(var, value, ctx, cb);
887 static int batch_option_callback(const struct option *opt,
888 const char *arg,
889 int unset)
891 struct batch_options *bo = opt->value;
893 BUG_ON_OPT_NEG(unset);
895 if (bo->enabled) {
896 return error(_("only one batch option may be specified"));
899 bo->enabled = 1;
901 if (!strcmp(opt->long_name, "batch"))
902 bo->batch_mode = BATCH_MODE_CONTENTS;
903 else if (!strcmp(opt->long_name, "batch-check"))
904 bo->batch_mode = BATCH_MODE_INFO;
905 else if (!strcmp(opt->long_name, "batch-command"))
906 bo->batch_mode = BATCH_MODE_QUEUE_AND_DISPATCH;
907 else
908 BUG("%s given to batch-option-callback", opt->long_name);
910 bo->format = arg;
912 return 0;
915 int cmd_cat_file(int argc, const char **argv, const char *prefix)
917 int opt = 0;
918 int opt_cw = 0;
919 int opt_epts = 0;
920 const char *exp_type = NULL, *obj_name = NULL;
921 struct batch_options batch = {0};
922 int unknown_type = 0;
923 int input_nul_terminated = 0;
924 int nul_terminated = 0;
926 const char * const usage[] = {
927 N_("git cat-file <type> <object>"),
928 N_("git cat-file (-e | -p) <object>"),
929 N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"),
930 N_("git cat-file (--textconv | --filters)\n"
931 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"),
932 N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
933 " [--buffer] [--follow-symlinks] [--unordered]\n"
934 " [--textconv | --filters] [-Z]"),
935 NULL
937 const struct option options[] = {
938 /* Simple queries */
939 OPT_GROUP(N_("Check object existence or emit object contents")),
940 OPT_CMDMODE('e', NULL, &opt,
941 N_("check if <object> exists"), 'e'),
942 OPT_CMDMODE('p', NULL, &opt, N_("pretty-print <object> content"), 'p'),
944 OPT_GROUP(N_("Emit [broken] object attributes")),
945 OPT_CMDMODE('t', NULL, &opt, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'),
946 OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
947 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
948 N_("allow -s and -t to work with broken/corrupt objects")),
949 OPT_BOOL(0, "use-mailmap", &use_mailmap, N_("use mail map file")),
950 OPT_ALIAS(0, "mailmap", "use-mailmap"),
951 /* Batch mode */
952 OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")),
953 OPT_CALLBACK_F(0, "batch", &batch, N_("format"),
954 N_("show full <object> or <rev> contents"),
955 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
956 batch_option_callback),
957 OPT_CALLBACK_F(0, "batch-check", &batch, N_("format"),
958 N_("like --batch, but don't emit <contents>"),
959 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
960 batch_option_callback),
961 OPT_BOOL_F('z', NULL, &input_nul_terminated, N_("stdin is NUL-terminated"),
962 PARSE_OPT_HIDDEN),
963 OPT_BOOL('Z', NULL, &nul_terminated, N_("stdin and stdout is NUL-terminated")),
964 OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
965 N_("read commands from stdin"),
966 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
967 batch_option_callback),
968 OPT_CMDMODE(0, "batch-all-objects", &opt,
969 N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'),
970 /* Batch-specific options */
971 OPT_GROUP(N_("Change or optimize batch output")),
972 OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
973 OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
974 N_("follow in-tree symlinks")),
975 OPT_BOOL(0, "unordered", &batch.unordered,
976 N_("do not order objects before emitting them")),
977 /* Textconv options, stand-ole*/
978 OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")),
979 OPT_CMDMODE(0, "textconv", &opt,
980 N_("run textconv on object's content"), 'c'),
981 OPT_CMDMODE(0, "filters", &opt,
982 N_("run filters on object's content"), 'w'),
983 OPT_STRING(0, "path", &force_path, N_("blob|tree"),
984 N_("use a <path> for (--textconv | --filters); Not with 'batch'")),
985 OPT_END()
988 git_config(git_cat_file_config, NULL);
990 batch.buffer_output = -1;
992 argc = parse_options(argc, argv, prefix, options, usage, 0);
993 opt_cw = (opt == 'c' || opt == 'w');
994 opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's');
996 if (use_mailmap)
997 read_mailmap(&mailmap);
999 /* --batch-all-objects? */
1000 if (opt == 'b')
1001 batch.all_objects = 1;
1003 /* Option compatibility */
1004 if (force_path && !opt_cw)
1005 usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
1006 usage, options,
1007 "--path", _("path|tree-ish"), "--filters",
1008 "--textconv");
1010 /* Option compatibility with batch mode */
1011 if (batch.enabled)
1013 else if (batch.follow_symlinks)
1014 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1015 "--follow-symlinks");
1016 else if (batch.buffer_output >= 0)
1017 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1018 "--buffer");
1019 else if (batch.all_objects)
1020 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1021 "--batch-all-objects");
1022 else if (input_nul_terminated)
1023 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1024 "-z");
1025 else if (nul_terminated)
1026 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1027 "-Z");
1029 batch.input_delim = batch.output_delim = '\n';
1030 if (input_nul_terminated)
1031 batch.input_delim = '\0';
1032 if (nul_terminated)
1033 batch.input_delim = batch.output_delim = '\0';
1035 /* Batch defaults */
1036 if (batch.buffer_output < 0)
1037 batch.buffer_output = batch.all_objects;
1039 /* Return early if we're in batch mode? */
1040 if (batch.enabled) {
1041 if (opt_cw)
1042 batch.transform_mode = opt;
1043 else if (opt && opt != 'b')
1044 usage_msg_optf(_("'-%c' is incompatible with batch mode"),
1045 usage, options, opt);
1046 else if (argc)
1047 usage_msg_opt(_("batch modes take no arguments"), usage,
1048 options);
1050 return batch_objects(&batch);
1053 if (opt) {
1054 if (!argc && opt == 'c')
1055 usage_msg_optf(_("<rev> required with '%s'"),
1056 usage, options, "--textconv");
1057 else if (!argc && opt == 'w')
1058 usage_msg_optf(_("<rev> required with '%s'"),
1059 usage, options, "--filters");
1060 else if (!argc && opt_epts)
1061 usage_msg_optf(_("<object> required with '-%c'"),
1062 usage, options, opt);
1063 else if (argc == 1)
1064 obj_name = argv[0];
1065 else
1066 usage_msg_opt(_("too many arguments"), usage, options);
1067 } else if (!argc) {
1068 usage_with_options(usage, options);
1069 } else if (argc != 2) {
1070 usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"),
1071 usage, options, argc);
1072 } else if (argc) {
1073 exp_type = argv[0];
1074 obj_name = argv[1];
1077 if (unknown_type && opt != 't' && opt != 's')
1078 die("git cat-file --allow-unknown-type: use with -s or -t");
1079 return cat_one_file(opt, exp_type, obj_name, unknown_type);