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