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