2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
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
;
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 return !repo_has_object_file(the_repository
, &oid
);
170 if (filter_object(path
, obj_context
.mode
,
171 &oid
, &buf
, &size
)) {
178 if (textconv_object(the_repository
, path
, obj_context
.mode
,
179 &oid
, 1, &buf
, &size
))
181 /* else fallthrough */
184 type
= oid_object_info(the_repository
, &oid
, NULL
);
186 die("Not a valid object name %s", obj_name
);
188 /* custom pretty-print here */
189 if (type
== OBJ_TREE
) {
190 const char *ls_args
[3] = { NULL
};
191 ls_args
[0] = "ls-tree";
192 ls_args
[1] = obj_name
;
193 ret
= cmd_ls_tree(2, ls_args
, NULL
);
197 if (type
== OBJ_BLOB
) {
198 ret
= stream_blob(&oid
);
201 buf
= repo_read_object_file(the_repository
, &oid
, &type
,
204 die("Cannot read object %s", obj_name
);
208 buf
= replace_idents_using_mailmap(buf
, &s
);
209 size
= cast_size_t_to_ulong(s
);
212 /* otherwise just spit out the data */
217 enum object_type exp_type_id
= type_from_string(exp_type
);
219 if (exp_type_id
== OBJ_BLOB
) {
220 struct object_id blob_oid
;
221 if (oid_object_info(the_repository
, &oid
, NULL
) == OBJ_TAG
) {
222 char *buffer
= repo_read_object_file(the_repository
,
229 die(_("unable to read %s"), oid_to_hex(&oid
));
231 if (!skip_prefix(buffer
, "object ", &target
) ||
232 get_oid_hex_algop(target
, &blob_oid
,
233 &hash_algos
[oid
.algo
]))
234 die("%s not a valid tag", oid_to_hex(&oid
));
237 oidcpy(&blob_oid
, &oid
);
239 if (oid_object_info(the_repository
, &blob_oid
, NULL
) == OBJ_BLOB
) {
240 ret
= stream_blob(&blob_oid
);
244 * we attempted to dereference a tag to a blob
245 * and failed; there may be new dereference
246 * mechanisms this code is not aware of.
247 * fall-back to the usual case.
250 buf
= read_object_with_reference(the_repository
, &oid
,
251 exp_type_id
, &size
, NULL
);
255 buf
= replace_idents_using_mailmap(buf
, &s
);
256 size
= cast_size_t_to_ulong(s
);
261 die("git cat-file: unknown option: %s", exp_type
);
265 die("git cat-file %s: bad file", obj_name
);
267 write_or_die(1, buf
, size
);
271 free(obj_context
.path
);
276 struct object_id oid
;
277 enum object_type type
;
281 struct object_id delta_base_oid
;
284 * If mark_query is true, we do not expand anything, but rather
285 * just mark the object_info with items we wish to query.
290 * Whether to split the input on whitespace before feeding it to
291 * get_sha1; this is decided during the mark_query phase based on
292 * whether we have a %(rest) token in our format.
294 int split_on_whitespace
;
297 * After a mark_query run, this object_info is set up to be
298 * passed to oid_object_info_extended. It will point to the data
299 * elements above, so you can retrieve the response from there.
301 struct object_info info
;
304 * This flag will be true if the requested batch format and options
305 * don't require us to call oid_object_info, which can then be
308 unsigned skip_object_info
: 1;
311 static int is_atom(const char *atom
, const char *s
, int slen
)
313 int alen
= strlen(atom
);
314 return alen
== slen
&& !memcmp(atom
, s
, alen
);
317 static int expand_atom(struct strbuf
*sb
, const char *atom
, int len
,
318 struct expand_data
*data
)
320 if (is_atom("objectname", atom
, len
)) {
321 if (!data
->mark_query
)
322 strbuf_addstr(sb
, oid_to_hex(&data
->oid
));
323 } else if (is_atom("objecttype", atom
, len
)) {
324 if (data
->mark_query
)
325 data
->info
.typep
= &data
->type
;
327 strbuf_addstr(sb
, type_name(data
->type
));
328 } else if (is_atom("objectsize", atom
, len
)) {
329 if (data
->mark_query
)
330 data
->info
.sizep
= &data
->size
;
332 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->size
);
333 } else if (is_atom("objectsize:disk", atom
, len
)) {
334 if (data
->mark_query
)
335 data
->info
.disk_sizep
= &data
->disk_size
;
337 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->disk_size
);
338 } else if (is_atom("rest", atom
, len
)) {
339 if (data
->mark_query
)
340 data
->split_on_whitespace
= 1;
342 strbuf_addstr(sb
, data
->rest
);
343 } else if (is_atom("deltabase", atom
, len
)) {
344 if (data
->mark_query
)
345 data
->info
.delta_base_oid
= &data
->delta_base_oid
;
348 oid_to_hex(&data
->delta_base_oid
));
354 static void expand_format(struct strbuf
*sb
, const char *start
,
355 struct expand_data
*data
)
357 while (strbuf_expand_step(sb
, &start
)) {
360 if (skip_prefix(start
, "%", &start
) || *start
!= '(')
361 strbuf_addch(sb
, '%');
362 else if ((end
= strchr(start
+ 1, ')')) &&
363 expand_atom(sb
, start
+ 1, end
- start
- 1, data
))
366 strbuf_expand_bad_format(start
, "cat-file");
370 static void batch_write(struct batch_options
*opt
, const void *data
, int len
)
372 if (opt
->buffer_output
) {
373 if (fwrite(data
, 1, len
, stdout
) != len
)
374 die_errno("unable to write to stdout");
376 write_or_die(1, data
, len
);
379 static void print_object_or_die(struct batch_options
*opt
, struct expand_data
*data
)
381 const struct object_id
*oid
= &data
->oid
;
383 assert(data
->info
.typep
);
385 if (data
->type
== OBJ_BLOB
) {
386 if (opt
->buffer_output
)
388 if (opt
->transform_mode
) {
393 die("missing path for '%s'", oid_to_hex(oid
));
395 if (opt
->transform_mode
== 'w') {
396 if (filter_object(data
->rest
, 0100644, oid
,
398 die("could not convert '%s' %s",
399 oid_to_hex(oid
), data
->rest
);
400 } else if (opt
->transform_mode
== 'c') {
401 enum object_type type
;
402 if (!textconv_object(the_repository
,
403 data
->rest
, 0100644, oid
,
404 1, &contents
, &size
))
405 contents
= repo_read_object_file(the_repository
,
410 die("could not convert '%s' %s",
411 oid_to_hex(oid
), data
->rest
);
413 BUG("invalid transform_mode: %c", opt
->transform_mode
);
414 batch_write(opt
, contents
, size
);
421 enum object_type type
;
425 contents
= repo_read_object_file(the_repository
, oid
, &type
,
428 die("object %s disappeared", oid_to_hex(oid
));
432 contents
= replace_idents_using_mailmap(contents
, &s
);
433 size
= cast_size_t_to_ulong(s
);
436 if (type
!= data
->type
)
437 die("object %s changed type!?", oid_to_hex(oid
));
438 if (data
->info
.sizep
&& size
!= data
->size
&& !use_mailmap
)
439 die("object %s changed size!?", oid_to_hex(oid
));
441 batch_write(opt
, contents
, size
);
446 static void print_default_format(struct strbuf
*scratch
, struct expand_data
*data
,
447 struct batch_options
*opt
)
449 strbuf_addf(scratch
, "%s %s %"PRIuMAX
"%c", oid_to_hex(&data
->oid
),
450 type_name(data
->type
),
451 (uintmax_t)data
->size
, opt
->output_delim
);
455 * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
456 * which the object may be accessed (though note that we may also rely on
457 * data->oid, too). If "pack" is NULL, then offset is ignored.
459 static void batch_object_write(const char *obj_name
,
460 struct strbuf
*scratch
,
461 struct batch_options
*opt
,
462 struct expand_data
*data
,
463 struct packed_git
*pack
,
466 if (!data
->skip_object_info
) {
470 data
->info
.typep
= &data
->type
;
473 ret
= packed_object_info(the_repository
, pack
, offset
,
476 ret
= oid_object_info_extended(the_repository
,
477 &data
->oid
, &data
->info
,
478 OBJECT_INFO_LOOKUP_REPLACE
);
480 printf("%s missing%c",
481 obj_name
? obj_name
: oid_to_hex(&data
->oid
), opt
->output_delim
);
486 if (use_mailmap
&& (data
->type
== OBJ_COMMIT
|| data
->type
== OBJ_TAG
)) {
487 size_t s
= data
->size
;
490 buf
= repo_read_object_file(the_repository
, &data
->oid
, &data
->type
,
493 die(_("unable to read %s"), oid_to_hex(&data
->oid
));
494 buf
= replace_idents_using_mailmap(buf
, &s
);
495 data
->size
= cast_size_t_to_ulong(s
);
501 strbuf_reset(scratch
);
504 print_default_format(scratch
, data
, opt
);
506 expand_format(scratch
, opt
->format
, data
);
507 strbuf_addch(scratch
, opt
->output_delim
);
510 batch_write(opt
, scratch
->buf
, scratch
->len
);
512 if (opt
->batch_mode
== BATCH_MODE_CONTENTS
) {
513 print_object_or_die(opt
, data
);
514 batch_write(opt
, &opt
->output_delim
, 1);
518 static void batch_one_object(const char *obj_name
,
519 struct strbuf
*scratch
,
520 struct batch_options
*opt
,
521 struct expand_data
*data
)
523 struct object_context ctx
;
526 (opt
->follow_symlinks
? GET_OID_FOLLOW_SYMLINKS
: 0);
527 enum get_oid_result result
;
529 result
= get_oid_with_context(the_repository
, obj_name
,
530 flags
, &data
->oid
, &ctx
);
531 if (result
!= FOUND
) {
534 printf("%s missing%c", obj_name
, opt
->output_delim
);
536 case SHORT_NAME_AMBIGUOUS
:
537 printf("%s ambiguous%c", obj_name
, opt
->output_delim
);
539 case DANGLING_SYMLINK
:
540 printf("dangling %"PRIuMAX
"%c%s%c",
541 (uintmax_t)strlen(obj_name
),
542 opt
->output_delim
, obj_name
, opt
->output_delim
);
545 printf("loop %"PRIuMAX
"%c%s%c",
546 (uintmax_t)strlen(obj_name
),
547 opt
->output_delim
, obj_name
, opt
->output_delim
);
550 printf("notdir %"PRIuMAX
"%c%s%c",
551 (uintmax_t)strlen(obj_name
),
552 opt
->output_delim
, obj_name
, opt
->output_delim
);
555 BUG("unknown get_sha1_with_context result %d\n",
564 printf("symlink %"PRIuMAX
"%c%s%c",
565 (uintmax_t)ctx
.symlink_path
.len
,
566 opt
->output_delim
, ctx
.symlink_path
.buf
, opt
->output_delim
);
571 batch_object_write(obj_name
, scratch
, opt
, data
, NULL
, 0);
574 struct object_cb_data
{
575 struct batch_options
*opt
;
576 struct expand_data
*expand
;
578 struct strbuf
*scratch
;
581 static int batch_object_cb(const struct object_id
*oid
, void *vdata
)
583 struct object_cb_data
*data
= vdata
;
584 oidcpy(&data
->expand
->oid
, oid
);
585 batch_object_write(NULL
, data
->scratch
, data
->opt
, data
->expand
,
590 static int collect_loose_object(const struct object_id
*oid
,
591 const char *path UNUSED
,
594 oid_array_append(data
, oid
);
598 static int collect_packed_object(const struct object_id
*oid
,
599 struct packed_git
*pack UNUSED
,
603 oid_array_append(data
, oid
);
607 static int batch_unordered_object(const struct object_id
*oid
,
608 struct packed_git
*pack
, off_t offset
,
611 struct object_cb_data
*data
= vdata
;
613 if (oidset_insert(data
->seen
, oid
))
616 oidcpy(&data
->expand
->oid
, oid
);
617 batch_object_write(NULL
, data
->scratch
, data
->opt
, data
->expand
,
622 static int batch_unordered_loose(const struct object_id
*oid
,
623 const char *path UNUSED
,
626 return batch_unordered_object(oid
, NULL
, 0, data
);
629 static int batch_unordered_packed(const struct object_id
*oid
,
630 struct packed_git
*pack
,
634 return batch_unordered_object(oid
, pack
,
635 nth_packed_object_offset(pack
, pos
),
639 typedef void (*parse_cmd_fn_t
)(struct batch_options
*, const char *,
640 struct strbuf
*, struct expand_data
*);
647 static void parse_cmd_contents(struct batch_options
*opt
,
649 struct strbuf
*output
,
650 struct expand_data
*data
)
652 opt
->batch_mode
= BATCH_MODE_CONTENTS
;
653 batch_one_object(line
, output
, opt
, data
);
656 static void parse_cmd_info(struct batch_options
*opt
,
658 struct strbuf
*output
,
659 struct expand_data
*data
)
661 opt
->batch_mode
= BATCH_MODE_INFO
;
662 batch_one_object(line
, output
, opt
, data
);
665 static void dispatch_calls(struct batch_options
*opt
,
666 struct strbuf
*output
,
667 struct expand_data
*data
,
668 struct queued_cmd
*cmd
,
673 if (!opt
->buffer_output
)
674 die(_("flush is only for --buffer mode"));
676 for (i
= 0; i
< nr
; i
++)
677 cmd
[i
].fn(opt
, cmd
[i
].line
, output
, data
);
682 static void free_cmds(struct queued_cmd
*cmd
, size_t *nr
)
686 for (i
= 0; i
< *nr
; i
++)
687 FREE_AND_NULL(cmd
[i
].line
);
693 static const struct parse_cmd
{
698 { "contents", parse_cmd_contents
, 1},
699 { "info", parse_cmd_info
, 1},
703 static void batch_objects_command(struct batch_options
*opt
,
704 struct strbuf
*output
,
705 struct expand_data
*data
)
707 struct strbuf input
= STRBUF_INIT
;
708 struct queued_cmd
*queued_cmd
= NULL
;
709 size_t alloc
= 0, nr
= 0;
711 while (strbuf_getdelim_strip_crlf(&input
, stdin
, opt
->input_delim
) != EOF
) {
713 const struct parse_cmd
*cmd
= NULL
;
714 const char *p
= NULL
, *cmd_end
;
715 struct queued_cmd call
= {0};
718 die(_("empty command in input"));
719 if (isspace(*input
.buf
))
720 die(_("whitespace before command: '%s'"), input
.buf
);
722 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
723 if (!skip_prefix(input
.buf
, commands
[i
].name
, &cmd_end
))
727 if (cmd
->takes_args
) {
729 die(_("%s requires arguments"),
733 } else if (*cmd_end
) {
734 die(_("%s takes no arguments"),
742 die(_("unknown command: '%s'"), input
.buf
);
744 if (!strcmp(cmd
->name
, "flush")) {
745 dispatch_calls(opt
, output
, data
, queued_cmd
, nr
);
746 free_cmds(queued_cmd
, &nr
);
747 } else if (!opt
->buffer_output
) {
748 cmd
->fn(opt
, p
, output
, data
);
750 ALLOC_GROW(queued_cmd
, nr
+ 1, alloc
);
752 call
.line
= xstrdup_or_null(p
);
753 queued_cmd
[nr
++] = call
;
757 if (opt
->buffer_output
&&
759 !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) {
760 dispatch_calls(opt
, output
, data
, queued_cmd
, nr
);
761 free_cmds(queued_cmd
, &nr
);
764 free_cmds(queued_cmd
, &nr
);
766 strbuf_release(&input
);
769 #define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
771 static int batch_objects(struct batch_options
*opt
)
773 struct strbuf input
= STRBUF_INIT
;
774 struct strbuf output
= STRBUF_INIT
;
775 struct expand_data data
;
780 * Expand once with our special mark_query flag, which will prime the
781 * object_info to be handed to oid_object_info_extended for each
784 memset(&data
, 0, sizeof(data
));
786 expand_format(&output
,
787 opt
->format
? opt
->format
: DEFAULT_FORMAT
,
790 strbuf_release(&output
);
791 if (opt
->transform_mode
)
792 data
.split_on_whitespace
= 1;
794 if (opt
->format
&& !strcmp(opt
->format
, DEFAULT_FORMAT
))
797 * If we are printing out the object, then always fill in the type,
798 * since we will want to decide whether or not to stream.
800 if (opt
->batch_mode
== BATCH_MODE_CONTENTS
)
801 data
.info
.typep
= &data
.type
;
803 if (opt
->all_objects
) {
804 struct object_cb_data cb
;
805 struct object_info empty
= OBJECT_INFO_INIT
;
807 if (!memcmp(&data
.info
, &empty
, sizeof(empty
)))
808 data
.skip_object_info
= 1;
810 if (repo_has_promisor_remote(the_repository
))
811 warning("This repository uses promisor remotes. Some objects may not be loaded.");
813 disable_replace_refs();
817 cb
.scratch
= &output
;
819 if (opt
->unordered
) {
820 struct oidset seen
= OIDSET_INIT
;
824 for_each_loose_object(batch_unordered_loose
, &cb
, 0);
825 for_each_packed_object(batch_unordered_packed
, &cb
,
826 FOR_EACH_OBJECT_PACK_ORDER
);
830 struct oid_array sa
= OID_ARRAY_INIT
;
832 for_each_loose_object(collect_loose_object
, &sa
, 0);
833 for_each_packed_object(collect_packed_object
, &sa
, 0);
835 oid_array_for_each_unique(&sa
, batch_object_cb
, &cb
);
837 oid_array_clear(&sa
);
840 strbuf_release(&output
);
845 * We are going to call get_sha1 on a potentially very large number of
846 * objects. In most large cases, these will be actual object sha1s. The
847 * cost to double-check that each one is not also a ref (just so we can
848 * warn) ends up dwarfing the actual cost of the object lookups
849 * themselves. We can work around it by just turning off the warning.
851 save_warning
= warn_on_object_refname_ambiguity
;
852 warn_on_object_refname_ambiguity
= 0;
854 if (opt
->batch_mode
== BATCH_MODE_QUEUE_AND_DISPATCH
) {
855 batch_objects_command(opt
, &output
, &data
);
859 while (strbuf_getdelim_strip_crlf(&input
, stdin
, opt
->input_delim
) != EOF
) {
860 if (data
.split_on_whitespace
) {
862 * Split at first whitespace, tying off the beginning
863 * of the string and saving the remainder (or NULL) in
866 char *p
= strpbrk(input
.buf
, " \t");
868 while (*p
&& strchr(" \t", *p
))
874 batch_one_object(input
.buf
, &output
, opt
, &data
);
878 strbuf_release(&input
);
879 strbuf_release(&output
);
880 warn_on_object_refname_ambiguity
= save_warning
;
884 static int git_cat_file_config(const char *var
, const char *value
,
885 const struct config_context
*ctx
, void *cb
)
887 if (userdiff_config(var
, value
) < 0)
890 return git_default_config(var
, value
, ctx
, cb
);
893 static int batch_option_callback(const struct option
*opt
,
897 struct batch_options
*bo
= opt
->value
;
899 BUG_ON_OPT_NEG(unset
);
902 return error(_("only one batch option may be specified"));
907 if (!strcmp(opt
->long_name
, "batch"))
908 bo
->batch_mode
= BATCH_MODE_CONTENTS
;
909 else if (!strcmp(opt
->long_name
, "batch-check"))
910 bo
->batch_mode
= BATCH_MODE_INFO
;
911 else if (!strcmp(opt
->long_name
, "batch-command"))
912 bo
->batch_mode
= BATCH_MODE_QUEUE_AND_DISPATCH
;
914 BUG("%s given to batch-option-callback", opt
->long_name
);
921 int cmd_cat_file(int argc
, const char **argv
, const char *prefix
)
926 const char *exp_type
= NULL
, *obj_name
= NULL
;
927 struct batch_options batch
= {0};
928 int unknown_type
= 0;
929 int input_nul_terminated
= 0;
930 int nul_terminated
= 0;
932 const char * const usage
[] = {
933 N_("git cat-file <type> <object>"),
934 N_("git cat-file (-e | -p) <object>"),
935 N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"),
936 N_("git cat-file (--textconv | --filters)\n"
937 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"),
938 N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
939 " [--buffer] [--follow-symlinks] [--unordered]\n"
940 " [--textconv | --filters] [-Z]"),
943 const struct option options
[] = {
945 OPT_GROUP(N_("Check object existence or emit object contents")),
946 OPT_CMDMODE('e', NULL
, &opt
,
947 N_("check if <object> exists"), 'e'),
948 OPT_CMDMODE('p', NULL
, &opt
, N_("pretty-print <object> content"), 'p'),
950 OPT_GROUP(N_("Emit [broken] object attributes")),
951 OPT_CMDMODE('t', NULL
, &opt
, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'),
952 OPT_CMDMODE('s', NULL
, &opt
, N_("show object size"), 's'),
953 OPT_BOOL(0, "allow-unknown-type", &unknown_type
,
954 N_("allow -s and -t to work with broken/corrupt objects")),
955 OPT_BOOL(0, "use-mailmap", &use_mailmap
, N_("use mail map file")),
956 OPT_ALIAS(0, "mailmap", "use-mailmap"),
958 OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")),
959 OPT_CALLBACK_F(0, "batch", &batch
, N_("format"),
960 N_("show full <object> or <rev> contents"),
961 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
962 batch_option_callback
),
963 OPT_CALLBACK_F(0, "batch-check", &batch
, N_("format"),
964 N_("like --batch, but don't emit <contents>"),
965 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
966 batch_option_callback
),
967 OPT_BOOL_F('z', NULL
, &input_nul_terminated
, N_("stdin is NUL-terminated"),
969 OPT_BOOL('Z', NULL
, &nul_terminated
, N_("stdin and stdout is NUL-terminated")),
970 OPT_CALLBACK_F(0, "batch-command", &batch
, N_("format"),
971 N_("read commands from stdin"),
972 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
973 batch_option_callback
),
974 OPT_CMDMODE(0, "batch-all-objects", &opt
,
975 N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'),
976 /* Batch-specific options */
977 OPT_GROUP(N_("Change or optimize batch output")),
978 OPT_BOOL(0, "buffer", &batch
.buffer_output
, N_("buffer --batch output")),
979 OPT_BOOL(0, "follow-symlinks", &batch
.follow_symlinks
,
980 N_("follow in-tree symlinks")),
981 OPT_BOOL(0, "unordered", &batch
.unordered
,
982 N_("do not order objects before emitting them")),
983 /* Textconv options, stand-ole*/
984 OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")),
985 OPT_CMDMODE(0, "textconv", &opt
,
986 N_("run textconv on object's content"), 'c'),
987 OPT_CMDMODE(0, "filters", &opt
,
988 N_("run filters on object's content"), 'w'),
989 OPT_STRING(0, "path", &force_path
, N_("blob|tree"),
990 N_("use a <path> for (--textconv | --filters); Not with 'batch'")),
994 git_config(git_cat_file_config
, NULL
);
996 batch
.buffer_output
= -1;
998 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
999 opt_cw
= (opt
== 'c' || opt
== 'w');
1000 opt_epts
= (opt
== 'e' || opt
== 'p' || opt
== 't' || opt
== 's');
1003 read_mailmap(&mailmap
);
1005 /* --batch-all-objects? */
1007 batch
.all_objects
= 1;
1009 /* Option compatibility */
1010 if (force_path
&& !opt_cw
)
1011 usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
1013 "--path", _("path|tree-ish"), "--filters",
1016 /* Option compatibility with batch mode */
1019 else if (batch
.follow_symlinks
)
1020 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1021 "--follow-symlinks");
1022 else if (batch
.buffer_output
>= 0)
1023 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1025 else if (batch
.all_objects
)
1026 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1027 "--batch-all-objects");
1028 else if (input_nul_terminated
)
1029 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1031 else if (nul_terminated
)
1032 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
1035 batch
.input_delim
= batch
.output_delim
= '\n';
1036 if (input_nul_terminated
)
1037 batch
.input_delim
= '\0';
1039 batch
.input_delim
= batch
.output_delim
= '\0';
1041 /* Batch defaults */
1042 if (batch
.buffer_output
< 0)
1043 batch
.buffer_output
= batch
.all_objects
;
1045 /* Return early if we're in batch mode? */
1046 if (batch
.enabled
) {
1048 batch
.transform_mode
= opt
;
1049 else if (opt
&& opt
!= 'b')
1050 usage_msg_optf(_("'-%c' is incompatible with batch mode"),
1051 usage
, options
, opt
);
1053 usage_msg_opt(_("batch modes take no arguments"), usage
,
1056 return batch_objects(&batch
);
1060 if (!argc
&& opt
== 'c')
1061 usage_msg_optf(_("<rev> required with '%s'"),
1062 usage
, options
, "--textconv");
1063 else if (!argc
&& opt
== 'w')
1064 usage_msg_optf(_("<rev> required with '%s'"),
1065 usage
, options
, "--filters");
1066 else if (!argc
&& opt_epts
)
1067 usage_msg_optf(_("<object> required with '-%c'"),
1068 usage
, options
, opt
);
1072 usage_msg_opt(_("too many arguments"), usage
, options
);
1074 usage_with_options(usage
, options
);
1075 } else if (argc
!= 2) {
1076 usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"),
1077 usage
, options
, argc
);
1083 if (unknown_type
&& opt
!= 't' && opt
!= 's')
1084 die("git cat-file --allow-unknown-type: use with -s or -t");
1085 return cat_one_file(opt
, exp_type
, obj_name
, unknown_type
);