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