2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
11 #include "parse-options.h"
13 #include "streaming.h"
14 #include "tree-walk.h"
15 #include "sha1-array.h"
17 #include "object-store.h"
18 #include "promisor-remote.h"
20 struct batch_options {
27 int cmdmode; /* may be 'w' or 'c' for --filters or --textconv */
31 static const char *force_path;
33 static int filter_object(const char *path, unsigned mode,
34 const struct object_id *oid,
35 char **buf, unsigned long *size)
37 enum object_type type;
39 *buf = read_object_file(oid, &type, size);
41 return error(_("cannot read object %s '%s'"),
42 oid_to_hex(oid), path);
43 if ((type == OBJ_BLOB) && S_ISREG(mode)) {
44 struct strbuf strbuf = STRBUF_INIT;
45 if (convert_to_working_tree(&the_index, path, *buf, *size, &strbuf)) {
48 *buf = strbuf_detach(&strbuf, NULL);
55 static int stream_blob(const struct object_id *oid)
57 if (stream_blob_to_fd(1, oid, NULL, 0))
58 die("unable to stream %s to stdout", oid_to_hex(oid));
62 static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
66 enum object_type type;
69 struct object_context obj_context;
70 struct object_info oi = OBJECT_INFO_INIT;
71 struct strbuf sb = STRBUF_INIT;
72 unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
73 const char *path = force_path;
76 flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
78 if (get_oid_with_context(the_repository, obj_name,
81 die("Not a valid object name %s", obj_name);
84 path = obj_context.path;
85 if (obj_context.mode == S_IFINVALID)
86 obj_context.mode = 0100644;
92 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
93 die("git cat-file: could not get object info");
95 printf("%s\n", sb.buf);
103 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
104 die("git cat-file: could not get object info");
105 printf("%"PRIuMAX"\n", (uintmax_t)size);
109 return !has_object_file(&oid);
113 die("git cat-file --filters %s: <object> must be "
114 "<sha1:path>", obj_name);
116 if (filter_object(path, obj_context.mode,
123 die("git cat-file --textconv %s: <object> must be <sha1:path>",
126 if (textconv_object(the_repository, path, obj_context.mode,
127 &oid, 1, &buf, &size))
129 /* else fallthrough */
132 type = oid_object_info(the_repository, &oid, NULL);
134 die("Not a valid object name %s", obj_name);
136 /* custom pretty-print here */
137 if (type == OBJ_TREE) {
138 const char *ls_args[3] = { NULL };
139 ls_args[0] = "ls-tree";
140 ls_args[1] = obj_name;
141 return cmd_ls_tree(2, ls_args, NULL);
144 if (type == OBJ_BLOB)
145 return stream_blob(&oid);
146 buf = read_object_file(&oid, &type, &size);
148 die("Cannot read object %s", obj_name);
150 /* otherwise just spit out the data */
154 if (type_from_string(exp_type) == OBJ_BLOB) {
155 struct object_id blob_oid;
156 if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
157 char *buffer = read_object_file(&oid, &type,
160 if (!skip_prefix(buffer, "object ", &target) ||
161 get_oid_hex(target, &blob_oid))
162 die("%s not a valid tag", oid_to_hex(&oid));
165 oidcpy(&blob_oid, &oid);
167 if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB)
168 return stream_blob(&blob_oid);
170 * we attempted to dereference a tag to a blob
171 * and failed; there may be new dereference
172 * mechanisms this code is not aware of.
173 * fall-back to the usual case.
176 buf = read_object_with_reference(the_repository,
177 &oid, exp_type, &size, NULL);
181 die("git cat-file: unknown option: %s", exp_type);
185 die("git cat-file %s: bad file", obj_name);
187 write_or_die(1, buf, size);
189 free(obj_context.path);
194 struct object_id oid;
195 enum object_type type;
199 struct object_id delta_base_oid;
202 * If mark_query is true, we do not expand anything, but rather
203 * just mark the object_info with items we wish to query.
208 * Whether to split the input on whitespace before feeding it to
209 * get_sha1; this is decided during the mark_query phase based on
210 * whether we have a %(rest) token in our format.
212 int split_on_whitespace;
215 * After a mark_query run, this object_info is set up to be
216 * passed to oid_object_info_extended. It will point to the data
217 * elements above, so you can retrieve the response from there.
219 struct object_info info;
222 * This flag will be true if the requested batch format and options
223 * don't require us to call oid_object_info, which can then be
226 unsigned skip_object_info : 1;
229 static int is_atom(const char *atom, const char *s, int slen)
231 int alen = strlen(atom);
232 return alen == slen && !memcmp(atom, s, alen);
235 static void expand_atom(struct strbuf *sb, const char *atom, int len,
238 struct expand_data *data = vdata;
240 if (is_atom("objectname", atom, len)) {
241 if (!data->mark_query)
242 strbuf_addstr(sb, oid_to_hex(&data->oid));
243 } else if (is_atom("objecttype", atom, len)) {
244 if (data->mark_query)
245 data->info.typep = &data->type;
247 strbuf_addstr(sb, type_name(data->type));
248 } else if (is_atom("objectsize", atom, len)) {
249 if (data->mark_query)
250 data->info.sizep = &data->size;
252 strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
253 } else if (is_atom("objectsize:disk", atom, len)) {
254 if (data->mark_query)
255 data->info.disk_sizep = &data->disk_size;
257 strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
258 } else if (is_atom("rest", atom, len)) {
259 if (data->mark_query)
260 data->split_on_whitespace = 1;
262 strbuf_addstr(sb, data->rest);
263 } else if (is_atom("deltabase", atom, len)) {
264 if (data->mark_query)
265 data->info.delta_base_sha1 = data->delta_base_oid.hash;
268 oid_to_hex(&data->delta_base_oid));
270 die("unknown format element: %.*s", len, atom);
273 static size_t expand_format(struct strbuf *sb, const char *start, void *data)
279 end = strchr(start + 1, ')');
281 die("format element '%s' does not end in ')'", start);
283 expand_atom(sb, start + 1, end - start - 1, data);
285 return end - start + 1;
288 static void batch_write(struct batch_options *opt, const void *data, int len)
290 if (opt->buffer_output) {
291 if (fwrite(data, 1, len, stdout) != len)
292 die_errno("unable to write to stdout");
294 write_or_die(1, data, len);
297 static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
299 const struct object_id *oid = &data->oid;
301 assert(data->info.typep);
303 if (data->type == OBJ_BLOB) {
304 if (opt->buffer_output)
311 die("missing path for '%s'", oid_to_hex(oid));
313 if (opt->cmdmode == 'w') {
314 if (filter_object(data->rest, 0100644, oid,
316 die("could not convert '%s' %s",
317 oid_to_hex(oid), data->rest);
318 } else if (opt->cmdmode == 'c') {
319 enum object_type type;
320 if (!textconv_object(the_repository,
321 data->rest, 0100644, oid,
322 1, &contents, &size))
323 contents = read_object_file(oid,
327 die("could not convert '%s' %s",
328 oid_to_hex(oid), data->rest);
330 BUG("invalid cmdmode: %c", opt->cmdmode);
331 batch_write(opt, contents, size);
338 enum object_type type;
342 contents = read_object_file(oid, &type, &size);
344 die("object %s disappeared", oid_to_hex(oid));
345 if (type != data->type)
346 die("object %s changed type!?", oid_to_hex(oid));
347 if (data->info.sizep && size != data->size)
348 die("object %s changed size!?", oid_to_hex(oid));
350 batch_write(opt, contents, size);
355 static void batch_object_write(const char *obj_name,
356 struct strbuf *scratch,
357 struct batch_options *opt,
358 struct expand_data *data)
360 if (!data->skip_object_info &&
361 oid_object_info_extended(the_repository, &data->oid, &data->info,
362 OBJECT_INFO_LOOKUP_REPLACE) < 0) {
363 printf("%s missing\n",
364 obj_name ? obj_name : oid_to_hex(&data->oid));
369 strbuf_reset(scratch);
370 strbuf_expand(scratch, opt->format, expand_format, data);
371 strbuf_addch(scratch, '\n');
372 batch_write(opt, scratch->buf, scratch->len);
374 if (opt->print_contents) {
375 print_object_or_die(opt, data);
376 batch_write(opt, "\n", 1);
380 static void batch_one_object(const char *obj_name,
381 struct strbuf *scratch,
382 struct batch_options *opt,
383 struct expand_data *data)
385 struct object_context ctx;
386 int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
387 enum get_oid_result result;
389 result = get_oid_with_context(the_repository, obj_name,
390 flags, &data->oid, &ctx);
391 if (result != FOUND) {
394 printf("%s missing\n", obj_name);
396 case SHORT_NAME_AMBIGUOUS:
397 printf("%s ambiguous\n", obj_name);
399 case DANGLING_SYMLINK:
400 printf("dangling %"PRIuMAX"\n%s\n",
401 (uintmax_t)strlen(obj_name), obj_name);
404 printf("loop %"PRIuMAX"\n%s\n",
405 (uintmax_t)strlen(obj_name), obj_name);
408 printf("notdir %"PRIuMAX"\n%s\n",
409 (uintmax_t)strlen(obj_name), obj_name);
412 BUG("unknown get_sha1_with_context result %d\n",
421 printf("symlink %"PRIuMAX"\n%s\n",
422 (uintmax_t)ctx.symlink_path.len,
423 ctx.symlink_path.buf);
428 batch_object_write(obj_name, scratch, opt, data);
431 struct object_cb_data {
432 struct batch_options *opt;
433 struct expand_data *expand;
435 struct strbuf *scratch;
438 static int batch_object_cb(const struct object_id *oid, void *vdata)
440 struct object_cb_data *data = vdata;
441 oidcpy(&data->expand->oid, oid);
442 batch_object_write(NULL, data->scratch, data->opt, data->expand);
446 static int collect_loose_object(const struct object_id *oid,
450 oid_array_append(data, oid);
454 static int collect_packed_object(const struct object_id *oid,
455 struct packed_git *pack,
459 oid_array_append(data, oid);
463 static int batch_unordered_object(const struct object_id *oid, void *vdata)
465 struct object_cb_data *data = vdata;
467 if (oidset_insert(data->seen, oid))
470 return batch_object_cb(oid, data);
473 static int batch_unordered_loose(const struct object_id *oid,
477 return batch_unordered_object(oid, data);
480 static int batch_unordered_packed(const struct object_id *oid,
481 struct packed_git *pack,
485 return batch_unordered_object(oid, data);
488 static int batch_objects(struct batch_options *opt)
490 struct strbuf input = STRBUF_INIT;
491 struct strbuf output = STRBUF_INIT;
492 struct expand_data data;
497 opt->format = "%(objectname) %(objecttype) %(objectsize)";
500 * Expand once with our special mark_query flag, which will prime the
501 * object_info to be handed to oid_object_info_extended for each
504 memset(&data, 0, sizeof(data));
506 strbuf_expand(&output, opt->format, expand_format, &data);
508 strbuf_release(&output);
510 data.split_on_whitespace = 1;
512 if (opt->all_objects) {
513 struct object_info empty = OBJECT_INFO_INIT;
514 if (!memcmp(&data.info, &empty, sizeof(empty)))
515 data.skip_object_info = 1;
519 * If we are printing out the object, then always fill in the type,
520 * since we will want to decide whether or not to stream.
522 if (opt->print_contents)
523 data.info.typep = &data.type;
525 if (opt->all_objects) {
526 struct object_cb_data cb;
528 if (has_promisor_remote())
529 warning("This repository uses promisor remotes. Some objects may not be loaded.");
533 cb.scratch = &output;
535 if (opt->unordered) {
536 struct oidset seen = OIDSET_INIT;
540 for_each_loose_object(batch_unordered_loose, &cb, 0);
541 for_each_packed_object(batch_unordered_packed, &cb,
542 FOR_EACH_OBJECT_PACK_ORDER);
546 struct oid_array sa = OID_ARRAY_INIT;
548 for_each_loose_object(collect_loose_object, &sa, 0);
549 for_each_packed_object(collect_packed_object, &sa, 0);
551 oid_array_for_each_unique(&sa, batch_object_cb, &cb);
553 oid_array_clear(&sa);
556 strbuf_release(&output);
561 * We are going to call get_sha1 on a potentially very large number of
562 * objects. In most large cases, these will be actual object sha1s. The
563 * cost to double-check that each one is not also a ref (just so we can
564 * warn) ends up dwarfing the actual cost of the object lookups
565 * themselves. We can work around it by just turning off the warning.
567 save_warning = warn_on_object_refname_ambiguity;
568 warn_on_object_refname_ambiguity = 0;
570 while (strbuf_getline(&input, stdin) != EOF) {
571 if (data.split_on_whitespace) {
573 * Split at first whitespace, tying off the beginning
574 * of the string and saving the remainder (or NULL) in
577 char *p = strpbrk(input.buf, " \t");
579 while (*p && strchr(" \t", *p))
585 batch_one_object(input.buf, &output, opt, &data);
588 strbuf_release(&input);
589 strbuf_release(&output);
590 warn_on_object_refname_ambiguity = save_warning;
594 static const char * const cat_file_usage[] = {
595 N_("git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | <type> | --textconv | --filters) [--path=<path>] <object>"),
596 N_("git cat-file (--batch | --batch-check) [--follow-symlinks] [--textconv | --filters]"),
600 static int git_cat_file_config(const char *var, const char *value, void *cb)
602 if (userdiff_config(var, value) < 0)
605 return git_default_config(var, value, cb);
608 static int batch_option_callback(const struct option *opt,
612 struct batch_options *bo = opt->value;
614 BUG_ON_OPT_NEG(unset);
617 return error(_("only one batch option may be specified"));
621 bo->print_contents = !strcmp(opt->long_name, "batch");
627 int cmd_cat_file(int argc, const char **argv, const char *prefix)
630 const char *exp_type = NULL, *obj_name = NULL;
631 struct batch_options batch = {0};
632 int unknown_type = 0;
634 const struct option options[] = {
635 OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")),
636 OPT_CMDMODE('t', NULL, &opt, N_("show object type"), 't'),
637 OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
638 OPT_CMDMODE('e', NULL, &opt,
639 N_("exit with zero when there's no error"), 'e'),
640 OPT_CMDMODE('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
641 OPT_CMDMODE(0, "textconv", &opt,
642 N_("for blob objects, run textconv on object's content"), 'c'),
643 OPT_CMDMODE(0, "filters", &opt,
644 N_("for blob objects, run filters on object's content"), 'w'),
645 OPT_STRING(0, "path", &force_path, N_("blob"),
646 N_("use a specific path for --textconv/--filters")),
647 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
648 N_("allow -s and -t to work with broken/corrupt objects")),
649 OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
650 { OPTION_CALLBACK, 0, "batch", &batch, "format",
651 N_("show info and content of objects fed from the standard input"),
652 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
653 batch_option_callback },
654 { OPTION_CALLBACK, 0, "batch-check", &batch, "format",
655 N_("show info about objects fed from the standard input"),
656 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
657 batch_option_callback },
658 OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
659 N_("follow in-tree symlinks (used with --batch or --batch-check)")),
660 OPT_BOOL(0, "batch-all-objects", &batch.all_objects,
661 N_("show all objects with --batch or --batch-check")),
662 OPT_BOOL(0, "unordered", &batch.unordered,
663 N_("do not order --batch-all-objects output")),
667 git_config(git_cat_file_config, NULL);
669 batch.buffer_output = -1;
670 argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);
673 if (batch.enabled && (opt == 'c' || opt == 'w'))
678 usage_with_options(cat_file_usage, options);
680 if (!opt && !batch.enabled) {
685 usage_with_options(cat_file_usage, options);
688 if (batch.cmdmode != opt || argc)
689 usage_with_options(cat_file_usage, options);
690 if (batch.cmdmode && batch.all_objects)
691 die("--batch-all-objects cannot be combined with "
692 "--textconv nor with --filters");
695 if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) {
696 usage_with_options(cat_file_usage, options);
699 if (force_path && opt != 'c' && opt != 'w') {
700 error("--path=<path> needs --textconv or --filters");
701 usage_with_options(cat_file_usage, options);
704 if (force_path && batch.enabled) {
705 error("--path=<path> incompatible with --batch");
706 usage_with_options(cat_file_usage, options);
709 if (batch.buffer_output < 0)
710 batch.buffer_output = batch.all_objects;
713 return batch_objects(&batch);
715 if (unknown_type && opt != 't' && opt != 's')
716 die("git cat-file --allow-unknown-type: use with -s or -t");
717 return cat_one_file(opt, exp_type, obj_name, unknown_type);