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