]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
e83c5163 | 6 | #include "cache.h" |
b2141fc1 | 7 | #include "config.h" |
f81daefe | 8 | #include "builtin.h" |
3a35cb2e | 9 | #include "diff.h" |
15d8e565 | 10 | #include "parse-options.h" |
e5fba602 | 11 | #include "userdiff.h" |
00c8fd49 | 12 | #include "streaming.h" |
122d5346 | 13 | #include "tree-walk.h" |
3115ee45 | 14 | #include "sha1-array.h" |
15d8e565 | 15 | |
bfd15594 JK |
16 | struct batch_options { |
17 | int enabled; | |
18 | int follow_symlinks; | |
19 | int print_contents; | |
fc4937c3 | 20 | int buffer_output; |
6a951937 | 21 | int all_objects; |
32145943 | 22 | int cmdmode; /* may be 'w' or 'c' for --filters or --textconv */ |
bfd15594 JK |
23 | const char *format; |
24 | }; | |
25 | ||
7bcf3414 JS |
26 | static const char *force_path; |
27 | ||
b9e62f60 | 28 | static int filter_object(const char *path, unsigned mode, |
7889ed25 | 29 | const struct object_id *oid, |
b9e62f60 JS |
30 | char **buf, unsigned long *size) |
31 | { | |
32 | enum object_type type; | |
33 | ||
7889ed25 | 34 | *buf = read_sha1_file(oid->hash, &type, size); |
b9e62f60 JS |
35 | if (!*buf) |
36 | return error(_("cannot read object %s '%s'"), | |
7889ed25 | 37 | oid_to_hex(oid), path); |
b9e62f60 JS |
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); | |
44 | } | |
45 | } | |
46 | ||
47 | return 0; | |
48 | } | |
49 | ||
39e4ae38 KN |
50 | static int cat_one_file(int opt, const char *exp_type, const char *obj_name, |
51 | int unknown_type) | |
e83c5163 | 52 | { |
63ecb99e | 53 | struct object_id oid; |
21666f1a | 54 | enum object_type type; |
e5fba602 | 55 | char *buf; |
e83c5163 | 56 | unsigned long size; |
e5fba602 | 57 | struct object_context obj_context; |
27b5c1a0 | 58 | struct object_info oi = OBJECT_INFO_INIT; |
39e4ae38 | 59 | struct strbuf sb = STRBUF_INIT; |
1f0c0d36 | 60 | unsigned flags = OBJECT_INFO_LOOKUP_REPLACE; |
7bcf3414 | 61 | const char *path = force_path; |
39e4ae38 KN |
62 | |
63 | if (unknown_type) | |
19fc5e84 | 64 | flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE; |
2b6854c8 | 65 | |
e82caf38 | 66 | if (get_oid_with_context(obj_name, GET_SHA1_RECORD_PATH, |
67 | &oid, &obj_context)) | |
2b6854c8 | 68 | die("Not a valid object name %s", obj_name); |
7950571a | 69 | |
7bcf3414 JS |
70 | if (!path) |
71 | path = obj_context.path; | |
72 | if (obj_context.mode == S_IFINVALID) | |
73 | obj_context.mode = 0100644; | |
74 | ||
7950571a PA |
75 | buf = NULL; |
76 | switch (opt) { | |
77 | case 't': | |
39e4ae38 | 78 | oi.typename = &sb; |
63ecb99e | 79 | if (sha1_object_info_extended(oid.hash, &oi, flags) < 0) |
39e4ae38 KN |
80 | die("git cat-file: could not get object info"); |
81 | if (sb.len) { | |
82 | printf("%s\n", sb.buf); | |
83 | strbuf_release(&sb); | |
f2a06330 | 84 | return 0; |
11e7d5c5 | 85 | } |
7950571a PA |
86 | break; |
87 | ||
88 | case 's': | |
39e4ae38 | 89 | oi.sizep = &size; |
63ecb99e | 90 | if (sha1_object_info_extended(oid.hash, &oi, flags) < 0) |
39e4ae38 KN |
91 | die("git cat-file: could not get object info"); |
92 | printf("%lu\n", size); | |
93 | return 0; | |
7950571a PA |
94 | |
95 | case 'e': | |
63ecb99e | 96 | return !has_object_file(&oid); |
7950571a | 97 | |
b9e62f60 | 98 | case 'w': |
7bcf3414 | 99 | if (!path[0]) |
b9e62f60 JS |
100 | die("git cat-file --filters %s: <object> must be " |
101 | "<sha1:path>", obj_name); | |
102 | ||
7bcf3414 | 103 | if (filter_object(path, obj_context.mode, |
7889ed25 | 104 | &oid, &buf, &size)) |
b9e62f60 JS |
105 | return -1; |
106 | break; | |
107 | ||
3ac21617 | 108 | case 'c': |
7bcf3414 | 109 | if (!path[0]) |
3ac21617 MG |
110 | die("git cat-file --textconv %s: <object> must be <sha1:path>", |
111 | obj_name); | |
112 | ||
7889ed25 | 113 | if (textconv_object(path, obj_context.mode, &oid, 1, &buf, &size)) |
3ac21617 MG |
114 | break; |
115 | ||
a0f15fa5 | 116 | case 'p': |
63ecb99e | 117 | type = sha1_object_info(oid.hash, NULL); |
21666f1a | 118 | if (type < 0) |
2b6854c8 | 119 | die("Not a valid object name %s", obj_name); |
a0f15fa5 JH |
120 | |
121 | /* custom pretty-print here */ | |
2b6854c8 | 122 | if (type == OBJ_TREE) { |
66dbfd55 GV |
123 | const char *ls_args[3] = { NULL }; |
124 | ls_args[0] = "ls-tree"; | |
125 | ls_args[1] = obj_name; | |
2b6854c8 SP |
126 | return cmd_ls_tree(2, ls_args, NULL); |
127 | } | |
a0f15fa5 | 128 | |
00c8fd49 | 129 | if (type == OBJ_BLOB) |
7eda0e4f | 130 | return stream_blob_to_fd(1, &oid, NULL, 0); |
63ecb99e | 131 | buf = read_sha1_file(oid.hash, &type, &size); |
a0f15fa5 | 132 | if (!buf) |
2b6854c8 | 133 | die("Cannot read object %s", obj_name); |
a0f15fa5 JH |
134 | |
135 | /* otherwise just spit out the data */ | |
136 | break; | |
e5fba602 | 137 | |
7950571a | 138 | case 0: |
00c8fd49 | 139 | if (type_from_string(exp_type) == OBJ_BLOB) { |
63ecb99e | 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); | |
e3f1da98 RS |
143 | const char *target; |
144 | if (!skip_prefix(buffer, "object ", &target) || | |
63ecb99e | 145 | get_oid_hex(target, &blob_oid)) |
146 | die("%s not a valid tag", oid_to_hex(&oid)); | |
00c8fd49 NTND |
147 | free(buffer); |
148 | } else | |
63ecb99e | 149 | oidcpy(&blob_oid, &oid); |
00c8fd49 | 150 | |
63ecb99e | 151 | if (sha1_object_info(blob_oid.hash, NULL) == OBJ_BLOB) |
7eda0e4f | 152 | return stream_blob_to_fd(1, &blob_oid, NULL, 0); |
00c8fd49 NTND |
153 | /* |
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. | |
158 | */ | |
159 | } | |
63ecb99e | 160 | buf = read_object_with_reference(oid.hash, exp_type, &size, NULL); |
7950571a PA |
161 | break; |
162 | ||
163 | default: | |
d7530708 | 164 | die("git cat-file: unknown option: %s", exp_type); |
bf0c6e83 LT |
165 | } |
166 | ||
11e7d5c5 | 167 | if (!buf) |
34baebce | 168 | die("git cat-file %s: bad file", obj_name); |
11e7d5c5 | 169 | |
7230e6d0 | 170 | write_or_die(1, buf, size); |
05c2b7ba | 171 | free(buf); |
dc944b65 | 172 | free(obj_context.path); |
bf0c6e83 | 173 | return 0; |
e83c5163 | 174 | } |
9cf71b17 | 175 | |
93d2a607 | 176 | struct expand_data { |
cd4f77be | 177 | struct object_id oid; |
93d2a607 JK |
178 | enum object_type type; |
179 | unsigned long size; | |
166df26f | 180 | off_t disk_size; |
97be0407 | 181 | const char *rest; |
cd4f77be | 182 | struct object_id delta_base_oid; |
93d2a607 JK |
183 | |
184 | /* | |
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. | |
187 | */ | |
188 | int mark_query; | |
189 | ||
97be0407 JK |
190 | /* |
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. | |
194 | */ | |
195 | int split_on_whitespace; | |
196 | ||
93d2a607 JK |
197 | /* |
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. | |
201 | */ | |
202 | struct object_info info; | |
845de33a JK |
203 | |
204 | /* | |
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. | |
208 | */ | |
209 | unsigned skip_object_info : 1; | |
93d2a607 JK |
210 | }; |
211 | ||
212 | static int is_atom(const char *atom, const char *s, int slen) | |
213 | { | |
214 | int alen = strlen(atom); | |
215 | return alen == slen && !memcmp(atom, s, alen); | |
216 | } | |
217 | ||
218 | static void expand_atom(struct strbuf *sb, const char *atom, int len, | |
219 | void *vdata) | |
220 | { | |
221 | struct expand_data *data = vdata; | |
222 | ||
223 | if (is_atom("objectname", atom, len)) { | |
224 | if (!data->mark_query) | |
cd4f77be | 225 | strbuf_addstr(sb, oid_to_hex(&data->oid)); |
93d2a607 | 226 | } else if (is_atom("objecttype", atom, len)) { |
5b086407 JK |
227 | if (data->mark_query) |
228 | data->info.typep = &data->type; | |
229 | else | |
93d2a607 JK |
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); | |
a4ac1061 JK |
236 | } else if (is_atom("objectsize:disk", atom, len)) { |
237 | if (data->mark_query) | |
238 | data->info.disk_sizep = &data->disk_size; | |
239 | else | |
166df26f | 240 | strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size); |
97be0407 JK |
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); | |
65ea9c3c JK |
246 | } else if (is_atom("deltabase", atom, len)) { |
247 | if (data->mark_query) | |
cd4f77be | 248 | data->info.delta_base_sha1 = data->delta_base_oid.hash; |
65ea9c3c | 249 | else |
cd4f77be | 250 | strbuf_addstr(sb, |
251 | oid_to_hex(&data->delta_base_oid)); | |
93d2a607 JK |
252 | } else |
253 | die("unknown format element: %.*s", len, atom); | |
254 | } | |
255 | ||
256 | static size_t expand_format(struct strbuf *sb, const char *start, void *data) | |
257 | { | |
258 | const char *end; | |
259 | ||
260 | if (*start != '(') | |
261 | return 0; | |
262 | end = strchr(start + 1, ')'); | |
263 | if (!end) | |
264 | die("format element '%s' does not end in ')'", start); | |
265 | ||
266 | expand_atom(sb, start + 1, end - start - 1, data); | |
267 | ||
268 | return end - start + 1; | |
269 | } | |
270 | ||
fc4937c3 JK |
271 | static void batch_write(struct batch_options *opt, const void *data, int len) |
272 | { | |
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); | |
278 | } | |
279 | ||
280 | static void print_object_or_die(struct batch_options *opt, struct expand_data *data) | |
98e2092b | 281 | { |
63ecb99e | 282 | const struct object_id *oid = &data->oid; |
370c9268 | 283 | |
6554dfa9 JK |
284 | assert(data->info.typep); |
285 | ||
370c9268 | 286 | if (data->type == OBJ_BLOB) { |
fc4937c3 JK |
287 | if (opt->buffer_output) |
288 | fflush(stdout); | |
32145943 JS |
289 | if (opt->cmdmode) { |
290 | char *contents; | |
291 | unsigned long size; | |
292 | ||
293 | if (!data->rest) | |
7889ed25 | 294 | die("missing path for '%s'", oid_to_hex(oid)); |
32145943 JS |
295 | |
296 | if (opt->cmdmode == 'w') { | |
7889ed25 | 297 | if (filter_object(data->rest, 0100644, oid, |
32145943 JS |
298 | &contents, &size)) |
299 | die("could not convert '%s' %s", | |
7889ed25 | 300 | oid_to_hex(oid), data->rest); |
32145943 JS |
301 | } else if (opt->cmdmode == 'c') { |
302 | enum object_type type; | |
7889ed25 | 303 | if (!textconv_object(data->rest, 0100644, oid, |
32145943 | 304 | 1, &contents, &size)) |
7889ed25 | 305 | contents = read_sha1_file(oid->hash, &type, |
32145943 JS |
306 | &size); |
307 | if (!contents) | |
308 | die("could not convert '%s' %s", | |
7889ed25 | 309 | oid_to_hex(oid), data->rest); |
32145943 JS |
310 | } else |
311 | die("BUG: invalid cmdmode: %c", opt->cmdmode); | |
312 | batch_write(opt, contents, size); | |
313 | free(contents); | |
7889ed25 | 314 | } else if (stream_blob_to_fd(1, oid, NULL, 0) < 0) |
63ecb99e | 315 | die("unable to stream %s to stdout", oid_to_hex(oid)); |
98e2092b JK |
316 | } |
317 | else { | |
370c9268 JK |
318 | enum object_type type; |
319 | unsigned long size; | |
98e2092b JK |
320 | void *contents; |
321 | ||
63ecb99e | 322 | contents = read_sha1_file(oid->hash, &type, &size); |
98e2092b | 323 | if (!contents) |
63ecb99e | 324 | die("object %s disappeared", oid_to_hex(oid)); |
370c9268 | 325 | if (type != data->type) |
63ecb99e | 326 | die("object %s changed type!?", oid_to_hex(oid)); |
6554dfa9 | 327 | if (data->info.sizep && size != data->size) |
63ecb99e | 328 | die("object %s changed size!?", oid_to_hex(oid)); |
98e2092b | 329 | |
fc4937c3 | 330 | batch_write(opt, contents, size); |
98e2092b JK |
331 | free(contents); |
332 | } | |
333 | } | |
334 | ||
44b877e9 JK |
335 | static void batch_object_write(const char *obj_name, struct batch_options *opt, |
336 | struct expand_data *data) | |
337 | { | |
338 | struct strbuf buf = STRBUF_INIT; | |
339 | ||
845de33a | 340 | if (!data->skip_object_info && |
1f0c0d36 JT |
341 | sha1_object_info_extended(data->oid.hash, &data->info, |
342 | OBJECT_INFO_LOOKUP_REPLACE) < 0) { | |
cd4f77be | 343 | printf("%s missing\n", |
344 | obj_name ? obj_name : oid_to_hex(&data->oid)); | |
44b877e9 JK |
345 | fflush(stdout); |
346 | return; | |
347 | } | |
348 | ||
349 | strbuf_expand(&buf, opt->format, expand_format, data); | |
350 | strbuf_addch(&buf, '\n'); | |
351 | batch_write(opt, buf.buf, buf.len); | |
352 | strbuf_release(&buf); | |
353 | ||
354 | if (opt->print_contents) { | |
355 | print_object_or_die(opt, data); | |
356 | batch_write(opt, "\n", 1); | |
357 | } | |
358 | } | |
359 | ||
82330950 JK |
360 | static void batch_one_object(const char *obj_name, struct batch_options *opt, |
361 | struct expand_data *data) | |
05d5667f | 362 | { |
122d5346 DT |
363 | struct object_context ctx; |
364 | int flags = opt->follow_symlinks ? GET_SHA1_FOLLOW_SYMLINKS : 0; | |
365 | enum follow_symlinks_result result; | |
05d5667f | 366 | |
e82caf38 | 367 | result = get_oid_with_context(obj_name, flags, &data->oid, &ctx); |
122d5346 DT |
368 | if (result != FOUND) { |
369 | switch (result) { | |
370 | case MISSING_OBJECT: | |
371 | printf("%s missing\n", obj_name); | |
372 | break; | |
373 | case DANGLING_SYMLINK: | |
374 | printf("dangling %"PRIuMAX"\n%s\n", | |
375 | (uintmax_t)strlen(obj_name), obj_name); | |
376 | break; | |
377 | case SYMLINK_LOOP: | |
378 | printf("loop %"PRIuMAX"\n%s\n", | |
379 | (uintmax_t)strlen(obj_name), obj_name); | |
380 | break; | |
381 | case NOT_DIR: | |
382 | printf("notdir %"PRIuMAX"\n%s\n", | |
383 | (uintmax_t)strlen(obj_name), obj_name); | |
384 | break; | |
385 | default: | |
386 | die("BUG: unknown get_sha1_with_context result %d\n", | |
387 | result); | |
388 | break; | |
389 | } | |
390 | fflush(stdout); | |
82330950 | 391 | return; |
122d5346 DT |
392 | } |
393 | ||
394 | if (ctx.mode == 0) { | |
395 | printf("symlink %"PRIuMAX"\n%s\n", | |
396 | (uintmax_t)ctx.symlink_path.len, | |
397 | ctx.symlink_path.buf); | |
422b2063 | 398 | fflush(stdout); |
82330950 | 399 | return; |
05d5667f AR |
400 | } |
401 | ||
44b877e9 | 402 | batch_object_write(obj_name, opt, data); |
05d5667f AR |
403 | } |
404 | ||
6a951937 JK |
405 | struct object_cb_data { |
406 | struct batch_options *opt; | |
407 | struct expand_data *expand; | |
408 | }; | |
409 | ||
1b7ba794 | 410 | static int batch_object_cb(const struct object_id *oid, void *vdata) |
6a951937 | 411 | { |
3115ee45 | 412 | struct object_cb_data *data = vdata; |
1b7ba794 | 413 | oidcpy(&data->expand->oid, oid); |
6a951937 | 414 | batch_object_write(NULL, data->opt, data->expand); |
16ddcd40 | 415 | return 0; |
6a951937 JK |
416 | } |
417 | ||
76c1d9a0 | 418 | static int batch_loose_object(const struct object_id *oid, |
6a951937 JK |
419 | const char *path, |
420 | void *data) | |
421 | { | |
910650d2 | 422 | oid_array_append(data, oid); |
3115ee45 | 423 | return 0; |
6a951937 JK |
424 | } |
425 | ||
76c1d9a0 | 426 | static int batch_packed_object(const struct object_id *oid, |
6a951937 JK |
427 | struct packed_git *pack, |
428 | uint32_t pos, | |
429 | void *data) | |
430 | { | |
910650d2 | 431 | oid_array_append(data, oid); |
3115ee45 | 432 | return 0; |
6a951937 JK |
433 | } |
434 | ||
b71bd480 | 435 | static int batch_objects(struct batch_options *opt) |
05d5667f | 436 | { |
f285a2d7 | 437 | struct strbuf buf = STRBUF_INIT; |
93d2a607 | 438 | struct expand_data data; |
a42fcd15 | 439 | int save_warning; |
07e23839 | 440 | int retval = 0; |
93d2a607 JK |
441 | |
442 | if (!opt->format) | |
443 | opt->format = "%(objectname) %(objecttype) %(objectsize)"; | |
444 | ||
445 | /* | |
446 | * Expand once with our special mark_query flag, which will prime the | |
447 | * object_info to be handed to sha1_object_info_extended for each | |
448 | * object. | |
449 | */ | |
450 | memset(&data, 0, sizeof(data)); | |
451 | data.mark_query = 1; | |
452 | strbuf_expand(&buf, opt->format, expand_format, &data); | |
453 | data.mark_query = 0; | |
32145943 JS |
454 | if (opt->cmdmode) |
455 | data.split_on_whitespace = 1; | |
05d5667f | 456 | |
845de33a | 457 | if (opt->all_objects) { |
27b5c1a0 | 458 | struct object_info empty = OBJECT_INFO_INIT; |
845de33a JK |
459 | if (!memcmp(&data.info, &empty, sizeof(empty))) |
460 | data.skip_object_info = 1; | |
461 | } | |
462 | ||
6554dfa9 JK |
463 | /* |
464 | * If we are printing out the object, then always fill in the type, | |
465 | * since we will want to decide whether or not to stream. | |
466 | */ | |
467 | if (opt->print_contents) | |
468 | data.info.typep = &data.type; | |
469 | ||
6a951937 | 470 | if (opt->all_objects) { |
910650d2 | 471 | struct oid_array sa = OID_ARRAY_INIT; |
6a951937 | 472 | struct object_cb_data cb; |
3115ee45 JK |
473 | |
474 | for_each_loose_object(batch_loose_object, &sa, 0); | |
475 | for_each_packed_object(batch_packed_object, &sa, 0); | |
476 | ||
6a951937 JK |
477 | cb.opt = opt; |
478 | cb.expand = &data; | |
910650d2 | 479 | oid_array_for_each_unique(&sa, batch_object_cb, &cb); |
3115ee45 | 480 | |
910650d2 | 481 | oid_array_clear(&sa); |
6a951937 JK |
482 | return 0; |
483 | } | |
484 | ||
25fba78d JK |
485 | /* |
486 | * We are going to call get_sha1 on a potentially very large number of | |
487 | * objects. In most large cases, these will be actual object sha1s. The | |
488 | * cost to double-check that each one is not also a ref (just so we can | |
489 | * warn) ends up dwarfing the actual cost of the object lookups | |
490 | * themselves. We can work around it by just turning off the warning. | |
491 | */ | |
a42fcd15 | 492 | save_warning = warn_on_object_refname_ambiguity; |
25fba78d JK |
493 | warn_on_object_refname_ambiguity = 0; |
494 | ||
b42ca3dd | 495 | while (strbuf_getline(&buf, stdin) != EOF) { |
97be0407 JK |
496 | if (data.split_on_whitespace) { |
497 | /* | |
498 | * Split at first whitespace, tying off the beginning | |
499 | * of the string and saving the remainder (or NULL) in | |
500 | * data.rest. | |
501 | */ | |
502 | char *p = strpbrk(buf.buf, " \t"); | |
503 | if (p) { | |
504 | while (*p && strchr(" \t", *p)) | |
505 | *p++ = '\0'; | |
506 | } | |
507 | data.rest = p; | |
508 | } | |
509 | ||
82330950 | 510 | batch_one_object(buf.buf, opt, &data); |
05d5667f AR |
511 | } |
512 | ||
648027c4 | 513 | strbuf_release(&buf); |
a42fcd15 | 514 | warn_on_object_refname_ambiguity = save_warning; |
07e23839 | 515 | return retval; |
05d5667f AR |
516 | } |
517 | ||
15d8e565 | 518 | static const char * const cat_file_usage[] = { |
7889ed25 JH |
519 | N_("git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | <type> | --textconv | --filters) [--path=<path>] <object>"), |
520 | N_("git cat-file (--batch | --batch-check) [--follow-symlinks] [--textconv | --filters]"), | |
15d8e565 MB |
521 | NULL |
522 | }; | |
4814dbe8 | 523 | |
e5fba602 CP |
524 | static int git_cat_file_config(const char *var, const char *value, void *cb) |
525 | { | |
6680a087 | 526 | if (userdiff_config(var, value) < 0) |
e5fba602 | 527 | return -1; |
e5fba602 CP |
528 | |
529 | return git_default_config(var, value, cb); | |
530 | } | |
531 | ||
b71bd480 JK |
532 | static int batch_option_callback(const struct option *opt, |
533 | const char *arg, | |
534 | int unset) | |
535 | { | |
536 | struct batch_options *bo = opt->value; | |
537 | ||
122d5346 DT |
538 | if (bo->enabled) { |
539 | return 1; | |
b71bd480 JK |
540 | } |
541 | ||
542 | bo->enabled = 1; | |
543 | bo->print_contents = !strcmp(opt->long_name, "batch"); | |
93d2a607 | 544 | bo->format = arg; |
b71bd480 JK |
545 | |
546 | return 0; | |
547 | } | |
548 | ||
9cf71b17 AR |
549 | int cmd_cat_file(int argc, const char **argv, const char *prefix) |
550 | { | |
b71bd480 | 551 | int opt = 0; |
4814dbe8 | 552 | const char *exp_type = NULL, *obj_name = NULL; |
b71bd480 | 553 | struct batch_options batch = {0}; |
39e4ae38 | 554 | int unknown_type = 0; |
9cf71b17 | 555 | |
15d8e565 | 556 | const struct option options[] = { |
d68faec7 | 557 | OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")), |
b48158ac KN |
558 | OPT_CMDMODE('t', NULL, &opt, N_("show object type"), 't'), |
559 | OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'), | |
560 | OPT_CMDMODE('e', NULL, &opt, | |
d68faec7 | 561 | N_("exit with zero when there's no error"), 'e'), |
b48158ac KN |
562 | OPT_CMDMODE('p', NULL, &opt, N_("pretty-print object's content"), 'p'), |
563 | OPT_CMDMODE(0, "textconv", &opt, | |
d68faec7 | 564 | N_("for blob objects, run textconv on object's content"), 'c'), |
b9e62f60 JS |
565 | OPT_CMDMODE(0, "filters", &opt, |
566 | N_("for blob objects, run filters on object's content"), 'w'), | |
7bcf3414 JS |
567 | OPT_STRING(0, "path", &force_path, N_("blob"), |
568 | N_("use a specific path for --textconv/--filters")), | |
ad42f28d | 569 | OPT_BOOL(0, "allow-unknown-type", &unknown_type, |
39e4ae38 | 570 | N_("allow -s and -t to work with broken/corrupt objects")), |
fc4937c3 | 571 | OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")), |
93d2a607 | 572 | { OPTION_CALLBACK, 0, "batch", &batch, "format", |
b71bd480 | 573 | N_("show info and content of objects fed from the standard input"), |
93d2a607 JK |
574 | PARSE_OPT_OPTARG, batch_option_callback }, |
575 | { OPTION_CALLBACK, 0, "batch-check", &batch, "format", | |
b71bd480 | 576 | N_("show info about objects fed from the standard input"), |
93d2a607 | 577 | PARSE_OPT_OPTARG, batch_option_callback }, |
122d5346 DT |
578 | OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks, |
579 | N_("follow in-tree symlinks (used with --batch or --batch-check)")), | |
6a951937 JK |
580 | OPT_BOOL(0, "batch-all-objects", &batch.all_objects, |
581 | N_("show all objects with --batch or --batch-check")), | |
15d8e565 MB |
582 | OPT_END() |
583 | }; | |
a8128ed6 | 584 | |
e5fba602 | 585 | git_config(git_cat_file_config, NULL); |
4814dbe8 | 586 | |
6a36e1e7 | 587 | batch.buffer_output = -1; |
37782920 | 588 | argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0); |
05d5667f | 589 | |
15d8e565 | 590 | if (opt) { |
32145943 JS |
591 | if (batch.enabled && (opt == 'c' || opt == 'w')) |
592 | batch.cmdmode = opt; | |
593 | else if (argc == 1) | |
15d8e565 MB |
594 | obj_name = argv[0]; |
595 | else | |
596 | usage_with_options(cat_file_usage, options); | |
597 | } | |
b71bd480 | 598 | if (!opt && !batch.enabled) { |
15d8e565 MB |
599 | if (argc == 2) { |
600 | exp_type = argv[0]; | |
601 | obj_name = argv[1]; | |
602 | } else | |
603 | usage_with_options(cat_file_usage, options); | |
604 | } | |
32145943 JS |
605 | if (batch.enabled) { |
606 | if (batch.cmdmode != opt || argc) | |
607 | usage_with_options(cat_file_usage, options); | |
608 | if (batch.cmdmode && batch.all_objects) | |
609 | die("--batch-all-objects cannot be combined with " | |
610 | "--textconv nor with --filters"); | |
9cf71b17 AR |
611 | } |
612 | ||
6a951937 | 613 | if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) { |
122d5346 DT |
614 | usage_with_options(cat_file_usage, options); |
615 | } | |
616 | ||
7bcf3414 JS |
617 | if (force_path && opt != 'c' && opt != 'w') { |
618 | error("--path=<path> needs --textconv or --filters"); | |
619 | usage_with_options(cat_file_usage, options); | |
620 | } | |
621 | ||
32145943 JS |
622 | if (force_path && batch.enabled) { |
623 | error("--path=<path> incompatible with --batch"); | |
624 | usage_with_options(cat_file_usage, options); | |
625 | } | |
626 | ||
6a36e1e7 JK |
627 | if (batch.buffer_output < 0) |
628 | batch.buffer_output = batch.all_objects; | |
629 | ||
b71bd480 JK |
630 | if (batch.enabled) |
631 | return batch_objects(&batch); | |
05d5667f | 632 | |
39e4ae38 KN |
633 | if (unknown_type && opt != 't' && opt != 's') |
634 | die("git cat-file --allow-unknown-type: use with -s or -t"); | |
635 | return cat_one_file(opt, exp_type, obj_name, unknown_type); | |
9cf71b17 | 636 | } |