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