2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 #include "parse-options.h"
12 #include "streaming.h"
13 #include "tree-walk.h"
14 #include "sha1-array.h"
17 struct batch_options
{
23 int cmdmode
; /* may be 'w' or 'c' for --filters or --textconv */
27 static const char *force_path
;
29 static int filter_object(const char *path
, unsigned mode
,
30 const struct object_id
*oid
,
31 char **buf
, unsigned long *size
)
33 enum object_type type
;
35 *buf
= read_object_file(oid
, &type
, size
);
37 return error(_("cannot read object %s '%s'"),
38 oid_to_hex(oid
), path
);
39 if ((type
== OBJ_BLOB
) && S_ISREG(mode
)) {
40 struct strbuf strbuf
= STRBUF_INIT
;
41 if (convert_to_working_tree(path
, *buf
, *size
, &strbuf
)) {
44 *buf
= strbuf_detach(&strbuf
, NULL
);
51 static int cat_one_file(int opt
, const char *exp_type
, const char *obj_name
,
55 enum object_type type
;
58 struct object_context obj_context
;
59 struct object_info oi
= OBJECT_INFO_INIT
;
60 struct strbuf sb
= STRBUF_INIT
;
61 unsigned flags
= OBJECT_INFO_LOOKUP_REPLACE
;
62 const char *path
= force_path
;
65 flags
|= OBJECT_INFO_ALLOW_UNKNOWN_TYPE
;
67 if (get_oid_with_context(obj_name
, GET_OID_RECORD_PATH
,
69 die("Not a valid object name %s", obj_name
);
72 path
= obj_context
.path
;
73 if (obj_context
.mode
== S_IFINVALID
)
74 obj_context
.mode
= 0100644;
80 if (oid_object_info_extended(&oid
, &oi
, flags
) < 0)
81 die("git cat-file: could not get object info");
83 printf("%s\n", sb
.buf
);
91 if (oid_object_info_extended(&oid
, &oi
, flags
) < 0)
92 die("git cat-file: could not get object info");
93 printf("%lu\n", size
);
97 return !has_object_file(&oid
);
101 die("git cat-file --filters %s: <object> must be "
102 "<sha1:path>", obj_name
);
104 if (filter_object(path
, obj_context
.mode
,
111 die("git cat-file --textconv %s: <object> must be <sha1:path>",
114 if (textconv_object(path
, obj_context
.mode
, &oid
, 1, &buf
, &size
))
116 /* else fallthrough */
119 type
= oid_object_info(&oid
, NULL
);
121 die("Not a valid object name %s", obj_name
);
123 /* custom pretty-print here */
124 if (type
== OBJ_TREE
) {
125 const char *ls_args
[3] = { NULL
};
126 ls_args
[0] = "ls-tree";
127 ls_args
[1] = obj_name
;
128 return cmd_ls_tree(2, ls_args
, NULL
);
131 if (type
== OBJ_BLOB
)
132 return stream_blob_to_fd(1, &oid
, NULL
, 0);
133 buf
= read_object_file(&oid
, &type
, &size
);
135 die("Cannot read object %s", obj_name
);
137 /* otherwise just spit out the data */
141 if (type_from_string(exp_type
) == OBJ_BLOB
) {
142 struct object_id blob_oid
;
143 if (oid_object_info(&oid
, NULL
) == OBJ_TAG
) {
144 char *buffer
= read_object_file(&oid
, &type
,
147 if (!skip_prefix(buffer
, "object ", &target
) ||
148 get_oid_hex(target
, &blob_oid
))
149 die("%s not a valid tag", oid_to_hex(&oid
));
152 oidcpy(&blob_oid
, &oid
);
154 if (oid_object_info(&blob_oid
, NULL
) == OBJ_BLOB
)
155 return stream_blob_to_fd(1, &blob_oid
, NULL
, 0);
157 * we attempted to dereference a tag to a blob
158 * and failed; there may be new dereference
159 * mechanisms this code is not aware of.
160 * fall-back to the usual case.
163 buf
= read_object_with_reference(&oid
, exp_type
, &size
, NULL
);
167 die("git cat-file: unknown option: %s", exp_type
);
171 die("git cat-file %s: bad file", obj_name
);
173 write_or_die(1, buf
, size
);
175 free(obj_context
.path
);
180 struct object_id oid
;
181 enum object_type type
;
185 struct object_id delta_base_oid
;
188 * If mark_query is true, we do not expand anything, but rather
189 * just mark the object_info with items we wish to query.
194 * Whether to split the input on whitespace before feeding it to
195 * get_sha1; this is decided during the mark_query phase based on
196 * whether we have a %(rest) token in our format.
198 int split_on_whitespace
;
201 * After a mark_query run, this object_info is set up to be
202 * passed to sha1_object_info_extended. It will point to the data
203 * elements above, so you can retrieve the response from there.
205 struct object_info info
;
208 * This flag will be true if the requested batch format and options
209 * don't require us to call sha1_object_info, which can then be
212 unsigned skip_object_info
: 1;
215 static int is_atom(const char *atom
, const char *s
, int slen
)
217 int alen
= strlen(atom
);
218 return alen
== slen
&& !memcmp(atom
, s
, alen
);
221 static void expand_atom(struct strbuf
*sb
, const char *atom
, int len
,
224 struct expand_data
*data
= vdata
;
226 if (is_atom("objectname", atom
, len
)) {
227 if (!data
->mark_query
)
228 strbuf_addstr(sb
, oid_to_hex(&data
->oid
));
229 } else if (is_atom("objecttype", atom
, len
)) {
230 if (data
->mark_query
)
231 data
->info
.typep
= &data
->type
;
233 strbuf_addstr(sb
, type_name(data
->type
));
234 } else if (is_atom("objectsize", atom
, len
)) {
235 if (data
->mark_query
)
236 data
->info
.sizep
= &data
->size
;
238 strbuf_addf(sb
, "%lu", data
->size
);
239 } else if (is_atom("objectsize:disk", atom
, len
)) {
240 if (data
->mark_query
)
241 data
->info
.disk_sizep
= &data
->disk_size
;
243 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->disk_size
);
244 } else if (is_atom("rest", atom
, len
)) {
245 if (data
->mark_query
)
246 data
->split_on_whitespace
= 1;
248 strbuf_addstr(sb
, data
->rest
);
249 } else if (is_atom("deltabase", atom
, len
)) {
250 if (data
->mark_query
)
251 data
->info
.delta_base_sha1
= data
->delta_base_oid
.hash
;
254 oid_to_hex(&data
->delta_base_oid
));
256 die("unknown format element: %.*s", len
, atom
);
259 static size_t expand_format(struct strbuf
*sb
, const char *start
, void *data
)
265 end
= strchr(start
+ 1, ')');
267 die("format element '%s' does not end in ')'", start
);
269 expand_atom(sb
, start
+ 1, end
- start
- 1, data
);
271 return end
- start
+ 1;
274 static void batch_write(struct batch_options
*opt
, const void *data
, int len
)
276 if (opt
->buffer_output
) {
277 if (fwrite(data
, 1, len
, stdout
) != len
)
278 die_errno("unable to write to stdout");
280 write_or_die(1, data
, len
);
283 static void print_object_or_die(struct batch_options
*opt
, struct expand_data
*data
)
285 const struct object_id
*oid
= &data
->oid
;
287 assert(data
->info
.typep
);
289 if (data
->type
== OBJ_BLOB
) {
290 if (opt
->buffer_output
)
297 die("missing path for '%s'", oid_to_hex(oid
));
299 if (opt
->cmdmode
== 'w') {
300 if (filter_object(data
->rest
, 0100644, oid
,
302 die("could not convert '%s' %s",
303 oid_to_hex(oid
), data
->rest
);
304 } else if (opt
->cmdmode
== 'c') {
305 enum object_type type
;
306 if (!textconv_object(data
->rest
, 0100644, oid
,
307 1, &contents
, &size
))
308 contents
= read_object_file(oid
,
312 die("could not convert '%s' %s",
313 oid_to_hex(oid
), data
->rest
);
315 die("BUG: invalid cmdmode: %c", opt
->cmdmode
);
316 batch_write(opt
, contents
, size
);
318 } else if (stream_blob_to_fd(1, oid
, NULL
, 0) < 0)
319 die("unable to stream %s to stdout", oid_to_hex(oid
));
322 enum object_type type
;
326 contents
= read_object_file(oid
, &type
, &size
);
328 die("object %s disappeared", oid_to_hex(oid
));
329 if (type
!= data
->type
)
330 die("object %s changed type!?", oid_to_hex(oid
));
331 if (data
->info
.sizep
&& size
!= data
->size
)
332 die("object %s changed size!?", oid_to_hex(oid
));
334 batch_write(opt
, contents
, size
);
339 static void batch_object_write(const char *obj_name
, struct batch_options
*opt
,
340 struct expand_data
*data
)
342 struct strbuf buf
= STRBUF_INIT
;
344 if (!data
->skip_object_info
&&
345 oid_object_info_extended(&data
->oid
, &data
->info
,
346 OBJECT_INFO_LOOKUP_REPLACE
) < 0) {
347 printf("%s missing\n",
348 obj_name
? obj_name
: oid_to_hex(&data
->oid
));
353 strbuf_expand(&buf
, opt
->format
, expand_format
, data
);
354 strbuf_addch(&buf
, '\n');
355 batch_write(opt
, buf
.buf
, buf
.len
);
356 strbuf_release(&buf
);
358 if (opt
->print_contents
) {
359 print_object_or_die(opt
, data
);
360 batch_write(opt
, "\n", 1);
364 static void batch_one_object(const char *obj_name
, struct batch_options
*opt
,
365 struct expand_data
*data
)
367 struct object_context ctx
;
368 int flags
= opt
->follow_symlinks
? GET_OID_FOLLOW_SYMLINKS
: 0;
369 enum follow_symlinks_result result
;
371 result
= get_oid_with_context(obj_name
, flags
, &data
->oid
, &ctx
);
372 if (result
!= FOUND
) {
375 printf("%s missing\n", obj_name
);
377 case DANGLING_SYMLINK
:
378 printf("dangling %"PRIuMAX
"\n%s\n",
379 (uintmax_t)strlen(obj_name
), obj_name
);
382 printf("loop %"PRIuMAX
"\n%s\n",
383 (uintmax_t)strlen(obj_name
), obj_name
);
386 printf("notdir %"PRIuMAX
"\n%s\n",
387 (uintmax_t)strlen(obj_name
), obj_name
);
390 die("BUG: unknown get_sha1_with_context result %d\n",
399 printf("symlink %"PRIuMAX
"\n%s\n",
400 (uintmax_t)ctx
.symlink_path
.len
,
401 ctx
.symlink_path
.buf
);
406 batch_object_write(obj_name
, opt
, data
);
409 struct object_cb_data
{
410 struct batch_options
*opt
;
411 struct expand_data
*expand
;
414 static int batch_object_cb(const struct object_id
*oid
, void *vdata
)
416 struct object_cb_data
*data
= vdata
;
417 oidcpy(&data
->expand
->oid
, oid
);
418 batch_object_write(NULL
, data
->opt
, data
->expand
);
422 static int batch_loose_object(const struct object_id
*oid
,
426 oid_array_append(data
, oid
);
430 static int batch_packed_object(const struct object_id
*oid
,
431 struct packed_git
*pack
,
435 oid_array_append(data
, oid
);
439 static int batch_objects(struct batch_options
*opt
)
441 struct strbuf buf
= STRBUF_INIT
;
442 struct expand_data data
;
447 opt
->format
= "%(objectname) %(objecttype) %(objectsize)";
450 * Expand once with our special mark_query flag, which will prime the
451 * object_info to be handed to sha1_object_info_extended for each
454 memset(&data
, 0, sizeof(data
));
456 strbuf_expand(&buf
, opt
->format
, expand_format
, &data
);
459 data
.split_on_whitespace
= 1;
461 if (opt
->all_objects
) {
462 struct object_info empty
= OBJECT_INFO_INIT
;
463 if (!memcmp(&data
.info
, &empty
, sizeof(empty
)))
464 data
.skip_object_info
= 1;
468 * If we are printing out the object, then always fill in the type,
469 * since we will want to decide whether or not to stream.
471 if (opt
->print_contents
)
472 data
.info
.typep
= &data
.type
;
474 if (opt
->all_objects
) {
475 struct oid_array sa
= OID_ARRAY_INIT
;
476 struct object_cb_data cb
;
478 for_each_loose_object(batch_loose_object
, &sa
, 0);
479 for_each_packed_object(batch_packed_object
, &sa
, 0);
480 if (repository_format_partial_clone
)
481 warning("This repository has extensions.partialClone set. Some objects may not be loaded.");
485 oid_array_for_each_unique(&sa
, batch_object_cb
, &cb
);
487 oid_array_clear(&sa
);
492 * We are going to call get_sha1 on a potentially very large number of
493 * objects. In most large cases, these will be actual object sha1s. The
494 * cost to double-check that each one is not also a ref (just so we can
495 * warn) ends up dwarfing the actual cost of the object lookups
496 * themselves. We can work around it by just turning off the warning.
498 save_warning
= warn_on_object_refname_ambiguity
;
499 warn_on_object_refname_ambiguity
= 0;
501 while (strbuf_getline(&buf
, stdin
) != EOF
) {
502 if (data
.split_on_whitespace
) {
504 * Split at first whitespace, tying off the beginning
505 * of the string and saving the remainder (or NULL) in
508 char *p
= strpbrk(buf
.buf
, " \t");
510 while (*p
&& strchr(" \t", *p
))
516 batch_one_object(buf
.buf
, opt
, &data
);
519 strbuf_release(&buf
);
520 warn_on_object_refname_ambiguity
= save_warning
;
524 static const char * const cat_file_usage
[] = {
525 N_("git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | <type> | --textconv | --filters) [--path=<path>] <object>"),
526 N_("git cat-file (--batch | --batch-check) [--follow-symlinks] [--textconv | --filters]"),
530 static int git_cat_file_config(const char *var
, const char *value
, void *cb
)
532 if (userdiff_config(var
, value
) < 0)
535 return git_default_config(var
, value
, cb
);
538 static int batch_option_callback(const struct option
*opt
,
542 struct batch_options
*bo
= opt
->value
;
549 bo
->print_contents
= !strcmp(opt
->long_name
, "batch");
555 int cmd_cat_file(int argc
, const char **argv
, const char *prefix
)
558 const char *exp_type
= NULL
, *obj_name
= NULL
;
559 struct batch_options batch
= {0};
560 int unknown_type
= 0;
562 const struct option options
[] = {
563 OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")),
564 OPT_CMDMODE('t', NULL
, &opt
, N_("show object type"), 't'),
565 OPT_CMDMODE('s', NULL
, &opt
, N_("show object size"), 's'),
566 OPT_CMDMODE('e', NULL
, &opt
,
567 N_("exit with zero when there's no error"), 'e'),
568 OPT_CMDMODE('p', NULL
, &opt
, N_("pretty-print object's content"), 'p'),
569 OPT_CMDMODE(0, "textconv", &opt
,
570 N_("for blob objects, run textconv on object's content"), 'c'),
571 OPT_CMDMODE(0, "filters", &opt
,
572 N_("for blob objects, run filters on object's content"), 'w'),
573 OPT_STRING(0, "path", &force_path
, N_("blob"),
574 N_("use a specific path for --textconv/--filters")),
575 OPT_BOOL(0, "allow-unknown-type", &unknown_type
,
576 N_("allow -s and -t to work with broken/corrupt objects")),
577 OPT_BOOL(0, "buffer", &batch
.buffer_output
, N_("buffer --batch output")),
578 { OPTION_CALLBACK
, 0, "batch", &batch
, "format",
579 N_("show info and content of objects fed from the standard input"),
580 PARSE_OPT_OPTARG
, batch_option_callback
},
581 { OPTION_CALLBACK
, 0, "batch-check", &batch
, "format",
582 N_("show info about objects fed from the standard input"),
583 PARSE_OPT_OPTARG
, batch_option_callback
},
584 OPT_BOOL(0, "follow-symlinks", &batch
.follow_symlinks
,
585 N_("follow in-tree symlinks (used with --batch or --batch-check)")),
586 OPT_BOOL(0, "batch-all-objects", &batch
.all_objects
,
587 N_("show all objects with --batch or --batch-check")),
591 git_config(git_cat_file_config
, NULL
);
593 batch
.buffer_output
= -1;
594 argc
= parse_options(argc
, argv
, prefix
, options
, cat_file_usage
, 0);
597 if (batch
.enabled
&& (opt
== 'c' || opt
== 'w'))
602 usage_with_options(cat_file_usage
, options
);
604 if (!opt
&& !batch
.enabled
) {
609 usage_with_options(cat_file_usage
, options
);
612 if (batch
.cmdmode
!= opt
|| argc
)
613 usage_with_options(cat_file_usage
, options
);
614 if (batch
.cmdmode
&& batch
.all_objects
)
615 die("--batch-all-objects cannot be combined with "
616 "--textconv nor with --filters");
619 if ((batch
.follow_symlinks
|| batch
.all_objects
) && !batch
.enabled
) {
620 usage_with_options(cat_file_usage
, options
);
623 if (force_path
&& opt
!= 'c' && opt
!= 'w') {
624 error("--path=<path> needs --textconv or --filters");
625 usage_with_options(cat_file_usage
, options
);
628 if (force_path
&& batch
.enabled
) {
629 error("--path=<path> incompatible with --batch");
630 usage_with_options(cat_file_usage
, options
);
633 if (batch
.buffer_output
< 0)
634 batch
.buffer_output
= batch
.all_objects
;
637 return batch_objects(&batch
);
639 if (unknown_type
&& opt
!= 't' && opt
!= 's')
640 die("git cat-file --allow-unknown-type: use with -s or -t");
641 return cat_one_file(opt
, exp_type
, obj_name
, unknown_type
);