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