]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/cat-file.c
Sync with maint-2.37
[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"
ec031da9 19#include "mailmap.h"
15d8e565 20
ac4e58ca
JC
21enum batch_mode {
22 BATCH_MODE_CONTENTS,
23 BATCH_MODE_INFO,
440c705e 24 BATCH_MODE_QUEUE_AND_DISPATCH,
ac4e58ca
JC
25};
26
bfd15594
JK
27struct batch_options {
28 int enabled;
29 int follow_symlinks;
ac4e58ca 30 enum batch_mode batch_mode;
fc4937c3 31 int buffer_output;
6a951937 32 int all_objects;
0750bb5b 33 int unordered;
a2c75526 34 int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */
db9d67f2 35 int nul_terminated;
bfd15594
JK
36 const char *format;
37};
38
7bcf3414
JS
39static const char *force_path;
40
ec031da9
SA
41static struct string_list mailmap = STRING_LIST_INIT_NODUP;
42static int use_mailmap;
43
44static char *replace_idents_using_mailmap(char *, size_t *);
45
46static char *replace_idents_using_mailmap(char *object_buf, size_t *size)
47{
48 struct strbuf sb = STRBUF_INIT;
49 const char *headers[] = { "author ", "committer ", "tagger ", NULL };
50
51 strbuf_attach(&sb, object_buf, *size, *size + 1);
52 apply_mailmap_to_header(&sb, headers, &mailmap);
53 *size = sb.len;
54 return strbuf_detach(&sb, NULL);
55}
56
b9e62f60 57static int filter_object(const char *path, unsigned mode,
7889ed25 58 const struct object_id *oid,
b9e62f60
JS
59 char **buf, unsigned long *size)
60{
61 enum object_type type;
62
b4f5aca4 63 *buf = read_object_file(oid, &type, size);
b9e62f60
JS
64 if (!*buf)
65 return error(_("cannot read object %s '%s'"),
7889ed25 66 oid_to_hex(oid), path);
b9e62f60
JS
67 if ((type == OBJ_BLOB) && S_ISREG(mode)) {
68 struct strbuf strbuf = STRBUF_INIT;
c397aac0 69 struct checkout_metadata meta;
70
71 init_checkout_metadata(&meta, NULL, NULL, oid);
72 if (convert_to_working_tree(&the_index, path, *buf, *size, &strbuf, &meta)) {
b9e62f60
JS
73 free(*buf);
74 *size = strbuf.len;
75 *buf = strbuf_detach(&strbuf, NULL);
76 }
77 }
78
79 return 0;
80}
81
98f425b4
JK
82static int stream_blob(const struct object_id *oid)
83{
84 if (stream_blob_to_fd(1, oid, NULL, 0))
85 die("unable to stream %s to stdout", oid_to_hex(oid));
86 return 0;
87}
88
39e4ae38
KN
89static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
90 int unknown_type)
e83c5163 91{
27472b51 92 int ret;
63ecb99e 93 struct object_id oid;
21666f1a 94 enum object_type type;
e5fba602 95 char *buf;
e83c5163 96 unsigned long size;
e5fba602 97 struct object_context obj_context;
27b5c1a0 98 struct object_info oi = OBJECT_INFO_INIT;
39e4ae38 99 struct strbuf sb = STRBUF_INIT;
1f0c0d36 100 unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
245b9488 101 unsigned get_oid_flags = GET_OID_RECORD_PATH | GET_OID_ONLY_TO_DIE;
7bcf3414 102 const char *path = force_path;
245b9488
ÆAB
103 const int opt_cw = (opt == 'c' || opt == 'w');
104 if (!path && opt_cw)
105 get_oid_flags |= GET_OID_REQUIRE_PATH;
39e4ae38
KN
106
107 if (unknown_type)
19fc5e84 108 flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
2b6854c8 109
245b9488
ÆAB
110 if (get_oid_with_context(the_repository, obj_name, get_oid_flags, &oid,
111 &obj_context))
2b6854c8 112 die("Not a valid object name %s", obj_name);
7950571a 113
7bcf3414
JS
114 if (!path)
115 path = obj_context.path;
116 if (obj_context.mode == S_IFINVALID)
117 obj_context.mode = 0100644;
118
7950571a
PA
119 buf = NULL;
120 switch (opt) {
121 case 't':
6ca32f47 122 oi.type_name = &sb;
7ecd8690 123 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
39e4ae38
KN
124 die("git cat-file: could not get object info");
125 if (sb.len) {
126 printf("%s\n", sb.buf);
127 strbuf_release(&sb);
27472b51
ÆAB
128 ret = 0;
129 goto cleanup;
11e7d5c5 130 }
7950571a
PA
131 break;
132
133 case 's':
39e4ae38 134 oi.sizep = &size;
7ecd8690 135 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
39e4ae38 136 die("git cat-file: could not get object info");
ca473cef 137 printf("%"PRIuMAX"\n", (uintmax_t)size);
27472b51
ÆAB
138 ret = 0;
139 goto cleanup;
7950571a
PA
140
141 case 'e':
63ecb99e 142 return !has_object_file(&oid);
7950571a 143
b9e62f60 144 case 'w':
b9e62f60 145
7bcf3414 146 if (filter_object(path, obj_context.mode,
27472b51
ÆAB
147 &oid, &buf, &size)) {
148 ret = -1;
149 goto cleanup;
150 }
b9e62f60
JS
151 break;
152
3ac21617 153 case 'c':
6afaf807
NTND
154 if (textconv_object(the_repository, path, obj_context.mode,
155 &oid, 1, &buf, &size))
3ac21617 156 break;
1cf01a34 157 /* else fallthrough */
3ac21617 158
a0f15fa5 159 case 'p':
0df8e965 160 type = oid_object_info(the_repository, &oid, NULL);
21666f1a 161 if (type < 0)
2b6854c8 162 die("Not a valid object name %s", obj_name);
a0f15fa5
JH
163
164 /* custom pretty-print here */
2b6854c8 165 if (type == OBJ_TREE) {
66dbfd55
GV
166 const char *ls_args[3] = { NULL };
167 ls_args[0] = "ls-tree";
168 ls_args[1] = obj_name;
27472b51
ÆAB
169 ret = cmd_ls_tree(2, ls_args, NULL);
170 goto cleanup;
2b6854c8 171 }
a0f15fa5 172
27472b51
ÆAB
173 if (type == OBJ_BLOB) {
174 ret = stream_blob(&oid);
175 goto cleanup;
176 }
b4f5aca4 177 buf = read_object_file(&oid, &type, &size);
a0f15fa5 178 if (!buf)
2b6854c8 179 die("Cannot read object %s", obj_name);
a0f15fa5 180
ec031da9
SA
181 if (use_mailmap) {
182 size_t s = size;
183 buf = replace_idents_using_mailmap(buf, &s);
184 size = cast_size_t_to_ulong(s);
185 }
186
a0f15fa5
JH
187 /* otherwise just spit out the data */
188 break;
e5fba602 189
7950571a 190 case 0:
6aea6bae
ÆAB
191 {
192 enum object_type exp_type_id = type_from_string(exp_type);
193
194 if (exp_type_id == OBJ_BLOB) {
63ecb99e 195 struct object_id blob_oid;
0df8e965 196 if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
b4f5aca4 197 char *buffer = read_object_file(&oid, &type,
198 &size);
e3f1da98
RS
199 const char *target;
200 if (!skip_prefix(buffer, "object ", &target) ||
63ecb99e 201 get_oid_hex(target, &blob_oid))
202 die("%s not a valid tag", oid_to_hex(&oid));
00c8fd49
NTND
203 free(buffer);
204 } else
63ecb99e 205 oidcpy(&blob_oid, &oid);
00c8fd49 206
27472b51
ÆAB
207 if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB) {
208 ret = stream_blob(&blob_oid);
209 goto cleanup;
210 }
00c8fd49
NTND
211 /*
212 * we attempted to dereference a tag to a blob
213 * and failed; there may be new dereference
214 * mechanisms this code is not aware of.
215 * fall-back to the usual case.
216 */
217 }
6aea6bae
ÆAB
218 buf = read_object_with_reference(the_repository, &oid,
219 exp_type_id, &size, NULL);
ec031da9
SA
220
221 if (use_mailmap) {
222 size_t s = size;
223 buf = replace_idents_using_mailmap(buf, &s);
224 size = cast_size_t_to_ulong(s);
225 }
7950571a 226 break;
6aea6bae 227 }
7950571a 228 default:
d7530708 229 die("git cat-file: unknown option: %s", exp_type);
bf0c6e83
LT
230 }
231
11e7d5c5 232 if (!buf)
34baebce 233 die("git cat-file %s: bad file", obj_name);
11e7d5c5 234
7230e6d0 235 write_or_die(1, buf, size);
27472b51
ÆAB
236 ret = 0;
237cleanup:
05c2b7ba 238 free(buf);
dc944b65 239 free(obj_context.path);
27472b51 240 return ret;
e83c5163 241}
9cf71b17 242
93d2a607 243struct expand_data {
cd4f77be 244 struct object_id oid;
93d2a607
JK
245 enum object_type type;
246 unsigned long size;
166df26f 247 off_t disk_size;
97be0407 248 const char *rest;
cd4f77be 249 struct object_id delta_base_oid;
93d2a607
JK
250
251 /*
252 * If mark_query is true, we do not expand anything, but rather
253 * just mark the object_info with items we wish to query.
254 */
255 int mark_query;
256
97be0407
JK
257 /*
258 * Whether to split the input on whitespace before feeding it to
259 * get_sha1; this is decided during the mark_query phase based on
260 * whether we have a %(rest) token in our format.
261 */
262 int split_on_whitespace;
263
93d2a607
JK
264 /*
265 * After a mark_query run, this object_info is set up to be
c93206b4 266 * passed to oid_object_info_extended. It will point to the data
93d2a607
JK
267 * elements above, so you can retrieve the response from there.
268 */
269 struct object_info info;
845de33a
JK
270
271 /*
272 * This flag will be true if the requested batch format and options
c93206b4 273 * don't require us to call oid_object_info, which can then be
845de33a
JK
274 * optimized out.
275 */
276 unsigned skip_object_info : 1;
93d2a607
JK
277};
278
279static int is_atom(const char *atom, const char *s, int slen)
280{
281 int alen = strlen(atom);
282 return alen == slen && !memcmp(atom, s, alen);
283}
284
285static void expand_atom(struct strbuf *sb, const char *atom, int len,
286 void *vdata)
287{
288 struct expand_data *data = vdata;
289
290 if (is_atom("objectname", atom, len)) {
291 if (!data->mark_query)
cd4f77be 292 strbuf_addstr(sb, oid_to_hex(&data->oid));
93d2a607 293 } else if (is_atom("objecttype", atom, len)) {
5b086407
JK
294 if (data->mark_query)
295 data->info.typep = &data->type;
296 else
debca9d2 297 strbuf_addstr(sb, type_name(data->type));
93d2a607
JK
298 } else if (is_atom("objectsize", atom, len)) {
299 if (data->mark_query)
300 data->info.sizep = &data->size;
301 else
ca473cef 302 strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
a4ac1061
JK
303 } else if (is_atom("objectsize:disk", atom, len)) {
304 if (data->mark_query)
305 data->info.disk_sizep = &data->disk_size;
306 else
166df26f 307 strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
97be0407
JK
308 } else if (is_atom("rest", atom, len)) {
309 if (data->mark_query)
310 data->split_on_whitespace = 1;
311 else if (data->rest)
312 strbuf_addstr(sb, data->rest);
65ea9c3c
JK
313 } else if (is_atom("deltabase", atom, len)) {
314 if (data->mark_query)
b99b6bcc 315 data->info.delta_base_oid = &data->delta_base_oid;
65ea9c3c 316 else
cd4f77be 317 strbuf_addstr(sb,
318 oid_to_hex(&data->delta_base_oid));
93d2a607
JK
319 } else
320 die("unknown format element: %.*s", len, atom);
321}
322
323static size_t expand_format(struct strbuf *sb, const char *start, void *data)
324{
325 const char *end;
326
327 if (*start != '(')
328 return 0;
329 end = strchr(start + 1, ')');
330 if (!end)
331 die("format element '%s' does not end in ')'", start);
332
333 expand_atom(sb, start + 1, end - start - 1, data);
334
335 return end - start + 1;
336}
337
fc4937c3
JK
338static void batch_write(struct batch_options *opt, const void *data, int len)
339{
340 if (opt->buffer_output) {
341 if (fwrite(data, 1, len, stdout) != len)
342 die_errno("unable to write to stdout");
343 } else
344 write_or_die(1, data, len);
345}
346
347static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
98e2092b 348{
63ecb99e 349 const struct object_id *oid = &data->oid;
370c9268 350
6554dfa9
JK
351 assert(data->info.typep);
352
370c9268 353 if (data->type == OBJ_BLOB) {
fc4937c3
JK
354 if (opt->buffer_output)
355 fflush(stdout);
a2c75526 356 if (opt->transform_mode) {
32145943
JS
357 char *contents;
358 unsigned long size;
359
360 if (!data->rest)
7889ed25 361 die("missing path for '%s'", oid_to_hex(oid));
32145943 362
a2c75526 363 if (opt->transform_mode == 'w') {
7889ed25 364 if (filter_object(data->rest, 0100644, oid,
32145943
JS
365 &contents, &size))
366 die("could not convert '%s' %s",
7889ed25 367 oid_to_hex(oid), data->rest);
a2c75526 368 } else if (opt->transform_mode == 'c') {
32145943 369 enum object_type type;
6afaf807
NTND
370 if (!textconv_object(the_repository,
371 data->rest, 0100644, oid,
32145943 372 1, &contents, &size))
b4f5aca4 373 contents = read_object_file(oid,
374 &type,
375 &size);
32145943
JS
376 if (!contents)
377 die("could not convert '%s' %s",
7889ed25 378 oid_to_hex(oid), data->rest);
32145943 379 } else
a2c75526 380 BUG("invalid transform_mode: %c", opt->transform_mode);
32145943
JS
381 batch_write(opt, contents, size);
382 free(contents);
98f425b4
JK
383 } else {
384 stream_blob(oid);
385 }
98e2092b
JK
386 }
387 else {
370c9268
JK
388 enum object_type type;
389 unsigned long size;
98e2092b
JK
390 void *contents;
391
b4f5aca4 392 contents = read_object_file(oid, &type, &size);
ec031da9
SA
393
394 if (use_mailmap) {
395 size_t s = size;
396 contents = replace_idents_using_mailmap(contents, &s);
397 size = cast_size_t_to_ulong(s);
398 }
399
98e2092b 400 if (!contents)
63ecb99e 401 die("object %s disappeared", oid_to_hex(oid));
370c9268 402 if (type != data->type)
63ecb99e 403 die("object %s changed type!?", oid_to_hex(oid));
ec031da9 404 if (data->info.sizep && size != data->size && !use_mailmap)
63ecb99e 405 die("object %s changed size!?", oid_to_hex(oid));
98e2092b 406
fc4937c3 407 batch_write(opt, contents, size);
98e2092b
JK
408 free(contents);
409 }
410}
411
eb54a339
JC
412static void print_default_format(struct strbuf *scratch, struct expand_data *data)
413{
414 strbuf_addf(scratch, "%s %s %"PRIuMAX"\n", oid_to_hex(&data->oid),
415 type_name(data->type),
416 (uintmax_t)data->size);
417}
418
bf972896
JK
419/*
420 * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
421 * which the object may be accessed (though note that we may also rely on
422 * data->oid, too). If "pack" is NULL, then offset is ignored.
423 */
79ed0a5e
JK
424static void batch_object_write(const char *obj_name,
425 struct strbuf *scratch,
426 struct batch_options *opt,
bf972896
JK
427 struct expand_data *data,
428 struct packed_git *pack,
429 off_t offset)
44b877e9 430{
bf972896
JK
431 if (!data->skip_object_info) {
432 int ret;
433
434 if (pack)
435 ret = packed_object_info(the_repository, pack, offset,
436 &data->info);
437 else
438 ret = oid_object_info_extended(the_repository,
439 &data->oid, &data->info,
440 OBJECT_INFO_LOOKUP_REPLACE);
441 if (ret < 0) {
442 printf("%s missing\n",
443 obj_name ? obj_name : oid_to_hex(&data->oid));
444 fflush(stdout);
445 return;
446 }
44b877e9
JK
447 }
448
79ed0a5e 449 strbuf_reset(scratch);
eb54a339
JC
450
451 if (!opt->format) {
452 print_default_format(scratch, data);
453 } else {
454 strbuf_expand(scratch, opt->format, expand_format, data);
455 strbuf_addch(scratch, '\n');
456 }
457
79ed0a5e 458 batch_write(opt, scratch->buf, scratch->len);
44b877e9 459
ac4e58ca 460 if (opt->batch_mode == BATCH_MODE_CONTENTS) {
44b877e9
JK
461 print_object_or_die(opt, data);
462 batch_write(opt, "\n", 1);
463 }
464}
465
79ed0a5e
JK
466static void batch_one_object(const char *obj_name,
467 struct strbuf *scratch,
468 struct batch_options *opt,
82330950 469 struct expand_data *data)
05d5667f 470{
122d5346 471 struct object_context ctx;
321c89bf 472 int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
d1dd94b3 473 enum get_oid_result result;
05d5667f 474
3a7a698e
NTND
475 result = get_oid_with_context(the_repository, obj_name,
476 flags, &data->oid, &ctx);
122d5346
DT
477 if (result != FOUND) {
478 switch (result) {
479 case MISSING_OBJECT:
480 printf("%s missing\n", obj_name);
481 break;
d1dd94b3
DT
482 case SHORT_NAME_AMBIGUOUS:
483 printf("%s ambiguous\n", obj_name);
484 break;
122d5346
DT
485 case DANGLING_SYMLINK:
486 printf("dangling %"PRIuMAX"\n%s\n",
487 (uintmax_t)strlen(obj_name), obj_name);
488 break;
489 case SYMLINK_LOOP:
490 printf("loop %"PRIuMAX"\n%s\n",
491 (uintmax_t)strlen(obj_name), obj_name);
492 break;
493 case NOT_DIR:
494 printf("notdir %"PRIuMAX"\n%s\n",
495 (uintmax_t)strlen(obj_name), obj_name);
496 break;
497 default:
033abf97 498 BUG("unknown get_sha1_with_context result %d\n",
122d5346
DT
499 result);
500 break;
501 }
502 fflush(stdout);
82330950 503 return;
122d5346
DT
504 }
505
506 if (ctx.mode == 0) {
507 printf("symlink %"PRIuMAX"\n%s\n",
508 (uintmax_t)ctx.symlink_path.len,
509 ctx.symlink_path.buf);
422b2063 510 fflush(stdout);
82330950 511 return;
05d5667f
AR
512 }
513
bf972896 514 batch_object_write(obj_name, scratch, opt, data, NULL, 0);
05d5667f
AR
515}
516
6a951937
JK
517struct object_cb_data {
518 struct batch_options *opt;
519 struct expand_data *expand;
0750bb5b 520 struct oidset *seen;
79ed0a5e 521 struct strbuf *scratch;
6a951937
JK
522};
523
1b7ba794 524static int batch_object_cb(const struct object_id *oid, void *vdata)
6a951937 525{
3115ee45 526 struct object_cb_data *data = vdata;
1b7ba794 527 oidcpy(&data->expand->oid, oid);
bf972896
JK
528 batch_object_write(NULL, data->scratch, data->opt, data->expand,
529 NULL, 0);
16ddcd40 530 return 0;
6a951937
JK
531}
532
b1adb384
JK
533static int collect_loose_object(const struct object_id *oid,
534 const char *path,
535 void *data)
6a951937 536{
910650d2 537 oid_array_append(data, oid);
3115ee45 538 return 0;
6a951937
JK
539}
540
b1adb384
JK
541static int collect_packed_object(const struct object_id *oid,
542 struct packed_git *pack,
543 uint32_t pos,
544 void *data)
6a951937 545{
910650d2 546 oid_array_append(data, oid);
3115ee45 547 return 0;
6a951937
JK
548}
549
bf972896
JK
550static int batch_unordered_object(const struct object_id *oid,
551 struct packed_git *pack, off_t offset,
552 void *vdata)
0750bb5b
JK
553{
554 struct object_cb_data *data = vdata;
555
ced9fff7 556 if (oidset_insert(data->seen, oid))
0750bb5b 557 return 0;
0750bb5b 558
818e3930 559 oidcpy(&data->expand->oid, oid);
bf972896
JK
560 batch_object_write(NULL, data->scratch, data->opt, data->expand,
561 pack, offset);
818e3930 562 return 0;
0750bb5b
JK
563}
564
565static int batch_unordered_loose(const struct object_id *oid,
566 const char *path,
567 void *data)
568{
bf972896 569 return batch_unordered_object(oid, NULL, 0, data);
0750bb5b
JK
570}
571
572static int batch_unordered_packed(const struct object_id *oid,
573 struct packed_git *pack,
574 uint32_t pos,
575 void *data)
576{
bf972896
JK
577 return batch_unordered_object(oid, pack,
578 nth_packed_object_offset(pack, pos),
579 data);
0750bb5b
JK
580}
581
440c705e
JC
582typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *,
583 struct strbuf *, struct expand_data *);
584
585struct queued_cmd {
586 parse_cmd_fn_t fn;
587 char *line;
588};
589
590static void parse_cmd_contents(struct batch_options *opt,
591 const char *line,
592 struct strbuf *output,
593 struct expand_data *data)
594{
595 opt->batch_mode = BATCH_MODE_CONTENTS;
596 batch_one_object(line, output, opt, data);
597}
598
599static void parse_cmd_info(struct batch_options *opt,
600 const char *line,
601 struct strbuf *output,
602 struct expand_data *data)
603{
604 opt->batch_mode = BATCH_MODE_INFO;
605 batch_one_object(line, output, opt, data);
606}
607
608static void dispatch_calls(struct batch_options *opt,
609 struct strbuf *output,
610 struct expand_data *data,
611 struct queued_cmd *cmd,
612 int nr)
613{
614 int i;
615
616 if (!opt->buffer_output)
617 die(_("flush is only for --buffer mode"));
618
619 for (i = 0; i < nr; i++)
620 cmd[i].fn(opt, cmd[i].line, output, data);
621
622 fflush(stdout);
623}
624
625static void free_cmds(struct queued_cmd *cmd, size_t *nr)
626{
627 size_t i;
628
629 for (i = 0; i < *nr; i++)
630 FREE_AND_NULL(cmd[i].line);
631
632 *nr = 0;
633}
634
635
636static const struct parse_cmd {
637 const char *name;
638 parse_cmd_fn_t fn;
639 unsigned takes_args;
640} commands[] = {
641 { "contents", parse_cmd_contents, 1},
642 { "info", parse_cmd_info, 1},
643 { "flush", NULL, 0},
644};
645
646static void batch_objects_command(struct batch_options *opt,
647 struct strbuf *output,
648 struct expand_data *data)
649{
650 struct strbuf input = STRBUF_INIT;
651 struct queued_cmd *queued_cmd = NULL;
652 size_t alloc = 0, nr = 0;
653
db9d67f2
TB
654 while (1) {
655 int i, ret;
440c705e
JC
656 const struct parse_cmd *cmd = NULL;
657 const char *p = NULL, *cmd_end;
658 struct queued_cmd call = {0};
659
db9d67f2
TB
660 if (opt->nul_terminated)
661 ret = strbuf_getline_nul(&input, stdin);
662 else
663 ret = strbuf_getline(&input, stdin);
664
665 if (ret)
666 break;
667
440c705e
JC
668 if (!input.len)
669 die(_("empty command in input"));
670 if (isspace(*input.buf))
671 die(_("whitespace before command: '%s'"), input.buf);
672
673 for (i = 0; i < ARRAY_SIZE(commands); i++) {
674 if (!skip_prefix(input.buf, commands[i].name, &cmd_end))
675 continue;
676
677 cmd = &commands[i];
678 if (cmd->takes_args) {
679 if (*cmd_end != ' ')
680 die(_("%s requires arguments"),
681 commands[i].name);
682
683 p = cmd_end + 1;
684 } else if (*cmd_end) {
685 die(_("%s takes no arguments"),
686 commands[i].name);
687 }
688
689 break;
690 }
691
692 if (!cmd)
693 die(_("unknown command: '%s'"), input.buf);
694
695 if (!strcmp(cmd->name, "flush")) {
696 dispatch_calls(opt, output, data, queued_cmd, nr);
697 free_cmds(queued_cmd, &nr);
698 } else if (!opt->buffer_output) {
699 cmd->fn(opt, p, output, data);
700 } else {
701 ALLOC_GROW(queued_cmd, nr + 1, alloc);
702 call.fn = cmd->fn;
703 call.line = xstrdup_or_null(p);
704 queued_cmd[nr++] = call;
705 }
706 }
707
708 if (opt->buffer_output &&
709 nr &&
710 !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) {
711 dispatch_calls(opt, output, data, queued_cmd, nr);
712 free_cmds(queued_cmd, &nr);
713 }
714
d90dafbe 715 free_cmds(queued_cmd, &nr);
440c705e
JC
716 free(queued_cmd);
717 strbuf_release(&input);
718}
719
eb54a339
JC
720#define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
721
b71bd480 722static int batch_objects(struct batch_options *opt)
05d5667f 723{
54d2f0d9
JK
724 struct strbuf input = STRBUF_INIT;
725 struct strbuf output = STRBUF_INIT;
93d2a607 726 struct expand_data data;
a42fcd15 727 int save_warning;
07e23839 728 int retval = 0;
93d2a607 729
93d2a607
JK
730 /*
731 * Expand once with our special mark_query flag, which will prime the
c93206b4 732 * object_info to be handed to oid_object_info_extended for each
93d2a607
JK
733 * object.
734 */
735 memset(&data, 0, sizeof(data));
736 data.mark_query = 1;
eb54a339
JC
737 strbuf_expand(&output,
738 opt->format ? opt->format : DEFAULT_FORMAT,
739 expand_format,
740 &data);
93d2a607 741 data.mark_query = 0;
54d2f0d9 742 strbuf_release(&output);
a2c75526 743 if (opt->transform_mode)
32145943 744 data.split_on_whitespace = 1;
05d5667f 745
eb54a339
JC
746 if (opt->format && !strcmp(opt->format, DEFAULT_FORMAT))
747 opt->format = NULL;
6554dfa9
JK
748 /*
749 * If we are printing out the object, then always fill in the type,
750 * since we will want to decide whether or not to stream.
751 */
ac4e58ca 752 if (opt->batch_mode == BATCH_MODE_CONTENTS)
6554dfa9
JK
753 data.info.typep = &data.type;
754
e16acc80 755 if (opt->all_objects) {
ee02ac61 756 struct object_cb_data cb;
e16acc80
ZH
757 struct object_info empty = OBJECT_INFO_INIT;
758
759 if (!memcmp(&data.info, &empty, sizeof(empty)))
760 data.skip_object_info = 1;
3115ee45 761
b14ed5ad
CC
762 if (has_promisor_remote())
763 warning("This repository uses promisor remotes. Some objects may not be loaded.");
3115ee45 764
5c5b29b4
JK
765 read_replace_refs = 0;
766
6a951937
JK
767 cb.opt = opt;
768 cb.expand = &data;
79ed0a5e 769 cb.scratch = &output;
3115ee45 770
0750bb5b
JK
771 if (opt->unordered) {
772 struct oidset seen = OIDSET_INIT;
773
774 cb.seen = &seen;
775
776 for_each_loose_object(batch_unordered_loose, &cb, 0);
777 for_each_packed_object(batch_unordered_packed, &cb,
778 FOR_EACH_OBJECT_PACK_ORDER);
779
780 oidset_clear(&seen);
781 } else {
782 struct oid_array sa = OID_ARRAY_INIT;
783
784 for_each_loose_object(collect_loose_object, &sa, 0);
785 for_each_packed_object(collect_packed_object, &sa, 0);
786
787 oid_array_for_each_unique(&sa, batch_object_cb, &cb);
788
789 oid_array_clear(&sa);
790 }
791
79ed0a5e 792 strbuf_release(&output);
6a951937
JK
793 return 0;
794 }
795
25fba78d
JK
796 /*
797 * We are going to call get_sha1 on a potentially very large number of
798 * objects. In most large cases, these will be actual object sha1s. The
799 * cost to double-check that each one is not also a ref (just so we can
800 * warn) ends up dwarfing the actual cost of the object lookups
801 * themselves. We can work around it by just turning off the warning.
802 */
a42fcd15 803 save_warning = warn_on_object_refname_ambiguity;
25fba78d
JK
804 warn_on_object_refname_ambiguity = 0;
805
440c705e
JC
806 if (opt->batch_mode == BATCH_MODE_QUEUE_AND_DISPATCH) {
807 batch_objects_command(opt, &output, &data);
808 goto cleanup;
809 }
810
db9d67f2
TB
811 while (1) {
812 int ret;
813 if (opt->nul_terminated)
814 ret = strbuf_getline_nul(&input, stdin);
815 else
816 ret = strbuf_getline(&input, stdin);
817
818 if (ret == EOF)
819 break;
820
97be0407
JK
821 if (data.split_on_whitespace) {
822 /*
823 * Split at first whitespace, tying off the beginning
824 * of the string and saving the remainder (or NULL) in
825 * data.rest.
826 */
54d2f0d9 827 char *p = strpbrk(input.buf, " \t");
97be0407
JK
828 if (p) {
829 while (*p && strchr(" \t", *p))
830 *p++ = '\0';
831 }
832 data.rest = p;
833 }
834
79ed0a5e 835 batch_one_object(input.buf, &output, opt, &data);
05d5667f
AR
836 }
837
440c705e 838 cleanup:
54d2f0d9 839 strbuf_release(&input);
79ed0a5e 840 strbuf_release(&output);
a42fcd15 841 warn_on_object_refname_ambiguity = save_warning;
07e23839 842 return retval;
05d5667f
AR
843}
844
e5fba602
CP
845static int git_cat_file_config(const char *var, const char *value, void *cb)
846{
6680a087 847 if (userdiff_config(var, value) < 0)
e5fba602 848 return -1;
e5fba602
CP
849
850 return git_default_config(var, value, cb);
851}
852
b71bd480
JK
853static int batch_option_callback(const struct option *opt,
854 const char *arg,
855 int unset)
856{
857 struct batch_options *bo = opt->value;
858
517fe807
JK
859 BUG_ON_OPT_NEG(unset);
860
122d5346 861 if (bo->enabled) {
0eb8d376 862 return error(_("only one batch option may be specified"));
b71bd480
JK
863 }
864
865 bo->enabled = 1;
ac4e58ca
JC
866
867 if (!strcmp(opt->long_name, "batch"))
868 bo->batch_mode = BATCH_MODE_CONTENTS;
869 else if (!strcmp(opt->long_name, "batch-check"))
870 bo->batch_mode = BATCH_MODE_INFO;
440c705e
JC
871 else if (!strcmp(opt->long_name, "batch-command"))
872 bo->batch_mode = BATCH_MODE_QUEUE_AND_DISPATCH;
ac4e58ca
JC
873 else
874 BUG("%s given to batch-option-callback", opt->long_name);
875
93d2a607 876 bo->format = arg;
b71bd480
JK
877
878 return 0;
879}
880
9cf71b17
AR
881int cmd_cat_file(int argc, const char **argv, const char *prefix)
882{
b71bd480 883 int opt = 0;
b3fe4680
ÆAB
884 int opt_cw = 0;
885 int opt_epts = 0;
4814dbe8 886 const char *exp_type = NULL, *obj_name = NULL;
b71bd480 887 struct batch_options batch = {0};
39e4ae38 888 int unknown_type = 0;
9cf71b17 889
5a404178
ÆAB
890 const char * const usage[] = {
891 N_("git cat-file <type> <object>"),
892 N_("git cat-file (-e | -p) <object>"),
83dc4434 893 N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"),
440c705e 894 N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
5a404178
ÆAB
895 " [--buffer] [--follow-symlinks] [--unordered]\n"
896 " [--textconv | --filters]"),
83dc4434 897 N_("git cat-file (--textconv | --filters)\n"
5a404178
ÆAB
898 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"),
899 NULL
900 };
15d8e565 901 const struct option options[] = {
57d6a1cf
ÆAB
902 /* Simple queries */
903 OPT_GROUP(N_("Check object existence or emit object contents")),
b48158ac 904 OPT_CMDMODE('e', NULL, &opt,
57d6a1cf
ÆAB
905 N_("check if <object> exists"), 'e'),
906 OPT_CMDMODE('p', NULL, &opt, N_("pretty-print <object> content"), 'p'),
907
908 OPT_GROUP(N_("Emit [broken] object attributes")),
909 OPT_CMDMODE('t', NULL, &opt, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'),
910 OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
ad42f28d 911 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
39e4ae38 912 N_("allow -s and -t to work with broken/corrupt objects")),
ec031da9
SA
913 OPT_BOOL(0, "use-mailmap", &use_mailmap, N_("use mail map file")),
914 OPT_ALIAS(0, "mailmap", "use-mailmap"),
57d6a1cf
ÆAB
915 /* Batch mode */
916 OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")),
917 OPT_CALLBACK_F(0, "batch", &batch, N_("format"),
918 N_("show full <object> or <rev> contents"),
0ad1efb4 919 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
203c8533 920 batch_option_callback),
57d6a1cf
ÆAB
921 OPT_CALLBACK_F(0, "batch-check", &batch, N_("format"),
922 N_("like --batch, but don't emit <contents>"),
0ad1efb4 923 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
203c8533 924 batch_option_callback),
db9d67f2 925 OPT_BOOL('z', NULL, &batch.nul_terminated, N_("stdin is NUL-terminated")),
440c705e
JC
926 OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
927 N_("read commands from stdin"),
928 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
929 batch_option_callback),
57d6a1cf
ÆAB
930 OPT_CMDMODE(0, "batch-all-objects", &opt,
931 N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'),
932 /* Batch-specific options */
933 OPT_GROUP(N_("Change or optimize batch output")),
934 OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
122d5346 935 OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
57d6a1cf 936 N_("follow in-tree symlinks")),
0750bb5b 937 OPT_BOOL(0, "unordered", &batch.unordered,
57d6a1cf
ÆAB
938 N_("do not order objects before emitting them")),
939 /* Textconv options, stand-ole*/
940 OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")),
941 OPT_CMDMODE(0, "textconv", &opt,
942 N_("run textconv on object's content"), 'c'),
943 OPT_CMDMODE(0, "filters", &opt,
944 N_("run filters on object's content"), 'w'),
945 OPT_STRING(0, "path", &force_path, N_("blob|tree"),
83dc4434 946 N_("use a <path> for (--textconv | --filters); Not with 'batch'")),
15d8e565
MB
947 OPT_END()
948 };
a8128ed6 949
e5fba602 950 git_config(git_cat_file_config, NULL);
4814dbe8 951
6a36e1e7 952 batch.buffer_output = -1;
05d5667f 953
485fd2c3 954 argc = parse_options(argc, argv, prefix, options, usage, 0);
b3fe4680
ÆAB
955 opt_cw = (opt == 'c' || opt == 'w');
956 opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's');
9cf71b17 957
ec031da9
SA
958 if (use_mailmap)
959 read_mailmap(&mailmap);
960
b3fe4680
ÆAB
961 /* --batch-all-objects? */
962 if (opt == 'b')
963 batch.all_objects = 1;
7bcf3414 964
b3fe4680
ÆAB
965 /* Option compatibility */
966 if (force_path && !opt_cw)
967 usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
968 usage, options,
969 "--path", _("path|tree-ish"), "--filters",
970 "--textconv");
32145943 971
b3fe4680
ÆAB
972 /* Option compatibility with batch mode */
973 if (batch.enabled)
974 ;
975 else if (batch.follow_symlinks)
976 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
5fb24902 977 "--follow-symlinks");
b3fe4680
ÆAB
978 else if (batch.buffer_output >= 0)
979 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
980 "--buffer");
981 else if (batch.all_objects)
982 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
983 "--batch-all-objects");
db9d67f2
TB
984 else if (batch.nul_terminated)
985 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
986 "-z");
b3fe4680
ÆAB
987
988 /* Batch defaults */
6a36e1e7
JK
989 if (batch.buffer_output < 0)
990 batch.buffer_output = batch.all_objects;
991
b3fe4680
ÆAB
992 /* Return early if we're in batch mode? */
993 if (batch.enabled) {
994 if (opt_cw)
a2c75526 995 batch.transform_mode = opt;
b3fe4680
ÆAB
996 else if (opt && opt != 'b')
997 usage_msg_optf(_("'-%c' is incompatible with batch mode"),
998 usage, options, opt);
999 else if (argc)
1000 usage_msg_opt(_("batch modes take no arguments"), usage,
1001 options);
1002
b71bd480 1003 return batch_objects(&batch);
b3fe4680
ÆAB
1004 }
1005
1006 if (opt) {
1007 if (!argc && opt == 'c')
1008 usage_msg_optf(_("<rev> required with '%s'"),
1009 usage, options, "--textconv");
1010 else if (!argc && opt == 'w')
1011 usage_msg_optf(_("<rev> required with '%s'"),
1012 usage, options, "--filters");
1013 else if (!argc && opt_epts)
1014 usage_msg_optf(_("<object> required with '-%c'"),
1015 usage, options, opt);
1016 else if (argc == 1)
1017 obj_name = argv[0];
1018 else
1019 usage_msg_opt(_("too many arguments"), usage, options);
1020 } else if (!argc) {
1021 usage_with_options(usage, options);
1022 } else if (argc != 2) {
1023 usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"),
1024 usage, options, argc);
1025 } else if (argc) {
1026 exp_type = argv[0];
1027 obj_name = argv[1];
1028 }
05d5667f 1029
39e4ae38
KN
1030 if (unknown_type && opt != 't' && opt != 's')
1031 die("git cat-file --allow-unknown-type: use with -s or -t");
1032 return cat_one_file(opt, exp_type, obj_name, unknown_type);
9cf71b17 1033}