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/fetch-pack.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 / fetch-pack.c` 2355 lines ยท 67521 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 #define DISABLE_SIGN_COMPARE_WARNINGS 3 4 #include "git-compat-util.h" 5 #include "repository.h" 6 #include "config.h" 7 #include "date.h" 8 #include "environment.h" 9 #include "gettext.h" 10 #include "hex.h" 11 #include "lockfile.h" 12 #include "refs.h" 13 #include "pkt-line.h" 14 #include "commit.h" 15 #include "tag.h" 16 #include "pack.h" 17 #include "sideband.h" 18 #include "fetch-pack.h" 19 #include "remote.h" 20 #include "run-command.h" 21 #include "connect.h" 22 #include "trace2.h" 23 #include "version.h" 24 #include "oid-array.h" 25 #include "oidset.h" 26 #include "packfile.h" 27 #include "odb.h" 28 #include "object-name.h" 29 #include "path.h" 30 #include "connected.h" 31 #include "fetch-negotiator.h" 32 #include "fsck.h" 33 #include "shallow.h" 34 #include "commit-reach.h" 35 #include "commit-graph.h" 36 #include "sigchain.h" 37 #include "mergesort.h" 38 #include "prio-queue.h" 39 #include "promisor-remote.h" 40 41 static int transfer_unpack_limit = -1; 42 static int fetch_unpack_limit = -1; 43 static int unpack_limit = 100; 44 static int prefer_ofs_delta = 1; 45 static int no_done; 46 static int deepen_since_ok; 47 static int deepen_not_ok; 48 static int fetch_fsck_objects = -1; 49 static int transfer_fsck_objects = -1; 50 static int agent_supported; 51 static int server_supports_filtering; 52 static struct shallow_lock shallow_lock; 53 static const char *alternate_shallow_file; 54 static struct strbuf fsck_msg_types = STRBUF_INIT; 55 static struct string_list uri_protocols = STRING_LIST_INIT_DUP; 56 57 /* Remember to update object flag allocation in object.h */ 58 #define COMPLETE (1U << 0) 59 #define ALTERNATE (1U << 1) 60 #define COMMON (1U << 6) 61 #define REACH_SCRATCH (1U << 7) 62 63 /* 64 * After sending this many "have"s if we do not get any new ACK , we 65 * give up traversing our history. 66 */ 67 #define MAX_IN_VAIN 256 68 69 static int multi_ack, use_sideband; 70 /* Allow specifying sha1 if it is a ref tip. */ 71 #define ALLOW_TIP_SHA1 01 72 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */ 73 #define ALLOW_REACHABLE_SHA1 02 74 static unsigned int allow_unadvertised_object_request; 75 76 __attribute__((format (printf, 2, 3))) 77 static inline void print_verbose(const struct fetch_pack_args *args, 78 const char *fmt, ...) 79 { 80 va_list params; 81 82 if (!args->verbose) 83 return; 84 85 va_start(params, fmt); 86 vfprintf(stderr, fmt, params); 87 va_end(params); 88 fputc('\n', stderr); 89 } 90 91 struct alternate_object_cache { 92 struct object **items; 93 size_t nr, alloc; 94 }; 95 96 static void cache_one_alternate(const struct object_id *oid, 97 void *vcache) 98 { 99 struct alternate_object_cache *cache = vcache; 100 struct object *obj = parse_object(the_repository, oid); 101 102 if (!obj || (obj->flags & ALTERNATE)) 103 return; 104 105 obj->flags |= ALTERNATE; 106 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc); 107 cache->items[cache->nr++] = obj; 108 } 109 110 static void for_each_cached_alternate(struct fetch_negotiator *negotiator, 111 void (*cb)(struct fetch_negotiator *, 112 struct object *)) 113 { 114 static int initialized; 115 static struct alternate_object_cache cache; 116 size_t i; 117 118 if (!initialized) { 119 odb_for_each_alternate_ref(the_repository->objects, 120 cache_one_alternate, &cache); 121 initialized = 1; 122 } 123 124 for (i = 0; i < cache.nr; i++) 125 cb(negotiator, cache.items[i]); 126 } 127 128 static void die_in_commit_graph_only(const struct object_id *oid) 129 { 130 die(_("You are attempting to fetch %s, which is in the commit graph file but not in the object database.\n" 131 "This is probably due to repo corruption.\n" 132 "If you are attempting to repair this repo corruption by refetching the missing object, use 'git fetch --refetch' with the missing object."), 133 oid_to_hex(oid)); 134 } 135 136 static struct commit *deref_without_lazy_fetch(const struct object_id *oid, 137 int mark_tags_complete_and_check_obj_db) 138 { 139 enum object_type type; 140 struct object_info info = { .typep = &type }; 141 struct commit *commit; 142 143 commit = lookup_commit_in_graph(the_repository, oid); 144 if (commit) { 145 if (mark_tags_complete_and_check_obj_db) { 146 if (!odb_has_object(the_repository->objects, oid, 147 ODB_HAS_OBJECT_RECHECK_PACKED)) 148 die_in_commit_graph_only(oid); 149 } 150 return commit; 151 } 152 153 while (1) { 154 if (odb_read_object_info_extended(the_repository->objects, oid, &info, 155 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK)) 156 return NULL; 157 if (type == OBJ_TAG) { 158 struct tag *tag = (struct tag *) 159 parse_object(the_repository, oid); 160 161 if (!tag->tagged) 162 return NULL; 163 if (mark_tags_complete_and_check_obj_db) 164 tag->object.flags |= COMPLETE; 165 oid = &tag->tagged->oid; 166 } else { 167 break; 168 } 169 } 170 171 if (type == OBJ_COMMIT) { 172 struct commit *commit = lookup_commit(the_repository, oid); 173 if (!commit || repo_parse_commit(the_repository, commit)) 174 return NULL; 175 return commit; 176 } 177 178 return NULL; 179 } 180 181 static int rev_list_insert_ref(struct fetch_negotiator *negotiator, 182 const struct object_id *oid) 183 { 184 struct commit *c = deref_without_lazy_fetch(oid, 0); 185 186 if (c) 187 negotiator->add_tip(negotiator, c); 188 return 0; 189 } 190 191 static int rev_list_insert_ref_oid(const struct reference *ref, void *cb_data) 192 { 193 return rev_list_insert_ref(cb_data, ref->oid); 194 } 195 196 enum ack_type { 197 NAK = 0, 198 ACK, 199 ACK_continue, 200 ACK_common, 201 ACK_ready 202 }; 203 204 static void consume_shallow_list(struct fetch_pack_args *args, 205 struct packet_reader *reader) 206 { 207 if (args->stateless_rpc && args->deepen) { 208 /* If we sent a depth we will get back "duplicate" 209 * shallow and unshallow commands every time there 210 * is a block of have lines exchanged. 211 */ 212 while (packet_reader_read(reader) == PACKET_READ_NORMAL) { 213 if (starts_with(reader->line, "shallow ")) 214 continue; 215 if (starts_with(reader->line, "unshallow ")) 216 continue; 217 die(_("git fetch-pack: expected shallow list")); 218 } 219 if (reader->status != PACKET_READ_FLUSH) 220 die(_("git fetch-pack: expected a flush packet after shallow list")); 221 } 222 } 223 224 static enum ack_type get_ack(struct packet_reader *reader, 225 struct object_id *result_oid) 226 { 227 int len; 228 const char *arg; 229 230 if (packet_reader_read(reader) != PACKET_READ_NORMAL) 231 die(_("git fetch-pack: expected ACK/NAK, got a flush packet")); 232 len = reader->pktlen; 233 234 if (!strcmp(reader->line, "NAK")) 235 return NAK; 236 if (skip_prefix(reader->line, "ACK ", &arg)) { 237 const char *p; 238 if (!parse_oid_hex(arg, result_oid, &p)) { 239 len -= p - reader->line; 240 if (len < 1) 241 return ACK; 242 if (strstr(p, "continue")) 243 return ACK_continue; 244 if (strstr(p, "common")) 245 return ACK_common; 246 if (strstr(p, "ready")) 247 return ACK_ready; 248 return ACK; 249 } 250 } 251 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line); 252 } 253 254 static void send_request(struct fetch_pack_args *args, 255 int fd, struct strbuf *buf) 256 { 257 if (args->stateless_rpc) { 258 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX); 259 packet_flush(fd); 260 } else { 261 if (write_in_full(fd, buf->buf, buf->len) < 0) 262 die_errno(_("unable to write to remote")); 263 } 264 } 265 266 static void insert_one_alternate_object(struct fetch_negotiator *negotiator, 267 struct object *obj) 268 { 269 rev_list_insert_ref(negotiator, &obj->oid); 270 } 271 272 #define INITIAL_FLUSH 16 273 #define PIPESAFE_FLUSH 32 274 #define LARGE_FLUSH 16384 275 276 static int next_flush(int stateless_rpc, int count) 277 { 278 if (stateless_rpc) { 279 if (count < LARGE_FLUSH) 280 count <<= 1; 281 else 282 count = count * 11 / 10; 283 } else { 284 if (count < PIPESAFE_FLUSH) 285 count <<= 1; 286 else 287 count += PIPESAFE_FLUSH; 288 } 289 return count; 290 } 291 292 static void mark_tips(struct fetch_negotiator *negotiator, 293 const struct oid_array *negotiation_restrict_tips) 294 { 295 struct refs_for_each_ref_options opts = { 296 .flags = REFS_FOR_EACH_INCLUDE_BROKEN, 297 }; 298 int i; 299 300 if (!negotiation_restrict_tips) { 301 refs_for_each_ref_ext(get_main_ref_store(the_repository), 302 rev_list_insert_ref_oid, negotiator, &opts); 303 return; 304 } 305 306 for (i = 0; i < negotiation_restrict_tips->nr; i++) 307 rev_list_insert_ref(negotiator, &negotiation_restrict_tips->oid[i]); 308 return; 309 } 310 311 static void send_filter(struct fetch_pack_args *args, 312 struct strbuf *req_buf, 313 int server_supports_filter) 314 { 315 if (args->filter_options.choice) { 316 const char *spec = 317 expand_list_objects_filter_spec(&args->filter_options); 318 if (server_supports_filter) { 319 print_verbose(args, _("Server supports filter")); 320 packet_buf_write(req_buf, "filter %s", spec); 321 trace2_data_string("fetch", the_repository, 322 "filter/effective", spec); 323 } else { 324 warning("filtering not recognized by server, ignoring"); 325 trace2_data_string("fetch", the_repository, 326 "filter/unsupported", spec); 327 } 328 } else { 329 trace2_data_string("fetch", the_repository, 330 "filter/none", ""); 331 } 332 } 333 334 static void add_oids_to_set(const struct oid_array *array, 335 struct oidset *set) 336 { 337 if (!array) 338 return; 339 340 for (size_t i = 0; i < array->nr; i++) { 341 struct object_id *oid = &array->oid[i]; 342 if (!odb_has_object(the_repository->objects, oid, 0)) 343 die(_("the object %s does not exist"), oid_to_hex(oid)); 344 345 oidset_insert(set, oid); 346 } 347 } 348 349 static int find_common(struct fetch_negotiator *negotiator, 350 struct fetch_pack_args *args, 351 int fd[2], struct object_id *result_oid, 352 struct ref *refs) 353 { 354 int fetching; 355 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval; 356 int negotiation_round = 0, haves = 0; 357 const struct object_id *oid; 358 unsigned in_vain = 0; 359 int got_continue = 0; 360 int got_ready = 0; 361 struct strbuf req_buf = STRBUF_INIT; 362 size_t state_len = 0; 363 struct packet_reader reader; 364 struct oidset negotiation_include_oids = OIDSET_INIT; 365 int advertise_sid = 0; 366 367 repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid); 368 369 if (args->stateless_rpc && multi_ack == 1) 370 die(_("the option '%s' requires '%s'"), "--stateless-rpc", "multi_ack_detailed"); 371 372 packet_reader_init(&reader, fd[0], NULL, 0, 373 PACKET_READ_CHOMP_NEWLINE | 374 PACKET_READ_DIE_ON_ERR_PACKET); 375 376 mark_tips(negotiator, args->negotiation_restrict_tips); 377 for_each_cached_alternate(negotiator, insert_one_alternate_object); 378 379 fetching = 0; 380 for ( ; refs ; refs = refs->next) { 381 struct object_id *remote = &refs->old_oid; 382 const char *remote_hex; 383 struct object *o; 384 385 if (!args->refetch) { 386 /* 387 * If that object is complete (i.e. it is an ancestor of a 388 * local ref), we tell them we have it but do not have to 389 * tell them about its ancestors, which they already know 390 * about. 391 * 392 * We use lookup_object here because we are only 393 * interested in the case we *know* the object is 394 * reachable and we have already scanned it. 395 */ 396 if (((o = lookup_object(the_repository, remote)) != NULL) && 397 (o->flags & COMPLETE)) { 398 continue; 399 } 400 } 401 402 remote_hex = oid_to_hex(remote); 403 if (!fetching) { 404 struct strbuf c = STRBUF_INIT; 405 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed"); 406 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack"); 407 if (no_done) strbuf_addstr(&c, " no-done"); 408 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k"); 409 if (use_sideband == 1) strbuf_addstr(&c, " side-band"); 410 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative"); 411 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack"); 412 if (args->no_progress) strbuf_addstr(&c, " no-progress"); 413 if (args->include_tag) strbuf_addstr(&c, " include-tag"); 414 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta"); 415 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since"); 416 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not"); 417 if (agent_supported) strbuf_addf(&c, " agent=%s", 418 git_user_agent_sanitized()); 419 if (advertise_sid && server_supports("session-id")) 420 strbuf_addf(&c, " session-id=%s", trace2_session_id()); 421 if (args->filter_options.choice) 422 strbuf_addstr(&c, " filter"); 423 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf); 424 strbuf_release(&c); 425 } else 426 packet_buf_write(&req_buf, "want %s\n", remote_hex); 427 fetching++; 428 } 429 430 if (!fetching) { 431 strbuf_release(&req_buf); 432 packet_flush(fd[1]); 433 return 1; 434 } 435 436 if (is_repository_shallow(the_repository)) 437 write_shallow_commits(&req_buf, 1, NULL); 438 if (args->depth > 0) 439 packet_buf_write(&req_buf, "deepen %d", args->depth); 440 if (args->deepen_since) { 441 timestamp_t max_age = approxidate(args->deepen_since); 442 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age); 443 } 444 if (args->deepen_not) { 445 int i; 446 for (i = 0; i < args->deepen_not->nr; i++) { 447 struct string_list_item *s = args->deepen_not->items + i; 448 packet_buf_write(&req_buf, "deepen-not %s", s->string); 449 } 450 } 451 send_filter(args, &req_buf, server_supports_filtering); 452 packet_buf_flush(&req_buf); 453 state_len = req_buf.len; 454 455 if (args->deepen) { 456 const char *arg; 457 struct object_id oid; 458 459 send_request(args, fd[1], &req_buf); 460 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) { 461 if (skip_prefix(reader.line, "shallow ", &arg)) { 462 if (get_oid_hex(arg, &oid)) 463 die(_("invalid shallow line: %s"), reader.line); 464 register_shallow(the_repository, &oid); 465 continue; 466 } 467 if (skip_prefix(reader.line, "unshallow ", &arg)) { 468 if (get_oid_hex(arg, &oid)) 469 die(_("invalid unshallow line: %s"), reader.line); 470 if (!lookup_object(the_repository, &oid)) 471 die(_("object not found: %s"), reader.line); 472 /* make sure that it is parsed as shallow */ 473 if (!parse_object(the_repository, &oid)) 474 die(_("error in object: %s"), reader.line); 475 if (unregister_shallow(&oid)) 476 die(_("no shallow found: %s"), reader.line); 477 continue; 478 } 479 die(_("expected shallow/unshallow, got %s"), reader.line); 480 } 481 } else if (!args->stateless_rpc) 482 send_request(args, fd[1], &req_buf); 483 484 if (!args->stateless_rpc) { 485 /* If we aren't using the stateless-rpc interface 486 * we don't need to retain the headers. 487 */ 488 strbuf_setlen(&req_buf, 0); 489 state_len = 0; 490 } 491 492 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository); 493 flushes = 0; 494 retval = -1; 495 496 /* Send unconditional haves from --negotiation-include */ 497 add_oids_to_set(args->negotiation_include_tips, 498 &negotiation_include_oids); 499 if (oidset_size(&negotiation_include_oids)) { 500 struct oidset_iter iter; 501 oidset_iter_init(&negotiation_include_oids, &iter); 502 503 while ((oid = oidset_iter_next(&iter))) { 504 struct commit *commit; 505 packet_buf_write(&req_buf, "have %s\n", 506 oid_to_hex(oid)); 507 print_verbose(args, "have %s", oid_to_hex(oid)); 508 count++; 509 510 commit = lookup_commit(the_repository, oid); 511 if (commit) 512 negotiator->have_sent(negotiator, commit); 513 } 514 } 515 516 while ((oid = negotiator->next(negotiator))) { 517 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid)); 518 print_verbose(args, "have %s", oid_to_hex(oid)); 519 in_vain++; 520 haves++; 521 if (flush_at <= ++count) { 522 int ack; 523 524 negotiation_round++; 525 trace2_region_enter_printf("negotiation_v0_v1", "round", 526 the_repository, "%d", 527 negotiation_round); 528 trace2_data_intmax("negotiation_v0_v1", the_repository, 529 "haves_added", haves); 530 trace2_data_intmax("negotiation_v0_v1", the_repository, 531 "in_vain", in_vain); 532 haves = 0; 533 packet_buf_flush(&req_buf); 534 send_request(args, fd[1], &req_buf); 535 strbuf_setlen(&req_buf, state_len); 536 flushes++; 537 flush_at = next_flush(args->stateless_rpc, count); 538 539 /* 540 * We keep one window "ahead" of the other side, and 541 * will wait for an ACK only on the next one 542 */ 543 if (!args->stateless_rpc && count == INITIAL_FLUSH) 544 continue; 545 546 consume_shallow_list(args, &reader); 547 do { 548 ack = get_ack(&reader, result_oid); 549 if (ack) 550 print_verbose(args, _("got %s %d %s"), "ack", 551 ack, oid_to_hex(result_oid)); 552 switch (ack) { 553 case ACK: 554 trace2_region_leave_printf("negotiation_v0_v1", "round", 555 the_repository, "%d", 556 negotiation_round); 557 flushes = 0; 558 multi_ack = 0; 559 retval = 0; 560 goto done; 561 case ACK_common: 562 case ACK_ready: 563 case ACK_continue: { 564 struct commit *commit = 565 lookup_commit(the_repository, 566 result_oid); 567 int was_common; 568 569 if (!commit) 570 die(_("invalid commit %s"), oid_to_hex(result_oid)); 571 was_common = negotiator->ack(negotiator, commit); 572 if (args->stateless_rpc 573 && ack == ACK_common 574 && !was_common) { 575 /* We need to replay the have for this object 576 * on the next RPC request so the peer knows 577 * it is in common with us. 578 */ 579 const char *hex = oid_to_hex(result_oid); 580 packet_buf_write(&req_buf, "have %s\n", hex); 581 state_len = req_buf.len; 582 haves++; 583 /* 584 * Reset in_vain because an ack 585 * for this commit has not been 586 * seen. 587 */ 588 in_vain = 0; 589 } else if (!args->stateless_rpc 590 || ack != ACK_common) 591 in_vain = 0; 592 retval = 0; 593 got_continue = 1; 594 if (ack == ACK_ready) 595 got_ready = 1; 596 break; 597 } 598 } 599 } while (ack); 600 flushes--; 601 trace2_region_leave_printf("negotiation_v0_v1", "round", 602 the_repository, "%d", 603 negotiation_round); 604 if (got_continue && MAX_IN_VAIN < in_vain) { 605 print_verbose(args, _("giving up")); 606 break; /* give up */ 607 } 608 if (got_ready) 609 break; 610 } 611 } 612 done: 613 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository); 614 trace2_data_intmax("negotiation_v0_v1", the_repository, "total_rounds", 615 negotiation_round); 616 if (!got_ready || !no_done) { 617 packet_buf_write(&req_buf, "done\n"); 618 send_request(args, fd[1], &req_buf); 619 } 620 print_verbose(args, _("done")); 621 if (retval != 0) { 622 multi_ack = 0; 623 flushes++; 624 } 625 strbuf_release(&req_buf); 626 oidset_clear(&negotiation_include_oids); 627 628 if (!got_ready || !no_done) 629 consume_shallow_list(args, &reader); 630 while (flushes || multi_ack) { 631 int ack = get_ack(&reader, result_oid); 632 if (ack) { 633 print_verbose(args, _("got %s (%d) %s"), "ack", 634 ack, oid_to_hex(result_oid)); 635 if (ack == ACK) 636 return 0; 637 multi_ack = 1; 638 continue; 639 } 640 flushes--; 641 } 642 /* it is no error to fetch into a completely empty repo */ 643 return count ? retval : 0; 644 } 645 646 static struct prio_queue complete = { compare_commits_by_commit_date }; 647 648 static int mark_complete(const struct object_id *oid) 649 { 650 struct commit *commit = deref_without_lazy_fetch(oid, 1); 651 652 if (commit && !(commit->object.flags & COMPLETE)) { 653 commit->object.flags |= COMPLETE; 654 prio_queue_put(&complete, commit); 655 } 656 return 0; 657 } 658 659 static int mark_complete_oid(const struct reference *ref, void *cb_data UNUSED) 660 { 661 return mark_complete(ref->oid); 662 } 663 664 static void mark_recent_complete_commits(struct fetch_pack_args *args, 665 timestamp_t cutoff) 666 { 667 struct commit *item; 668 while ((item = prio_queue_peek(&complete))) { 669 if (item->date < cutoff) 670 break; 671 print_verbose(args, _("Marking %s as complete"), 672 oid_to_hex(&item->object.oid)); 673 pop_most_recent_commit(&complete, COMPLETE); 674 } 675 } 676 677 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs) 678 { 679 for (; refs; refs = refs->next) 680 oidset_insert(oids, &refs->old_oid); 681 } 682 683 static int is_unmatched_ref(const struct ref *ref) 684 { 685 struct object_id oid; 686 const char *p; 687 return ref->match_status == REF_NOT_MATCHED && 688 !parse_oid_hex(ref->name, &oid, &p) && 689 *p == '\0' && 690 oideq(&oid, &ref->old_oid); 691 } 692 693 static void filter_refs(struct fetch_pack_args *args, 694 struct ref **refs, 695 struct ref **sought, int nr_sought) 696 { 697 struct ref *newlist = NULL; 698 struct ref **newtail = &newlist; 699 struct ref *unmatched = NULL; 700 struct ref *ref, *next; 701 struct oidset tip_oids = OIDSET_INIT; 702 int i; 703 int strict = !(allow_unadvertised_object_request & 704 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)); 705 706 i = 0; 707 for (ref = *refs; ref; ref = next) { 708 int keep = 0; 709 next = ref->next; 710 711 if (starts_with(ref->name, "refs/") && 712 check_refname_format(ref->name, 0)) { 713 /* 714 * trash or a peeled value; do not even add it to 715 * unmatched list 716 */ 717 free_one_ref(ref); 718 continue; 719 } else { 720 while (i < nr_sought) { 721 int cmp = strcmp(ref->name, sought[i]->name); 722 if (cmp < 0) 723 break; /* definitely do not have it */ 724 else if (cmp == 0) { 725 keep = 1; /* definitely have it */ 726 sought[i]->match_status = REF_MATCHED; 727 } 728 i++; 729 } 730 731 if (!keep && args->fetch_all && 732 (!args->deepen || !starts_with(ref->name, "refs/tags/"))) 733 keep = 1; 734 } 735 736 if (keep) { 737 *newtail = ref; 738 ref->next = NULL; 739 newtail = &ref->next; 740 } else { 741 ref->next = unmatched; 742 unmatched = ref; 743 } 744 } 745 746 if (strict) { 747 for (i = 0; i < nr_sought; i++) { 748 ref = sought[i]; 749 if (!is_unmatched_ref(ref)) 750 continue; 751 752 add_refs_to_oidset(&tip_oids, unmatched); 753 add_refs_to_oidset(&tip_oids, newlist); 754 break; 755 } 756 } 757 758 /* Append unmatched requests to the list */ 759 for (i = 0; i < nr_sought; i++) { 760 ref = sought[i]; 761 if (!is_unmatched_ref(ref)) 762 continue; 763 764 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) { 765 ref->match_status = REF_MATCHED; 766 *newtail = copy_ref(ref); 767 newtail = &(*newtail)->next; 768 } else { 769 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED; 770 } 771 } 772 773 oidset_clear(&tip_oids); 774 free_refs(unmatched); 775 776 *refs = newlist; 777 } 778 779 static void mark_alternate_complete(struct fetch_negotiator *negotiator UNUSED, 780 struct object *obj) 781 { 782 mark_complete(&obj->oid); 783 } 784 785 /* 786 * Mark recent commits available locally and reachable from a local ref as 787 * COMPLETE. 788 * 789 * The cutoff time for recency is determined by this heuristic: it is the 790 * earliest commit time of the objects in refs that are commits and that we know 791 * the commit time of. 792 */ 793 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator, 794 struct fetch_pack_args *args, 795 struct ref **refs) 796 { 797 struct ref *ref; 798 int old_save_commit_buffer = save_commit_buffer; 799 timestamp_t cutoff = 0; 800 801 if (args->refetch) 802 return; 803 804 save_commit_buffer = 0; 805 806 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL); 807 for (ref = *refs; ref; ref = ref->next) { 808 struct commit *commit; 809 810 commit = lookup_commit_in_graph(the_repository, &ref->old_oid); 811 if (!commit) { 812 struct object *o; 813 814 if (!odb_has_object(the_repository->objects, &ref->old_oid, 0)) 815 continue; 816 o = parse_object(the_repository, &ref->old_oid); 817 if (!o || o->type != OBJ_COMMIT) 818 continue; 819 820 commit = (struct commit *)o; 821 } 822 823 /* 824 * We already have it -- which may mean that we were 825 * in sync with the other side at some time after 826 * that (it is OK if we guess wrong here). 827 */ 828 if (!cutoff || cutoff < commit->date) 829 cutoff = commit->date; 830 } 831 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL); 832 833 /* 834 * This block marks all local refs as COMPLETE, and then recursively marks all 835 * parents of those refs as COMPLETE. 836 */ 837 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL); 838 if (!args->deepen) { 839 struct refs_for_each_ref_options opts = { 840 .flags = REFS_FOR_EACH_INCLUDE_BROKEN, 841 }; 842 843 refs_for_each_ref_ext(get_main_ref_store(the_repository), 844 mark_complete_oid, NULL, &opts); 845 for_each_cached_alternate(NULL, mark_alternate_complete); 846 if (cutoff) 847 mark_recent_complete_commits(args, cutoff); 848 } 849 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL); 850 851 /* 852 * Mark all complete remote refs as common refs. 853 * Don't mark them common yet; the server has to be told so first. 854 */ 855 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL); 856 for (ref = *refs; ref; ref = ref->next) { 857 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0); 858 859 if (!c || !(c->object.flags & COMPLETE)) 860 continue; 861 862 negotiator->known_common(negotiator, c); 863 } 864 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL); 865 866 save_commit_buffer = old_save_commit_buffer; 867 } 868 869 /* 870 * Returns 1 if every object pointed to by the given remote refs is available 871 * locally and reachable from a local ref, and 0 otherwise. 872 */ 873 static int everything_local(struct fetch_pack_args *args, 874 struct ref **refs) 875 { 876 struct ref *ref; 877 int retval; 878 879 for (retval = 1, ref = *refs; ref ; ref = ref->next) { 880 const struct object_id *remote = &ref->old_oid; 881 struct object *o; 882 883 o = lookup_object(the_repository, remote); 884 if (!o || !(o->flags & COMPLETE)) { 885 retval = 0; 886 print_verbose(args, "want %s (%s)", oid_to_hex(remote), 887 ref->name); 888 continue; 889 } 890 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote), 891 ref->name); 892 } 893 894 return retval; 895 } 896 897 static int sideband_demux(int in UNUSED, int out, void *data) 898 { 899 int *xd = data; 900 int ret; 901 902 ret = recv_sideband("fetch-pack", xd[0], out); 903 close(out); 904 return ret; 905 } 906 907 static void create_promisor_file(const char *keep_name, 908 struct ref **sought, int nr_sought) 909 { 910 struct strbuf promisor_name = STRBUF_INIT; 911 int suffix_stripped; 912 913 strbuf_addstr(&promisor_name, keep_name); 914 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep"); 915 if (!suffix_stripped) 916 BUG("name of pack lockfile should end with .keep (was '%s')", 917 keep_name); 918 strbuf_addstr(&promisor_name, ".promisor"); 919 920 write_promisor_file(promisor_name.buf, sought, nr_sought); 921 922 strbuf_release(&promisor_name); 923 } 924 925 static void parse_gitmodules_oids(int fd, struct oidset *gitmodules_oids) 926 { 927 int len = the_hash_algo->hexsz + 1; /* hash + NL */ 928 929 do { 930 char hex_hash[GIT_MAX_HEXSZ + 1]; 931 int read_len = read_in_full(fd, hex_hash, len); 932 struct object_id oid; 933 const char *end; 934 935 if (!read_len) 936 return; 937 if (read_len != len) 938 die("invalid length read %d", read_len); 939 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n') 940 die("invalid hash"); 941 oidset_insert(gitmodules_oids, &oid); 942 } while (1); 943 } 944 945 static void add_index_pack_keep_option(struct strvec *args) 946 { 947 char hostname[HOST_NAME_MAX + 1]; 948 949 if (xgethostname(hostname, sizeof(hostname))) 950 xsnprintf(hostname, sizeof(hostname), "localhost"); 951 strvec_pushf(args, "--keep=fetch-pack %"PRIuMAX " on %s", 952 (uintmax_t)getpid(), hostname); 953 } 954 955 /* 956 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args. 957 * The strings to pass as the --index-pack-arg arguments to http-fetch will be 958 * stored there. (It must be freed by the caller.) 959 */ 960 static int get_pack(struct fetch_pack_args *args, 961 int xd[2], struct string_list *pack_lockfiles, 962 struct strvec *index_pack_args, 963 struct ref **sought, int nr_sought, 964 struct oidset *gitmodules_oids) 965 { 966 struct async demux; 967 int do_keep = args->keep_pack; 968 const char *cmd_name; 969 struct pack_header header; 970 int pass_header = 0; 971 struct child_process cmd = CHILD_PROCESS_INIT; 972 int fsck_objects = 0; 973 int ret; 974 975 memset(&demux, 0, sizeof(demux)); 976 if (use_sideband) { 977 /* xd[] is talking with upload-pack; subprocess reads from 978 * xd[0], spits out band#2 to stderr, and feeds us band#1 979 * through demux->out. 980 */ 981 demux.proc = sideband_demux; 982 demux.data = xd; 983 demux.out = -1; 984 demux.isolate_sigpipe = 1; 985 if (start_async(&demux)) 986 die(_("fetch-pack: unable to fork off sideband demultiplexer")); 987 } 988 else 989 demux.out = xd[0]; 990 991 if (!args->keep_pack && unpack_limit && !index_pack_args) { 992 993 if (read_pack_header(demux.out, &header)) 994 die(_("protocol error: bad pack header")); 995 pass_header = 1; 996 if (ntohl(header.hdr_entries) < unpack_limit) 997 do_keep = 0; 998 else 999 do_keep = 1; 1000 } 1001 1002 if (alternate_shallow_file) { 1003 strvec_push(&cmd.args, "--shallow-file"); 1004 strvec_push(&cmd.args, alternate_shallow_file); 1005 } 1006 1007 fsck_objects = fetch_pack_fsck_objects(); 1008 1009 if (do_keep || args->from_promisor || index_pack_args || fsck_objects) { 1010 if (pack_lockfiles || fsck_objects) 1011 cmd.out = -1; 1012 cmd_name = "index-pack"; 1013 strvec_push(&cmd.args, cmd_name); 1014 strvec_push(&cmd.args, "--stdin"); 1015 if (!args->quiet && !args->no_progress) 1016 strvec_push(&cmd.args, "-v"); 1017 if (args->use_thin_pack) 1018 strvec_push(&cmd.args, "--fix-thin"); 1019 if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit)) 1020 add_index_pack_keep_option(&cmd.args); 1021 if (!index_pack_args && args->check_self_contained_and_connected) 1022 strvec_push(&cmd.args, "--check-self-contained-and-connected"); 1023 else 1024 /* 1025 * We cannot perform any connectivity checks because 1026 * not all packs have been downloaded; let the caller 1027 * have this responsibility. 1028 */ 1029 args->check_self_contained_and_connected = 0; 1030 1031 if (args->from_promisor) 1032 /* 1033 * create_promisor_file() may be called afterwards but 1034 * we still need index-pack to know that this is a 1035 * promisor pack. For example, if transfer.fsckobjects 1036 * is true, index-pack needs to know that .gitmodules 1037 * is a promisor object (so that it won't complain if 1038 * it is missing). 1039 */ 1040 strvec_push(&cmd.args, "--promisor"); 1041 } 1042 else { 1043 cmd_name = "unpack-objects"; 1044 strvec_push(&cmd.args, cmd_name); 1045 if (args->quiet || args->no_progress) 1046 strvec_push(&cmd.args, "-q"); 1047 args->check_self_contained_and_connected = 0; 1048 } 1049 1050 if (pass_header) 1051 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32, 1052 ntohl(header.hdr_version), 1053 ntohl(header.hdr_entries)); 1054 if (fsck_objects) { 1055 if (args->from_promisor || index_pack_args) 1056 /* 1057 * We cannot use --strict in index-pack because it 1058 * checks both broken objects and links, but we only 1059 * want to check for broken objects. 1060 */ 1061 strvec_push(&cmd.args, "--fsck-objects"); 1062 else 1063 strvec_pushf(&cmd.args, "--strict%s", 1064 fsck_msg_types.buf); 1065 } 1066 1067 if (index_pack_args) 1068 strvec_pushv(index_pack_args, cmd.args.v); 1069 1070 sigchain_push(SIGPIPE, SIG_IGN); 1071 1072 cmd.in = demux.out; 1073 cmd.git_cmd = 1; 1074 if (start_command(&cmd)) 1075 die(_("fetch-pack: unable to fork off %s"), cmd_name); 1076 if (do_keep && (pack_lockfiles || fsck_objects)) { 1077 int is_well_formed; 1078 char *pack_lockfile = index_pack_lockfile(the_repository->objects->source, 1079 cmd.out, 1080 &is_well_formed); 1081 1082 if (!is_well_formed) 1083 die(_("fetch-pack: invalid index-pack output")); 1084 if (pack_lockfiles && pack_lockfile) 1085 string_list_append_nodup(pack_lockfiles, pack_lockfile); 1086 else 1087 free(pack_lockfile); 1088 parse_gitmodules_oids(cmd.out, gitmodules_oids); 1089 close(cmd.out); 1090 } 1091 1092 if (!use_sideband) 1093 /* Closed by start_command() */ 1094 xd[0] = -1; 1095 1096 ret = finish_command(&cmd); 1097 if (!ret || (args->check_self_contained_and_connected && ret == 1)) 1098 args->self_contained_and_connected = 1099 args->check_self_contained_and_connected && 1100 ret == 0; 1101 else 1102 die(_("%s failed"), cmd_name); 1103 if (use_sideband && finish_async(&demux)) 1104 die(_("error in sideband demultiplexer")); 1105 1106 sigchain_pop(SIGPIPE); 1107 1108 /* 1109 * Now that index-pack has succeeded, write the promisor file using the 1110 * obtained .keep filename if necessary 1111 */ 1112 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor) 1113 create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought); 1114 1115 return 0; 1116 } 1117 1118 static int ref_compare_name(const struct ref *a, const struct ref *b) 1119 { 1120 return strcmp(a->name, b->name); 1121 } 1122 1123 DEFINE_LIST_SORT(static, sort_ref_list, struct ref, next); 1124 1125 static int cmp_ref_by_name(const void *a_, const void *b_) 1126 { 1127 const struct ref *a = *((const struct ref **)a_); 1128 const struct ref *b = *((const struct ref **)b_); 1129 return strcmp(a->name, b->name); 1130 } 1131 1132 static struct ref *do_fetch_pack(struct fetch_pack_args *args, 1133 int fd[2], 1134 const struct ref *orig_ref, 1135 struct ref **sought, int nr_sought, 1136 struct shallow_info *si, 1137 struct string_list *pack_lockfiles) 1138 { 1139 struct fsck_options fsck_options = { 0 }; 1140 struct repository *r = the_repository; 1141 struct ref *ref = copy_ref_list(orig_ref); 1142 struct object_id oid; 1143 const char *agent_feature; 1144 size_t agent_len; 1145 struct fetch_negotiator negotiator_alloc; 1146 struct fetch_negotiator *negotiator; 1147 1148 negotiator = &negotiator_alloc; 1149 if (args->refetch) { 1150 fetch_negotiator_init_noop(negotiator); 1151 } else { 1152 fetch_negotiator_init(r, negotiator); 1153 } 1154 1155 sort_ref_list(&ref, ref_compare_name); 1156 QSORT(sought, nr_sought, cmp_ref_by_name); 1157 1158 if ((agent_feature = server_feature_value("agent", &agent_len))) { 1159 agent_supported = 1; 1160 if (agent_len) 1161 print_verbose(args, _("Server version is %.*s"), 1162 (int)agent_len, agent_feature); 1163 } 1164 1165 if (server_supports("shallow")) 1166 print_verbose(args, _("Server supports %s"), "shallow"); 1167 else if (args->depth > 0 || is_repository_shallow(r)) 1168 die(_("Server does not support shallow clients")); 1169 if (args->depth > 0 || args->deepen_since || args->deepen_not) 1170 args->deepen = 1; 1171 if (server_supports("multi_ack_detailed")) { 1172 print_verbose(args, _("Server supports %s"), "multi_ack_detailed"); 1173 multi_ack = 2; 1174 if (server_supports("no-done")) { 1175 print_verbose(args, _("Server supports %s"), "no-done"); 1176 if (args->stateless_rpc) 1177 no_done = 1; 1178 } 1179 } 1180 else if (server_supports("multi_ack")) { 1181 print_verbose(args, _("Server supports %s"), "multi_ack"); 1182 multi_ack = 1; 1183 } 1184 if (server_supports("side-band-64k")) { 1185 print_verbose(args, _("Server supports %s"), "side-band-64k"); 1186 use_sideband = 2; 1187 } 1188 else if (server_supports("side-band")) { 1189 print_verbose(args, _("Server supports %s"), "side-band"); 1190 use_sideband = 1; 1191 } 1192 if (server_supports("allow-tip-sha1-in-want")) { 1193 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want"); 1194 allow_unadvertised_object_request |= ALLOW_TIP_SHA1; 1195 } 1196 if (server_supports("allow-reachable-sha1-in-want")) { 1197 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want"); 1198 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; 1199 } 1200 if (server_supports("thin-pack")) 1201 print_verbose(args, _("Server supports %s"), "thin-pack"); 1202 else 1203 args->use_thin_pack = 0; 1204 if (server_supports("no-progress")) 1205 print_verbose(args, _("Server supports %s"), "no-progress"); 1206 else 1207 args->no_progress = 0; 1208 if (server_supports("include-tag")) 1209 print_verbose(args, _("Server supports %s"), "include-tag"); 1210 else 1211 args->include_tag = 0; 1212 if (server_supports("ofs-delta")) 1213 print_verbose(args, _("Server supports %s"), "ofs-delta"); 1214 else 1215 prefer_ofs_delta = 0; 1216 1217 if (server_supports("filter")) { 1218 server_supports_filtering = 1; 1219 print_verbose(args, _("Server supports %s"), "filter"); 1220 } else if (args->filter_options.choice) { 1221 warning("filtering not recognized by server, ignoring"); 1222 } 1223 1224 if (server_supports("deepen-since")) { 1225 print_verbose(args, _("Server supports %s"), "deepen-since"); 1226 deepen_since_ok = 1; 1227 } else if (args->deepen_since) 1228 die(_("Server does not support --shallow-since")); 1229 if (server_supports("deepen-not")) { 1230 print_verbose(args, _("Server supports %s"), "deepen-not"); 1231 deepen_not_ok = 1; 1232 } else if (args->deepen_not) 1233 die(_("Server does not support --shallow-exclude")); 1234 if (server_supports("deepen-relative")) 1235 print_verbose(args, _("Server supports %s"), "deepen-relative"); 1236 else if (args->deepen_relative) 1237 die(_("Server does not support --deepen")); 1238 if (!server_supports_hash(the_hash_algo->name, NULL)) 1239 die(_("Server does not support this repository's object format")); 1240 1241 mark_complete_and_common_ref(negotiator, args, &ref); 1242 filter_refs(args, &ref, sought, nr_sought); 1243 if (!args->refetch && everything_local(args, &ref)) { 1244 packet_flush(fd[1]); 1245 goto all_done; 1246 } 1247 if (find_common(negotiator, args, fd, &oid, ref) < 0) 1248 if (!args->keep_pack) 1249 /* When cloning, it is not unusual to have 1250 * no common commit. 1251 */ 1252 warning(_("no common commits")); 1253 1254 if (args->stateless_rpc) 1255 packet_flush(fd[1]); 1256 if (args->deepen) 1257 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, 1258 NULL); 1259 else if (si->nr_ours || si->nr_theirs) { 1260 if (args->reject_shallow_remote) 1261 die(_("source repository is shallow, reject to clone.")); 1262 alternate_shallow_file = setup_temporary_shallow(si->shallow); 1263 } else 1264 alternate_shallow_file = NULL; 1265 1266 fsck_options_init(&fsck_options, the_repository, FSCK_OPTIONS_MISSING_GITMODULES); 1267 if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought, 1268 &fsck_options.gitmodules_found)) 1269 die(_("git fetch-pack: fetch failed.")); 1270 if (fsck_finish(&fsck_options)) 1271 die("fsck failed"); 1272 1273 all_done: 1274 fsck_options_clear(&fsck_options); 1275 if (negotiator) 1276 negotiator->release(negotiator); 1277 return ref; 1278 } 1279 1280 static void add_shallow_requests(struct strbuf *req_buf, 1281 const struct fetch_pack_args *args) 1282 { 1283 if (is_repository_shallow(the_repository)) 1284 write_shallow_commits(req_buf, 1, NULL); 1285 if (args->depth > 0) 1286 packet_buf_write(req_buf, "deepen %d", args->depth); 1287 if (args->deepen_since) { 1288 timestamp_t max_age = approxidate(args->deepen_since); 1289 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age); 1290 } 1291 if (args->deepen_not) { 1292 int i; 1293 for (i = 0; i < args->deepen_not->nr; i++) { 1294 struct string_list_item *s = args->deepen_not->items + i; 1295 packet_buf_write(req_buf, "deepen-not %s", s->string); 1296 } 1297 } 1298 if (args->deepen_relative) 1299 packet_buf_write(req_buf, "deepen-relative\n"); 1300 } 1301 1302 static void add_wants(const struct ref *wants, struct strbuf *req_buf) 1303 { 1304 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0); 1305 1306 for ( ; wants ; wants = wants->next) { 1307 const struct object_id *remote = &wants->old_oid; 1308 struct object *o; 1309 1310 /* 1311 * If that object is complete (i.e. it is an ancestor of a 1312 * local ref), we tell them we have it but do not have to 1313 * tell them about its ancestors, which they already know 1314 * about. 1315 * 1316 * We use lookup_object here because we are only 1317 * interested in the case we *know* the object is 1318 * reachable and we have already scanned it. 1319 */ 1320 if (((o = lookup_object(the_repository, remote)) != NULL) && 1321 (o->flags & COMPLETE)) { 1322 continue; 1323 } 1324 1325 if (!use_ref_in_want || wants->exact_oid) 1326 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote)); 1327 else 1328 packet_buf_write(req_buf, "want-ref %s\n", wants->name); 1329 } 1330 } 1331 1332 static void add_common(struct strbuf *req_buf, struct oidset *common) 1333 { 1334 struct oidset_iter iter; 1335 const struct object_id *oid; 1336 oidset_iter_init(common, &iter); 1337 1338 while ((oid = oidset_iter_next(&iter))) { 1339 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid)); 1340 } 1341 } 1342 1343 static int add_haves(struct fetch_negotiator *negotiator, 1344 struct strbuf *req_buf, 1345 int *haves_to_send, 1346 struct oidset *negotiation_include_oids) 1347 { 1348 int haves_added = 0; 1349 const struct object_id *oid; 1350 1351 /* Send unconditional haves from --negotiation-include */ 1352 if (negotiation_include_oids) { 1353 struct oidset_iter iter; 1354 oidset_iter_init(negotiation_include_oids, &iter); 1355 1356 while ((oid = oidset_iter_next(&iter))) { 1357 struct commit *commit = lookup_commit(the_repository, oid); 1358 if (commit) { 1359 packet_buf_write(req_buf, "have %s\n", 1360 oid_to_hex(oid)); 1361 negotiator->have_sent(negotiator, commit); 1362 } 1363 } 1364 } 1365 1366 while ((oid = negotiator->next(negotiator))) { 1367 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid)); 1368 if (++haves_added >= *haves_to_send) 1369 break; 1370 } 1371 1372 /* Increase haves to send on next round */ 1373 *haves_to_send = next_flush(1, *haves_to_send); 1374 1375 return haves_added; 1376 } 1377 1378 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, 1379 struct fetch_pack_args *args, 1380 const struct ref *wants, struct oidset *common, 1381 int *haves_to_send, int *in_vain, 1382 int sideband_all, int seen_ack, 1383 struct oidset *negotiation_include_oids) 1384 { 1385 int haves_added; 1386 int done_sent = 0; 1387 struct strbuf req_buf = STRBUF_INIT; 1388 1389 write_command_and_capabilities(&req_buf, "fetch", args->server_options); 1390 1391 if (args->use_thin_pack) 1392 packet_buf_write(&req_buf, "thin-pack"); 1393 if (args->no_progress) 1394 packet_buf_write(&req_buf, "no-progress"); 1395 if (args->include_tag) 1396 packet_buf_write(&req_buf, "include-tag"); 1397 if (prefer_ofs_delta) 1398 packet_buf_write(&req_buf, "ofs-delta"); 1399 if (sideband_all) 1400 packet_buf_write(&req_buf, "sideband-all"); 1401 1402 /* Add shallow-info and deepen request */ 1403 if (server_supports_feature("fetch", "shallow", 0)) 1404 add_shallow_requests(&req_buf, args); 1405 else if (is_repository_shallow(the_repository) || args->deepen) 1406 die(_("Server does not support shallow requests")); 1407 1408 /* Add filter */ 1409 send_filter(args, &req_buf, 1410 server_supports_feature("fetch", "filter", 0)); 1411 1412 if (server_supports_feature("fetch", "packfile-uris", 0)) { 1413 int i; 1414 struct strbuf to_send = STRBUF_INIT; 1415 1416 for (i = 0; i < uri_protocols.nr; i++) { 1417 const char *s = uri_protocols.items[i].string; 1418 1419 if (!strcmp(s, "https") || !strcmp(s, "http")) { 1420 if (to_send.len) 1421 strbuf_addch(&to_send, ','); 1422 strbuf_addstr(&to_send, s); 1423 } 1424 } 1425 if (to_send.len) { 1426 packet_buf_write(&req_buf, "packfile-uris %s", 1427 to_send.buf); 1428 strbuf_release(&to_send); 1429 } 1430 } 1431 1432 /* add wants */ 1433 add_wants(wants, &req_buf); 1434 1435 /* Add all of the common commits we've found in previous rounds */ 1436 add_common(&req_buf, common); 1437 1438 haves_added = add_haves(negotiator, &req_buf, haves_to_send, 1439 negotiation_include_oids); 1440 *in_vain += haves_added; 1441 trace2_data_intmax("negotiation_v2", the_repository, "haves_added", haves_added); 1442 trace2_data_intmax("negotiation_v2", the_repository, "in_vain", *in_vain); 1443 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) { 1444 /* Send Done */ 1445 packet_buf_write(&req_buf, "done\n"); 1446 done_sent = 1; 1447 } 1448 1449 /* Send request */ 1450 packet_buf_flush(&req_buf); 1451 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0) 1452 die_errno(_("unable to write request to remote")); 1453 1454 strbuf_release(&req_buf); 1455 return done_sent; 1456 } 1457 1458 /* 1459 * Processes a section header in a server's response and checks if it matches 1460 * `section`. If the value of `peek` is 1, the header line will be peeked (and 1461 * not consumed); if 0, the line will be consumed and the function will die if 1462 * the section header doesn't match what was expected. 1463 */ 1464 static int process_section_header(struct packet_reader *reader, 1465 const char *section, int peek) 1466 { 1467 int ret = 0; 1468 1469 if (packet_reader_peek(reader) == PACKET_READ_NORMAL && 1470 !strcmp(reader->line, section)) 1471 ret = 1; 1472 1473 if (!peek) { 1474 if (!ret) { 1475 if (reader->line) 1476 die(_("expected '%s', received '%s'"), 1477 section, reader->line); 1478 else 1479 die(_("expected '%s'"), section); 1480 } 1481 packet_reader_read(reader); 1482 } 1483 1484 return ret; 1485 } 1486 1487 static int process_ack(struct fetch_negotiator *negotiator, 1488 struct packet_reader *reader, 1489 struct object_id *common_oid, 1490 int *received_ready) 1491 { 1492 while (packet_reader_read(reader) == PACKET_READ_NORMAL) { 1493 const char *arg; 1494 1495 if (!strcmp(reader->line, "NAK")) 1496 continue; 1497 1498 if (skip_prefix(reader->line, "ACK ", &arg)) { 1499 if (!get_oid_hex(arg, common_oid)) { 1500 struct commit *commit; 1501 commit = lookup_commit(the_repository, common_oid); 1502 if (negotiator) 1503 negotiator->ack(negotiator, commit); 1504 } 1505 return 1; 1506 } 1507 1508 if (!strcmp(reader->line, "ready")) { 1509 *received_ready = 1; 1510 continue; 1511 } 1512 1513 die(_("unexpected acknowledgment line: '%s'"), reader->line); 1514 } 1515 1516 if (reader->status != PACKET_READ_FLUSH && 1517 reader->status != PACKET_READ_DELIM) 1518 die(_("error processing acks: %d"), reader->status); 1519 1520 /* 1521 * If an "acknowledgments" section is sent, a packfile is sent if and 1522 * only if "ready" was sent in this section. The other sections 1523 * ("shallow-info" and "wanted-refs") are sent only if a packfile is 1524 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH 1525 * otherwise. 1526 */ 1527 if (*received_ready && reader->status != PACKET_READ_DELIM) 1528 /* 1529 * TRANSLATORS: The parameter will be 'ready', a protocol 1530 * keyword. 1531 */ 1532 die(_("expected packfile to be sent after '%s'"), "ready"); 1533 if (!*received_ready && reader->status != PACKET_READ_FLUSH) 1534 /* 1535 * TRANSLATORS: The parameter will be 'ready', a protocol 1536 * keyword. 1537 */ 1538 die(_("expected no other sections to be sent after no '%s'"), "ready"); 1539 1540 return 0; 1541 } 1542 1543 static void receive_shallow_info(struct fetch_pack_args *args, 1544 struct packet_reader *reader, 1545 struct oid_array *shallows, 1546 struct shallow_info *si) 1547 { 1548 int unshallow_received = 0; 1549 1550 process_section_header(reader, "shallow-info", 0); 1551 while (packet_reader_read(reader) == PACKET_READ_NORMAL) { 1552 const char *arg; 1553 struct object_id oid; 1554 1555 if (skip_prefix(reader->line, "shallow ", &arg)) { 1556 if (get_oid_hex(arg, &oid)) 1557 die(_("invalid shallow line: %s"), reader->line); 1558 oid_array_append(shallows, &oid); 1559 continue; 1560 } 1561 if (skip_prefix(reader->line, "unshallow ", &arg)) { 1562 if (get_oid_hex(arg, &oid)) 1563 die(_("invalid unshallow line: %s"), reader->line); 1564 if (!lookup_object(the_repository, &oid)) 1565 die(_("object not found: %s"), reader->line); 1566 /* make sure that it is parsed as shallow */ 1567 if (!parse_object(the_repository, &oid)) 1568 die(_("error in object: %s"), reader->line); 1569 if (unregister_shallow(&oid)) 1570 die(_("no shallow found: %s"), reader->line); 1571 unshallow_received = 1; 1572 continue; 1573 } 1574 die(_("expected shallow/unshallow, got %s"), reader->line); 1575 } 1576 1577 if (reader->status != PACKET_READ_FLUSH && 1578 reader->status != PACKET_READ_DELIM) 1579 die(_("error processing shallow info: %d"), reader->status); 1580 1581 if (args->deepen || unshallow_received) { 1582 /* 1583 * Treat these as shallow lines caused by our depth settings. 1584 * In v0, these lines cannot cause refs to be rejected; do the 1585 * same. 1586 */ 1587 int i; 1588 1589 for (i = 0; i < shallows->nr; i++) 1590 register_shallow(the_repository, &shallows->oid[i]); 1591 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, 1592 NULL); 1593 args->deepen = 1; 1594 } else if (shallows->nr) { 1595 /* 1596 * Treat these as shallow lines caused by the remote being 1597 * shallow. In v0, remote refs that reach these objects are 1598 * rejected (unless --update-shallow is set); do the same. 1599 */ 1600 prepare_shallow_info(si, shallows); 1601 if (si->nr_ours || si->nr_theirs) { 1602 if (args->reject_shallow_remote) 1603 die(_("source repository is shallow, reject to clone.")); 1604 alternate_shallow_file = 1605 setup_temporary_shallow(si->shallow); 1606 } else 1607 alternate_shallow_file = NULL; 1608 } else { 1609 alternate_shallow_file = NULL; 1610 } 1611 } 1612 1613 static int cmp_name_ref(const void *name, const void *ref) 1614 { 1615 return strcmp(name, (*(struct ref **)ref)->name); 1616 } 1617 1618 static void receive_wanted_refs(struct packet_reader *reader, 1619 struct ref **sought, int nr_sought) 1620 { 1621 process_section_header(reader, "wanted-refs", 0); 1622 while (packet_reader_read(reader) == PACKET_READ_NORMAL) { 1623 struct object_id oid; 1624 const char *end; 1625 struct ref **found; 1626 1627 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ') 1628 die(_("expected wanted-ref, got '%s'"), reader->line); 1629 1630 found = bsearch(end, sought, nr_sought, sizeof(*sought), 1631 cmp_name_ref); 1632 if (!found) 1633 die(_("unexpected wanted-ref: '%s'"), reader->line); 1634 oidcpy(&(*found)->old_oid, &oid); 1635 } 1636 1637 if (reader->status != PACKET_READ_DELIM) 1638 die(_("error processing wanted refs: %d"), reader->status); 1639 } 1640 1641 static void receive_packfile_uris(struct packet_reader *reader, 1642 struct string_list *uris) 1643 { 1644 process_section_header(reader, "packfile-uris", 0); 1645 while (packet_reader_read(reader) == PACKET_READ_NORMAL) { 1646 if (reader->pktlen < the_hash_algo->hexsz || 1647 reader->line[the_hash_algo->hexsz] != ' ') 1648 die("expected ' ', got: %s", reader->line); 1649 1650 string_list_append(uris, reader->line); 1651 } 1652 if (reader->status != PACKET_READ_DELIM) 1653 die("expected DELIM"); 1654 } 1655 1656 enum fetch_state { 1657 FETCH_CHECK_LOCAL = 0, 1658 FETCH_SEND_REQUEST, 1659 FETCH_PROCESS_ACKS, 1660 FETCH_GET_PACK, 1661 FETCH_DONE, 1662 }; 1663 1664 static void do_check_stateless_delimiter(int stateless_rpc, 1665 struct packet_reader *reader) 1666 { 1667 check_stateless_delimiter(stateless_rpc, reader, 1668 _("git fetch-pack: expected response end packet")); 1669 } 1670 1671 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args, 1672 int fd[2], 1673 const struct ref *orig_ref, 1674 struct ref **sought, int nr_sought, 1675 struct oid_array *shallows, 1676 struct shallow_info *si, 1677 struct string_list *pack_lockfiles) 1678 { 1679 struct repository *r = the_repository; 1680 struct fsck_options fsck_options; 1681 struct ref *ref = copy_ref_list(orig_ref); 1682 enum fetch_state state = FETCH_CHECK_LOCAL; 1683 struct oidset common = OIDSET_INIT; 1684 struct oidset negotiation_include_oids = OIDSET_INIT; 1685 struct packet_reader reader; 1686 int in_vain = 0, negotiation_started = 0; 1687 int negotiation_round = 0; 1688 int haves_to_send = INITIAL_FLUSH; 1689 struct fetch_negotiator negotiator_alloc; 1690 struct fetch_negotiator *negotiator; 1691 int seen_ack = 0; 1692 struct object_id common_oid; 1693 int received_ready = 0; 1694 struct string_list packfile_uris = STRING_LIST_INIT_DUP; 1695 int i; 1696 struct strvec index_pack_args = STRVEC_INIT; 1697 const char *promisor_remote_config; 1698 1699 fsck_options_init(&fsck_options, the_repository, FSCK_OPTIONS_MISSING_GITMODULES); 1700 1701 if (server_feature_v2("promisor-remote", &promisor_remote_config)) 1702 promisor_remote_reply(promisor_remote_config, NULL); 1703 1704 if (args->filter_options.choice == LOFC_AUTO) { 1705 struct strbuf errbuf = STRBUF_INIT; 1706 char *constructed_filter = promisor_remote_construct_filter(r); 1707 1708 list_objects_filter_release(&args->filter_options); 1709 /* Disallow 'auto' as a result of the resolution of this 'auto' filter below */ 1710 args->filter_options.allow_auto_filter = 0; 1711 1712 if (constructed_filter && 1713 gently_parse_list_objects_filter(&args->filter_options, 1714 constructed_filter, 1715 &errbuf)) 1716 die(_("couldn't resolve 'auto' filter '%s': %s"), 1717 constructed_filter, errbuf.buf); 1718 1719 free(constructed_filter); 1720 strbuf_release(&errbuf); 1721 } 1722 1723 negotiator = &negotiator_alloc; 1724 if (args->refetch) 1725 fetch_negotiator_init_noop(negotiator); 1726 else 1727 fetch_negotiator_init(r, negotiator); 1728 1729 packet_reader_init(&reader, fd[0], NULL, 0, 1730 PACKET_READ_CHOMP_NEWLINE | 1731 PACKET_READ_DIE_ON_ERR_PACKET); 1732 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) && 1733 server_supports_feature("fetch", "sideband-all", 0)) { 1734 reader.use_sideband = 1; 1735 reader.me = "fetch-pack"; 1736 } 1737 1738 /* v2 supports these by default */ 1739 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; 1740 use_sideband = 2; 1741 if (args->depth > 0 || args->deepen_since || args->deepen_not) 1742 args->deepen = 1; 1743 1744 while (state != FETCH_DONE) { 1745 switch (state) { 1746 case FETCH_CHECK_LOCAL: 1747 sort_ref_list(&ref, ref_compare_name); 1748 QSORT(sought, nr_sought, cmp_ref_by_name); 1749 1750 /* Filter 'ref' by 'sought' and those that aren't local */ 1751 mark_complete_and_common_ref(negotiator, args, &ref); 1752 filter_refs(args, &ref, sought, nr_sought); 1753 if (!args->refetch && everything_local(args, &ref)) 1754 state = FETCH_DONE; 1755 else 1756 state = FETCH_SEND_REQUEST; 1757 1758 mark_tips(negotiator, args->negotiation_restrict_tips); 1759 add_oids_to_set(args->negotiation_include_tips, 1760 &negotiation_include_oids); 1761 for_each_cached_alternate(negotiator, 1762 insert_one_alternate_object); 1763 break; 1764 case FETCH_SEND_REQUEST: 1765 if (!negotiation_started) { 1766 negotiation_started = 1; 1767 trace2_region_enter("fetch-pack", 1768 "negotiation_v2", 1769 the_repository); 1770 } 1771 negotiation_round++; 1772 trace2_region_enter_printf("negotiation_v2", "round", 1773 the_repository, "%d", 1774 negotiation_round); 1775 if (send_fetch_request(negotiator, fd[1], args, ref, 1776 &common, 1777 &haves_to_send, &in_vain, 1778 reader.use_sideband, 1779 seen_ack, 1780 &negotiation_include_oids)) { 1781 trace2_region_leave_printf("negotiation_v2", "round", 1782 the_repository, "%d", 1783 negotiation_round); 1784 state = FETCH_GET_PACK; 1785 } 1786 else 1787 state = FETCH_PROCESS_ACKS; 1788 break; 1789 case FETCH_PROCESS_ACKS: 1790 /* Process ACKs/NAKs */ 1791 process_section_header(&reader, "acknowledgments", 0); 1792 while (process_ack(negotiator, &reader, &common_oid, 1793 &received_ready)) { 1794 in_vain = 0; 1795 seen_ack = 1; 1796 oidset_insert(&common, &common_oid); 1797 } 1798 trace2_region_leave_printf("negotiation_v2", "round", 1799 the_repository, "%d", 1800 negotiation_round); 1801 if (received_ready) { 1802 /* 1803 * Don't check for response delimiter; get_pack() will 1804 * read the rest of this response. 1805 */ 1806 state = FETCH_GET_PACK; 1807 } else { 1808 do_check_stateless_delimiter(args->stateless_rpc, &reader); 1809 state = FETCH_SEND_REQUEST; 1810 } 1811 break; 1812 case FETCH_GET_PACK: 1813 trace2_region_leave("fetch-pack", 1814 "negotiation_v2", 1815 the_repository); 1816 trace2_data_intmax("negotiation_v2", the_repository, 1817 "total_rounds", negotiation_round); 1818 /* Check for shallow-info section */ 1819 if (process_section_header(&reader, "shallow-info", 1)) 1820 receive_shallow_info(args, &reader, shallows, si); 1821 1822 if (process_section_header(&reader, "wanted-refs", 1)) 1823 receive_wanted_refs(&reader, sought, nr_sought); 1824 1825 /* get the pack(s) */ 1826 if (git_env_bool("GIT_TRACE_REDACT", 1)) 1827 reader.options |= PACKET_READ_REDACT_URI_PATH; 1828 if (process_section_header(&reader, "packfile-uris", 1)) 1829 receive_packfile_uris(&reader, &packfile_uris); 1830 /* We don't expect more URIs. Reset to avoid expensive URI check. */ 1831 reader.options &= ~PACKET_READ_REDACT_URI_PATH; 1832 1833 process_section_header(&reader, "packfile", 0); 1834 1835 /* 1836 * this is the final request we'll make of the server; 1837 * do a half-duplex shutdown to indicate that they can 1838 * hang up as soon as the pack is sent. 1839 */ 1840 close(fd[1]); 1841 fd[1] = -1; 1842 1843 if (get_pack(args, fd, pack_lockfiles, 1844 packfile_uris.nr ? &index_pack_args : NULL, 1845 sought, nr_sought, &fsck_options.gitmodules_found)) 1846 die(_("git fetch-pack: fetch failed.")); 1847 do_check_stateless_delimiter(args->stateless_rpc, &reader); 1848 1849 state = FETCH_DONE; 1850 break; 1851 case FETCH_DONE: 1852 continue; 1853 } 1854 } 1855 1856 if (packfile_uris.nr) { 1857 trace2_region_enter("fetch-pack", "packfile-uris", 1858 the_repository); 1859 trace2_data_intmax("fetch-pack", the_repository, 1860 "packfile-uris/count", packfile_uris.nr); 1861 } 1862 1863 for (i = 0; i < packfile_uris.nr; i++) { 1864 bool created_keep; 1865 int j; 1866 struct child_process cmd = CHILD_PROCESS_INIT; 1867 char packhash[GIT_MAX_HEXSZ + 1]; 1868 const char *uri = packfile_uris.items[i].string + 1869 the_hash_algo->hexsz + 1; 1870 1871 strvec_push(&cmd.args, "http-fetch"); 1872 strvec_pushf(&cmd.args, "--packfile=%.*s", 1873 (int) the_hash_algo->hexsz, 1874 packfile_uris.items[i].string); 1875 for (j = 0; j < index_pack_args.nr; j++) 1876 strvec_pushf(&cmd.args, "--index-pack-arg=%s", 1877 index_pack_args.v[j]); 1878 strvec_push(&cmd.args, uri); 1879 cmd.git_cmd = 1; 1880 cmd.no_stdin = 1; 1881 cmd.out = -1; 1882 if (start_command(&cmd)) 1883 die("fetch-pack: unable to spawn http-fetch"); 1884 1885 if (read_in_full(cmd.out, packhash, 5) != 5 || 1886 (memcmp(packhash, "keep\t", 5) && 1887 memcmp(packhash, "pack\t", 5))) 1888 die("fetch-pack: expected pack or keep then TAB at start of http-fetch output"); 1889 created_keep = !memcmp(packhash, "keep\t", 5); 1890 1891 if (read_in_full(cmd.out, packhash, 1892 the_hash_algo->hexsz + 1) != the_hash_algo->hexsz + 1 || 1893 packhash[the_hash_algo->hexsz] != '\n') 1894 die("fetch-pack: expected hash then LF in http-fetch output"); 1895 packhash[the_hash_algo->hexsz] = '\0'; 1896 1897 parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found); 1898 1899 close(cmd.out); 1900 1901 if (finish_command(&cmd)) 1902 die("fetch-pack: unable to finish http-fetch"); 1903 1904 if (memcmp(packfile_uris.items[i].string, packhash, 1905 the_hash_algo->hexsz)) 1906 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s", 1907 uri, (int) the_hash_algo->hexsz, 1908 packfile_uris.items[i].string); 1909 1910 if (created_keep) 1911 string_list_append_nodup(pack_lockfiles, 1912 xstrfmt("%s/pack/pack-%s.keep", 1913 repo_get_object_directory(the_repository), 1914 packhash)); 1915 } 1916 1917 if (packfile_uris.nr) 1918 trace2_region_leave("fetch-pack", "packfile-uris", 1919 the_repository); 1920 1921 string_list_clear(&packfile_uris, 0); 1922 strvec_clear(&index_pack_args); 1923 1924 if (fsck_finish(&fsck_options)) 1925 die("fsck failed"); 1926 1927 if (negotiator) 1928 negotiator->release(negotiator); 1929 1930 fsck_options_clear(&fsck_options); 1931 oidset_clear(&common); 1932 oidset_clear(&negotiation_include_oids); 1933 return ref; 1934 } 1935 1936 int fetch_pack_fsck_config(const char *var, const char *value, 1937 struct strbuf *msg_types) 1938 { 1939 const char *msg_id; 1940 1941 if (strcmp(var, "fetch.fsck.skiplist") == 0) { 1942 char *path ; 1943 1944 if (git_config_pathname(&path, var, value)) 1945 return -1; 1946 if (path) 1947 strbuf_addf(msg_types, "%cskiplist=%s", 1948 msg_types->len ? ',' : '=', path); 1949 free(path); 1950 return 0; 1951 } 1952 1953 if (skip_prefix(var, "fetch.fsck.", &msg_id)) { 1954 if (!value) 1955 return config_error_nonbool(var); 1956 if (is_valid_msg_type(msg_id, value)) 1957 strbuf_addf(msg_types, "%c%s=%s", 1958 msg_types->len ? ',' : '=', msg_id, value); 1959 else 1960 warning("Skipping unknown msg id '%s'", msg_id); 1961 return 0; 1962 } 1963 1964 return 1; 1965 } 1966 1967 static int fetch_pack_config_cb(const char *var, const char *value, 1968 const struct config_context *ctx, void *cb) 1969 { 1970 int ret = fetch_pack_fsck_config(var, value, &fsck_msg_types); 1971 if (ret > 0) 1972 return git_default_config(var, value, ctx, cb); 1973 1974 return ret; 1975 } 1976 1977 static void fetch_pack_config(void) 1978 { 1979 repo_config_get_int(the_repository, "fetch.unpacklimit", &fetch_unpack_limit); 1980 repo_config_get_int(the_repository, "transfer.unpacklimit", &transfer_unpack_limit); 1981 repo_config_get_bool(the_repository, "repack.usedeltabaseoffset", &prefer_ofs_delta); 1982 repo_config_get_bool(the_repository, "fetch.fsckobjects", &fetch_fsck_objects); 1983 repo_config_get_bool(the_repository, "transfer.fsckobjects", &transfer_fsck_objects); 1984 if (!uri_protocols.nr) { 1985 char *str; 1986 1987 if (!repo_config_get_string(the_repository, "fetch.uriprotocols", &str) && str) { 1988 string_list_split(&uri_protocols, str, ",", -1); 1989 free(str); 1990 } 1991 } 1992 1993 repo_config(the_repository, fetch_pack_config_cb, NULL); 1994 } 1995 1996 static void fetch_pack_setup(void) 1997 { 1998 static int did_setup; 1999 if (did_setup) 2000 return; 2001 fetch_pack_config(); 2002 if (0 <= fetch_unpack_limit) 2003 unpack_limit = fetch_unpack_limit; 2004 else if (0 <= transfer_unpack_limit) 2005 unpack_limit = transfer_unpack_limit; 2006 did_setup = 1; 2007 } 2008 2009 static int remove_duplicates_in_refs(struct ref **ref, int nr) 2010 { 2011 struct string_list names = STRING_LIST_INIT_NODUP; 2012 int src, dst; 2013 2014 for (src = dst = 0; src < nr; src++) { 2015 struct string_list_item *item; 2016 item = string_list_insert(&names, ref[src]->name); 2017 if (item->util) 2018 continue; /* already have it */ 2019 item->util = ref[src]; 2020 if (src != dst) 2021 ref[dst] = ref[src]; 2022 dst++; 2023 } 2024 for (src = dst; src < nr; src++) 2025 ref[src] = NULL; 2026 string_list_clear(&names, 0); 2027 return dst; 2028 } 2029 2030 static void update_shallow(struct fetch_pack_args *args, 2031 struct ref **sought, int nr_sought, 2032 struct shallow_info *si) 2033 { 2034 struct oid_array ref = OID_ARRAY_INIT; 2035 int *status; 2036 int i; 2037 2038 if (args->deepen && alternate_shallow_file) { 2039 if (*alternate_shallow_file == '\0') { /* --unshallow */ 2040 unlink_or_warn(git_path_shallow(the_repository)); 2041 rollback_shallow_file(the_repository, &shallow_lock); 2042 } else 2043 commit_shallow_file(the_repository, &shallow_lock); 2044 alternate_shallow_file = NULL; 2045 return; 2046 } 2047 2048 if (!si->shallow || !si->shallow->nr) 2049 return; 2050 2051 if (args->cloning) { 2052 /* 2053 * remote is shallow, but this is a clone, there are 2054 * no objects in repo to worry about. Accept any 2055 * shallow points that exist in the pack (iow in repo 2056 * after get_pack() and odb_reprepare()) 2057 */ 2058 struct oid_array extra = OID_ARRAY_INIT; 2059 struct object_id *oid = si->shallow->oid; 2060 for (i = 0; i < si->shallow->nr; i++) 2061 if (odb_has_object(the_repository->objects, &oid[i], 2062 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR)) 2063 oid_array_append(&extra, &oid[i]); 2064 if (extra.nr) { 2065 setup_alternate_shallow(&shallow_lock, 2066 &alternate_shallow_file, 2067 &extra); 2068 commit_shallow_file(the_repository, &shallow_lock); 2069 alternate_shallow_file = NULL; 2070 } 2071 oid_array_clear(&extra); 2072 return; 2073 } 2074 2075 if (!si->nr_ours && !si->nr_theirs) 2076 return; 2077 2078 remove_nonexistent_theirs_shallow(si); 2079 if (!si->nr_ours && !si->nr_theirs) 2080 return; 2081 for (i = 0; i < nr_sought; i++) 2082 oid_array_append(&ref, &sought[i]->old_oid); 2083 si->ref = &ref; 2084 2085 if (args->update_shallow) { 2086 /* 2087 * remote is also shallow, .git/shallow may be updated 2088 * so all refs can be accepted. Make sure we only add 2089 * shallow roots that are actually reachable from new 2090 * refs. 2091 */ 2092 struct oid_array extra = OID_ARRAY_INIT; 2093 struct object_id *oid = si->shallow->oid; 2094 assign_shallow_commits_to_refs(si, NULL, NULL); 2095 if (!si->nr_ours && !si->nr_theirs) { 2096 oid_array_clear(&ref); 2097 return; 2098 } 2099 for (i = 0; i < si->nr_ours; i++) 2100 oid_array_append(&extra, &oid[si->ours[i]]); 2101 for (i = 0; i < si->nr_theirs; i++) 2102 oid_array_append(&extra, &oid[si->theirs[i]]); 2103 setup_alternate_shallow(&shallow_lock, 2104 &alternate_shallow_file, 2105 &extra); 2106 commit_shallow_file(the_repository, &shallow_lock); 2107 oid_array_clear(&extra); 2108 oid_array_clear(&ref); 2109 alternate_shallow_file = NULL; 2110 return; 2111 } 2112 2113 /* 2114 * remote is also shallow, check what ref is safe to update 2115 * without updating .git/shallow 2116 */ 2117 CALLOC_ARRAY(status, nr_sought); 2118 assign_shallow_commits_to_refs(si, NULL, status); 2119 if (si->nr_ours || si->nr_theirs) { 2120 for (i = 0; i < nr_sought; i++) 2121 if (status[i]) 2122 sought[i]->status = REF_STATUS_REJECT_SHALLOW; 2123 } 2124 free(status); 2125 oid_array_clear(&ref); 2126 } 2127 2128 static const struct object_id *iterate_ref_map(void *cb_data) 2129 { 2130 struct ref **rm = cb_data; 2131 struct ref *ref = *rm; 2132 2133 if (!ref) 2134 return NULL; 2135 *rm = ref->next; 2136 return &ref->old_oid; 2137 } 2138 2139 int fetch_pack_fsck_objects(void) 2140 { 2141 fetch_pack_setup(); 2142 if (fetch_fsck_objects >= 0) 2143 return fetch_fsck_objects; 2144 if (transfer_fsck_objects >= 0) 2145 return transfer_fsck_objects; 2146 return 0; 2147 } 2148 2149 struct ref *fetch_pack(struct fetch_pack_args *args, 2150 int fd[], 2151 const struct ref *ref, 2152 struct ref **sought, int nr_sought, 2153 struct oid_array *shallow, 2154 struct string_list *pack_lockfiles, 2155 enum protocol_version version) 2156 { 2157 struct ref *ref_cpy; 2158 struct shallow_info si; 2159 struct oid_array shallows_scratch = OID_ARRAY_INIT; 2160 2161 fetch_pack_setup(); 2162 if (nr_sought) 2163 nr_sought = remove_duplicates_in_refs(sought, nr_sought); 2164 2165 if (version != protocol_v2 && !ref) { 2166 packet_flush(fd[1]); 2167 die(_("no matching remote head")); 2168 } 2169 if (version == protocol_v2) { 2170 if (shallow->nr) 2171 BUG("Protocol V2 does not provide shallows at this point in the fetch"); 2172 memset(&si, 0, sizeof(si)); 2173 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought, 2174 &shallows_scratch, &si, 2175 pack_lockfiles); 2176 } else { 2177 prepare_shallow_info(&si, shallow); 2178 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought, 2179 &si, pack_lockfiles); 2180 } 2181 odb_reprepare(the_repository->objects); 2182 2183 if (!args->cloning && args->deepen) { 2184 struct check_connected_options opt = CHECK_CONNECTED_INIT; 2185 struct ref *iterator = ref_cpy; 2186 opt.shallow_file = alternate_shallow_file; 2187 if (args->deepen) 2188 opt.is_deepening_fetch = 1; 2189 if (check_connected(the_repository, iterate_ref_map, 2190 &iterator, &opt)) { 2191 error(_("remote did not send all necessary objects")); 2192 free_refs(ref_cpy); 2193 ref_cpy = NULL; 2194 rollback_shallow_file(the_repository, &shallow_lock); 2195 goto cleanup; 2196 } 2197 args->connectivity_checked = 1; 2198 } 2199 2200 update_shallow(args, sought, nr_sought, &si); 2201 cleanup: 2202 clear_shallow_info(&si); 2203 oid_array_clear(&shallows_scratch); 2204 return ref_cpy; 2205 } 2206 2207 static int add_to_object_array(const struct object_id *oid, void *data) 2208 { 2209 struct object_array *a = data; 2210 2211 add_object_array(lookup_object(the_repository, oid), "", a); 2212 return 0; 2213 } 2214 2215 static void clear_common_flag(struct oidset *s) 2216 { 2217 struct oidset_iter iter; 2218 const struct object_id *oid; 2219 oidset_iter_init(s, &iter); 2220 2221 while ((oid = oidset_iter_next(&iter))) { 2222 struct object *obj = lookup_object(the_repository, oid); 2223 obj->flags &= ~COMMON; 2224 } 2225 } 2226 2227 void negotiate_using_fetch(const struct oid_array *negotiation_restrict_tips, 2228 const struct string_list *server_options, 2229 int stateless_rpc, 2230 int fd[], 2231 struct oidset *acked_commits, 2232 const struct oid_array *negotiation_include_tips) 2233 { 2234 struct fetch_negotiator negotiator; 2235 struct packet_reader reader; 2236 struct object_array nt_object_array = OBJECT_ARRAY_INIT; 2237 struct strbuf req_buf = STRBUF_INIT; 2238 struct oidset negotiation_include_oids = OIDSET_INIT; 2239 int haves_to_send = INITIAL_FLUSH; 2240 int in_vain = 0; 2241 int seen_ack = 0; 2242 int last_iteration = 0; 2243 int negotiation_round = 0; 2244 timestamp_t min_generation = GENERATION_NUMBER_INFINITY; 2245 2246 fetch_negotiator_init(the_repository, &negotiator); 2247 mark_tips(&negotiator, negotiation_restrict_tips); 2248 2249 add_oids_to_set(negotiation_include_tips, 2250 &negotiation_include_oids); 2251 2252 packet_reader_init(&reader, fd[0], NULL, 0, 2253 PACKET_READ_CHOMP_NEWLINE | 2254 PACKET_READ_DIE_ON_ERR_PACKET); 2255 2256 oid_array_for_each((struct oid_array *) negotiation_restrict_tips, 2257 add_to_object_array, 2258 &nt_object_array); 2259 2260 trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository); 2261 while (!last_iteration) { 2262 int haves_added; 2263 struct object_id common_oid; 2264 int received_ready = 0; 2265 2266 negotiation_round++; 2267 2268 trace2_region_enter_printf("negotiate_using_fetch", "round", 2269 the_repository, "%d", 2270 negotiation_round); 2271 strbuf_reset(&req_buf); 2272 write_command_and_capabilities(&req_buf, "fetch", server_options); 2273 2274 packet_buf_write(&req_buf, "wait-for-done"); 2275 2276 haves_added = add_haves(&negotiator, &req_buf, &haves_to_send, 2277 &negotiation_include_oids); 2278 in_vain += haves_added; 2279 if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN)) 2280 last_iteration = 1; 2281 2282 trace2_data_intmax("negotiate_using_fetch", the_repository, 2283 "haves_added", haves_added); 2284 trace2_data_intmax("negotiate_using_fetch", the_repository, 2285 "in_vain", in_vain); 2286 2287 /* Send request */ 2288 packet_buf_flush(&req_buf); 2289 if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0) 2290 die_errno(_("unable to write request to remote")); 2291 2292 /* Process ACKs/NAKs */ 2293 process_section_header(&reader, "acknowledgments", 0); 2294 while (process_ack(&negotiator, &reader, &common_oid, 2295 &received_ready)) { 2296 struct commit *commit = lookup_commit(the_repository, 2297 &common_oid); 2298 if (commit) { 2299 timestamp_t generation; 2300 2301 parse_commit_or_die(commit); 2302 commit->object.flags |= COMMON; 2303 generation = commit_graph_generation(commit); 2304 if (generation < min_generation) 2305 min_generation = generation; 2306 } 2307 in_vain = 0; 2308 seen_ack = 1; 2309 oidset_insert(acked_commits, &common_oid); 2310 } 2311 if (received_ready) 2312 die(_("unexpected 'ready' from remote")); 2313 else 2314 do_check_stateless_delimiter(stateless_rpc, &reader); 2315 if (can_all_from_reach_with_flag(the_repository, &nt_object_array, 2316 COMMON, REACH_SCRATCH, 0, 2317 min_generation)) 2318 last_iteration = 1; 2319 trace2_region_leave_printf("negotiation", "round", 2320 the_repository, "%d", 2321 negotiation_round); 2322 } 2323 trace2_region_leave("fetch-pack", "negotiate_using_fetch", the_repository); 2324 trace2_data_intmax("negotiate_using_fetch", the_repository, 2325 "total_rounds", negotiation_round); 2326 2327 clear_common_flag(acked_commits); 2328 object_array_clear(&nt_object_array); 2329 oidset_clear(&negotiation_include_oids); 2330 negotiator.release(&negotiator); 2331 strbuf_release(&req_buf); 2332 } 2333 2334 int report_unmatched_refs(struct ref **sought, int nr_sought) 2335 { 2336 int i, ret = 0; 2337 2338 for (i = 0; i < nr_sought; i++) { 2339 if (!sought[i]) 2340 continue; 2341 switch (sought[i]->match_status) { 2342 case REF_MATCHED: 2343 continue; 2344 case REF_NOT_MATCHED: 2345 error(_("no such remote ref %s"), sought[i]->name); 2346 break; 2347 case REF_UNADVERTISED_NOT_ALLOWED: 2348 error(_("Server does not allow request for unadvertised object %s"), 2349 sought[i]->name); 2350 break; 2351 } 2352 ret = 1; 2353 } 2354 return ret; 2355 }