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