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