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"
19 struct batch_options
{
26 int cmdmode
; /* may be 'w' or 'c' for --filters or --textconv */
30 static const char *force_path
;
32 static int filter_object(const char *path
, unsigned mode
,
33 const struct object_id
*oid
,
34 char **buf
, unsigned long *size
)
36 enum object_type type
;
38 *buf
= read_object_file(oid
, &type
, size
);
40 return error(_("cannot read object %s '%s'"),
41 oid_to_hex(oid
), path
);
42 if ((type
== OBJ_BLOB
) && S_ISREG(mode
)) {
43 struct strbuf strbuf
= STRBUF_INIT
;
44 if (convert_to_working_tree(&the_index
, path
, *buf
, *size
, &strbuf
)) {
47 *buf
= strbuf_detach(&strbuf
, NULL
);
54 static int stream_blob(const struct object_id
*oid
)
56 if (stream_blob_to_fd(1, oid
, NULL
, 0))
57 die("unable to stream %s to stdout", oid_to_hex(oid
));
61 static int cat_one_file(int opt
, const char *exp_type
, const char *obj_name
,
65 enum object_type type
;
68 struct object_context obj_context
;
69 struct object_info oi
= OBJECT_INFO_INIT
;
70 struct strbuf sb
= STRBUF_INIT
;
71 unsigned flags
= OBJECT_INFO_LOOKUP_REPLACE
;
72 const char *path
= force_path
;
75 flags
|= OBJECT_INFO_ALLOW_UNKNOWN_TYPE
;
77 if (get_oid_with_context(the_repository
, obj_name
,
80 die("Not a valid object name %s", obj_name
);
83 path
= obj_context
.path
;
84 if (obj_context
.mode
== S_IFINVALID
)
85 obj_context
.mode
= 0100644;
91 if (oid_object_info_extended(the_repository
, &oid
, &oi
, flags
) < 0)
92 die("git cat-file: could not get object info");
94 printf("%s\n", sb
.buf
);
102 if (oid_object_info_extended(the_repository
, &oid
, &oi
, flags
) < 0)
103 die("git cat-file: could not get object info");
104 printf("%"PRIuMAX
"\n", (uintmax_t)size
);
108 return !has_object_file(&oid
);
112 die("git cat-file --filters %s: <object> must be "
113 "<sha1:path>", obj_name
);
115 if (filter_object(path
, obj_context
.mode
,
122 die("git cat-file --textconv %s: <object> must be <sha1:path>",
125 if (textconv_object(the_repository
, path
, obj_context
.mode
,
126 &oid
, 1, &buf
, &size
))
128 /* else fallthrough */
131 type
= oid_object_info(the_repository
, &oid
, NULL
);
133 die("Not a valid object name %s", obj_name
);
135 /* custom pretty-print here */
136 if (type
== OBJ_TREE
) {
137 const char *ls_args
[3] = { NULL
};
138 ls_args
[0] = "ls-tree";
139 ls_args
[1] = obj_name
;
140 return cmd_ls_tree(2, ls_args
, NULL
);
143 if (type
== OBJ_BLOB
)
144 return stream_blob(&oid
);
145 buf
= read_object_file(&oid
, &type
, &size
);
147 die("Cannot read object %s", obj_name
);
149 /* otherwise just spit out the data */
153 if (type_from_string(exp_type
) == OBJ_BLOB
) {
154 struct object_id blob_oid
;
155 if (oid_object_info(the_repository
, &oid
, NULL
) == OBJ_TAG
) {
156 char *buffer
= read_object_file(&oid
, &type
,
159 if (!skip_prefix(buffer
, "object ", &target
) ||
160 get_oid_hex(target
, &blob_oid
))
161 die("%s not a valid tag", oid_to_hex(&oid
));
164 oidcpy(&blob_oid
, &oid
);
166 if (oid_object_info(the_repository
, &blob_oid
, NULL
) == OBJ_BLOB
)
167 return stream_blob(&blob_oid
);
169 * we attempted to dereference a tag to a blob
170 * and failed; there may be new dereference
171 * mechanisms this code is not aware of.
172 * fall-back to the usual case.
175 buf
= read_object_with_reference(&oid
, exp_type
, &size
, NULL
);
179 die("git cat-file: unknown option: %s", exp_type
);
183 die("git cat-file %s: bad file", obj_name
);
185 write_or_die(1, buf
, size
);
187 free(obj_context
.path
);
192 struct object_id oid
;
193 enum object_type type
;
197 struct object_id delta_base_oid
;
200 * If mark_query is true, we do not expand anything, but rather
201 * just mark the object_info with items we wish to query.
206 * Whether to split the input on whitespace before feeding it to
207 * get_sha1; this is decided during the mark_query phase based on
208 * whether we have a %(rest) token in our format.
210 int split_on_whitespace
;
213 * After a mark_query run, this object_info is set up to be
214 * passed to oid_object_info_extended. It will point to the data
215 * elements above, so you can retrieve the response from there.
217 struct object_info info
;
220 * This flag will be true if the requested batch format and options
221 * don't require us to call oid_object_info, which can then be
224 unsigned skip_object_info
: 1;
227 static int is_atom(const char *atom
, const char *s
, int slen
)
229 int alen
= strlen(atom
);
230 return alen
== slen
&& !memcmp(atom
, s
, alen
);
233 static void expand_atom(struct strbuf
*sb
, const char *atom
, int len
,
236 struct expand_data
*data
= vdata
;
238 if (is_atom("objectname", atom
, len
)) {
239 if (!data
->mark_query
)
240 strbuf_addstr(sb
, oid_to_hex(&data
->oid
));
241 } else if (is_atom("objecttype", atom
, len
)) {
242 if (data
->mark_query
)
243 data
->info
.typep
= &data
->type
;
245 strbuf_addstr(sb
, type_name(data
->type
));
246 } else if (is_atom("objectsize", atom
, len
)) {
247 if (data
->mark_query
)
248 data
->info
.sizep
= &data
->size
;
250 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->size
);
251 } else if (is_atom("objectsize:disk", atom
, len
)) {
252 if (data
->mark_query
)
253 data
->info
.disk_sizep
= &data
->disk_size
;
255 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->disk_size
);
256 } else if (is_atom("rest", atom
, len
)) {
257 if (data
->mark_query
)
258 data
->split_on_whitespace
= 1;
260 strbuf_addstr(sb
, data
->rest
);
261 } else if (is_atom("deltabase", atom
, len
)) {
262 if (data
->mark_query
)
263 data
->info
.delta_base_sha1
= data
->delta_base_oid
.hash
;
266 oid_to_hex(&data
->delta_base_oid
));
268 die("unknown format element: %.*s", len
, atom
);
271 static size_t expand_format(struct strbuf
*sb
, const char *start
, void *data
)
277 end
= strchr(start
+ 1, ')');
279 die("format element '%s' does not end in ')'", start
);
281 expand_atom(sb
, start
+ 1, end
- start
- 1, data
);
283 return end
- start
+ 1;
286 static void batch_write(struct batch_options
*opt
, const void *data
, int len
)
288 if (opt
->buffer_output
) {
289 if (fwrite(data
, 1, len
, stdout
) != len
)
290 die_errno("unable to write to stdout");
292 write_or_die(1, data
, len
);
295 static void print_object_or_die(struct batch_options
*opt
, struct expand_data
*data
)
297 const struct object_id
*oid
= &data
->oid
;
299 assert(data
->info
.typep
);
301 if (data
->type
== OBJ_BLOB
) {
302 if (opt
->buffer_output
)
309 die("missing path for '%s'", oid_to_hex(oid
));
311 if (opt
->cmdmode
== 'w') {
312 if (filter_object(data
->rest
, 0100644, oid
,
314 die("could not convert '%s' %s",
315 oid_to_hex(oid
), data
->rest
);
316 } else if (opt
->cmdmode
== 'c') {
317 enum object_type type
;
318 if (!textconv_object(the_repository
,
319 data
->rest
, 0100644, oid
,
320 1, &contents
, &size
))
321 contents
= read_object_file(oid
,
325 die("could not convert '%s' %s",
326 oid_to_hex(oid
), data
->rest
);
328 BUG("invalid cmdmode: %c", opt
->cmdmode
);
329 batch_write(opt
, contents
, size
);
336 enum object_type type
;
340 contents
= read_object_file(oid
, &type
, &size
);
342 die("object %s disappeared", oid_to_hex(oid
));
343 if (type
!= data
->type
)
344 die("object %s changed type!?", oid_to_hex(oid
));
345 if (data
->info
.sizep
&& size
!= data
->size
)
346 die("object %s changed size!?", oid_to_hex(oid
));
348 batch_write(opt
, contents
, size
);
353 static void batch_object_write(const char *obj_name
,
354 struct strbuf
*scratch
,
355 struct batch_options
*opt
,
356 struct expand_data
*data
)
358 if (!data
->skip_object_info
&&
359 oid_object_info_extended(the_repository
, &data
->oid
, &data
->info
,
360 OBJECT_INFO_LOOKUP_REPLACE
) < 0) {
361 printf("%s missing\n",
362 obj_name
? obj_name
: oid_to_hex(&data
->oid
));
367 strbuf_reset(scratch
);
368 strbuf_expand(scratch
, opt
->format
, expand_format
, data
);
369 strbuf_addch(scratch
, '\n');
370 batch_write(opt
, scratch
->buf
, scratch
->len
);
372 if (opt
->print_contents
) {
373 print_object_or_die(opt
, data
);
374 batch_write(opt
, "\n", 1);
378 static void batch_one_object(const char *obj_name
,
379 struct strbuf
*scratch
,
380 struct batch_options
*opt
,
381 struct expand_data
*data
)
383 struct object_context ctx
;
384 int flags
= opt
->follow_symlinks
? GET_OID_FOLLOW_SYMLINKS
: 0;
385 enum get_oid_result result
;
387 result
= get_oid_with_context(the_repository
, obj_name
,
388 flags
, &data
->oid
, &ctx
);
389 if (result
!= FOUND
) {
392 printf("%s missing\n", obj_name
);
394 case SHORT_NAME_AMBIGUOUS
:
395 printf("%s ambiguous\n", obj_name
);
397 case DANGLING_SYMLINK
:
398 printf("dangling %"PRIuMAX
"\n%s\n",
399 (uintmax_t)strlen(obj_name
), obj_name
);
402 printf("loop %"PRIuMAX
"\n%s\n",
403 (uintmax_t)strlen(obj_name
), obj_name
);
406 printf("notdir %"PRIuMAX
"\n%s\n",
407 (uintmax_t)strlen(obj_name
), obj_name
);
410 BUG("unknown get_sha1_with_context result %d\n",
419 printf("symlink %"PRIuMAX
"\n%s\n",
420 (uintmax_t)ctx
.symlink_path
.len
,
421 ctx
.symlink_path
.buf
);
426 batch_object_write(obj_name
, scratch
, opt
, data
);
429 struct object_cb_data
{
430 struct batch_options
*opt
;
431 struct expand_data
*expand
;
433 struct strbuf
*scratch
;
436 static int batch_object_cb(const struct object_id
*oid
, void *vdata
)
438 struct object_cb_data
*data
= vdata
;
439 oidcpy(&data
->expand
->oid
, oid
);
440 batch_object_write(NULL
, data
->scratch
, data
->opt
, data
->expand
);
444 static int collect_loose_object(const struct object_id
*oid
,
448 oid_array_append(data
, oid
);
452 static int collect_packed_object(const struct object_id
*oid
,
453 struct packed_git
*pack
,
457 oid_array_append(data
, oid
);
461 static int batch_unordered_object(const struct object_id
*oid
, void *vdata
)
463 struct object_cb_data
*data
= vdata
;
465 if (oidset_insert(data
->seen
, oid
))
468 return batch_object_cb(oid
, data
);
471 static int batch_unordered_loose(const struct object_id
*oid
,
475 return batch_unordered_object(oid
, data
);
478 static int batch_unordered_packed(const struct object_id
*oid
,
479 struct packed_git
*pack
,
483 return batch_unordered_object(oid
, data
);
486 static int batch_objects(struct batch_options
*opt
)
488 struct strbuf input
= STRBUF_INIT
;
489 struct strbuf output
= STRBUF_INIT
;
490 struct expand_data data
;
495 opt
->format
= "%(objectname) %(objecttype) %(objectsize)";
498 * Expand once with our special mark_query flag, which will prime the
499 * object_info to be handed to oid_object_info_extended for each
502 memset(&data
, 0, sizeof(data
));
504 strbuf_expand(&output
, opt
->format
, expand_format
, &data
);
506 strbuf_release(&output
);
508 data
.split_on_whitespace
= 1;
510 if (opt
->all_objects
) {
511 struct object_info empty
= OBJECT_INFO_INIT
;
512 if (!memcmp(&data
.info
, &empty
, sizeof(empty
)))
513 data
.skip_object_info
= 1;
517 * If we are printing out the object, then always fill in the type,
518 * since we will want to decide whether or not to stream.
520 if (opt
->print_contents
)
521 data
.info
.typep
= &data
.type
;
523 if (opt
->all_objects
) {
524 struct object_cb_data cb
;
526 if (repository_format_partial_clone
)
527 warning("This repository has extensions.partialClone set. Some objects may not be loaded.");
531 cb
.scratch
= &output
;
533 if (opt
->unordered
) {
534 struct oidset seen
= OIDSET_INIT
;
538 for_each_loose_object(batch_unordered_loose
, &cb
, 0);
539 for_each_packed_object(batch_unordered_packed
, &cb
,
540 FOR_EACH_OBJECT_PACK_ORDER
);
544 struct oid_array sa
= OID_ARRAY_INIT
;
546 for_each_loose_object(collect_loose_object
, &sa
, 0);
547 for_each_packed_object(collect_packed_object
, &sa
, 0);
549 oid_array_for_each_unique(&sa
, batch_object_cb
, &cb
);
551 oid_array_clear(&sa
);
554 strbuf_release(&output
);
559 * We are going to call get_sha1 on a potentially very large number of
560 * objects. In most large cases, these will be actual object sha1s. The
561 * cost to double-check that each one is not also a ref (just so we can
562 * warn) ends up dwarfing the actual cost of the object lookups
563 * themselves. We can work around it by just turning off the warning.
565 save_warning
= warn_on_object_refname_ambiguity
;
566 warn_on_object_refname_ambiguity
= 0;
568 while (strbuf_getline(&input
, stdin
) != EOF
) {
569 if (data
.split_on_whitespace
) {
571 * Split at first whitespace, tying off the beginning
572 * of the string and saving the remainder (or NULL) in
575 char *p
= strpbrk(input
.buf
, " \t");
577 while (*p
&& strchr(" \t", *p
))
583 batch_one_object(input
.buf
, &output
, opt
, &data
);
586 strbuf_release(&input
);
587 strbuf_release(&output
);
588 warn_on_object_refname_ambiguity
= save_warning
;
592 static const char * const cat_file_usage
[] = {
593 N_("git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | <type> | --textconv | --filters) [--path=<path>] <object>"),
594 N_("git cat-file (--batch | --batch-check) [--follow-symlinks] [--textconv | --filters]"),
598 static int git_cat_file_config(const char *var
, const char *value
, void *cb
)
600 if (userdiff_config(var
, value
) < 0)
603 return git_default_config(var
, value
, cb
);
606 static int batch_option_callback(const struct option
*opt
,
610 struct batch_options
*bo
= opt
->value
;
612 BUG_ON_OPT_NEG(unset
);
615 return error(_("only one batch option may be specified"));
619 bo
->print_contents
= !strcmp(opt
->long_name
, "batch");
625 int cmd_cat_file(int argc
, const char **argv
, const char *prefix
)
628 const char *exp_type
= NULL
, *obj_name
= NULL
;
629 struct batch_options batch
= {0};
630 int unknown_type
= 0;
632 const struct option options
[] = {
633 OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")),
634 OPT_CMDMODE('t', NULL
, &opt
, N_("show object type"), 't'),
635 OPT_CMDMODE('s', NULL
, &opt
, N_("show object size"), 's'),
636 OPT_CMDMODE('e', NULL
, &opt
,
637 N_("exit with zero when there's no error"), 'e'),
638 OPT_CMDMODE('p', NULL
, &opt
, N_("pretty-print object's content"), 'p'),
639 OPT_CMDMODE(0, "textconv", &opt
,
640 N_("for blob objects, run textconv on object's content"), 'c'),
641 OPT_CMDMODE(0, "filters", &opt
,
642 N_("for blob objects, run filters on object's content"), 'w'),
643 OPT_STRING(0, "path", &force_path
, N_("blob"),
644 N_("use a specific path for --textconv/--filters")),
645 OPT_BOOL(0, "allow-unknown-type", &unknown_type
,
646 N_("allow -s and -t to work with broken/corrupt objects")),
647 OPT_BOOL(0, "buffer", &batch
.buffer_output
, N_("buffer --batch output")),
648 { OPTION_CALLBACK
, 0, "batch", &batch
, "format",
649 N_("show info and content of objects fed from the standard input"),
650 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
651 batch_option_callback
},
652 { OPTION_CALLBACK
, 0, "batch-check", &batch
, "format",
653 N_("show info about objects fed from the standard input"),
654 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
655 batch_option_callback
},
656 OPT_BOOL(0, "follow-symlinks", &batch
.follow_symlinks
,
657 N_("follow in-tree symlinks (used with --batch or --batch-check)")),
658 OPT_BOOL(0, "batch-all-objects", &batch
.all_objects
,
659 N_("show all objects with --batch or --batch-check")),
660 OPT_BOOL(0, "unordered", &batch
.unordered
,
661 N_("do not order --batch-all-objects output")),
665 git_config(git_cat_file_config
, NULL
);
667 batch
.buffer_output
= -1;
668 argc
= parse_options(argc
, argv
, prefix
, options
, cat_file_usage
, 0);
671 if (batch
.enabled
&& (opt
== 'c' || opt
== 'w'))
676 usage_with_options(cat_file_usage
, options
);
678 if (!opt
&& !batch
.enabled
) {
683 usage_with_options(cat_file_usage
, options
);
686 if (batch
.cmdmode
!= opt
|| argc
)
687 usage_with_options(cat_file_usage
, options
);
688 if (batch
.cmdmode
&& batch
.all_objects
)
689 die("--batch-all-objects cannot be combined with "
690 "--textconv nor with --filters");
693 if ((batch
.follow_symlinks
|| batch
.all_objects
) && !batch
.enabled
) {
694 usage_with_options(cat_file_usage
, options
);
697 if (force_path
&& opt
!= 'c' && opt
!= 'w') {
698 error("--path=<path> needs --textconv or --filters");
699 usage_with_options(cat_file_usage
, options
);
702 if (force_path
&& batch
.enabled
) {
703 error("--path=<path> incompatible with --batch");
704 usage_with_options(cat_file_usage
, options
);
707 if (batch
.buffer_output
< 0)
708 batch
.buffer_output
= batch
.all_objects
;
711 return batch_objects(&batch
);
713 if (unknown_type
&& opt
!= 't' && opt
!= 's')
714 die("git cat-file --allow-unknown-type: use with -s or -t");
715 return cat_one_file(opt
, exp_type
, obj_name
, unknown_type
);