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