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/builtin/fetch.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 / builtin / fetch.c` 3018 lines ยท 89410 bytes Raw Blame History 1 /* 2 * "git fetch" 3 */ 4 5 #define USE_THE_REPOSITORY_VARIABLE 6 #define DISABLE_SIGN_COMPARE_WARNINGS 7 8 #include "builtin.h" 9 #include "advice.h" 10 #include "config.h" 11 #include "gettext.h" 12 #include "environment.h" 13 #include "hex.h" 14 #include "refs.h" 15 #include "refspec.h" 16 #include "object-name.h" 17 #include "odb.h" 18 #include "odb/source.h" 19 #include "oidset.h" 20 #include "oid-array.h" 21 #include "commit.h" 22 #include "string-list.h" 23 #include "remote.h" 24 #include "transport.h" 25 #include "run-command.h" 26 #include "parse-options.h" 27 #include "sigchain.h" 28 #include "submodule-config.h" 29 #include "submodule.h" 30 #include "connected.h" 31 #include "strvec.h" 32 #include "utf8.h" 33 #include "pager.h" 34 #include "path.h" 35 #include "pkt-line.h" 36 #include "list-objects-filter-options.h" 37 #include "commit-reach.h" 38 #include "branch.h" 39 #include "promisor-remote.h" 40 #include "commit-graph.h" 41 #include "shallow.h" 42 #include "trace.h" 43 #include "trace2.h" 44 #include "bundle-uri.h" 45 46 #define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000) 47 48 static const char * const builtin_fetch_usage[] = { 49 N_("git fetch [] [ [...]]"), 50 N_("git fetch [] "), 51 N_("git fetch --multiple [] [(|)...]"), 52 N_("git fetch --all []"), 53 NULL 54 }; 55 56 enum { 57 TAGS_UNSET = 0, 58 TAGS_DEFAULT = 1, 59 TAGS_SET = 2 60 }; 61 62 enum display_format { 63 DISPLAY_FORMAT_FULL, 64 DISPLAY_FORMAT_COMPACT, 65 DISPLAY_FORMAT_PORCELAIN, 66 }; 67 68 struct display_state { 69 struct strbuf buf; 70 71 int refcol_width; 72 enum display_format format; 73 74 char *url; 75 int url_len, shown_url; 76 }; 77 78 static uint64_t forced_updates_ms = 0; 79 static int prefetch = 0; 80 static int prune = -1; /* unspecified */ 81 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */ 82 83 static int prune_tags = -1; /* unspecified */ 84 #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */ 85 86 static int append, dry_run, force, keep, update_head_ok; 87 static int write_fetch_head = 1; 88 static int verbosity, deepen_relative, set_upstream, refetch; 89 static int progress = -1; 90 static int tags = TAGS_DEFAULT, update_shallow, deepen; 91 static int atomic_fetch; 92 static enum transport_family family; 93 static const char *depth; 94 static const char *deepen_since; 95 static const char *upload_pack; 96 static struct string_list deepen_not = STRING_LIST_INIT_NODUP; 97 static struct strbuf default_rla = STRBUF_INIT; 98 static struct transport *gtransport; 99 static struct transport *gsecondary; 100 static struct refspec refmap; 101 static struct string_list server_options = STRING_LIST_INIT_DUP; 102 static struct string_list negotiation_restrict = STRING_LIST_INIT_NODUP; 103 static struct string_list negotiation_include = STRING_LIST_INIT_NODUP; 104 105 struct fetch_config { 106 enum display_format display_format; 107 char *follow_remote_head_raw; 108 int follow_remote_head_seen; 109 int all; 110 int prune; 111 int prune_tags; 112 int show_forced_updates; 113 int recurse_submodules; 114 int parallel; 115 int submodule_fetch_jobs; 116 }; 117 118 static int git_fetch_config(const char *k, const char *v, 119 const struct config_context *ctx, void *cb) 120 { 121 struct fetch_config *fetch_config = cb; 122 123 if (!strcmp(k, "fetch.all")) { 124 fetch_config->all = git_config_bool(k, v); 125 return 0; 126 } 127 128 if (!strcmp(k, "fetch.prune")) { 129 fetch_config->prune = git_config_bool(k, v); 130 return 0; 131 } 132 133 if (!strcmp(k, "fetch.prunetags")) { 134 fetch_config->prune_tags = git_config_bool(k, v); 135 return 0; 136 } 137 138 if (!strcmp(k, "fetch.showforcedupdates")) { 139 fetch_config->show_forced_updates = git_config_bool(k, v); 140 return 0; 141 } 142 143 if (!strcmp(k, "submodule.recurse")) { 144 int r = git_config_bool(k, v) ? 145 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF; 146 fetch_config->recurse_submodules = r; 147 return 0; 148 } 149 150 if (!strcmp(k, "submodule.fetchjobs")) { 151 fetch_config->submodule_fetch_jobs = parse_submodule_fetchjobs(k, v, ctx->kvi); 152 return 0; 153 } else if (!strcmp(k, "fetch.recursesubmodules")) { 154 fetch_config->recurse_submodules = parse_fetch_recurse_submodules_arg(k, v); 155 return 0; 156 } 157 158 if (!strcmp(k, "fetch.parallel")) { 159 fetch_config->parallel = git_config_int(k, v, ctx->kvi); 160 if (fetch_config->parallel < 0) 161 die(_("fetch.parallel cannot be negative")); 162 if (!fetch_config->parallel) 163 fetch_config->parallel = online_cpus(); 164 return 0; 165 } 166 167 if (!strcmp(k, "fetch.output")) { 168 if (!v) 169 return config_error_nonbool(k); 170 else if (!strcasecmp(v, "full")) 171 fetch_config->display_format = DISPLAY_FORMAT_FULL; 172 else if (!strcasecmp(v, "compact")) 173 fetch_config->display_format = DISPLAY_FORMAT_COMPACT; 174 else 175 die(_("invalid value for '%s': '%s'"), 176 "fetch.output", v); 177 return 0; 178 } 179 180 if (!strcmp(k, "fetch.followremotehead")) { 181 free(fetch_config->follow_remote_head_raw); 182 fetch_config->follow_remote_head_raw = xstrdup_or_null(v); 183 fetch_config->follow_remote_head_seen = 1; 184 return 0; 185 } 186 187 return git_default_config(k, v, ctx, cb); 188 } 189 190 static enum follow_remote_head_settings get_follow_remote_head(const char *setting) 191 { 192 if (!setting) 193 die(_("missing value for 'fetch.followRemoteHEAD'")); 194 else if (!strcmp(setting, "never")) 195 return FOLLOW_REMOTE_NEVER; 196 else if (!strcmp(setting, "create")) 197 return FOLLOW_REMOTE_CREATE; 198 else if (!strcmp(setting, "warn")) 199 return FOLLOW_REMOTE_WARN; 200 else if (!strcmp(setting, "always")) 201 return FOLLOW_REMOTE_ALWAYS; 202 warning(_("unrecognized fetch.followRemoteHEAD value '%s' ignored"), setting); 203 return BUILTIN_FOLLOW_REMOTE_HEAD_DFLT; 204 } 205 206 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset) 207 { 208 BUG_ON_OPT_NEG(unset); 209 210 /* 211 * "git fetch --refmap='' origin foo" 212 * can be used to tell the command not to store anywhere 213 */ 214 refspec_append(opt->value, arg); 215 216 return 0; 217 } 218 219 static void unlock_pack(unsigned int flags) 220 { 221 if (gtransport) 222 transport_unlock_pack(gtransport, flags); 223 if (gsecondary) 224 transport_unlock_pack(gsecondary, flags); 225 } 226 227 static void unlock_pack_atexit(void) 228 { 229 unlock_pack(0); 230 } 231 232 static void unlock_pack_on_signal(int signo) 233 { 234 unlock_pack(TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER); 235 sigchain_pop(signo); 236 raise(signo); 237 } 238 239 static void add_merge_config(struct ref **head, 240 const struct ref *remote_refs, 241 struct branch *branch, 242 struct ref ***tail) 243 { 244 int i; 245 246 for (i = 0; i < branch->merge_nr; i++) { 247 struct ref *rm, **old_tail = *tail; 248 struct refspec_item refspec; 249 250 for (rm = *head; rm; rm = rm->next) { 251 if (branch_merge_matches(branch, i, rm->name)) { 252 rm->fetch_head_status = FETCH_HEAD_MERGE; 253 break; 254 } 255 } 256 if (rm) 257 continue; 258 259 /* 260 * Not fetched to a remote-tracking branch? We need to fetch 261 * it anyway to allow this branch's "branch.$name.merge" 262 * to be honored by 'git pull', but we do not have to 263 * fail if branch.$name.merge is misconfigured to point 264 * at a nonexisting branch. If we were indeed called by 265 * 'git pull', it will notice the misconfiguration because 266 * there is no entry in the resulting FETCH_HEAD marked 267 * for merging. 268 */ 269 memset(&refspec, 0, sizeof(refspec)); 270 refspec.src = branch->merge[i]->src; 271 get_fetch_map(remote_refs, &refspec, tail, 1); 272 for (rm = *old_tail; rm; rm = rm->next) 273 rm->fetch_head_status = FETCH_HEAD_MERGE; 274 } 275 } 276 277 static void create_fetch_oidset(struct ref **head, struct oidset *out) 278 { 279 struct ref *rm = *head; 280 while (rm) { 281 oidset_insert(out, &rm->old_oid); 282 rm = rm->next; 283 } 284 } 285 286 struct refname_hash_entry { 287 struct hashmap_entry ent; 288 struct object_id oid; 289 int ignore; 290 char refname[FLEX_ARRAY]; 291 }; 292 293 static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data UNUSED, 294 const struct hashmap_entry *eptr, 295 const struct hashmap_entry *entry_or_key, 296 const void *keydata) 297 { 298 const struct refname_hash_entry *e1, *e2; 299 300 e1 = container_of(eptr, const struct refname_hash_entry, ent); 301 e2 = container_of(entry_or_key, const struct refname_hash_entry, ent); 302 return strcmp(e1->refname, keydata ? keydata : e2->refname); 303 } 304 305 static struct refname_hash_entry *refname_hash_add(struct hashmap *map, 306 const char *refname, 307 const struct object_id *oid) 308 { 309 struct refname_hash_entry *ent; 310 size_t len = strlen(refname); 311 312 FLEX_ALLOC_MEM(ent, refname, refname, len); 313 hashmap_entry_init(&ent->ent, strhash(refname)); 314 oidcpy(&ent->oid, oid); 315 hashmap_add(map, &ent->ent); 316 return ent; 317 } 318 319 static int add_one_refname(const struct reference *ref, void *cbdata) 320 { 321 struct hashmap *refname_map = cbdata; 322 323 (void) refname_hash_add(refname_map, ref->name, ref->oid); 324 return 0; 325 } 326 327 static void refname_hash_init(struct hashmap *map) 328 { 329 hashmap_init(map, refname_hash_entry_cmp, NULL, 0); 330 } 331 332 static int refname_hash_exists(struct hashmap *map, const char *refname) 333 { 334 return !!hashmap_get_from_hash(map, strhash(refname), refname); 335 } 336 337 static void clear_item(struct refname_hash_entry *item) 338 { 339 item->ignore = 1; 340 } 341 342 343 static void add_already_queued_tags(const char *refname, 344 const struct object_id *old_oid UNUSED, 345 const struct object_id *new_oid, 346 void *cb_data) 347 { 348 struct hashmap *queued_tags = cb_data; 349 if (starts_with(refname, "refs/tags/") && new_oid) 350 (void) refname_hash_add(queued_tags, refname, new_oid); 351 } 352 353 static void find_non_local_tags(const struct ref *refs, 354 struct ref_transaction *transaction, 355 struct ref **head, 356 struct ref ***tail) 357 { 358 struct hashmap existing_refs; 359 struct hashmap remote_refs; 360 struct oidset fetch_oids = OIDSET_INIT; 361 struct string_list remote_refs_list = STRING_LIST_INIT_NODUP; 362 struct string_list_item *remote_ref_item; 363 const struct ref *ref; 364 struct refname_hash_entry *item = NULL; 365 366 refname_hash_init(&existing_refs); 367 refname_hash_init(&remote_refs); 368 create_fetch_oidset(head, &fetch_oids); 369 370 refs_for_each_ref(get_main_ref_store(the_repository), add_one_refname, 371 &existing_refs); 372 373 /* 374 * If we already have a transaction, then we need to filter out all 375 * tags which have already been queued up. 376 */ 377 if (transaction) 378 ref_transaction_for_each_queued_update(transaction, 379 add_already_queued_tags, 380 &existing_refs); 381 382 for (ref = refs; ref; ref = ref->next) { 383 if (!starts_with(ref->name, "refs/tags/")) 384 continue; 385 386 /* 387 * The peeled ref always follows the matching base 388 * ref, so if we see a peeled ref that we don't want 389 * to fetch then we can mark the ref entry in the list 390 * as one to ignore by setting util to NULL. 391 */ 392 if (ends_with(ref->name, "^{}")) { 393 if (item && 394 !odb_has_object(the_repository->objects, &ref->old_oid, 0) && 395 !oidset_contains(&fetch_oids, &ref->old_oid) && 396 !odb_has_object(the_repository->objects, &item->oid, 0) && 397 !oidset_contains(&fetch_oids, &item->oid)) 398 clear_item(item); 399 item = NULL; 400 continue; 401 } 402 403 /* 404 * If item is non-NULL here, then we previously saw a 405 * ref not followed by a peeled reference, so we need 406 * to check if it is a lightweight tag that we want to 407 * fetch. 408 */ 409 if (item && 410 !odb_has_object(the_repository->objects, &item->oid, 0) && 411 !oidset_contains(&fetch_oids, &item->oid)) 412 clear_item(item); 413 414 item = NULL; 415 416 /* skip duplicates and refs that we already have */ 417 if (refname_hash_exists(&remote_refs, ref->name) || 418 refname_hash_exists(&existing_refs, ref->name)) 419 continue; 420 421 item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid); 422 string_list_insert(&remote_refs_list, ref->name); 423 } 424 hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent); 425 426 /* 427 * We may have a final lightweight tag that needs to be 428 * checked to see if it needs fetching. 429 */ 430 if (item && 431 !odb_has_object(the_repository->objects, &item->oid, 0) && 432 !oidset_contains(&fetch_oids, &item->oid)) 433 clear_item(item); 434 435 /* 436 * For all the tags in the remote_refs_list, 437 * add them to the list of refs to be fetched 438 */ 439 for_each_string_list_item(remote_ref_item, &remote_refs_list) { 440 const char *refname = remote_ref_item->string; 441 struct ref *rm; 442 unsigned int hash = strhash(refname); 443 444 item = hashmap_get_entry_from_hash(&remote_refs, hash, refname, 445 struct refname_hash_entry, ent); 446 if (!item) 447 BUG("unseen remote ref?"); 448 449 /* Unless we have already decided to ignore this item... */ 450 if (item->ignore) 451 continue; 452 453 rm = alloc_ref(item->refname); 454 rm->peer_ref = alloc_ref(item->refname); 455 oidcpy(&rm->old_oid, &item->oid); 456 **tail = rm; 457 *tail = &rm->next; 458 } 459 hashmap_clear_and_free(&remote_refs, struct refname_hash_entry, ent); 460 string_list_clear(&remote_refs_list, 0); 461 oidset_clear(&fetch_oids); 462 } 463 464 static void filter_prefetch_refspec(struct refspec *rs) 465 { 466 int i; 467 468 if (!prefetch) 469 return; 470 471 for (i = 0; i < rs->nr; i++) { 472 struct strbuf new_dst = STRBUF_INIT; 473 char *old_dst; 474 const char *sub = NULL; 475 476 if (rs->items[i].negative) 477 continue; 478 if (!rs->items[i].dst || 479 (rs->items[i].src && 480 starts_with(rs->items[i].src, 481 ref_namespace[NAMESPACE_TAGS].ref))) { 482 int j; 483 484 refspec_item_clear(&rs->items[i]); 485 486 for (j = i + 1; j < rs->nr; j++) 487 rs->items[j - 1] = rs->items[j]; 488 rs->nr--; 489 i--; 490 continue; 491 } 492 493 old_dst = rs->items[i].dst; 494 strbuf_addstr(&new_dst, ref_namespace[NAMESPACE_PREFETCH].ref); 495 496 /* 497 * If old_dst starts with "refs/", then place 498 * sub after that prefix. Otherwise, start at 499 * the beginning of the string. 500 */ 501 if (!skip_prefix(old_dst, "refs/", &sub)) 502 sub = old_dst; 503 strbuf_addstr(&new_dst, sub); 504 505 rs->items[i].dst = strbuf_detach(&new_dst, NULL); 506 rs->items[i].force = 1; 507 508 free(old_dst); 509 } 510 } 511 512 static struct ref *get_ref_map(struct remote *remote, 513 const struct ref *remote_refs, 514 struct refspec *rs, 515 int tags, int *autotags) 516 { 517 int i; 518 struct ref *rm; 519 struct ref *ref_map = NULL; 520 struct ref **tail = &ref_map; 521 522 /* opportunistically-updated references: */ 523 struct ref *orefs = NULL, **oref_tail = &orefs; 524 525 struct hashmap existing_refs; 526 int existing_refs_populated = 0; 527 528 filter_prefetch_refspec(rs); 529 if (remote) 530 filter_prefetch_refspec(&remote->fetch); 531 532 if (rs->nr) { 533 struct refspec *fetch_refspec; 534 535 for (i = 0; i < rs->nr; i++) { 536 get_fetch_map(remote_refs, &rs->items[i], &tail, 0); 537 if (rs->items[i].dst && rs->items[i].dst[0]) 538 *autotags = 1; 539 } 540 /* Merge everything on the command line (but not --tags) */ 541 for (rm = ref_map; rm; rm = rm->next) 542 rm->fetch_head_status = FETCH_HEAD_MERGE; 543 544 /* 545 * For any refs that we happen to be fetching via 546 * command-line arguments, the destination ref might 547 * have been missing or have been different than the 548 * remote-tracking ref that would be derived from the 549 * configured refspec. In these cases, we want to 550 * take the opportunity to update their configured 551 * remote-tracking reference. However, we do not want 552 * to mention these entries in FETCH_HEAD at all, as 553 * they would simply be duplicates of existing 554 * entries, so we set them FETCH_HEAD_IGNORE below. 555 * 556 * We compute these entries now, based only on the 557 * refspecs specified on the command line. But we add 558 * them to the list following the refspecs resulting 559 * from the tags option so that one of the latter, 560 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed 561 * by ref_remove_duplicates() in favor of one of these 562 * opportunistic entries with FETCH_HEAD_IGNORE. 563 */ 564 if (refmap.nr) 565 fetch_refspec = &refmap; 566 else 567 fetch_refspec = &remote->fetch; 568 569 for (i = 0; i < fetch_refspec->nr; i++) 570 get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1); 571 } else if (refmap.nr) { 572 die("--refmap option is only meaningful with command-line refspec(s)"); 573 } else { 574 /* Use the defaults */ 575 struct branch *branch = branch_get(NULL); 576 int has_merge = branch_has_merge_config(branch); 577 if (remote && 578 (remote->fetch.nr || 579 /* Note: has_merge implies non-NULL branch->remote_name */ 580 (has_merge && !strcmp(branch->remote_name, remote->name)))) { 581 for (i = 0; i < remote->fetch.nr; i++) { 582 get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0); 583 if (remote->fetch.items[i].dst && 584 remote->fetch.items[i].dst[0]) 585 *autotags = 1; 586 if (!i && !has_merge && ref_map && 587 !remote->fetch.items[0].pattern) 588 ref_map->fetch_head_status = FETCH_HEAD_MERGE; 589 } 590 /* 591 * if the remote we're fetching from is the same 592 * as given in branch..remote, we add the 593 * ref given in branch..merge, too. 594 * 595 * Note: has_merge implies non-NULL branch->remote_name 596 */ 597 if (has_merge && 598 !strcmp(branch->remote_name, remote->name)) 599 add_merge_config(&ref_map, remote_refs, branch, &tail); 600 } else if (!prefetch) { 601 ref_map = get_remote_ref(remote_refs, "HEAD"); 602 if (!ref_map) 603 die(_("couldn't find remote ref HEAD")); 604 ref_map->fetch_head_status = FETCH_HEAD_MERGE; 605 tail = &ref_map->next; 606 } 607 } 608 609 if (tags == TAGS_SET) { 610 struct refspec_item tag_refspec; 611 612 /* also fetch all tags */ 613 refspec_item_init_push(&tag_refspec, TAG_REFSPEC, 614 the_hash_algo); 615 get_fetch_map(remote_refs, &tag_refspec, &tail, 0); 616 refspec_item_clear(&tag_refspec); 617 } else if (tags == TAGS_DEFAULT && *autotags) { 618 find_non_local_tags(remote_refs, NULL, &ref_map, &tail); 619 } 620 621 /* Now append any refs to be updated opportunistically: */ 622 *tail = orefs; 623 for (rm = orefs; rm; rm = rm->next) { 624 rm->fetch_head_status = FETCH_HEAD_IGNORE; 625 tail = &rm->next; 626 } 627 628 /* 629 * apply negative refspecs first, before we remove duplicates. This is 630 * necessary as negative refspecs might remove an otherwise conflicting 631 * duplicate. 632 */ 633 if (rs->nr) 634 ref_map = apply_negative_refspecs(ref_map, rs); 635 else 636 ref_map = apply_negative_refspecs(ref_map, &remote->fetch); 637 638 ref_map = ref_remove_duplicates(ref_map); 639 640 for (rm = ref_map; rm; rm = rm->next) { 641 if (rm->peer_ref) { 642 const char *refname = rm->peer_ref->name; 643 struct refname_hash_entry *peer_item; 644 unsigned int hash = strhash(refname); 645 646 if (!existing_refs_populated) { 647 refname_hash_init(&existing_refs); 648 refs_for_each_ref(get_main_ref_store(the_repository), 649 add_one_refname, 650 &existing_refs); 651 existing_refs_populated = 1; 652 } 653 654 peer_item = hashmap_get_entry_from_hash(&existing_refs, 655 hash, refname, 656 struct refname_hash_entry, ent); 657 if (peer_item) { 658 struct object_id *old_oid = &peer_item->oid; 659 oidcpy(&rm->peer_ref->old_oid, old_oid); 660 } 661 } 662 } 663 if (existing_refs_populated) 664 hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent); 665 666 return ref_map; 667 } 668 669 static int s_update_ref(const char *action, 670 struct ref *ref, 671 struct ref_transaction *transaction, 672 int check_old) 673 { 674 char *msg; 675 char *rla = getenv("GIT_REFLOG_ACTION"); 676 struct strbuf err = STRBUF_INIT; 677 int ret; 678 679 if (dry_run) 680 return 0; 681 if (!rla) 682 rla = default_rla.buf; 683 msg = xstrfmt("%s: %s", rla, action); 684 685 ret = ref_transaction_update(transaction, ref->name, &ref->new_oid, 686 check_old ? &ref->old_oid : NULL, 687 NULL, NULL, 0, msg, &err); 688 689 if (ret) 690 error("%s", err.buf); 691 strbuf_release(&err); 692 free(msg); 693 return ret; 694 } 695 696 static int refcol_width(const struct ref *ref_map, int compact_format) 697 { 698 const struct ref *ref; 699 int max, width = 10; 700 701 max = term_columns(); 702 if (compact_format) 703 max = max * 2 / 3; 704 705 for (ref = ref_map; ref; ref = ref->next) { 706 int rlen, llen = 0, len; 707 708 if (ref->status == REF_STATUS_REJECT_SHALLOW || 709 !ref->peer_ref || 710 !strcmp(ref->name, "HEAD")) 711 continue; 712 713 /* uptodate lines are only shown on high verbosity level */ 714 if (verbosity <= 0 && oideq(&ref->peer_ref->old_oid, &ref->old_oid)) 715 continue; 716 717 rlen = utf8_strwidth(prettify_refname(ref->name)); 718 if (!compact_format) 719 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name)); 720 721 /* 722 * rough estimation to see if the output line is too long and 723 * should not be counted (we can't do precise calculation 724 * anyway because we don't know if the error explanation part 725 * will be printed in update_local_ref) 726 */ 727 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen; 728 if (len >= max) 729 continue; 730 731 if (width < rlen) 732 width = rlen; 733 } 734 735 return width; 736 } 737 738 static void display_state_init(struct display_state *display_state, struct ref *ref_map, 739 const char *raw_url, enum display_format format) 740 { 741 int i; 742 743 memset(display_state, 0, sizeof(*display_state)); 744 strbuf_init(&display_state->buf, 0); 745 display_state->format = format; 746 747 if (raw_url) 748 display_state->url = transport_anonymize_url(raw_url); 749 else 750 display_state->url = xstrdup("foreign"); 751 752 display_state->url_len = strlen(display_state->url); 753 for (i = display_state->url_len - 1; 0 <= i && display_state->url[i] == '/'; i--) 754 ; 755 display_state->url_len = i + 1; 756 if (4 < i && !strncmp(".git", display_state->url + i - 3, 4)) 757 display_state->url_len = i - 3; 758 759 if (verbosity < 0) 760 return; 761 762 switch (display_state->format) { 763 case DISPLAY_FORMAT_FULL: 764 case DISPLAY_FORMAT_COMPACT: 765 display_state->refcol_width = refcol_width(ref_map, 766 display_state->format == DISPLAY_FORMAT_COMPACT); 767 break; 768 case DISPLAY_FORMAT_PORCELAIN: 769 /* We don't need to precompute anything here. */ 770 break; 771 default: 772 BUG("unexpected display format %d", display_state->format); 773 } 774 } 775 776 static void display_state_release(struct display_state *display_state) 777 { 778 strbuf_release(&display_state->buf); 779 free(display_state->url); 780 } 781 782 static void print_remote_to_local(struct display_state *display_state, 783 const char *remote, const char *local) 784 { 785 strbuf_addf(&display_state->buf, "%-*s -> %s", 786 display_state->refcol_width, remote, local); 787 } 788 789 static int find_and_replace(struct strbuf *haystack, 790 const char *needle, 791 const char *placeholder) 792 { 793 const char *p = NULL; 794 int plen, nlen; 795 796 nlen = strlen(needle); 797 if (ends_with(haystack->buf, needle)) 798 p = haystack->buf + haystack->len - nlen; 799 else 800 p = strstr(haystack->buf, needle); 801 if (!p) 802 return 0; 803 804 if (p > haystack->buf && p[-1] != '/') 805 return 0; 806 807 plen = strlen(p); 808 if (plen > nlen && p[nlen] != '/') 809 return 0; 810 811 strbuf_splice(haystack, p - haystack->buf, nlen, 812 placeholder, strlen(placeholder)); 813 return 1; 814 } 815 816 static void print_compact(struct display_state *display_state, 817 const char *remote, const char *local) 818 { 819 struct strbuf r = STRBUF_INIT; 820 struct strbuf l = STRBUF_INIT; 821 822 if (!strcmp(remote, local)) { 823 strbuf_addf(&display_state->buf, "%-*s -> *", display_state->refcol_width, remote); 824 return; 825 } 826 827 strbuf_addstr(&r, remote); 828 strbuf_addstr(&l, local); 829 830 if (!find_and_replace(&r, local, "*")) 831 find_and_replace(&l, remote, "*"); 832 print_remote_to_local(display_state, r.buf, l.buf); 833 834 strbuf_release(&r); 835 strbuf_release(&l); 836 } 837 838 static void display_ref_update(struct display_state *display_state, char code, 839 const char *summary, const char *error, 840 const char *remote, const char *local, 841 const struct object_id *old_oid, 842 const struct object_id *new_oid, 843 int summary_width) 844 { 845 FILE *f = stderr; 846 847 if (verbosity < 0) 848 return; 849 850 strbuf_reset(&display_state->buf); 851 852 switch (display_state->format) { 853 case DISPLAY_FORMAT_FULL: 854 case DISPLAY_FORMAT_COMPACT: { 855 int width; 856 857 if (!display_state->shown_url) { 858 strbuf_addf(&display_state->buf, _("From %.*s\n"), 859 display_state->url_len, display_state->url); 860 display_state->shown_url = 1; 861 } 862 863 width = (summary_width + strlen(summary) - gettext_width(summary)); 864 remote = prettify_refname(remote); 865 local = prettify_refname(local); 866 867 strbuf_addf(&display_state->buf, " %c %-*s ", code, width, summary); 868 869 if (display_state->format != DISPLAY_FORMAT_COMPACT) 870 print_remote_to_local(display_state, remote, local); 871 else 872 print_compact(display_state, remote, local); 873 874 if (error) 875 strbuf_addf(&display_state->buf, " (%s)", error); 876 877 break; 878 } 879 case DISPLAY_FORMAT_PORCELAIN: 880 strbuf_addf(&display_state->buf, "%c %s %s %s", code, 881 oid_to_hex(old_oid), oid_to_hex(new_oid), local); 882 f = stdout; 883 break; 884 default: 885 BUG("unexpected display format %d", display_state->format); 886 }; 887 strbuf_addch(&display_state->buf, '\n'); 888 889 fputs(display_state->buf.buf, f); 890 } 891 892 struct ref_update_display_info { 893 bool failed; 894 char success_code; 895 char fail_code; 896 char *summary; 897 char *fail_detail; 898 char *success_detail; 899 char *ref; 900 char *remote; 901 struct object_id old_oid; 902 struct object_id new_oid; 903 }; 904 905 struct ref_update_display_info_array { 906 struct ref_update_display_info *info; 907 size_t alloc, nr; 908 }; 909 910 static struct ref_update_display_info *ref_update_display_info_append( 911 struct ref_update_display_info_array *array, 912 char success_code, 913 char fail_code, 914 const char *summary, 915 const char *success_detail, 916 const char *fail_detail, 917 const char *ref, 918 const char *remote, 919 const struct object_id *old_oid, 920 const struct object_id *new_oid) 921 { 922 struct ref_update_display_info *info; 923 924 ALLOC_GROW(array->info, array->nr + 1, array->alloc); 925 info = &array->info[array->nr++]; 926 927 info->failed = false; 928 info->success_code = success_code; 929 info->fail_code = fail_code; 930 info->summary = xstrdup(summary); 931 info->success_detail = xstrdup_or_null(success_detail); 932 info->fail_detail = xstrdup_or_null(fail_detail); 933 info->remote = xstrdup(remote); 934 info->ref = xstrdup(ref); 935 936 oidcpy(&info->old_oid, old_oid); 937 oidcpy(&info->new_oid, new_oid); 938 939 return info; 940 } 941 942 static void ref_update_display_info_set_failed(struct ref_update_display_info *info) 943 { 944 info->failed = true; 945 } 946 947 static void ref_update_display_info_free(struct ref_update_display_info *info) 948 { 949 free(info->summary); 950 free(info->success_detail); 951 free(info->fail_detail); 952 free(info->remote); 953 free(info->ref); 954 } 955 956 static void ref_update_display_info_display(struct ref_update_display_info *info, 957 struct display_state *display_state, 958 int summary_width) 959 { 960 display_ref_update(display_state, 961 info->failed ? info->fail_code : info->success_code, 962 info->summary, 963 info->failed ? info->fail_detail : info->success_detail, 964 info->remote, info->ref, &info->old_oid, 965 &info->new_oid, summary_width); 966 } 967 968 static int update_local_ref(struct ref *ref, 969 struct ref_transaction *transaction, 970 const struct ref *remote_ref, 971 const struct fetch_config *config, 972 struct ref_update_display_info_array *display_array) 973 { 974 struct commit *current = NULL, *updated; 975 int fast_forward = 0; 976 977 if (!odb_has_object(the_repository->objects, &ref->new_oid, 978 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR)) 979 die(_("object %s not found"), oid_to_hex(&ref->new_oid)); 980 981 if (oideq(&ref->old_oid, &ref->new_oid)) { 982 if (verbosity > 0) 983 ref_update_display_info_append(display_array, '=', '=', 984 _("[up to date]"), NULL, 985 NULL, ref->name, 986 remote_ref->name, &ref->old_oid, 987 &ref->new_oid); 988 return 0; 989 } 990 991 if (!update_head_ok && 992 !is_null_oid(&ref->old_oid) && 993 branch_checked_out(ref->name)) { 994 struct ref_update_display_info *info; 995 /* 996 * If this is the head, and it's not okay to update 997 * the head, and the old value of the head isn't empty... 998 */ 999 info = ref_update_display_info_append(display_array, '!', '!', 1000 _("[rejected]"), NULL, 1001 _("can't fetch into checked-out branch"), 1002 ref->name, remote_ref->name, 1003 &ref->old_oid, &ref->new_oid); 1004 ref_update_display_info_set_failed(info); 1005 return 1; 1006 } 1007 1008 if (!is_null_oid(&ref->old_oid) && 1009 starts_with(ref->name, "refs/tags/")) { 1010 struct ref_update_display_info *info; 1011 1012 if (force || ref->force) { 1013 int r; 1014 1015 r = s_update_ref("updating tag", ref, transaction, 0); 1016 1017 info = ref_update_display_info_append(display_array, 't', '!', 1018 _("[tag update]"), NULL, 1019 _("unable to update local ref"), 1020 ref->name, remote_ref->name, 1021 &ref->old_oid, &ref->new_oid); 1022 if (r) 1023 ref_update_display_info_set_failed(info); 1024 1025 return r; 1026 } else { 1027 info = ref_update_display_info_append(display_array, '!', '!', 1028 _("[rejected]"), NULL, 1029 _("would clobber existing tag"), 1030 ref->name, remote_ref->name, 1031 &ref->old_oid, &ref->new_oid); 1032 ref_update_display_info_set_failed(info); 1033 return 1; 1034 } 1035 } 1036 1037 current = lookup_commit_reference_gently(the_repository, 1038 &ref->old_oid, 1); 1039 updated = lookup_commit_reference_gently(the_repository, 1040 &ref->new_oid, 1); 1041 if (!current || !updated) { 1042 struct ref_update_display_info *info; 1043 const char *msg; 1044 const char *what; 1045 int r; 1046 /* 1047 * Nicely describe the new ref we're fetching. 1048 * Base this on the remote's ref name, as it's 1049 * more likely to follow a standard layout. 1050 */ 1051 if (starts_with(remote_ref->name, "refs/tags/")) { 1052 msg = "storing tag"; 1053 what = _("[new tag]"); 1054 } else if (starts_with(remote_ref->name, "refs/heads/")) { 1055 msg = "storing head"; 1056 what = _("[new branch]"); 1057 } else { 1058 msg = "storing ref"; 1059 what = _("[new ref]"); 1060 } 1061 1062 r = s_update_ref(msg, ref, transaction, 0); 1063 1064 info = ref_update_display_info_append(display_array, '*', '!', 1065 what, NULL, 1066 _("unable to update local ref"), 1067 ref->name, remote_ref->name, 1068 &ref->old_oid, &ref->new_oid); 1069 if (r) 1070 ref_update_display_info_set_failed(info); 1071 1072 return r; 1073 } 1074 1075 if (config->show_forced_updates) { 1076 uint64_t t_before = getnanotime(); 1077 fast_forward = repo_in_merge_bases(the_repository, current, 1078 updated); 1079 if (fast_forward < 0) 1080 die(NULL); 1081 forced_updates_ms += (getnanotime() - t_before) / `1000000`; 1082 } else { 1083 fast_forward = 1; 1084 } 1085 1086 if (fast_forward) { 1087 struct ref_update_display_info *info; 1088 struct strbuf quickref = STRBUF_INIT; 1089 int r; 1090 1091 strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV); 1092 strbuf_addstr(&quickref, ".."); 1093 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV); 1094 r = s_update_ref("fast-forward", ref, transaction, 1); 1095 1096 info = ref_update_display_info_append(display_array, ' ', '!', 1097 quickref.buf, NULL, 1098 _("unable to update local ref"), 1099 ref->name, remote_ref->name, 1100 &ref->old_oid, &ref->new_oid); 1101 if (r) 1102 ref_update_display_info_set_failed(info); 1103 1104 strbuf_release(&quickref); 1105 return r; 1106 } else if (force || ref->force) { 1107 struct ref_update_display_info *info; 1108 struct strbuf quickref = STRBUF_INIT; 1109 int r; 1110 1111 strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV); 1112 strbuf_addstr(&quickref, "..."); 1113 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV); 1114 r = s_update_ref("forced-update", ref, transaction, 1); 1115 1116 info = ref_update_display_info_append(display_array, '+', '!', 1117 quickref.buf, _("forced update"), 1118 _("unable to update local ref"), 1119 ref->name, remote_ref->name, 1120 &ref->old_oid, &ref->new_oid); 1121 1122 if (r) 1123 ref_update_display_info_set_failed(info); 1124 1125 strbuf_release(&quickref); 1126 return r; 1127 } else { 1128 struct ref_update_display_info *info; 1129 info = ref_update_display_info_append(display_array, '!', '!', 1130 _("[rejected]"), NULL, 1131 _("non-fast-forward"), 1132 ref->name, remote_ref->name, 1133 &ref->old_oid, &ref->new_oid); 1134 ref_update_display_info_set_failed(info); 1135 return 1; 1136 } 1137 } 1138 1139 static const struct object_id *iterate_ref_map(void *cb_data) 1140 { 1141 struct ref **rm = cb_data; 1142 struct ref *ref = *rm; 1143 1144 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW) 1145 ref = ref->next; 1146 if (!ref) 1147 return NULL; 1148 *rm = ref->next; 1149 return &ref->old_oid; 1150 } 1151 1152 struct fetch_head { 1153 FILE *fp; 1154 struct strbuf buf; 1155 }; 1156 1157 static int open_fetch_head(struct fetch_head *fetch_head) 1158 { 1159 const char *filename = git_path_fetch_head(the_repository); 1160 1161 if (write_fetch_head) { 1162 fetch_head->fp = fopen(filename, "a"); 1163 if (!fetch_head->fp) 1164 return error_errno(_("cannot open '%s'"), filename); 1165 strbuf_init(&fetch_head->buf, 0); 1166 } else { 1167 fetch_head->fp = NULL; 1168 } 1169 1170 return 0; 1171 } 1172 1173 static void append_fetch_head(struct fetch_head *fetch_head, 1174 const struct object_id *old_oid, 1175 enum fetch_head_status fetch_head_status, 1176 const char *note, 1177 const char *url, size_t url_len) 1178 { 1179 char old_oid_hex[GIT_MAX_HEXSZ + 1]; 1180 const char *merge_status_marker; 1181 size_t i; 1182 1183 if (!fetch_head->fp) 1184 return; 1185 1186 switch (fetch_head_status) { 1187 case FETCH_HEAD_NOT_FOR_MERGE: 1188 merge_status_marker = "not-for-merge"; 1189 break; 1190 case FETCH_HEAD_MERGE: 1191 merge_status_marker = ""; 1192 break; 1193 default: 1194 /* do not write anything to FETCH_HEAD */ 1195 return; 1196 } 1197 1198 strbuf_addf(&fetch_head->buf, "%s\t%s\t%s", 1199 oid_to_hex_r(old_oid_hex, old_oid), merge_status_marker, note); 1200 for (i = 0; i < url_len; ++i) 1201 if ('\n' == url[i]) 1202 strbuf_addstr(&fetch_head->buf, "\\n"); 1203 else 1204 strbuf_addch(&fetch_head->buf, url[i]); 1205 strbuf_addch(&fetch_head->buf, '\n'); 1206 1207 /* 1208 * When using an atomic fetch, we do not want to update FETCH_HEAD if 1209 * any of the reference updates fails. We thus have to write all 1210 * updates to a buffer first and only commit it as soon as all 1211 * references have been successfully updated. 1212 */ 1213 if (!atomic_fetch) { 1214 strbuf_write(&fetch_head->buf, fetch_head->fp); 1215 strbuf_reset(&fetch_head->buf); 1216 } 1217 } 1218 1219 static void commit_fetch_head(struct fetch_head *fetch_head) 1220 { 1221 if (!fetch_head->fp || !atomic_fetch) 1222 return; 1223 strbuf_write(&fetch_head->buf, fetch_head->fp); 1224 } 1225 1226 static void close_fetch_head(struct fetch_head *fetch_head) 1227 { 1228 if (!fetch_head->fp) 1229 return; 1230 1231 fclose(fetch_head->fp); 1232 strbuf_release(&fetch_head->buf); 1233 } 1234 1235 static const char warn_show_forced_updates[] = 1236 N_("fetch normally indicates which branches had a forced update,\n" 1237 "but that check has been disabled; to re-enable, use '--show-forced-updates'\n" 1238 "flag or run 'git config fetch.showForcedUpdates true'"); 1239 static const char warn_time_show_forced_updates[] = 1240 N_("it took %.2f seconds to check forced updates; you can use\n" 1241 "'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n" 1242 "to avoid this check\n"); 1243 1244 static int store_updated_refs(struct display_state *display_state, 1245 int connectivity_checked, 1246 struct ref_transaction *transaction, struct ref *ref_map, 1247 struct fetch_head *fetch_head, 1248 const struct fetch_config *config, 1249 struct ref_update_display_info_array *display_array) 1250 { 1251 int rc = 0; 1252 struct strbuf note = STRBUF_INIT; 1253 const char *what, *kind; 1254 struct ref *rm; 1255 int want_status; 1256 1257 if (!connectivity_checked) { 1258 struct check_connected_options opt = CHECK_CONNECTED_INIT; 1259 1260 opt.exclude_hidden_refs_section = "fetch"; 1261 rm = ref_map; 1262 if (check_connected(the_repository, iterate_ref_map, 1263 &rm, &opt)) { 1264 rc = error(_("%s did not send all necessary objects"), 1265 display_state->url); 1266 goto abort; 1267 } 1268 } 1269 1270 /* 1271 * We do a pass for each fetch_head_status type in their enum order, so 1272 * merged entries are written before not-for-merge. That lets readers 1273 * use FETCH_HEAD as a refname to refer to the ref to be merged. 1274 */ 1275 for (want_status = FETCH_HEAD_MERGE; 1276 want_status <= FETCH_HEAD_IGNORE; 1277 want_status++) { 1278 for (rm = ref_map; rm; rm = rm->next) { 1279 struct ref *ref = NULL; 1280 1281 if (rm->status == REF_STATUS_REJECT_SHALLOW) { 1282 if (want_status == FETCH_HEAD_MERGE) 1283 warning(_("rejected %s because shallow roots are not allowed to be updated"), 1284 rm->peer_ref ? rm->peer_ref->name : rm->name); 1285 continue; 1286 } 1287 1288 /* 1289 * When writing FETCH_HEAD we need to determine whether 1290 * we already have the commit or not. If not, then the 1291 * reference is not for merge and needs to be written 1292 * to the reflog after other commits which we already 1293 * have. We're not interested in this property though 1294 * in case FETCH_HEAD is not to be updated, so we can 1295 * skip the classification in that case. 1296 */ 1297 if (fetch_head->fp) { 1298 struct commit *commit = NULL; 1299 1300 /* 1301 * References in "refs/tags/" are often going to point 1302 * to annotated tags, which are not part of the 1303 * commit-graph. We thus only try to look up refs in 1304 * the graph which are not in that namespace to not 1305 * regress performance in repositories with many 1306 * annotated tags. 1307 */ 1308 if (!starts_with(rm->name, "refs/tags/")) 1309 commit = lookup_commit_in_graph(the_repository, &rm->old_oid); 1310 if (!commit) { 1311 commit = lookup_commit_reference_gently(the_repository, 1312 &rm->old_oid, 1313 1); 1314 if (!commit) 1315 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE; 1316 } 1317 } 1318 1319 if (rm->fetch_head_status != want_status) 1320 continue; 1321 1322 if (rm->peer_ref) { 1323 ref = alloc_ref(rm->peer_ref->name); 1324 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid); 1325 oidcpy(&ref->new_oid, &rm->old_oid); 1326 ref->force = rm->peer_ref->force; 1327 } 1328 1329 if (config->recurse_submodules != RECURSE_SUBMODULES_OFF && 1330 (!rm->peer_ref || !oideq(&ref->old_oid, &ref->new_oid))) { 1331 check_for_new_submodule_commits(&rm->old_oid); 1332 } 1333 1334 if (!strcmp(rm->name, "HEAD")) { 1335 kind = ""; 1336 what = ""; 1337 } else if (skip_prefix(rm->name, "refs/heads/", &what)) { 1338 kind = "branch"; 1339 } else if (skip_prefix(rm->name, "refs/tags/", &what)) { 1340 kind = "tag"; 1341 } else if (skip_prefix(rm->name, "refs/remotes/", &what)) { 1342 kind = "remote-tracking branch"; 1343 } else { 1344 kind = ""; 1345 what = rm->name; 1346 } 1347 1348 strbuf_reset(¬e); 1349 if (*what) { 1350 if (*kind) 1351 strbuf_addf(¬e, "%s ", kind); 1352 strbuf_addf(¬e, "'%s' of ", what); 1353 } 1354 1355 append_fetch_head(fetch_head, &rm->old_oid, 1356 rm->fetch_head_status, 1357 note.buf, display_state->url, 1358 display_state->url_len); 1359 1360 if (ref) { 1361 rc |= update_local_ref(ref, transaction, rm, 1362 config, display_array); 1363 free(ref); 1364 } else if (write_fetch_head || dry_run) { 1365 /* 1366 * Display fetches written to FETCH_HEAD (or 1367 * would be written to FETCH_HEAD, if --dry-run 1368 * is set). 1369 */ 1370 1371 ref_update_display_info_append(display_array, '*', '*', 1372 *kind ? kind : "branch", 1373 NULL, NULL, "FETCH_HEAD", 1374 rm->name, &rm->new_oid, 1375 &rm->old_oid); 1376 } 1377 } 1378 } 1379 1380 if (advice_enabled(ADVICE_FETCH_SHOW_FORCED_UPDATES)) { 1381 if (!config->show_forced_updates) { 1382 warning(_(warn_show_forced_updates)); 1383 } else if (forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) { 1384 warning(_(warn_time_show_forced_updates), 1385 forced_updates_ms / 1000.0); 1386 } 1387 } 1388 1389 abort: 1390 strbuf_release(¬e); 1391 return rc; 1392 } 1393 1394 /* 1395 * We would want to bypass the object transfer altogether if 1396 * everything we are going to fetch already exists and is connected 1397 * locally. 1398 */ 1399 static int check_exist_and_connected(struct ref *ref_map) 1400 { 1401 struct ref *rm = ref_map; 1402 struct check_connected_options opt = CHECK_CONNECTED_INIT; 1403 struct ref *r; 1404 1405 /* 1406 * If we are deepening a shallow clone we already have these 1407 * objects reachable. Running rev-list here will return with 1408 * a good (0) exit status and we'll bypass the fetch that we 1409 * really need to perform. Claiming failure now will ensure 1410 * we perform the network exchange to deepen our history. 1411 */ 1412 if (deepen) 1413 return -1; 1414 1415 /* 1416 * Similarly, if we need to refetch, we always want to perform a full 1417 * fetch ignoring existing objects. 1418 */ 1419 if (refetch) 1420 return -1; 1421 1422 1423 /* 1424 * check_connected() allows objects to merely be promised, but 1425 * we need all direct targets to exist. 1426 */ 1427 for (r = rm; r; r = r->next) { 1428 if (!odb_has_object(the_repository->objects, &r->old_oid, 1429 ODB_HAS_OBJECT_RECHECK_PACKED)) 1430 return -1; 1431 } 1432 1433 opt.quiet = 1; 1434 opt.exclude_hidden_refs_section = "fetch"; 1435 return check_connected(the_repository, iterate_ref_map, &rm, &opt); 1436 } 1437 1438 static int fetch_and_consume_refs(struct display_state *display_state, 1439 struct transport *transport, 1440 struct ref_transaction *transaction, 1441 struct ref *ref_map, 1442 struct fetch_head *fetch_head, 1443 const struct fetch_config *config, 1444 struct ref_update_display_info_array *display_array) 1445 { 1446 int connectivity_checked = 1; 1447 int ret; 1448 1449 /* 1450 * We don't need to perform a fetch in case we can already satisfy all 1451 * refs. 1452 */ 1453 ret = check_exist_and_connected(ref_map); 1454 if (ret) { 1455 trace2_region_enter("fetch", "fetch_refs", the_repository); 1456 ret = transport_fetch_refs(transport, ref_map); 1457 trace2_region_leave("fetch", "fetch_refs", the_repository); 1458 if (ret) 1459 goto out; 1460 connectivity_checked = transport->smart_options ? 1461 transport->smart_options->connectivity_checked : 0; 1462 } 1463 1464 trace2_region_enter("fetch", "consume_refs", the_repository); 1465 ret = store_updated_refs(display_state, connectivity_checked, 1466 transaction, ref_map, fetch_head, config, 1467 display_array); 1468 trace2_region_leave("fetch", "consume_refs", the_repository); 1469 1470 out: 1471 transport_unlock_pack(transport, 0); 1472 return ret; 1473 } 1474 1475 static int prune_refs(struct display_state *display_state, 1476 struct refspec *rs, 1477 struct ref_transaction *transaction, 1478 struct ref *ref_map) 1479 { 1480 int result = 0; 1481 struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map); 1482 struct strbuf err = STRBUF_INIT; 1483 struct string_list refnames = STRING_LIST_INIT_NODUP; 1484 1485 for (ref = stale_refs; ref; ref = ref->next) 1486 string_list_append(&refnames, ref->name); 1487 1488 if (!dry_run) { 1489 if (transaction) { 1490 for (ref = stale_refs; ref; ref = ref->next) { 1491 result = ref_transaction_delete(transaction, ref->name, NULL, 1492 NULL, 0, "fetch: prune", &err); 1493 if (result) 1494 goto cleanup; 1495 } 1496 } else { 1497 result = refs_delete_refs(get_main_ref_store(the_repository), 1498 "fetch: prune", &refnames, 1499 0); 1500 } 1501 } 1502 1503 if (verbosity >= 0) { 1504 int summary_width = transport_summary_width(stale_refs); 1505 1506 for (ref = stale_refs; ref; ref = ref->next) { 1507 display_ref_update(display_state, '-', _("[deleted]"), NULL, 1508 _("(none)"), ref->name, 1509 &ref->new_oid, &ref->old_oid, 1510 summary_width); 1511 } 1512 string_list_sort(&refnames); 1513 refs_warn_dangling_symrefs(get_main_ref_store(the_repository), 1514 stderr, " ", dry_run, &refnames); 1515 } 1516 1517 cleanup: 1518 string_list_clear(&refnames, 0); 1519 strbuf_release(&err); 1520 free_refs(stale_refs); 1521 return result; 1522 } 1523 1524 static void check_not_current_branch(struct ref *ref_map) 1525 { 1526 const char *path; 1527 for (; ref_map; ref_map = ref_map->next) 1528 if (ref_map->peer_ref && 1529 starts_with(ref_map->peer_ref->name, "refs/heads/") && 1530 (path = branch_checked_out(ref_map->peer_ref->name))) 1531 die(_("refusing to fetch into branch '%s' " 1532 "checked out at '%s'"), 1533 ref_map->peer_ref->name, path); 1534 } 1535 1536 static int truncate_fetch_head(void) 1537 { 1538 const char *filename = git_path_fetch_head(the_repository); 1539 FILE *fp = fopen_for_writing(filename); 1540 1541 if (!fp) 1542 return error_errno(_("cannot open '%s'"), filename); 1543 fclose(fp); 1544 return 0; 1545 } 1546 1547 static void set_option(struct transport *transport, const char *name, const char *value) 1548 { 1549 int r = transport_set_option(transport, name, value); 1550 if (r < 0) 1551 die(_("option \"%s\" value \"%s\" is not valid for %s"), 1552 name, value, transport->url); 1553 if (r > 0) 1554 warning(_("option \"%s\" is ignored for %s"), 1555 name, transport->url); 1556 } 1557 1558 1559 static int add_oid(const struct reference *ref, void *cb_data) 1560 { 1561 struct oid_array *oids = cb_data; 1562 1563 oid_array_append(oids, ref->oid); 1564 return 0; 1565 } 1566 1567 static void add_negotiation_tips(struct string_list *input_list, 1568 struct oid_array **output_list, 1569 const char *argname) 1570 { 1571 struct oid_array *oids = xcalloc(1, sizeof(*oids)); 1572 int i; 1573 1574 for (i = 0; i < input_list->nr; i++) { 1575 const char *s = input_list->items[i].string; 1576 struct refs_for_each_ref_options opts = { 1577 .pattern = s, 1578 }; 1579 int old_nr; 1580 if (!has_glob_specials(s)) { 1581 struct object_id oid; 1582 1583 /* Ignore missing reference. */ 1584 if (repo_get_oid(the_repository, s, &oid)) 1585 continue; 1586 /* Fail on missing object pointed by ref. */ 1587 if (!odb_has_object(the_repository->objects, &oid, 0)) 1588 die(_("the object %s does not exist"), s); 1589 1590 oid_array_append(oids, &oid); 1591 continue; 1592 } 1593 old_nr = oids->nr; 1594 refs_for_each_ref_ext(get_main_ref_store(the_repository), 1595 add_oid, oids, &opts); 1596 if (old_nr == oids->nr) 1597 warning(_("ignoring %s=%s because it does not match any refs"), 1598 argname, s); 1599 } 1600 *output_list = oids; 1601 } 1602 1603 static struct transport *prepare_transport(struct remote *remote, int deepen, 1604 struct list_objects_filter_options *filter_options) 1605 { 1606 struct transport *transport; 1607 1608 transport = transport_get(remote, NULL); 1609 transport_set_verbosity(transport, verbosity, progress); 1610 transport->family = family; 1611 if (upload_pack) 1612 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack); 1613 if (keep) 1614 set_option(transport, TRANS_OPT_KEEP, "yes"); 1615 if (depth) 1616 set_option(transport, TRANS_OPT_DEPTH, depth); 1617 if (deepen && deepen_since) 1618 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since); 1619 if (deepen && deepen_not.nr) 1620 set_option(transport, TRANS_OPT_DEEPEN_NOT, 1621 (const char *)&deepen_not); 1622 if (deepen_relative) 1623 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes"); 1624 if (update_shallow) 1625 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes"); 1626 if (refetch) 1627 set_option(transport, TRANS_OPT_REFETCH, "yes"); 1628 if (filter_options->choice) { 1629 const char *spec = 1630 expand_list_objects_filter_spec(filter_options); 1631 set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, spec); 1632 set_option(transport, TRANS_OPT_FROM_PROMISOR, "1"); 1633 } 1634 if (negotiation_restrict.nr) { 1635 if (transport->smart_options) 1636 add_negotiation_tips(&negotiation_restrict, 1637 &transport->smart_options->negotiation_restrict_tips, 1638 "--negotiation-restrict"); 1639 else 1640 warning(_("ignoring %s because the protocol does not support it"), 1641 "--negotiation-restrict"); 1642 } else if (remote->negotiation_restrict.nr) { 1643 struct string_list_item *item; 1644 for_each_string_list_item(item, &remote->negotiation_restrict) 1645 string_list_append(&negotiation_restrict, item->string); 1646 if (transport->smart_options) 1647 add_negotiation_tips(&negotiation_restrict, 1648 &transport->smart_options->negotiation_restrict_tips, 1649 "--negotiation-restrict"); 1650 else { 1651 struct strbuf config_name = STRBUF_INIT; 1652 strbuf_addf(&config_name, "remote.%s.negotiationRestrict", remote->name); 1653 warning(_("ignoring %s because the protocol does not support it"), 1654 config_name.buf); 1655 strbuf_release(&config_name); 1656 } 1657 } 1658 if (negotiation_include.nr) { 1659 if (transport->smart_options) 1660 add_negotiation_tips(&negotiation_include, 1661 &transport->smart_options->negotiation_include_tips, 1662 "--negotiation-include"); 1663 else 1664 warning(_("ignoring %s because the protocol does not support it"), 1665 "--negotiation-include"); 1666 } else if (remote->negotiation_include.nr) { 1667 if (transport->smart_options) { 1668 add_negotiation_tips(&remote->negotiation_include, 1669 &transport->smart_options->negotiation_include_tips, 1670 "--negotiation-include"); 1671 } else { 1672 struct strbuf config_name = STRBUF_INIT; 1673 strbuf_addf(&config_name, "remote.%s.negotiationInclude", remote->name); 1674 warning(_("ignoring %s because the protocol does not support it"), 1675 config_name.buf); 1676 strbuf_release(&config_name); 1677 } 1678 } 1679 return transport; 1680 } 1681 1682 static int backfill_tags(struct display_state *display_state, 1683 struct transport *transport, 1684 struct ref_transaction *transaction, 1685 struct ref *ref_map, 1686 struct fetch_head *fetch_head, 1687 const struct fetch_config *config, 1688 struct ref_update_display_info_array *display_array, 1689 struct list_objects_filter_options *filter_options, 1690 const struct oid_array *fetched_tips) 1691 { 1692 int retcode, cannot_reuse; 1693 1694 /* 1695 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it 1696 * when remote helper is used (setting it to an empty string 1697 * is not unsetting). We could extend the remote helper 1698 * protocol for that, but for now, just force a new connection 1699 * without deepen-since. Similar story for deepen-not. 1700 */ 1701 cannot_reuse = transport->cannot_reuse || 1702 deepen_since || deepen_not.nr; 1703 if (cannot_reuse) { 1704 gsecondary = prepare_transport(transport->remote, 0, filter_options); 1705 transport = gsecondary; 1706 } 1707 1708 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL); 1709 transport_set_option(transport, TRANS_OPT_DEPTH, "0"); 1710 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL); 1711 1712 /* 1713 * References fetched before the tag backfill aren't yet committed 1714 * to disk, so negotiation cannot discover them. Advertise them to 1715 * the server, so that it does not re-send objects we already have. 1716 */ 1717 if (fetched_tips && fetched_tips->nr && transport->smart_options) { 1718 struct oid_array **include_tips = 1719 &transport->smart_options->negotiation_include_tips; 1720 if (!*include_tips) 1721 *include_tips = xcalloc(1, sizeof(**include_tips)); 1722 for (size_t i = 0; i < fetched_tips->nr; i++) 1723 oid_array_append(*include_tips, &fetched_tips->oid[i]); 1724 } 1725 1726 retcode = fetch_and_consume_refs(display_state, transport, transaction, ref_map, 1727 fetch_head, config, display_array); 1728 1729 if (gsecondary) { 1730 transport_disconnect(gsecondary); 1731 gsecondary = NULL; 1732 } 1733 1734 return retcode; 1735 } 1736 1737 static const char *strip_refshead(const char *name){ 1738 skip_prefix(name, "refs/heads/", &name); 1739 return name; 1740 } 1741 1742 static void set_head_advice_msg(const char *remote, const char *head_name) 1743 { 1744 const char message_advice_set_head[] = 1745 N_("Run 'git remote set-head %s %s' to follow the change, or modify\n" 1746 "either of the 'remote.%s.followRemoteHEAD' or 'fetch.followRemoteHEAD'\n" 1747 "configuration variables to handle the situation differently.\n\n" 1748 1749 "Using this specific setting\n\n" 1750 " git config set remote.%s.followRemoteHEAD warn-if-not-%s\n\n" 1751 "will suppress the warning until the remote changes HEAD to something else."); 1752 1753 advise_if_enabled(ADVICE_FETCH_SET_HEAD_WARN, _(message_advice_set_head), 1754 remote, head_name, remote, remote, head_name); 1755 } 1756 1757 static void warn_set_head(const char *remote, const char *head_name, 1758 struct strbuf *buf_prev, int updateres) { 1759 struct strbuf buf_prefix = STRBUF_INIT; 1760 const char *prev_head = NULL; 1761 1762 strbuf_addf(&buf_prefix, "refs/remotes/%s/", remote); 1763 skip_prefix(buf_prev->buf, buf_prefix.buf, &prev_head); 1764 1765 if (prev_head && strcmp(prev_head, head_name)) { 1766 printf("'HEAD' at '%s' is '%s', but we have '%s' locally.\n", 1767 remote, head_name, prev_head); 1768 set_head_advice_msg(remote, head_name); 1769 } 1770 else if (updateres && buf_prev->len) { 1771 printf("'HEAD' at '%s' is '%s', " 1772 "but we have a detached HEAD pointing to '%s' locally.\n", 1773 remote, head_name, buf_prev->buf); 1774 set_head_advice_msg(remote, head_name); 1775 } 1776 strbuf_release(&buf_prefix); 1777 } 1778 1779 static int set_head(const struct ref *remote_refs, struct remote *remote, 1780 int follow_remote_head) 1781 { 1782 int result = 0, create_only, baremirror, was_detached; 1783 struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT, 1784 b_local_head = STRBUF_INIT; 1785 const char *no_warn_branch = remote->no_warn_branch; 1786 char *head_name = NULL; 1787 struct ref *ref, *matches; 1788 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map; 1789 struct refspec_item refspec = { 1790 .force = 0, 1791 .pattern = 1, 1792 .src = (char *) "refs/heads/*", 1793 .dst = (char *) "refs/heads/*", 1794 }; 1795 struct string_list heads = STRING_LIST_INIT_DUP; 1796 struct ref_store *refs = get_main_ref_store(the_repository); 1797 1798 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0); 1799 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"), 1800 fetch_map, REMOTE_GUESS_HEAD_ALL); 1801 for (ref = matches; ref; ref = ref->next) { 1802 string_list_append(&heads, strip_refshead(ref->name)); 1803 } 1804 1805 if (!heads.nr) 1806 result = 1; 1807 else if (heads.nr > 1) 1808 result = 1; 1809 else 1810 head_name = xstrdup(heads.items[0].string); 1811 1812 if (!head_name) 1813 goto cleanup; 1814 baremirror = is_bare_repository(the_repository) && remote->mirror; 1815 create_only = follow_remote_head == FOLLOW_REMOTE_ALWAYS ? 0 : !baremirror; 1816 if (baremirror) { 1817 strbuf_addstr(&b_head, "HEAD"); 1818 strbuf_addf(&b_remote_head, "refs/heads/%s", head_name); 1819 } else { 1820 strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote->name); 1821 strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote->name, head_name); 1822 } 1823 /* make sure it's valid */ 1824 if (!baremirror && !refs_ref_exists(refs, b_remote_head.buf)) { 1825 result = 1; 1826 goto cleanup; 1827 } 1828 was_detached = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf, 1829 "fetch", &b_local_head, create_only); 1830 if (was_detached == -1) { 1831 result = 1; 1832 goto cleanup; 1833 } 1834 if (verbosity >= 0 && 1835 follow_remote_head == FOLLOW_REMOTE_WARN && 1836 (!no_warn_branch || strcmp(no_warn_branch, head_name))) 1837 warn_set_head(remote->name, head_name, &b_local_head, was_detached); 1838 1839 cleanup: 1840 free(head_name); 1841 free_refs(fetch_map); 1842 free_refs(matches); 1843 string_list_clear(&heads, 0); 1844 strbuf_release(&b_head); 1845 strbuf_release(&b_local_head); 1846 strbuf_release(&b_remote_head); 1847 return result; 1848 } 1849 1850 struct ref_rejection_data { 1851 int *retcode; 1852 bool conflict_msg_shown; 1853 bool case_sensitive_msg_shown; 1854 const char *remote_name; 1855 struct strmap *rejected_refs; 1856 }; 1857 1858 static void ref_transaction_rejection_handler(const char *refname, 1859 const struct object_id *old_oid UNUSED, 1860 const struct object_id *new_oid UNUSED, 1861 const char *old_target UNUSED, 1862 const char *new_target UNUSED, 1863 enum ref_transaction_error err, 1864 const char *details, 1865 void *cb_data) 1866 { 1867 struct ref_rejection_data *data = cb_data; 1868 1869 if (err == REF_TRANSACTION_ERROR_CASE_CONFLICT && repo_ignore_case(the_repository) && 1870 !data->case_sensitive_msg_shown) { 1871 error(_("You're on a case-insensitive filesystem, and the remote you are\n" 1872 "trying to fetch from has references that only differ in casing. It\n" 1873 "is impossible to store such references with the 'files' backend. You\n" 1874 "can either accept this as-is, in which case you won't be able to\n" 1875 "store all remote references on disk. Or you can alternatively\n" 1876 "migrate your repository to use the 'reftable' backend with the\n" 1877 "following command:\n\n git refs migrate --ref-storage-format=reftable\n\n" 1878 "Please keep in mind that not all implementations of Git support this\n" 1879 "new format yet. So if you use tools other than Git to access this\n" 1880 "repository it may not be an option to migrate to reftables.\n")); 1881 data->case_sensitive_msg_shown = true; 1882 } else if (err == REF_TRANSACTION_ERROR_NAME_CONFLICT && 1883 !data->conflict_msg_shown) { 1884 error(_("some local refs could not be updated; try running\n" 1885 " 'git remote prune %s' to remove any old, conflicting " 1886 "branches"), data->remote_name); 1887 data->conflict_msg_shown = true; 1888 } else { 1889 if (details) 1890 error("%s", details); 1891 else 1892 error(_("fetching ref %s failed: %s"), 1893 refname, ref_transaction_error_msg(err)); 1894 } 1895 1896 strmap_put(data->rejected_refs, refname, NULL); 1897 *data->retcode = 1; 1898 } 1899 1900 /* 1901 * Commit the reference transaction. If it isn't an atomic transaction, handle 1902 * rejected updates as part of using batched updates. 1903 */ 1904 static int commit_ref_transaction(struct ref_transaction **transaction, 1905 bool is_atomic, const char *remote_name, 1906 struct strmap *rejected_refs, 1907 struct strbuf *err) 1908 { 1909 int retcode = ref_transaction_commit(*transaction, err); 1910 if (retcode) 1911 goto out; 1912 1913 if (!is_atomic) { 1914 struct ref_rejection_data data = { 1915 .conflict_msg_shown = 0, 1916 .remote_name = remote_name, 1917 .retcode = &retcode, 1918 .rejected_refs = rejected_refs, 1919 }; 1920 1921 ref_transaction_for_each_rejected_update(*transaction, 1922 ref_transaction_rejection_handler, 1923 &data); 1924 } 1925 1926 out: 1927 ref_transaction_free(*transaction); 1928 *transaction = NULL; 1929 return retcode; 1930 } 1931 1932 static void collect_updated_tips(struct oidset *tips, struct ref *ref_map) 1933 { 1934 struct ref *rm; 1935 for (rm = ref_map; rm; rm = rm->next) { 1936 struct commit *commit; 1937 /* 1938 * Shallow-rejected refs are not stored and their history 1939 * is incomplete, so skip them. 1940 */ 1941 if (rm->status == REF_STATUS_REJECT_SHALLOW) 1942 continue; 1943 if (is_null_oid(&rm->old_oid)) 1944 continue; 1945 if (rm->peer_ref && 1946 oideq(&rm->old_oid, &rm->peer_ref->old_oid)) 1947 continue; 1948 commit = lookup_commit_reference_gently(the_repository, 1949 &rm->old_oid, 1); 1950 if (commit) 1951 oidset_insert(tips, &commit->object.oid); 1952 } 1953 } 1954 1955 static int do_fetch(struct transport *transport, 1956 struct refspec *rs, 1957 const struct fetch_config *config, 1958 struct list_objects_filter_options *filter_options, 1959 struct oidset *updated_tips) 1960 { 1961 struct ref_transaction *transaction = NULL; 1962 struct ref *ref_map = NULL; 1963 struct display_state display_state = { 0 }; 1964 int autotags = (transport->remote->fetch_tags == 1); 1965 int retcode = 0; 1966 const struct ref *remote_refs; 1967 struct transport_ls_refs_options transport_ls_refs_options = 1968 TRANSPORT_LS_REFS_OPTIONS_INIT; 1969 struct fetch_head fetch_head = { 0 }; 1970 struct strbuf err = STRBUF_INIT; 1971 struct ref_update_display_info_array display_array = { 0 }; 1972 struct strmap rejected_refs = STRMAP_INIT; 1973 int summary_width = 0; 1974 int follow_remote_head = FOLLOW_REMOTE_NEVER; 1975 1976 if (tags == TAGS_DEFAULT) { 1977 if (transport->remote->fetch_tags == 2) 1978 tags = TAGS_SET; 1979 if (transport->remote->fetch_tags == -1) 1980 tags = TAGS_UNSET; 1981 } 1982 1983 /* if not appending, truncate FETCH_HEAD */ 1984 if (!append && write_fetch_head) { 1985 retcode = truncate_fetch_head(); 1986 if (retcode) 1987 goto cleanup; 1988 } 1989 1990 if (rs->nr) { 1991 refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes); 1992 } else { 1993 struct branch *branch = branch_get(NULL); 1994 1995 if (transport->remote->fetch.nr) { 1996 refspec_ref_prefixes(&transport->remote->fetch, 1997 &transport_ls_refs_options.ref_prefixes); 1998 /* 1999 * See remote.c's handling of remote..followRemoteHEAD 2000 * for the analogous, still-unresolved case. 2001 */ 2002 if (transport->remote->follow_remote_head) 2003 follow_remote_head = transport->remote->follow_remote_head; 2004 else if (config->follow_remote_head_seen) 2005 follow_remote_head = get_follow_remote_head(config->follow_remote_head_raw); 2006 else 2007 follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT; 2008 } 2009 if (branch && branch_has_merge_config(branch) && 2010 !strcmp(branch->remote_name, transport->remote->name)) { 2011 int i; 2012 for (i = 0; i < branch->merge_nr; i++) { 2013 strvec_push(&transport_ls_refs_options.ref_prefixes, 2014 branch->merge[i]->src); 2015 } 2016 } 2017 2018 /* 2019 * If there are no refs specified to fetch, then we just 2020 * fetch HEAD; mention that to narrow the advertisement. 2021 */ 2022 if (!transport_ls_refs_options.ref_prefixes.nr) 2023 strvec_push(&transport_ls_refs_options.ref_prefixes, 2024 "HEAD"); 2025 } 2026 2027 if (tags == TAGS_SET || tags == TAGS_DEFAULT) 2028 strvec_push(&transport_ls_refs_options.ref_prefixes, 2029 "refs/tags/"); 2030 2031 if (follow_remote_head != FOLLOW_REMOTE_NEVER) 2032 strvec_push(&transport_ls_refs_options.ref_prefixes, 2033 "HEAD"); 2034 2035 /* 2036 * Only initiate ref listing if we have at least one ref we want to 2037 * know about. 2038 */ 2039 if (transport_ls_refs_options.ref_prefixes.nr) { 2040 trace2_region_enter("fetch", "remote_refs", the_repository); 2041 remote_refs = transport_get_remote_refs(transport, 2042 &transport_ls_refs_options); 2043 trace2_region_leave("fetch", "remote_refs", the_repository); 2044 } else 2045 remote_refs = NULL; 2046 2047 transport_ls_refs_options_release(&transport_ls_refs_options); 2048 2049 ref_map = get_ref_map(transport->remote, remote_refs, rs, 2050 tags, &autotags); 2051 if (!update_head_ok) 2052 check_not_current_branch(ref_map); 2053 2054 retcode = open_fetch_head(&fetch_head); 2055 if (retcode) 2056 goto cleanup; 2057 2058 display_state_init(&display_state, ref_map, transport->url, 2059 config->display_format); 2060 2061 if (atomic_fetch) { 2062 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), 2063 0, &err); 2064 if (!transaction) { 2065 retcode = -1; 2066 goto cleanup; 2067 } 2068 } 2069 2070 if (tags == TAGS_DEFAULT && autotags) 2071 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1"); 2072 if (prune) { 2073 /* 2074 * We only prune based on refspecs specified 2075 * explicitly (via command line or configuration); we 2076 * don't care whether --tags was specified. 2077 */ 2078 if (rs->nr) { 2079 retcode = prune_refs(&display_state, rs, transaction, ref_map); 2080 } else { 2081 retcode = prune_refs(&display_state, &transport->remote->fetch, 2082 transaction, ref_map); 2083 } 2084 if (retcode != 0) 2085 retcode = 1; 2086 } 2087 2088 /* 2089 * If not atomic, we can still use batched updates, which would be much 2090 * more performant. We don't initiate the transaction before pruning, 2091 * since pruning must be an independent step, to avoid F/D conflicts. 2092 * 2093 * TODO: if reference transactions gain logical conflict resolution, we 2094 * can delete and create refs (with F/D conflicts) in the same transaction 2095 * and this can be moved above the 'prune_refs()' block. 2096 */ 2097 if (!transaction) { 2098 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), 2099 REF_TRANSACTION_ALLOW_FAILURE, &err); 2100 if (!transaction) { 2101 retcode = -1; 2102 goto cleanup; 2103 } 2104 } 2105 2106 if (fetch_and_consume_refs(&display_state, transport, transaction, ref_map, 2107 &fetch_head, config, &display_array)) { 2108 retcode = 1; 2109 goto cleanup; 2110 } 2111 2112 /* 2113 * If neither --no-tags nor --tags was specified, do automated tag 2114 * following. 2115 */ 2116 if (tags == TAGS_DEFAULT && autotags) { 2117 struct ref *tags_ref_map = NULL, **tail = &tags_ref_map; 2118 2119 find_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail); 2120 if (tags_ref_map) { 2121 struct oid_array fetched_tips = OID_ARRAY_INIT; 2122 2123 /* 2124 * Collect the tips we just fetched so the tag backfill 2125 * can advertise them to the server as 'haves'. 2126 */ 2127 for (struct ref *rm = ref_map; rm; rm = rm->next) 2128 if (!is_null_oid(&rm->old_oid)) 2129 oid_array_append(&fetched_tips, &rm->old_oid); 2130 2131 /* 2132 * If backfilling of tags fails then we want to tell 2133 * the user so, but we have to continue regardless to 2134 * populate upstream information of the references we 2135 * have already fetched above. The exception though is 2136 * when `--atomic` is passed: in that case we'll abort 2137 * the transaction and don't commit anything. 2138 */ 2139 if (backfill_tags(&display_state, transport, transaction, tags_ref_map, 2140 &fetch_head, config, &display_array, filter_options, 2141 &fetched_tips)) 2142 retcode = 1; 2143 2144 oid_array_clear(&fetched_tips); 2145 } 2146 2147 free_refs(tags_ref_map); 2148 } 2149 2150 if (retcode) 2151 goto cleanup; 2152 2153 if (verbosity >= 0) 2154 summary_width = transport_summary_width(ref_map); 2155 2156 retcode = commit_ref_transaction(&transaction, atomic_fetch, 2157 transport->remote->name, 2158 &rejected_refs, &err); 2159 /* 2160 * With '--atomic', bail out if the transaction fails. Without '--atomic', 2161 * continue to fetch head and perform other post-fetch operations. 2162 */ 2163 if (retcode && atomic_fetch) 2164 goto cleanup; 2165 2166 commit_fetch_head(&fetch_head); 2167 2168 collect_updated_tips(updated_tips, ref_map); 2169 2170 if (set_upstream) { 2171 struct branch *branch = branch_get("HEAD"); 2172 struct ref *rm; 2173 struct ref *source_ref = NULL; 2174 2175 /* 2176 * We're setting the upstream configuration for the 2177 * current branch. The relevant upstream is the 2178 * fetched branch that is meant to be merged with the 2179 * current one, i.e. the one fetched to FETCH_HEAD. 2180 * 2181 * When there are several such branches, consider the 2182 * request ambiguous and err on the safe side by doing 2183 * nothing and just emit a warning. 2184 */ 2185 for (rm = ref_map; rm; rm = rm->next) { 2186 if (!rm->peer_ref) { 2187 if (source_ref) { 2188 warning(_("multiple branches detected, incompatible with --set-upstream")); 2189 goto cleanup; 2190 } else { 2191 source_ref = rm; 2192 } 2193 } 2194 } 2195 if (source_ref) { 2196 if (!branch) { 2197 const char *shortname = source_ref->name; 2198 skip_prefix(shortname, "refs/heads/", &shortname); 2199 2200 warning(_("could not set upstream of HEAD to '%s' from '%s' when " 2201 "it does not point to any branch."), 2202 shortname, transport->remote->name); 2203 goto cleanup; 2204 } 2205 2206 if (!strcmp(source_ref->name, "HEAD") || 2207 starts_with(source_ref->name, "refs/heads/")) 2208 install_branch_config(0, 2209 branch->name, 2210 transport->remote->name, 2211 source_ref->name); 2212 else if (starts_with(source_ref->name, "refs/remotes/")) 2213 warning(_("not setting upstream for a remote remote-tracking branch")); 2214 else if (starts_with(source_ref->name, "refs/tags/")) 2215 warning(_("not setting upstream for a remote tag")); 2216 else 2217 warning(_("unknown branch type")); 2218 } else { 2219 warning(_("no source branch found;\n" 2220 "you need to specify exactly one branch with the --set-upstream option")); 2221 } 2222 } 2223 if (follow_remote_head != FOLLOW_REMOTE_NEVER) { 2224 /* 2225 * Way too many cases where this can go wrong so let's just 2226 * ignore errors and fail silently for now. 2227 */ 2228 set_head(remote_refs, transport->remote, follow_remote_head); 2229 } 2230 2231 cleanup: 2232 /* 2233 * When using batched updates, we want to commit the non-rejected 2234 * updates and also handle the rejections. 2235 */ 2236 if (retcode && !atomic_fetch && transaction) 2237 commit_ref_transaction(&transaction, false, 2238 transport->remote->name, 2239 &rejected_refs, &err); 2240 2241 for (size_t i = 0; i < display_array.nr; i++) { 2242 struct ref_update_display_info *info = &display_array.info[i]; 2243 2244 if (!info->failed && strmap_contains(&rejected_refs, info->ref)) 2245 ref_update_display_info_set_failed(info); 2246 ref_update_display_info_display(info, &display_state, summary_width); 2247 ref_update_display_info_free(info); 2248 } 2249 2250 if (retcode) { 2251 if (err.len) { 2252 error("%s", err.buf); 2253 strbuf_reset(&err); 2254 } 2255 if (transaction && ref_transaction_abort(transaction, &err) && 2256 err.len) 2257 error("%s", err.buf); 2258 transaction = NULL; 2259 } 2260 2261 if (transaction) 2262 ref_transaction_free(transaction); 2263 2264 free(display_array.info); 2265 strmap_clear(&rejected_refs, 0); 2266 display_state_release(&display_state); 2267 close_fetch_head(&fetch_head); 2268 strbuf_release(&err); 2269 free_refs(ref_map); 2270 return retcode; 2271 } 2272 2273 static int get_one_remote_for_fetch(struct remote *remote, void *priv) 2274 { 2275 struct string_list *list = priv; 2276 if (!remote->skip_default_update) 2277 string_list_append(list, remote->name); 2278 return 0; 2279 } 2280 2281 static void add_options_to_argv(struct strvec *argv, 2282 const struct fetch_config *config) 2283 { 2284 if (dry_run) 2285 strvec_push(argv, "--dry-run"); 2286 if (prune != -1) 2287 strvec_push(argv, prune ? "--prune" : "--no-prune"); 2288 if (prune_tags != -1) 2289 strvec_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags"); 2290 if (update_head_ok) 2291 strvec_push(argv, "--update-head-ok"); 2292 if (force) 2293 strvec_push(argv, "--force"); 2294 if (keep) 2295 strvec_push(argv, "--keep"); 2296 if (config->recurse_submodules == RECURSE_SUBMODULES_ON) 2297 strvec_push(argv, "--recurse-submodules"); 2298 else if (config->recurse_submodules == RECURSE_SUBMODULES_OFF) 2299 strvec_push(argv, "--no-recurse-submodules"); 2300 else if (config->recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) 2301 strvec_push(argv, "--recurse-submodules=on-demand"); 2302 if (tags == TAGS_SET) 2303 strvec_push(argv, "--tags"); 2304 else if (tags == TAGS_UNSET) 2305 strvec_push(argv, "--no-tags"); 2306 if (verbosity >= 2) 2307 strvec_push(argv, "-v"); 2308 if (verbosity >= 1) 2309 strvec_push(argv, "-v"); 2310 else if (verbosity < 0) 2311 strvec_push(argv, "-q"); 2312 if (family == TRANSPORT_FAMILY_IPV4) 2313 strvec_push(argv, "--ipv4"); 2314 else if (family == TRANSPORT_FAMILY_IPV6) 2315 strvec_push(argv, "--ipv6"); 2316 if (!write_fetch_head) 2317 strvec_push(argv, "--no-write-fetch-head"); 2318 if (config->display_format == DISPLAY_FORMAT_PORCELAIN) 2319 strvec_pushf(argv, "--porcelain"); 2320 } 2321 2322 /* Fetch multiple remotes in parallel */ 2323 2324 struct parallel_fetch_state { 2325 const char **argv; 2326 struct string_list *remotes; 2327 int next, result; 2328 const struct fetch_config *config; 2329 }; 2330 2331 static int fetch_next_remote(struct child_process *cp, 2332 struct strbuf *out UNUSED, 2333 void *cb, void **task_cb) 2334 { 2335 struct parallel_fetch_state *state = cb; 2336 char *remote; 2337 2338 if (state->next < 0 || state->next >= state->remotes->nr) 2339 return 0; 2340 2341 remote = state->remotes->items[state->next++].string; 2342 *task_cb = remote; 2343 2344 strvec_pushv(&cp->args, state->argv); 2345 strvec_push(&cp->args, remote); 2346 cp->git_cmd = 1; 2347 2348 if (verbosity >= 0 && state->config->display_format != DISPLAY_FORMAT_PORCELAIN) 2349 printf(_("Fetching %s\n"), remote); 2350 2351 return 1; 2352 } 2353 2354 static int fetch_failed_to_start(struct strbuf *out UNUSED, 2355 void *cb, void *task_cb) 2356 { 2357 struct parallel_fetch_state *state = cb; 2358 const char *remote = task_cb; 2359 2360 state->result = error(_("could not fetch %s"), remote); 2361 2362 return 0; 2363 } 2364 2365 static int fetch_finished(int result, struct strbuf *out, 2366 void *cb, void *task_cb) 2367 { 2368 struct parallel_fetch_state *state = cb; 2369 const char *remote = task_cb; 2370 2371 if (result) { 2372 strbuf_addf(out, _("could not fetch '%s' (exit code: %d)\n"), 2373 remote, result); 2374 state->result = -1; 2375 } 2376 2377 return 0; 2378 } 2379 2380 static int fetch_multiple(struct string_list *list, int max_children, 2381 const struct fetch_config *config) 2382 { 2383 int i, result = 0; 2384 struct strvec argv = STRVEC_INIT; 2385 2386 if (!append && write_fetch_head) { 2387 int errcode = truncate_fetch_head(); 2388 if (errcode) 2389 return errcode; 2390 } 2391 2392 /* 2393 * Cancel out the fetch.bundleURI config when running subprocesses, 2394 * to avoid fetching from the same bundle list multiple times. 2395 */ 2396 strvec_pushl(&argv, "-c", "fetch.bundleURI=", 2397 "fetch", "--append", "--no-auto-gc", 2398 "--no-write-commit-graph", NULL); 2399 for (i = 0; i < server_options.nr; i++) 2400 strvec_pushf(&argv, "--server-option=%s", server_options.items[i].string); 2401 add_options_to_argv(&argv, config); 2402 2403 if (max_children != 1 && list->nr != 1) { 2404 struct parallel_fetch_state state = { argv.v, list, 0, 0, config }; 2405 const struct run_process_parallel_opts opts = { 2406 .tr2_category = "fetch", 2407 .tr2_label = "parallel/fetch", 2408 2409 .processes = max_children, 2410 2411 .get_next_task = &fetch_next_remote, 2412 .start_failure = &fetch_failed_to_start, 2413 .task_finished = &fetch_finished, 2414 .data = &state, 2415 }; 2416 2417 strvec_push(&argv, "--end-of-options"); 2418 2419 run_processes_parallel(&opts); 2420 result = state.result; 2421 } else 2422 for (i = 0; i < list->nr; i++) { 2423 const char *name = list->items[i].string; 2424 struct child_process cmd = CHILD_PROCESS_INIT; 2425 2426 strvec_pushv(&cmd.args, argv.v); 2427 strvec_push(&cmd.args, name); 2428 if (verbosity >= 0 && config->display_format != DISPLAY_FORMAT_PORCELAIN) 2429 printf(_("Fetching %s\n"), name); 2430 cmd.git_cmd = 1; 2431 if (run_command(&cmd)) { 2432 error(_("could not fetch %s"), name); 2433 result = 1; 2434 } 2435 } 2436 2437 strvec_clear(&argv); 2438 return !!result; 2439 } 2440 2441 /* 2442 * Fetching from the promisor remote should use the given filter-spec 2443 * or inherit the default filter-spec from the config. 2444 */ 2445 static inline void fetch_one_setup_partial(struct remote *remote, 2446 struct list_objects_filter_options *filter_options) 2447 { 2448 /* 2449 * Explicit --no-filter argument overrides everything, regardless 2450 * of any prior partial clones and fetches. 2451 */ 2452 if (filter_options->no_filter) 2453 return; 2454 2455 /* 2456 * If no prior partial clone/fetch and the current fetch DID NOT 2457 * request a partial-fetch, do a normal fetch. 2458 */ 2459 if (!repo_has_promisor_remote(the_repository) && !filter_options->choice) 2460 return; 2461 2462 /* 2463 * If this is a partial-fetch request, we enable partial on 2464 * this repo if not already enabled and remember the given 2465 * filter-spec as the default for subsequent fetches to this 2466 * remote if there is currently no default filter-spec. 2467 */ 2468 if (filter_options->choice) { 2469 partial_clone_register(remote->name, filter_options); 2470 return; 2471 } 2472 2473 /* 2474 * Do a partial-fetch from the promisor remote using either the 2475 * explicitly given filter-spec or inherit the filter-spec from 2476 * the config. 2477 */ 2478 if (!filter_options->choice) 2479 partial_clone_get_default_filter_spec(filter_options, remote->name); 2480 return; 2481 } 2482 2483 static int fetch_one(struct remote *remote, int argc, const char **argv, 2484 int prune_tags_ok, int use_stdin_refspecs, 2485 const struct fetch_config *config, 2486 struct list_objects_filter_options *filter_options, 2487 struct oidset *updated_tips) 2488 { 2489 struct refspec rs = REFSPEC_INIT_FETCH(the_hash_algo); 2490 int i; 2491 int exit_code; 2492 int maybe_prune_tags; 2493 int remote_via_config = remote_is_configured(remote, 0); 2494 2495 if (!remote) 2496 die(_("no remote repository specified; please specify either a URL or a\n" 2497 "remote name from which new revisions should be fetched")); 2498 2499 gtransport = prepare_transport(remote, 1, filter_options); 2500 2501 if (prune < 0) { 2502 /* no command line request */ 2503 if (0 <= remote->prune) 2504 prune = remote->prune; 2505 else if (0 <= config->prune) 2506 prune = config->prune; 2507 else 2508 prune = PRUNE_BY_DEFAULT; 2509 } 2510 2511 if (prune_tags < 0) { 2512 /* no command line request */ 2513 if (0 <= remote->prune_tags) 2514 prune_tags = remote->prune_tags; 2515 else if (0 <= config->prune_tags) 2516 prune_tags = config->prune_tags; 2517 else 2518 prune_tags = PRUNE_TAGS_BY_DEFAULT; 2519 } 2520 2521 maybe_prune_tags = prune_tags_ok && prune_tags; 2522 if (maybe_prune_tags && remote_via_config) 2523 refspec_append(&remote->fetch, TAG_REFSPEC); 2524 2525 if (maybe_prune_tags && (argc || !remote_via_config)) 2526 refspec_append(&rs, TAG_REFSPEC); 2527 2528 for (i = 0; i < argc; i++) { 2529 if (!strcmp(argv[i], "tag")) { 2530 i++; 2531 if (i >= argc) 2532 die(_("you need to specify a tag name")); 2533 2534 refspec_appendf(&rs, "refs/tags/%s:refs/tags/%s", 2535 argv[i], argv[i]); 2536 } else { 2537 refspec_append(&rs, argv[i]); 2538 } 2539 } 2540 2541 if (use_stdin_refspecs) { 2542 struct strbuf line = STRBUF_INIT; 2543 while (strbuf_getline_lf(&line, stdin) != EOF) 2544 refspec_append(&rs, line.buf); 2545 strbuf_release(&line); 2546 } 2547 2548 if (server_options.nr) 2549 gtransport->server_options = &server_options; 2550 2551 sigchain_push_common(unlock_pack_on_signal); 2552 atexit(unlock_pack_atexit); 2553 sigchain_push(SIGPIPE, SIG_IGN); 2554 exit_code = do_fetch(gtransport, &rs, config, filter_options, 2555 updated_tips); 2556 sigchain_pop(SIGPIPE); 2557 refspec_clear(&rs); 2558 transport_disconnect(gtransport); 2559 gtransport = NULL; 2560 return exit_code; 2561 } 2562 2563 int cmd_fetch(int argc, 2564 const char **argv, 2565 const char *prefix, 2566 struct repository *repo UNUSED) 2567 { 2568 struct fetch_config config = { 2569 .display_format = DISPLAY_FORMAT_FULL, 2570 .follow_remote_head_raw = NULL, 2571 .follow_remote_head_seen = 0, 2572 .prune = -1, 2573 .prune_tags = -1, 2574 .show_forced_updates = 1, 2575 .recurse_submodules = RECURSE_SUBMODULES_DEFAULT, 2576 .parallel = 1, 2577 .submodule_fetch_jobs = -1, 2578 }; 2579 const char *submodule_prefix = ""; 2580 const char *bundle_uri; 2581 struct string_list list = STRING_LIST_INIT_DUP; 2582 struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT; 2583 struct remote *remote = NULL; 2584 int all = -1, multiple = 0; 2585 int result = 0; 2586 int prune_tags_ok = 1; 2587 int enable_auto_gc = 1; 2588 int unshallow = 0; 2589 int max_jobs = -1; 2590 int recurse_submodules_cli = RECURSE_SUBMODULES_DEFAULT; 2591 int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND; 2592 int fetch_write_commit_graph = -1; 2593 int stdin_refspecs = 0; 2594 int negotiate_only = 0; 2595 int porcelain = 0; 2596 int i; 2597 enum { 2598 GRAPH_WRITE_REACHABLE, 2599 GRAPH_WRITE_TIPS, 2600 GRAPH_WRITE_SKIP, 2601 } graph_write_mode = GRAPH_WRITE_REACHABLE; 2602 struct oidset updated_tips = OIDSET_INIT; 2603 2604 struct option builtin_fetch_options[] = { 2605 OPT__VERBOSITY(&verbosity), 2606 OPT_BOOL(0, "all", &all, 2607 N_("fetch from all remotes")), 2608 OPT_BOOL(0, "set-upstream", &set_upstream, 2609 N_("set upstream for git pull/fetch")), 2610 OPT_BOOL('a', "append", &append, 2611 N_("append to .git/FETCH_HEAD instead of overwriting")), 2612 OPT_BOOL(0, "atomic", &atomic_fetch, 2613 N_("use atomic transaction to update references")), 2614 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"), 2615 N_("path to upload pack on remote end")), 2616 OPT__FORCE(&force, N_("force overwrite of local reference"), 0), 2617 OPT_BOOL('m', "multiple", &multiple, 2618 N_("fetch from multiple remotes")), 2619 OPT_SET_INT('t', "tags", &tags, 2620 N_("fetch all tags and associated objects"), TAGS_SET), 2621 OPT_SET_INT('n', NULL, &tags, 2622 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET), 2623 OPT_INTEGER('j', "jobs", &max_jobs, 2624 N_("number of submodules fetched in parallel")), 2625 OPT_BOOL(0, "prefetch", &prefetch, 2626 N_("modify the refspec to place all refs within refs/prefetch/")), 2627 OPT_BOOL('p', "prune", &prune, 2628 N_("prune remote-tracking branches no longer on remote")), 2629 OPT_BOOL('P', "prune-tags", &prune_tags, 2630 N_("prune local tags no longer on remote and clobber changed tags")), 2631 OPT_CALLBACK_F(0, "recurse-submodules", &recurse_submodules_cli, N_("on-demand"), 2632 N_("control recursive fetching of submodules"), 2633 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules), 2634 OPT_BOOL(0, "dry-run", &dry_run, 2635 N_("dry run")), 2636 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")), 2637 OPT_BOOL(0, "write-fetch-head", &write_fetch_head, 2638 N_("write fetched references to the FETCH_HEAD file")), 2639 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")), 2640 OPT_BOOL('u', "update-head-ok", &update_head_ok, 2641 N_("allow updating of HEAD ref")), 2642 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")), 2643 OPT_STRING(0, "depth", &depth, N_("depth"), 2644 N_("deepen history of shallow clone")), 2645 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"), 2646 N_("deepen history of shallow repository based on time")), 2647 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("ref"), 2648 N_("deepen history of shallow clone, excluding ref")), 2649 OPT_INTEGER(0, "deepen", &deepen_relative, 2650 N_("deepen history of shallow clone")), 2651 OPT_SET_INT_F(0, "unshallow", &unshallow, 2652 N_("convert to a complete repository"), 2653 1, PARSE_OPT_NONEG), 2654 OPT_SET_INT_F(0, "refetch", &refetch, 2655 N_("re-fetch without negotiating common commits"), 2656 1, PARSE_OPT_NONEG), 2657 { 2658 .type = OPTION_STRING, 2659 .long_name = "submodule-prefix", 2660 .value = &submodule_prefix, 2661 .argh = N_("dir"), 2662 .help = N_("prepend this to submodule path output"), 2663 .flags = PARSE_OPT_HIDDEN, 2664 }, 2665 OPT_CALLBACK_F(0, "recurse-submodules-default", 2666 &recurse_submodules_default, N_("on-demand"), 2667 N_("default for recursive fetching of submodules " 2668 "(lower priority than config files)"), 2669 PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules), 2670 OPT_BOOL(0, "update-shallow", &update_shallow, 2671 N_("accept refs that update .git/shallow")), 2672 OPT_CALLBACK_F(0, "refmap", &refmap, N_("refmap"), 2673 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg), 2674 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")), 2675 OPT_IPVERSION(&family), 2676 OPT_STRING_LIST(0, "negotiation-restrict", &negotiation_restrict, N_("revision"), 2677 N_("report that we have only objects reachable from this object")), 2678 OPT_ALIAS(0, "negotiation-tip", "negotiation-restrict"), 2679 OPT_STRING_LIST(0, "negotiation-include", &negotiation_include, N_("revision"), 2680 N_("ensure this ref is always sent as a negotiation have")), 2681 OPT_BOOL(0, "negotiate-only", &negotiate_only, 2682 N_("do not fetch a packfile; instead, print ancestors of negotiation tips")), 2683 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options), 2684 OPT_BOOL(0, "auto-maintenance", &enable_auto_gc, 2685 N_("run 'maintenance --auto' after fetching")), 2686 OPT_BOOL(0, "auto-gc", &enable_auto_gc, 2687 N_("run 'maintenance --auto' after fetching")), 2688 OPT_BOOL(0, "show-forced-updates", &config.show_forced_updates, 2689 N_("check for forced-updates on all updated branches")), 2690 OPT_BOOL(0, "write-commit-graph", &fetch_write_commit_graph, 2691 N_("write the commit-graph after fetching")), 2692 OPT_BOOL(0, "stdin", &stdin_refspecs, 2693 N_("accept refspecs from stdin")), 2694 OPT_END() 2695 }; 2696 2697 filter_options.allow_auto_filter = 1; 2698 2699 refspec_init_fetch(&refmap, the_hash_algo); 2700 2701 packet_trace_identity("fetch"); 2702 2703 /* Record the command line for the reflog */ 2704 strbuf_addstr(&default_rla, "fetch"); 2705 for (i = 1; i < argc; i++) { 2706 /* This handles non-URLs gracefully */ 2707 char *anon = transport_anonymize_url(argv[i]); 2708 2709 strbuf_addf(&default_rla, " %s", anon); 2710 free(anon); 2711 } 2712 2713 repo_config(the_repository, git_fetch_config, &config); 2714 if (the_repository->gitdir) { 2715 prepare_repo_settings(the_repository); 2716 the_repository->settings.command_requires_full_index = 0; 2717 } 2718 2719 argc = parse_options(argc, argv, prefix, 2720 builtin_fetch_options, builtin_fetch_usage, 0); 2721 2722 if (recurse_submodules_cli != RECURSE_SUBMODULES_DEFAULT) 2723 config.recurse_submodules = recurse_submodules_cli; 2724 2725 if (negotiate_only) { 2726 switch (recurse_submodules_cli) { 2727 case RECURSE_SUBMODULES_OFF: 2728 case RECURSE_SUBMODULES_DEFAULT: 2729 /* 2730 * --negotiate-only should never recurse into 2731 * submodules. Skip it by setting recurse_submodules to 2732 * RECURSE_SUBMODULES_OFF. 2733 */ 2734 config.recurse_submodules = RECURSE_SUBMODULES_OFF; 2735 break; 2736 2737 default: 2738 die(_("options '%s' and '%s' cannot be used together"), 2739 "--negotiate-only", "--recurse-submodules"); 2740 } 2741 } 2742 2743 if (config.recurse_submodules != RECURSE_SUBMODULES_OFF) { 2744 int *sfjc = config.submodule_fetch_jobs == -1 2745 ? &config.submodule_fetch_jobs : NULL; 2746 int *rs = config.recurse_submodules == RECURSE_SUBMODULES_DEFAULT 2747 ? &config.recurse_submodules : NULL; 2748 2749 fetch_config_from_gitmodules(the_repository, sfjc, rs); 2750 } 2751 2752 2753 if (porcelain) { 2754 switch (recurse_submodules_cli) { 2755 case RECURSE_SUBMODULES_OFF: 2756 case RECURSE_SUBMODULES_DEFAULT: 2757 /* 2758 * Reference updates in submodules would be ambiguous 2759 * in porcelain mode, so we reject this combination. 2760 */ 2761 config.recurse_submodules = RECURSE_SUBMODULES_OFF; 2762 break; 2763 2764 default: 2765 die(_("options '%s' and '%s' cannot be used together"), 2766 "--porcelain", "--recurse-submodules"); 2767 } 2768 2769 config.display_format = DISPLAY_FORMAT_PORCELAIN; 2770 } 2771 2772 if (deepen_relative) { 2773 if (deepen_relative < 0) 2774 die(_("negative depth in --deepen is not supported")); 2775 if (depth) 2776 die(_("options '%s' and '%s' cannot be used together"), "--deepen", "--depth"); 2777 depth = xstrfmt("%d", deepen_relative); 2778 } 2779 if (unshallow) { 2780 if (depth) 2781 die(_("options '%s' and '%s' cannot be used together"), "--depth", "--unshallow"); 2782 else if (!is_repository_shallow(the_repository)) 2783 die(_("--unshallow on a complete repository does not make sense")); 2784 else 2785 depth = xstrfmt("%d", INFINITE_DEPTH); 2786 } 2787 2788 /* no need to be strict, transport_set_option() will validate it again */ 2789 if (depth && atoi(depth) < 1) 2790 die(_("depth %s is not a positive number"), depth); 2791 if (depth || deepen_since || deepen_not.nr) 2792 deepen = 1; 2793 2794 /* FETCH_HEAD never gets updated in --dry-run mode */ 2795 if (dry_run) 2796 write_fetch_head = 0; 2797 2798 if (!max_jobs) 2799 max_jobs = online_cpus(); 2800 2801 if (!repo_config_get_string_tmp(the_repository, "fetch.bundleuri", &bundle_uri) && 2802 fetch_bundle_uri(the_repository, bundle_uri, NULL)) 2803 warning(_("failed to fetch bundles from '%s'"), bundle_uri); 2804 2805 if (all < 0) { 2806 /* 2807 * no --[no-]all given; 2808 * only use config option if no remote was explicitly specified 2809 */ 2810 all = (!argc) ? config.all : 0; 2811 } 2812 2813 if (all) { 2814 if (argc == 1) 2815 die(_("fetch --all does not take a repository argument")); 2816 else if (argc > 1) 2817 die(_("fetch --all does not make sense with refspecs")); 2818 2819 (void) for_each_remote(get_one_remote_for_fetch, &list); 2820 2821 /* do not do fetch_multiple() of one */ 2822 if (list.nr == 1) 2823 remote = remote_get(list.items[0].string); 2824 } else if (argc == 0) { 2825 /* No arguments -- use default remote */ 2826 remote = remote_get(NULL); 2827 } else if (multiple) { 2828 /* All arguments are assumed to be remotes or groups */ 2829 for (i = 0; i < argc; i++) 2830 if (!add_remote_or_group(argv[i], &list)) 2831 die(_("no such remote or remote group: %s"), 2832 argv[i]); 2833 } else { 2834 /* Single remote or group */ 2835 (void) add_remote_or_group(argv[0], &list); 2836 if (list.nr > 1) { 2837 /* More than one remote */ 2838 if (argc > 1) 2839 die(_("fetching a group and specifying refspecs does not make sense")); 2840 } else { 2841 /* Zero or one remotes */ 2842 remote = remote_get(argv[0]); 2843 prune_tags_ok = (argc == 1); 2844 argc--; 2845 argv++; 2846 } 2847 } 2848 string_list_remove_duplicates(&list, 0); 2849 2850 if (negotiate_only) { 2851 struct oidset acked_commits = OIDSET_INIT; 2852 struct oidset_iter iter; 2853 const struct object_id *oid; 2854 2855 trace2_region_enter("fetch", "negotiate-only", the_repository); 2856 if (!remote) 2857 die(_("must supply remote when using --negotiate-only")); 2858 gtransport = prepare_transport(remote, 1, &filter_options); 2859 2860 if (!gtransport->smart_options) { 2861 warning(_("protocol does not support --negotiate-only, exiting")); 2862 result = 1; 2863 trace2_region_leave("fetch", "negotiate-only", the_repository); 2864 goto cleanup; 2865 } 2866 if (!gtransport->smart_options->negotiation_restrict_tips) 2867 die(_("%s needs one or more %s"), "--negotiate-only", 2868 "--negotiation-restrict=*"); 2869 2870 gtransport->smart_options->acked_commits = &acked_commits; 2871 2872 if (server_options.nr) 2873 gtransport->server_options = &server_options; 2874 result = transport_fetch_refs(gtransport, NULL); 2875 gtransport->smart_options->acked_commits = NULL; 2876 2877 oidset_iter_init(&acked_commits, &iter); 2878 while ((oid = oidset_iter_next(&iter))) 2879 printf("%s\n", oid_to_hex(oid)); 2880 oidset_clear(&acked_commits); 2881 trace2_region_leave("fetch", "negotiate-only", the_repository); 2882 } else if (remote) { 2883 if (filter_options.choice || repo_has_promisor_remote(the_repository)) { 2884 trace2_region_enter("fetch", "setup-partial", the_repository); 2885 fetch_one_setup_partial(remote, &filter_options); 2886 trace2_region_leave("fetch", "setup-partial", the_repository); 2887 } 2888 trace2_region_enter("fetch", "fetch-one", the_repository); 2889 result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs, 2890 &config, &filter_options, &updated_tips); 2891 if (prepare_commit_graph(the_repository)) { 2892 if (oidset_size(&updated_tips)) 2893 graph_write_mode = GRAPH_WRITE_TIPS; 2894 else 2895 graph_write_mode = GRAPH_WRITE_SKIP; 2896 } 2897 trace2_region_leave("fetch", "fetch-one", the_repository); 2898 } else { 2899 int max_children = max_jobs; 2900 2901 if (filter_options.choice) 2902 die(_("--filter can only be used with the remote " 2903 "configured in extensions.partialclone")); 2904 2905 if (atomic_fetch) 2906 die(_("--atomic can only be used when fetching " 2907 "from one remote")); 2908 2909 if (stdin_refspecs) 2910 die(_("--stdin can only be used when fetching " 2911 "from one remote")); 2912 2913 if (max_children < 0) 2914 max_children = config.parallel; 2915 2916 /* TODO should this also die if we have a previous partial-clone? */ 2917 trace2_region_enter("fetch", "fetch-multiple", the_repository); 2918 result = fetch_multiple(&list, max_children, &config); 2919 trace2_region_leave("fetch", "fetch-multiple", the_repository); 2920 } 2921 2922 /* 2923 * This is only needed after fetch_one(), which does not fetch 2924 * submodules by itself. 2925 * 2926 * When we fetch from multiple remotes, fetch_multiple() has 2927 * already updated submodules to grab commits necessary for 2928 * the fetched history from each remote, so there is no need 2929 * to fetch submodules from here. 2930 */ 2931 if (!result && remote && (config.recurse_submodules != RECURSE_SUBMODULES_OFF)) { 2932 struct strvec options = STRVEC_INIT; 2933 int max_children = max_jobs; 2934 2935 if (max_children < 0) 2936 max_children = config.submodule_fetch_jobs; 2937 if (max_children < 0) 2938 max_children = config.parallel; 2939 2940 add_options_to_argv(&options, &config); 2941 trace2_region_enter_printf("fetch", "recurse-submodule", the_repository, "%s", submodule_prefix); 2942 result = fetch_submodules(the_repository, 2943 &options, 2944 submodule_prefix, 2945 config.recurse_submodules, 2946 recurse_submodules_default, 2947 verbosity < 0, 2948 max_children); 2949 trace2_region_leave_printf("fetch", "recurse-submodule", the_repository, "%s", submodule_prefix); 2950 strvec_clear(&options); 2951 } 2952 2953 /* 2954 * Skip irrelevant tasks because we know objects were not 2955 * fetched. 2956 * 2957 * NEEDSWORK: as a future optimization, we can return early 2958 * whenever objects were not fetched e.g. if we already have all 2959 * of them. 2960 */ 2961 if (negotiate_only) 2962 goto cleanup; 2963 2964 prepare_repo_settings(the_repository); 2965 if (fetch_write_commit_graph > 0 || 2966 (fetch_write_commit_graph < 0 && 2967 the_repository->settings.fetch_write_commit_graph)) { 2968 int commit_graph_flags = COMMIT_GRAPH_WRITE_SPLIT; 2969 2970 if (progress) 2971 commit_graph_flags |= COMMIT_GRAPH_WRITE_PROGRESS; 2972 2973 if (graph_write_mode != GRAPH_WRITE_SKIP) { 2974 trace2_region_enter("fetch", "write-commit-graph", 2975 the_repository); 2976 if (graph_write_mode == GRAPH_WRITE_TIPS) 2977 write_commit_graph( 2978 the_repository, 2979 the_repository->objects->source->path, 2980 NULL, &updated_tips, 2981 commit_graph_flags, NULL); 2982 else 2983 write_commit_graph_reachable( 2984 the_repository, 2985 the_repository->objects->source->path, 2986 commit_graph_flags, NULL); 2987 trace2_region_leave("fetch", "write-commit-graph", 2988 the_repository); 2989 } 2990 } 2991 2992 if (enable_auto_gc) { 2993 if (refetch) { 2994 /* 2995 * Hint auto-maintenance strongly to encourage repacking, 2996 * but respect config settings disabling it. 2997 */ 2998 int opt_val; 2999 if (repo_config_get_int(the_repository, "gc.autopacklimit", &opt_val)) 3000 opt_val = -1; 3001 if (opt_val != 0) 3002 git_config_push_parameter("gc.autoPackLimit=1"); 3003 3004 if (repo_config_get_int(the_repository, "maintenance.incremental-repack.auto", &opt_val)) 3005 opt_val = -1; 3006 if (opt_val != 0) 3007 git_config_push_parameter("maintenance.incremental-repack.auto=-1"); 3008 } 3009 run_auto_maintenance(the_repository, verbosity < 0); 3010 } 3011 3012 cleanup: 3013 oidset_clear(&updated_tips); 3014 string_list_clear(&list, 0); 3015 list_objects_filter_release(&filter_options); 3016 free(config.follow_remote_head_raw); 3017 return result; 3018 }