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