| 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
| 6 | |
| 7 | #define USE_THE_REPOSITORY_VARIABLE |
| 8 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 9 | |
| 10 | #include "builtin.h" |
| 11 | #include "config.h" |
| 12 | #include "convert.h" |
| 13 | #include "diff.h" |
| 14 | #include "environment.h" |
| 15 | #include "gettext.h" |
| 16 | #include "hex.h" |
| 17 | #include "ident.h" |
| 18 | #include "list-objects-filter-options.h" |
| 19 | #include "parse-options.h" |
| 20 | #include "userdiff.h" |
| 21 | #include "oid-array.h" |
| 22 | #include "packfile.h" |
| 23 | #include "pack-bitmap.h" |
| 24 | #include "object-file.h" |
| 25 | #include "object-name.h" |
| 26 | #include "odb.h" |
| 27 | #include "odb/streaming.h" |
| 28 | #include "replace-object.h" |
| 29 | #include "promisor-remote.h" |
| 30 | #include "mailmap.h" |
| 31 | #include "write-or-die.h" |
| 32 | |
| 33 | enum batch_mode { |
| 34 | BATCH_MODE_CONTENTS, |
| 35 | BATCH_MODE_INFO, |
| 36 | BATCH_MODE_QUEUE_AND_DISPATCH, |
| 37 | }; |
| 38 | |
| 39 | struct batch_options { |
| 40 | struct list_objects_filter_options objects_filter; |
| 41 | int enabled; |
| 42 | int follow_symlinks; |
| 43 | enum batch_mode batch_mode; |
| 44 | int buffer_output; |
| 45 | int all_objects; |
| 46 | int unordered; |
| 47 | int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */ |
| 48 | char input_delim; |
| 49 | char output_delim; |
| 50 | const char *format; |
| 51 | }; |
| 52 | |
| 53 | static const char *force_path; |
| 54 | |
| 55 | static struct string_list mailmap = STRING_LIST_INIT_NODUP; |
| 56 | static int use_mailmap; |
| 57 | |
| 58 | static char *replace_idents_using_mailmap(char *, size_t *); |
| 59 | |
| 60 | /* |
| 61 | * The mailmap is initialized with .strdup_strings set to 0, |
| 62 | * but read_mailmap() sets the bit to 1 (this is true even when |
| 63 | * not a single mailmap entry is read), so it can be used for |
| 64 | * lazy loading. |
| 65 | */ |
| 66 | static void load_mailmap(void) |
| 67 | { |
| 68 | if (mailmap.strdup_strings) |
| 69 | return; |
| 70 | |
| 71 | read_mailmap(the_repository, &mailmap); |
| 72 | } |
| 73 | |
| 74 | static char *replace_idents_using_mailmap(char *object_buf, size_t *size) |
| 75 | { |
| 76 | struct strbuf sb = STRBUF_INIT; |
| 77 | const char *headers[] = { "author ", "committer ", "tagger ", NULL }; |
| 78 | |
| 79 | strbuf_attach(&sb, object_buf, *size, *size + 1); |
| 80 | apply_mailmap_to_header(&sb, headers, &mailmap); |
| 81 | *size = sb.len; |
| 82 | return strbuf_detach(&sb, NULL); |
| 83 | } |
| 84 | |
| 85 | static int filter_object(const char *path, unsigned mode, |
| 86 | const struct object_id *oid, |
| 87 | char **buf, unsigned long *size) |
| 88 | { |
| 89 | enum object_type type; |
| 90 | |
| 91 | *buf = odb_read_object(the_repository->objects, oid, &type, size); |
| 92 | if (!*buf) |
| 93 | return error(_("cannot read object %s '%s'"), |
| 94 | oid_to_hex(oid), path); |
| 95 | if ((type == OBJ_BLOB) && S_ISREG(mode)) { |
| 96 | struct strbuf strbuf = STRBUF_INIT; |
| 97 | struct checkout_metadata meta; |
| 98 | |
| 99 | init_checkout_metadata(&meta, NULL, NULL, oid); |
| 100 | if (convert_to_working_tree(the_repository->index, path, *buf, *size, &strbuf, &meta)) { |
| 101 | free(*buf); |
| 102 | *size = strbuf.len; |
| 103 | *buf = strbuf_detach(&strbuf, NULL); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int stream_blob(const struct object_id *oid) |
| 111 | { |
| 112 | if (odb_stream_blob_to_fd(the_repository->objects, 1, oid, NULL, 0)) |
| 113 | die("unable to stream %s to stdout", oid_to_hex(oid)); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int cat_one_file(int opt, const char *exp_type, const char *obj_name) |
| 118 | { |
| 119 | int ret; |
| 120 | struct object_id oid; |
| 121 | enum object_type type; |
| 122 | char *buf; |
| 123 | unsigned long size; |
| 124 | struct object_context obj_context = {0}; |
| 125 | struct object_info oi = OBJECT_INFO_INIT; |
| 126 | unsigned flags = OBJECT_INFO_LOOKUP_REPLACE; |
| 127 | unsigned get_oid_flags = |
| 128 | GET_OID_RECORD_PATH | |
| 129 | GET_OID_ONLY_TO_DIE | |
| 130 | GET_OID_HASH_ANY; |
| 131 | const char *path = force_path; |
| 132 | const int opt_cw = (opt == 'c' || opt == 'w'); |
| 133 | if (!path && opt_cw) |
| 134 | get_oid_flags |= GET_OID_REQUIRE_PATH; |
| 135 | |
| 136 | if (get_oid_with_context(the_repository, obj_name, get_oid_flags, &oid, |
| 137 | &obj_context)) |
| 138 | die("Not a valid object name %s", obj_name); |
| 139 | |
| 140 | if (!path) |
| 141 | path = obj_context.path; |
| 142 | if (obj_context.mode == S_IFINVALID) |
| 143 | obj_context.mode = 0100644; |
| 144 | |
| 145 | buf = NULL; |
| 146 | switch (opt) { |
| 147 | case 't': |
| 148 | oi.typep = &type; |
| 149 | if (odb_read_object_info_extended(the_repository->objects, &oid, &oi, flags) < 0) |
| 150 | die("git cat-file: could not get object info"); |
| 151 | printf("%s\n", type_name(type)); |
| 152 | ret = 0; |
| 153 | goto cleanup; |
| 154 | |
| 155 | case 's': |
| 156 | oi.sizep = &size; |
| 157 | |
| 158 | if (use_mailmap) { |
| 159 | oi.typep = &type; |
| 160 | oi.contentp = (void**)&buf; |
| 161 | } |
| 162 | |
| 163 | if (odb_read_object_info_extended(the_repository->objects, &oid, &oi, flags) < 0) |
| 164 | die("git cat-file: could not get object info"); |
| 165 | |
| 166 | if (use_mailmap && (type == OBJ_COMMIT || type == OBJ_TAG)) { |
| 167 | size_t s = size; |
| 168 | buf = replace_idents_using_mailmap(buf, &s); |
| 169 | size = cast_size_t_to_ulong(s); |
| 170 | } |
| 171 | |
| 172 | printf("%"PRIuMAX"\n", (uintmax_t)size); |
| 173 | ret = 0; |
| 174 | goto cleanup; |
| 175 | |
| 176 | case 'e': |
| 177 | ret = !odb_has_object(the_repository->objects, &oid, |
| 178 | ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR); |
| 179 | goto cleanup; |
| 180 | |
| 181 | case 'w': |
| 182 | |
| 183 | if (filter_object(path, obj_context.mode, |
| 184 | &oid, &buf, &size)) { |
| 185 | ret = -1; |
| 186 | goto cleanup; |
| 187 | } |
| 188 | break; |
| 189 | |
| 190 | case 'c': |
| 191 | if (textconv_object(the_repository, path, obj_context.mode, |
| 192 | &oid, 1, &buf, &size)) |
| 193 | break; |
| 194 | /* else fallthrough */ |
| 195 | |
| 196 | case 'p': |
| 197 | type = odb_read_object_info(the_repository->objects, &oid, NULL); |
| 198 | if (type < 0) |
| 199 | die("Not a valid object name %s", obj_name); |
| 200 | |
| 201 | /* custom pretty-print here */ |
| 202 | if (type == OBJ_TREE) { |
| 203 | const char *ls_args[3] = { NULL }; |
| 204 | ls_args[0] = "ls-tree"; |
| 205 | ls_args[1] = obj_name; |
| 206 | ret = cmd_ls_tree(2, ls_args, NULL, the_repository); |
| 207 | goto cleanup; |
| 208 | } |
| 209 | |
| 210 | if (type == OBJ_BLOB) { |
| 211 | ret = stream_blob(&oid); |
| 212 | goto cleanup; |
| 213 | } |
| 214 | buf = odb_read_object(the_repository->objects, &oid, |
| 215 | &type, &size); |
| 216 | if (!buf) |
| 217 | die("Cannot read object %s", obj_name); |
| 218 | |
| 219 | if (use_mailmap) { |
| 220 | size_t s = size; |
| 221 | buf = replace_idents_using_mailmap(buf, &s); |
| 222 | size = cast_size_t_to_ulong(s); |
| 223 | } |
| 224 | |
| 225 | /* otherwise just spit out the data */ |
| 226 | break; |
| 227 | |
| 228 | case 0: |
| 229 | { |
| 230 | enum object_type exp_type_id = type_from_string(exp_type); |
| 231 | |
| 232 | if (exp_type_id == OBJ_BLOB) { |
| 233 | struct object_id blob_oid; |
| 234 | if (odb_read_object_info(the_repository->objects, |
| 235 | &oid, NULL) == OBJ_TAG) { |
| 236 | char *buffer = odb_read_object(the_repository->objects, |
| 237 | &oid, &type, &size); |
| 238 | const char *target; |
| 239 | |
| 240 | if (!buffer) |
| 241 | die(_("unable to read %s"), oid_to_hex(&oid)); |
| 242 | |
| 243 | if (!skip_prefix(buffer, "object ", &target) || |
| 244 | get_oid_hex_algop(target, &blob_oid, |
| 245 | &hash_algos[oid.algo])) |
| 246 | die("%s not a valid tag", oid_to_hex(&oid)); |
| 247 | free(buffer); |
| 248 | } else |
| 249 | oidcpy(&blob_oid, &oid); |
| 250 | |
| 251 | if (odb_read_object_info(the_repository->objects, |
| 252 | &blob_oid, NULL) == OBJ_BLOB) { |
| 253 | ret = stream_blob(&blob_oid); |
| 254 | goto cleanup; |
| 255 | } |
| 256 | /* |
| 257 | * we attempted to dereference a tag to a blob |
| 258 | * and failed; there may be new dereference |
| 259 | * mechanisms this code is not aware of. |
| 260 | * fall-back to the usual case. |
| 261 | */ |
| 262 | } |
| 263 | buf = odb_read_object_peeled(the_repository->objects, &oid, |
| 264 | exp_type_id, &size, NULL); |
| 265 | |
| 266 | if (use_mailmap) { |
| 267 | size_t s = size; |
| 268 | buf = replace_idents_using_mailmap(buf, &s); |
| 269 | size = cast_size_t_to_ulong(s); |
| 270 | } |
| 271 | break; |
| 272 | } |
| 273 | default: |
| 274 | die("git cat-file: unknown option: %s", exp_type); |
| 275 | } |
| 276 | |
| 277 | if (!buf) |
| 278 | die("git cat-file %s: bad file", obj_name); |
| 279 | |
| 280 | write_or_die(1, buf, size); |
| 281 | ret = 0; |
| 282 | cleanup: |
| 283 | free(buf); |
| 284 | object_context_release(&obj_context); |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | struct expand_data { |
| 289 | struct object_id oid; |
| 290 | enum object_type type; |
| 291 | unsigned long size; |
| 292 | unsigned short mode; |
| 293 | off_t disk_size; |
| 294 | const char *rest; |
| 295 | struct object_id delta_base_oid; |
| 296 | |
| 297 | /* |
| 298 | * If mark_query is true, we do not expand anything, but rather |
| 299 | * just mark the object_info with items we wish to query. |
| 300 | */ |
| 301 | int mark_query; |
| 302 | |
| 303 | /* |
| 304 | * Whether to split the input on whitespace before feeding it to |
| 305 | * get_sha1; this is decided during the mark_query phase based on |
| 306 | * whether we have a %(rest) token in our format. |
| 307 | */ |
| 308 | int split_on_whitespace; |
| 309 | |
| 310 | /* |
| 311 | * After a mark_query run, this object_info is set up to be |
| 312 | * passed to odb_read_object_info_extended. It will point to the data |
| 313 | * elements above, so you can retrieve the response from there. |
| 314 | */ |
| 315 | struct object_info info; |
| 316 | |
| 317 | /* |
| 318 | * This flag will be true if the requested batch format and options |
| 319 | * don't require us to call oid_object_info, which can then be |
| 320 | * optimized out. |
| 321 | */ |
| 322 | unsigned skip_object_info : 1; |
| 323 | }; |
| 324 | #define EXPAND_DATA_INIT { .mode = S_IFINVALID } |
| 325 | |
| 326 | static int is_atom(const char *atom, const char *s, int slen) |
| 327 | { |
| 328 | int alen = strlen(atom); |
| 329 | return alen == slen && !memcmp(atom, s, alen); |
| 330 | } |
| 331 | |
| 332 | static int expand_atom(struct strbuf *sb, const char *atom, int len, |
| 333 | struct expand_data *data) |
| 334 | { |
| 335 | if (is_atom("objectname", atom, len)) { |
| 336 | if (!data->mark_query) |
| 337 | strbuf_add_oid_hex(sb, &data->oid); |
| 338 | } else if (is_atom("objecttype", atom, len)) { |
| 339 | if (data->mark_query) |
| 340 | data->info.typep = &data->type; |
| 341 | else |
| 342 | strbuf_addstr(sb, type_name(data->type)); |
| 343 | } else if (is_atom("objectsize", atom, len)) { |
| 344 | if (data->mark_query) |
| 345 | data->info.sizep = &data->size; |
| 346 | else |
| 347 | strbuf_add_uint(sb, data->size); |
| 348 | } else if (is_atom("objectsize:disk", atom, len)) { |
| 349 | if (data->mark_query) |
| 350 | data->info.disk_sizep = &data->disk_size; |
| 351 | else |
| 352 | strbuf_add_uint(sb, data->disk_size); |
| 353 | } else if (is_atom("rest", atom, len)) { |
| 354 | if (data->mark_query) |
| 355 | data->split_on_whitespace = 1; |
| 356 | else if (data->rest) |
| 357 | strbuf_addstr(sb, data->rest); |
| 358 | } else if (is_atom("deltabase", atom, len)) { |
| 359 | if (data->mark_query) |
| 360 | data->info.delta_base_oid = &data->delta_base_oid; |
| 361 | else |
| 362 | strbuf_add_oid_hex(sb, &data->delta_base_oid); |
| 363 | } else if (is_atom("objectmode", atom, len)) { |
| 364 | if (!data->mark_query && !(S_IFINVALID == data->mode)) |
| 365 | strbuf_addf(sb, "%06o", data->mode); |
| 366 | } else |
| 367 | return 0; |
| 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | static void expand_format(struct strbuf *sb, const char *start, |
| 372 | struct expand_data *data) |
| 373 | { |
| 374 | while (strbuf_expand_step(sb, &start)) { |
| 375 | const char *end; |
| 376 | |
| 377 | if (skip_prefix(start, "%", &start) || *start != '(') |
| 378 | strbuf_addch(sb, '%'); |
| 379 | else if ((end = strchr(start + 1, ')')) && |
| 380 | expand_atom(sb, start + 1, end - start - 1, data)) |
| 381 | start = end + 1; |
| 382 | else |
| 383 | strbuf_expand_bad_format(start, "cat-file"); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | static void batch_write(struct batch_options *opt, const void *data, int len) |
| 388 | { |
| 389 | if (opt->buffer_output) { |
| 390 | if (fwrite(data, 1, len, stdout) != len) |
| 391 | die_errno("unable to write to stdout"); |
| 392 | } else |
| 393 | write_or_die(1, data, len); |
| 394 | } |
| 395 | |
| 396 | static void print_object_or_die(struct batch_options *opt, struct expand_data *data) |
| 397 | { |
| 398 | const struct object_id *oid = &data->oid; |
| 399 | |
| 400 | assert(data->info.typep); |
| 401 | |
| 402 | if (data->type == OBJ_BLOB) { |
| 403 | if (opt->buffer_output) |
| 404 | fflush(stdout); |
| 405 | if (opt->transform_mode) { |
| 406 | char *contents; |
| 407 | unsigned long size; |
| 408 | |
| 409 | if (!data->rest) |
| 410 | die("missing path for '%s'", oid_to_hex(oid)); |
| 411 | |
| 412 | if (opt->transform_mode == 'w') { |
| 413 | if (filter_object(data->rest, 0100644, oid, |
| 414 | &contents, &size)) |
| 415 | die("could not convert '%s' %s", |
| 416 | oid_to_hex(oid), data->rest); |
| 417 | } else if (opt->transform_mode == 'c') { |
| 418 | enum object_type type; |
| 419 | if (!textconv_object(the_repository, |
| 420 | data->rest, 0100644, oid, |
| 421 | 1, &contents, &size)) |
| 422 | contents = odb_read_object(the_repository->objects, |
| 423 | oid, &type, &size); |
| 424 | if (!contents) |
| 425 | die("could not convert '%s' %s", |
| 426 | oid_to_hex(oid), data->rest); |
| 427 | } else |
| 428 | BUG("invalid transform_mode: %c", opt->transform_mode); |
| 429 | batch_write(opt, contents, size); |
| 430 | free(contents); |
| 431 | } else { |
| 432 | stream_blob(oid); |
| 433 | } |
| 434 | } |
| 435 | else { |
| 436 | enum object_type type; |
| 437 | unsigned long size; |
| 438 | void *contents; |
| 439 | |
| 440 | contents = odb_read_object(the_repository->objects, oid, |
| 441 | &type, &size); |
| 442 | if (!contents) |
| 443 | die("object %s disappeared", oid_to_hex(oid)); |
| 444 | |
| 445 | if (use_mailmap) { |
| 446 | size_t s = size; |
| 447 | contents = replace_idents_using_mailmap(contents, &s); |
| 448 | size = cast_size_t_to_ulong(s); |
| 449 | } |
| 450 | |
| 451 | if (type != data->type) |
| 452 | die("object %s changed type!?", oid_to_hex(oid)); |
| 453 | if (data->info.sizep && size != data->size && !use_mailmap) |
| 454 | die("object %s changed size!?", oid_to_hex(oid)); |
| 455 | |
| 456 | batch_write(opt, contents, size); |
| 457 | free(contents); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | static void print_default_format(struct strbuf *scratch, struct expand_data *data, |
| 462 | struct batch_options *opt) |
| 463 | { |
| 464 | strbuf_addf(scratch, "%s %s %"PRIuMAX"%c", oid_to_hex(&data->oid), |
| 465 | type_name(data->type), |
| 466 | (uintmax_t)data->size, opt->output_delim); |
| 467 | } |
| 468 | |
| 469 | static void report_object_status(struct batch_options *opt, |
| 470 | const char *obj_name, |
| 471 | const struct object_id *oid, |
| 472 | const char *status) |
| 473 | { |
| 474 | printf("%s %s%c", obj_name ? obj_name : oid_to_hex(oid), |
| 475 | status, opt->output_delim); |
| 476 | fflush(stdout); |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * If "pack" is non-NULL, then "offset" is the byte offset within the pack from |
| 481 | * which the object may be accessed (though note that we may also rely on |
| 482 | * data->oid, too). If "pack" is NULL, then offset is ignored. |
| 483 | */ |
| 484 | static void batch_object_write(const char *obj_name, |
| 485 | struct strbuf *scratch, |
| 486 | struct batch_options *opt, |
| 487 | struct expand_data *data, |
| 488 | struct packed_git *pack, |
| 489 | off_t offset) |
| 490 | { |
| 491 | if (!data->skip_object_info) { |
| 492 | int ret; |
| 493 | |
| 494 | if (use_mailmap || |
| 495 | opt->objects_filter.choice == LOFC_BLOB_NONE || |
| 496 | opt->objects_filter.choice == LOFC_BLOB_LIMIT || |
| 497 | opt->objects_filter.choice == LOFC_OBJECT_TYPE) |
| 498 | data->info.typep = &data->type; |
| 499 | if (opt->objects_filter.choice == LOFC_BLOB_LIMIT) |
| 500 | data->info.sizep = &data->size; |
| 501 | |
| 502 | if (pack) |
| 503 | ret = packed_object_info(pack, offset, &data->info); |
| 504 | else |
| 505 | ret = odb_read_object_info_extended(the_repository->objects, |
| 506 | &data->oid, &data->info, |
| 507 | OBJECT_INFO_LOOKUP_REPLACE); |
| 508 | if (ret < 0) { |
| 509 | if (data->mode == S_IFGITLINK) |
| 510 | report_object_status(opt, NULL, &data->oid, "submodule"); |
| 511 | else |
| 512 | report_object_status(opt, obj_name, &data->oid, "missing"); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | switch (opt->objects_filter.choice) { |
| 517 | case LOFC_DISABLED: |
| 518 | break; |
| 519 | case LOFC_BLOB_NONE: |
| 520 | if (data->type == OBJ_BLOB) { |
| 521 | if (!opt->all_objects) |
| 522 | report_object_status(opt, obj_name, |
| 523 | &data->oid, "excluded"); |
| 524 | return; |
| 525 | } |
| 526 | break; |
| 527 | case LOFC_BLOB_LIMIT: |
| 528 | if (data->type == OBJ_BLOB && |
| 529 | data->size >= opt->objects_filter.blob_limit_value) { |
| 530 | if (!opt->all_objects) |
| 531 | report_object_status(opt, obj_name, |
| 532 | &data->oid, "excluded"); |
| 533 | return; |
| 534 | } |
| 535 | break; |
| 536 | case LOFC_OBJECT_TYPE: |
| 537 | if (data->type != opt->objects_filter.object_type) { |
| 538 | if (!opt->all_objects) |
| 539 | report_object_status(opt, obj_name, |
| 540 | &data->oid, "excluded"); |
| 541 | return; |
| 542 | } |
| 543 | break; |
| 544 | default: |
| 545 | BUG("unsupported objects filter"); |
| 546 | } |
| 547 | |
| 548 | if (use_mailmap && (data->type == OBJ_COMMIT || data->type == OBJ_TAG)) { |
| 549 | size_t s = data->size; |
| 550 | char *buf = NULL; |
| 551 | |
| 552 | buf = odb_read_object(the_repository->objects, &data->oid, |
| 553 | &data->type, &data->size); |
| 554 | if (!buf) |
| 555 | die(_("unable to read %s"), oid_to_hex(&data->oid)); |
| 556 | buf = replace_idents_using_mailmap(buf, &s); |
| 557 | data->size = cast_size_t_to_ulong(s); |
| 558 | |
| 559 | free(buf); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | strbuf_reset(scratch); |
| 564 | |
| 565 | if (!opt->format) { |
| 566 | print_default_format(scratch, data, opt); |
| 567 | } else { |
| 568 | expand_format(scratch, opt->format, data); |
| 569 | strbuf_addch(scratch, opt->output_delim); |
| 570 | } |
| 571 | |
| 572 | batch_write(opt, scratch->buf, scratch->len); |
| 573 | |
| 574 | if (opt->batch_mode == BATCH_MODE_CONTENTS) { |
| 575 | print_object_or_die(opt, data); |
| 576 | batch_write(opt, &opt->output_delim, 1); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | static void batch_one_object(const char *obj_name, |
| 581 | struct strbuf *scratch, |
| 582 | struct batch_options *opt, |
| 583 | struct expand_data *data) |
| 584 | { |
| 585 | struct object_context ctx = {0}; |
| 586 | int flags = |
| 587 | GET_OID_HASH_ANY | |
| 588 | (opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0); |
| 589 | enum get_oid_result result; |
| 590 | |
| 591 | result = get_oid_with_context(the_repository, obj_name, |
| 592 | flags, &data->oid, &ctx); |
| 593 | if (result != FOUND) { |
| 594 | switch (result) { |
| 595 | case MISSING_OBJECT: |
| 596 | report_object_status(opt, obj_name, &data->oid, "missing"); |
| 597 | break; |
| 598 | case SHORT_NAME_AMBIGUOUS: |
| 599 | report_object_status(opt, obj_name, &data->oid, "ambiguous"); |
| 600 | break; |
| 601 | case DANGLING_SYMLINK: |
| 602 | printf("dangling %"PRIuMAX"%c%s%c", |
| 603 | (uintmax_t)strlen(obj_name), |
| 604 | opt->output_delim, obj_name, opt->output_delim); |
| 605 | break; |
| 606 | case SYMLINK_LOOP: |
| 607 | printf("loop %"PRIuMAX"%c%s%c", |
| 608 | (uintmax_t)strlen(obj_name), |
| 609 | opt->output_delim, obj_name, opt->output_delim); |
| 610 | break; |
| 611 | case NOT_DIR: |
| 612 | printf("notdir %"PRIuMAX"%c%s%c", |
| 613 | (uintmax_t)strlen(obj_name), |
| 614 | opt->output_delim, obj_name, opt->output_delim); |
| 615 | break; |
| 616 | default: |
| 617 | BUG("unknown get_sha1_with_context result %d\n", |
| 618 | result); |
| 619 | break; |
| 620 | } |
| 621 | fflush(stdout); |
| 622 | |
| 623 | goto out; |
| 624 | } |
| 625 | |
| 626 | if (ctx.mode == 0) { |
| 627 | printf("symlink %"PRIuMAX"%c%s%c", |
| 628 | (uintmax_t)ctx.symlink_path.len, |
| 629 | opt->output_delim, ctx.symlink_path.buf, opt->output_delim); |
| 630 | fflush(stdout); |
| 631 | goto out; |
| 632 | } |
| 633 | |
| 634 | data->mode = ctx.mode; |
| 635 | batch_object_write(obj_name, scratch, opt, data, NULL, 0); |
| 636 | |
| 637 | out: |
| 638 | object_context_release(&ctx); |
| 639 | } |
| 640 | |
| 641 | struct object_cb_data { |
| 642 | struct batch_options *opt; |
| 643 | struct expand_data *expand; |
| 644 | struct oidset *seen; |
| 645 | struct strbuf *scratch; |
| 646 | }; |
| 647 | |
| 648 | static int batch_object_cb(const struct object_id *oid, void *vdata) |
| 649 | { |
| 650 | struct object_cb_data *data = vdata; |
| 651 | oidcpy(&data->expand->oid, oid); |
| 652 | batch_object_write(NULL, data->scratch, data->opt, data->expand, |
| 653 | NULL, 0); |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static int collect_object(const struct object_id *oid, |
| 658 | struct packed_git *pack UNUSED, |
| 659 | off_t offset UNUSED, |
| 660 | void *data) |
| 661 | { |
| 662 | oid_array_append(data, oid); |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static int batch_unordered_object(const struct object_id *oid, |
| 667 | struct packed_git *pack, |
| 668 | off_t offset, |
| 669 | void *vdata) |
| 670 | { |
| 671 | struct object_cb_data *data = vdata; |
| 672 | |
| 673 | if (oidset_insert(data->seen, oid)) |
| 674 | return 0; |
| 675 | |
| 676 | oidcpy(&data->expand->oid, oid); |
| 677 | batch_object_write(NULL, data->scratch, data->opt, data->expand, |
| 678 | pack, offset); |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *, |
| 683 | struct strbuf *, struct expand_data *); |
| 684 | |
| 685 | struct queued_cmd { |
| 686 | parse_cmd_fn_t fn; |
| 687 | char *line; |
| 688 | }; |
| 689 | |
| 690 | static void parse_cmd_contents(struct batch_options *opt, |
| 691 | const char *line, |
| 692 | struct strbuf *output, |
| 693 | struct expand_data *data) |
| 694 | { |
| 695 | opt->batch_mode = BATCH_MODE_CONTENTS; |
| 696 | batch_one_object(line, output, opt, data); |
| 697 | } |
| 698 | |
| 699 | static void parse_cmd_info(struct batch_options *opt, |
| 700 | const char *line, |
| 701 | struct strbuf *output, |
| 702 | struct expand_data *data) |
| 703 | { |
| 704 | opt->batch_mode = BATCH_MODE_INFO; |
| 705 | batch_one_object(line, output, opt, data); |
| 706 | } |
| 707 | |
| 708 | static void parse_cmd_mailmap(struct batch_options *opt UNUSED, |
| 709 | const char *line, |
| 710 | struct strbuf *output UNUSED, |
| 711 | struct expand_data *data UNUSED) |
| 712 | { |
| 713 | use_mailmap = git_parse_maybe_bool(line); |
| 714 | |
| 715 | if (use_mailmap < 0) |
| 716 | die(_("mailmap: invalid boolean '%s'"), line); |
| 717 | |
| 718 | if (use_mailmap) |
| 719 | load_mailmap(); |
| 720 | } |
| 721 | |
| 722 | static void dispatch_calls(struct batch_options *opt, |
| 723 | struct strbuf *output, |
| 724 | struct expand_data *data, |
| 725 | struct queued_cmd *cmd, |
| 726 | int nr) |
| 727 | { |
| 728 | int i; |
| 729 | |
| 730 | if (!opt->buffer_output) |
| 731 | die(_("flush is only for --buffer mode")); |
| 732 | |
| 733 | for (i = 0; i < nr; i++) |
| 734 | cmd[i].fn(opt, cmd[i].line, output, data); |
| 735 | |
| 736 | fflush(stdout); |
| 737 | } |
| 738 | |
| 739 | static void free_cmds(struct queued_cmd *cmd, size_t *nr) |
| 740 | { |
| 741 | size_t i; |
| 742 | |
| 743 | for (i = 0; i < *nr; i++) |
| 744 | FREE_AND_NULL(cmd[i].line); |
| 745 | |
| 746 | *nr = 0; |
| 747 | } |
| 748 | |
| 749 | |
| 750 | static const struct parse_cmd { |
| 751 | const char *name; |
| 752 | parse_cmd_fn_t fn; |
| 753 | unsigned takes_args; |
| 754 | } commands[] = { |
| 755 | { "contents", parse_cmd_contents, 1 }, |
| 756 | { "info", parse_cmd_info, 1 }, |
| 757 | { "flush", NULL, 0 }, |
| 758 | { "mailmap", parse_cmd_mailmap, 1 }, |
| 759 | }; |
| 760 | |
| 761 | static void batch_objects_command(struct batch_options *opt, |
| 762 | struct strbuf *output, |
| 763 | struct expand_data *data) |
| 764 | { |
| 765 | struct strbuf input = STRBUF_INIT; |
| 766 | struct queued_cmd *queued_cmd = NULL; |
| 767 | size_t alloc = 0, nr = 0; |
| 768 | |
| 769 | while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) { |
| 770 | int i; |
| 771 | const struct parse_cmd *cmd = NULL; |
| 772 | const char *p = NULL, *cmd_end; |
| 773 | struct queued_cmd call = {0}; |
| 774 | |
| 775 | if (!input.len) |
| 776 | die(_("empty command in input")); |
| 777 | if (isspace(*input.buf)) |
| 778 | die(_("whitespace before command: '%s'"), input.buf); |
| 779 | |
| 780 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
| 781 | if (!skip_prefix(input.buf, commands[i].name, &cmd_end)) |
| 782 | continue; |
| 783 | |
| 784 | cmd = &commands[i]; |
| 785 | if (cmd->takes_args) { |
| 786 | if (*cmd_end != ' ') |
| 787 | die(_("%s requires arguments"), |
| 788 | commands[i].name); |
| 789 | |
| 790 | p = cmd_end + 1; |
| 791 | } else if (*cmd_end) { |
| 792 | die(_("%s takes no arguments"), |
| 793 | commands[i].name); |
| 794 | } |
| 795 | |
| 796 | break; |
| 797 | } |
| 798 | |
| 799 | if (!cmd) |
| 800 | die(_("unknown command: '%s'"), input.buf); |
| 801 | |
| 802 | if (!strcmp(cmd->name, "flush")) { |
| 803 | dispatch_calls(opt, output, data, queued_cmd, nr); |
| 804 | free_cmds(queued_cmd, &nr); |
| 805 | } else if (!opt->buffer_output) { |
| 806 | cmd->fn(opt, p, output, data); |
| 807 | } else { |
| 808 | ALLOC_GROW(queued_cmd, nr + 1, alloc); |
| 809 | call.fn = cmd->fn; |
| 810 | call.line = xstrdup_or_null(p); |
| 811 | queued_cmd[nr++] = call; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | if (opt->buffer_output && |
| 816 | nr && |
| 817 | !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) { |
| 818 | dispatch_calls(opt, output, data, queued_cmd, nr); |
| 819 | free_cmds(queued_cmd, &nr); |
| 820 | } |
| 821 | |
| 822 | free_cmds(queued_cmd, &nr); |
| 823 | free(queued_cmd); |
| 824 | strbuf_release(&input); |
| 825 | } |
| 826 | |
| 827 | #define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)" |
| 828 | |
| 829 | typedef int (*for_each_object_fn)(const struct object_id *oid, struct packed_git *pack, |
| 830 | off_t offset, void *data); |
| 831 | |
| 832 | struct for_each_object_payload { |
| 833 | for_each_object_fn callback; |
| 834 | void *payload; |
| 835 | }; |
| 836 | |
| 837 | static int batch_one_object_oi(const struct object_id *oid, |
| 838 | struct object_info *oi, |
| 839 | void *_payload) |
| 840 | { |
| 841 | struct for_each_object_payload *payload = _payload; |
| 842 | if (oi && oi->whence == OI_PACKED) |
| 843 | return payload->callback(oid, oi->u.packed.pack, oi->u.packed.offset, |
| 844 | payload->payload); |
| 845 | return payload->callback(oid, NULL, 0, payload->payload); |
| 846 | } |
| 847 | |
| 848 | static int batch_one_object_packed(const struct object_id *oid, |
| 849 | struct packed_git *pack, |
| 850 | uint32_t pos, |
| 851 | void *_payload) |
| 852 | { |
| 853 | struct for_each_object_payload *payload = _payload; |
| 854 | return payload->callback(oid, pack, nth_packed_object_offset(pack, pos), |
| 855 | payload->payload); |
| 856 | } |
| 857 | |
| 858 | static int batch_one_object_bitmapped(const struct object_id *oid, |
| 859 | enum object_type type UNUSED, |
| 860 | int flags UNUSED, |
| 861 | uint32_t hash UNUSED, |
| 862 | struct packed_git *pack, |
| 863 | off_t offset, |
| 864 | void *_payload) |
| 865 | { |
| 866 | struct for_each_object_payload *payload = _payload; |
| 867 | return payload->callback(oid, pack, offset, payload->payload); |
| 868 | } |
| 869 | |
| 870 | static void batch_each_object(struct batch_options *opt, |
| 871 | for_each_object_fn callback, |
| 872 | unsigned flags, |
| 873 | void *_payload) |
| 874 | { |
| 875 | struct for_each_object_payload payload = { |
| 876 | .callback = callback, |
| 877 | .payload = _payload, |
| 878 | }; |
| 879 | struct odb_for_each_object_options opts = { |
| 880 | .flags = flags, |
| 881 | }; |
| 882 | struct bitmap_index *bitmap = NULL; |
| 883 | struct odb_source *source; |
| 884 | |
| 885 | /* |
| 886 | * TODO: we still need to tap into implementation details of the object |
| 887 | * database sources. Ideally, we should extend `odb_for_each_object()` |
| 888 | * to handle object filters itself so that we can move the filtering |
| 889 | * logic into the individual sources. |
| 890 | */ |
| 891 | odb_prepare_alternates(the_repository->objects); |
| 892 | for (source = the_repository->objects->sources; source; source = source->next) { |
| 893 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 894 | int ret = odb_source_for_each_object(&files->loose->base, NULL, batch_one_object_oi, |
| 895 | &payload, &opts); |
| 896 | if (ret) |
| 897 | break; |
| 898 | } |
| 899 | |
| 900 | if (opt->objects_filter.choice != LOFC_DISABLED && |
| 901 | (bitmap = prepare_bitmap_git(the_repository)) && |
| 902 | !for_each_bitmapped_object(bitmap, &opt->objects_filter, |
| 903 | batch_one_object_bitmapped, &payload)) { |
| 904 | struct packed_git *pack; |
| 905 | |
| 906 | repo_for_each_pack(the_repository, pack) { |
| 907 | if (bitmap_index_contains_pack(bitmap, pack) || |
| 908 | open_pack_index(pack)) |
| 909 | continue; |
| 910 | for_each_object_in_pack(pack, batch_one_object_packed, |
| 911 | &payload, flags); |
| 912 | } |
| 913 | } else { |
| 914 | struct object_info oi = { 0 }; |
| 915 | |
| 916 | for (source = the_repository->objects->sources; source; source = source->next) { |
| 917 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 918 | int ret = packfile_store_for_each_object(files->packed, &oi, |
| 919 | batch_one_object_oi, &payload, &opts); |
| 920 | if (ret) |
| 921 | break; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | free_bitmap_index(bitmap); |
| 926 | } |
| 927 | |
| 928 | static int batch_objects(struct batch_options *opt) |
| 929 | { |
| 930 | struct strbuf input = STRBUF_INIT; |
| 931 | struct strbuf output = STRBUF_INIT; |
| 932 | struct expand_data data = EXPAND_DATA_INIT; |
| 933 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 934 | int save_warning; |
| 935 | int retval = 0; |
| 936 | |
| 937 | /* |
| 938 | * Expand once with our special mark_query flag, which will prime the |
| 939 | * object_info to be handed to odb_read_object_info_extended for each |
| 940 | * object. |
| 941 | */ |
| 942 | data.mark_query = 1; |
| 943 | expand_format(&output, |
| 944 | opt->format ? opt->format : DEFAULT_FORMAT, |
| 945 | &data); |
| 946 | data.mark_query = 0; |
| 947 | strbuf_release(&output); |
| 948 | if (opt->transform_mode) |
| 949 | data.split_on_whitespace = 1; |
| 950 | |
| 951 | if (opt->format && !strcmp(opt->format, DEFAULT_FORMAT)) |
| 952 | opt->format = NULL; |
| 953 | /* |
| 954 | * If we are printing out the object, then always fill in the type, |
| 955 | * since we will want to decide whether or not to stream. |
| 956 | */ |
| 957 | if (opt->batch_mode == BATCH_MODE_CONTENTS) |
| 958 | data.info.typep = &data.type; |
| 959 | |
| 960 | if (opt->all_objects) { |
| 961 | struct object_cb_data cb; |
| 962 | struct object_info empty = OBJECT_INFO_INIT; |
| 963 | |
| 964 | if (!memcmp(&data.info, &empty, sizeof(empty)) && |
| 965 | opt->objects_filter.choice == LOFC_DISABLED) |
| 966 | data.skip_object_info = 1; |
| 967 | |
| 968 | if (repo_has_promisor_remote(the_repository)) |
| 969 | warning("This repository uses promisor remotes. Some objects may not be loaded."); |
| 970 | |
| 971 | disable_replace_refs(); |
| 972 | |
| 973 | cb.opt = opt; |
| 974 | cb.expand = &data; |
| 975 | cb.scratch = &output; |
| 976 | |
| 977 | if (opt->unordered) { |
| 978 | struct oidset seen = OIDSET_INIT; |
| 979 | |
| 980 | cb.seen = &seen; |
| 981 | |
| 982 | batch_each_object(opt, batch_unordered_object, |
| 983 | ODB_FOR_EACH_OBJECT_PACK_ORDER, &cb); |
| 984 | |
| 985 | oidset_clear(&seen); |
| 986 | } else { |
| 987 | struct oid_array sa = OID_ARRAY_INIT; |
| 988 | |
| 989 | batch_each_object(opt, collect_object, 0, &sa); |
| 990 | oid_array_for_each_unique(&sa, batch_object_cb, &cb); |
| 991 | |
| 992 | oid_array_clear(&sa); |
| 993 | } |
| 994 | |
| 995 | strbuf_release(&output); |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | /* |
| 1000 | * We are going to call get_sha1 on a potentially very large number of |
| 1001 | * objects. In most large cases, these will be actual object sha1s. The |
| 1002 | * cost to double-check that each one is not also a ref (just so we can |
| 1003 | * warn) ends up dwarfing the actual cost of the object lookups |
| 1004 | * themselves. We can work around it by just turning off the warning. |
| 1005 | */ |
| 1006 | save_warning = cfg->warn_on_object_refname_ambiguity; |
| 1007 | cfg->warn_on_object_refname_ambiguity = 0; |
| 1008 | |
| 1009 | if (opt->batch_mode == BATCH_MODE_QUEUE_AND_DISPATCH) { |
| 1010 | batch_objects_command(opt, &output, &data); |
| 1011 | goto cleanup; |
| 1012 | } |
| 1013 | |
| 1014 | while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) { |
| 1015 | if (data.split_on_whitespace) { |
| 1016 | /* |
| 1017 | * Split at first whitespace, tying off the beginning |
| 1018 | * of the string and saving the remainder (or NULL) in |
| 1019 | * data.rest. |
| 1020 | */ |
| 1021 | char *p = strpbrk(input.buf, " \t"); |
| 1022 | if (p) { |
| 1023 | while (*p && strchr(" \t", *p)) |
| 1024 | *p++ = '\0'; |
| 1025 | } |
| 1026 | data.rest = p; |
| 1027 | } |
| 1028 | |
| 1029 | batch_one_object(input.buf, &output, opt, &data); |
| 1030 | } |
| 1031 | |
| 1032 | cleanup: |
| 1033 | strbuf_release(&input); |
| 1034 | strbuf_release(&output); |
| 1035 | cfg->warn_on_object_refname_ambiguity = save_warning; |
| 1036 | return retval; |
| 1037 | } |
| 1038 | |
| 1039 | static int git_cat_file_config(const char *var, const char *value, |
| 1040 | const struct config_context *ctx, void *cb) |
| 1041 | { |
| 1042 | if (userdiff_config(var, value) < 0) |
| 1043 | return -1; |
| 1044 | |
| 1045 | return git_default_config(var, value, ctx, cb); |
| 1046 | } |
| 1047 | |
| 1048 | static int batch_option_callback(const struct option *opt, |
| 1049 | const char *arg, |
| 1050 | int unset) |
| 1051 | { |
| 1052 | struct batch_options *bo = opt->value; |
| 1053 | |
| 1054 | BUG_ON_OPT_NEG(unset); |
| 1055 | |
| 1056 | if (bo->enabled) { |
| 1057 | return error(_("only one batch option may be specified")); |
| 1058 | } |
| 1059 | |
| 1060 | bo->enabled = 1; |
| 1061 | |
| 1062 | if (!strcmp(opt->long_name, "batch")) |
| 1063 | bo->batch_mode = BATCH_MODE_CONTENTS; |
| 1064 | else if (!strcmp(opt->long_name, "batch-check")) |
| 1065 | bo->batch_mode = BATCH_MODE_INFO; |
| 1066 | else if (!strcmp(opt->long_name, "batch-command")) |
| 1067 | bo->batch_mode = BATCH_MODE_QUEUE_AND_DISPATCH; |
| 1068 | else |
| 1069 | BUG("%s given to batch-option-callback", opt->long_name); |
| 1070 | |
| 1071 | bo->format = arg; |
| 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | int cmd_cat_file(int argc, |
| 1077 | const char **argv, |
| 1078 | const char *prefix, |
| 1079 | struct repository *repo UNUSED) |
| 1080 | { |
| 1081 | int opt = 0; |
| 1082 | int opt_cw = 0; |
| 1083 | int opt_epts = 0; |
| 1084 | const char *exp_type = NULL, *obj_name = NULL; |
| 1085 | struct batch_options batch = { |
| 1086 | .objects_filter = LIST_OBJECTS_FILTER_INIT, |
| 1087 | }; |
| 1088 | int unknown_type = 0; |
| 1089 | int input_nul_terminated = 0; |
| 1090 | int nul_terminated = 0; |
| 1091 | int ret; |
| 1092 | |
| 1093 | const char * const builtin_catfile_usage[] = { |
| 1094 | N_("git cat-file <type> <object>"), |
| 1095 | N_("git cat-file (-e | -p | -t | -s) <object>"), |
| 1096 | N_("git cat-file (--textconv | --filters)\n" |
| 1097 | " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"), |
| 1098 | N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n" |
| 1099 | " [--buffer] [--follow-symlinks] [--unordered]\n" |
| 1100 | " [--textconv | --filters] [-Z]"), |
| 1101 | NULL |
| 1102 | }; |
| 1103 | const struct option options[] = { |
| 1104 | /* Simple queries */ |
| 1105 | OPT_GROUP(N_("Check object existence or emit object contents")), |
| 1106 | OPT_CMDMODE('e', NULL, &opt, |
| 1107 | N_("check if <object> exists"), 'e'), |
| 1108 | OPT_CMDMODE('p', NULL, &opt, N_("pretty-print <object> content"), 'p'), |
| 1109 | |
| 1110 | OPT_GROUP(N_("Emit [broken] object attributes")), |
| 1111 | OPT_CMDMODE('t', NULL, &opt, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'), |
| 1112 | OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'), |
| 1113 | OPT_HIDDEN_BOOL(0, "allow-unknown-type", &unknown_type, |
| 1114 | N_("historical option -- no-op")), |
| 1115 | OPT_BOOL(0, "use-mailmap", &use_mailmap, N_("use mail map file")), |
| 1116 | OPT_ALIAS(0, "mailmap", "use-mailmap"), |
| 1117 | /* Batch mode */ |
| 1118 | OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")), |
| 1119 | OPT_CALLBACK_F(0, "batch", &batch, N_("format"), |
| 1120 | N_("show full <object> or <rev> contents"), |
| 1121 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, |
| 1122 | batch_option_callback), |
| 1123 | OPT_CALLBACK_F(0, "batch-check", &batch, N_("format"), |
| 1124 | N_("like --batch, but don't emit <contents>"), |
| 1125 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, |
| 1126 | batch_option_callback), |
| 1127 | OPT_BOOL_F('z', NULL, &input_nul_terminated, N_("stdin is NUL-terminated"), |
| 1128 | PARSE_OPT_HIDDEN), |
| 1129 | OPT_BOOL('Z', NULL, &nul_terminated, N_("stdin and stdout is NUL-terminated")), |
| 1130 | OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"), |
| 1131 | N_("read commands from stdin"), |
| 1132 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, |
| 1133 | batch_option_callback), |
| 1134 | OPT_CMDMODE(0, "batch-all-objects", &opt, |
| 1135 | N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'), |
| 1136 | /* Batch-specific options */ |
| 1137 | OPT_GROUP(N_("Change or optimize batch output")), |
| 1138 | OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")), |
| 1139 | OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks, |
| 1140 | N_("follow in-tree symlinks")), |
| 1141 | OPT_BOOL(0, "unordered", &batch.unordered, |
| 1142 | N_("do not order objects before emitting them")), |
| 1143 | /* Textconv options, stand-ole*/ |
| 1144 | OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")), |
| 1145 | OPT_CMDMODE(0, "textconv", &opt, |
| 1146 | N_("run textconv on object's content"), 'c'), |
| 1147 | OPT_CMDMODE(0, "filters", &opt, |
| 1148 | N_("run filters on object's content"), 'w'), |
| 1149 | OPT_STRING(0, "path", &force_path, N_("blob|tree"), |
| 1150 | N_("use a <path> for (--textconv | --filters); Not with 'batch'")), |
| 1151 | OPT_PARSE_LIST_OBJECTS_FILTER(&batch.objects_filter), |
| 1152 | OPT_END() |
| 1153 | }; |
| 1154 | |
| 1155 | repo_config(the_repository, git_cat_file_config, NULL); |
| 1156 | |
| 1157 | batch.buffer_output = -1; |
| 1158 | |
| 1159 | argc = parse_options(argc, argv, prefix, options, builtin_catfile_usage, 0); |
| 1160 | opt_cw = (opt == 'c' || opt == 'w'); |
| 1161 | opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's'); |
| 1162 | |
| 1163 | if (use_mailmap) |
| 1164 | load_mailmap(); |
| 1165 | |
| 1166 | switch (batch.objects_filter.choice) { |
| 1167 | case LOFC_DISABLED: |
| 1168 | break; |
| 1169 | case LOFC_BLOB_NONE: |
| 1170 | case LOFC_BLOB_LIMIT: |
| 1171 | case LOFC_OBJECT_TYPE: |
| 1172 | if (!batch.enabled) |
| 1173 | usage(_("objects filter only supported in batch mode")); |
| 1174 | break; |
| 1175 | default: |
| 1176 | usagef(_("objects filter not supported: '%s'"), |
| 1177 | list_object_filter_config_name(batch.objects_filter.choice)); |
| 1178 | } |
| 1179 | |
| 1180 | /* --batch-all-objects? */ |
| 1181 | if (opt == 'b') |
| 1182 | batch.all_objects = 1; |
| 1183 | |
| 1184 | /* Option compatibility */ |
| 1185 | if (force_path && !opt_cw) |
| 1186 | usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"), |
| 1187 | builtin_catfile_usage, options, |
| 1188 | "--path", _("path|tree-ish"), "--filters", |
| 1189 | "--textconv"); |
| 1190 | |
| 1191 | /* Option compatibility with batch mode */ |
| 1192 | if (batch.enabled) |
| 1193 | ; |
| 1194 | else if (batch.follow_symlinks) |
| 1195 | usage_msg_optf(_("'%s' requires a batch mode"), builtin_catfile_usage, |
| 1196 | options, "--follow-symlinks"); |
| 1197 | else if (batch.buffer_output >= 0) |
| 1198 | usage_msg_optf(_("'%s' requires a batch mode"), builtin_catfile_usage, |
| 1199 | options, "--buffer"); |
| 1200 | else if (batch.all_objects) |
| 1201 | usage_msg_optf(_("'%s' requires a batch mode"), builtin_catfile_usage, |
| 1202 | options, "--batch-all-objects"); |
| 1203 | else if (input_nul_terminated) |
| 1204 | usage_msg_optf(_("'%s' requires a batch mode"), builtin_catfile_usage, |
| 1205 | options, "-z"); |
| 1206 | else if (nul_terminated) |
| 1207 | usage_msg_optf(_("'%s' requires a batch mode"), builtin_catfile_usage, |
| 1208 | options, "-Z"); |
| 1209 | |
| 1210 | batch.input_delim = batch.output_delim = '\n'; |
| 1211 | if (input_nul_terminated) |
| 1212 | batch.input_delim = '\0'; |
| 1213 | if (nul_terminated) |
| 1214 | batch.input_delim = batch.output_delim = '\0'; |
| 1215 | |
| 1216 | /* Batch defaults */ |
| 1217 | if (batch.buffer_output < 0) |
| 1218 | batch.buffer_output = batch.all_objects; |
| 1219 | |
| 1220 | prepare_repo_settings(the_repository); |
| 1221 | the_repository->settings.command_requires_full_index = 0; |
| 1222 | |
| 1223 | /* Return early if we're in batch mode? */ |
| 1224 | if (batch.enabled) { |
| 1225 | if (opt_cw) |
| 1226 | batch.transform_mode = opt; |
| 1227 | else if (opt && opt != 'b') |
| 1228 | usage_msg_optf(_("'-%c' is incompatible with batch mode"), |
| 1229 | builtin_catfile_usage, options, opt); |
| 1230 | else if (argc) |
| 1231 | usage_msg_opt(_("batch modes take no arguments"), |
| 1232 | builtin_catfile_usage, options); |
| 1233 | |
| 1234 | ret = batch_objects(&batch); |
| 1235 | goto out; |
| 1236 | } |
| 1237 | |
| 1238 | if (opt) { |
| 1239 | if (!argc && opt == 'c') |
| 1240 | usage_msg_optf(_("<rev> required with '%s'"), |
| 1241 | builtin_catfile_usage, options, |
| 1242 | "--textconv"); |
| 1243 | else if (!argc && opt == 'w') |
| 1244 | usage_msg_optf(_("<rev> required with '%s'"), |
| 1245 | builtin_catfile_usage, options, |
| 1246 | "--filters"); |
| 1247 | else if (!argc && opt_epts) |
| 1248 | usage_msg_optf(_("<object> required with '-%c'"), |
| 1249 | builtin_catfile_usage, options, opt); |
| 1250 | else if (argc == 1) |
| 1251 | obj_name = argv[0]; |
| 1252 | else |
| 1253 | usage_msg_opt(_("too many arguments"), builtin_catfile_usage, |
| 1254 | options); |
| 1255 | } else if (!argc) { |
| 1256 | usage_with_options(builtin_catfile_usage, options); |
| 1257 | } else if (argc != 2) { |
| 1258 | usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"), |
| 1259 | builtin_catfile_usage, options, argc); |
| 1260 | } else if (argc) { |
| 1261 | exp_type = argv[0]; |
| 1262 | obj_name = argv[1]; |
| 1263 | } |
| 1264 | |
| 1265 | ret = cat_one_file(opt, exp_type, obj_name); |
| 1266 | |
| 1267 | out: |
| 1268 | list_objects_filter_release(&batch.objects_filter); |
| 1269 | return ret; |
| 1270 | } |