2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #define USE_THE_INDEX_VARIABLE
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"
24 BATCH_MODE_QUEUE_AND_DISPATCH
,
27 struct batch_options
{
30 enum batch_mode batch_mode
;
34 int transform_mode
; /* may be 'w' or 'c' for --filters or --textconv */
39 static const char *force_path
;
41 static struct string_list mailmap
= STRING_LIST_INIT_NODUP
;
42 static int use_mailmap
;
44 static char *replace_idents_using_mailmap(char *, size_t *);
46 static char *replace_idents_using_mailmap(char *object_buf
, size_t *size
)
48 struct strbuf sb
= STRBUF_INIT
;
49 const char *headers
[] = { "author ", "committer ", "tagger ", NULL
};
51 strbuf_attach(&sb
, object_buf
, *size
, *size
+ 1);
52 apply_mailmap_to_header(&sb
, headers
, &mailmap
);
54 return strbuf_detach(&sb
, NULL
);
57 static int filter_object(const char *path
, unsigned mode
,
58 const struct object_id
*oid
,
59 char **buf
, unsigned long *size
)
61 enum object_type type
;
63 *buf
= read_object_file(oid
, &type
, size
);
65 return error(_("cannot read object %s '%s'"),
66 oid_to_hex(oid
), path
);
67 if ((type
== OBJ_BLOB
) && S_ISREG(mode
)) {
68 struct strbuf strbuf
= STRBUF_INIT
;
69 struct checkout_metadata meta
;
71 init_checkout_metadata(&meta
, NULL
, NULL
, oid
);
72 if (convert_to_working_tree(&the_index
, path
, *buf
, *size
, &strbuf
, &meta
)) {
75 *buf
= strbuf_detach(&strbuf
, NULL
);
82 static int stream_blob(const struct object_id
*oid
)
84 if (stream_blob_to_fd(1, oid
, NULL
, 0))
85 die("unable to stream %s to stdout", oid_to_hex(oid
));
89 static int cat_one_file(int opt
, const char *exp_type
, const char *obj_name
,
94 enum object_type type
;
97 struct object_context obj_context
;
98 struct object_info oi
= OBJECT_INFO_INIT
;
99 struct strbuf sb
= STRBUF_INIT
;
100 unsigned flags
= OBJECT_INFO_LOOKUP_REPLACE
;
101 unsigned get_oid_flags
= GET_OID_RECORD_PATH
| GET_OID_ONLY_TO_DIE
;
102 const char *path
= force_path
;
103 const int opt_cw
= (opt
== 'c' || opt
== 'w');
105 get_oid_flags
|= GET_OID_REQUIRE_PATH
;
108 flags
|= OBJECT_INFO_ALLOW_UNKNOWN_TYPE
;
110 if (get_oid_with_context(the_repository
, obj_name
, get_oid_flags
, &oid
,
112 die("Not a valid object name %s", obj_name
);
115 path
= obj_context
.path
;
116 if (obj_context
.mode
== S_IFINVALID
)
117 obj_context
.mode
= 0100644;
123 if (oid_object_info_extended(the_repository
, &oid
, &oi
, flags
) < 0)
124 die("git cat-file: could not get object info");
126 printf("%s\n", sb
.buf
);
138 oi
.contentp
= (void**)&buf
;
141 if (oid_object_info_extended(the_repository
, &oid
, &oi
, flags
) < 0)
142 die("git cat-file: could not get object info");
144 if (use_mailmap
&& (type
== OBJ_COMMIT
|| type
== OBJ_TAG
)) {
146 buf
= replace_idents_using_mailmap(buf
, &s
);
147 size
= cast_size_t_to_ulong(s
);
150 printf("%"PRIuMAX
"\n", (uintmax_t)size
);
155 return !has_object_file(&oid
);
159 if (filter_object(path
, obj_context
.mode
,
160 &oid
, &buf
, &size
)) {
167 if (textconv_object(the_repository
, path
, obj_context
.mode
,
168 &oid
, 1, &buf
, &size
))
170 /* else fallthrough */
173 type
= oid_object_info(the_repository
, &oid
, NULL
);
175 die("Not a valid object name %s", obj_name
);
177 /* custom pretty-print here */
178 if (type
== OBJ_TREE
) {
179 const char *ls_args
[3] = { NULL
};
180 ls_args
[0] = "ls-tree";
181 ls_args
[1] = obj_name
;
182 ret
= cmd_ls_tree(2, ls_args
, NULL
);
186 if (type
== OBJ_BLOB
) {
187 ret
= stream_blob(&oid
);
190 buf
= read_object_file(&oid
, &type
, &size
);
192 die("Cannot read object %s", obj_name
);
196 buf
= replace_idents_using_mailmap(buf
, &s
);
197 size
= cast_size_t_to_ulong(s
);
200 /* otherwise just spit out the data */
205 enum object_type exp_type_id
= type_from_string(exp_type
);
207 if (exp_type_id
== OBJ_BLOB
) {
208 struct object_id blob_oid
;
209 if (oid_object_info(the_repository
, &oid
, NULL
) == OBJ_TAG
) {
210 char *buffer
= read_object_file(&oid
, &type
,
213 if (!skip_prefix(buffer
, "object ", &target
) ||
214 get_oid_hex(target
, &blob_oid
))
215 die("%s not a valid tag", oid_to_hex(&oid
));
218 oidcpy(&blob_oid
, &oid
);
220 if (oid_object_info(the_repository
, &blob_oid
, NULL
) == OBJ_BLOB
) {
221 ret
= stream_blob(&blob_oid
);
225 * we attempted to dereference a tag to a blob
226 * and failed; there may be new dereference
227 * mechanisms this code is not aware of.
228 * fall-back to the usual case.
231 buf
= read_object_with_reference(the_repository
, &oid
,
232 exp_type_id
, &size
, NULL
);
236 buf
= replace_idents_using_mailmap(buf
, &s
);
237 size
= cast_size_t_to_ulong(s
);
242 die("git cat-file: unknown option: %s", exp_type
);
246 die("git cat-file %s: bad file", obj_name
);
248 write_or_die(1, buf
, size
);
252 free(obj_context
.path
);
257 struct object_id oid
;
258 enum object_type type
;
262 struct object_id delta_base_oid
;
265 * If mark_query is true, we do not expand anything, but rather
266 * just mark the object_info with items we wish to query.
271 * Whether to split the input on whitespace before feeding it to
272 * get_sha1; this is decided during the mark_query phase based on
273 * whether we have a %(rest) token in our format.
275 int split_on_whitespace
;
278 * After a mark_query run, this object_info is set up to be
279 * passed to oid_object_info_extended. It will point to the data
280 * elements above, so you can retrieve the response from there.
282 struct object_info info
;
285 * This flag will be true if the requested batch format and options
286 * don't require us to call oid_object_info, which can then be
289 unsigned skip_object_info
: 1;
292 static int is_atom(const char *atom
, const char *s
, int slen
)
294 int alen
= strlen(atom
);
295 return alen
== slen
&& !memcmp(atom
, s
, alen
);
298 static void expand_atom(struct strbuf
*sb
, const char *atom
, int len
,
301 struct expand_data
*data
= vdata
;
303 if (is_atom("objectname", atom
, len
)) {
304 if (!data
->mark_query
)
305 strbuf_addstr(sb
, oid_to_hex(&data
->oid
));
306 } else if (is_atom("objecttype", atom
, len
)) {
307 if (data
->mark_query
)
308 data
->info
.typep
= &data
->type
;
310 strbuf_addstr(sb
, type_name(data
->type
));
311 } else if (is_atom("objectsize", atom
, len
)) {
312 if (data
->mark_query
)
313 data
->info
.sizep
= &data
->size
;
315 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->size
);
316 } else if (is_atom("objectsize:disk", atom
, len
)) {
317 if (data
->mark_query
)
318 data
->info
.disk_sizep
= &data
->disk_size
;
320 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->disk_size
);
321 } else if (is_atom("rest", atom
, len
)) {
322 if (data
->mark_query
)
323 data
->split_on_whitespace
= 1;
325 strbuf_addstr(sb
, data
->rest
);
326 } else if (is_atom("deltabase", atom
, len
)) {
327 if (data
->mark_query
)
328 data
->info
.delta_base_oid
= &data
->delta_base_oid
;
331 oid_to_hex(&data
->delta_base_oid
));
333 die("unknown format element: %.*s", len
, atom
);
336 static size_t expand_format(struct strbuf
*sb
, const char *start
, void *data
)
342 end
= strchr(start
+ 1, ')');
344 die("format element '%s' does not end in ')'", start
);
346 expand_atom(sb
, start
+ 1, end
- start
- 1, data
);
348 return end
- start
+ 1;
351 static void batch_write(struct batch_options
*opt
, const void *data
, int len
)
353 if (opt
->buffer_output
) {
354 if (fwrite(data
, 1, len
, stdout
) != len
)
355 die_errno("unable to write to stdout");
357 write_or_die(1, data
, len
);
360 static void print_object_or_die(struct batch_options
*opt
, struct expand_data
*data
)
362 const struct object_id
*oid
= &data
->oid
;
364 assert(data
->info
.typep
);
366 if (data
->type
== OBJ_BLOB
) {
367 if (opt
->buffer_output
)
369 if (opt
->transform_mode
) {
374 die("missing path for '%s'", oid_to_hex(oid
));
376 if (opt
->transform_mode
== 'w') {
377 if (filter_object(data
->rest
, 0100644, oid
,
379 die("could not convert '%s' %s",
380 oid_to_hex(oid
), data
->rest
);
381 } else if (opt
->transform_mode
== 'c') {
382 enum object_type type
;
383 if (!textconv_object(the_repository
,
384 data
->rest
, 0100644, oid
,
385 1, &contents
, &size
))
386 contents
= read_object_file(oid
,
390 die("could not convert '%s' %s",
391 oid_to_hex(oid
), data
->rest
);
393 BUG("invalid transform_mode: %c", opt
->transform_mode
);
394 batch_write(opt
, contents
, size
);
401 enum object_type type
;
405 contents
= read_object_file(oid
, &type
, &size
);
409 contents
= replace_idents_using_mailmap(contents
, &s
);
410 size
= cast_size_t_to_ulong(s
);
414 die("object %s disappeared", oid_to_hex(oid
));
415 if (type
!= data
->type
)
416 die("object %s changed type!?", oid_to_hex(oid
));
417 if (data
->info
.sizep
&& size
!= data
->size
&& !use_mailmap
)
418 die("object %s changed size!?", oid_to_hex(oid
));
420 batch_write(opt
, contents
, size
);
425 static void print_default_format(struct strbuf
*scratch
, struct expand_data
*data
)
427 strbuf_addf(scratch
, "%s %s %"PRIuMAX
"\n", oid_to_hex(&data
->oid
),
428 type_name(data
->type
),
429 (uintmax_t)data
->size
);
433 * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
434 * which the object may be accessed (though note that we may also rely on
435 * data->oid, too). If "pack" is NULL, then offset is ignored.
437 static void batch_object_write(const char *obj_name
,
438 struct strbuf
*scratch
,
439 struct batch_options
*opt
,
440 struct expand_data
*data
,
441 struct packed_git
*pack
,
444 if (!data
->skip_object_info
) {
448 data
->info
.typep
= &data
->type
;
451 ret
= packed_object_info(the_repository
, pack
, offset
,
454 ret
= oid_object_info_extended(the_repository
,
455 &data
->oid
, &data
->info
,
456 OBJECT_INFO_LOOKUP_REPLACE
);
458 printf("%s missing\n",
459 obj_name
? obj_name
: oid_to_hex(&data
->oid
));
464 if (use_mailmap
&& (data
->type
== OBJ_COMMIT
|| data
->type
== OBJ_TAG
)) {
465 size_t s
= data
->size
;
468 buf
= repo_read_object_file(the_repository
, &data
->oid
, &data
->type
,
470 buf
= replace_idents_using_mailmap(buf
, &s
);
471 data
->size
= cast_size_t_to_ulong(s
);
477 strbuf_reset(scratch
);
480 print_default_format(scratch
, data
);
482 strbuf_expand(scratch
, opt
->format
, expand_format
, data
);
483 strbuf_addch(scratch
, '\n');
486 batch_write(opt
, scratch
->buf
, scratch
->len
);
488 if (opt
->batch_mode
== BATCH_MODE_CONTENTS
) {
489 print_object_or_die(opt
, data
);
490 batch_write(opt
, "\n", 1);
494 static void batch_one_object(const char *obj_name
,
495 struct strbuf
*scratch
,
496 struct batch_options
*opt
,
497 struct expand_data
*data
)
499 struct object_context ctx
;
500 int flags
= opt
->follow_symlinks
? GET_OID_FOLLOW_SYMLINKS
: 0;
501 enum get_oid_result result
;
503 result
= get_oid_with_context(the_repository
, obj_name
,
504 flags
, &data
->oid
, &ctx
);
505 if (result
!= FOUND
) {
508 printf("%s missing\n", obj_name
);
510 case SHORT_NAME_AMBIGUOUS
:
511 printf("%s ambiguous\n", obj_name
);
513 case DANGLING_SYMLINK
:
514 printf("dangling %"PRIuMAX
"\n%s\n",
515 (uintmax_t)strlen(obj_name
), obj_name
);
518 printf("loop %"PRIuMAX
"\n%s\n",
519 (uintmax_t)strlen(obj_name
), obj_name
);
522 printf("notdir %"PRIuMAX
"\n%s\n",
523 (uintmax_t)strlen(obj_name
), obj_name
);
526 BUG("unknown get_sha1_with_context result %d\n",
535 printf("symlink %"PRIuMAX
"\n%s\n",
536 (uintmax_t)ctx
.symlink_path
.len
,
537 ctx
.symlink_path
.buf
);
542 batch_object_write(obj_name
, scratch
, opt
, data
, NULL
, 0);
545 struct object_cb_data
{
546 struct batch_options
*opt
;
547 struct expand_data
*expand
;
549 struct strbuf
*scratch
;
552 static int batch_object_cb(const struct object_id
*oid
, void *vdata
)
554 struct object_cb_data
*data
= vdata
;
555 oidcpy(&data
->expand
->oid
, oid
);
556 batch_object_write(NULL
, data
->scratch
, data
->opt
, data
->expand
,
561 static int collect_loose_object(const struct object_id
*oid
,
565 oid_array_append(data
, oid
);
569 static int collect_packed_object(const struct object_id
*oid
,
570 struct packed_git
*pack
,
574 oid_array_append(data
, oid
);
578 static int batch_unordered_object(const struct object_id
*oid
,
579 struct packed_git
*pack
, off_t offset
,
582 struct object_cb_data
*data
= vdata
;
584 if (oidset_insert(data
->seen
, oid
))
587 oidcpy(&data
->expand
->oid
, oid
);
588 batch_object_write(NULL
, data
->scratch
, data
->opt
, data
->expand
,
593 static int batch_unordered_loose(const struct object_id
*oid
,
597 return batch_unordered_object(oid
, NULL
, 0, data
);
600 static int batch_unordered_packed(const struct object_id
*oid
,
601 struct packed_git
*pack
,
605 return batch_unordered_object(oid
, pack
,
606 nth_packed_object_offset(pack
, pos
),
610 typedef void (*parse_cmd_fn_t
)(struct batch_options
*, const char *,
611 struct strbuf
*, struct expand_data
*);
618 static void parse_cmd_contents(struct batch_options
*opt
,
620 struct strbuf
*output
,
621 struct expand_data
*data
)
623 opt
->batch_mode
= BATCH_MODE_CONTENTS
;
624 batch_one_object(line
, output
, opt
, data
);
627 static void parse_cmd_info(struct batch_options
*opt
,
629 struct strbuf
*output
,
630 struct expand_data
*data
)
632 opt
->batch_mode
= BATCH_MODE_INFO
;
633 batch_one_object(line
, output
, opt
, data
);
636 static void dispatch_calls(struct batch_options
*opt
,
637 struct strbuf
*output
,
638 struct expand_data
*data
,
639 struct queued_cmd
*cmd
,
644 if (!opt
->buffer_output
)
645 die(_("flush is only for --buffer mode"));
647 for (i
= 0; i
< nr
; i
++)
648 cmd
[i
].fn(opt
, cmd
[i
].line
, output
, data
);
653 static void free_cmds(struct queued_cmd
*cmd
, size_t *nr
)
657 for (i
= 0; i
< *nr
; i
++)
658 FREE_AND_NULL(cmd
[i
].line
);
664 static const struct parse_cmd
{
669 { "contents", parse_cmd_contents
, 1},
670 { "info", parse_cmd_info
, 1},
674 static void batch_objects_command(struct batch_options
*opt
,
675 struct strbuf
*output
,
676 struct expand_data
*data
)
678 struct strbuf input
= STRBUF_INIT
;
679 struct queued_cmd
*queued_cmd
= NULL
;
680 size_t alloc
= 0, nr
= 0;
684 const struct parse_cmd
*cmd
= NULL
;
685 const char *p
= NULL
, *cmd_end
;
686 struct queued_cmd call
= {0};
688 if (opt
->nul_terminated
)
689 ret
= strbuf_getline_nul(&input
, stdin
);
691 ret
= strbuf_getline(&input
, stdin
);
697 die(_("empty command in input"));
698 if (isspace(*input
.buf
))
699 die(_("whitespace before command: '%s'"), input
.buf
);
701 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
702 if (!skip_prefix(input
.buf
, commands
[i
].name
, &cmd_end
))
706 if (cmd
->takes_args
) {
708 die(_("%s requires arguments"),
712 } else if (*cmd_end
) {
713 die(_("%s takes no arguments"),
721 die(_("unknown command: '%s'"), input
.buf
);
723 if (!strcmp(cmd
->name
, "flush")) {
724 dispatch_calls(opt
, output
, data
, queued_cmd
, nr
);
725 free_cmds(queued_cmd
, &nr
);
726 } else if (!opt
->buffer_output
) {
727 cmd
->fn(opt
, p
, output
, data
);
729 ALLOC_GROW(queued_cmd
, nr
+ 1, alloc
);
731 call
.line
= xstrdup_or_null(p
);
732 queued_cmd
[nr
++] = call
;
736 if (opt
->buffer_output
&&
738 !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) {
739 dispatch_calls(opt
, output
, data
, queued_cmd
, nr
);
740 free_cmds(queued_cmd
, &nr
);
743 free_cmds(queued_cmd
, &nr
);
745 strbuf_release(&input
);
748 #define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
750 static int batch_objects(struct batch_options
*opt
)
752 struct strbuf input
= STRBUF_INIT
;
753 struct strbuf output
= STRBUF_INIT
;
754 struct expand_data data
;
759 * Expand once with our special mark_query flag, which will prime the
760 * object_info to be handed to oid_object_info_extended for each
763 memset(&data
, 0, sizeof(data
));
765 strbuf_expand(&output
,
766 opt
->format
? opt
->format
: DEFAULT_FORMAT
,
770 strbuf_release(&output
);
771 if (opt
->transform_mode
)
772 data
.split_on_whitespace
= 1;
774 if (opt
->format
&& !strcmp(opt
->format
, DEFAULT_FORMAT
))
777 * If we are printing out the object, then always fill in the type,
778 * since we will want to decide whether or not to stream.
780 if (opt
->batch_mode
== BATCH_MODE_CONTENTS
)
781 data
.info
.typep
= &data
.type
;
783 if (opt
->all_objects
) {
784 struct object_cb_data cb
;
785 struct object_info empty
= OBJECT_INFO_INIT
;
787 if (!memcmp(&data
.info
, &empty
, sizeof(empty
)))
788 data
.skip_object_info
= 1;
790 if (has_promisor_remote())
791 warning("This repository uses promisor remotes. Some objects may not be loaded.");
793 read_replace_refs
= 0;
797 cb
.scratch
= &output
;
799 if (opt
->unordered
) {
800 struct oidset seen
= OIDSET_INIT
;
804 for_each_loose_object(batch_unordered_loose
, &cb
, 0);
805 for_each_packed_object(batch_unordered_packed
, &cb
,
806 FOR_EACH_OBJECT_PACK_ORDER
);
810 struct oid_array sa
= OID_ARRAY_INIT
;
812 for_each_loose_object(collect_loose_object
, &sa
, 0);
813 for_each_packed_object(collect_packed_object
, &sa
, 0);
815 oid_array_for_each_unique(&sa
, batch_object_cb
, &cb
);
817 oid_array_clear(&sa
);
820 strbuf_release(&output
);
825 * We are going to call get_sha1 on a potentially very large number of
826 * objects. In most large cases, these will be actual object sha1s. The
827 * cost to double-check that each one is not also a ref (just so we can
828 * warn) ends up dwarfing the actual cost of the object lookups
829 * themselves. We can work around it by just turning off the warning.
831 save_warning
= warn_on_object_refname_ambiguity
;
832 warn_on_object_refname_ambiguity
= 0;
834 if (opt
->batch_mode
== BATCH_MODE_QUEUE_AND_DISPATCH
) {
835 batch_objects_command(opt
, &output
, &data
);
841 if (opt
->nul_terminated
)
842 ret
= strbuf_getline_nul(&input
, stdin
);
844 ret
= strbuf_getline(&input
, stdin
);
849 if (data
.split_on_whitespace
) {
851 * Split at first whitespace, tying off the beginning
852 * of the string and saving the remainder (or NULL) in
855 char *p
= strpbrk(input
.buf
, " \t");
857 while (*p
&& strchr(" \t", *p
))
863 batch_one_object(input
.buf
, &output
, opt
, &data
);
867 strbuf_release(&input
);
868 strbuf_release(&output
);
869 warn_on_object_refname_ambiguity
= save_warning
;
873 static int git_cat_file_config(const char *var
, const char *value
, void *cb
)
875 if (userdiff_config(var
, value
) < 0)
878 return git_default_config(var
, value
, cb
);
881 static int batch_option_callback(const struct option
*opt
,
885 struct batch_options
*bo
= opt
->value
;
887 BUG_ON_OPT_NEG(unset
);
890 return error(_("only one batch option may be specified"));
895 if (!strcmp(opt
->long_name
, "batch"))
896 bo
->batch_mode
= BATCH_MODE_CONTENTS
;
897 else if (!strcmp(opt
->long_name
, "batch-check"))
898 bo
->batch_mode
= BATCH_MODE_INFO
;
899 else if (!strcmp(opt
->long_name
, "batch-command"))
900 bo
->batch_mode
= BATCH_MODE_QUEUE_AND_DISPATCH
;
902 BUG("%s given to batch-option-callback", opt
->long_name
);
909 int cmd_cat_file(int argc
, const char **argv
, const char *prefix
)
914 const char *exp_type
= NULL
, *obj_name
= NULL
;
915 struct batch_options batch
= {0};
916 int unknown_type
= 0;
918 const char * const usage
[] = {
919 N_("git cat-file <type> <object>"),
920 N_("git cat-file (-e | -p) <object>"),
921 N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"),
922 N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
923 " [--buffer] [--follow-symlinks] [--unordered]\n"
924 " [--textconv | --filters] [-z]"),
925 N_("git cat-file (--textconv | --filters)\n"
926 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"),
929 const struct option options
[] = {
931 OPT_GROUP(N_("Check object existence or emit object contents")),
932 OPT_CMDMODE('e', NULL
, &opt
,
933 N_("check if <object> exists"), 'e'),
934 OPT_CMDMODE('p', NULL
, &opt
, N_("pretty-print <object> content"), 'p'),
936 OPT_GROUP(N_("Emit [broken] object attributes")),
937 OPT_CMDMODE('t', NULL
, &opt
, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'),
938 OPT_CMDMODE('s', NULL
, &opt
, N_("show object size"), 's'),
939 OPT_BOOL(0, "allow-unknown-type", &unknown_type
,
940 N_("allow -s and -t to work with broken/corrupt objects")),
941 OPT_BOOL(0, "use-mailmap", &use_mailmap
, N_("use mail map file")),
942 OPT_ALIAS(0, "mailmap", "use-mailmap"),
944 OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")),
945 OPT_CALLBACK_F(0, "batch", &batch
, N_("format"),
946 N_("show full <object> or <rev> contents"),
947 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
948 batch_option_callback
),
949 OPT_CALLBACK_F(0, "batch-check", &batch
, N_("format"),
950 N_("like --batch, but don't emit <contents>"),
951 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
952 batch_option_callback
),
953 OPT_BOOL('z', NULL
, &batch
.nul_terminated
, N_("stdin is NUL-terminated")),
954 OPT_CALLBACK_F(0, "batch-command", &batch
, N_("format"),
955 N_("read commands from stdin"),
956 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
957 batch_option_callback
),
958 OPT_CMDMODE(0, "batch-all-objects", &opt
,
959 N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'),
960 /* Batch-specific options */
961 OPT_GROUP(N_("Change or optimize batch output")),
962 OPT_BOOL(0, "buffer", &batch
.buffer_output
, N_("buffer --batch output")),
963 OPT_BOOL(0, "follow-symlinks", &batch
.follow_symlinks
,
964 N_("follow in-tree symlinks")),
965 OPT_BOOL(0, "unordered", &batch
.unordered
,
966 N_("do not order objects before emitting them")),
967 /* Textconv options, stand-ole*/
968 OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")),
969 OPT_CMDMODE(0, "textconv", &opt
,
970 N_("run textconv on object's content"), 'c'),
971 OPT_CMDMODE(0, "filters", &opt
,
972 N_("run filters on object's content"), 'w'),
973 OPT_STRING(0, "path", &force_path
, N_("blob|tree"),
974 N_("use a <path> for (--textconv | --filters); Not with 'batch'")),
978 git_config(git_cat_file_config
, NULL
);
980 batch
.buffer_output
= -1;
982 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
983 opt_cw
= (opt
== 'c' || opt
== 'w');
984 opt_epts
= (opt
== 'e' || opt
== 'p' || opt
== 't' || opt
== 's');
987 read_mailmap(&mailmap
);
989 /* --batch-all-objects? */
991 batch
.all_objects
= 1;
993 /* Option compatibility */
994 if (force_path
&& !opt_cw
)
995 usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
997 "--path", _("path|tree-ish"), "--filters",
1000 /* Option compatibility with batch mode */
1003 else if (batch
.follow_symlinks
)
1004 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1005 "--follow-symlinks");
1006 else if (batch
.buffer_output
>= 0)
1007 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1009 else if (batch
.all_objects
)
1010 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1011 "--batch-all-objects");
1012 else if (batch
.nul_terminated
)
1013 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1016 /* Batch defaults */
1017 if (batch
.buffer_output
< 0)
1018 batch
.buffer_output
= batch
.all_objects
;
1020 /* Return early if we're in batch mode? */
1021 if (batch
.enabled
) {
1023 batch
.transform_mode
= opt
;
1024 else if (opt
&& opt
!= 'b')
1025 usage_msg_optf(_("'-%c' is incompatible with batch mode"),
1026 usage
, options
, opt
);
1028 usage_msg_opt(_("batch modes take no arguments"), usage
,
1031 return batch_objects(&batch
);
1035 if (!argc
&& opt
== 'c')
1036 usage_msg_optf(_("<rev> required with '%s'"),
1037 usage
, options
, "--textconv");
1038 else if (!argc
&& opt
== 'w')
1039 usage_msg_optf(_("<rev> required with '%s'"),
1040 usage
, options
, "--filters");
1041 else if (!argc
&& opt_epts
)
1042 usage_msg_optf(_("<object> required with '-%c'"),
1043 usage
, options
, opt
);
1047 usage_msg_opt(_("too many arguments"), usage
, options
);
1049 usage_with_options(usage
, options
);
1050 } else if (argc
!= 2) {
1051 usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"),
1052 usage
, options
, argc
);
1058 if (unknown_type
&& opt
!= 't' && opt
!= 's')
1059 die("git cat-file --allow-unknown-type: use with -s or -t");
1060 return cat_one_file(opt
, exp_type
, obj_name
, unknown_type
);