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