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 "oid-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 struct checkout_metadata meta
;
47 init_checkout_metadata(&meta
, NULL
, NULL
, oid
);
48 if (convert_to_working_tree(&the_index
, path
, *buf
, *size
, &strbuf
, &meta
)) {
51 *buf
= strbuf_detach(&strbuf
, NULL
);
58 static int stream_blob(const struct object_id
*oid
)
60 if (stream_blob_to_fd(1, oid
, NULL
, 0))
61 die("unable to stream %s to stdout", oid_to_hex(oid
));
65 static int cat_one_file(int opt
, const char *exp_type
, const char *obj_name
,
69 enum object_type type
;
72 struct object_context obj_context
;
73 struct object_info oi
= OBJECT_INFO_INIT
;
74 struct strbuf sb
= STRBUF_INIT
;
75 unsigned flags
= OBJECT_INFO_LOOKUP_REPLACE
;
76 const char *path
= force_path
;
79 flags
|= OBJECT_INFO_ALLOW_UNKNOWN_TYPE
;
81 if (get_oid_with_context(the_repository
, obj_name
,
84 die("Not a valid object name %s", obj_name
);
87 path
= obj_context
.path
;
88 if (obj_context
.mode
== S_IFINVALID
)
89 obj_context
.mode
= 0100644;
95 if (oid_object_info_extended(the_repository
, &oid
, &oi
, flags
) < 0)
96 die("git cat-file: could not get object info");
98 printf("%s\n", sb
.buf
);
106 if (oid_object_info_extended(the_repository
, &oid
, &oi
, flags
) < 0)
107 die("git cat-file: could not get object info");
108 printf("%"PRIuMAX
"\n", (uintmax_t)size
);
112 return !has_object_file(&oid
);
116 die("git cat-file --filters %s: <object> must be "
117 "<sha1:path>", obj_name
);
119 if (filter_object(path
, obj_context
.mode
,
126 die("git cat-file --textconv %s: <object> must be <sha1:path>",
129 if (textconv_object(the_repository
, path
, obj_context
.mode
,
130 &oid
, 1, &buf
, &size
))
132 /* else fallthrough */
135 type
= oid_object_info(the_repository
, &oid
, NULL
);
137 die("Not a valid object name %s", obj_name
);
139 /* custom pretty-print here */
140 if (type
== OBJ_TREE
) {
141 const char *ls_args
[3] = { NULL
};
142 ls_args
[0] = "ls-tree";
143 ls_args
[1] = obj_name
;
144 return cmd_ls_tree(2, ls_args
, NULL
);
147 if (type
== OBJ_BLOB
)
148 return stream_blob(&oid
);
149 buf
= read_object_file(&oid
, &type
, &size
);
151 die("Cannot read object %s", obj_name
);
153 /* otherwise just spit out the data */
157 if (type_from_string(exp_type
) == OBJ_BLOB
) {
158 struct object_id blob_oid
;
159 if (oid_object_info(the_repository
, &oid
, NULL
) == OBJ_TAG
) {
160 char *buffer
= read_object_file(&oid
, &type
,
163 if (!skip_prefix(buffer
, "object ", &target
) ||
164 get_oid_hex(target
, &blob_oid
))
165 die("%s not a valid tag", oid_to_hex(&oid
));
168 oidcpy(&blob_oid
, &oid
);
170 if (oid_object_info(the_repository
, &blob_oid
, NULL
) == OBJ_BLOB
)
171 return stream_blob(&blob_oid
);
173 * we attempted to dereference a tag to a blob
174 * and failed; there may be new dereference
175 * mechanisms this code is not aware of.
176 * fall-back to the usual case.
179 buf
= read_object_with_reference(the_repository
,
180 &oid
, exp_type
, &size
, NULL
);
184 die("git cat-file: unknown option: %s", exp_type
);
188 die("git cat-file %s: bad file", obj_name
);
190 write_or_die(1, buf
, size
);
192 free(obj_context
.path
);
197 struct object_id oid
;
198 enum object_type type
;
202 struct object_id delta_base_oid
;
205 * If mark_query is true, we do not expand anything, but rather
206 * just mark the object_info with items we wish to query.
211 * Whether to split the input on whitespace before feeding it to
212 * get_sha1; this is decided during the mark_query phase based on
213 * whether we have a %(rest) token in our format.
215 int split_on_whitespace
;
218 * After a mark_query run, this object_info is set up to be
219 * passed to oid_object_info_extended. It will point to the data
220 * elements above, so you can retrieve the response from there.
222 struct object_info info
;
225 * This flag will be true if the requested batch format and options
226 * don't require us to call oid_object_info, which can then be
229 unsigned skip_object_info
: 1;
232 static int is_atom(const char *atom
, const char *s
, int slen
)
234 int alen
= strlen(atom
);
235 return alen
== slen
&& !memcmp(atom
, s
, alen
);
238 static void expand_atom(struct strbuf
*sb
, const char *atom
, int len
,
241 struct expand_data
*data
= vdata
;
243 if (is_atom("objectname", atom
, len
)) {
244 if (!data
->mark_query
)
245 strbuf_addstr(sb
, oid_to_hex(&data
->oid
));
246 } else if (is_atom("objecttype", atom
, len
)) {
247 if (data
->mark_query
)
248 data
->info
.typep
= &data
->type
;
250 strbuf_addstr(sb
, type_name(data
->type
));
251 } else if (is_atom("objectsize", atom
, len
)) {
252 if (data
->mark_query
)
253 data
->info
.sizep
= &data
->size
;
255 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->size
);
256 } else if (is_atom("objectsize:disk", atom
, len
)) {
257 if (data
->mark_query
)
258 data
->info
.disk_sizep
= &data
->disk_size
;
260 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->disk_size
);
261 } else if (is_atom("rest", atom
, len
)) {
262 if (data
->mark_query
)
263 data
->split_on_whitespace
= 1;
265 strbuf_addstr(sb
, data
->rest
);
266 } else if (is_atom("deltabase", atom
, len
)) {
267 if (data
->mark_query
)
268 data
->info
.delta_base_oid
= &data
->delta_base_oid
;
271 oid_to_hex(&data
->delta_base_oid
));
273 die("unknown format element: %.*s", len
, atom
);
276 static size_t expand_format(struct strbuf
*sb
, const char *start
, void *data
)
282 end
= strchr(start
+ 1, ')');
284 die("format element '%s' does not end in ')'", start
);
286 expand_atom(sb
, start
+ 1, end
- start
- 1, data
);
288 return end
- start
+ 1;
291 static void batch_write(struct batch_options
*opt
, const void *data
, int len
)
293 if (opt
->buffer_output
) {
294 if (fwrite(data
, 1, len
, stdout
) != len
)
295 die_errno("unable to write to stdout");
297 write_or_die(1, data
, len
);
300 static void print_object_or_die(struct batch_options
*opt
, struct expand_data
*data
)
302 const struct object_id
*oid
= &data
->oid
;
304 assert(data
->info
.typep
);
306 if (data
->type
== OBJ_BLOB
) {
307 if (opt
->buffer_output
)
314 die("missing path for '%s'", oid_to_hex(oid
));
316 if (opt
->cmdmode
== 'w') {
317 if (filter_object(data
->rest
, 0100644, oid
,
319 die("could not convert '%s' %s",
320 oid_to_hex(oid
), data
->rest
);
321 } else if (opt
->cmdmode
== 'c') {
322 enum object_type type
;
323 if (!textconv_object(the_repository
,
324 data
->rest
, 0100644, oid
,
325 1, &contents
, &size
))
326 contents
= read_object_file(oid
,
330 die("could not convert '%s' %s",
331 oid_to_hex(oid
), data
->rest
);
333 BUG("invalid cmdmode: %c", opt
->cmdmode
);
334 batch_write(opt
, contents
, size
);
341 enum object_type type
;
345 contents
= read_object_file(oid
, &type
, &size
);
347 die("object %s disappeared", oid_to_hex(oid
));
348 if (type
!= data
->type
)
349 die("object %s changed type!?", oid_to_hex(oid
));
350 if (data
->info
.sizep
&& size
!= data
->size
)
351 die("object %s changed size!?", oid_to_hex(oid
));
353 batch_write(opt
, contents
, size
);
358 static void batch_object_write(const char *obj_name
,
359 struct strbuf
*scratch
,
360 struct batch_options
*opt
,
361 struct expand_data
*data
)
363 if (!data
->skip_object_info
&&
364 oid_object_info_extended(the_repository
, &data
->oid
, &data
->info
,
365 OBJECT_INFO_LOOKUP_REPLACE
) < 0) {
366 printf("%s missing\n",
367 obj_name
? obj_name
: oid_to_hex(&data
->oid
));
372 strbuf_reset(scratch
);
373 strbuf_expand(scratch
, opt
->format
, expand_format
, data
);
374 strbuf_addch(scratch
, '\n');
375 batch_write(opt
, scratch
->buf
, scratch
->len
);
377 if (opt
->print_contents
) {
378 print_object_or_die(opt
, data
);
379 batch_write(opt
, "\n", 1);
383 static void batch_one_object(const char *obj_name
,
384 struct strbuf
*scratch
,
385 struct batch_options
*opt
,
386 struct expand_data
*data
)
388 struct object_context ctx
;
389 int flags
= opt
->follow_symlinks
? GET_OID_FOLLOW_SYMLINKS
: 0;
390 enum get_oid_result result
;
392 result
= get_oid_with_context(the_repository
, obj_name
,
393 flags
, &data
->oid
, &ctx
);
394 if (result
!= FOUND
) {
397 printf("%s missing\n", obj_name
);
399 case SHORT_NAME_AMBIGUOUS
:
400 printf("%s ambiguous\n", obj_name
);
402 case DANGLING_SYMLINK
:
403 printf("dangling %"PRIuMAX
"\n%s\n",
404 (uintmax_t)strlen(obj_name
), obj_name
);
407 printf("loop %"PRIuMAX
"\n%s\n",
408 (uintmax_t)strlen(obj_name
), obj_name
);
411 printf("notdir %"PRIuMAX
"\n%s\n",
412 (uintmax_t)strlen(obj_name
), obj_name
);
415 BUG("unknown get_sha1_with_context result %d\n",
424 printf("symlink %"PRIuMAX
"\n%s\n",
425 (uintmax_t)ctx
.symlink_path
.len
,
426 ctx
.symlink_path
.buf
);
431 batch_object_write(obj_name
, scratch
, opt
, data
);
434 struct object_cb_data
{
435 struct batch_options
*opt
;
436 struct expand_data
*expand
;
438 struct strbuf
*scratch
;
441 static int batch_object_cb(const struct object_id
*oid
, void *vdata
)
443 struct object_cb_data
*data
= vdata
;
444 oidcpy(&data
->expand
->oid
, oid
);
445 batch_object_write(NULL
, data
->scratch
, data
->opt
, data
->expand
);
449 static int collect_loose_object(const struct object_id
*oid
,
453 oid_array_append(data
, oid
);
457 static int collect_packed_object(const struct object_id
*oid
,
458 struct packed_git
*pack
,
462 oid_array_append(data
, oid
);
466 static int batch_unordered_object(const struct object_id
*oid
, void *vdata
)
468 struct object_cb_data
*data
= vdata
;
470 if (oidset_insert(data
->seen
, oid
))
473 return batch_object_cb(oid
, data
);
476 static int batch_unordered_loose(const struct object_id
*oid
,
480 return batch_unordered_object(oid
, data
);
483 static int batch_unordered_packed(const struct object_id
*oid
,
484 struct packed_git
*pack
,
488 return batch_unordered_object(oid
, data
);
491 static int batch_objects(struct batch_options
*opt
)
493 struct strbuf input
= STRBUF_INIT
;
494 struct strbuf output
= STRBUF_INIT
;
495 struct expand_data data
;
500 opt
->format
= "%(objectname) %(objecttype) %(objectsize)";
503 * Expand once with our special mark_query flag, which will prime the
504 * object_info to be handed to oid_object_info_extended for each
507 memset(&data
, 0, sizeof(data
));
509 strbuf_expand(&output
, opt
->format
, expand_format
, &data
);
511 strbuf_release(&output
);
513 data
.split_on_whitespace
= 1;
515 if (opt
->all_objects
) {
516 struct object_info empty
= OBJECT_INFO_INIT
;
517 if (!memcmp(&data
.info
, &empty
, sizeof(empty
)))
518 data
.skip_object_info
= 1;
522 * If we are printing out the object, then always fill in the type,
523 * since we will want to decide whether or not to stream.
525 if (opt
->print_contents
)
526 data
.info
.typep
= &data
.type
;
528 if (opt
->all_objects
) {
529 struct object_cb_data cb
;
531 if (has_promisor_remote())
532 warning("This repository uses promisor remotes. Some objects may not be loaded.");
536 cb
.scratch
= &output
;
538 if (opt
->unordered
) {
539 struct oidset seen
= OIDSET_INIT
;
543 for_each_loose_object(batch_unordered_loose
, &cb
, 0);
544 for_each_packed_object(batch_unordered_packed
, &cb
,
545 FOR_EACH_OBJECT_PACK_ORDER
);
549 struct oid_array sa
= OID_ARRAY_INIT
;
551 for_each_loose_object(collect_loose_object
, &sa
, 0);
552 for_each_packed_object(collect_packed_object
, &sa
, 0);
554 oid_array_for_each_unique(&sa
, batch_object_cb
, &cb
);
556 oid_array_clear(&sa
);
559 strbuf_release(&output
);
564 * We are going to call get_sha1 on a potentially very large number of
565 * objects. In most large cases, these will be actual object sha1s. The
566 * cost to double-check that each one is not also a ref (just so we can
567 * warn) ends up dwarfing the actual cost of the object lookups
568 * themselves. We can work around it by just turning off the warning.
570 save_warning
= warn_on_object_refname_ambiguity
;
571 warn_on_object_refname_ambiguity
= 0;
573 while (strbuf_getline(&input
, stdin
) != EOF
) {
574 if (data
.split_on_whitespace
) {
576 * Split at first whitespace, tying off the beginning
577 * of the string and saving the remainder (or NULL) in
580 char *p
= strpbrk(input
.buf
, " \t");
582 while (*p
&& strchr(" \t", *p
))
588 batch_one_object(input
.buf
, &output
, opt
, &data
);
591 strbuf_release(&input
);
592 strbuf_release(&output
);
593 warn_on_object_refname_ambiguity
= save_warning
;
597 static const char * const cat_file_usage
[] = {
598 N_("git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | <type> | --textconv | --filters) [--path=<path>] <object>"),
599 N_("git cat-file (--batch | --batch-check) [--follow-symlinks] [--textconv | --filters]"),
603 static int git_cat_file_config(const char *var
, const char *value
, void *cb
)
605 if (userdiff_config(var
, value
) < 0)
608 return git_default_config(var
, value
, cb
);
611 static int batch_option_callback(const struct option
*opt
,
615 struct batch_options
*bo
= opt
->value
;
617 BUG_ON_OPT_NEG(unset
);
620 return error(_("only one batch option may be specified"));
624 bo
->print_contents
= !strcmp(opt
->long_name
, "batch");
630 int cmd_cat_file(int argc
, const char **argv
, const char *prefix
)
633 const char *exp_type
= NULL
, *obj_name
= NULL
;
634 struct batch_options batch
= {0};
635 int unknown_type
= 0;
637 const struct option options
[] = {
638 OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")),
639 OPT_CMDMODE('t', NULL
, &opt
, N_("show object type"), 't'),
640 OPT_CMDMODE('s', NULL
, &opt
, N_("show object size"), 's'),
641 OPT_CMDMODE('e', NULL
, &opt
,
642 N_("exit with zero when there's no error"), 'e'),
643 OPT_CMDMODE('p', NULL
, &opt
, N_("pretty-print object's content"), 'p'),
644 OPT_CMDMODE(0, "textconv", &opt
,
645 N_("for blob objects, run textconv on object's content"), 'c'),
646 OPT_CMDMODE(0, "filters", &opt
,
647 N_("for blob objects, run filters on object's content"), 'w'),
648 OPT_STRING(0, "path", &force_path
, N_("blob"),
649 N_("use a specific path for --textconv/--filters")),
650 OPT_BOOL(0, "allow-unknown-type", &unknown_type
,
651 N_("allow -s and -t to work with broken/corrupt objects")),
652 OPT_BOOL(0, "buffer", &batch
.buffer_output
, N_("buffer --batch output")),
653 OPT_CALLBACK_F(0, "batch", &batch
, "format",
654 N_("show info and content of objects fed from the standard input"),
655 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
656 batch_option_callback
),
657 OPT_CALLBACK_F(0, "batch-check", &batch
, "format",
658 N_("show info about objects fed from the standard input"),
659 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
660 batch_option_callback
),
661 OPT_BOOL(0, "follow-symlinks", &batch
.follow_symlinks
,
662 N_("follow in-tree symlinks (used with --batch or --batch-check)")),
663 OPT_BOOL(0, "batch-all-objects", &batch
.all_objects
,
664 N_("show all objects with --batch or --batch-check")),
665 OPT_BOOL(0, "unordered", &batch
.unordered
,
666 N_("do not order --batch-all-objects output")),
670 git_config(git_cat_file_config
, NULL
);
672 batch
.buffer_output
= -1;
673 argc
= parse_options(argc
, argv
, prefix
, options
, cat_file_usage
, 0);
676 if (batch
.enabled
&& (opt
== 'c' || opt
== 'w'))
681 usage_with_options(cat_file_usage
, options
);
683 if (!opt
&& !batch
.enabled
) {
688 usage_with_options(cat_file_usage
, options
);
691 if (batch
.cmdmode
!= opt
|| argc
)
692 usage_with_options(cat_file_usage
, options
);
693 if (batch
.cmdmode
&& batch
.all_objects
)
694 die("--batch-all-objects cannot be combined with "
695 "--textconv nor with --filters");
698 if ((batch
.follow_symlinks
|| batch
.all_objects
) && !batch
.enabled
) {
699 usage_with_options(cat_file_usage
, options
);
702 if (force_path
&& opt
!= 'c' && opt
!= 'w') {
703 error("--path=<path> needs --textconv or --filters");
704 usage_with_options(cat_file_usage
, options
);
707 if (force_path
&& batch
.enabled
) {
708 error("--path=<path> incompatible with --batch");
709 usage_with_options(cat_file_usage
, options
);
712 if (batch
.buffer_output
< 0)
713 batch
.buffer_output
= batch
.all_objects
;
716 return batch_objects(&batch
);
718 if (unknown_type
&& opt
!= 't' && opt
!= 's')
719 die("git cat-file --allow-unknown-type: use with -s or -t");
720 return cat_one_file(opt
, exp_type
, obj_name
, unknown_type
);