grep: remove redundant REG_NEWLINE when compiling fixed regex
[git.git] / builtin / cat-file.c
blob7efbc4019ac59a16ae4f147889e6f489dac1e661
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "config.h"
8 #include "builtin.h"
9 #include "diff.h"
10 #include "parse-options.h"
11 #include "userdiff.h"
12 #include "streaming.h"
13 #include "tree-walk.h"
14 #include "sha1-array.h"
16 struct batch_options {
17 int enabled;
18 int follow_symlinks;
19 int print_contents;
20 int buffer_output;
21 int all_objects;
22 int cmdmode; /* may be 'w' or 'c' for --filters or --textconv */
23 const char *format;
26 static const char *force_path;
28 static int filter_object(const char *path, unsigned mode,
29 const struct object_id *oid,
30 char **buf, unsigned long *size)
32 enum object_type type;
34 *buf = read_sha1_file(oid->hash, &type, size);
35 if (!*buf)
36 return error(_("cannot read object %s '%s'"),
37 oid_to_hex(oid), path);
38 if ((type == OBJ_BLOB) && S_ISREG(mode)) {
39 struct strbuf strbuf = STRBUF_INIT;
40 if (convert_to_working_tree(path, *buf, *size, &strbuf)) {
41 free(*buf);
42 *size = strbuf.len;
43 *buf = strbuf_detach(&strbuf, NULL);
47 return 0;
50 static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
51 int unknown_type)
53 struct object_id oid;
54 enum object_type type;
55 char *buf;
56 unsigned long size;
57 struct object_context obj_context;
58 struct object_info oi = OBJECT_INFO_INIT;
59 struct strbuf sb = STRBUF_INIT;
60 unsigned flags = LOOKUP_REPLACE_OBJECT;
61 const char *path = force_path;
63 if (unknown_type)
64 flags |= LOOKUP_UNKNOWN_OBJECT;
66 if (get_sha1_with_context(obj_name, GET_SHA1_RECORD_PATH,
67 oid.hash, &obj_context))
68 die("Not a valid object name %s", obj_name);
70 if (!path)
71 path = obj_context.path;
72 if (obj_context.mode == S_IFINVALID)
73 obj_context.mode = 0100644;
75 buf = NULL;
76 switch (opt) {
77 case 't':
78 oi.typename = &sb;
79 if (sha1_object_info_extended(oid.hash, &oi, flags) < 0)
80 die("git cat-file: could not get object info");
81 if (sb.len) {
82 printf("%s\n", sb.buf);
83 strbuf_release(&sb);
84 return 0;
86 break;
88 case 's':
89 oi.sizep = &size;
90 if (sha1_object_info_extended(oid.hash, &oi, flags) < 0)
91 die("git cat-file: could not get object info");
92 printf("%lu\n", size);
93 return 0;
95 case 'e':
96 return !has_object_file(&oid);
98 case 'w':
99 if (!path[0])
100 die("git cat-file --filters %s: <object> must be "
101 "<sha1:path>", obj_name);
103 if (filter_object(path, obj_context.mode,
104 &oid, &buf, &size))
105 return -1;
106 break;
108 case 'c':
109 if (!path[0])
110 die("git cat-file --textconv %s: <object> must be <sha1:path>",
111 obj_name);
113 if (textconv_object(path, obj_context.mode, &oid, 1, &buf, &size))
114 break;
116 case 'p':
117 type = sha1_object_info(oid.hash, NULL);
118 if (type < 0)
119 die("Not a valid object name %s", obj_name);
121 /* custom pretty-print here */
122 if (type == OBJ_TREE) {
123 const char *ls_args[3] = { NULL };
124 ls_args[0] = "ls-tree";
125 ls_args[1] = obj_name;
126 return cmd_ls_tree(2, ls_args, NULL);
129 if (type == OBJ_BLOB)
130 return stream_blob_to_fd(1, &oid, NULL, 0);
131 buf = read_sha1_file(oid.hash, &type, &size);
132 if (!buf)
133 die("Cannot read object %s", obj_name);
135 /* otherwise just spit out the data */
136 break;
138 case 0:
139 if (type_from_string(exp_type) == OBJ_BLOB) {
140 struct object_id blob_oid;
141 if (sha1_object_info(oid.hash, NULL) == OBJ_TAG) {
142 char *buffer = read_sha1_file(oid.hash, &type, &size);
143 const char *target;
144 if (!skip_prefix(buffer, "object ", &target) ||
145 get_oid_hex(target, &blob_oid))
146 die("%s not a valid tag", oid_to_hex(&oid));
147 free(buffer);
148 } else
149 oidcpy(&blob_oid, &oid);
151 if (sha1_object_info(blob_oid.hash, NULL) == OBJ_BLOB)
152 return stream_blob_to_fd(1, &blob_oid, NULL, 0);
154 * we attempted to dereference a tag to a blob
155 * and failed; there may be new dereference
156 * mechanisms this code is not aware of.
157 * fall-back to the usual case.
160 buf = read_object_with_reference(oid.hash, exp_type, &size, NULL);
161 break;
163 default:
164 die("git cat-file: unknown option: %s", exp_type);
167 if (!buf)
168 die("git cat-file %s: bad file", obj_name);
170 write_or_die(1, buf, size);
171 free(buf);
172 free(obj_context.path);
173 return 0;
176 struct expand_data {
177 struct object_id oid;
178 enum object_type type;
179 unsigned long size;
180 off_t disk_size;
181 const char *rest;
182 struct object_id delta_base_oid;
185 * If mark_query is true, we do not expand anything, but rather
186 * just mark the object_info with items we wish to query.
188 int mark_query;
191 * Whether to split the input on whitespace before feeding it to
192 * get_sha1; this is decided during the mark_query phase based on
193 * whether we have a %(rest) token in our format.
195 int split_on_whitespace;
198 * After a mark_query run, this object_info is set up to be
199 * passed to sha1_object_info_extended. It will point to the data
200 * elements above, so you can retrieve the response from there.
202 struct object_info info;
205 * This flag will be true if the requested batch format and options
206 * don't require us to call sha1_object_info, which can then be
207 * optimized out.
209 unsigned skip_object_info : 1;
212 static int is_atom(const char *atom, const char *s, int slen)
214 int alen = strlen(atom);
215 return alen == slen && !memcmp(atom, s, alen);
218 static void expand_atom(struct strbuf *sb, const char *atom, int len,
219 void *vdata)
221 struct expand_data *data = vdata;
223 if (is_atom("objectname", atom, len)) {
224 if (!data->mark_query)
225 strbuf_addstr(sb, oid_to_hex(&data->oid));
226 } else if (is_atom("objecttype", atom, len)) {
227 if (data->mark_query)
228 data->info.typep = &data->type;
229 else
230 strbuf_addstr(sb, typename(data->type));
231 } else if (is_atom("objectsize", atom, len)) {
232 if (data->mark_query)
233 data->info.sizep = &data->size;
234 else
235 strbuf_addf(sb, "%lu", data->size);
236 } else if (is_atom("objectsize:disk", atom, len)) {
237 if (data->mark_query)
238 data->info.disk_sizep = &data->disk_size;
239 else
240 strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
241 } else if (is_atom("rest", atom, len)) {
242 if (data->mark_query)
243 data->split_on_whitespace = 1;
244 else if (data->rest)
245 strbuf_addstr(sb, data->rest);
246 } else if (is_atom("deltabase", atom, len)) {
247 if (data->mark_query)
248 data->info.delta_base_sha1 = data->delta_base_oid.hash;
249 else
250 strbuf_addstr(sb,
251 oid_to_hex(&data->delta_base_oid));
252 } else
253 die("unknown format element: %.*s", len, atom);
256 static size_t expand_format(struct strbuf *sb, const char *start, void *data)
258 const char *end;
260 if (*start != '(')
261 return 0;
262 end = strchr(start + 1, ')');
263 if (!end)
264 die("format element '%s' does not end in ')'", start);
266 expand_atom(sb, start + 1, end - start - 1, data);
268 return end - start + 1;
271 static void batch_write(struct batch_options *opt, const void *data, int len)
273 if (opt->buffer_output) {
274 if (fwrite(data, 1, len, stdout) != len)
275 die_errno("unable to write to stdout");
276 } else
277 write_or_die(1, data, len);
280 static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
282 const struct object_id *oid = &data->oid;
284 assert(data->info.typep);
286 if (data->type == OBJ_BLOB) {
287 if (opt->buffer_output)
288 fflush(stdout);
289 if (opt->cmdmode) {
290 char *contents;
291 unsigned long size;
293 if (!data->rest)
294 die("missing path for '%s'", oid_to_hex(oid));
296 if (opt->cmdmode == 'w') {
297 if (filter_object(data->rest, 0100644, oid,
298 &contents, &size))
299 die("could not convert '%s' %s",
300 oid_to_hex(oid), data->rest);
301 } else if (opt->cmdmode == 'c') {
302 enum object_type type;
303 if (!textconv_object(data->rest, 0100644, oid,
304 1, &contents, &size))
305 contents = read_sha1_file(oid->hash, &type,
306 &size);
307 if (!contents)
308 die("could not convert '%s' %s",
309 oid_to_hex(oid), data->rest);
310 } else
311 die("BUG: invalid cmdmode: %c", opt->cmdmode);
312 batch_write(opt, contents, size);
313 free(contents);
314 } else if (stream_blob_to_fd(1, oid, NULL, 0) < 0)
315 die("unable to stream %s to stdout", oid_to_hex(oid));
317 else {
318 enum object_type type;
319 unsigned long size;
320 void *contents;
322 contents = read_sha1_file(oid->hash, &type, &size);
323 if (!contents)
324 die("object %s disappeared", oid_to_hex(oid));
325 if (type != data->type)
326 die("object %s changed type!?", oid_to_hex(oid));
327 if (data->info.sizep && size != data->size)
328 die("object %s changed size!?", oid_to_hex(oid));
330 batch_write(opt, contents, size);
331 free(contents);
335 static void batch_object_write(const char *obj_name, struct batch_options *opt,
336 struct expand_data *data)
338 struct strbuf buf = STRBUF_INIT;
340 if (!data->skip_object_info &&
341 sha1_object_info_extended(data->oid.hash, &data->info, LOOKUP_REPLACE_OBJECT) < 0) {
342 printf("%s missing\n",
343 obj_name ? obj_name : oid_to_hex(&data->oid));
344 fflush(stdout);
345 return;
348 strbuf_expand(&buf, opt->format, expand_format, data);
349 strbuf_addch(&buf, '\n');
350 batch_write(opt, buf.buf, buf.len);
351 strbuf_release(&buf);
353 if (opt->print_contents) {
354 print_object_or_die(opt, data);
355 batch_write(opt, "\n", 1);
359 static void batch_one_object(const char *obj_name, struct batch_options *opt,
360 struct expand_data *data)
362 struct object_context ctx;
363 int flags = opt->follow_symlinks ? GET_SHA1_FOLLOW_SYMLINKS : 0;
364 enum follow_symlinks_result result;
366 result = get_sha1_with_context(obj_name, flags, data->oid.hash, &ctx);
367 if (result != FOUND) {
368 switch (result) {
369 case MISSING_OBJECT:
370 printf("%s missing\n", obj_name);
371 break;
372 case DANGLING_SYMLINK:
373 printf("dangling %"PRIuMAX"\n%s\n",
374 (uintmax_t)strlen(obj_name), obj_name);
375 break;
376 case SYMLINK_LOOP:
377 printf("loop %"PRIuMAX"\n%s\n",
378 (uintmax_t)strlen(obj_name), obj_name);
379 break;
380 case NOT_DIR:
381 printf("notdir %"PRIuMAX"\n%s\n",
382 (uintmax_t)strlen(obj_name), obj_name);
383 break;
384 default:
385 die("BUG: unknown get_sha1_with_context result %d\n",
386 result);
387 break;
389 fflush(stdout);
390 return;
393 if (ctx.mode == 0) {
394 printf("symlink %"PRIuMAX"\n%s\n",
395 (uintmax_t)ctx.symlink_path.len,
396 ctx.symlink_path.buf);
397 fflush(stdout);
398 return;
401 batch_object_write(obj_name, opt, data);
404 struct object_cb_data {
405 struct batch_options *opt;
406 struct expand_data *expand;
409 static int batch_object_cb(const struct object_id *oid, void *vdata)
411 struct object_cb_data *data = vdata;
412 oidcpy(&data->expand->oid, oid);
413 batch_object_write(NULL, data->opt, data->expand);
414 return 0;
417 static int batch_loose_object(const struct object_id *oid,
418 const char *path,
419 void *data)
421 oid_array_append(data, oid);
422 return 0;
425 static int batch_packed_object(const struct object_id *oid,
426 struct packed_git *pack,
427 uint32_t pos,
428 void *data)
430 oid_array_append(data, oid);
431 return 0;
434 static int batch_objects(struct batch_options *opt)
436 struct strbuf buf = STRBUF_INIT;
437 struct expand_data data;
438 int save_warning;
439 int retval = 0;
441 if (!opt->format)
442 opt->format = "%(objectname) %(objecttype) %(objectsize)";
445 * Expand once with our special mark_query flag, which will prime the
446 * object_info to be handed to sha1_object_info_extended for each
447 * object.
449 memset(&data, 0, sizeof(data));
450 data.mark_query = 1;
451 strbuf_expand(&buf, opt->format, expand_format, &data);
452 data.mark_query = 0;
453 if (opt->cmdmode)
454 data.split_on_whitespace = 1;
456 if (opt->all_objects) {
457 struct object_info empty = OBJECT_INFO_INIT;
458 if (!memcmp(&data.info, &empty, sizeof(empty)))
459 data.skip_object_info = 1;
463 * If we are printing out the object, then always fill in the type,
464 * since we will want to decide whether or not to stream.
466 if (opt->print_contents)
467 data.info.typep = &data.type;
469 if (opt->all_objects) {
470 struct oid_array sa = OID_ARRAY_INIT;
471 struct object_cb_data cb;
473 for_each_loose_object(batch_loose_object, &sa, 0);
474 for_each_packed_object(batch_packed_object, &sa, 0);
476 cb.opt = opt;
477 cb.expand = &data;
478 oid_array_for_each_unique(&sa, batch_object_cb, &cb);
480 oid_array_clear(&sa);
481 return 0;
485 * We are going to call get_sha1 on a potentially very large number of
486 * objects. In most large cases, these will be actual object sha1s. The
487 * cost to double-check that each one is not also a ref (just so we can
488 * warn) ends up dwarfing the actual cost of the object lookups
489 * themselves. We can work around it by just turning off the warning.
491 save_warning = warn_on_object_refname_ambiguity;
492 warn_on_object_refname_ambiguity = 0;
494 while (strbuf_getline(&buf, stdin) != EOF) {
495 if (data.split_on_whitespace) {
497 * Split at first whitespace, tying off the beginning
498 * of the string and saving the remainder (or NULL) in
499 * data.rest.
501 char *p = strpbrk(buf.buf, " \t");
502 if (p) {
503 while (*p && strchr(" \t", *p))
504 *p++ = '\0';
506 data.rest = p;
509 batch_one_object(buf.buf, opt, &data);
512 strbuf_release(&buf);
513 warn_on_object_refname_ambiguity = save_warning;
514 return retval;
517 static const char * const cat_file_usage[] = {
518 N_("git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | <type> | --textconv | --filters) [--path=<path>] <object>"),
519 N_("git cat-file (--batch | --batch-check) [--follow-symlinks] [--textconv | --filters]"),
520 NULL
523 static int git_cat_file_config(const char *var, const char *value, void *cb)
525 if (userdiff_config(var, value) < 0)
526 return -1;
528 return git_default_config(var, value, cb);
531 static int batch_option_callback(const struct option *opt,
532 const char *arg,
533 int unset)
535 struct batch_options *bo = opt->value;
537 if (bo->enabled) {
538 return 1;
541 bo->enabled = 1;
542 bo->print_contents = !strcmp(opt->long_name, "batch");
543 bo->format = arg;
545 return 0;
548 int cmd_cat_file(int argc, const char **argv, const char *prefix)
550 int opt = 0;
551 const char *exp_type = NULL, *obj_name = NULL;
552 struct batch_options batch = {0};
553 int unknown_type = 0;
555 const struct option options[] = {
556 OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")),
557 OPT_CMDMODE('t', NULL, &opt, N_("show object type"), 't'),
558 OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
559 OPT_CMDMODE('e', NULL, &opt,
560 N_("exit with zero when there's no error"), 'e'),
561 OPT_CMDMODE('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
562 OPT_CMDMODE(0, "textconv", &opt,
563 N_("for blob objects, run textconv on object's content"), 'c'),
564 OPT_CMDMODE(0, "filters", &opt,
565 N_("for blob objects, run filters on object's content"), 'w'),
566 OPT_STRING(0, "path", &force_path, N_("blob"),
567 N_("use a specific path for --textconv/--filters")),
568 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
569 N_("allow -s and -t to work with broken/corrupt objects")),
570 OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
571 { OPTION_CALLBACK, 0, "batch", &batch, "format",
572 N_("show info and content of objects fed from the standard input"),
573 PARSE_OPT_OPTARG, batch_option_callback },
574 { OPTION_CALLBACK, 0, "batch-check", &batch, "format",
575 N_("show info about objects fed from the standard input"),
576 PARSE_OPT_OPTARG, batch_option_callback },
577 OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
578 N_("follow in-tree symlinks (used with --batch or --batch-check)")),
579 OPT_BOOL(0, "batch-all-objects", &batch.all_objects,
580 N_("show all objects with --batch or --batch-check")),
581 OPT_END()
584 git_config(git_cat_file_config, NULL);
586 batch.buffer_output = -1;
587 argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);
589 if (opt) {
590 if (batch.enabled && (opt == 'c' || opt == 'w'))
591 batch.cmdmode = opt;
592 else if (argc == 1)
593 obj_name = argv[0];
594 else
595 usage_with_options(cat_file_usage, options);
597 if (!opt && !batch.enabled) {
598 if (argc == 2) {
599 exp_type = argv[0];
600 obj_name = argv[1];
601 } else
602 usage_with_options(cat_file_usage, options);
604 if (batch.enabled) {
605 if (batch.cmdmode != opt || argc)
606 usage_with_options(cat_file_usage, options);
607 if (batch.cmdmode && batch.all_objects)
608 die("--batch-all-objects cannot be combined with "
609 "--textconv nor with --filters");
612 if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) {
613 usage_with_options(cat_file_usage, options);
616 if (force_path && opt != 'c' && opt != 'w') {
617 error("--path=<path> needs --textconv or --filters");
618 usage_with_options(cat_file_usage, options);
621 if (force_path && batch.enabled) {
622 error("--path=<path> incompatible with --batch");
623 usage_with_options(cat_file_usage, options);
626 if (batch.buffer_output < 0)
627 batch.buffer_output = batch.all_objects;
629 if (batch.enabled)
630 return batch_objects(&batch);
632 if (unknown_type && opt != 't' && opt != 's')
633 die("git cat-file --allow-unknown-type: use with -s or -t");
634 return cat_one_file(opt, exp_type, obj_name, unknown_type);