2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
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
);
135 if (oid_object_info_extended(the_repository
, &oid
, &oi
, flags
) < 0)
136 die("git cat-file: could not get object info");
137 printf("%"PRIuMAX
"\n", (uintmax_t)size
);
142 return !has_object_file(&oid
);
146 if (filter_object(path
, obj_context
.mode
,
147 &oid
, &buf
, &size
)) {
154 if (textconv_object(the_repository
, path
, obj_context
.mode
,
155 &oid
, 1, &buf
, &size
))
157 /* else fallthrough */
160 type
= oid_object_info(the_repository
, &oid
, NULL
);
162 die("Not a valid object name %s", obj_name
);
164 /* custom pretty-print here */
165 if (type
== OBJ_TREE
) {
166 const char *ls_args
[3] = { NULL
};
167 ls_args
[0] = "ls-tree";
168 ls_args
[1] = obj_name
;
169 ret
= cmd_ls_tree(2, ls_args
, NULL
);
173 if (type
== OBJ_BLOB
) {
174 ret
= stream_blob(&oid
);
177 buf
= read_object_file(&oid
, &type
, &size
);
179 die("Cannot read object %s", obj_name
);
183 buf
= replace_idents_using_mailmap(buf
, &s
);
184 size
= cast_size_t_to_ulong(s
);
187 /* otherwise just spit out the data */
192 enum object_type exp_type_id
= type_from_string(exp_type
);
194 if (exp_type_id
== OBJ_BLOB
) {
195 struct object_id blob_oid
;
196 if (oid_object_info(the_repository
, &oid
, NULL
) == OBJ_TAG
) {
197 char *buffer
= read_object_file(&oid
, &type
,
200 if (!skip_prefix(buffer
, "object ", &target
) ||
201 get_oid_hex(target
, &blob_oid
))
202 die("%s not a valid tag", oid_to_hex(&oid
));
205 oidcpy(&blob_oid
, &oid
);
207 if (oid_object_info(the_repository
, &blob_oid
, NULL
) == OBJ_BLOB
) {
208 ret
= stream_blob(&blob_oid
);
212 * we attempted to dereference a tag to a blob
213 * and failed; there may be new dereference
214 * mechanisms this code is not aware of.
215 * fall-back to the usual case.
218 buf
= read_object_with_reference(the_repository
, &oid
,
219 exp_type_id
, &size
, NULL
);
223 buf
= replace_idents_using_mailmap(buf
, &s
);
224 size
= cast_size_t_to_ulong(s
);
229 die("git cat-file: unknown option: %s", exp_type
);
233 die("git cat-file %s: bad file", obj_name
);
235 write_or_die(1, buf
, size
);
239 free(obj_context
.path
);
244 struct object_id oid
;
245 enum object_type type
;
249 struct object_id delta_base_oid
;
252 * If mark_query is true, we do not expand anything, but rather
253 * just mark the object_info with items we wish to query.
258 * Whether to split the input on whitespace before feeding it to
259 * get_sha1; this is decided during the mark_query phase based on
260 * whether we have a %(rest) token in our format.
262 int split_on_whitespace
;
265 * After a mark_query run, this object_info is set up to be
266 * passed to oid_object_info_extended. It will point to the data
267 * elements above, so you can retrieve the response from there.
269 struct object_info info
;
272 * This flag will be true if the requested batch format and options
273 * don't require us to call oid_object_info, which can then be
276 unsigned skip_object_info
: 1;
279 static int is_atom(const char *atom
, const char *s
, int slen
)
281 int alen
= strlen(atom
);
282 return alen
== slen
&& !memcmp(atom
, s
, alen
);
285 static void expand_atom(struct strbuf
*sb
, const char *atom
, int len
,
288 struct expand_data
*data
= vdata
;
290 if (is_atom("objectname", atom
, len
)) {
291 if (!data
->mark_query
)
292 strbuf_addstr(sb
, oid_to_hex(&data
->oid
));
293 } else if (is_atom("objecttype", atom
, len
)) {
294 if (data
->mark_query
)
295 data
->info
.typep
= &data
->type
;
297 strbuf_addstr(sb
, type_name(data
->type
));
298 } else if (is_atom("objectsize", atom
, len
)) {
299 if (data
->mark_query
)
300 data
->info
.sizep
= &data
->size
;
302 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->size
);
303 } else if (is_atom("objectsize:disk", atom
, len
)) {
304 if (data
->mark_query
)
305 data
->info
.disk_sizep
= &data
->disk_size
;
307 strbuf_addf(sb
, "%"PRIuMAX
, (uintmax_t)data
->disk_size
);
308 } else if (is_atom("rest", atom
, len
)) {
309 if (data
->mark_query
)
310 data
->split_on_whitespace
= 1;
312 strbuf_addstr(sb
, data
->rest
);
313 } else if (is_atom("deltabase", atom
, len
)) {
314 if (data
->mark_query
)
315 data
->info
.delta_base_oid
= &data
->delta_base_oid
;
318 oid_to_hex(&data
->delta_base_oid
));
320 die("unknown format element: %.*s", len
, atom
);
323 static size_t expand_format(struct strbuf
*sb
, const char *start
, void *data
)
329 end
= strchr(start
+ 1, ')');
331 die("format element '%s' does not end in ')'", start
);
333 expand_atom(sb
, start
+ 1, end
- start
- 1, data
);
335 return end
- start
+ 1;
338 static void batch_write(struct batch_options
*opt
, const void *data
, int len
)
340 if (opt
->buffer_output
) {
341 if (fwrite(data
, 1, len
, stdout
) != len
)
342 die_errno("unable to write to stdout");
344 write_or_die(1, data
, len
);
347 static void print_object_or_die(struct batch_options
*opt
, struct expand_data
*data
)
349 const struct object_id
*oid
= &data
->oid
;
351 assert(data
->info
.typep
);
353 if (data
->type
== OBJ_BLOB
) {
354 if (opt
->buffer_output
)
356 if (opt
->transform_mode
) {
361 die("missing path for '%s'", oid_to_hex(oid
));
363 if (opt
->transform_mode
== 'w') {
364 if (filter_object(data
->rest
, 0100644, oid
,
366 die("could not convert '%s' %s",
367 oid_to_hex(oid
), data
->rest
);
368 } else if (opt
->transform_mode
== 'c') {
369 enum object_type type
;
370 if (!textconv_object(the_repository
,
371 data
->rest
, 0100644, oid
,
372 1, &contents
, &size
))
373 contents
= read_object_file(oid
,
377 die("could not convert '%s' %s",
378 oid_to_hex(oid
), data
->rest
);
380 BUG("invalid transform_mode: %c", opt
->transform_mode
);
381 batch_write(opt
, contents
, size
);
388 enum object_type type
;
392 contents
= read_object_file(oid
, &type
, &size
);
396 contents
= replace_idents_using_mailmap(contents
, &s
);
397 size
= cast_size_t_to_ulong(s
);
401 die("object %s disappeared", oid_to_hex(oid
));
402 if (type
!= data
->type
)
403 die("object %s changed type!?", oid_to_hex(oid
));
404 if (data
->info
.sizep
&& size
!= data
->size
&& !use_mailmap
)
405 die("object %s changed size!?", oid_to_hex(oid
));
407 batch_write(opt
, contents
, size
);
412 static void print_default_format(struct strbuf
*scratch
, struct expand_data
*data
)
414 strbuf_addf(scratch
, "%s %s %"PRIuMAX
"\n", oid_to_hex(&data
->oid
),
415 type_name(data
->type
),
416 (uintmax_t)data
->size
);
420 * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
421 * which the object may be accessed (though note that we may also rely on
422 * data->oid, too). If "pack" is NULL, then offset is ignored.
424 static void batch_object_write(const char *obj_name
,
425 struct strbuf
*scratch
,
426 struct batch_options
*opt
,
427 struct expand_data
*data
,
428 struct packed_git
*pack
,
431 if (!data
->skip_object_info
) {
435 ret
= packed_object_info(the_repository
, pack
, offset
,
438 ret
= oid_object_info_extended(the_repository
,
439 &data
->oid
, &data
->info
,
440 OBJECT_INFO_LOOKUP_REPLACE
);
442 printf("%s missing\n",
443 obj_name
? obj_name
: oid_to_hex(&data
->oid
));
449 strbuf_reset(scratch
);
452 print_default_format(scratch
, data
);
454 strbuf_expand(scratch
, opt
->format
, expand_format
, data
);
455 strbuf_addch(scratch
, '\n');
458 batch_write(opt
, scratch
->buf
, scratch
->len
);
460 if (opt
->batch_mode
== BATCH_MODE_CONTENTS
) {
461 print_object_or_die(opt
, data
);
462 batch_write(opt
, "\n", 1);
466 static void batch_one_object(const char *obj_name
,
467 struct strbuf
*scratch
,
468 struct batch_options
*opt
,
469 struct expand_data
*data
)
471 struct object_context ctx
;
472 int flags
= opt
->follow_symlinks
? GET_OID_FOLLOW_SYMLINKS
: 0;
473 enum get_oid_result result
;
475 result
= get_oid_with_context(the_repository
, obj_name
,
476 flags
, &data
->oid
, &ctx
);
477 if (result
!= FOUND
) {
480 printf("%s missing\n", obj_name
);
482 case SHORT_NAME_AMBIGUOUS
:
483 printf("%s ambiguous\n", obj_name
);
485 case DANGLING_SYMLINK
:
486 printf("dangling %"PRIuMAX
"\n%s\n",
487 (uintmax_t)strlen(obj_name
), obj_name
);
490 printf("loop %"PRIuMAX
"\n%s\n",
491 (uintmax_t)strlen(obj_name
), obj_name
);
494 printf("notdir %"PRIuMAX
"\n%s\n",
495 (uintmax_t)strlen(obj_name
), obj_name
);
498 BUG("unknown get_sha1_with_context result %d\n",
507 printf("symlink %"PRIuMAX
"\n%s\n",
508 (uintmax_t)ctx
.symlink_path
.len
,
509 ctx
.symlink_path
.buf
);
514 batch_object_write(obj_name
, scratch
, opt
, data
, NULL
, 0);
517 struct object_cb_data
{
518 struct batch_options
*opt
;
519 struct expand_data
*expand
;
521 struct strbuf
*scratch
;
524 static int batch_object_cb(const struct object_id
*oid
, void *vdata
)
526 struct object_cb_data
*data
= vdata
;
527 oidcpy(&data
->expand
->oid
, oid
);
528 batch_object_write(NULL
, data
->scratch
, data
->opt
, data
->expand
,
533 static int collect_loose_object(const struct object_id
*oid
,
537 oid_array_append(data
, oid
);
541 static int collect_packed_object(const struct object_id
*oid
,
542 struct packed_git
*pack
,
546 oid_array_append(data
, oid
);
550 static int batch_unordered_object(const struct object_id
*oid
,
551 struct packed_git
*pack
, off_t offset
,
554 struct object_cb_data
*data
= vdata
;
556 if (oidset_insert(data
->seen
, oid
))
559 oidcpy(&data
->expand
->oid
, oid
);
560 batch_object_write(NULL
, data
->scratch
, data
->opt
, data
->expand
,
565 static int batch_unordered_loose(const struct object_id
*oid
,
569 return batch_unordered_object(oid
, NULL
, 0, data
);
572 static int batch_unordered_packed(const struct object_id
*oid
,
573 struct packed_git
*pack
,
577 return batch_unordered_object(oid
, pack
,
578 nth_packed_object_offset(pack
, pos
),
582 typedef void (*parse_cmd_fn_t
)(struct batch_options
*, const char *,
583 struct strbuf
*, struct expand_data
*);
590 static void parse_cmd_contents(struct batch_options
*opt
,
592 struct strbuf
*output
,
593 struct expand_data
*data
)
595 opt
->batch_mode
= BATCH_MODE_CONTENTS
;
596 batch_one_object(line
, output
, opt
, data
);
599 static void parse_cmd_info(struct batch_options
*opt
,
601 struct strbuf
*output
,
602 struct expand_data
*data
)
604 opt
->batch_mode
= BATCH_MODE_INFO
;
605 batch_one_object(line
, output
, opt
, data
);
608 static void dispatch_calls(struct batch_options
*opt
,
609 struct strbuf
*output
,
610 struct expand_data
*data
,
611 struct queued_cmd
*cmd
,
616 if (!opt
->buffer_output
)
617 die(_("flush is only for --buffer mode"));
619 for (i
= 0; i
< nr
; i
++)
620 cmd
[i
].fn(opt
, cmd
[i
].line
, output
, data
);
625 static void free_cmds(struct queued_cmd
*cmd
, size_t *nr
)
629 for (i
= 0; i
< *nr
; i
++)
630 FREE_AND_NULL(cmd
[i
].line
);
636 static const struct parse_cmd
{
641 { "contents", parse_cmd_contents
, 1},
642 { "info", parse_cmd_info
, 1},
646 static void batch_objects_command(struct batch_options
*opt
,
647 struct strbuf
*output
,
648 struct expand_data
*data
)
650 struct strbuf input
= STRBUF_INIT
;
651 struct queued_cmd
*queued_cmd
= NULL
;
652 size_t alloc
= 0, nr
= 0;
656 const struct parse_cmd
*cmd
= NULL
;
657 const char *p
= NULL
, *cmd_end
;
658 struct queued_cmd call
= {0};
660 if (opt
->nul_terminated
)
661 ret
= strbuf_getline_nul(&input
, stdin
);
663 ret
= strbuf_getline(&input
, stdin
);
669 die(_("empty command in input"));
670 if (isspace(*input
.buf
))
671 die(_("whitespace before command: '%s'"), input
.buf
);
673 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
674 if (!skip_prefix(input
.buf
, commands
[i
].name
, &cmd_end
))
678 if (cmd
->takes_args
) {
680 die(_("%s requires arguments"),
684 } else if (*cmd_end
) {
685 die(_("%s takes no arguments"),
693 die(_("unknown command: '%s'"), input
.buf
);
695 if (!strcmp(cmd
->name
, "flush")) {
696 dispatch_calls(opt
, output
, data
, queued_cmd
, nr
);
697 free_cmds(queued_cmd
, &nr
);
698 } else if (!opt
->buffer_output
) {
699 cmd
->fn(opt
, p
, output
, data
);
701 ALLOC_GROW(queued_cmd
, nr
+ 1, alloc
);
703 call
.line
= xstrdup_or_null(p
);
704 queued_cmd
[nr
++] = call
;
708 if (opt
->buffer_output
&&
710 !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) {
711 dispatch_calls(opt
, output
, data
, queued_cmd
, nr
);
712 free_cmds(queued_cmd
, &nr
);
715 free_cmds(queued_cmd
, &nr
);
717 strbuf_release(&input
);
720 #define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
722 static int batch_objects(struct batch_options
*opt
)
724 struct strbuf input
= STRBUF_INIT
;
725 struct strbuf output
= STRBUF_INIT
;
726 struct expand_data data
;
731 * Expand once with our special mark_query flag, which will prime the
732 * object_info to be handed to oid_object_info_extended for each
735 memset(&data
, 0, sizeof(data
));
737 strbuf_expand(&output
,
738 opt
->format
? opt
->format
: DEFAULT_FORMAT
,
742 strbuf_release(&output
);
743 if (opt
->transform_mode
)
744 data
.split_on_whitespace
= 1;
746 if (opt
->format
&& !strcmp(opt
->format
, DEFAULT_FORMAT
))
749 * If we are printing out the object, then always fill in the type,
750 * since we will want to decide whether or not to stream.
752 if (opt
->batch_mode
== BATCH_MODE_CONTENTS
)
753 data
.info
.typep
= &data
.type
;
755 if (opt
->all_objects
) {
756 struct object_cb_data cb
;
757 struct object_info empty
= OBJECT_INFO_INIT
;
759 if (!memcmp(&data
.info
, &empty
, sizeof(empty
)))
760 data
.skip_object_info
= 1;
762 if (has_promisor_remote())
763 warning("This repository uses promisor remotes. Some objects may not be loaded.");
765 read_replace_refs
= 0;
769 cb
.scratch
= &output
;
771 if (opt
->unordered
) {
772 struct oidset seen
= OIDSET_INIT
;
776 for_each_loose_object(batch_unordered_loose
, &cb
, 0);
777 for_each_packed_object(batch_unordered_packed
, &cb
,
778 FOR_EACH_OBJECT_PACK_ORDER
);
782 struct oid_array sa
= OID_ARRAY_INIT
;
784 for_each_loose_object(collect_loose_object
, &sa
, 0);
785 for_each_packed_object(collect_packed_object
, &sa
, 0);
787 oid_array_for_each_unique(&sa
, batch_object_cb
, &cb
);
789 oid_array_clear(&sa
);
792 strbuf_release(&output
);
797 * We are going to call get_sha1 on a potentially very large number of
798 * objects. In most large cases, these will be actual object sha1s. The
799 * cost to double-check that each one is not also a ref (just so we can
800 * warn) ends up dwarfing the actual cost of the object lookups
801 * themselves. We can work around it by just turning off the warning.
803 save_warning
= warn_on_object_refname_ambiguity
;
804 warn_on_object_refname_ambiguity
= 0;
806 if (opt
->batch_mode
== BATCH_MODE_QUEUE_AND_DISPATCH
) {
807 batch_objects_command(opt
, &output
, &data
);
813 if (opt
->nul_terminated
)
814 ret
= strbuf_getline_nul(&input
, stdin
);
816 ret
= strbuf_getline(&input
, stdin
);
821 if (data
.split_on_whitespace
) {
823 * Split at first whitespace, tying off the beginning
824 * of the string and saving the remainder (or NULL) in
827 char *p
= strpbrk(input
.buf
, " \t");
829 while (*p
&& strchr(" \t", *p
))
835 batch_one_object(input
.buf
, &output
, opt
, &data
);
839 strbuf_release(&input
);
840 strbuf_release(&output
);
841 warn_on_object_refname_ambiguity
= save_warning
;
845 static int git_cat_file_config(const char *var
, const char *value
, void *cb
)
847 if (userdiff_config(var
, value
) < 0)
850 return git_default_config(var
, value
, cb
);
853 static int batch_option_callback(const struct option
*opt
,
857 struct batch_options
*bo
= opt
->value
;
859 BUG_ON_OPT_NEG(unset
);
862 return error(_("only one batch option may be specified"));
867 if (!strcmp(opt
->long_name
, "batch"))
868 bo
->batch_mode
= BATCH_MODE_CONTENTS
;
869 else if (!strcmp(opt
->long_name
, "batch-check"))
870 bo
->batch_mode
= BATCH_MODE_INFO
;
871 else if (!strcmp(opt
->long_name
, "batch-command"))
872 bo
->batch_mode
= BATCH_MODE_QUEUE_AND_DISPATCH
;
874 BUG("%s given to batch-option-callback", opt
->long_name
);
881 int cmd_cat_file(int argc
, const char **argv
, const char *prefix
)
886 const char *exp_type
= NULL
, *obj_name
= NULL
;
887 struct batch_options batch
= {0};
888 int unknown_type
= 0;
890 const char * const usage
[] = {
891 N_("git cat-file <type> <object>"),
892 N_("git cat-file (-e | -p) <object>"),
893 N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"),
894 N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
895 " [--buffer] [--follow-symlinks] [--unordered]\n"
896 " [--textconv | --filters]"),
897 N_("git cat-file (--textconv | --filters)\n"
898 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"),
901 const struct option options
[] = {
903 OPT_GROUP(N_("Check object existence or emit object contents")),
904 OPT_CMDMODE('e', NULL
, &opt
,
905 N_("check if <object> exists"), 'e'),
906 OPT_CMDMODE('p', NULL
, &opt
, N_("pretty-print <object> content"), 'p'),
908 OPT_GROUP(N_("Emit [broken] object attributes")),
909 OPT_CMDMODE('t', NULL
, &opt
, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'),
910 OPT_CMDMODE('s', NULL
, &opt
, N_("show object size"), 's'),
911 OPT_BOOL(0, "allow-unknown-type", &unknown_type
,
912 N_("allow -s and -t to work with broken/corrupt objects")),
913 OPT_BOOL(0, "use-mailmap", &use_mailmap
, N_("use mail map file")),
914 OPT_ALIAS(0, "mailmap", "use-mailmap"),
916 OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")),
917 OPT_CALLBACK_F(0, "batch", &batch
, N_("format"),
918 N_("show full <object> or <rev> contents"),
919 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
920 batch_option_callback
),
921 OPT_CALLBACK_F(0, "batch-check", &batch
, N_("format"),
922 N_("like --batch, but don't emit <contents>"),
923 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
924 batch_option_callback
),
925 OPT_BOOL('z', NULL
, &batch
.nul_terminated
, N_("stdin is NUL-terminated")),
926 OPT_CALLBACK_F(0, "batch-command", &batch
, N_("format"),
927 N_("read commands from stdin"),
928 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
929 batch_option_callback
),
930 OPT_CMDMODE(0, "batch-all-objects", &opt
,
931 N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'),
932 /* Batch-specific options */
933 OPT_GROUP(N_("Change or optimize batch output")),
934 OPT_BOOL(0, "buffer", &batch
.buffer_output
, N_("buffer --batch output")),
935 OPT_BOOL(0, "follow-symlinks", &batch
.follow_symlinks
,
936 N_("follow in-tree symlinks")),
937 OPT_BOOL(0, "unordered", &batch
.unordered
,
938 N_("do not order objects before emitting them")),
939 /* Textconv options, stand-ole*/
940 OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")),
941 OPT_CMDMODE(0, "textconv", &opt
,
942 N_("run textconv on object's content"), 'c'),
943 OPT_CMDMODE(0, "filters", &opt
,
944 N_("run filters on object's content"), 'w'),
945 OPT_STRING(0, "path", &force_path
, N_("blob|tree"),
946 N_("use a <path> for (--textconv | --filters); Not with 'batch'")),
950 git_config(git_cat_file_config
, NULL
);
952 batch
.buffer_output
= -1;
954 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
955 opt_cw
= (opt
== 'c' || opt
== 'w');
956 opt_epts
= (opt
== 'e' || opt
== 'p' || opt
== 't' || opt
== 's');
959 read_mailmap(&mailmap
);
961 /* --batch-all-objects? */
963 batch
.all_objects
= 1;
965 /* Option compatibility */
966 if (force_path
&& !opt_cw
)
967 usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
969 "--path", _("path|tree-ish"), "--filters",
972 /* Option compatibility with batch mode */
975 else if (batch
.follow_symlinks
)
976 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
977 "--follow-symlinks");
978 else if (batch
.buffer_output
>= 0)
979 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
981 else if (batch
.all_objects
)
982 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
983 "--batch-all-objects");
984 else if (batch
.nul_terminated
)
985 usage_msg_optf(_("'%s' requires a batch mode"), usage
, options
,
989 if (batch
.buffer_output
< 0)
990 batch
.buffer_output
= batch
.all_objects
;
992 /* Return early if we're in batch mode? */
995 batch
.transform_mode
= opt
;
996 else if (opt
&& opt
!= 'b')
997 usage_msg_optf(_("'-%c' is incompatible with batch mode"),
998 usage
, options
, opt
);
1000 usage_msg_opt(_("batch modes take no arguments"), usage
,
1003 return batch_objects(&batch
);
1007 if (!argc
&& opt
== 'c')
1008 usage_msg_optf(_("<rev> required with '%s'"),
1009 usage
, options
, "--textconv");
1010 else if (!argc
&& opt
== 'w')
1011 usage_msg_optf(_("<rev> required with '%s'"),
1012 usage
, options
, "--filters");
1013 else if (!argc
&& opt_epts
)
1014 usage_msg_optf(_("<object> required with '-%c'"),
1015 usage
, options
, opt
);
1019 usage_msg_opt(_("too many arguments"), usage
, options
);
1021 usage_with_options(usage
, options
);
1022 } else if (argc
!= 2) {
1023 usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"),
1024 usage
, options
, argc
);
1030 if (unknown_type
&& opt
!= 't' && opt
!= 's')
1031 die("git cat-file --allow-unknown-type: use with -s or -t");
1032 return cat_one_file(opt
, exp_type
, obj_name
, unknown_type
);