]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/cat-file.c
Upcast size_t variables to uintmax_t when printing
[thirdparty/git.git] / builtin / cat-file.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
b2141fc1 7#include "config.h"
f81daefe 8#include "builtin.h"
3a35cb2e 9#include "diff.h"
15d8e565 10#include "parse-options.h"
e5fba602 11#include "userdiff.h"
00c8fd49 12#include "streaming.h"
122d5346 13#include "tree-walk.h"
3115ee45 14#include "sha1-array.h"
7709f468 15#include "packfile.h"
cbd53a21 16#include "object-store.h"
15d8e565 17
bfd15594
JK
18struct batch_options {
19 int enabled;
20 int follow_symlinks;
21 int print_contents;
fc4937c3 22 int buffer_output;
6a951937 23 int all_objects;
0750bb5b 24 int unordered;
32145943 25 int cmdmode; /* may be 'w' or 'c' for --filters or --textconv */
bfd15594
JK
26 const char *format;
27};
28
7bcf3414
JS
29static const char *force_path;
30
b9e62f60 31static int filter_object(const char *path, unsigned mode,
7889ed25 32 const struct object_id *oid,
b9e62f60
JS
33 char **buf, unsigned long *size)
34{
35 enum object_type type;
36
b4f5aca4 37 *buf = read_object_file(oid, &type, size);
b9e62f60
JS
38 if (!*buf)
39 return error(_("cannot read object %s '%s'"),
7889ed25 40 oid_to_hex(oid), path);
b9e62f60
JS
41 if ((type == OBJ_BLOB) && S_ISREG(mode)) {
42 struct strbuf strbuf = STRBUF_INIT;
7f944e26 43 if (convert_to_working_tree(&the_index, path, *buf, *size, &strbuf)) {
b9e62f60
JS
44 free(*buf);
45 *size = strbuf.len;
46 *buf = strbuf_detach(&strbuf, NULL);
47 }
48 }
49
50 return 0;
51}
52
39e4ae38
KN
53static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
54 int unknown_type)
e83c5163 55{
63ecb99e 56 struct object_id oid;
21666f1a 57 enum object_type type;
e5fba602 58 char *buf;
e83c5163 59 unsigned long size;
e5fba602 60 struct object_context obj_context;
27b5c1a0 61 struct object_info oi = OBJECT_INFO_INIT;
39e4ae38 62 struct strbuf sb = STRBUF_INIT;
1f0c0d36 63 unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
7bcf3414 64 const char *path = force_path;
39e4ae38
KN
65
66 if (unknown_type)
19fc5e84 67 flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
2b6854c8 68
321c89bf 69 if (get_oid_with_context(obj_name, GET_OID_RECORD_PATH,
e82caf38 70 &oid, &obj_context))
2b6854c8 71 die("Not a valid object name %s", obj_name);
7950571a 72
7bcf3414
JS
73 if (!path)
74 path = obj_context.path;
75 if (obj_context.mode == S_IFINVALID)
76 obj_context.mode = 0100644;
77
7950571a
PA
78 buf = NULL;
79 switch (opt) {
80 case 't':
6ca32f47 81 oi.type_name = &sb;
7ecd8690 82 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
39e4ae38
KN
83 die("git cat-file: could not get object info");
84 if (sb.len) {
85 printf("%s\n", sb.buf);
86 strbuf_release(&sb);
f2a06330 87 return 0;
11e7d5c5 88 }
7950571a
PA
89 break;
90
91 case 's':
39e4ae38 92 oi.sizep = &size;
7ecd8690 93 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
39e4ae38 94 die("git cat-file: could not get object info");
ca473cef 95 printf("%"PRIuMAX"\n", (uintmax_t)size);
39e4ae38 96 return 0;
7950571a
PA
97
98 case 'e':
63ecb99e 99 return !has_object_file(&oid);
7950571a 100
b9e62f60 101 case 'w':
cc0ea7c9 102 if (!path)
b9e62f60
JS
103 die("git cat-file --filters %s: <object> must be "
104 "<sha1:path>", obj_name);
105
7bcf3414 106 if (filter_object(path, obj_context.mode,
7889ed25 107 &oid, &buf, &size))
b9e62f60
JS
108 return -1;
109 break;
110
3ac21617 111 case 'c':
cc0ea7c9 112 if (!path)
3ac21617
MG
113 die("git cat-file --textconv %s: <object> must be <sha1:path>",
114 obj_name);
115
6afaf807
NTND
116 if (textconv_object(the_repository, path, obj_context.mode,
117 &oid, 1, &buf, &size))
3ac21617 118 break;
1cf01a34 119 /* else fallthrough */
3ac21617 120
a0f15fa5 121 case 'p':
0df8e965 122 type = oid_object_info(the_repository, &oid, NULL);
21666f1a 123 if (type < 0)
2b6854c8 124 die("Not a valid object name %s", obj_name);
a0f15fa5
JH
125
126 /* custom pretty-print here */
2b6854c8 127 if (type == OBJ_TREE) {
66dbfd55
GV
128 const char *ls_args[3] = { NULL };
129 ls_args[0] = "ls-tree";
130 ls_args[1] = obj_name;
2b6854c8
SP
131 return cmd_ls_tree(2, ls_args, NULL);
132 }
a0f15fa5 133
00c8fd49 134 if (type == OBJ_BLOB)
7eda0e4f 135 return stream_blob_to_fd(1, &oid, NULL, 0);
b4f5aca4 136 buf = read_object_file(&oid, &type, &size);
a0f15fa5 137 if (!buf)
2b6854c8 138 die("Cannot read object %s", obj_name);
a0f15fa5
JH
139
140 /* otherwise just spit out the data */
141 break;
e5fba602 142
7950571a 143 case 0:
00c8fd49 144 if (type_from_string(exp_type) == OBJ_BLOB) {
63ecb99e 145 struct object_id blob_oid;
0df8e965 146 if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
b4f5aca4 147 char *buffer = read_object_file(&oid, &type,
148 &size);
e3f1da98
RS
149 const char *target;
150 if (!skip_prefix(buffer, "object ", &target) ||
63ecb99e 151 get_oid_hex(target, &blob_oid))
152 die("%s not a valid tag", oid_to_hex(&oid));
00c8fd49
NTND
153 free(buffer);
154 } else
63ecb99e 155 oidcpy(&blob_oid, &oid);
00c8fd49 156
0df8e965 157 if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB)
7eda0e4f 158 return stream_blob_to_fd(1, &blob_oid, NULL, 0);
00c8fd49
NTND
159 /*
160 * we attempted to dereference a tag to a blob
161 * and failed; there may be new dereference
162 * mechanisms this code is not aware of.
163 * fall-back to the usual case.
164 */
165 }
02f0547e 166 buf = read_object_with_reference(&oid, exp_type, &size, NULL);
7950571a
PA
167 break;
168
169 default:
d7530708 170 die("git cat-file: unknown option: %s", exp_type);
bf0c6e83
LT
171 }
172
11e7d5c5 173 if (!buf)
34baebce 174 die("git cat-file %s: bad file", obj_name);
11e7d5c5 175
7230e6d0 176 write_or_die(1, buf, size);
05c2b7ba 177 free(buf);
dc944b65 178 free(obj_context.path);
bf0c6e83 179 return 0;
e83c5163 180}
9cf71b17 181
93d2a607 182struct expand_data {
cd4f77be 183 struct object_id oid;
93d2a607
JK
184 enum object_type type;
185 unsigned long size;
166df26f 186 off_t disk_size;
97be0407 187 const char *rest;
cd4f77be 188 struct object_id delta_base_oid;
93d2a607
JK
189
190 /*
191 * If mark_query is true, we do not expand anything, but rather
192 * just mark the object_info with items we wish to query.
193 */
194 int mark_query;
195
97be0407
JK
196 /*
197 * Whether to split the input on whitespace before feeding it to
198 * get_sha1; this is decided during the mark_query phase based on
199 * whether we have a %(rest) token in our format.
200 */
201 int split_on_whitespace;
202
93d2a607
JK
203 /*
204 * After a mark_query run, this object_info is set up to be
205 * passed to sha1_object_info_extended. It will point to the data
206 * elements above, so you can retrieve the response from there.
207 */
208 struct object_info info;
845de33a
JK
209
210 /*
211 * This flag will be true if the requested batch format and options
212 * don't require us to call sha1_object_info, which can then be
213 * optimized out.
214 */
215 unsigned skip_object_info : 1;
93d2a607
JK
216};
217
218static int is_atom(const char *atom, const char *s, int slen)
219{
220 int alen = strlen(atom);
221 return alen == slen && !memcmp(atom, s, alen);
222}
223
224static void expand_atom(struct strbuf *sb, const char *atom, int len,
225 void *vdata)
226{
227 struct expand_data *data = vdata;
228
229 if (is_atom("objectname", atom, len)) {
230 if (!data->mark_query)
cd4f77be 231 strbuf_addstr(sb, oid_to_hex(&data->oid));
93d2a607 232 } else if (is_atom("objecttype", atom, len)) {
5b086407
JK
233 if (data->mark_query)
234 data->info.typep = &data->type;
235 else
debca9d2 236 strbuf_addstr(sb, type_name(data->type));
93d2a607
JK
237 } else if (is_atom("objectsize", atom, len)) {
238 if (data->mark_query)
239 data->info.sizep = &data->size;
240 else
ca473cef 241 strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
a4ac1061
JK
242 } else if (is_atom("objectsize:disk", atom, len)) {
243 if (data->mark_query)
244 data->info.disk_sizep = &data->disk_size;
245 else
166df26f 246 strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
97be0407
JK
247 } else if (is_atom("rest", atom, len)) {
248 if (data->mark_query)
249 data->split_on_whitespace = 1;
250 else if (data->rest)
251 strbuf_addstr(sb, data->rest);
65ea9c3c
JK
252 } else if (is_atom("deltabase", atom, len)) {
253 if (data->mark_query)
cd4f77be 254 data->info.delta_base_sha1 = data->delta_base_oid.hash;
65ea9c3c 255 else
cd4f77be 256 strbuf_addstr(sb,
257 oid_to_hex(&data->delta_base_oid));
93d2a607
JK
258 } else
259 die("unknown format element: %.*s", len, atom);
260}
261
262static size_t expand_format(struct strbuf *sb, const char *start, void *data)
263{
264 const char *end;
265
266 if (*start != '(')
267 return 0;
268 end = strchr(start + 1, ')');
269 if (!end)
270 die("format element '%s' does not end in ')'", start);
271
272 expand_atom(sb, start + 1, end - start - 1, data);
273
274 return end - start + 1;
275}
276
fc4937c3
JK
277static void batch_write(struct batch_options *opt, const void *data, int len)
278{
279 if (opt->buffer_output) {
280 if (fwrite(data, 1, len, stdout) != len)
281 die_errno("unable to write to stdout");
282 } else
283 write_or_die(1, data, len);
284}
285
286static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
98e2092b 287{
63ecb99e 288 const struct object_id *oid = &data->oid;
370c9268 289
6554dfa9
JK
290 assert(data->info.typep);
291
370c9268 292 if (data->type == OBJ_BLOB) {
fc4937c3
JK
293 if (opt->buffer_output)
294 fflush(stdout);
32145943
JS
295 if (opt->cmdmode) {
296 char *contents;
297 unsigned long size;
298
299 if (!data->rest)
7889ed25 300 die("missing path for '%s'", oid_to_hex(oid));
32145943
JS
301
302 if (opt->cmdmode == 'w') {
7889ed25 303 if (filter_object(data->rest, 0100644, oid,
32145943
JS
304 &contents, &size))
305 die("could not convert '%s' %s",
7889ed25 306 oid_to_hex(oid), data->rest);
32145943
JS
307 } else if (opt->cmdmode == 'c') {
308 enum object_type type;
6afaf807
NTND
309 if (!textconv_object(the_repository,
310 data->rest, 0100644, oid,
32145943 311 1, &contents, &size))
b4f5aca4 312 contents = read_object_file(oid,
313 &type,
314 &size);
32145943
JS
315 if (!contents)
316 die("could not convert '%s' %s",
7889ed25 317 oid_to_hex(oid), data->rest);
32145943 318 } else
033abf97 319 BUG("invalid cmdmode: %c", opt->cmdmode);
32145943
JS
320 batch_write(opt, contents, size);
321 free(contents);
7889ed25 322 } else if (stream_blob_to_fd(1, oid, NULL, 0) < 0)
63ecb99e 323 die("unable to stream %s to stdout", oid_to_hex(oid));
98e2092b
JK
324 }
325 else {
370c9268
JK
326 enum object_type type;
327 unsigned long size;
98e2092b
JK
328 void *contents;
329
b4f5aca4 330 contents = read_object_file(oid, &type, &size);
98e2092b 331 if (!contents)
63ecb99e 332 die("object %s disappeared", oid_to_hex(oid));
370c9268 333 if (type != data->type)
63ecb99e 334 die("object %s changed type!?", oid_to_hex(oid));
6554dfa9 335 if (data->info.sizep && size != data->size)
63ecb99e 336 die("object %s changed size!?", oid_to_hex(oid));
98e2092b 337
fc4937c3 338 batch_write(opt, contents, size);
98e2092b
JK
339 free(contents);
340 }
341}
342
79ed0a5e
JK
343static void batch_object_write(const char *obj_name,
344 struct strbuf *scratch,
345 struct batch_options *opt,
44b877e9
JK
346 struct expand_data *data)
347{
845de33a 348 if (!data->skip_object_info &&
7ecd8690 349 oid_object_info_extended(the_repository, &data->oid, &data->info,
abef9020 350 OBJECT_INFO_LOOKUP_REPLACE) < 0) {
cd4f77be 351 printf("%s missing\n",
352 obj_name ? obj_name : oid_to_hex(&data->oid));
44b877e9
JK
353 fflush(stdout);
354 return;
355 }
356
79ed0a5e
JK
357 strbuf_reset(scratch);
358 strbuf_expand(scratch, opt->format, expand_format, data);
359 strbuf_addch(scratch, '\n');
360 batch_write(opt, scratch->buf, scratch->len);
44b877e9
JK
361
362 if (opt->print_contents) {
363 print_object_or_die(opt, data);
364 batch_write(opt, "\n", 1);
365 }
366}
367
79ed0a5e
JK
368static void batch_one_object(const char *obj_name,
369 struct strbuf *scratch,
370 struct batch_options *opt,
82330950 371 struct expand_data *data)
05d5667f 372{
122d5346 373 struct object_context ctx;
321c89bf 374 int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
122d5346 375 enum follow_symlinks_result result;
05d5667f 376
e82caf38 377 result = get_oid_with_context(obj_name, flags, &data->oid, &ctx);
122d5346
DT
378 if (result != FOUND) {
379 switch (result) {
380 case MISSING_OBJECT:
381 printf("%s missing\n", obj_name);
382 break;
383 case DANGLING_SYMLINK:
384 printf("dangling %"PRIuMAX"\n%s\n",
385 (uintmax_t)strlen(obj_name), obj_name);
386 break;
387 case SYMLINK_LOOP:
388 printf("loop %"PRIuMAX"\n%s\n",
389 (uintmax_t)strlen(obj_name), obj_name);
390 break;
391 case NOT_DIR:
392 printf("notdir %"PRIuMAX"\n%s\n",
393 (uintmax_t)strlen(obj_name), obj_name);
394 break;
395 default:
033abf97 396 BUG("unknown get_sha1_with_context result %d\n",
122d5346
DT
397 result);
398 break;
399 }
400 fflush(stdout);
82330950 401 return;
122d5346
DT
402 }
403
404 if (ctx.mode == 0) {
405 printf("symlink %"PRIuMAX"\n%s\n",
406 (uintmax_t)ctx.symlink_path.len,
407 ctx.symlink_path.buf);
422b2063 408 fflush(stdout);
82330950 409 return;
05d5667f
AR
410 }
411
79ed0a5e 412 batch_object_write(obj_name, scratch, opt, data);
05d5667f
AR
413}
414
6a951937
JK
415struct object_cb_data {
416 struct batch_options *opt;
417 struct expand_data *expand;
0750bb5b 418 struct oidset *seen;
79ed0a5e 419 struct strbuf *scratch;
6a951937
JK
420};
421
1b7ba794 422static int batch_object_cb(const struct object_id *oid, void *vdata)
6a951937 423{
3115ee45 424 struct object_cb_data *data = vdata;
1b7ba794 425 oidcpy(&data->expand->oid, oid);
79ed0a5e 426 batch_object_write(NULL, data->scratch, data->opt, data->expand);
16ddcd40 427 return 0;
6a951937
JK
428}
429
b1adb384
JK
430static int collect_loose_object(const struct object_id *oid,
431 const char *path,
432 void *data)
6a951937 433{
910650d2 434 oid_array_append(data, oid);
3115ee45 435 return 0;
6a951937
JK
436}
437
b1adb384
JK
438static int collect_packed_object(const struct object_id *oid,
439 struct packed_git *pack,
440 uint32_t pos,
441 void *data)
6a951937 442{
910650d2 443 oid_array_append(data, oid);
3115ee45 444 return 0;
6a951937
JK
445}
446
0750bb5b
JK
447static int batch_unordered_object(const struct object_id *oid, void *vdata)
448{
449 struct object_cb_data *data = vdata;
450
ced9fff7 451 if (oidset_insert(data->seen, oid))
0750bb5b 452 return 0;
0750bb5b
JK
453
454 return batch_object_cb(oid, data);
455}
456
457static int batch_unordered_loose(const struct object_id *oid,
458 const char *path,
459 void *data)
460{
461 return batch_unordered_object(oid, data);
462}
463
464static int batch_unordered_packed(const struct object_id *oid,
465 struct packed_git *pack,
466 uint32_t pos,
467 void *data)
468{
469 return batch_unordered_object(oid, data);
470}
471
b71bd480 472static int batch_objects(struct batch_options *opt)
05d5667f 473{
54d2f0d9
JK
474 struct strbuf input = STRBUF_INIT;
475 struct strbuf output = STRBUF_INIT;
93d2a607 476 struct expand_data data;
a42fcd15 477 int save_warning;
07e23839 478 int retval = 0;
93d2a607
JK
479
480 if (!opt->format)
481 opt->format = "%(objectname) %(objecttype) %(objectsize)";
482
483 /*
484 * Expand once with our special mark_query flag, which will prime the
485 * object_info to be handed to sha1_object_info_extended for each
486 * object.
487 */
488 memset(&data, 0, sizeof(data));
489 data.mark_query = 1;
54d2f0d9 490 strbuf_expand(&output, opt->format, expand_format, &data);
93d2a607 491 data.mark_query = 0;
54d2f0d9 492 strbuf_release(&output);
32145943
JS
493 if (opt->cmdmode)
494 data.split_on_whitespace = 1;
05d5667f 495
845de33a 496 if (opt->all_objects) {
27b5c1a0 497 struct object_info empty = OBJECT_INFO_INIT;
845de33a
JK
498 if (!memcmp(&data.info, &empty, sizeof(empty)))
499 data.skip_object_info = 1;
500 }
501
6554dfa9
JK
502 /*
503 * If we are printing out the object, then always fill in the type,
504 * since we will want to decide whether or not to stream.
505 */
506 if (opt->print_contents)
507 data.info.typep = &data.type;
508
6a951937
JK
509 if (opt->all_objects) {
510 struct object_cb_data cb;
3115ee45 511
8b4c0103
JT
512 if (repository_format_partial_clone)
513 warning("This repository has extensions.partialClone set. Some objects may not be loaded.");
3115ee45 514
6a951937
JK
515 cb.opt = opt;
516 cb.expand = &data;
79ed0a5e 517 cb.scratch = &output;
3115ee45 518
0750bb5b
JK
519 if (opt->unordered) {
520 struct oidset seen = OIDSET_INIT;
521
522 cb.seen = &seen;
523
524 for_each_loose_object(batch_unordered_loose, &cb, 0);
525 for_each_packed_object(batch_unordered_packed, &cb,
526 FOR_EACH_OBJECT_PACK_ORDER);
527
528 oidset_clear(&seen);
529 } else {
530 struct oid_array sa = OID_ARRAY_INIT;
531
532 for_each_loose_object(collect_loose_object, &sa, 0);
533 for_each_packed_object(collect_packed_object, &sa, 0);
534
535 oid_array_for_each_unique(&sa, batch_object_cb, &cb);
536
537 oid_array_clear(&sa);
538 }
539
79ed0a5e 540 strbuf_release(&output);
6a951937
JK
541 return 0;
542 }
543
25fba78d
JK
544 /*
545 * We are going to call get_sha1 on a potentially very large number of
546 * objects. In most large cases, these will be actual object sha1s. The
547 * cost to double-check that each one is not also a ref (just so we can
548 * warn) ends up dwarfing the actual cost of the object lookups
549 * themselves. We can work around it by just turning off the warning.
550 */
a42fcd15 551 save_warning = warn_on_object_refname_ambiguity;
25fba78d
JK
552 warn_on_object_refname_ambiguity = 0;
553
54d2f0d9 554 while (strbuf_getline(&input, stdin) != EOF) {
97be0407
JK
555 if (data.split_on_whitespace) {
556 /*
557 * Split at first whitespace, tying off the beginning
558 * of the string and saving the remainder (or NULL) in
559 * data.rest.
560 */
54d2f0d9 561 char *p = strpbrk(input.buf, " \t");
97be0407
JK
562 if (p) {
563 while (*p && strchr(" \t", *p))
564 *p++ = '\0';
565 }
566 data.rest = p;
567 }
568
79ed0a5e 569 batch_one_object(input.buf, &output, opt, &data);
05d5667f
AR
570 }
571
54d2f0d9 572 strbuf_release(&input);
79ed0a5e 573 strbuf_release(&output);
a42fcd15 574 warn_on_object_refname_ambiguity = save_warning;
07e23839 575 return retval;
05d5667f
AR
576}
577
15d8e565 578static const char * const cat_file_usage[] = {
7889ed25
JH
579 N_("git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | <type> | --textconv | --filters) [--path=<path>] <object>"),
580 N_("git cat-file (--batch | --batch-check) [--follow-symlinks] [--textconv | --filters]"),
15d8e565
MB
581 NULL
582};
4814dbe8 583
e5fba602
CP
584static int git_cat_file_config(const char *var, const char *value, void *cb)
585{
6680a087 586 if (userdiff_config(var, value) < 0)
e5fba602 587 return -1;
e5fba602
CP
588
589 return git_default_config(var, value, cb);
590}
591
b71bd480
JK
592static int batch_option_callback(const struct option *opt,
593 const char *arg,
594 int unset)
595{
596 struct batch_options *bo = opt->value;
597
122d5346
DT
598 if (bo->enabled) {
599 return 1;
b71bd480
JK
600 }
601
602 bo->enabled = 1;
603 bo->print_contents = !strcmp(opt->long_name, "batch");
93d2a607 604 bo->format = arg;
b71bd480
JK
605
606 return 0;
607}
608
9cf71b17
AR
609int cmd_cat_file(int argc, const char **argv, const char *prefix)
610{
b71bd480 611 int opt = 0;
4814dbe8 612 const char *exp_type = NULL, *obj_name = NULL;
b71bd480 613 struct batch_options batch = {0};
39e4ae38 614 int unknown_type = 0;
9cf71b17 615
15d8e565 616 const struct option options[] = {
d68faec7 617 OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")),
b48158ac
KN
618 OPT_CMDMODE('t', NULL, &opt, N_("show object type"), 't'),
619 OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
620 OPT_CMDMODE('e', NULL, &opt,
d68faec7 621 N_("exit with zero when there's no error"), 'e'),
b48158ac
KN
622 OPT_CMDMODE('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
623 OPT_CMDMODE(0, "textconv", &opt,
d68faec7 624 N_("for blob objects, run textconv on object's content"), 'c'),
b9e62f60
JS
625 OPT_CMDMODE(0, "filters", &opt,
626 N_("for blob objects, run filters on object's content"), 'w'),
7bcf3414
JS
627 OPT_STRING(0, "path", &force_path, N_("blob"),
628 N_("use a specific path for --textconv/--filters")),
ad42f28d 629 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
39e4ae38 630 N_("allow -s and -t to work with broken/corrupt objects")),
fc4937c3 631 OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
93d2a607 632 { OPTION_CALLBACK, 0, "batch", &batch, "format",
b71bd480 633 N_("show info and content of objects fed from the standard input"),
93d2a607
JK
634 PARSE_OPT_OPTARG, batch_option_callback },
635 { OPTION_CALLBACK, 0, "batch-check", &batch, "format",
b71bd480 636 N_("show info about objects fed from the standard input"),
93d2a607 637 PARSE_OPT_OPTARG, batch_option_callback },
122d5346
DT
638 OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
639 N_("follow in-tree symlinks (used with --batch or --batch-check)")),
6a951937
JK
640 OPT_BOOL(0, "batch-all-objects", &batch.all_objects,
641 N_("show all objects with --batch or --batch-check")),
0750bb5b
JK
642 OPT_BOOL(0, "unordered", &batch.unordered,
643 N_("do not order --batch-all-objects output")),
15d8e565
MB
644 OPT_END()
645 };
a8128ed6 646
e5fba602 647 git_config(git_cat_file_config, NULL);
4814dbe8 648
6a36e1e7 649 batch.buffer_output = -1;
37782920 650 argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);
05d5667f 651
15d8e565 652 if (opt) {
32145943
JS
653 if (batch.enabled && (opt == 'c' || opt == 'w'))
654 batch.cmdmode = opt;
655 else if (argc == 1)
15d8e565
MB
656 obj_name = argv[0];
657 else
658 usage_with_options(cat_file_usage, options);
659 }
b71bd480 660 if (!opt && !batch.enabled) {
15d8e565
MB
661 if (argc == 2) {
662 exp_type = argv[0];
663 obj_name = argv[1];
664 } else
665 usage_with_options(cat_file_usage, options);
666 }
32145943
JS
667 if (batch.enabled) {
668 if (batch.cmdmode != opt || argc)
669 usage_with_options(cat_file_usage, options);
670 if (batch.cmdmode && batch.all_objects)
671 die("--batch-all-objects cannot be combined with "
672 "--textconv nor with --filters");
9cf71b17
AR
673 }
674
6a951937 675 if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) {
122d5346
DT
676 usage_with_options(cat_file_usage, options);
677 }
678
7bcf3414
JS
679 if (force_path && opt != 'c' && opt != 'w') {
680 error("--path=<path> needs --textconv or --filters");
681 usage_with_options(cat_file_usage, options);
682 }
683
32145943
JS
684 if (force_path && batch.enabled) {
685 error("--path=<path> incompatible with --batch");
686 usage_with_options(cat_file_usage, options);
687 }
688
6a36e1e7
JK
689 if (batch.buffer_output < 0)
690 batch.buffer_output = batch.all_objects;
691
b71bd480
JK
692 if (batch.enabled)
693 return batch_objects(&batch);
05d5667f 694
39e4ae38
KN
695 if (unknown_type && opt != 't' && opt != 's')
696 die("git cat-file --allow-unknown-type: use with -s or -t");
697 return cat_one_file(opt, exp_type, obj_name, unknown_type);
9cf71b17 698}