2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #define USE_THE_INDEX_VARIABLE
13 #include "environment.h"
17 #include "parse-options.h"
19 #include "streaming.h"
20 #include "tree-walk.h"
21 #include "oid-array.h"
23 #include "object-file.h"
24 #include "object-name.h"
25 #include "object-store.h"
26 #include "replace-object.h"
27 #include "promisor-remote.h"
29 #include "write-or-die.h"
34 BATCH_MODE_QUEUE_AND_DISPATCH
,
37 struct batch_options
{
40 enum batch_mode batch_mode
;
44 int transform_mode
; /* may be 'w' or 'c' for --filters or --textconv */
49 static const char *force_path
;
51 static struct string_list mailmap
= STRING_LIST_INIT_NODUP
;
52 static int use_mailmap
;
54 static char *replace_idents_using_mailmap(char *, size_t *);
56 static char *replace_idents_using_mailmap(char *object_buf
, size_t *size
)
58 struct strbuf sb
= STRBUF_INIT
;
59 const char *headers
[] = { "author ", "committer ", "tagger ", NULL
};
61 strbuf_attach(&sb
, object_buf
, *size
, *size
+ 1);
62 apply_mailmap_to_header(&sb
, headers
, &mailmap
);
64 return strbuf_detach(&sb
, NULL
);
67 static int filter_object(const char *path
, unsigned mode
,
68 const struct object_id
*oid
,
69 char **buf
, unsigned long *size
)
71 enum object_type type
;
73 *buf
= repo_read_object_file(the_repository
, oid
, &type
, size
);
75 return error(_("cannot read object %s '%s'"),
76 oid_to_hex(oid
), path
);
77 if ((type
== OBJ_BLOB
) && S_ISREG(mode
)) {
78 struct strbuf strbuf
= STRBUF_INIT
;
79 struct checkout_metadata meta
;
81 init_checkout_metadata(&meta
, NULL
, NULL
, oid
);
82 if (convert_to_working_tree(&the_index
, path
, *buf
, *size
, &strbuf
, &meta
)) {
85 *buf
= strbuf_detach(&strbuf
, NULL
);
92 static int stream_blob(const struct object_id
*oid
)
94 if (stream_blob_to_fd(1, oid
, NULL
, 0))
95 die("unable to stream %s to stdout", oid_to_hex(oid
));
99 static int cat_one_file(int opt
, const char *exp_type
, const char *obj_name
,
103 struct object_id oid
;
104 enum object_type type
;
107 struct object_context obj_context
;
108 struct object_info oi
= OBJECT_INFO_INIT
;
109 struct strbuf sb
= STRBUF_INIT
;
110 unsigned flags
= OBJECT_INFO_LOOKUP_REPLACE
;
111 unsigned get_oid_flags
= GET_OID_RECORD_PATH
| GET_OID_ONLY_TO_DIE
;
112 const char *path
= force_path
;
113 const int opt_cw
= (opt
== 'c' || opt
== 'w');
115 get_oid_flags
|= GET_OID_REQUIRE_PATH
;
118 flags
|= OBJECT_INFO_ALLOW_UNKNOWN_TYPE
;
120 if (get_oid_with_context(the_repository
, obj_name
, get_oid_flags
, &oid
,
122 die("Not a valid object name %s", obj_name
);
125 path
= obj_context
.path
;
126 if (obj_context
.mode
== S_IFINVALID
)
127 obj_context
.mode
= 0100644;
133 if (oid_object_info_extended(the_repository
, &oid
, &oi
, flags
) < 0)
134 die("git cat-file: could not get object info");
136 printf("%s\n", sb
.buf
);
148 oi
.contentp
= (void**)&buf
;
151 if (oid_object_info_extended(the_repository
, &oid
, &oi
, flags
) < 0)
152 die("git cat-file: could not get object info");
154 if (use_mailmap
&& (type
== OBJ_COMMIT
|| type
== OBJ_TAG
)) {
156 buf
= replace_idents_using_mailmap(buf
, &s
);
157 size
= cast_size_t_to_ulong(s
);
160 printf("%"PRIuMAX
"\n", (uintmax_t)size
);
165 return !repo_has_object_file(the_repository
, &oid
);
169 if (filter_object(path
, obj_context
.mode
,
170 &oid
, &buf
, &size
)) {
177 if (textconv_object(the_repository
, path
, obj_context
.mode
,
178 &oid
, 1, &buf
, &size
))
180 /* else fallthrough */
183 type
= oid_object_info(the_repository
, &oid
, NULL
);
185 die("Not a valid object name %s", obj_name
);
187 /* custom pretty-print here */
188 if (type
== OBJ_TREE
) {
189 const char *ls_args
[3] = { NULL
};
190 ls_args
[0] = "ls-tree";
191 ls_args
[1] = obj_name
;
192 ret
= cmd_ls_tree(2, ls_args
, NULL
);
196 if (type
== OBJ_BLOB
) {
197 ret
= stream_blob(&oid
);
200 buf
= repo_read_object_file(the_repository
, &oid
, &type
,
203 die("Cannot read object %s", obj_name
);
207 buf
= replace_idents_using_mailmap(buf
, &s
);
208 size
= cast_size_t_to_ulong(s
);
211 /* otherwise just spit out the data */
216 enum object_type exp_type_id
= type_from_string(exp_type
);
218 if (exp_type_id
== OBJ_BLOB
) {
219 struct object_id blob_oid
;
220 if (oid_object_info(the_repository
, &oid
, NULL
) == OBJ_TAG
) {
221 char *buffer
= repo_read_object_file(the_repository
,
226 if (!skip_prefix(buffer
, "object ", &target
) ||
227 get_oid_hex(target
, &blob_oid
))
228 die("%s not a valid tag", oid_to_hex(&oid
));
231 oidcpy(&blob_oid
, &oid
);
233 if (oid_object_info(the_repository
, &blob_oid
, NULL
) == OBJ_BLOB
) {
234 ret
= stream_blob(&blob_oid
);
238 * we attempted to dereference a tag to a blob
239 * and failed; there may be new dereference
240 * mechanisms this code is not aware of.
241 * fall-back to the usual case.
244 buf
= read_object_with_reference(the_repository
, &oid
,
245 exp_type_id
, &size
, NULL
);
249 buf
= replace_idents_using_mailmap(buf
, &s
);
250 size
= cast_size_t_to_ulong(s
);
255 die("git cat-file: unknown option: %s", exp_type
);
259 die("git cat-file %s: bad file", obj_name
);
261 write_or_die(1, buf
, size
);
265 free(obj_context
.path
);
270 struct object_id oid
;
271 enum object_type type
;
275 struct object_id delta_base_oid
;
278 * If mark_query is true, we do not expand anything, but rather
279 * just mark the object_info with items we wish to query.
284 * Whether to split the input on whitespace before feeding it to
285 * get_sha1; this is decided during the mark_query phase based on
286 * whether we have a %(rest) token in our format.
288 int split_on_whitespace
;
291 * After a mark_query run, this object_info is set up to be
292 * passed to oid_object_info_extended. It will point to the data
293 * elements above, so you can retrieve the response from there.
295 struct object_info info
;
298 * This flag will be true if the requested batch format and options
299 * don't require us to call oid_object_info, which can then be
302 unsigned skip_object_info
: 1;
305 static int is_atom(const char *atom
, const char *s
, int slen
)
307 int alen
= strlen(atom
);
308 return alen
== slen
&& !memcmp(atom
, s
, alen
);
311 static void expand_atom(struct strbuf
*sb
, const char *atom
, int len
,
314 struct expand_data
*data
= vdata
;
316 if (is_atom("objectname", atom
, len
)) {
317 if (!data
->mark_query
)
318 strbuf_addstr(sb
, oid_to_hex(&data
->oid
));
319 } else if (is_atom("objecttype", atom
, len
)) {
320 if (data
->mark_query
)
321 data
->info
.typep
= &data
->type
;
323 strbuf_addstr(sb
, type_name(data
->type
));
324 } else if (is_atom("objectsize", atom
, len
)) {
325 if (data
->mark_query
)
326 data
->info
.sizep
= &data
->size
;
328 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->size
);
329 } else if (is_atom("objectsize:disk", atom
, len
)) {
330 if (data
->mark_query
)
331 data
->info
.disk_sizep
= &data
->disk_size
;
333 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->disk_size
);
334 } else if (is_atom("rest", atom
, len
)) {
335 if (data
->mark_query
)
336 data
->split_on_whitespace
= 1;
338 strbuf_addstr(sb
, data
->rest
);
339 } else if (is_atom("deltabase", atom
, len
)) {
340 if (data
->mark_query
)
341 data
->info
.delta_base_oid
= &data
->delta_base_oid
;
344 oid_to_hex(&data
->delta_base_oid
));
346 die("unknown format element: %.*s", len
, atom
);
349 static size_t expand_format(struct strbuf
*sb
, const char *start
, void *data
)
355 end
= strchr(start
+ 1, ')');
357 die("format element '%s' does not end in ')'", start
);
359 expand_atom(sb
, start
+ 1, end
- start
- 1, data
);
361 return end
- start
+ 1;
364 static void batch_write(struct batch_options
*opt
, const void *data
, int len
)
366 if (opt
->buffer_output
) {
367 if (fwrite(data
, 1, len
, stdout
) != len
)
368 die_errno("unable to write to stdout");
370 write_or_die(1, data
, len
);
373 static void print_object_or_die(struct batch_options
*opt
, struct expand_data
*data
)
375 const struct object_id
*oid
= &data
->oid
;
377 assert(data
->info
.typep
);
379 if (data
->type
== OBJ_BLOB
) {
380 if (opt
->buffer_output
)
382 if (opt
->transform_mode
) {
387 die("missing path for '%s'", oid_to_hex(oid
));
389 if (opt
->transform_mode
== 'w') {
390 if (filter_object(data
->rest
, 0100644, oid
,
392 die("could not convert '%s' %s",
393 oid_to_hex(oid
), data
->rest
);
394 } else if (opt
->transform_mode
== 'c') {
395 enum object_type type
;
396 if (!textconv_object(the_repository
,
397 data
->rest
, 0100644, oid
,
398 1, &contents
, &size
))
399 contents
= repo_read_object_file(the_repository
,
404 die("could not convert '%s' %s",
405 oid_to_hex(oid
), data
->rest
);
407 BUG("invalid transform_mode: %c", opt
->transform_mode
);
408 batch_write(opt
, contents
, size
);
415 enum object_type type
;
419 contents
= repo_read_object_file(the_repository
, oid
, &type
,
424 contents
= replace_idents_using_mailmap(contents
, &s
);
425 size
= cast_size_t_to_ulong(s
);
429 die("object %s disappeared", oid_to_hex(oid
));
430 if (type
!= data
->type
)
431 die("object %s changed type!?", oid_to_hex(oid
));
432 if (data
->info
.sizep
&& size
!= data
->size
&& !use_mailmap
)
433 die("object %s changed size!?", oid_to_hex(oid
));
435 batch_write(opt
, contents
, size
);
440 static void print_default_format(struct strbuf
*scratch
, struct expand_data
*data
)
442 strbuf_addf(scratch
, "%s %s %"PRIuMAX
"\n", oid_to_hex(&data
->oid
),
443 type_name(data
->type
),
444 (uintmax_t)data
->size
);
448 * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
449 * which the object may be accessed (though note that we may also rely on
450 * data->oid, too). If "pack" is NULL, then offset is ignored.
452 static void batch_object_write(const char *obj_name
,
453 struct strbuf
*scratch
,
454 struct batch_options
*opt
,
455 struct expand_data
*data
,
456 struct packed_git
*pack
,
459 if (!data
->skip_object_info
) {
463 data
->info
.typep
= &data
->type
;
466 ret
= packed_object_info(the_repository
, pack
, offset
,
469 ret
= oid_object_info_extended(the_repository
,
470 &data
->oid
, &data
->info
,
471 OBJECT_INFO_LOOKUP_REPLACE
);
473 printf("%s missing\n",
474 obj_name
? obj_name
: oid_to_hex(&data
->oid
));
479 if (use_mailmap
&& (data
->type
== OBJ_COMMIT
|| data
->type
== OBJ_TAG
)) {
480 size_t s
= data
->size
;
483 buf
= repo_read_object_file(the_repository
, &data
->oid
, &data
->type
,
485 buf
= replace_idents_using_mailmap(buf
, &s
);
486 data
->size
= cast_size_t_to_ulong(s
);
492 strbuf_reset(scratch
);
495 print_default_format(scratch
, data
);
497 strbuf_expand(scratch
, opt
->format
, expand_format
, data
);
498 strbuf_addch(scratch
, '\n');
501 batch_write(opt
, scratch
->buf
, scratch
->len
);
503 if (opt
->batch_mode
== BATCH_MODE_CONTENTS
) {
504 print_object_or_die(opt
, data
);
505 batch_write(opt
, "\n", 1);
509 static void batch_one_object(const char *obj_name
,
510 struct strbuf
*scratch
,
511 struct batch_options
*opt
,
512 struct expand_data
*data
)
514 struct object_context ctx
;
515 int flags
= opt
->follow_symlinks
? GET_OID_FOLLOW_SYMLINKS
: 0;
516 enum get_oid_result result
;
518 result
= get_oid_with_context(the_repository
, obj_name
,
519 flags
, &data
->oid
, &ctx
);
520 if (result
!= FOUND
) {
523 printf("%s missing\n", obj_name
);
525 case SHORT_NAME_AMBIGUOUS
:
526 printf("%s ambiguous\n", obj_name
);
528 case DANGLING_SYMLINK
:
529 printf("dangling %"PRIuMAX
"\n%s\n",
530 (uintmax_t)strlen(obj_name
), obj_name
);
533 printf("loop %"PRIuMAX
"\n%s\n",
534 (uintmax_t)strlen(obj_name
), obj_name
);
537 printf("notdir %"PRIuMAX
"\n%s\n",
538 (uintmax_t)strlen(obj_name
), obj_name
);
541 BUG("unknown get_sha1_with_context result %d\n",
550 printf("symlink %"PRIuMAX
"\n%s\n",
551 (uintmax_t)ctx
.symlink_path
.len
,
552 ctx
.symlink_path
.buf
);
557 batch_object_write(obj_name
, scratch
, opt
, data
, NULL
, 0);
560 struct object_cb_data
{
561 struct batch_options
*opt
;
562 struct expand_data
*expand
;
564 struct strbuf
*scratch
;
567 static int batch_object_cb(const struct object_id
*oid
, void *vdata
)
569 struct object_cb_data
*data
= vdata
;
570 oidcpy(&data
->expand
->oid
, oid
);
571 batch_object_write(NULL
, data
->scratch
, data
->opt
, data
->expand
,
576 static int collect_loose_object(const struct object_id
*oid
,
577 const char *path UNUSED
,
580 oid_array_append(data
, oid
);
584 static int collect_packed_object(const struct object_id
*oid
,
585 struct packed_git
*pack UNUSED
,
589 oid_array_append(data
, oid
);
593 static int batch_unordered_object(const struct object_id
*oid
,
594 struct packed_git
*pack
, off_t offset
,
597 struct object_cb_data
*data
= vdata
;
599 if (oidset_insert(data
->seen
, oid
))
602 oidcpy(&data
->expand
->oid
, oid
);
603 batch_object_write(NULL
, data
->scratch
, data
->opt
, data
->expand
,
608 static int batch_unordered_loose(const struct object_id
*oid
,
609 const char *path UNUSED
,
612 return batch_unordered_object(oid
, NULL
, 0, data
);
615 static int batch_unordered_packed(const struct object_id
*oid
,
616 struct packed_git
*pack
,
620 return batch_unordered_object(oid
, pack
,
621 nth_packed_object_offset(pack
, pos
),
625 typedef void (*parse_cmd_fn_t
)(struct batch_options
*, const char *,
626 struct strbuf
*, struct expand_data
*);
633 static void parse_cmd_contents(struct batch_options
*opt
,
635 struct strbuf
*output
,
636 struct expand_data
*data
)
638 opt
->batch_mode
= BATCH_MODE_CONTENTS
;
639 batch_one_object(line
, output
, opt
, data
);
642 static void parse_cmd_info(struct batch_options
*opt
,
644 struct strbuf
*output
,
645 struct expand_data
*data
)
647 opt
->batch_mode
= BATCH_MODE_INFO
;
648 batch_one_object(line
, output
, opt
, data
);
651 static void dispatch_calls(struct batch_options
*opt
,
652 struct strbuf
*output
,
653 struct expand_data
*data
,
654 struct queued_cmd
*cmd
,
659 if (!opt
->buffer_output
)
660 die(_("flush is only for --buffer mode"));
662 for (i
= 0; i
< nr
; i
++)
663 cmd
[i
].fn(opt
, cmd
[i
].line
, output
, data
);
668 static void free_cmds(struct queued_cmd
*cmd
, size_t *nr
)
672 for (i
= 0; i
< *nr
; i
++)
673 FREE_AND_NULL(cmd
[i
].line
);
679 static const struct parse_cmd
{
684 { "contents", parse_cmd_contents
, 1},
685 { "info", parse_cmd_info
, 1},
689 static void batch_objects_command(struct batch_options
*opt
,
690 struct strbuf
*output
,
691 struct expand_data
*data
)
693 struct strbuf input
= STRBUF_INIT
;
694 struct queued_cmd
*queued_cmd
= NULL
;
695 size_t alloc
= 0, nr
= 0;
699 const struct parse_cmd
*cmd
= NULL
;
700 const char *p
= NULL
, *cmd_end
;
701 struct queued_cmd call
= {0};
703 if (opt
->nul_terminated
)
704 ret
= strbuf_getline_nul(&input
, stdin
);
706 ret
= strbuf_getline(&input
, stdin
);
712 die(_("empty command in input"));
713 if (isspace(*input
.buf
))
714 die(_("whitespace before command: '%s'"), input
.buf
);
716 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
717 if (!skip_prefix(input
.buf
, commands
[i
].name
, &cmd_end
))
721 if (cmd
->takes_args
) {
723 die(_("%s requires arguments"),
727 } else if (*cmd_end
) {
728 die(_("%s takes no arguments"),
736 die(_("unknown command: '%s'"), input
.buf
);
738 if (!strcmp(cmd
->name
, "flush")) {
739 dispatch_calls(opt
, output
, data
, queued_cmd
, nr
);
740 free_cmds(queued_cmd
, &nr
);
741 } else if (!opt
->buffer_output
) {
742 cmd
->fn(opt
, p
, output
, data
);
744 ALLOC_GROW(queued_cmd
, nr
+ 1, alloc
);
746 call
.line
= xstrdup_or_null(p
);
747 queued_cmd
[nr
++] = call
;
751 if (opt
->buffer_output
&&
753 !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) {
754 dispatch_calls(opt
, output
, data
, queued_cmd
, nr
);
755 free_cmds(queued_cmd
, &nr
);
758 free_cmds(queued_cmd
, &nr
);
760 strbuf_release(&input
);
763 #define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
765 static int batch_objects(struct batch_options
*opt
)
767 struct strbuf input
= STRBUF_INIT
;
768 struct strbuf output
= STRBUF_INIT
;
769 struct expand_data data
;
774 * Expand once with our special mark_query flag, which will prime the
775 * object_info to be handed to oid_object_info_extended for each
778 memset(&data
, 0, sizeof(data
));
780 strbuf_expand(&output
,
781 opt
->format
? opt
->format
: DEFAULT_FORMAT
,
785 strbuf_release(&output
);
786 if (opt
->transform_mode
)
787 data
.split_on_whitespace
= 1;
789 if (opt
->format
&& !strcmp(opt
->format
, DEFAULT_FORMAT
))
792 * If we are printing out the object, then always fill in the type,
793 * since we will want to decide whether or not to stream.
795 if (opt
->batch_mode
== BATCH_MODE_CONTENTS
)
796 data
.info
.typep
= &data
.type
;
798 if (opt
->all_objects
) {
799 struct object_cb_data cb
;
800 struct object_info empty
= OBJECT_INFO_INIT
;
802 if (!memcmp(&data
.info
, &empty
, sizeof(empty
)))
803 data
.skip_object_info
= 1;
805 if (repo_has_promisor_remote(the_repository
))
806 warning("This repository uses promisor remotes. Some objects may not be loaded.");
808 read_replace_refs
= 0;
812 cb
.scratch
= &output
;
814 if (opt
->unordered
) {
815 struct oidset seen
= OIDSET_INIT
;
819 for_each_loose_object(batch_unordered_loose
, &cb
, 0);
820 for_each_packed_object(batch_unordered_packed
, &cb
,
821 FOR_EACH_OBJECT_PACK_ORDER
);
825 struct oid_array sa
= OID_ARRAY_INIT
;
827 for_each_loose_object(collect_loose_object
, &sa
, 0);
828 for_each_packed_object(collect_packed_object
, &sa
, 0);
830 oid_array_for_each_unique(&sa
, batch_object_cb
, &cb
);
832 oid_array_clear(&sa
);
835 strbuf_release(&output
);
840 * We are going to call get_sha1 on a potentially very large number of
841 * objects. In most large cases, these will be actual object sha1s. The
842 * cost to double-check that each one is not also a ref (just so we can
843 * warn) ends up dwarfing the actual cost of the object lookups
844 * themselves. We can work around it by just turning off the warning.
846 save_warning
= warn_on_object_refname_ambiguity
;
847 warn_on_object_refname_ambiguity
= 0;
849 if (opt
->batch_mode
== BATCH_MODE_QUEUE_AND_DISPATCH
) {
850 batch_objects_command(opt
, &output
, &data
);
856 if (opt
->nul_terminated
)
857 ret
= strbuf_getline_nul(&input
, stdin
);
859 ret
= strbuf_getline(&input
, stdin
);
864 if (data
.split_on_whitespace
) {
866 * Split at first whitespace, tying off the beginning
867 * of the string and saving the remainder (or NULL) in
870 char *p
= strpbrk(input
.buf
, " \t");
872 while (*p
&& strchr(" \t", *p
))
878 batch_one_object(input
.buf
, &output
, opt
, &data
);
882 strbuf_release(&input
);
883 strbuf_release(&output
);
884 warn_on_object_refname_ambiguity
= save_warning
;
888 static int git_cat_file_config(const char *var
, const char *value
, void *cb
)
890 if (userdiff_config(var
, value
) < 0)
893 return git_default_config(var
, value
, cb
);
896 static int batch_option_callback(const struct option
*opt
,
900 struct batch_options
*bo
= opt
->value
;
902 BUG_ON_OPT_NEG(unset
);
905 return error(_("only one batch option may be specified"));
910 if (!strcmp(opt
->long_name
, "batch"))
911 bo
->batch_mode
= BATCH_MODE_CONTENTS
;
912 else if (!strcmp(opt
->long_name
, "batch-check"))
913 bo
->batch_mode
= BATCH_MODE_INFO
;
914 else if (!strcmp(opt
->long_name
, "batch-command"))
915 bo
->batch_mode
= BATCH_MODE_QUEUE_AND_DISPATCH
;
917 BUG("%s given to batch-option-callback", opt
->long_name
);
924 int cmd_cat_file(int argc
, const char **argv
, const char *prefix
)
929 const char *exp_type
= NULL
, *obj_name
= NULL
;
930 struct batch_options batch
= {0};
931 int unknown_type
= 0;
933 const char * const usage
[] = {
934 N_("git cat-file <type> <object>"),
935 N_("git cat-file (-e | -p) <object>"),
936 N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"),
937 N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
938 " [--buffer] [--follow-symlinks] [--unordered]\n"
939 " [--textconv | --filters] [-z]"),
940 N_("git cat-file (--textconv | --filters)\n"
941 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"),
944 const struct option options
[] = {
946 OPT_GROUP(N_("Check object existence or emit object contents")),
947 OPT_CMDMODE('e', NULL
, &opt
,
948 N_("check if <object> exists"), 'e'),
949 OPT_CMDMODE('p', NULL
, &opt
, N_("pretty-print <object> content"), 'p'),
951 OPT_GROUP(N_("Emit [broken] object attributes")),
952 OPT_CMDMODE('t', NULL
, &opt
, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'),
953 OPT_CMDMODE('s', NULL
, &opt
, N_("show object size"), 's'),
954 OPT_BOOL(0, "allow-unknown-type", &unknown_type
,
955 N_("allow -s and -t to work with broken/corrupt objects")),
956 OPT_BOOL(0, "use-mailmap", &use_mailmap
, N_("use mail map file")),
957 OPT_ALIAS(0, "mailmap", "use-mailmap"),
959 OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")),
960 OPT_CALLBACK_F(0, "batch", &batch
, N_("format"),
961 N_("show full <object> or <rev> contents"),
962 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
963 batch_option_callback
),
964 OPT_CALLBACK_F(0, "batch-check", &batch
, N_("format"),
965 N_("like --batch, but don't emit <contents>"),
966 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
967 batch_option_callback
),
968 OPT_BOOL('z', NULL
, &batch
.nul_terminated
, N_("stdin is NUL-terminated")),
969 OPT_CALLBACK_F(0, "batch-command", &batch
, N_("format"),
970 N_("read commands from stdin"),
971 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
972 batch_option_callback
),
973 OPT_CMDMODE(0, "batch-all-objects", &opt
,
974 N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'),
975 /* Batch-specific options */
976 OPT_GROUP(N_("Change or optimize batch output")),
977 OPT_BOOL(0, "buffer", &batch
.buffer_output
, N_("buffer --batch output")),
978 OPT_BOOL(0, "follow-symlinks", &batch
.follow_symlinks
,
979 N_("follow in-tree symlinks")),
980 OPT_BOOL(0, "unordered", &batch
.unordered
,
981 N_("do not order objects before emitting them")),
982 /* Textconv options, stand-ole*/
983 OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")),
984 OPT_CMDMODE(0, "textconv", &opt
,
985 N_("run textconv on object's content"), 'c'),
986 OPT_CMDMODE(0, "filters", &opt
,
987 N_("run filters on object's content"), 'w'),
988 OPT_STRING(0, "path", &force_path
, N_("blob|tree"),
989 N_("use a <path> for (--textconv | --filters); Not with 'batch'")),
993 git_config(git_cat_file_config
, NULL
);
995 batch
.buffer_output
= -1;
997 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
998 opt_cw
= (opt
== 'c' || opt
== 'w');
999 opt_epts
= (opt
== 'e' || opt
== 'p' || opt
== 't' || opt
== 's');
1002 read_mailmap(&mailmap
);
1004 /* --batch-all-objects? */
1006 batch
.all_objects
= 1;
1008 /* Option compatibility */
1009 if (force_path
&& !opt_cw
)
1010 usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
1012 "--path", _("path|tree-ish"), "--filters",
1015 /* Option compatibility with batch mode */
1018 else if (batch
.follow_symlinks
)
1019 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1020 "--follow-symlinks");
1021 else if (batch
.buffer_output
>= 0)
1022 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1024 else if (batch
.all_objects
)
1025 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1026 "--batch-all-objects");
1027 else if (batch
.nul_terminated
)
1028 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1031 /* Batch defaults */
1032 if (batch
.buffer_output
< 0)
1033 batch
.buffer_output
= batch
.all_objects
;
1035 /* Return early if we're in batch mode? */
1036 if (batch
.enabled
) {
1038 batch
.transform_mode
= opt
;
1039 else if (opt
&& opt
!= 'b')
1040 usage_msg_optf(_("'-%c' is incompatible with batch mode"),
1041 usage
, options
, opt
);
1043 usage_msg_opt(_("batch modes take no arguments"), usage
,
1046 return batch_objects(&batch
);
1050 if (!argc
&& opt
== 'c')
1051 usage_msg_optf(_("<rev> required with '%s'"),
1052 usage
, options
, "--textconv");
1053 else if (!argc
&& opt
== 'w')
1054 usage_msg_optf(_("<rev> required with '%s'"),
1055 usage
, options
, "--filters");
1056 else if (!argc
&& opt_epts
)
1057 usage_msg_optf(_("<object> required with '-%c'"),
1058 usage
, options
, opt
);
1062 usage_msg_opt(_("too many arguments"), usage
, options
);
1064 usage_with_options(usage
, options
);
1065 } else if (argc
!= 2) {
1066 usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"),
1067 usage
, options
, argc
);
1073 if (unknown_type
&& opt
!= 't' && opt
!= 's')
1074 die("git cat-file --allow-unknown-type: use with -s or -t");
1075 return cat_one_file(opt
, exp_type
, obj_name
, unknown_type
);