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