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