cat-file: add --batch-all-objects option
[git/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 */
e83c5163 6#include "cache.h"
f81daefe 7#include "builtin.h"
15d8e565 8#include "parse-options.h"
e5fba602 9#include "userdiff.h"
00c8fd49 10#include "streaming.h"
122d5346 11#include "tree-walk.h"
15d8e565 12
bfd15594
JK
13struct batch_options {
14 int enabled;
15 int follow_symlinks;
16 int print_contents;
fc4937c3 17 int buffer_output;
6a951937 18 int all_objects;
bfd15594
JK
19 const char *format;
20};
21
39e4ae38
KN
22static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
23 int unknown_type)
e83c5163
LT
24{
25 unsigned char sha1[20];
21666f1a 26 enum object_type type;
e5fba602 27 char *buf;
e83c5163 28 unsigned long size;
e5fba602 29 struct object_context obj_context;
39e4ae38
KN
30 struct object_info oi = {NULL};
31 struct strbuf sb = STRBUF_INIT;
32 unsigned flags = LOOKUP_REPLACE_OBJECT;
33
34 if (unknown_type)
35 flags |= LOOKUP_UNKNOWN_OBJECT;
2b6854c8 36
33bd598c 37 if (get_sha1_with_context(obj_name, 0, sha1, &obj_context))
2b6854c8 38 die("Not a valid object name %s", obj_name);
7950571a 39
7950571a
PA
40 buf = NULL;
41 switch (opt) {
42 case 't':
39e4ae38
KN
43 oi.typename = &sb;
44 if (sha1_object_info_extended(sha1, &oi, flags) < 0)
45 die("git cat-file: could not get object info");
46 if (sb.len) {
47 printf("%s\n", sb.buf);
48 strbuf_release(&sb);
f2a06330 49 return 0;
11e7d5c5 50 }
7950571a
PA
51 break;
52
53 case 's':
39e4ae38
KN
54 oi.sizep = &size;
55 if (sha1_object_info_extended(sha1, &oi, flags) < 0)
56 die("git cat-file: could not get object info");
57 printf("%lu\n", size);
58 return 0;
7950571a
PA
59
60 case 'e':
61 return !has_sha1_file(sha1);
62
3ac21617
MG
63 case 'c':
64 if (!obj_context.path[0])
65 die("git cat-file --textconv %s: <object> must be <sha1:path>",
66 obj_name);
67
68 if (textconv_object(obj_context.path, obj_context.mode, sha1, 1, &buf, &size))
69 break;
70
a0f15fa5 71 case 'p':
21666f1a
NP
72 type = sha1_object_info(sha1, NULL);
73 if (type < 0)
2b6854c8 74 die("Not a valid object name %s", obj_name);
a0f15fa5
JH
75
76 /* custom pretty-print here */
2b6854c8 77 if (type == OBJ_TREE) {
66dbfd55
GV
78 const char *ls_args[3] = { NULL };
79 ls_args[0] = "ls-tree";
80 ls_args[1] = obj_name;
2b6854c8
SP
81 return cmd_ls_tree(2, ls_args, NULL);
82 }
a0f15fa5 83
00c8fd49
NTND
84 if (type == OBJ_BLOB)
85 return stream_blob_to_fd(1, sha1, NULL, 0);
21666f1a 86 buf = read_sha1_file(sha1, &type, &size);
a0f15fa5 87 if (!buf)
2b6854c8 88 die("Cannot read object %s", obj_name);
a0f15fa5
JH
89
90 /* otherwise just spit out the data */
91 break;
e5fba602 92
7950571a 93 case 0:
00c8fd49
NTND
94 if (type_from_string(exp_type) == OBJ_BLOB) {
95 unsigned char blob_sha1[20];
96 if (sha1_object_info(sha1, NULL) == OBJ_TAG) {
00c8fd49 97 char *buffer = read_sha1_file(sha1, &type, &size);
e3f1da98
RS
98 const char *target;
99 if (!skip_prefix(buffer, "object ", &target) ||
100 get_sha1_hex(target, blob_sha1))
00c8fd49
NTND
101 die("%s not a valid tag", sha1_to_hex(sha1));
102 free(buffer);
103 } else
104 hashcpy(blob_sha1, sha1);
105
106 if (sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
107 return stream_blob_to_fd(1, blob_sha1, NULL, 0);
108 /*
109 * we attempted to dereference a tag to a blob
110 * and failed; there may be new dereference
111 * mechanisms this code is not aware of.
112 * fall-back to the usual case.
113 */
114 }
2b6854c8 115 buf = read_object_with_reference(sha1, exp_type, &size, NULL);
7950571a
PA
116 break;
117
118 default:
d7530708 119 die("git cat-file: unknown option: %s", exp_type);
bf0c6e83
LT
120 }
121
11e7d5c5 122 if (!buf)
34baebce 123 die("git cat-file %s: bad file", obj_name);
11e7d5c5 124
7230e6d0 125 write_or_die(1, buf, size);
bf0c6e83 126 return 0;
e83c5163 127}
9cf71b17 128
93d2a607
JK
129struct expand_data {
130 unsigned char sha1[20];
131 enum object_type type;
132 unsigned long size;
a4ac1061 133 unsigned long disk_size;
97be0407 134 const char *rest;
65ea9c3c 135 unsigned char delta_base_sha1[20];
93d2a607
JK
136
137 /*
138 * If mark_query is true, we do not expand anything, but rather
139 * just mark the object_info with items we wish to query.
140 */
141 int mark_query;
142
97be0407
JK
143 /*
144 * Whether to split the input on whitespace before feeding it to
145 * get_sha1; this is decided during the mark_query phase based on
146 * whether we have a %(rest) token in our format.
147 */
148 int split_on_whitespace;
149
93d2a607
JK
150 /*
151 * After a mark_query run, this object_info is set up to be
152 * passed to sha1_object_info_extended. It will point to the data
153 * elements above, so you can retrieve the response from there.
154 */
155 struct object_info info;
156};
157
158static int is_atom(const char *atom, const char *s, int slen)
159{
160 int alen = strlen(atom);
161 return alen == slen && !memcmp(atom, s, alen);
162}
163
164static void expand_atom(struct strbuf *sb, const char *atom, int len,
165 void *vdata)
166{
167 struct expand_data *data = vdata;
168
169 if (is_atom("objectname", atom, len)) {
170 if (!data->mark_query)
171 strbuf_addstr(sb, sha1_to_hex(data->sha1));
172 } else if (is_atom("objecttype", atom, len)) {
5b086407
JK
173 if (data->mark_query)
174 data->info.typep = &data->type;
175 else
93d2a607
JK
176 strbuf_addstr(sb, typename(data->type));
177 } else if (is_atom("objectsize", atom, len)) {
178 if (data->mark_query)
179 data->info.sizep = &data->size;
180 else
181 strbuf_addf(sb, "%lu", data->size);
a4ac1061
JK
182 } else if (is_atom("objectsize:disk", atom, len)) {
183 if (data->mark_query)
184 data->info.disk_sizep = &data->disk_size;
185 else
186 strbuf_addf(sb, "%lu", data->disk_size);
97be0407
JK
187 } else if (is_atom("rest", atom, len)) {
188 if (data->mark_query)
189 data->split_on_whitespace = 1;
190 else if (data->rest)
191 strbuf_addstr(sb, data->rest);
65ea9c3c
JK
192 } else if (is_atom("deltabase", atom, len)) {
193 if (data->mark_query)
194 data->info.delta_base_sha1 = data->delta_base_sha1;
195 else
196 strbuf_addstr(sb, sha1_to_hex(data->delta_base_sha1));
93d2a607
JK
197 } else
198 die("unknown format element: %.*s", len, atom);
199}
200
201static size_t expand_format(struct strbuf *sb, const char *start, void *data)
202{
203 const char *end;
204
205 if (*start != '(')
206 return 0;
207 end = strchr(start + 1, ')');
208 if (!end)
209 die("format element '%s' does not end in ')'", start);
210
211 expand_atom(sb, start + 1, end - start - 1, data);
212
213 return end - start + 1;
214}
215
fc4937c3
JK
216static void batch_write(struct batch_options *opt, const void *data, int len)
217{
218 if (opt->buffer_output) {
219 if (fwrite(data, 1, len, stdout) != len)
220 die_errno("unable to write to stdout");
221 } else
222 write_or_die(1, data, len);
223}
224
225static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
98e2092b 226{
370c9268
JK
227 const unsigned char *sha1 = data->sha1;
228
6554dfa9
JK
229 assert(data->info.typep);
230
370c9268 231 if (data->type == OBJ_BLOB) {
fc4937c3
JK
232 if (opt->buffer_output)
233 fflush(stdout);
234 if (stream_blob_to_fd(1, sha1, NULL, 0) < 0)
98e2092b
JK
235 die("unable to stream %s to stdout", sha1_to_hex(sha1));
236 }
237 else {
370c9268
JK
238 enum object_type type;
239 unsigned long size;
98e2092b
JK
240 void *contents;
241
370c9268 242 contents = read_sha1_file(sha1, &type, &size);
98e2092b
JK
243 if (!contents)
244 die("object %s disappeared", sha1_to_hex(sha1));
370c9268 245 if (type != data->type)
98e2092b 246 die("object %s changed type!?", sha1_to_hex(sha1));
6554dfa9 247 if (data->info.sizep && size != data->size)
370c9268 248 die("object %s changed size!?", sha1_to_hex(sha1));
98e2092b 249
fc4937c3 250 batch_write(opt, contents, size);
98e2092b
JK
251 free(contents);
252 }
253}
254
44b877e9
JK
255static void batch_object_write(const char *obj_name, struct batch_options *opt,
256 struct expand_data *data)
257{
258 struct strbuf buf = STRBUF_INIT;
259
260 if (sha1_object_info_extended(data->sha1, &data->info, LOOKUP_REPLACE_OBJECT) < 0) {
6a951937 261 printf("%s missing\n", obj_name ? obj_name : sha1_to_hex(data->sha1));
44b877e9
JK
262 fflush(stdout);
263 return;
264 }
265
266 strbuf_expand(&buf, opt->format, expand_format, data);
267 strbuf_addch(&buf, '\n');
268 batch_write(opt, buf.buf, buf.len);
269 strbuf_release(&buf);
270
271 if (opt->print_contents) {
272 print_object_or_die(opt, data);
273 batch_write(opt, "\n", 1);
274 }
275}
276
82330950
JK
277static void batch_one_object(const char *obj_name, struct batch_options *opt,
278 struct expand_data *data)
05d5667f 279{
122d5346
DT
280 struct object_context ctx;
281 int flags = opt->follow_symlinks ? GET_SHA1_FOLLOW_SYMLINKS : 0;
282 enum follow_symlinks_result result;
05d5667f 283
122d5346
DT
284 result = get_sha1_with_context(obj_name, flags, data->sha1, &ctx);
285 if (result != FOUND) {
286 switch (result) {
287 case MISSING_OBJECT:
288 printf("%s missing\n", obj_name);
289 break;
290 case DANGLING_SYMLINK:
291 printf("dangling %"PRIuMAX"\n%s\n",
292 (uintmax_t)strlen(obj_name), obj_name);
293 break;
294 case SYMLINK_LOOP:
295 printf("loop %"PRIuMAX"\n%s\n",
296 (uintmax_t)strlen(obj_name), obj_name);
297 break;
298 case NOT_DIR:
299 printf("notdir %"PRIuMAX"\n%s\n",
300 (uintmax_t)strlen(obj_name), obj_name);
301 break;
302 default:
303 die("BUG: unknown get_sha1_with_context result %d\n",
304 result);
305 break;
306 }
307 fflush(stdout);
82330950 308 return;
122d5346
DT
309 }
310
311 if (ctx.mode == 0) {
312 printf("symlink %"PRIuMAX"\n%s\n",
313 (uintmax_t)ctx.symlink_path.len,
314 ctx.symlink_path.buf);
422b2063 315 fflush(stdout);
82330950 316 return;
05d5667f
AR
317 }
318
44b877e9 319 batch_object_write(obj_name, opt, data);
05d5667f
AR
320}
321
6a951937
JK
322struct object_cb_data {
323 struct batch_options *opt;
324 struct expand_data *expand;
325};
326
327static int batch_object_cb(const unsigned char *sha1,
328 struct object_cb_data *data)
329{
330 hashcpy(data->expand->sha1, sha1);
331 batch_object_write(NULL, data->opt, data->expand);
332 return 0;
333}
334
335static int batch_loose_object(const unsigned char *sha1,
336 const char *path,
337 void *data)
338{
339 return batch_object_cb(sha1, data);
340}
341
342static int batch_packed_object(const unsigned char *sha1,
343 struct packed_git *pack,
344 uint32_t pos,
345 void *data)
346{
347 return batch_object_cb(sha1, data);
348}
349
b71bd480 350static int batch_objects(struct batch_options *opt)
05d5667f 351{
f285a2d7 352 struct strbuf buf = STRBUF_INIT;
93d2a607 353 struct expand_data data;
a42fcd15 354 int save_warning;
07e23839 355 int retval = 0;
93d2a607
JK
356
357 if (!opt->format)
358 opt->format = "%(objectname) %(objecttype) %(objectsize)";
359
360 /*
361 * Expand once with our special mark_query flag, which will prime the
362 * object_info to be handed to sha1_object_info_extended for each
363 * object.
364 */
365 memset(&data, 0, sizeof(data));
366 data.mark_query = 1;
367 strbuf_expand(&buf, opt->format, expand_format, &data);
368 data.mark_query = 0;
05d5667f 369
6554dfa9
JK
370 /*
371 * If we are printing out the object, then always fill in the type,
372 * since we will want to decide whether or not to stream.
373 */
374 if (opt->print_contents)
375 data.info.typep = &data.type;
376
6a951937
JK
377 if (opt->all_objects) {
378 struct object_cb_data cb;
379 cb.opt = opt;
380 cb.expand = &data;
381 for_each_loose_object(batch_loose_object, &cb, 0);
382 for_each_packed_object(batch_packed_object, &cb, 0);
383 return 0;
384 }
385
25fba78d
JK
386 /*
387 * We are going to call get_sha1 on a potentially very large number of
388 * objects. In most large cases, these will be actual object sha1s. The
389 * cost to double-check that each one is not also a ref (just so we can
390 * warn) ends up dwarfing the actual cost of the object lookups
391 * themselves. We can work around it by just turning off the warning.
392 */
a42fcd15 393 save_warning = warn_on_object_refname_ambiguity;
25fba78d
JK
394 warn_on_object_refname_ambiguity = 0;
395
05d5667f 396 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
97be0407
JK
397 if (data.split_on_whitespace) {
398 /*
399 * Split at first whitespace, tying off the beginning
400 * of the string and saving the remainder (or NULL) in
401 * data.rest.
402 */
403 char *p = strpbrk(buf.buf, " \t");
404 if (p) {
405 while (*p && strchr(" \t", *p))
406 *p++ = '\0';
407 }
408 data.rest = p;
409 }
410
82330950 411 batch_one_object(buf.buf, opt, &data);
05d5667f
AR
412 }
413
648027c4 414 strbuf_release(&buf);
a42fcd15 415 warn_on_object_refname_ambiguity = save_warning;
07e23839 416 return retval;
05d5667f
AR
417}
418
15d8e565 419static const char * const cat_file_usage[] = {
39e4ae38 420 N_("git cat-file (-t [--allow-unknown-type]|-s [--allow-unknown-type]|-e|-p|<type>|--textconv) <object>"),
122d5346 421 N_("git cat-file (--batch | --batch-check) [--follow-symlinks] < <list-of-objects>"),
15d8e565
MB
422 NULL
423};
4814dbe8 424
e5fba602
CP
425static int git_cat_file_config(const char *var, const char *value, void *cb)
426{
6680a087 427 if (userdiff_config(var, value) < 0)
e5fba602 428 return -1;
e5fba602
CP
429
430 return git_default_config(var, value, cb);
431}
432
b71bd480
JK
433static int batch_option_callback(const struct option *opt,
434 const char *arg,
435 int unset)
436{
437 struct batch_options *bo = opt->value;
438
122d5346
DT
439 if (bo->enabled) {
440 return 1;
b71bd480
JK
441 }
442
443 bo->enabled = 1;
444 bo->print_contents = !strcmp(opt->long_name, "batch");
93d2a607 445 bo->format = arg;
b71bd480
JK
446
447 return 0;
448}
449
9cf71b17
AR
450int cmd_cat_file(int argc, const char **argv, const char *prefix)
451{
b71bd480 452 int opt = 0;
4814dbe8 453 const char *exp_type = NULL, *obj_name = NULL;
b71bd480 454 struct batch_options batch = {0};
39e4ae38 455 int unknown_type = 0;
9cf71b17 456
15d8e565 457 const struct option options[] = {
d68faec7 458 OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")),
b48158ac
KN
459 OPT_CMDMODE('t', NULL, &opt, N_("show object type"), 't'),
460 OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
461 OPT_CMDMODE('e', NULL, &opt,
d68faec7 462 N_("exit with zero when there's no error"), 'e'),
b48158ac
KN
463 OPT_CMDMODE('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
464 OPT_CMDMODE(0, "textconv", &opt,
d68faec7 465 N_("for blob objects, run textconv on object's content"), 'c'),
ad42f28d 466 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
39e4ae38 467 N_("allow -s and -t to work with broken/corrupt objects")),
fc4937c3 468 OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
93d2a607 469 { OPTION_CALLBACK, 0, "batch", &batch, "format",
b71bd480 470 N_("show info and content of objects fed from the standard input"),
93d2a607
JK
471 PARSE_OPT_OPTARG, batch_option_callback },
472 { OPTION_CALLBACK, 0, "batch-check", &batch, "format",
b71bd480 473 N_("show info about objects fed from the standard input"),
93d2a607 474 PARSE_OPT_OPTARG, batch_option_callback },
122d5346
DT
475 OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
476 N_("follow in-tree symlinks (used with --batch or --batch-check)")),
6a951937
JK
477 OPT_BOOL(0, "batch-all-objects", &batch.all_objects,
478 N_("show all objects with --batch or --batch-check")),
15d8e565
MB
479 OPT_END()
480 };
a8128ed6 481
e5fba602 482 git_config(git_cat_file_config, NULL);
4814dbe8 483
37782920 484 argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);
05d5667f 485
15d8e565
MB
486 if (opt) {
487 if (argc == 1)
488 obj_name = argv[0];
489 else
490 usage_with_options(cat_file_usage, options);
491 }
b71bd480 492 if (!opt && !batch.enabled) {
15d8e565
MB
493 if (argc == 2) {
494 exp_type = argv[0];
495 obj_name = argv[1];
496 } else
497 usage_with_options(cat_file_usage, options);
498 }
b71bd480 499 if (batch.enabled && (opt || argc)) {
15d8e565 500 usage_with_options(cat_file_usage, options);
9cf71b17
AR
501 }
502
6a951937 503 if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) {
122d5346
DT
504 usage_with_options(cat_file_usage, options);
505 }
506
b71bd480
JK
507 if (batch.enabled)
508 return batch_objects(&batch);
05d5667f 509
39e4ae38
KN
510 if (unknown_type && opt != 't' && opt != 's')
511 die("git cat-file --allow-unknown-type: use with -s or -t");
512 return cat_one_file(opt, exp_type, obj_name, unknown_type);
9cf71b17 513}