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