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