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