]>
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" |
fe299ec5 | 15 | #include "oid-array.h" |
7709f468 | 16 | #include "packfile.h" |
cbd53a21 | 17 | #include "object-store.h" |
b14ed5ad | 18 | #include "promisor-remote.h" |
15d8e565 | 19 | |
ac4e58ca JC |
20 | enum batch_mode { |
21 | BATCH_MODE_CONTENTS, | |
22 | BATCH_MODE_INFO, | |
440c705e | 23 | BATCH_MODE_QUEUE_AND_DISPATCH, |
ac4e58ca JC |
24 | }; |
25 | ||
bfd15594 JK |
26 | struct batch_options { |
27 | int enabled; | |
28 | int follow_symlinks; | |
ac4e58ca | 29 | enum batch_mode batch_mode; |
fc4937c3 | 30 | int buffer_output; |
6a951937 | 31 | int all_objects; |
0750bb5b | 32 | int unordered; |
a2c75526 | 33 | int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */ |
bfd15594 JK |
34 | const char *format; |
35 | }; | |
36 | ||
7bcf3414 JS |
37 | static const char *force_path; |
38 | ||
b9e62f60 | 39 | static int filter_object(const char *path, unsigned mode, |
7889ed25 | 40 | const struct object_id *oid, |
b9e62f60 JS |
41 | char **buf, unsigned long *size) |
42 | { | |
43 | enum object_type type; | |
44 | ||
b4f5aca4 | 45 | *buf = read_object_file(oid, &type, size); |
b9e62f60 JS |
46 | if (!*buf) |
47 | return error(_("cannot read object %s '%s'"), | |
7889ed25 | 48 | oid_to_hex(oid), path); |
b9e62f60 JS |
49 | if ((type == OBJ_BLOB) && S_ISREG(mode)) { |
50 | struct strbuf strbuf = STRBUF_INIT; | |
c397aac0 | 51 | struct checkout_metadata meta; |
52 | ||
53 | init_checkout_metadata(&meta, NULL, NULL, oid); | |
54 | if (convert_to_working_tree(&the_index, path, *buf, *size, &strbuf, &meta)) { | |
b9e62f60 JS |
55 | free(*buf); |
56 | *size = strbuf.len; | |
57 | *buf = strbuf_detach(&strbuf, NULL); | |
58 | } | |
59 | } | |
60 | ||
61 | return 0; | |
62 | } | |
63 | ||
98f425b4 JK |
64 | static int stream_blob(const struct object_id *oid) |
65 | { | |
66 | if (stream_blob_to_fd(1, oid, NULL, 0)) | |
67 | die("unable to stream %s to stdout", oid_to_hex(oid)); | |
68 | return 0; | |
69 | } | |
70 | ||
39e4ae38 KN |
71 | static int cat_one_file(int opt, const char *exp_type, const char *obj_name, |
72 | int unknown_type) | |
e83c5163 | 73 | { |
63ecb99e | 74 | struct object_id oid; |
21666f1a | 75 | enum object_type type; |
e5fba602 | 76 | char *buf; |
e83c5163 | 77 | unsigned long size; |
e5fba602 | 78 | struct object_context obj_context; |
27b5c1a0 | 79 | struct object_info oi = OBJECT_INFO_INIT; |
39e4ae38 | 80 | struct strbuf sb = STRBUF_INIT; |
1f0c0d36 | 81 | unsigned flags = OBJECT_INFO_LOOKUP_REPLACE; |
245b9488 | 82 | unsigned get_oid_flags = GET_OID_RECORD_PATH | GET_OID_ONLY_TO_DIE; |
7bcf3414 | 83 | const char *path = force_path; |
245b9488 ÆAB |
84 | const int opt_cw = (opt == 'c' || opt == 'w'); |
85 | if (!path && opt_cw) | |
86 | get_oid_flags |= GET_OID_REQUIRE_PATH; | |
39e4ae38 KN |
87 | |
88 | if (unknown_type) | |
19fc5e84 | 89 | flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE; |
2b6854c8 | 90 | |
245b9488 ÆAB |
91 | if (get_oid_with_context(the_repository, obj_name, get_oid_flags, &oid, |
92 | &obj_context)) | |
2b6854c8 | 93 | die("Not a valid object name %s", obj_name); |
7950571a | 94 | |
7bcf3414 JS |
95 | if (!path) |
96 | path = obj_context.path; | |
97 | if (obj_context.mode == S_IFINVALID) | |
98 | obj_context.mode = 0100644; | |
99 | ||
7950571a PA |
100 | buf = NULL; |
101 | switch (opt) { | |
102 | case 't': | |
6ca32f47 | 103 | oi.type_name = &sb; |
7ecd8690 | 104 | if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0) |
39e4ae38 KN |
105 | die("git cat-file: could not get object info"); |
106 | if (sb.len) { | |
107 | printf("%s\n", sb.buf); | |
108 | strbuf_release(&sb); | |
f2a06330 | 109 | return 0; |
11e7d5c5 | 110 | } |
7950571a PA |
111 | break; |
112 | ||
113 | case 's': | |
39e4ae38 | 114 | oi.sizep = &size; |
7ecd8690 | 115 | if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0) |
39e4ae38 | 116 | die("git cat-file: could not get object info"); |
ca473cef | 117 | printf("%"PRIuMAX"\n", (uintmax_t)size); |
39e4ae38 | 118 | return 0; |
7950571a PA |
119 | |
120 | case 'e': | |
63ecb99e | 121 | return !has_object_file(&oid); |
7950571a | 122 | |
b9e62f60 | 123 | case 'w': |
b9e62f60 | 124 | |
7bcf3414 | 125 | if (filter_object(path, obj_context.mode, |
7889ed25 | 126 | &oid, &buf, &size)) |
b9e62f60 JS |
127 | return -1; |
128 | break; | |
129 | ||
3ac21617 | 130 | case 'c': |
6afaf807 NTND |
131 | if (textconv_object(the_repository, path, obj_context.mode, |
132 | &oid, 1, &buf, &size)) | |
3ac21617 | 133 | break; |
1cf01a34 | 134 | /* else fallthrough */ |
3ac21617 | 135 | |
a0f15fa5 | 136 | case 'p': |
0df8e965 | 137 | type = oid_object_info(the_repository, &oid, NULL); |
21666f1a | 138 | if (type < 0) |
2b6854c8 | 139 | die("Not a valid object name %s", obj_name); |
a0f15fa5 JH |
140 | |
141 | /* custom pretty-print here */ | |
2b6854c8 | 142 | if (type == OBJ_TREE) { |
66dbfd55 GV |
143 | const char *ls_args[3] = { NULL }; |
144 | ls_args[0] = "ls-tree"; | |
145 | ls_args[1] = obj_name; | |
2b6854c8 SP |
146 | return cmd_ls_tree(2, ls_args, NULL); |
147 | } | |
a0f15fa5 | 148 | |
00c8fd49 | 149 | if (type == OBJ_BLOB) |
98f425b4 | 150 | return stream_blob(&oid); |
b4f5aca4 | 151 | buf = read_object_file(&oid, &type, &size); |
a0f15fa5 | 152 | if (!buf) |
2b6854c8 | 153 | die("Cannot read object %s", obj_name); |
a0f15fa5 JH |
154 | |
155 | /* otherwise just spit out the data */ | |
156 | break; | |
e5fba602 | 157 | |
7950571a | 158 | case 0: |
6aea6bae ÆAB |
159 | { |
160 | enum object_type exp_type_id = type_from_string(exp_type); | |
161 | ||
162 | if (exp_type_id == OBJ_BLOB) { | |
63ecb99e | 163 | struct object_id blob_oid; |
0df8e965 | 164 | if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) { |
b4f5aca4 | 165 | char *buffer = read_object_file(&oid, &type, |
166 | &size); | |
e3f1da98 RS |
167 | const char *target; |
168 | if (!skip_prefix(buffer, "object ", &target) || | |
63ecb99e | 169 | get_oid_hex(target, &blob_oid)) |
170 | die("%s not a valid tag", oid_to_hex(&oid)); | |
00c8fd49 NTND |
171 | free(buffer); |
172 | } else | |
63ecb99e | 173 | oidcpy(&blob_oid, &oid); |
00c8fd49 | 174 | |
0df8e965 | 175 | if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB) |
98f425b4 | 176 | return stream_blob(&blob_oid); |
00c8fd49 NTND |
177 | /* |
178 | * we attempted to dereference a tag to a blob | |
179 | * and failed; there may be new dereference | |
180 | * mechanisms this code is not aware of. | |
181 | * fall-back to the usual case. | |
182 | */ | |
183 | } | |
6aea6bae ÆAB |
184 | buf = read_object_with_reference(the_repository, &oid, |
185 | exp_type_id, &size, NULL); | |
7950571a | 186 | break; |
6aea6bae | 187 | } |
7950571a | 188 | default: |
d7530708 | 189 | die("git cat-file: unknown option: %s", exp_type); |
bf0c6e83 LT |
190 | } |
191 | ||
11e7d5c5 | 192 | if (!buf) |
34baebce | 193 | die("git cat-file %s: bad file", obj_name); |
11e7d5c5 | 194 | |
7230e6d0 | 195 | write_or_die(1, buf, size); |
05c2b7ba | 196 | free(buf); |
dc944b65 | 197 | free(obj_context.path); |
bf0c6e83 | 198 | return 0; |
e83c5163 | 199 | } |
9cf71b17 | 200 | |
93d2a607 | 201 | struct expand_data { |
cd4f77be | 202 | struct object_id oid; |
93d2a607 JK |
203 | enum object_type type; |
204 | unsigned long size; | |
166df26f | 205 | off_t disk_size; |
97be0407 | 206 | const char *rest; |
cd4f77be | 207 | struct object_id delta_base_oid; |
93d2a607 JK |
208 | |
209 | /* | |
210 | * If mark_query is true, we do not expand anything, but rather | |
211 | * just mark the object_info with items we wish to query. | |
212 | */ | |
213 | int mark_query; | |
214 | ||
97be0407 JK |
215 | /* |
216 | * Whether to split the input on whitespace before feeding it to | |
217 | * get_sha1; this is decided during the mark_query phase based on | |
218 | * whether we have a %(rest) token in our format. | |
219 | */ | |
220 | int split_on_whitespace; | |
221 | ||
93d2a607 JK |
222 | /* |
223 | * After a mark_query run, this object_info is set up to be | |
c93206b4 | 224 | * passed to oid_object_info_extended. It will point to the data |
93d2a607 JK |
225 | * elements above, so you can retrieve the response from there. |
226 | */ | |
227 | struct object_info info; | |
845de33a JK |
228 | |
229 | /* | |
230 | * This flag will be true if the requested batch format and options | |
c93206b4 | 231 | * don't require us to call oid_object_info, which can then be |
845de33a JK |
232 | * optimized out. |
233 | */ | |
234 | unsigned skip_object_info : 1; | |
93d2a607 JK |
235 | }; |
236 | ||
237 | static int is_atom(const char *atom, const char *s, int slen) | |
238 | { | |
239 | int alen = strlen(atom); | |
240 | return alen == slen && !memcmp(atom, s, alen); | |
241 | } | |
242 | ||
243 | static void expand_atom(struct strbuf *sb, const char *atom, int len, | |
244 | void *vdata) | |
245 | { | |
246 | struct expand_data *data = vdata; | |
247 | ||
248 | if (is_atom("objectname", atom, len)) { | |
249 | if (!data->mark_query) | |
cd4f77be | 250 | strbuf_addstr(sb, oid_to_hex(&data->oid)); |
93d2a607 | 251 | } else if (is_atom("objecttype", atom, len)) { |
5b086407 JK |
252 | if (data->mark_query) |
253 | data->info.typep = &data->type; | |
254 | else | |
debca9d2 | 255 | strbuf_addstr(sb, type_name(data->type)); |
93d2a607 JK |
256 | } else if (is_atom("objectsize", atom, len)) { |
257 | if (data->mark_query) | |
258 | data->info.sizep = &data->size; | |
259 | else | |
ca473cef | 260 | strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size); |
a4ac1061 JK |
261 | } else if (is_atom("objectsize:disk", atom, len)) { |
262 | if (data->mark_query) | |
263 | data->info.disk_sizep = &data->disk_size; | |
264 | else | |
166df26f | 265 | strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size); |
97be0407 JK |
266 | } else if (is_atom("rest", atom, len)) { |
267 | if (data->mark_query) | |
268 | data->split_on_whitespace = 1; | |
269 | else if (data->rest) | |
270 | strbuf_addstr(sb, data->rest); | |
65ea9c3c JK |
271 | } else if (is_atom("deltabase", atom, len)) { |
272 | if (data->mark_query) | |
b99b6bcc | 273 | data->info.delta_base_oid = &data->delta_base_oid; |
65ea9c3c | 274 | else |
cd4f77be | 275 | strbuf_addstr(sb, |
276 | oid_to_hex(&data->delta_base_oid)); | |
93d2a607 JK |
277 | } else |
278 | die("unknown format element: %.*s", len, atom); | |
279 | } | |
280 | ||
281 | static size_t expand_format(struct strbuf *sb, const char *start, void *data) | |
282 | { | |
283 | const char *end; | |
284 | ||
285 | if (*start != '(') | |
286 | return 0; | |
287 | end = strchr(start + 1, ')'); | |
288 | if (!end) | |
289 | die("format element '%s' does not end in ')'", start); | |
290 | ||
291 | expand_atom(sb, start + 1, end - start - 1, data); | |
292 | ||
293 | return end - start + 1; | |
294 | } | |
295 | ||
fc4937c3 JK |
296 | static void batch_write(struct batch_options *opt, const void *data, int len) |
297 | { | |
298 | if (opt->buffer_output) { | |
299 | if (fwrite(data, 1, len, stdout) != len) | |
300 | die_errno("unable to write to stdout"); | |
301 | } else | |
302 | write_or_die(1, data, len); | |
303 | } | |
304 | ||
305 | static void print_object_or_die(struct batch_options *opt, struct expand_data *data) | |
98e2092b | 306 | { |
63ecb99e | 307 | const struct object_id *oid = &data->oid; |
370c9268 | 308 | |
6554dfa9 JK |
309 | assert(data->info.typep); |
310 | ||
370c9268 | 311 | if (data->type == OBJ_BLOB) { |
fc4937c3 JK |
312 | if (opt->buffer_output) |
313 | fflush(stdout); | |
a2c75526 | 314 | if (opt->transform_mode) { |
32145943 JS |
315 | char *contents; |
316 | unsigned long size; | |
317 | ||
318 | if (!data->rest) | |
7889ed25 | 319 | die("missing path for '%s'", oid_to_hex(oid)); |
32145943 | 320 | |
a2c75526 | 321 | if (opt->transform_mode == 'w') { |
7889ed25 | 322 | if (filter_object(data->rest, 0100644, oid, |
32145943 JS |
323 | &contents, &size)) |
324 | die("could not convert '%s' %s", | |
7889ed25 | 325 | oid_to_hex(oid), data->rest); |
a2c75526 | 326 | } else if (opt->transform_mode == 'c') { |
32145943 | 327 | enum object_type type; |
6afaf807 NTND |
328 | if (!textconv_object(the_repository, |
329 | data->rest, 0100644, oid, | |
32145943 | 330 | 1, &contents, &size)) |
b4f5aca4 | 331 | contents = read_object_file(oid, |
332 | &type, | |
333 | &size); | |
32145943 JS |
334 | if (!contents) |
335 | die("could not convert '%s' %s", | |
7889ed25 | 336 | oid_to_hex(oid), data->rest); |
32145943 | 337 | } else |
a2c75526 | 338 | BUG("invalid transform_mode: %c", opt->transform_mode); |
32145943 JS |
339 | batch_write(opt, contents, size); |
340 | free(contents); | |
98f425b4 JK |
341 | } else { |
342 | stream_blob(oid); | |
343 | } | |
98e2092b JK |
344 | } |
345 | else { | |
370c9268 JK |
346 | enum object_type type; |
347 | unsigned long size; | |
98e2092b JK |
348 | void *contents; |
349 | ||
b4f5aca4 | 350 | contents = read_object_file(oid, &type, &size); |
98e2092b | 351 | if (!contents) |
63ecb99e | 352 | die("object %s disappeared", oid_to_hex(oid)); |
370c9268 | 353 | if (type != data->type) |
63ecb99e | 354 | die("object %s changed type!?", oid_to_hex(oid)); |
6554dfa9 | 355 | if (data->info.sizep && size != data->size) |
63ecb99e | 356 | die("object %s changed size!?", oid_to_hex(oid)); |
98e2092b | 357 | |
fc4937c3 | 358 | batch_write(opt, contents, size); |
98e2092b JK |
359 | free(contents); |
360 | } | |
361 | } | |
362 | ||
eb54a339 JC |
363 | static void print_default_format(struct strbuf *scratch, struct expand_data *data) |
364 | { | |
365 | strbuf_addf(scratch, "%s %s %"PRIuMAX"\n", oid_to_hex(&data->oid), | |
366 | type_name(data->type), | |
367 | (uintmax_t)data->size); | |
368 | } | |
369 | ||
bf972896 JK |
370 | /* |
371 | * If "pack" is non-NULL, then "offset" is the byte offset within the pack from | |
372 | * which the object may be accessed (though note that we may also rely on | |
373 | * data->oid, too). If "pack" is NULL, then offset is ignored. | |
374 | */ | |
79ed0a5e JK |
375 | static void batch_object_write(const char *obj_name, |
376 | struct strbuf *scratch, | |
377 | struct batch_options *opt, | |
bf972896 JK |
378 | struct expand_data *data, |
379 | struct packed_git *pack, | |
380 | off_t offset) | |
44b877e9 | 381 | { |
bf972896 JK |
382 | if (!data->skip_object_info) { |
383 | int ret; | |
384 | ||
385 | if (pack) | |
386 | ret = packed_object_info(the_repository, pack, offset, | |
387 | &data->info); | |
388 | else | |
389 | ret = oid_object_info_extended(the_repository, | |
390 | &data->oid, &data->info, | |
391 | OBJECT_INFO_LOOKUP_REPLACE); | |
392 | if (ret < 0) { | |
393 | printf("%s missing\n", | |
394 | obj_name ? obj_name : oid_to_hex(&data->oid)); | |
395 | fflush(stdout); | |
396 | return; | |
397 | } | |
44b877e9 JK |
398 | } |
399 | ||
79ed0a5e | 400 | strbuf_reset(scratch); |
eb54a339 JC |
401 | |
402 | if (!opt->format) { | |
403 | print_default_format(scratch, data); | |
404 | } else { | |
405 | strbuf_expand(scratch, opt->format, expand_format, data); | |
406 | strbuf_addch(scratch, '\n'); | |
407 | } | |
408 | ||
79ed0a5e | 409 | batch_write(opt, scratch->buf, scratch->len); |
44b877e9 | 410 | |
ac4e58ca | 411 | if (opt->batch_mode == BATCH_MODE_CONTENTS) { |
44b877e9 JK |
412 | print_object_or_die(opt, data); |
413 | batch_write(opt, "\n", 1); | |
414 | } | |
415 | } | |
416 | ||
79ed0a5e JK |
417 | static void batch_one_object(const char *obj_name, |
418 | struct strbuf *scratch, | |
419 | struct batch_options *opt, | |
82330950 | 420 | struct expand_data *data) |
05d5667f | 421 | { |
122d5346 | 422 | struct object_context ctx; |
321c89bf | 423 | int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0; |
d1dd94b3 | 424 | enum get_oid_result result; |
05d5667f | 425 | |
3a7a698e NTND |
426 | result = get_oid_with_context(the_repository, obj_name, |
427 | flags, &data->oid, &ctx); | |
122d5346 DT |
428 | if (result != FOUND) { |
429 | switch (result) { | |
430 | case MISSING_OBJECT: | |
431 | printf("%s missing\n", obj_name); | |
432 | break; | |
d1dd94b3 DT |
433 | case SHORT_NAME_AMBIGUOUS: |
434 | printf("%s ambiguous\n", obj_name); | |
435 | break; | |
122d5346 DT |
436 | case DANGLING_SYMLINK: |
437 | printf("dangling %"PRIuMAX"\n%s\n", | |
438 | (uintmax_t)strlen(obj_name), obj_name); | |
439 | break; | |
440 | case SYMLINK_LOOP: | |
441 | printf("loop %"PRIuMAX"\n%s\n", | |
442 | (uintmax_t)strlen(obj_name), obj_name); | |
443 | break; | |
444 | case NOT_DIR: | |
445 | printf("notdir %"PRIuMAX"\n%s\n", | |
446 | (uintmax_t)strlen(obj_name), obj_name); | |
447 | break; | |
448 | default: | |
033abf97 | 449 | BUG("unknown get_sha1_with_context result %d\n", |
122d5346 DT |
450 | result); |
451 | break; | |
452 | } | |
453 | fflush(stdout); | |
82330950 | 454 | return; |
122d5346 DT |
455 | } |
456 | ||
457 | if (ctx.mode == 0) { | |
458 | printf("symlink %"PRIuMAX"\n%s\n", | |
459 | (uintmax_t)ctx.symlink_path.len, | |
460 | ctx.symlink_path.buf); | |
422b2063 | 461 | fflush(stdout); |
82330950 | 462 | return; |
05d5667f AR |
463 | } |
464 | ||
bf972896 | 465 | batch_object_write(obj_name, scratch, opt, data, NULL, 0); |
05d5667f AR |
466 | } |
467 | ||
6a951937 JK |
468 | struct object_cb_data { |
469 | struct batch_options *opt; | |
470 | struct expand_data *expand; | |
0750bb5b | 471 | struct oidset *seen; |
79ed0a5e | 472 | struct strbuf *scratch; |
6a951937 JK |
473 | }; |
474 | ||
1b7ba794 | 475 | static int batch_object_cb(const struct object_id *oid, void *vdata) |
6a951937 | 476 | { |
3115ee45 | 477 | struct object_cb_data *data = vdata; |
1b7ba794 | 478 | oidcpy(&data->expand->oid, oid); |
bf972896 JK |
479 | batch_object_write(NULL, data->scratch, data->opt, data->expand, |
480 | NULL, 0); | |
16ddcd40 | 481 | return 0; |
6a951937 JK |
482 | } |
483 | ||
b1adb384 JK |
484 | static int collect_loose_object(const struct object_id *oid, |
485 | const char *path, | |
486 | void *data) | |
6a951937 | 487 | { |
910650d2 | 488 | oid_array_append(data, oid); |
3115ee45 | 489 | return 0; |
6a951937 JK |
490 | } |
491 | ||
b1adb384 JK |
492 | static int collect_packed_object(const struct object_id *oid, |
493 | struct packed_git *pack, | |
494 | uint32_t pos, | |
495 | void *data) | |
6a951937 | 496 | { |
910650d2 | 497 | oid_array_append(data, oid); |
3115ee45 | 498 | return 0; |
6a951937 JK |
499 | } |
500 | ||
bf972896 JK |
501 | static int batch_unordered_object(const struct object_id *oid, |
502 | struct packed_git *pack, off_t offset, | |
503 | void *vdata) | |
0750bb5b JK |
504 | { |
505 | struct object_cb_data *data = vdata; | |
506 | ||
ced9fff7 | 507 | if (oidset_insert(data->seen, oid)) |
0750bb5b | 508 | return 0; |
0750bb5b | 509 | |
818e3930 | 510 | oidcpy(&data->expand->oid, oid); |
bf972896 JK |
511 | batch_object_write(NULL, data->scratch, data->opt, data->expand, |
512 | pack, offset); | |
818e3930 | 513 | return 0; |
0750bb5b JK |
514 | } |
515 | ||
516 | static int batch_unordered_loose(const struct object_id *oid, | |
517 | const char *path, | |
518 | void *data) | |
519 | { | |
bf972896 | 520 | return batch_unordered_object(oid, NULL, 0, data); |
0750bb5b JK |
521 | } |
522 | ||
523 | static int batch_unordered_packed(const struct object_id *oid, | |
524 | struct packed_git *pack, | |
525 | uint32_t pos, | |
526 | void *data) | |
527 | { | |
bf972896 JK |
528 | return batch_unordered_object(oid, pack, |
529 | nth_packed_object_offset(pack, pos), | |
530 | data); | |
0750bb5b JK |
531 | } |
532 | ||
440c705e JC |
533 | typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *, |
534 | struct strbuf *, struct expand_data *); | |
535 | ||
536 | struct queued_cmd { | |
537 | parse_cmd_fn_t fn; | |
538 | char *line; | |
539 | }; | |
540 | ||
541 | static void parse_cmd_contents(struct batch_options *opt, | |
542 | const char *line, | |
543 | struct strbuf *output, | |
544 | struct expand_data *data) | |
545 | { | |
546 | opt->batch_mode = BATCH_MODE_CONTENTS; | |
547 | batch_one_object(line, output, opt, data); | |
548 | } | |
549 | ||
550 | static void parse_cmd_info(struct batch_options *opt, | |
551 | const char *line, | |
552 | struct strbuf *output, | |
553 | struct expand_data *data) | |
554 | { | |
555 | opt->batch_mode = BATCH_MODE_INFO; | |
556 | batch_one_object(line, output, opt, data); | |
557 | } | |
558 | ||
559 | static void dispatch_calls(struct batch_options *opt, | |
560 | struct strbuf *output, | |
561 | struct expand_data *data, | |
562 | struct queued_cmd *cmd, | |
563 | int nr) | |
564 | { | |
565 | int i; | |
566 | ||
567 | if (!opt->buffer_output) | |
568 | die(_("flush is only for --buffer mode")); | |
569 | ||
570 | for (i = 0; i < nr; i++) | |
571 | cmd[i].fn(opt, cmd[i].line, output, data); | |
572 | ||
573 | fflush(stdout); | |
574 | } | |
575 | ||
576 | static void free_cmds(struct queued_cmd *cmd, size_t *nr) | |
577 | { | |
578 | size_t i; | |
579 | ||
580 | for (i = 0; i < *nr; i++) | |
581 | FREE_AND_NULL(cmd[i].line); | |
582 | ||
583 | *nr = 0; | |
584 | } | |
585 | ||
586 | ||
587 | static const struct parse_cmd { | |
588 | const char *name; | |
589 | parse_cmd_fn_t fn; | |
590 | unsigned takes_args; | |
591 | } commands[] = { | |
592 | { "contents", parse_cmd_contents, 1}, | |
593 | { "info", parse_cmd_info, 1}, | |
594 | { "flush", NULL, 0}, | |
595 | }; | |
596 | ||
597 | static void batch_objects_command(struct batch_options *opt, | |
598 | struct strbuf *output, | |
599 | struct expand_data *data) | |
600 | { | |
601 | struct strbuf input = STRBUF_INIT; | |
602 | struct queued_cmd *queued_cmd = NULL; | |
603 | size_t alloc = 0, nr = 0; | |
604 | ||
605 | while (!strbuf_getline(&input, stdin)) { | |
606 | int i; | |
607 | const struct parse_cmd *cmd = NULL; | |
608 | const char *p = NULL, *cmd_end; | |
609 | struct queued_cmd call = {0}; | |
610 | ||
611 | if (!input.len) | |
612 | die(_("empty command in input")); | |
613 | if (isspace(*input.buf)) | |
614 | die(_("whitespace before command: '%s'"), input.buf); | |
615 | ||
616 | for (i = 0; i < ARRAY_SIZE(commands); i++) { | |
617 | if (!skip_prefix(input.buf, commands[i].name, &cmd_end)) | |
618 | continue; | |
619 | ||
620 | cmd = &commands[i]; | |
621 | if (cmd->takes_args) { | |
622 | if (*cmd_end != ' ') | |
623 | die(_("%s requires arguments"), | |
624 | commands[i].name); | |
625 | ||
626 | p = cmd_end + 1; | |
627 | } else if (*cmd_end) { | |
628 | die(_("%s takes no arguments"), | |
629 | commands[i].name); | |
630 | } | |
631 | ||
632 | break; | |
633 | } | |
634 | ||
635 | if (!cmd) | |
636 | die(_("unknown command: '%s'"), input.buf); | |
637 | ||
638 | if (!strcmp(cmd->name, "flush")) { | |
639 | dispatch_calls(opt, output, data, queued_cmd, nr); | |
640 | free_cmds(queued_cmd, &nr); | |
641 | } else if (!opt->buffer_output) { | |
642 | cmd->fn(opt, p, output, data); | |
643 | } else { | |
644 | ALLOC_GROW(queued_cmd, nr + 1, alloc); | |
645 | call.fn = cmd->fn; | |
646 | call.line = xstrdup_or_null(p); | |
647 | queued_cmd[nr++] = call; | |
648 | } | |
649 | } | |
650 | ||
651 | if (opt->buffer_output && | |
652 | nr && | |
653 | !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) { | |
654 | dispatch_calls(opt, output, data, queued_cmd, nr); | |
655 | free_cmds(queued_cmd, &nr); | |
656 | } | |
657 | ||
d90dafbe | 658 | free_cmds(queued_cmd, &nr); |
440c705e JC |
659 | free(queued_cmd); |
660 | strbuf_release(&input); | |
661 | } | |
662 | ||
eb54a339 JC |
663 | #define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)" |
664 | ||
b71bd480 | 665 | static int batch_objects(struct batch_options *opt) |
05d5667f | 666 | { |
54d2f0d9 JK |
667 | struct strbuf input = STRBUF_INIT; |
668 | struct strbuf output = STRBUF_INIT; | |
93d2a607 | 669 | struct expand_data data; |
a42fcd15 | 670 | int save_warning; |
07e23839 | 671 | int retval = 0; |
93d2a607 | 672 | |
93d2a607 JK |
673 | /* |
674 | * Expand once with our special mark_query flag, which will prime the | |
c93206b4 | 675 | * object_info to be handed to oid_object_info_extended for each |
93d2a607 JK |
676 | * object. |
677 | */ | |
678 | memset(&data, 0, sizeof(data)); | |
679 | data.mark_query = 1; | |
eb54a339 JC |
680 | strbuf_expand(&output, |
681 | opt->format ? opt->format : DEFAULT_FORMAT, | |
682 | expand_format, | |
683 | &data); | |
93d2a607 | 684 | data.mark_query = 0; |
54d2f0d9 | 685 | strbuf_release(&output); |
a2c75526 | 686 | if (opt->transform_mode) |
32145943 | 687 | data.split_on_whitespace = 1; |
05d5667f | 688 | |
eb54a339 JC |
689 | if (opt->format && !strcmp(opt->format, DEFAULT_FORMAT)) |
690 | opt->format = NULL; | |
6554dfa9 JK |
691 | /* |
692 | * If we are printing out the object, then always fill in the type, | |
693 | * since we will want to decide whether or not to stream. | |
694 | */ | |
ac4e58ca | 695 | if (opt->batch_mode == BATCH_MODE_CONTENTS) |
6554dfa9 JK |
696 | data.info.typep = &data.type; |
697 | ||
e16acc80 | 698 | if (opt->all_objects) { |
ee02ac61 | 699 | struct object_cb_data cb; |
e16acc80 ZH |
700 | struct object_info empty = OBJECT_INFO_INIT; |
701 | ||
702 | if (!memcmp(&data.info, &empty, sizeof(empty))) | |
703 | data.skip_object_info = 1; | |
3115ee45 | 704 | |
b14ed5ad CC |
705 | if (has_promisor_remote()) |
706 | warning("This repository uses promisor remotes. Some objects may not be loaded."); | |
3115ee45 | 707 | |
5c5b29b4 JK |
708 | read_replace_refs = 0; |
709 | ||
6a951937 JK |
710 | cb.opt = opt; |
711 | cb.expand = &data; | |
79ed0a5e | 712 | cb.scratch = &output; |
3115ee45 | 713 | |
0750bb5b JK |
714 | if (opt->unordered) { |
715 | struct oidset seen = OIDSET_INIT; | |
716 | ||
717 | cb.seen = &seen; | |
718 | ||
719 | for_each_loose_object(batch_unordered_loose, &cb, 0); | |
720 | for_each_packed_object(batch_unordered_packed, &cb, | |
721 | FOR_EACH_OBJECT_PACK_ORDER); | |
722 | ||
723 | oidset_clear(&seen); | |
724 | } else { | |
725 | struct oid_array sa = OID_ARRAY_INIT; | |
726 | ||
727 | for_each_loose_object(collect_loose_object, &sa, 0); | |
728 | for_each_packed_object(collect_packed_object, &sa, 0); | |
729 | ||
730 | oid_array_for_each_unique(&sa, batch_object_cb, &cb); | |
731 | ||
732 | oid_array_clear(&sa); | |
733 | } | |
734 | ||
79ed0a5e | 735 | strbuf_release(&output); |
6a951937 JK |
736 | return 0; |
737 | } | |
738 | ||
25fba78d JK |
739 | /* |
740 | * We are going to call get_sha1 on a potentially very large number of | |
741 | * objects. In most large cases, these will be actual object sha1s. The | |
742 | * cost to double-check that each one is not also a ref (just so we can | |
743 | * warn) ends up dwarfing the actual cost of the object lookups | |
744 | * themselves. We can work around it by just turning off the warning. | |
745 | */ | |
a42fcd15 | 746 | save_warning = warn_on_object_refname_ambiguity; |
25fba78d JK |
747 | warn_on_object_refname_ambiguity = 0; |
748 | ||
440c705e JC |
749 | if (opt->batch_mode == BATCH_MODE_QUEUE_AND_DISPATCH) { |
750 | batch_objects_command(opt, &output, &data); | |
751 | goto cleanup; | |
752 | } | |
753 | ||
54d2f0d9 | 754 | while (strbuf_getline(&input, stdin) != EOF) { |
97be0407 JK |
755 | if (data.split_on_whitespace) { |
756 | /* | |
757 | * Split at first whitespace, tying off the beginning | |
758 | * of the string and saving the remainder (or NULL) in | |
759 | * data.rest. | |
760 | */ | |
54d2f0d9 | 761 | char *p = strpbrk(input.buf, " \t"); |
97be0407 JK |
762 | if (p) { |
763 | while (*p && strchr(" \t", *p)) | |
764 | *p++ = '\0'; | |
765 | } | |
766 | data.rest = p; | |
767 | } | |
768 | ||
79ed0a5e | 769 | batch_one_object(input.buf, &output, opt, &data); |
05d5667f AR |
770 | } |
771 | ||
440c705e | 772 | cleanup: |
54d2f0d9 | 773 | strbuf_release(&input); |
79ed0a5e | 774 | strbuf_release(&output); |
a42fcd15 | 775 | warn_on_object_refname_ambiguity = save_warning; |
07e23839 | 776 | return retval; |
05d5667f AR |
777 | } |
778 | ||
e5fba602 CP |
779 | static int git_cat_file_config(const char *var, const char *value, void *cb) |
780 | { | |
6680a087 | 781 | if (userdiff_config(var, value) < 0) |
e5fba602 | 782 | return -1; |
e5fba602 CP |
783 | |
784 | return git_default_config(var, value, cb); | |
785 | } | |
786 | ||
b71bd480 JK |
787 | static int batch_option_callback(const struct option *opt, |
788 | const char *arg, | |
789 | int unset) | |
790 | { | |
791 | struct batch_options *bo = opt->value; | |
792 | ||
517fe807 JK |
793 | BUG_ON_OPT_NEG(unset); |
794 | ||
122d5346 | 795 | if (bo->enabled) { |
0eb8d376 | 796 | return error(_("only one batch option may be specified")); |
b71bd480 JK |
797 | } |
798 | ||
799 | bo->enabled = 1; | |
ac4e58ca JC |
800 | |
801 | if (!strcmp(opt->long_name, "batch")) | |
802 | bo->batch_mode = BATCH_MODE_CONTENTS; | |
803 | else if (!strcmp(opt->long_name, "batch-check")) | |
804 | bo->batch_mode = BATCH_MODE_INFO; | |
440c705e JC |
805 | else if (!strcmp(opt->long_name, "batch-command")) |
806 | bo->batch_mode = BATCH_MODE_QUEUE_AND_DISPATCH; | |
ac4e58ca JC |
807 | else |
808 | BUG("%s given to batch-option-callback", opt->long_name); | |
809 | ||
93d2a607 | 810 | bo->format = arg; |
b71bd480 JK |
811 | |
812 | return 0; | |
813 | } | |
814 | ||
9cf71b17 AR |
815 | int cmd_cat_file(int argc, const char **argv, const char *prefix) |
816 | { | |
b71bd480 | 817 | int opt = 0; |
b3fe4680 ÆAB |
818 | int opt_cw = 0; |
819 | int opt_epts = 0; | |
4814dbe8 | 820 | const char *exp_type = NULL, *obj_name = NULL; |
b71bd480 | 821 | struct batch_options batch = {0}; |
39e4ae38 | 822 | int unknown_type = 0; |
9cf71b17 | 823 | |
5a404178 ÆAB |
824 | const char * const usage[] = { |
825 | N_("git cat-file <type> <object>"), | |
826 | N_("git cat-file (-e | -p) <object>"), | |
83dc4434 | 827 | N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"), |
440c705e | 828 | N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n" |
5a404178 ÆAB |
829 | " [--buffer] [--follow-symlinks] [--unordered]\n" |
830 | " [--textconv | --filters]"), | |
83dc4434 | 831 | N_("git cat-file (--textconv | --filters)\n" |
5a404178 ÆAB |
832 | " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"), |
833 | NULL | |
834 | }; | |
15d8e565 | 835 | const struct option options[] = { |
57d6a1cf ÆAB |
836 | /* Simple queries */ |
837 | OPT_GROUP(N_("Check object existence or emit object contents")), | |
b48158ac | 838 | OPT_CMDMODE('e', NULL, &opt, |
57d6a1cf ÆAB |
839 | N_("check if <object> exists"), 'e'), |
840 | OPT_CMDMODE('p', NULL, &opt, N_("pretty-print <object> content"), 'p'), | |
841 | ||
842 | OPT_GROUP(N_("Emit [broken] object attributes")), | |
843 | OPT_CMDMODE('t', NULL, &opt, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'), | |
844 | OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'), | |
ad42f28d | 845 | OPT_BOOL(0, "allow-unknown-type", &unknown_type, |
39e4ae38 | 846 | N_("allow -s and -t to work with broken/corrupt objects")), |
57d6a1cf ÆAB |
847 | /* Batch mode */ |
848 | OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")), | |
849 | OPT_CALLBACK_F(0, "batch", &batch, N_("format"), | |
850 | N_("show full <object> or <rev> contents"), | |
0ad1efb4 | 851 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, |
203c8533 | 852 | batch_option_callback), |
57d6a1cf ÆAB |
853 | OPT_CALLBACK_F(0, "batch-check", &batch, N_("format"), |
854 | N_("like --batch, but don't emit <contents>"), | |
0ad1efb4 | 855 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, |
203c8533 | 856 | batch_option_callback), |
440c705e JC |
857 | OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"), |
858 | N_("read commands from stdin"), | |
859 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, | |
860 | batch_option_callback), | |
57d6a1cf ÆAB |
861 | OPT_CMDMODE(0, "batch-all-objects", &opt, |
862 | N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'), | |
863 | /* Batch-specific options */ | |
864 | OPT_GROUP(N_("Change or optimize batch output")), | |
865 | OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")), | |
122d5346 | 866 | OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks, |
57d6a1cf | 867 | N_("follow in-tree symlinks")), |
0750bb5b | 868 | OPT_BOOL(0, "unordered", &batch.unordered, |
57d6a1cf ÆAB |
869 | N_("do not order objects before emitting them")), |
870 | /* Textconv options, stand-ole*/ | |
871 | OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")), | |
872 | OPT_CMDMODE(0, "textconv", &opt, | |
873 | N_("run textconv on object's content"), 'c'), | |
874 | OPT_CMDMODE(0, "filters", &opt, | |
875 | N_("run filters on object's content"), 'w'), | |
876 | OPT_STRING(0, "path", &force_path, N_("blob|tree"), | |
83dc4434 | 877 | N_("use a <path> for (--textconv | --filters); Not with 'batch'")), |
15d8e565 MB |
878 | OPT_END() |
879 | }; | |
a8128ed6 | 880 | |
e5fba602 | 881 | git_config(git_cat_file_config, NULL); |
4814dbe8 | 882 | |
6a36e1e7 | 883 | batch.buffer_output = -1; |
05d5667f | 884 | |
485fd2c3 | 885 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
b3fe4680 ÆAB |
886 | opt_cw = (opt == 'c' || opt == 'w'); |
887 | opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's'); | |
9cf71b17 | 888 | |
b3fe4680 ÆAB |
889 | /* --batch-all-objects? */ |
890 | if (opt == 'b') | |
891 | batch.all_objects = 1; | |
7bcf3414 | 892 | |
b3fe4680 ÆAB |
893 | /* Option compatibility */ |
894 | if (force_path && !opt_cw) | |
895 | usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"), | |
896 | usage, options, | |
897 | "--path", _("path|tree-ish"), "--filters", | |
898 | "--textconv"); | |
32145943 | 899 | |
b3fe4680 ÆAB |
900 | /* Option compatibility with batch mode */ |
901 | if (batch.enabled) | |
902 | ; | |
903 | else if (batch.follow_symlinks) | |
904 | usage_msg_optf(_("'%s' requires a batch mode"), usage, options, | |
5fb24902 | 905 | "--follow-symlinks"); |
b3fe4680 ÆAB |
906 | else if (batch.buffer_output >= 0) |
907 | usage_msg_optf(_("'%s' requires a batch mode"), usage, options, | |
908 | "--buffer"); | |
909 | else if (batch.all_objects) | |
910 | usage_msg_optf(_("'%s' requires a batch mode"), usage, options, | |
911 | "--batch-all-objects"); | |
912 | ||
913 | /* Batch defaults */ | |
6a36e1e7 JK |
914 | if (batch.buffer_output < 0) |
915 | batch.buffer_output = batch.all_objects; | |
916 | ||
b3fe4680 ÆAB |
917 | /* Return early if we're in batch mode? */ |
918 | if (batch.enabled) { | |
919 | if (opt_cw) | |
a2c75526 | 920 | batch.transform_mode = opt; |
b3fe4680 ÆAB |
921 | else if (opt && opt != 'b') |
922 | usage_msg_optf(_("'-%c' is incompatible with batch mode"), | |
923 | usage, options, opt); | |
924 | else if (argc) | |
925 | usage_msg_opt(_("batch modes take no arguments"), usage, | |
926 | options); | |
927 | ||
b71bd480 | 928 | return batch_objects(&batch); |
b3fe4680 ÆAB |
929 | } |
930 | ||
931 | if (opt) { | |
932 | if (!argc && opt == 'c') | |
933 | usage_msg_optf(_("<rev> required with '%s'"), | |
934 | usage, options, "--textconv"); | |
935 | else if (!argc && opt == 'w') | |
936 | usage_msg_optf(_("<rev> required with '%s'"), | |
937 | usage, options, "--filters"); | |
938 | else if (!argc && opt_epts) | |
939 | usage_msg_optf(_("<object> required with '-%c'"), | |
940 | usage, options, opt); | |
941 | else if (argc == 1) | |
942 | obj_name = argv[0]; | |
943 | else | |
944 | usage_msg_opt(_("too many arguments"), usage, options); | |
945 | } else if (!argc) { | |
946 | usage_with_options(usage, options); | |
947 | } else if (argc != 2) { | |
948 | usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"), | |
949 | usage, options, argc); | |
950 | } else if (argc) { | |
951 | exp_type = argv[0]; | |
952 | obj_name = argv[1]; | |
953 | } | |
05d5667f | 954 | |
39e4ae38 KN |
955 | if (unknown_type && opt != 't' && opt != 's') |
956 | die("git cat-file --allow-unknown-type: use with -s or -t"); | |
957 | return cat_one_file(opt, exp_type, obj_name, unknown_type); | |
9cf71b17 | 958 | } |