Git fork
at reftables-rust 640 lines 17 kB view raw
1#define USE_THE_REPOSITORY_VARIABLE 2#define DISABLE_SIGN_COMPARE_WARNINGS 3 4#include "git-compat-util.h" 5#include "lockfile.h" 6#include "bundle.h" 7#include "environment.h" 8#include "gettext.h" 9#include "hex.h" 10#include "odb.h" 11#include "repository.h" 12#include "object.h" 13#include "commit.h" 14#include "diff.h" 15#include "revision.h" 16#include "list-objects.h" 17#include "run-command.h" 18#include "refs.h" 19#include "strvec.h" 20#include "list-objects-filter-options.h" 21#include "connected.h" 22#include "write-or-die.h" 23 24static const char v2_bundle_signature[] = "# v2 git bundle\n"; 25static const char v3_bundle_signature[] = "# v3 git bundle\n"; 26static struct { 27 int version; 28 const char *signature; 29} bundle_sigs[] = { 30 { 2, v2_bundle_signature }, 31 { 3, v3_bundle_signature }, 32}; 33 34void bundle_header_init(struct bundle_header *header) 35{ 36 struct bundle_header blank = BUNDLE_HEADER_INIT; 37 memcpy(header, &blank, sizeof(*header)); 38} 39 40void bundle_header_release(struct bundle_header *header) 41{ 42 string_list_clear(&header->prerequisites, 1); 43 string_list_clear(&header->references, 1); 44 list_objects_filter_release(&header->filter); 45} 46 47static int parse_capability(struct bundle_header *header, const char *capability) 48{ 49 const char *arg; 50 if (skip_prefix(capability, "object-format=", &arg)) { 51 int algo = hash_algo_by_name(arg); 52 if (algo == GIT_HASH_UNKNOWN) 53 return error(_("unrecognized bundle hash algorithm: %s"), arg); 54 header->hash_algo = &hash_algos[algo]; 55 return 0; 56 } 57 if (skip_prefix(capability, "filter=", &arg)) { 58 parse_list_objects_filter(&header->filter, arg); 59 return 0; 60 } 61 return error(_("unknown capability '%s'"), capability); 62} 63 64static int parse_bundle_signature(struct bundle_header *header, const char *line) 65{ 66 int i; 67 68 for (i = 0; i < ARRAY_SIZE(bundle_sigs); i++) { 69 if (!strcmp(line, bundle_sigs[i].signature)) { 70 header->version = bundle_sigs[i].version; 71 return 0; 72 } 73 } 74 return -1; 75} 76 77int read_bundle_header_fd(int fd, struct bundle_header *header, 78 const char *report_path) 79{ 80 struct strbuf buf = STRBUF_INIT; 81 int status = 0; 82 83 /* The bundle header begins with the signature */ 84 if (strbuf_getwholeline_fd(&buf, fd, '\n') || 85 parse_bundle_signature(header, buf.buf)) { 86 if (report_path) 87 error(_("'%s' does not look like a v2 or v3 bundle file"), 88 report_path); 89 status = -1; 90 goto abort; 91 } 92 93 /* 94 * The default hash format for bundles is SHA1, unless told otherwise 95 * by an "object-format=" capability, which is being handled in 96 * `parse_capability()`. 97 */ 98 header->hash_algo = &hash_algos[GIT_HASH_SHA1_LEGACY]; 99 100 /* The bundle header ends with an empty line */ 101 while (!strbuf_getwholeline_fd(&buf, fd, '\n') && 102 buf.len && buf.buf[0] != '\n') { 103 struct object_id oid; 104 int is_prereq = 0; 105 const char *p; 106 107 strbuf_rtrim(&buf); 108 109 if (header->version == 3 && *buf.buf == '@') { 110 if (parse_capability(header, buf.buf + 1)) { 111 status = -1; 112 break; 113 } 114 continue; 115 } 116 117 if (*buf.buf == '-') { 118 is_prereq = 1; 119 strbuf_remove(&buf, 0, 1); 120 } 121 122 /* 123 * Tip lines have object name, SP, and refname. 124 * Prerequisites have object name that is optionally 125 * followed by SP and subject line. 126 */ 127 if (parse_oid_hex_algop(buf.buf, &oid, &p, header->hash_algo) || 128 (*p && !isspace(*p)) || 129 (!is_prereq && !*p)) { 130 if (report_path) 131 error(_("unrecognized header: %s%s (%d)"), 132 (is_prereq ? "-" : ""), buf.buf, (int)buf.len); 133 status = -1; 134 break; 135 } else { 136 struct object_id *dup = oiddup(&oid); 137 if (is_prereq) 138 string_list_append(&header->prerequisites, "")->util = dup; 139 else 140 string_list_append(&header->references, p + 1)->util = dup; 141 } 142 } 143 144 abort: 145 if (status) { 146 close(fd); 147 fd = -1; 148 } 149 strbuf_release(&buf); 150 return fd; 151} 152 153int read_bundle_header(const char *path, struct bundle_header *header) 154{ 155 int fd = open(path, O_RDONLY); 156 157 if (fd < 0) 158 return error(_("could not open '%s'"), path); 159 return read_bundle_header_fd(fd, header, path); 160} 161 162int is_bundle(const char *path, int quiet) 163{ 164 struct bundle_header header = BUNDLE_HEADER_INIT; 165 int fd = open(path, O_RDONLY); 166 167 if (fd < 0) 168 return 0; 169 fd = read_bundle_header_fd(fd, &header, quiet ? NULL : path); 170 if (fd >= 0) 171 close(fd); 172 bundle_header_release(&header); 173 return (fd >= 0); 174} 175 176static int list_refs(struct string_list *r, int argc, const char **argv) 177{ 178 int i; 179 180 for (i = 0; i < r->nr; i++) { 181 struct object_id *oid; 182 const char *name; 183 184 if (argc > 1) { 185 int j; 186 for (j = 1; j < argc; j++) 187 if (!strcmp(r->items[i].string, argv[j])) 188 break; 189 if (j == argc) 190 continue; 191 } 192 193 oid = r->items[i].util; 194 name = r->items[i].string; 195 printf("%s %s\n", oid_to_hex(oid), name); 196 } 197 return 0; 198} 199 200/* Remember to update object flag allocation in object.h */ 201#define PREREQ_MARK (1u<<16) 202 203struct string_list_iterator { 204 struct string_list *list; 205 size_t cur; 206}; 207 208static const struct object_id *iterate_ref_map(void *cb_data) 209{ 210 struct string_list_iterator *iter = cb_data; 211 212 if (iter->cur >= iter->list->nr) 213 return NULL; 214 215 return iter->list->items[iter->cur++].util; 216} 217 218int verify_bundle(struct repository *r, 219 struct bundle_header *header, 220 enum verify_bundle_flags flags) 221{ 222 /* 223 * Do fast check, then if any prereqs are missing then go line by line 224 * to be verbose about the errors 225 */ 226 struct string_list *p = &header->prerequisites; 227 int i, ret = 0; 228 const char *message = _("Repository lacks these prerequisite commits:"); 229 struct string_list_iterator iter = { 230 .list = p, 231 }; 232 struct check_connected_options opts = { 233 .quiet = 1, 234 }; 235 236 if (!r || !r->objects || !r->objects->sources) 237 return error(_("need a repository to verify a bundle")); 238 239 for (i = 0; i < p->nr; i++) { 240 struct string_list_item *e = p->items + i; 241 const char *name = e->string; 242 struct object_id *oid = e->util; 243 struct object *o = parse_object(r, oid); 244 if (o) 245 continue; 246 ret++; 247 if (flags & VERIFY_BUNDLE_QUIET) 248 continue; 249 if (ret == 1) 250 error("%s", message); 251 error("%s %s", oid_to_hex(oid), name); 252 } 253 if (ret) 254 goto cleanup; 255 256 if ((ret = check_connected(iterate_ref_map, &iter, &opts))) 257 error(_("some prerequisite commits exist in the object store, " 258 "but are not connected to the repository's history")); 259 260 /* TODO: preserve this verbose language. */ 261 if (flags & VERIFY_BUNDLE_VERBOSE) { 262 struct string_list *r; 263 264 r = &header->references; 265 printf_ln(Q_("The bundle contains this ref:", 266 "The bundle contains these %"PRIuMAX" refs:", 267 r->nr), 268 (uintmax_t)r->nr); 269 list_refs(r, 0, NULL); 270 271 r = &header->prerequisites; 272 if (!r->nr) { 273 printf_ln(_("The bundle records a complete history.")); 274 } else { 275 printf_ln(Q_("The bundle requires this ref:", 276 "The bundle requires these %"PRIuMAX" refs:", 277 r->nr), 278 (uintmax_t)r->nr); 279 list_refs(r, 0, NULL); 280 } 281 282 printf_ln(_("The bundle uses this hash algorithm: %s"), 283 header->hash_algo->name); 284 if (header->filter.choice) 285 printf_ln(_("The bundle uses this filter: %s"), 286 list_objects_filter_spec(&header->filter)); 287 } 288cleanup: 289 return ret; 290} 291 292int list_bundle_refs(struct bundle_header *header, int argc, const char **argv) 293{ 294 return list_refs(&header->references, argc, argv); 295} 296 297static int is_tag_in_date_range(struct object *tag, struct rev_info *revs) 298{ 299 unsigned long size; 300 enum object_type type; 301 char *buf = NULL, *line, *lineend; 302 timestamp_t date; 303 int result = 1; 304 305 if (revs->max_age == -1 && revs->min_age == -1) 306 goto out; 307 308 buf = odb_read_object(the_repository->objects, &tag->oid, &type, &size); 309 if (!buf) 310 goto out; 311 line = memmem(buf, size, "\ntagger ", 8); 312 if (!line++) 313 goto out; 314 lineend = memchr(line, '\n', buf + size - line); 315 line = memchr(line, '>', lineend ? lineend - line : buf + size - line); 316 if (!line++) 317 goto out; 318 date = parse_timestamp(line, NULL, 10); 319 result = (revs->max_age == -1 || revs->max_age < date) && 320 (revs->min_age == -1 || revs->min_age > date); 321out: 322 free(buf); 323 return result; 324} 325 326 327/* Write the pack data to bundle_fd */ 328static int write_pack_data(int bundle_fd, struct rev_info *revs, struct strvec *pack_options) 329{ 330 struct child_process pack_objects = CHILD_PROCESS_INIT; 331 int i; 332 333 strvec_pushl(&pack_objects.args, 334 "pack-objects", 335 "--stdout", "--thin", "--delta-base-offset", 336 NULL); 337 strvec_pushv(&pack_objects.args, pack_options->v); 338 if (revs->filter.choice) 339 strvec_pushf(&pack_objects.args, "--filter=%s", 340 list_objects_filter_spec(&revs->filter)); 341 pack_objects.in = -1; 342 pack_objects.out = bundle_fd; 343 pack_objects.git_cmd = 1; 344 345 /* 346 * start_command() will close our descriptor if it's >1. Duplicate it 347 * to avoid surprising the caller. 348 */ 349 if (pack_objects.out > 1) { 350 pack_objects.out = dup(pack_objects.out); 351 if (pack_objects.out < 0) { 352 error_errno(_("unable to dup bundle descriptor")); 353 child_process_clear(&pack_objects); 354 return -1; 355 } 356 } 357 358 if (start_command(&pack_objects)) 359 return error(_("Could not spawn pack-objects")); 360 361 for (i = 0; i < revs->pending.nr; i++) { 362 struct object *object = revs->pending.objects[i].item; 363 if (object->flags & UNINTERESTING) 364 write_or_die(pack_objects.in, "^", 1); 365 write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz); 366 write_or_die(pack_objects.in, "\n", 1); 367 } 368 close(pack_objects.in); 369 if (finish_command(&pack_objects)) 370 return error(_("pack-objects died")); 371 return 0; 372} 373 374/* 375 * Write out bundle refs based on the tips already 376 * parsed into revs.pending. As a side effect, may 377 * manipulate revs.pending to include additional 378 * necessary objects (like tags). 379 * 380 * Returns the number of refs written, or negative 381 * on error. 382 */ 383static int write_bundle_refs(int bundle_fd, struct rev_info *revs) 384{ 385 int i; 386 int ref_count = 0; 387 struct strset objects = STRSET_INIT; 388 389 for (i = 0; i < revs->pending.nr; i++) { 390 struct object_array_entry *e = revs->pending.objects + i; 391 struct object_id oid; 392 char *ref; 393 const char *display_ref; 394 int flag; 395 396 if (e->item->flags & UNINTERESTING) 397 continue; 398 if (repo_dwim_ref(the_repository, e->name, strlen(e->name), 399 &oid, &ref, 0) != 1) 400 goto skip_write_ref; 401 if (refs_read_ref_full(get_main_ref_store(the_repository), e->name, RESOLVE_REF_READING, &oid, &flag)) 402 flag = 0; 403 display_ref = (flag & REF_ISSYMREF) ? e->name : ref; 404 405 if (strset_contains(&objects, display_ref)) 406 goto skip_write_ref; 407 408 if (e->item->type == OBJ_TAG && 409 !is_tag_in_date_range(e->item, revs)) { 410 e->item->flags |= UNINTERESTING; 411 goto skip_write_ref; 412 } 413 414 /* 415 * Make sure the refs we wrote out is correct; --max-count and 416 * other limiting options could have prevented all the tips 417 * from getting output. 418 * 419 * Non commit objects such as tags and blobs do not have 420 * this issue as they are not affected by those extra 421 * constraints. 422 */ 423 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) { 424 warning(_("ref '%s' is excluded by the rev-list options"), 425 e->name); 426 goto skip_write_ref; 427 } 428 429 ref_count++; 430 strset_add(&objects, display_ref); 431 write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz); 432 write_or_die(bundle_fd, " ", 1); 433 write_or_die(bundle_fd, display_ref, strlen(display_ref)); 434 write_or_die(bundle_fd, "\n", 1); 435 skip_write_ref: 436 free(ref); 437 } 438 439 strset_clear(&objects); 440 441 /* end header */ 442 write_or_die(bundle_fd, "\n", 1); 443 return ref_count; 444} 445 446struct bundle_prerequisites_info { 447 struct object_array *pending; 448 int fd; 449}; 450 451static void write_bundle_prerequisites(struct commit *commit, void *data) 452{ 453 struct bundle_prerequisites_info *bpi = data; 454 struct object *object; 455 struct pretty_print_context ctx = { 0 }; 456 struct strbuf buf = STRBUF_INIT; 457 458 if (!(commit->object.flags & BOUNDARY)) 459 return; 460 strbuf_addf(&buf, "-%s ", oid_to_hex(&commit->object.oid)); 461 write_or_die(bpi->fd, buf.buf, buf.len); 462 463 ctx.fmt = CMIT_FMT_ONELINE; 464 ctx.output_encoding = get_log_output_encoding(); 465 strbuf_reset(&buf); 466 pretty_print_commit(&ctx, commit, &buf); 467 strbuf_trim(&buf); 468 469 object = (struct object *)commit; 470 object->flags |= UNINTERESTING; 471 add_object_array_with_path(object, buf.buf, bpi->pending, S_IFINVALID, 472 NULL); 473 strbuf_addch(&buf, '\n'); 474 write_or_die(bpi->fd, buf.buf, buf.len); 475 strbuf_release(&buf); 476} 477 478int create_bundle(struct repository *r, const char *path, 479 int argc, const char **argv, struct strvec *pack_options, int version) 480{ 481 struct lock_file lock = LOCK_INIT; 482 int bundle_fd = -1; 483 int bundle_to_stdout; 484 int ref_count = 0; 485 struct rev_info revs, revs_copy; 486 int min_version = 2; 487 struct bundle_prerequisites_info bpi; 488 int ret; 489 int i; 490 491 /* init revs to list objects for pack-objects later */ 492 save_commit_buffer = 0; 493 repo_init_revisions(r, &revs, NULL); 494 495 /* 496 * Pre-initialize the '--objects' flag so we can parse a 497 * --filter option successfully. 498 */ 499 revs.tree_objects = revs.blob_objects = 1; 500 501 argc = setup_revisions(argc, argv, &revs, NULL); 502 503 /* 504 * Reasons to require version 3: 505 * 506 * 1. @object-format is required because our hash algorithm is not 507 * SHA1. 508 * 2. @filter is required because we parsed an object filter. 509 */ 510 if (the_hash_algo != &hash_algos[GIT_HASH_SHA1_LEGACY] || revs.filter.choice) 511 min_version = 3; 512 513 if (argc > 1) { 514 ret = error(_("unrecognized argument: %s"), argv[1]); 515 goto out; 516 } 517 518 bundle_to_stdout = !strcmp(path, "-"); 519 if (bundle_to_stdout) 520 bundle_fd = 1; 521 else 522 bundle_fd = hold_lock_file_for_update(&lock, path, 523 LOCK_DIE_ON_ERROR); 524 525 if (version == -1) 526 version = min_version; 527 528 if (version < 2 || version > 3) { 529 die(_("unsupported bundle version %d"), version); 530 } else if (version < min_version) { 531 die(_("cannot write bundle version %d with algorithm %s"), version, the_hash_algo->name); 532 } else if (version == 2) { 533 write_or_die(bundle_fd, v2_bundle_signature, strlen(v2_bundle_signature)); 534 } else { 535 const char *capability = "@object-format="; 536 write_or_die(bundle_fd, v3_bundle_signature, strlen(v3_bundle_signature)); 537 write_or_die(bundle_fd, capability, strlen(capability)); 538 write_or_die(bundle_fd, the_hash_algo->name, strlen(the_hash_algo->name)); 539 write_or_die(bundle_fd, "\n", 1); 540 541 if (revs.filter.choice) { 542 const char *value = expand_list_objects_filter_spec(&revs.filter); 543 capability = "@filter="; 544 write_or_die(bundle_fd, capability, strlen(capability)); 545 write_or_die(bundle_fd, value, strlen(value)); 546 write_or_die(bundle_fd, "\n", 1); 547 } 548 } 549 550 /* save revs.pending in revs_copy for later use */ 551 memcpy(&revs_copy, &revs, sizeof(revs)); 552 revs_copy.pending.nr = 0; 553 revs_copy.pending.alloc = 0; 554 revs_copy.pending.objects = NULL; 555 for (i = 0; i < revs.pending.nr; i++) { 556 struct object_array_entry *e = revs.pending.objects + i; 557 if (e) 558 add_object_array_with_path(e->item, e->name, 559 &revs_copy.pending, 560 e->mode, e->path); 561 } 562 563 /* write prerequisites */ 564 revs.boundary = 1; 565 if (prepare_revision_walk(&revs)) 566 die("revision walk setup failed"); 567 bpi.fd = bundle_fd; 568 bpi.pending = &revs_copy.pending; 569 570 /* 571 * Remove any object walking here. We only care about commits and 572 * tags here. The revs_copy has the right instances of these values. 573 */ 574 revs.blob_objects = revs.tree_objects = 0; 575 traverse_commit_list(&revs, write_bundle_prerequisites, NULL, &bpi); 576 577 /* write bundle refs */ 578 ref_count = write_bundle_refs(bundle_fd, &revs_copy); 579 if (!ref_count) { 580 die(_("Refusing to create empty bundle.")); 581 } else if (ref_count < 0) { 582 ret = -1; 583 goto out; 584 } 585 586 /* write pack */ 587 if (write_pack_data(bundle_fd, &revs_copy, pack_options)) { 588 ret = -1; 589 goto out; 590 } 591 592 if (!bundle_to_stdout) { 593 if (commit_lock_file(&lock)) 594 die_errno(_("cannot create '%s'"), path); 595 } 596 597 ret = 0; 598 599out: 600 object_array_clear(&revs_copy.pending); 601 release_revisions(&revs); 602 rollback_lock_file(&lock); 603 return ret; 604} 605 606int unbundle(struct repository *r, struct bundle_header *header, 607 int bundle_fd, struct strvec *extra_index_pack_args, 608 struct unbundle_opts *opts) 609{ 610 struct child_process ip = CHILD_PROCESS_INIT; 611 struct unbundle_opts opts_fallback = { 0 }; 612 613 if (!opts) 614 opts = &opts_fallback; 615 616 if (verify_bundle(r, header, opts->flags)) { 617 close(bundle_fd); 618 return -1; 619 } 620 621 strvec_pushl(&ip.args, "index-pack", "--fix-thin", "--stdin", NULL); 622 623 /* If there is a filter, then we need to create the promisor pack. */ 624 if (header->filter.choice) 625 strvec_push(&ip.args, "--promisor=from-bundle"); 626 627 if (opts->flags & VERIFY_BUNDLE_FSCK) 628 strvec_pushf(&ip.args, "--fsck-objects%s", 629 opts->fsck_msg_types ? opts->fsck_msg_types : ""); 630 631 if (extra_index_pack_args) 632 strvec_pushv(&ip.args, extra_index_pack_args->v); 633 634 ip.in = bundle_fd; 635 ip.no_stdout = 1; 636 ip.git_cmd = 1; 637 if (run_command(&ip)) 638 return error(_("index-pack died")); 639 return 0; 640}