2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #define USE_THE_REPOSITORY_VARIABLE
11 #include "environment.h"
15 #include "parse-options.h"
17 #include "streaming.h"
18 #include "oid-array.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"
26 #include "write-or-die.h"
31 BATCH_MODE_QUEUE_AND_DISPATCH
,
34 struct batch_options
{
37 enum batch_mode batch_mode
;
41 int transform_mode
; /* may be 'w' or 'c' for --filters or --textconv */
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
);
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
);
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_repository
->index
, path
, *buf
, *size
, &strbuf
, &meta
)) {
83 *buf
= strbuf_detach(&strbuf
, NULL
);
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
));
97 static int cat_one_file(int opt
, const char *exp_type
, const char *obj_name
,
101 struct object_id oid
;
102 enum object_type type
;
105 struct object_context obj_context
= {0};
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
=
110 GET_OID_RECORD_PATH
|
111 GET_OID_ONLY_TO_DIE
|
113 const char *path
= force_path
;
114 const int opt_cw
= (opt
== 'c' || opt
== 'w');
116 get_oid_flags
|= GET_OID_REQUIRE_PATH
;
119 flags
|= OBJECT_INFO_ALLOW_UNKNOWN_TYPE
;
121 if (get_oid_with_context(the_repository
, obj_name
, get_oid_flags
, &oid
,
123 die("Not a valid object name %s", obj_name
);
126 path
= obj_context
.path
;
127 if (obj_context
.mode
== S_IFINVALID
)
128 obj_context
.mode
= 0100644;
134 if (oid_object_info_extended(the_repository
, &oid
, &oi
, flags
) < 0)
135 die("git cat-file: could not get object info");
137 printf("%s\n", sb
.buf
);
149 oi
.contentp
= (void**)&buf
;
152 if (oid_object_info_extended(the_repository
, &oid
, &oi
, flags
) < 0)
153 die("git cat-file: could not get object info");
155 if (use_mailmap
&& (type
== OBJ_COMMIT
|| type
== OBJ_TAG
)) {
157 buf
= replace_idents_using_mailmap(buf
, &s
);
158 size
= cast_size_t_to_ulong(s
);
161 printf("%"PRIuMAX
"\n", (uintmax_t)size
);
166 ret
= !repo_has_object_file(the_repository
, &oid
);
171 if (filter_object(path
, obj_context
.mode
,
172 &oid
, &buf
, &size
)) {
179 if (textconv_object(the_repository
, path
, obj_context
.mode
,
180 &oid
, 1, &buf
, &size
))
182 /* else fallthrough */
185 type
= oid_object_info(the_repository
, &oid
, NULL
);
187 die("Not a valid object name %s", obj_name
);
189 /* custom pretty-print here */
190 if (type
== OBJ_TREE
) {
191 const char *ls_args
[3] = { NULL
};
192 ls_args
[0] = "ls-tree";
193 ls_args
[1] = obj_name
;
194 ret
= cmd_ls_tree(2, ls_args
, NULL
, the_repository
);
198 if (type
== OBJ_BLOB
) {
199 ret
= stream_blob(&oid
);
202 buf
= repo_read_object_file(the_repository
, &oid
, &type
,
205 die("Cannot read object %s", obj_name
);
209 buf
= replace_idents_using_mailmap(buf
, &s
);
210 size
= cast_size_t_to_ulong(s
);
213 /* otherwise just spit out the data */
218 enum object_type exp_type_id
= type_from_string(exp_type
);
220 if (exp_type_id
== OBJ_BLOB
) {
221 struct object_id blob_oid
;
222 if (oid_object_info(the_repository
, &oid
, NULL
) == OBJ_TAG
) {
223 char *buffer
= repo_read_object_file(the_repository
,
230 die(_("unable to read %s"), oid_to_hex(&oid
));
232 if (!skip_prefix(buffer
, "object ", &target
) ||
233 get_oid_hex_algop(target
, &blob_oid
,
234 &hash_algos
[oid
.algo
]))
235 die("%s not a valid tag", oid_to_hex(&oid
));
238 oidcpy(&blob_oid
, &oid
);
240 if (oid_object_info(the_repository
, &blob_oid
, NULL
) == OBJ_BLOB
) {
241 ret
= stream_blob(&blob_oid
);
245 * we attempted to dereference a tag to a blob
246 * and failed; there may be new dereference
247 * mechanisms this code is not aware of.
248 * fall-back to the usual case.
251 buf
= read_object_with_reference(the_repository
, &oid
,
252 exp_type_id
, &size
, NULL
);
256 buf
= replace_idents_using_mailmap(buf
, &s
);
257 size
= cast_size_t_to_ulong(s
);
262 die("git cat-file: unknown option: %s", exp_type
);
266 die("git cat-file %s: bad file", obj_name
);
268 write_or_die(1, buf
, size
);
272 object_context_release(&obj_context
);
277 struct object_id oid
;
278 enum object_type type
;
282 struct object_id delta_base_oid
;
285 * If mark_query is true, we do not expand anything, but rather
286 * just mark the object_info with items we wish to query.
291 * Whether to split the input on whitespace before feeding it to
292 * get_sha1; this is decided during the mark_query phase based on
293 * whether we have a %(rest) token in our format.
295 int split_on_whitespace
;
298 * After a mark_query run, this object_info is set up to be
299 * passed to oid_object_info_extended. It will point to the data
300 * elements above, so you can retrieve the response from there.
302 struct object_info info
;
305 * This flag will be true if the requested batch format and options
306 * don't require us to call oid_object_info, which can then be
309 unsigned skip_object_info
: 1;
312 static int is_atom(const char *atom
, const char *s
, int slen
)
314 int alen
= strlen(atom
);
315 return alen
== slen
&& !memcmp(atom
, s
, alen
);
318 static int expand_atom(struct strbuf
*sb
, const char *atom
, int len
,
319 struct expand_data
*data
)
321 if (is_atom("objectname", atom
, len
)) {
322 if (!data
->mark_query
)
323 strbuf_addstr(sb
, oid_to_hex(&data
->oid
));
324 } else if (is_atom("objecttype", atom
, len
)) {
325 if (data
->mark_query
)
326 data
->info
.typep
= &data
->type
;
328 strbuf_addstr(sb
, type_name(data
->type
));
329 } else if (is_atom("objectsize", atom
, len
)) {
330 if (data
->mark_query
)
331 data
->info
.sizep
= &data
->size
;
333 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->size
);
334 } else if (is_atom("objectsize:disk", atom
, len
)) {
335 if (data
->mark_query
)
336 data
->info
.disk_sizep
= &data
->disk_size
;
338 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->disk_size
);
339 } else if (is_atom("rest", atom
, len
)) {
340 if (data
->mark_query
)
341 data
->split_on_whitespace
= 1;
343 strbuf_addstr(sb
, data
->rest
);
344 } else if (is_atom("deltabase", atom
, len
)) {
345 if (data
->mark_query
)
346 data
->info
.delta_base_oid
= &data
->delta_base_oid
;
349 oid_to_hex(&data
->delta_base_oid
));
355 static void expand_format(struct strbuf
*sb
, const char *start
,
356 struct expand_data
*data
)
358 while (strbuf_expand_step(sb
, &start
)) {
361 if (skip_prefix(start
, "%", &start
) || *start
!= '(')
362 strbuf_addch(sb
, '%');
363 else if ((end
= strchr(start
+ 1, ')')) &&
364 expand_atom(sb
, start
+ 1, end
- start
- 1, data
))
367 strbuf_expand_bad_format(start
, "cat-file");
371 static void batch_write(struct batch_options
*opt
, const void *data
, int len
)
373 if (opt
->buffer_output
) {
374 if (fwrite(data
, 1, len
, stdout
) != len
)
375 die_errno("unable to write to stdout");
377 write_or_die(1, data
, len
);
380 static void print_object_or_die(struct batch_options
*opt
, struct expand_data
*data
)
382 const struct object_id
*oid
= &data
->oid
;
384 assert(data
->info
.typep
);
386 if (data
->type
== OBJ_BLOB
) {
387 if (opt
->buffer_output
)
389 if (opt
->transform_mode
) {
394 die("missing path for '%s'", oid_to_hex(oid
));
396 if (opt
->transform_mode
== 'w') {
397 if (filter_object(data
->rest
, 0100644, oid
,
399 die("could not convert '%s' %s",
400 oid_to_hex(oid
), data
->rest
);
401 } else if (opt
->transform_mode
== 'c') {
402 enum object_type type
;
403 if (!textconv_object(the_repository
,
404 data
->rest
, 0100644, oid
,
405 1, &contents
, &size
))
406 contents
= repo_read_object_file(the_repository
,
411 die("could not convert '%s' %s",
412 oid_to_hex(oid
), data
->rest
);
414 BUG("invalid transform_mode: %c", opt
->transform_mode
);
415 batch_write(opt
, contents
, size
);
422 enum object_type type
;
426 contents
= repo_read_object_file(the_repository
, oid
, &type
,
429 die("object %s disappeared", oid_to_hex(oid
));
433 contents
= replace_idents_using_mailmap(contents
, &s
);
434 size
= cast_size_t_to_ulong(s
);
437 if (type
!= data
->type
)
438 die("object %s changed type!?", oid_to_hex(oid
));
439 if (data
->info
.sizep
&& size
!= data
->size
&& !use_mailmap
)
440 die("object %s changed size!?", oid_to_hex(oid
));
442 batch_write(opt
, contents
, size
);
447 static void print_default_format(struct strbuf
*scratch
, struct expand_data
*data
,
448 struct batch_options
*opt
)
450 strbuf_addf(scratch
, "%s %s %"PRIuMAX
"%c", oid_to_hex(&data
->oid
),
451 type_name(data
->type
),
452 (uintmax_t)data
->size
, opt
->output_delim
);
456 * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
457 * which the object may be accessed (though note that we may also rely on
458 * data->oid, too). If "pack" is NULL, then offset is ignored.
460 static void batch_object_write(const char *obj_name
,
461 struct strbuf
*scratch
,
462 struct batch_options
*opt
,
463 struct expand_data
*data
,
464 struct packed_git
*pack
,
467 if (!data
->skip_object_info
) {
471 data
->info
.typep
= &data
->type
;
474 ret
= packed_object_info(the_repository
, pack
, offset
,
477 ret
= oid_object_info_extended(the_repository
,
478 &data
->oid
, &data
->info
,
479 OBJECT_INFO_LOOKUP_REPLACE
);
481 printf("%s missing%c",
482 obj_name
? obj_name
: oid_to_hex(&data
->oid
), opt
->output_delim
);
487 if (use_mailmap
&& (data
->type
== OBJ_COMMIT
|| data
->type
== OBJ_TAG
)) {
488 size_t s
= data
->size
;
491 buf
= repo_read_object_file(the_repository
, &data
->oid
, &data
->type
,
494 die(_("unable to read %s"), oid_to_hex(&data
->oid
));
495 buf
= replace_idents_using_mailmap(buf
, &s
);
496 data
->size
= cast_size_t_to_ulong(s
);
502 strbuf_reset(scratch
);
505 print_default_format(scratch
, data
, opt
);
507 expand_format(scratch
, opt
->format
, data
);
508 strbuf_addch(scratch
, opt
->output_delim
);
511 batch_write(opt
, scratch
->buf
, scratch
->len
);
513 if (opt
->batch_mode
== BATCH_MODE_CONTENTS
) {
514 print_object_or_die(opt
, data
);
515 batch_write(opt
, &opt
->output_delim
, 1);
519 static void batch_one_object(const char *obj_name
,
520 struct strbuf
*scratch
,
521 struct batch_options
*opt
,
522 struct expand_data
*data
)
524 struct object_context ctx
= {0};
527 (opt
->follow_symlinks
? GET_OID_FOLLOW_SYMLINKS
: 0);
528 enum get_oid_result result
;
530 result
= get_oid_with_context(the_repository
, obj_name
,
531 flags
, &data
->oid
, &ctx
);
532 if (result
!= FOUND
) {
535 printf("%s missing%c", obj_name
, opt
->output_delim
);
537 case SHORT_NAME_AMBIGUOUS
:
538 printf("%s ambiguous%c", obj_name
, opt
->output_delim
);
540 case DANGLING_SYMLINK
:
541 printf("dangling %"PRIuMAX
"%c%s%c",
542 (uintmax_t)strlen(obj_name
),
543 opt
->output_delim
, obj_name
, opt
->output_delim
);
546 printf("loop %"PRIuMAX
"%c%s%c",
547 (uintmax_t)strlen(obj_name
),
548 opt
->output_delim
, obj_name
, opt
->output_delim
);
551 printf("notdir %"PRIuMAX
"%c%s%c",
552 (uintmax_t)strlen(obj_name
),
553 opt
->output_delim
, obj_name
, opt
->output_delim
);
556 BUG("unknown get_sha1_with_context result %d\n",
566 printf("symlink %"PRIuMAX
"%c%s%c",
567 (uintmax_t)ctx
.symlink_path
.len
,
568 opt
->output_delim
, ctx
.symlink_path
.buf
, opt
->output_delim
);
573 batch_object_write(obj_name
, scratch
, opt
, data
, NULL
, 0);
576 object_context_release(&ctx
);
579 struct object_cb_data
{
580 struct batch_options
*opt
;
581 struct expand_data
*expand
;
583 struct strbuf
*scratch
;
586 static int batch_object_cb(const struct object_id
*oid
, void *vdata
)
588 struct object_cb_data
*data
= vdata
;
589 oidcpy(&data
->expand
->oid
, oid
);
590 batch_object_write(NULL
, data
->scratch
, data
->opt
, data
->expand
,
595 static int collect_loose_object(const struct object_id
*oid
,
596 const char *path UNUSED
,
599 oid_array_append(data
, oid
);
603 static int collect_packed_object(const struct object_id
*oid
,
604 struct packed_git
*pack UNUSED
,
608 oid_array_append(data
, oid
);
612 static int batch_unordered_object(const struct object_id
*oid
,
613 struct packed_git
*pack
, off_t offset
,
616 struct object_cb_data
*data
= vdata
;
618 if (oidset_insert(data
->seen
, oid
))
621 oidcpy(&data
->expand
->oid
, oid
);
622 batch_object_write(NULL
, data
->scratch
, data
->opt
, data
->expand
,
627 static int batch_unordered_loose(const struct object_id
*oid
,
628 const char *path UNUSED
,
631 return batch_unordered_object(oid
, NULL
, 0, data
);
634 static int batch_unordered_packed(const struct object_id
*oid
,
635 struct packed_git
*pack
,
639 return batch_unordered_object(oid
, pack
,
640 nth_packed_object_offset(pack
, pos
),
644 typedef void (*parse_cmd_fn_t
)(struct batch_options
*, const char *,
645 struct strbuf
*, struct expand_data
*);
652 static void parse_cmd_contents(struct batch_options
*opt
,
654 struct strbuf
*output
,
655 struct expand_data
*data
)
657 opt
->batch_mode
= BATCH_MODE_CONTENTS
;
658 batch_one_object(line
, output
, opt
, data
);
661 static void parse_cmd_info(struct batch_options
*opt
,
663 struct strbuf
*output
,
664 struct expand_data
*data
)
666 opt
->batch_mode
= BATCH_MODE_INFO
;
667 batch_one_object(line
, output
, opt
, data
);
670 static void dispatch_calls(struct batch_options
*opt
,
671 struct strbuf
*output
,
672 struct expand_data
*data
,
673 struct queued_cmd
*cmd
,
678 if (!opt
->buffer_output
)
679 die(_("flush is only for --buffer mode"));
681 for (i
= 0; i
< nr
; i
++)
682 cmd
[i
].fn(opt
, cmd
[i
].line
, output
, data
);
687 static void free_cmds(struct queued_cmd
*cmd
, size_t *nr
)
691 for (i
= 0; i
< *nr
; i
++)
692 FREE_AND_NULL(cmd
[i
].line
);
698 static const struct parse_cmd
{
703 { "contents", parse_cmd_contents
, 1},
704 { "info", parse_cmd_info
, 1},
708 static void batch_objects_command(struct batch_options
*opt
,
709 struct strbuf
*output
,
710 struct expand_data
*data
)
712 struct strbuf input
= STRBUF_INIT
;
713 struct queued_cmd
*queued_cmd
= NULL
;
714 size_t alloc
= 0, nr
= 0;
716 while (strbuf_getdelim_strip_crlf(&input
, stdin
, opt
->input_delim
) != EOF
) {
718 const struct parse_cmd
*cmd
= NULL
;
719 const char *p
= NULL
, *cmd_end
;
720 struct queued_cmd call
= {0};
723 die(_("empty command in input"));
724 if (isspace(*input
.buf
))
725 die(_("whitespace before command: '%s'"), input
.buf
);
727 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
728 if (!skip_prefix(input
.buf
, commands
[i
].name
, &cmd_end
))
732 if (cmd
->takes_args
) {
734 die(_("%s requires arguments"),
738 } else if (*cmd_end
) {
739 die(_("%s takes no arguments"),
747 die(_("unknown command: '%s'"), input
.buf
);
749 if (!strcmp(cmd
->name
, "flush")) {
750 dispatch_calls(opt
, output
, data
, queued_cmd
, nr
);
751 free_cmds(queued_cmd
, &nr
);
752 } else if (!opt
->buffer_output
) {
753 cmd
->fn(opt
, p
, output
, data
);
755 ALLOC_GROW(queued_cmd
, nr
+ 1, alloc
);
757 call
.line
= xstrdup_or_null(p
);
758 queued_cmd
[nr
++] = call
;
762 if (opt
->buffer_output
&&
764 !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) {
765 dispatch_calls(opt
, output
, data
, queued_cmd
, nr
);
766 free_cmds(queued_cmd
, &nr
);
769 free_cmds(queued_cmd
, &nr
);
771 strbuf_release(&input
);
774 #define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
776 static int batch_objects(struct batch_options
*opt
)
778 struct strbuf input
= STRBUF_INIT
;
779 struct strbuf output
= STRBUF_INIT
;
780 struct expand_data data
;
785 * Expand once with our special mark_query flag, which will prime the
786 * object_info to be handed to oid_object_info_extended for each
789 memset(&data
, 0, sizeof(data
));
791 expand_format(&output
,
792 opt
->format
? opt
->format
: DEFAULT_FORMAT
,
795 strbuf_release(&output
);
796 if (opt
->transform_mode
)
797 data
.split_on_whitespace
= 1;
799 if (opt
->format
&& !strcmp(opt
->format
, DEFAULT_FORMAT
))
802 * If we are printing out the object, then always fill in the type,
803 * since we will want to decide whether or not to stream.
805 if (opt
->batch_mode
== BATCH_MODE_CONTENTS
)
806 data
.info
.typep
= &data
.type
;
808 if (opt
->all_objects
) {
809 struct object_cb_data cb
;
810 struct object_info empty
= OBJECT_INFO_INIT
;
812 if (!memcmp(&data
.info
, &empty
, sizeof(empty
)))
813 data
.skip_object_info
= 1;
815 if (repo_has_promisor_remote(the_repository
))
816 warning("This repository uses promisor remotes. Some objects may not be loaded.");
818 disable_replace_refs();
822 cb
.scratch
= &output
;
824 if (opt
->unordered
) {
825 struct oidset seen
= OIDSET_INIT
;
829 for_each_loose_object(batch_unordered_loose
, &cb
, 0);
830 for_each_packed_object(batch_unordered_packed
, &cb
,
831 FOR_EACH_OBJECT_PACK_ORDER
);
835 struct oid_array sa
= OID_ARRAY_INIT
;
837 for_each_loose_object(collect_loose_object
, &sa
, 0);
838 for_each_packed_object(collect_packed_object
, &sa
, 0);
840 oid_array_for_each_unique(&sa
, batch_object_cb
, &cb
);
842 oid_array_clear(&sa
);
845 strbuf_release(&output
);
850 * We are going to call get_sha1 on a potentially very large number of
851 * objects. In most large cases, these will be actual object sha1s. The
852 * cost to double-check that each one is not also a ref (just so we can
853 * warn) ends up dwarfing the actual cost of the object lookups
854 * themselves. We can work around it by just turning off the warning.
856 save_warning
= warn_on_object_refname_ambiguity
;
857 warn_on_object_refname_ambiguity
= 0;
859 if (opt
->batch_mode
== BATCH_MODE_QUEUE_AND_DISPATCH
) {
860 batch_objects_command(opt
, &output
, &data
);
864 while (strbuf_getdelim_strip_crlf(&input
, stdin
, opt
->input_delim
) != EOF
) {
865 if (data
.split_on_whitespace
) {
867 * Split at first whitespace, tying off the beginning
868 * of the string and saving the remainder (or NULL) in
871 char *p
= strpbrk(input
.buf
, " \t");
873 while (*p
&& strchr(" \t", *p
))
879 batch_one_object(input
.buf
, &output
, opt
, &data
);
883 strbuf_release(&input
);
884 strbuf_release(&output
);
885 warn_on_object_refname_ambiguity
= save_warning
;
889 static int git_cat_file_config(const char *var
, const char *value
,
890 const struct config_context
*ctx
, void *cb
)
892 if (userdiff_config(var
, value
) < 0)
895 return git_default_config(var
, value
, ctx
, cb
);
898 static int batch_option_callback(const struct option
*opt
,
902 struct batch_options
*bo
= opt
->value
;
904 BUG_ON_OPT_NEG(unset
);
907 return error(_("only one batch option may be specified"));
912 if (!strcmp(opt
->long_name
, "batch"))
913 bo
->batch_mode
= BATCH_MODE_CONTENTS
;
914 else if (!strcmp(opt
->long_name
, "batch-check"))
915 bo
->batch_mode
= BATCH_MODE_INFO
;
916 else if (!strcmp(opt
->long_name
, "batch-command"))
917 bo
->batch_mode
= BATCH_MODE_QUEUE_AND_DISPATCH
;
919 BUG("%s given to batch-option-callback", opt
->long_name
);
926 int cmd_cat_file(int argc
,
929 struct repository
*repo UNUSED
)
934 const char *exp_type
= NULL
, *obj_name
= NULL
;
935 struct batch_options batch
= {0};
936 int unknown_type
= 0;
937 int input_nul_terminated
= 0;
938 int nul_terminated
= 0;
940 const char * const usage
[] = {
941 N_("git cat-file <type> <object>"),
942 N_("git cat-file (-e | -p) <object>"),
943 N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"),
944 N_("git cat-file (--textconv | --filters)\n"
945 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"),
946 N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
947 " [--buffer] [--follow-symlinks] [--unordered]\n"
948 " [--textconv | --filters] [-Z]"),
951 const struct option options
[] = {
953 OPT_GROUP(N_("Check object existence or emit object contents")),
954 OPT_CMDMODE('e', NULL
, &opt
,
955 N_("check if <object> exists"), 'e'),
956 OPT_CMDMODE('p', NULL
, &opt
, N_("pretty-print <object> content"), 'p'),
958 OPT_GROUP(N_("Emit [broken] object attributes")),
959 OPT_CMDMODE('t', NULL
, &opt
, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'),
960 OPT_CMDMODE('s', NULL
, &opt
, N_("show object size"), 's'),
961 OPT_BOOL(0, "allow-unknown-type", &unknown_type
,
962 N_("allow -s and -t to work with broken/corrupt objects")),
963 OPT_BOOL(0, "use-mailmap", &use_mailmap
, N_("use mail map file")),
964 OPT_ALIAS(0, "mailmap", "use-mailmap"),
966 OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")),
967 OPT_CALLBACK_F(0, "batch", &batch
, N_("format"),
968 N_("show full <object> or <rev> contents"),
969 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
970 batch_option_callback
),
971 OPT_CALLBACK_F(0, "batch-check", &batch
, N_("format"),
972 N_("like --batch, but don't emit <contents>"),
973 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
974 batch_option_callback
),
975 OPT_BOOL_F('z', NULL
, &input_nul_terminated
, N_("stdin is NUL-terminated"),
977 OPT_BOOL('Z', NULL
, &nul_terminated
, N_("stdin and stdout is NUL-terminated")),
978 OPT_CALLBACK_F(0, "batch-command", &batch
, N_("format"),
979 N_("read commands from stdin"),
980 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
981 batch_option_callback
),
982 OPT_CMDMODE(0, "batch-all-objects", &opt
,
983 N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'),
984 /* Batch-specific options */
985 OPT_GROUP(N_("Change or optimize batch output")),
986 OPT_BOOL(0, "buffer", &batch
.buffer_output
, N_("buffer --batch output")),
987 OPT_BOOL(0, "follow-symlinks", &batch
.follow_symlinks
,
988 N_("follow in-tree symlinks")),
989 OPT_BOOL(0, "unordered", &batch
.unordered
,
990 N_("do not order objects before emitting them")),
991 /* Textconv options, stand-ole*/
992 OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")),
993 OPT_CMDMODE(0, "textconv", &opt
,
994 N_("run textconv on object's content"), 'c'),
995 OPT_CMDMODE(0, "filters", &opt
,
996 N_("run filters on object's content"), 'w'),
997 OPT_STRING(0, "path", &force_path
, N_("blob|tree"),
998 N_("use a <path> for (--textconv | --filters); Not with 'batch'")),
1002 git_config(git_cat_file_config
, NULL
);
1004 batch
.buffer_output
= -1;
1006 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
1007 opt_cw
= (opt
== 'c' || opt
== 'w');
1008 opt_epts
= (opt
== 'e' || opt
== 'p' || opt
== 't' || opt
== 's');
1011 read_mailmap(&mailmap
);
1013 /* --batch-all-objects? */
1015 batch
.all_objects
= 1;
1017 /* Option compatibility */
1018 if (force_path
&& !opt_cw
)
1019 usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
1021 "--path", _("path|tree-ish"), "--filters",
1024 /* Option compatibility with batch mode */
1027 else if (batch
.follow_symlinks
)
1028 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1029 "--follow-symlinks");
1030 else if (batch
.buffer_output
>= 0)
1031 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1033 else if (batch
.all_objects
)
1034 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1035 "--batch-all-objects");
1036 else if (input_nul_terminated
)
1037 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1039 else if (nul_terminated
)
1040 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1043 batch
.input_delim
= batch
.output_delim
= '\n';
1044 if (input_nul_terminated
)
1045 batch
.input_delim
= '\0';
1047 batch
.input_delim
= batch
.output_delim
= '\0';
1049 /* Batch defaults */
1050 if (batch
.buffer_output
< 0)
1051 batch
.buffer_output
= batch
.all_objects
;
1053 prepare_repo_settings(the_repository
);
1054 the_repository
->settings
.command_requires_full_index
= 0;
1056 /* Return early if we're in batch mode? */
1057 if (batch
.enabled
) {
1059 batch
.transform_mode
= opt
;
1060 else if (opt
&& opt
!= 'b')
1061 usage_msg_optf(_("'-%c' is incompatible with batch mode"),
1062 usage
, options
, opt
);
1064 usage_msg_opt(_("batch modes take no arguments"), usage
,
1067 return batch_objects(&batch
);
1071 if (!argc
&& opt
== 'c')
1072 usage_msg_optf(_("<rev> required with '%s'"),
1073 usage
, options
, "--textconv");
1074 else if (!argc
&& opt
== 'w')
1075 usage_msg_optf(_("<rev> required with '%s'"),
1076 usage
, options
, "--filters");
1077 else if (!argc
&& opt_epts
)
1078 usage_msg_optf(_("<object> required with '-%c'"),
1079 usage
, options
, opt
);
1083 usage_msg_opt(_("too many arguments"), usage
, options
);
1085 usage_with_options(usage
, options
);
1086 } else if (argc
!= 2) {
1087 usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"),
1088 usage
, options
, argc
);
1094 if (unknown_type
&& opt
!= 't' && opt
!= 's')
1095 die("git cat-file --allow-unknown-type: use with -s or -t");
1096 return cat_one_file(opt
, exp_type
, obj_name
, unknown_type
);