]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/cat-file.c
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / builtin / cat-file.c
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define USE_THE_INDEX_VARIABLE
7 #include "cache.h"
8 #include "alloc.h"
9 #include "config.h"
10 #include "convert.h"
11 #include "builtin.h"
12 #include "diff.h"
13 #include "environment.h"
14 #include "gettext.h"
15 #include "hex.h"
16 #include "ident.h"
17 #include "parse-options.h"
18 #include "userdiff.h"
19 #include "streaming.h"
20 #include "tree-walk.h"
21 #include "oid-array.h"
22 #include "packfile.h"
23 #include "object-name.h"
24 #include "object-store.h"
25 #include "replace-object.h"
26 #include "promisor-remote.h"
27 #include "mailmap.h"
28 #include "write-or-die.h"
29
30 enum batch_mode {
31 BATCH_MODE_CONTENTS,
32 BATCH_MODE_INFO,
33 BATCH_MODE_QUEUE_AND_DISPATCH,
34 };
35
36 struct batch_options {
37 int enabled;
38 int follow_symlinks;
39 enum batch_mode batch_mode;
40 int buffer_output;
41 int all_objects;
42 int unordered;
43 int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */
44 int nul_terminated;
45 const char *format;
46 };
47
48 static const char *force_path;
49
50 static struct string_list mailmap = STRING_LIST_INIT_NODUP;
51 static int use_mailmap;
52
53 static char *replace_idents_using_mailmap(char *, size_t *);
54
55 static 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
66 static int filter_object(const char *path, unsigned mode,
67 const struct object_id *oid,
68 char **buf, unsigned long *size)
69 {
70 enum object_type type;
71
72 *buf = repo_read_object_file(the_repository, oid, &type, size);
73 if (!*buf)
74 return error(_("cannot read object %s '%s'"),
75 oid_to_hex(oid), path);
76 if ((type == OBJ_BLOB) && S_ISREG(mode)) {
77 struct strbuf strbuf = STRBUF_INIT;
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)) {
82 free(*buf);
83 *size = strbuf.len;
84 *buf = strbuf_detach(&strbuf, NULL);
85 }
86 }
87
88 return 0;
89 }
90
91 static 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
98 static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
99 int unknown_type)
100 {
101 int ret;
102 struct object_id oid;
103 enum object_type type;
104 char *buf;
105 unsigned long size;
106 struct object_context obj_context;
107 struct object_info oi = OBJECT_INFO_INIT;
108 struct strbuf sb = STRBUF_INIT;
109 unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
110 unsigned get_oid_flags = GET_OID_RECORD_PATH | GET_OID_ONLY_TO_DIE;
111 const char *path = force_path;
112 const int opt_cw = (opt == 'c' || opt == 'w');
113 if (!path && opt_cw)
114 get_oid_flags |= GET_OID_REQUIRE_PATH;
115
116 if (unknown_type)
117 flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
118
119 if (get_oid_with_context(the_repository, obj_name, get_oid_flags, &oid,
120 &obj_context))
121 die("Not a valid object name %s", obj_name);
122
123 if (!path)
124 path = obj_context.path;
125 if (obj_context.mode == S_IFINVALID)
126 obj_context.mode = 0100644;
127
128 buf = NULL;
129 switch (opt) {
130 case 't':
131 oi.type_name = &sb;
132 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
133 die("git cat-file: could not get object info");
134 if (sb.len) {
135 printf("%s\n", sb.buf);
136 strbuf_release(&sb);
137 ret = 0;
138 goto cleanup;
139 }
140 break;
141
142 case 's':
143 oi.sizep = &size;
144
145 if (use_mailmap) {
146 oi.typep = &type;
147 oi.contentp = (void**)&buf;
148 }
149
150 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
151 die("git cat-file: could not get object info");
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
159 printf("%"PRIuMAX"\n", (uintmax_t)size);
160 ret = 0;
161 goto cleanup;
162
163 case 'e':
164 return !repo_has_object_file(the_repository, &oid);
165
166 case 'w':
167
168 if (filter_object(path, obj_context.mode,
169 &oid, &buf, &size)) {
170 ret = -1;
171 goto cleanup;
172 }
173 break;
174
175 case 'c':
176 if (textconv_object(the_repository, path, obj_context.mode,
177 &oid, 1, &buf, &size))
178 break;
179 /* else fallthrough */
180
181 case 'p':
182 type = oid_object_info(the_repository, &oid, NULL);
183 if (type < 0)
184 die("Not a valid object name %s", obj_name);
185
186 /* custom pretty-print here */
187 if (type == OBJ_TREE) {
188 const char *ls_args[3] = { NULL };
189 ls_args[0] = "ls-tree";
190 ls_args[1] = obj_name;
191 ret = cmd_ls_tree(2, ls_args, NULL);
192 goto cleanup;
193 }
194
195 if (type == OBJ_BLOB) {
196 ret = stream_blob(&oid);
197 goto cleanup;
198 }
199 buf = repo_read_object_file(the_repository, &oid, &type,
200 &size);
201 if (!buf)
202 die("Cannot read object %s", obj_name);
203
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
210 /* otherwise just spit out the data */
211 break;
212
213 case 0:
214 {
215 enum object_type exp_type_id = type_from_string(exp_type);
216
217 if (exp_type_id == OBJ_BLOB) {
218 struct object_id blob_oid;
219 if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
220 char *buffer = repo_read_object_file(the_repository,
221 &oid,
222 &type,
223 &size);
224 const char *target;
225 if (!skip_prefix(buffer, "object ", &target) ||
226 get_oid_hex(target, &blob_oid))
227 die("%s not a valid tag", oid_to_hex(&oid));
228 free(buffer);
229 } else
230 oidcpy(&blob_oid, &oid);
231
232 if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB) {
233 ret = stream_blob(&blob_oid);
234 goto cleanup;
235 }
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 }
243 buf = read_object_with_reference(the_repository, &oid,
244 exp_type_id, &size, NULL);
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 }
251 break;
252 }
253 default:
254 die("git cat-file: unknown option: %s", exp_type);
255 }
256
257 if (!buf)
258 die("git cat-file %s: bad file", obj_name);
259
260 write_or_die(1, buf, size);
261 ret = 0;
262 cleanup:
263 free(buf);
264 free(obj_context.path);
265 return ret;
266 }
267
268 struct expand_data {
269 struct object_id oid;
270 enum object_type type;
271 unsigned long size;
272 off_t disk_size;
273 const char *rest;
274 struct object_id delta_base_oid;
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
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
289 /*
290 * After a mark_query run, this object_info is set up to be
291 * passed to oid_object_info_extended. It will point to the data
292 * elements above, so you can retrieve the response from there.
293 */
294 struct object_info info;
295
296 /*
297 * This flag will be true if the requested batch format and options
298 * don't require us to call oid_object_info, which can then be
299 * optimized out.
300 */
301 unsigned skip_object_info : 1;
302 };
303
304 static 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
310 static void expand_atom(struct strbuf *sb, const char *atom, int len,
311 void *vdata)
312 {
313 struct expand_data *data = vdata;
314
315 if (is_atom("objectname", atom, len)) {
316 if (!data->mark_query)
317 strbuf_addstr(sb, oid_to_hex(&data->oid));
318 } else if (is_atom("objecttype", atom, len)) {
319 if (data->mark_query)
320 data->info.typep = &data->type;
321 else
322 strbuf_addstr(sb, type_name(data->type));
323 } else if (is_atom("objectsize", atom, len)) {
324 if (data->mark_query)
325 data->info.sizep = &data->size;
326 else
327 strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
328 } else if (is_atom("objectsize:disk", atom, len)) {
329 if (data->mark_query)
330 data->info.disk_sizep = &data->disk_size;
331 else
332 strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
333 } else if (is_atom("rest", atom, len)) {
334 if (data->mark_query)
335 data->split_on_whitespace = 1;
336 else if (data->rest)
337 strbuf_addstr(sb, data->rest);
338 } else if (is_atom("deltabase", atom, len)) {
339 if (data->mark_query)
340 data->info.delta_base_oid = &data->delta_base_oid;
341 else
342 strbuf_addstr(sb,
343 oid_to_hex(&data->delta_base_oid));
344 } else
345 die("unknown format element: %.*s", len, atom);
346 }
347
348 static size_t expand_format(struct strbuf *sb, const char *start, void *data)
349 {
350 const char *end;
351
352 if (*start != '(')
353 return 0;
354 end = strchr(start + 1, ')');
355 if (!end)
356 die("format element '%s' does not end in ')'", start);
357
358 expand_atom(sb, start + 1, end - start - 1, data);
359
360 return end - start + 1;
361 }
362
363 static 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
372 static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
373 {
374 const struct object_id *oid = &data->oid;
375
376 assert(data->info.typep);
377
378 if (data->type == OBJ_BLOB) {
379 if (opt->buffer_output)
380 fflush(stdout);
381 if (opt->transform_mode) {
382 char *contents;
383 unsigned long size;
384
385 if (!data->rest)
386 die("missing path for '%s'", oid_to_hex(oid));
387
388 if (opt->transform_mode == 'w') {
389 if (filter_object(data->rest, 0100644, oid,
390 &contents, &size))
391 die("could not convert '%s' %s",
392 oid_to_hex(oid), data->rest);
393 } else if (opt->transform_mode == 'c') {
394 enum object_type type;
395 if (!textconv_object(the_repository,
396 data->rest, 0100644, oid,
397 1, &contents, &size))
398 contents = repo_read_object_file(the_repository,
399 oid,
400 &type,
401 &size);
402 if (!contents)
403 die("could not convert '%s' %s",
404 oid_to_hex(oid), data->rest);
405 } else
406 BUG("invalid transform_mode: %c", opt->transform_mode);
407 batch_write(opt, contents, size);
408 free(contents);
409 } else {
410 stream_blob(oid);
411 }
412 }
413 else {
414 enum object_type type;
415 unsigned long size;
416 void *contents;
417
418 contents = repo_read_object_file(the_repository, oid, &type,
419 &size);
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
427 if (!contents)
428 die("object %s disappeared", oid_to_hex(oid));
429 if (type != data->type)
430 die("object %s changed type!?", oid_to_hex(oid));
431 if (data->info.sizep && size != data->size && !use_mailmap)
432 die("object %s changed size!?", oid_to_hex(oid));
433
434 batch_write(opt, contents, size);
435 free(contents);
436 }
437 }
438
439 static void print_default_format(struct strbuf *scratch, struct expand_data *data)
440 {
441 strbuf_addf(scratch, "%s %s %"PRIuMAX"\n", oid_to_hex(&data->oid),
442 type_name(data->type),
443 (uintmax_t)data->size);
444 }
445
446 /*
447 * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
448 * which the object may be accessed (though note that we may also rely on
449 * data->oid, too). If "pack" is NULL, then offset is ignored.
450 */
451 static void batch_object_write(const char *obj_name,
452 struct strbuf *scratch,
453 struct batch_options *opt,
454 struct expand_data *data,
455 struct packed_git *pack,
456 off_t offset)
457 {
458 if (!data->skip_object_info) {
459 int ret;
460
461 if (use_mailmap)
462 data->info.typep = &data->type;
463
464 if (pack)
465 ret = packed_object_info(the_repository, pack, offset,
466 &data->info);
467 else
468 ret = oid_object_info_extended(the_repository,
469 &data->oid, &data->info,
470 OBJECT_INFO_LOOKUP_REPLACE);
471 if (ret < 0) {
472 printf("%s missing\n",
473 obj_name ? obj_name : oid_to_hex(&data->oid));
474 fflush(stdout);
475 return;
476 }
477
478 if (use_mailmap && (data->type == OBJ_COMMIT || data->type == OBJ_TAG)) {
479 size_t s = data->size;
480 char *buf = NULL;
481
482 buf = repo_read_object_file(the_repository, &data->oid, &data->type,
483 &data->size);
484 buf = replace_idents_using_mailmap(buf, &s);
485 data->size = cast_size_t_to_ulong(s);
486
487 free(buf);
488 }
489 }
490
491 strbuf_reset(scratch);
492
493 if (!opt->format) {
494 print_default_format(scratch, data);
495 } else {
496 strbuf_expand(scratch, opt->format, expand_format, data);
497 strbuf_addch(scratch, '\n');
498 }
499
500 batch_write(opt, scratch->buf, scratch->len);
501
502 if (opt->batch_mode == BATCH_MODE_CONTENTS) {
503 print_object_or_die(opt, data);
504 batch_write(opt, "\n", 1);
505 }
506 }
507
508 static void batch_one_object(const char *obj_name,
509 struct strbuf *scratch,
510 struct batch_options *opt,
511 struct expand_data *data)
512 {
513 struct object_context ctx;
514 int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
515 enum get_oid_result result;
516
517 result = get_oid_with_context(the_repository, obj_name,
518 flags, &data->oid, &ctx);
519 if (result != FOUND) {
520 switch (result) {
521 case MISSING_OBJECT:
522 printf("%s missing\n", obj_name);
523 break;
524 case SHORT_NAME_AMBIGUOUS:
525 printf("%s ambiguous\n", obj_name);
526 break;
527 case DANGLING_SYMLINK:
528 printf("dangling %"PRIuMAX"\n%s\n",
529 (uintmax_t)strlen(obj_name), obj_name);
530 break;
531 case SYMLINK_LOOP:
532 printf("loop %"PRIuMAX"\n%s\n",
533 (uintmax_t)strlen(obj_name), obj_name);
534 break;
535 case NOT_DIR:
536 printf("notdir %"PRIuMAX"\n%s\n",
537 (uintmax_t)strlen(obj_name), obj_name);
538 break;
539 default:
540 BUG("unknown get_sha1_with_context result %d\n",
541 result);
542 break;
543 }
544 fflush(stdout);
545 return;
546 }
547
548 if (ctx.mode == 0) {
549 printf("symlink %"PRIuMAX"\n%s\n",
550 (uintmax_t)ctx.symlink_path.len,
551 ctx.symlink_path.buf);
552 fflush(stdout);
553 return;
554 }
555
556 batch_object_write(obj_name, scratch, opt, data, NULL, 0);
557 }
558
559 struct object_cb_data {
560 struct batch_options *opt;
561 struct expand_data *expand;
562 struct oidset *seen;
563 struct strbuf *scratch;
564 };
565
566 static int batch_object_cb(const struct object_id *oid, void *vdata)
567 {
568 struct object_cb_data *data = vdata;
569 oidcpy(&data->expand->oid, oid);
570 batch_object_write(NULL, data->scratch, data->opt, data->expand,
571 NULL, 0);
572 return 0;
573 }
574
575 static int collect_loose_object(const struct object_id *oid,
576 const char *path UNUSED,
577 void *data)
578 {
579 oid_array_append(data, oid);
580 return 0;
581 }
582
583 static int collect_packed_object(const struct object_id *oid,
584 struct packed_git *pack UNUSED,
585 uint32_t pos UNUSED,
586 void *data)
587 {
588 oid_array_append(data, oid);
589 return 0;
590 }
591
592 static int batch_unordered_object(const struct object_id *oid,
593 struct packed_git *pack, off_t offset,
594 void *vdata)
595 {
596 struct object_cb_data *data = vdata;
597
598 if (oidset_insert(data->seen, oid))
599 return 0;
600
601 oidcpy(&data->expand->oid, oid);
602 batch_object_write(NULL, data->scratch, data->opt, data->expand,
603 pack, offset);
604 return 0;
605 }
606
607 static int batch_unordered_loose(const struct object_id *oid,
608 const char *path UNUSED,
609 void *data)
610 {
611 return batch_unordered_object(oid, NULL, 0, data);
612 }
613
614 static int batch_unordered_packed(const struct object_id *oid,
615 struct packed_git *pack,
616 uint32_t pos,
617 void *data)
618 {
619 return batch_unordered_object(oid, pack,
620 nth_packed_object_offset(pack, pos),
621 data);
622 }
623
624 typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *,
625 struct strbuf *, struct expand_data *);
626
627 struct queued_cmd {
628 parse_cmd_fn_t fn;
629 char *line;
630 };
631
632 static void parse_cmd_contents(struct batch_options *opt,
633 const char *line,
634 struct strbuf *output,
635 struct expand_data *data)
636 {
637 opt->batch_mode = BATCH_MODE_CONTENTS;
638 batch_one_object(line, output, opt, data);
639 }
640
641 static void parse_cmd_info(struct batch_options *opt,
642 const char *line,
643 struct strbuf *output,
644 struct expand_data *data)
645 {
646 opt->batch_mode = BATCH_MODE_INFO;
647 batch_one_object(line, output, opt, data);
648 }
649
650 static void dispatch_calls(struct batch_options *opt,
651 struct strbuf *output,
652 struct expand_data *data,
653 struct queued_cmd *cmd,
654 int nr)
655 {
656 int i;
657
658 if (!opt->buffer_output)
659 die(_("flush is only for --buffer mode"));
660
661 for (i = 0; i < nr; i++)
662 cmd[i].fn(opt, cmd[i].line, output, data);
663
664 fflush(stdout);
665 }
666
667 static void free_cmds(struct queued_cmd *cmd, size_t *nr)
668 {
669 size_t i;
670
671 for (i = 0; i < *nr; i++)
672 FREE_AND_NULL(cmd[i].line);
673
674 *nr = 0;
675 }
676
677
678 static const struct parse_cmd {
679 const char *name;
680 parse_cmd_fn_t fn;
681 unsigned takes_args;
682 } commands[] = {
683 { "contents", parse_cmd_contents, 1},
684 { "info", parse_cmd_info, 1},
685 { "flush", NULL, 0},
686 };
687
688 static void batch_objects_command(struct batch_options *opt,
689 struct strbuf *output,
690 struct expand_data *data)
691 {
692 struct strbuf input = STRBUF_INIT;
693 struct queued_cmd *queued_cmd = NULL;
694 size_t alloc = 0, nr = 0;
695
696 while (1) {
697 int i, ret;
698 const struct parse_cmd *cmd = NULL;
699 const char *p = NULL, *cmd_end;
700 struct queued_cmd call = {0};
701
702 if (opt->nul_terminated)
703 ret = strbuf_getline_nul(&input, stdin);
704 else
705 ret = strbuf_getline(&input, stdin);
706
707 if (ret)
708 break;
709
710 if (!input.len)
711 die(_("empty command in input"));
712 if (isspace(*input.buf))
713 die(_("whitespace before command: '%s'"), input.buf);
714
715 for (i = 0; i < ARRAY_SIZE(commands); i++) {
716 if (!skip_prefix(input.buf, commands[i].name, &cmd_end))
717 continue;
718
719 cmd = &commands[i];
720 if (cmd->takes_args) {
721 if (*cmd_end != ' ')
722 die(_("%s requires arguments"),
723 commands[i].name);
724
725 p = cmd_end + 1;
726 } else if (*cmd_end) {
727 die(_("%s takes no arguments"),
728 commands[i].name);
729 }
730
731 break;
732 }
733
734 if (!cmd)
735 die(_("unknown command: '%s'"), input.buf);
736
737 if (!strcmp(cmd->name, "flush")) {
738 dispatch_calls(opt, output, data, queued_cmd, nr);
739 free_cmds(queued_cmd, &nr);
740 } else if (!opt->buffer_output) {
741 cmd->fn(opt, p, output, data);
742 } else {
743 ALLOC_GROW(queued_cmd, nr + 1, alloc);
744 call.fn = cmd->fn;
745 call.line = xstrdup_or_null(p);
746 queued_cmd[nr++] = call;
747 }
748 }
749
750 if (opt->buffer_output &&
751 nr &&
752 !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) {
753 dispatch_calls(opt, output, data, queued_cmd, nr);
754 free_cmds(queued_cmd, &nr);
755 }
756
757 free_cmds(queued_cmd, &nr);
758 free(queued_cmd);
759 strbuf_release(&input);
760 }
761
762 #define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
763
764 static int batch_objects(struct batch_options *opt)
765 {
766 struct strbuf input = STRBUF_INIT;
767 struct strbuf output = STRBUF_INIT;
768 struct expand_data data;
769 int save_warning;
770 int retval = 0;
771
772 /*
773 * Expand once with our special mark_query flag, which will prime the
774 * object_info to be handed to oid_object_info_extended for each
775 * object.
776 */
777 memset(&data, 0, sizeof(data));
778 data.mark_query = 1;
779 strbuf_expand(&output,
780 opt->format ? opt->format : DEFAULT_FORMAT,
781 expand_format,
782 &data);
783 data.mark_query = 0;
784 strbuf_release(&output);
785 if (opt->transform_mode)
786 data.split_on_whitespace = 1;
787
788 if (opt->format && !strcmp(opt->format, DEFAULT_FORMAT))
789 opt->format = NULL;
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 */
794 if (opt->batch_mode == BATCH_MODE_CONTENTS)
795 data.info.typep = &data.type;
796
797 if (opt->all_objects) {
798 struct object_cb_data cb;
799 struct object_info empty = OBJECT_INFO_INIT;
800
801 if (!memcmp(&data.info, &empty, sizeof(empty)))
802 data.skip_object_info = 1;
803
804 if (repo_has_promisor_remote(the_repository))
805 warning("This repository uses promisor remotes. Some objects may not be loaded.");
806
807 read_replace_refs = 0;
808
809 cb.opt = opt;
810 cb.expand = &data;
811 cb.scratch = &output;
812
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
834 strbuf_release(&output);
835 return 0;
836 }
837
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 */
845 save_warning = warn_on_object_refname_ambiguity;
846 warn_on_object_refname_ambiguity = 0;
847
848 if (opt->batch_mode == BATCH_MODE_QUEUE_AND_DISPATCH) {
849 batch_objects_command(opt, &output, &data);
850 goto cleanup;
851 }
852
853 while (1) {
854 int ret;
855 if (opt->nul_terminated)
856 ret = strbuf_getline_nul(&input, stdin);
857 else
858 ret = strbuf_getline(&input, stdin);
859
860 if (ret == EOF)
861 break;
862
863 if (data.split_on_whitespace) {
864 /*
865 * Split at first whitespace, tying off the beginning
866 * of the string and saving the remainder (or NULL) in
867 * data.rest.
868 */
869 char *p = strpbrk(input.buf, " \t");
870 if (p) {
871 while (*p && strchr(" \t", *p))
872 *p++ = '\0';
873 }
874 data.rest = p;
875 }
876
877 batch_one_object(input.buf, &output, opt, &data);
878 }
879
880 cleanup:
881 strbuf_release(&input);
882 strbuf_release(&output);
883 warn_on_object_refname_ambiguity = save_warning;
884 return retval;
885 }
886
887 static int git_cat_file_config(const char *var, const char *value, void *cb)
888 {
889 if (userdiff_config(var, value) < 0)
890 return -1;
891
892 return git_default_config(var, value, cb);
893 }
894
895 static int batch_option_callback(const struct option *opt,
896 const char *arg,
897 int unset)
898 {
899 struct batch_options *bo = opt->value;
900
901 BUG_ON_OPT_NEG(unset);
902
903 if (bo->enabled) {
904 return error(_("only one batch option may be specified"));
905 }
906
907 bo->enabled = 1;
908
909 if (!strcmp(opt->long_name, "batch"))
910 bo->batch_mode = BATCH_MODE_CONTENTS;
911 else if (!strcmp(opt->long_name, "batch-check"))
912 bo->batch_mode = BATCH_MODE_INFO;
913 else if (!strcmp(opt->long_name, "batch-command"))
914 bo->batch_mode = BATCH_MODE_QUEUE_AND_DISPATCH;
915 else
916 BUG("%s given to batch-option-callback", opt->long_name);
917
918 bo->format = arg;
919
920 return 0;
921 }
922
923 int cmd_cat_file(int argc, const char **argv, const char *prefix)
924 {
925 int opt = 0;
926 int opt_cw = 0;
927 int opt_epts = 0;
928 const char *exp_type = NULL, *obj_name = NULL;
929 struct batch_options batch = {0};
930 int unknown_type = 0;
931
932 const char * const usage[] = {
933 N_("git cat-file <type> <object>"),
934 N_("git cat-file (-e | -p) <object>"),
935 N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"),
936 N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
937 " [--buffer] [--follow-symlinks] [--unordered]\n"
938 " [--textconv | --filters] [-z]"),
939 N_("git cat-file (--textconv | --filters)\n"
940 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"),
941 NULL
942 };
943 const struct option options[] = {
944 /* Simple queries */
945 OPT_GROUP(N_("Check object existence or emit object contents")),
946 OPT_CMDMODE('e', NULL, &opt,
947 N_("check if <object> exists"), 'e'),
948 OPT_CMDMODE('p', NULL, &opt, N_("pretty-print <object> content"), 'p'),
949
950 OPT_GROUP(N_("Emit [broken] object attributes")),
951 OPT_CMDMODE('t', NULL, &opt, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'),
952 OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
953 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
954 N_("allow -s and -t to work with broken/corrupt objects")),
955 OPT_BOOL(0, "use-mailmap", &use_mailmap, N_("use mail map file")),
956 OPT_ALIAS(0, "mailmap", "use-mailmap"),
957 /* Batch mode */
958 OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")),
959 OPT_CALLBACK_F(0, "batch", &batch, N_("format"),
960 N_("show full <object> or <rev> contents"),
961 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
962 batch_option_callback),
963 OPT_CALLBACK_F(0, "batch-check", &batch, N_("format"),
964 N_("like --batch, but don't emit <contents>"),
965 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
966 batch_option_callback),
967 OPT_BOOL('z', NULL, &batch.nul_terminated, N_("stdin is NUL-terminated")),
968 OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
969 N_("read commands from stdin"),
970 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
971 batch_option_callback),
972 OPT_CMDMODE(0, "batch-all-objects", &opt,
973 N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'),
974 /* Batch-specific options */
975 OPT_GROUP(N_("Change or optimize batch output")),
976 OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
977 OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
978 N_("follow in-tree symlinks")),
979 OPT_BOOL(0, "unordered", &batch.unordered,
980 N_("do not order objects before emitting them")),
981 /* Textconv options, stand-ole*/
982 OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")),
983 OPT_CMDMODE(0, "textconv", &opt,
984 N_("run textconv on object's content"), 'c'),
985 OPT_CMDMODE(0, "filters", &opt,
986 N_("run filters on object's content"), 'w'),
987 OPT_STRING(0, "path", &force_path, N_("blob|tree"),
988 N_("use a <path> for (--textconv | --filters); Not with 'batch'")),
989 OPT_END()
990 };
991
992 git_config(git_cat_file_config, NULL);
993
994 batch.buffer_output = -1;
995
996 argc = parse_options(argc, argv, prefix, options, usage, 0);
997 opt_cw = (opt == 'c' || opt == 'w');
998 opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's');
999
1000 if (use_mailmap)
1001 read_mailmap(&mailmap);
1002
1003 /* --batch-all-objects? */
1004 if (opt == 'b')
1005 batch.all_objects = 1;
1006
1007 /* Option compatibility */
1008 if (force_path && !opt_cw)
1009 usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
1010 usage, options,
1011 "--path", _("path|tree-ish"), "--filters",
1012 "--textconv");
1013
1014 /* Option compatibility with batch mode */
1015 if (batch.enabled)
1016 ;
1017 else if (batch.follow_symlinks)
1018 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1019 "--follow-symlinks");
1020 else if (batch.buffer_output >= 0)
1021 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1022 "--buffer");
1023 else if (batch.all_objects)
1024 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1025 "--batch-all-objects");
1026 else if (batch.nul_terminated)
1027 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1028 "-z");
1029
1030 /* Batch defaults */
1031 if (batch.buffer_output < 0)
1032 batch.buffer_output = batch.all_objects;
1033
1034 /* Return early if we're in batch mode? */
1035 if (batch.enabled) {
1036 if (opt_cw)
1037 batch.transform_mode = opt;
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
1045 return batch_objects(&batch);
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 }
1071
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);
1075 }