Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/remote.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / remote.c` 3113 lines ยท 82233 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 #define DISABLE_SIGN_COMPARE_WARNINGS 3 4 #include "git-compat-util.h" 5 #include "abspath.h" 6 #include "config.h" 7 #include "environment.h" 8 #include "gettext.h" 9 #include "hex.h" 10 #include "remote.h" 11 #include "url.h" 12 #include "urlmatch.h" 13 #include "refs.h" 14 #include "refspec.h" 15 #include "object-name.h" 16 #include "odb.h" 17 #include "path.h" 18 #include "commit.h" 19 #include "diff.h" 20 #include "revision.h" 21 #include "dir.h" 22 #include "setup.h" 23 #include "string-list.h" 24 #include "strvec.h" 25 #include "commit-reach.h" 26 #include "advice.h" 27 #include "connect.h" 28 #include "parse-options.h" 29 #include "transport.h" 30 31 enum map_direction { FROM_SRC, FROM_DST }; 32 33 enum { 34 ENABLE_ADVICE_PULL = (1 << 0), 35 ENABLE_ADVICE_PUSH = (1 << 1), 36 ENABLE_ADVICE_DIVERGENCE = (1 << 2), 37 }; 38 39 struct counted_string { 40 size_t len; 41 const char *s; 42 }; 43 44 static int valid_remote(const struct remote *remote) 45 { 46 return !!remote->url.nr; 47 } 48 49 static char *alias_url(const char *url, struct rewrites *r) 50 { 51 int i, j; 52 struct counted_string *longest; 53 int longest_i; 54 55 longest = NULL; 56 longest_i = -1; 57 for (i = 0; i < r->rewrite_nr; i++) { 58 if (!r->rewrite[i]) 59 continue; 60 for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) { 61 if (starts_with(url, r->rewrite[i]->instead_of[j].s) && 62 (!longest || 63 longest->len < r->rewrite[i]->instead_of[j].len)) { 64 longest = &(r->rewrite[i]->instead_of[j]); 65 longest_i = i; 66 } 67 } 68 } 69 if (!longest) 70 return NULL; 71 72 return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len); 73 } 74 75 static void add_url(struct remote *remote, const char *url) 76 { 77 if (*url) 78 strvec_push(&remote->url, url); 79 else 80 strvec_clear(&remote->url); 81 } 82 83 static void add_pushurl(struct remote *remote, const char *pushurl) 84 { 85 if (*pushurl) 86 strvec_push(&remote->pushurl, pushurl); 87 else 88 strvec_clear(&remote->pushurl); 89 } 90 91 static void add_pushurl_alias(struct remote_state *remote_state, 92 struct remote *remote, const char *url) 93 { 94 char *alias = alias_url(url, &remote_state->rewrites_push); 95 if (alias) 96 add_pushurl(remote, alias); 97 free(alias); 98 } 99 100 static void add_url_alias(struct remote_state *remote_state, 101 struct remote *remote, const char *url) 102 { 103 char *alias = alias_url(url, &remote_state->rewrites); 104 add_url(remote, alias ? alias : url); 105 add_pushurl_alias(remote_state, remote, url); 106 free(alias); 107 } 108 109 struct remotes_hash_key { 110 const char *str; 111 int len; 112 }; 113 114 static int remotes_hash_cmp(const void *cmp_data UNUSED, 115 const struct hashmap_entry *eptr, 116 const struct hashmap_entry *entry_or_key, 117 const void *keydata) 118 { 119 const struct remote *a, *b; 120 const struct remotes_hash_key *key = keydata; 121 122 a = container_of(eptr, const struct remote, ent); 123 b = container_of(entry_or_key, const struct remote, ent); 124 125 if (key) 126 return !!xstrncmpz(a->name, key->str, key->len); 127 else 128 return strcmp(a->name, b->name); 129 } 130 131 static struct remote *make_remote(struct remote_state *remote_state, 132 const char *name, int len) 133 { 134 struct remote *ret; 135 struct remotes_hash_key lookup; 136 struct hashmap_entry lookup_entry, *e; 137 138 if (!len) 139 len = strlen(name); 140 141 lookup.str = name; 142 lookup.len = len; 143 hashmap_entry_init(&lookup_entry, memhash(name, len)); 144 145 e = hashmap_get(&remote_state->remotes_hash, &lookup_entry, &lookup); 146 if (e) 147 return container_of(e, struct remote, ent); 148 149 CALLOC_ARRAY(ret, 1); 150 ret->prune = -1; /* unspecified */ 151 ret->prune_tags = -1; /* unspecified */ 152 ret->name = xstrndup(name, len); 153 refspec_init_push(&ret->push, the_hash_algo); 154 refspec_init_fetch(&ret->fetch, the_hash_algo); 155 string_list_init_dup(&ret->server_options); 156 string_list_init_dup(&ret->negotiation_restrict); 157 string_list_init_dup(&ret->negotiation_include); 158 159 ALLOC_GROW(remote_state->remotes, remote_state->remotes_nr + 1, 160 remote_state->remotes_alloc); 161 remote_state->remotes[remote_state->remotes_nr++] = ret; 162 163 hashmap_entry_init(&ret->ent, lookup_entry.hash); 164 if (hashmap_put_entry(&remote_state->remotes_hash, ret, ent)) 165 BUG("hashmap_put overwrote entry after hashmap_get returned NULL"); 166 return ret; 167 } 168 169 static void remote_clear(struct remote *remote) 170 { 171 free((char *)remote->name); 172 free((char *)remote->foreign_vcs); 173 174 strvec_clear(&remote->url); 175 strvec_clear(&remote->pushurl); 176 177 refspec_clear(&remote->push); 178 refspec_clear(&remote->fetch); 179 180 free((char *)remote->receivepack); 181 free((char *)remote->uploadpack); 182 FREE_AND_NULL(remote->http_proxy); 183 FREE_AND_NULL(remote->http_proxy_authmethod); 184 string_list_clear(&remote->server_options, 0); 185 string_list_clear(&remote->negotiation_restrict, 0); 186 string_list_clear(&remote->negotiation_include, 0); 187 } 188 189 static void add_merge(struct branch *branch, const char *name) 190 { 191 struct refspec_item *merge; 192 193 ALLOC_GROW(branch->merge, branch->merge_nr + 1, 194 branch->merge_alloc); 195 196 merge = xcalloc(1, sizeof(*merge)); 197 merge->src = xstrdup(name); 198 199 branch->merge[branch->merge_nr++] = merge; 200 } 201 202 struct branches_hash_key { 203 const char *str; 204 int len; 205 }; 206 207 static int branches_hash_cmp(const void *cmp_data UNUSED, 208 const struct hashmap_entry *eptr, 209 const struct hashmap_entry *entry_or_key, 210 const void *keydata) 211 { 212 const struct branch *a, *b; 213 const struct branches_hash_key *key = keydata; 214 215 a = container_of(eptr, const struct branch, ent); 216 b = container_of(entry_or_key, const struct branch, ent); 217 218 if (key) 219 return !!xstrncmpz(a->name, key->str, key->len); 220 else 221 return strcmp(a->name, b->name); 222 } 223 224 static struct branch *find_branch(struct remote_state *remote_state, 225 const char *name, size_t len) 226 { 227 struct branches_hash_key lookup; 228 struct hashmap_entry lookup_entry, *e; 229 230 lookup.str = name; 231 lookup.len = len; 232 hashmap_entry_init(&lookup_entry, memhash(name, len)); 233 234 e = hashmap_get(&remote_state->branches_hash, &lookup_entry, &lookup); 235 if (e) 236 return container_of(e, struct branch, ent); 237 238 return NULL; 239 } 240 241 static void die_on_missing_branch(struct repository *repo, 242 struct branch *branch) 243 { 244 /* branch == NULL is always valid because it represents detached HEAD. */ 245 if (branch && 246 branch != find_branch(repo->remote_state, branch->name, 247 strlen(branch->name))) 248 die("branch %s was not found in the repository", branch->name); 249 } 250 251 static struct branch *make_branch(struct remote_state *remote_state, 252 const char *name, size_t len) 253 { 254 struct branch *ret; 255 256 ret = find_branch(remote_state, name, len); 257 if (ret) 258 return ret; 259 260 CALLOC_ARRAY(ret, 1); 261 ret->name = xstrndup(name, len); 262 ret->refname = xstrfmt("refs/heads/%s", ret->name); 263 264 hashmap_entry_init(&ret->ent, memhash(name, len)); 265 if (hashmap_put_entry(&remote_state->branches_hash, ret, ent)) 266 BUG("hashmap_put overwrote entry after hashmap_get returned NULL"); 267 return ret; 268 } 269 270 static void merge_clear(struct branch *branch) 271 { 272 for (int i = 0; i < branch->merge_nr; i++) { 273 refspec_item_clear(branch->merge[i]); 274 free(branch->merge[i]); 275 } 276 FREE_AND_NULL(branch->merge); 277 branch->merge_nr = 0; 278 } 279 280 static void branch_release(struct branch *branch) 281 { 282 free((char *)branch->name); 283 free((char *)branch->refname); 284 free(branch->remote_name); 285 free(branch->pushremote_name); 286 free(branch->push_tracking_ref); 287 merge_clear(branch); 288 } 289 290 static struct rewrite *make_rewrite(struct rewrites *r, 291 const char *base, size_t len) 292 { 293 struct rewrite *ret; 294 int i; 295 296 for (i = 0; i < r->rewrite_nr; i++) { 297 if (len == r->rewrite[i]->baselen && 298 !strncmp(base, r->rewrite[i]->base, len)) 299 return r->rewrite[i]; 300 } 301 302 ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc); 303 CALLOC_ARRAY(ret, 1); 304 r->rewrite[r->rewrite_nr++] = ret; 305 ret->base = xstrndup(base, len); 306 ret->baselen = len; 307 return ret; 308 } 309 310 static void rewrites_release(struct rewrites *r) 311 { 312 for (int i = 0; i < r->rewrite_nr; i++) { 313 struct rewrite *rewrite = r->rewrite[i]; 314 315 free((char *)rewrite->base); 316 for (int j = 0; j < rewrite->instead_of_nr; j++) 317 free((char *)rewrite->instead_of[j].s); 318 free(rewrite->instead_of); 319 free(rewrite); 320 } 321 free(r->rewrite); 322 memset(r, 0, sizeof(*r)); 323 } 324 325 static void add_instead_of(struct rewrite *rewrite, const char *instead_of) 326 { 327 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc); 328 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of; 329 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of); 330 rewrite->instead_of_nr++; 331 } 332 333 #ifndef WITH_BREAKING_CHANGES 334 static const char *skip_spaces(const char *s) 335 { 336 while (isspace(*s)) 337 s++; 338 return s; 339 } 340 341 static void warn_about_deprecated_remote_type(const char *type, 342 const struct remote *remote) 343 { 344 warning(_("reading remote from \"%s/%s\", which is nominated for removal.\n" 345 "\n" 346 "If you still use the \"remotes/\" directory it is recommended to\n" 347 "migrate to config-based remotes:\n" 348 "\n" 349 "\tgit remote rename %s %s\n" 350 "\n" 351 "If you cannot, please let us know why you still need to use it by\n" 352 "sending an e-mail to ."), 353 type, remote->name, remote->name, remote->name); 354 } 355 356 static void read_remotes_file(struct repository *repo, struct remote *remote) 357 { 358 struct strbuf buf = STRBUF_INIT; 359 FILE *f = fopen_or_warn(repo_git_path_append(repo, &buf, 360 "remotes/%s", remote->name), "r"); 361 362 if (!f) 363 goto out; 364 365 warn_about_deprecated_remote_type("remotes", remote); 366 367 remote->configured_in_repo = 1; 368 remote->origin = REMOTE_REMOTES; 369 while (strbuf_getline(&buf, f) != EOF) { 370 const char *v; 371 372 strbuf_rtrim(&buf); 373 374 if (skip_prefix(buf.buf, "URL:", &v)) 375 add_url_alias(repo->remote_state, remote, 376 skip_spaces(v)); 377 else if (skip_prefix(buf.buf, "Push:", &v)) 378 refspec_append(&remote->push, skip_spaces(v)); 379 else if (skip_prefix(buf.buf, "Pull:", &v)) 380 refspec_append(&remote->fetch, skip_spaces(v)); 381 } 382 fclose(f); 383 384 out: 385 strbuf_release(&buf); 386 } 387 388 static void read_branches_file(struct repository *repo, struct remote *remote) 389 { 390 char *frag, *to_free = NULL; 391 struct strbuf buf = STRBUF_INIT; 392 FILE *f = fopen_or_warn(repo_git_path_append(repo, &buf, 393 "branches/%s", remote->name), "r"); 394 395 if (!f) 396 goto out; 397 398 warn_about_deprecated_remote_type("branches", remote); 399 400 strbuf_getline_lf(&buf, f); 401 fclose(f); 402 strbuf_trim(&buf); 403 if (!buf.len) 404 goto out; 405 406 remote->configured_in_repo = 1; 407 remote->origin = REMOTE_BRANCHES; 408 409 /* 410 * The branches file would have URL and optionally 411 * #branch specified. The default (or specified) branch is 412 * fetched and stored in the local branch matching the 413 * remote name. 414 */ 415 frag = strchr(buf.buf, '#'); 416 if (frag) 417 *(frag++) = '\0'; 418 else 419 frag = to_free = repo_default_branch_name(repo, 0); 420 421 add_url_alias(repo->remote_state, remote, buf.buf); 422 refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s", 423 frag, remote->name); 424 425 /* 426 * Cogito compatible push: push current HEAD to remote #branch 427 * (master if missing) 428 */ 429 refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag); 430 remote->fetch_tags = 1; /* always auto-follow */ 431 432 out: 433 strbuf_release(&buf); 434 free(to_free); 435 } 436 #endif /* WITH_BREAKING_CHANGES */ 437 438 static int handle_config(const char *key, const char *value, 439 const struct config_context *ctx, void *cb) 440 { 441 const char *name; 442 size_t namelen; 443 const char *subkey; 444 struct remote *remote; 445 struct branch *branch; 446 struct remote_state *remote_state = cb; 447 const struct key_value_info *kvi = ctx->kvi; 448 449 if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) { 450 /* There is no subsection. */ 451 if (!name) 452 return 0; 453 /* There is a subsection, but it is empty. */ 454 if (!namelen) 455 return -1; 456 branch = make_branch(remote_state, name, namelen); 457 if (!strcmp(subkey, "remote")) { 458 FREE_AND_NULL(branch->remote_name); 459 return git_config_string(&branch->remote_name, key, value); 460 } else if (!strcmp(subkey, "pushremote")) { 461 FREE_AND_NULL(branch->pushremote_name); 462 return git_config_string(&branch->pushremote_name, key, value); 463 } else if (!strcmp(subkey, "merge")) { 464 if (!value) 465 return config_error_nonbool(key); 466 add_merge(branch, value); 467 } 468 return 0; 469 } 470 if (parse_config_key(key, "url", &name, &namelen, &subkey) >= 0) { 471 struct rewrite *rewrite; 472 if (!name) 473 return 0; 474 if (!strcmp(subkey, "insteadof")) { 475 if (!value) 476 return config_error_nonbool(key); 477 rewrite = make_rewrite(&remote_state->rewrites, name, 478 namelen); 479 add_instead_of(rewrite, xstrdup(value)); 480 } else if (!strcmp(subkey, "pushinsteadof")) { 481 if (!value) 482 return config_error_nonbool(key); 483 rewrite = make_rewrite(&remote_state->rewrites_push, 484 name, namelen); 485 add_instead_of(rewrite, xstrdup(value)); 486 } 487 } 488 489 if (parse_config_key(key, "remote", &name, &namelen, &subkey) < 0) 490 return 0; 491 492 /* Handle remote.* variables */ 493 if (!name && !strcmp(subkey, "pushdefault")) { 494 FREE_AND_NULL(remote_state->pushremote_name); 495 return git_config_string(&remote_state->pushremote_name, key, 496 value); 497 } 498 499 if (!name) 500 return 0; 501 /* Handle remote..* variables */ 502 if (*name == '/') { 503 warning(_("config remote shorthand cannot begin with '/': %s"), 504 name); 505 return 0; 506 } 507 remote = make_remote(remote_state, name, namelen); 508 remote->origin = REMOTE_CONFIG; 509 if (kvi->scope == CONFIG_SCOPE_LOCAL || 510 kvi->scope == CONFIG_SCOPE_WORKTREE) 511 remote->configured_in_repo = 1; 512 if (!strcmp(subkey, "mirror")) 513 remote->mirror = git_config_bool(key, value); 514 else if (!strcmp(subkey, "skipdefaultupdate")) 515 remote->skip_default_update = git_config_bool(key, value); 516 else if (!strcmp(subkey, "skipfetchall")) 517 remote->skip_default_update = git_config_bool(key, value); 518 else if (!strcmp(subkey, "prune")) 519 remote->prune = git_config_bool(key, value); 520 else if (!strcmp(subkey, "prunetags")) 521 remote->prune_tags = git_config_bool(key, value); 522 else if (!strcmp(subkey, "url")) { 523 if (!value) 524 return config_error_nonbool(key); 525 add_url(remote, value); 526 } else if (!strcmp(subkey, "pushurl")) { 527 if (!value) 528 return config_error_nonbool(key); 529 add_pushurl(remote, value); 530 } else if (!strcmp(subkey, "push")) { 531 char *v; 532 if (git_config_string(&v, key, value)) 533 return -1; 534 refspec_append(&remote->push, v); 535 free(v); 536 } else if (!strcmp(subkey, "fetch")) { 537 char *v; 538 if (git_config_string(&v, key, value)) 539 return -1; 540 refspec_append(&remote->fetch, v); 541 free(v); 542 } else if (!strcmp(subkey, "receivepack")) { 543 char *v; 544 if (git_config_string(&v, key, value)) 545 return -1; 546 if (!remote->receivepack) 547 remote->receivepack = v; 548 else 549 error(_("more than one receivepack given, using the first")); 550 } else if (!strcmp(subkey, "uploadpack")) { 551 char *v; 552 if (git_config_string(&v, key, value)) 553 return -1; 554 if (!remote->uploadpack) 555 remote->uploadpack = v; 556 else 557 error(_("more than one uploadpack given, using the first")); 558 } else if (!strcmp(subkey, "tagopt")) { 559 if (!strcmp(value, "--no-tags")) 560 remote->fetch_tags = -1; 561 else if (!strcmp(value, "--tags")) 562 remote->fetch_tags = 2; 563 } else if (!strcmp(subkey, "proxy")) { 564 FREE_AND_NULL(remote->http_proxy); 565 return git_config_string(&remote->http_proxy, 566 key, value); 567 } else if (!strcmp(subkey, "proxyauthmethod")) { 568 FREE_AND_NULL(remote->http_proxy_authmethod); 569 return git_config_string(&remote->http_proxy_authmethod, 570 key, value); 571 } else if (!strcmp(subkey, "vcs")) { 572 FREE_AND_NULL(remote->foreign_vcs); 573 return git_config_string(&remote->foreign_vcs, key, value); 574 } else if (!strcmp(subkey, "serveroption")) { 575 return parse_transport_option(key, value, 576 &remote->server_options); 577 } else if (!strcmp(subkey, "negotiationrestrict")) { 578 return parse_transport_option(key, value, 579 &remote->negotiation_restrict); 580 } else if (!strcmp(subkey, "negotiationinclude")) { 581 return parse_transport_option(key, value, 582 &remote->negotiation_include); 583 } else if (!strcmp(subkey, "followremotehead")) { 584 const char *no_warn_branch; 585 /* 586 * NEEDSWORK: this is validated/warned about here, during config 587 * parsing, regardless of whether the fetch that triggered this 588 * parse will ever consult it for this particular remote. See 589 * fetch.c's deferred handling of fetch.followRemoteHEAD for the 590 * pattern this should likely follow. 591 */ 592 if (!strcmp(value, "never")) 593 remote->follow_remote_head = FOLLOW_REMOTE_NEVER; 594 else if (!strcmp(value, "create")) 595 remote->follow_remote_head = FOLLOW_REMOTE_CREATE; 596 else if (!strcmp(value, "warn")) { 597 remote->follow_remote_head = FOLLOW_REMOTE_WARN; 598 remote->no_warn_branch = NULL; 599 } else if (skip_prefix(value, "warn-if-not-", &no_warn_branch)) { 600 remote->follow_remote_head = FOLLOW_REMOTE_WARN; 601 remote->no_warn_branch = no_warn_branch; 602 } else if (!strcmp(value, "always")) { 603 remote->follow_remote_head = FOLLOW_REMOTE_ALWAYS; 604 } else { 605 warning(_("unrecognized followRemoteHEAD value '%s' ignored"), 606 value); 607 } 608 } 609 return 0; 610 } 611 612 static void alias_all_urls(struct remote_state *remote_state) 613 { 614 int i, j; 615 for (i = 0; i < remote_state->remotes_nr; i++) { 616 int add_pushurl_aliases; 617 if (!remote_state->remotes[i]) 618 continue; 619 for (j = 0; j < remote_state->remotes[i]->pushurl.nr; j++) { 620 char *alias = alias_url(remote_state->remotes[i]->pushurl.v[j], 621 &remote_state->rewrites); 622 if (alias) 623 strvec_replace(&remote_state->remotes[i]->pushurl, 624 j, alias); 625 free(alias); 626 } 627 add_pushurl_aliases = remote_state->remotes[i]->pushurl.nr == 0; 628 for (j = 0; j < remote_state->remotes[i]->url.nr; j++) { 629 char *alias; 630 if (add_pushurl_aliases) 631 add_pushurl_alias( 632 remote_state, remote_state->remotes[i], 633 remote_state->remotes[i]->url.v[j]); 634 alias = alias_url(remote_state->remotes[i]->url.v[j], 635 &remote_state->rewrites); 636 if (alias) 637 strvec_replace(&remote_state->remotes[i]->url, 638 j, alias); 639 free(alias); 640 } 641 } 642 } 643 644 static void read_config(struct repository *repo, int early) 645 { 646 int flag; 647 648 if (repo->remote_state->initialized) 649 return; 650 repo->remote_state->initialized = 1; 651 652 repo->remote_state->current_branch = NULL; 653 if (startup_info->have_repository && !early) { 654 const char *head_ref = refs_resolve_ref_unsafe( 655 get_main_ref_store(repo), "HEAD", 0, NULL, &flag); 656 if (head_ref && (flag & REF_ISSYMREF) && 657 skip_prefix(head_ref, "refs/heads/", &head_ref)) { 658 repo->remote_state->current_branch = make_branch( 659 repo->remote_state, head_ref, strlen(head_ref)); 660 } 661 } 662 repo_config(repo, handle_config, repo->remote_state); 663 alias_all_urls(repo->remote_state); 664 } 665 666 #ifndef WITH_BREAKING_CHANGES 667 static int valid_remote_nick(const char *name) 668 { 669 if (!name[0] || is_dot_or_dotdot(name)) 670 return 0; 671 672 /* remote nicknames cannot contain slashes */ 673 while (*name) 674 if (is_dir_sep(*name++)) 675 return 0; 676 return 1; 677 } 678 #endif /* WITH_BREAKING_CHANGES */ 679 680 static const char *remotes_remote_for_branch(struct remote_state *remote_state, 681 struct branch *branch, 682 int *explicit) 683 { 684 if (branch && branch->remote_name) { 685 if (explicit) 686 *explicit = 1; 687 return branch->remote_name; 688 } 689 if (explicit) 690 *explicit = 0; 691 if (remote_state->remotes_nr == 1) 692 return remote_state->remotes[0]->name; 693 return "origin"; 694 } 695 696 const char *remote_for_branch(struct branch *branch, int *explicit) 697 { 698 read_config(the_repository, 0); 699 die_on_missing_branch(the_repository, branch); 700 701 return remotes_remote_for_branch(the_repository->remote_state, branch, 702 explicit); 703 } 704 705 static const char * 706 remotes_pushremote_for_branch(struct remote_state *remote_state, 707 struct branch *branch, int *explicit) 708 { 709 if (branch && branch->pushremote_name) { 710 if (explicit) 711 *explicit = 1; 712 return branch->pushremote_name; 713 } 714 if (remote_state->pushremote_name) { 715 if (explicit) 716 *explicit = 1; 717 return remote_state->pushremote_name; 718 } 719 return remotes_remote_for_branch(remote_state, branch, explicit); 720 } 721 722 const char *pushremote_for_branch(struct branch *branch, int *explicit) 723 { 724 read_config(the_repository, 0); 725 die_on_missing_branch(the_repository, branch); 726 727 return remotes_pushremote_for_branch(the_repository->remote_state, 728 branch, explicit); 729 } 730 731 static struct remote *remotes_remote_get(struct repository *repo, 732 const char *name); 733 734 char *remote_ref_for_branch(struct branch *branch, int for_push) 735 { 736 read_config(the_repository, 0); 737 die_on_missing_branch(the_repository, branch); 738 739 if (branch) { 740 if (!for_push) { 741 if (branch->merge_nr) { 742 return xstrdup(branch->merge[0]->src); 743 } 744 } else { 745 char *dst; 746 const char *remote_name = remotes_pushremote_for_branch( 747 the_repository->remote_state, branch, 748 NULL); 749 struct remote *remote = remotes_remote_get( 750 the_repository, remote_name); 751 752 if (remote && remote->push.nr && 753 (dst = apply_refspecs(&remote->push, 754 branch->refname))) { 755 return dst; 756 } 757 } 758 } 759 return NULL; 760 } 761 762 static void validate_remote_url(struct remote *remote) 763 { 764 int i; 765 const char *value; 766 struct strbuf redacted = STRBUF_INIT; 767 int warn_not_die; 768 769 if (repo_config_get_string_tmp(the_repository, "transfer.credentialsinurl", &value)) 770 return; 771 772 if (!strcmp("warn", value)) 773 warn_not_die = 1; 774 else if (!strcmp("die", value)) 775 warn_not_die = 0; 776 else if (!strcmp("allow", value)) 777 return; 778 else 779 die(_("unrecognized value transfer.credentialsInUrl: '%s'"), value); 780 781 for (i = 0; i < remote->url.nr; i++) { 782 struct url_info url_info = { 0 }; 783 784 if (!url_normalize(remote->url.v[i], &url_info) || 785 !url_info.passwd_off) 786 goto loop_cleanup; 787 788 strbuf_reset(&redacted); 789 strbuf_add(&redacted, url_info.url, url_info.passwd_off); 790 strbuf_addstr(&redacted, ""); 791 strbuf_addstr(&redacted, 792 url_info.url + url_info.passwd_off + url_info.passwd_len); 793 794 if (warn_not_die) 795 warning(_("URL '%s' uses plaintext credentials"), redacted.buf); 796 else 797 die(_("URL '%s' uses plaintext credentials"), redacted.buf); 798 799 loop_cleanup: 800 free(url_info.url); 801 } 802 803 strbuf_release(&redacted); 804 } 805 806 static struct remote * 807 remotes_remote_get_1(struct repository *repo, const char *name, 808 const char *(*get_default)(struct remote_state *, 809 struct branch *, int *)) 810 { 811 struct remote_state *remote_state = repo->remote_state; 812 struct remote *ret; 813 int name_given = 0; 814 815 if (name) 816 name_given = 1; 817 else 818 name = get_default(remote_state, remote_state->current_branch, 819 &name_given); 820 821 ret = make_remote(remote_state, name, 0); 822 #ifndef WITH_BREAKING_CHANGES 823 if (valid_remote_nick(name) && have_git_dir()) { 824 if (!valid_remote(ret)) 825 read_remotes_file(repo, ret); 826 if (!valid_remote(ret)) 827 read_branches_file(repo, ret); 828 } 829 #endif /* WITH_BREAKING_CHANGES */ 830 if (name_given && !valid_remote(ret)) 831 add_url_alias(remote_state, ret, name); 832 if (!valid_remote(ret)) 833 return NULL; 834 835 validate_remote_url(ret); 836 837 return ret; 838 } 839 840 static inline struct remote * 841 remotes_remote_get(struct repository *repo, const char *name) 842 { 843 return remotes_remote_get_1(repo, name, remotes_remote_for_branch); 844 } 845 846 struct remote *remote_get(const char *name) 847 { 848 read_config(the_repository, 0); 849 return remotes_remote_get(the_repository, name); 850 } 851 852 struct remote *remote_get_early(const char *name) 853 { 854 read_config(the_repository, 1); 855 return remotes_remote_get(the_repository, name); 856 } 857 858 static inline struct remote * 859 remotes_pushremote_get(struct repository *repo, const char *name) 860 { 861 return remotes_remote_get_1(repo, name, remotes_pushremote_for_branch); 862 } 863 864 struct remote *pushremote_get(const char *name) 865 { 866 read_config(the_repository, 0); 867 return remotes_pushremote_get(the_repository, name); 868 } 869 870 int remote_is_configured(struct remote *remote, int in_repo) 871 { 872 if (!remote) 873 return 0; 874 if (in_repo) 875 return remote->configured_in_repo; 876 return !!remote->origin; 877 } 878 879 int for_each_remote(each_remote_fn fn, void *priv) 880 { 881 int i, result = 0; 882 read_config(the_repository, 0); 883 for (i = 0; i < the_repository->remote_state->remotes_nr && !result; 884 i++) { 885 struct remote *remote = 886 the_repository->remote_state->remotes[i]; 887 if (!remote) 888 continue; 889 result = fn(remote, priv); 890 } 891 return result; 892 } 893 894 static void handle_duplicate(struct ref *ref1, struct ref *ref2) 895 { 896 if (strcmp(ref1->name, ref2->name)) { 897 if (ref1->fetch_head_status != FETCH_HEAD_IGNORE && 898 ref2->fetch_head_status != FETCH_HEAD_IGNORE) { 899 die(_("Cannot fetch both %s and %s to %s"), 900 ref1->name, ref2->name, ref2->peer_ref->name); 901 } else if (ref1->fetch_head_status != FETCH_HEAD_IGNORE && 902 ref2->fetch_head_status == FETCH_HEAD_IGNORE) { 903 warning(_("%s usually tracks %s, not %s"), 904 ref2->peer_ref->name, ref2->name, ref1->name); 905 } else if (ref1->fetch_head_status == FETCH_HEAD_IGNORE && 906 ref2->fetch_head_status == FETCH_HEAD_IGNORE) { 907 die(_("%s tracks both %s and %s"), 908 ref2->peer_ref->name, ref1->name, ref2->name); 909 } else { 910 /* 911 * This last possibility doesn't occur because 912 * FETCH_HEAD_IGNORE entries always appear at 913 * the end of the list. 914 */ 915 BUG("Internal error"); 916 } 917 } 918 free(ref2->peer_ref); 919 free(ref2); 920 } 921 922 struct ref *ref_remove_duplicates(struct ref *ref_map) 923 { 924 struct string_list refs = STRING_LIST_INIT_NODUP; 925 struct ref *retval = NULL; 926 struct ref **p = &retval; 927 928 while (ref_map) { 929 struct ref *ref = ref_map; 930 931 ref_map = ref_map->next; 932 ref->next = NULL; 933 934 if (!ref->peer_ref) { 935 *p = ref; 936 p = &ref->next; 937 } else { 938 struct string_list_item *item = 939 string_list_insert(&refs, ref->peer_ref->name); 940 941 if (item->util) { 942 /* Entry already existed */ 943 handle_duplicate((struct ref *)item->util, ref); 944 } else { 945 *p = ref; 946 p = &ref->next; 947 item->util = ref; 948 } 949 } 950 } 951 952 string_list_clear(&refs, 0); 953 return retval; 954 } 955 956 int remote_has_url(struct remote *remote, const char *url) 957 { 958 int i; 959 for (i = 0; i < remote->url.nr; i++) { 960 if (!strcmp(remote->url.v[i], url)) 961 return 1; 962 } 963 return 0; 964 } 965 966 struct strvec *push_url_of_remote(struct remote *remote) 967 { 968 return remote->pushurl.nr ? &remote->pushurl : &remote->url; 969 } 970 971 static bool remote_has_push_url(struct remote *remote, const char *url) 972 { 973 const struct strvec *push_urls = push_url_of_remote(remote); 974 975 for (size_t i = 0; i < push_urls->nr; i++) { 976 if (!strcmp(push_urls->v[i], url)) 977 return true; 978 } 979 return false; 980 } 981 982 void ref_push_report_free(struct ref_push_report *report) 983 { 984 while (report) { 985 struct ref_push_report *next = report->next; 986 987 free(report->ref_name); 988 free(report->old_oid); 989 free(report->new_oid); 990 free(report); 991 992 report = next; 993 } 994 } 995 996 int remote_find_tracking(struct remote *remote, struct refspec_item *refspec) 997 { 998 return refspec_find_match(&remote->fetch, refspec); 999 } 1000 1001 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen, 1002 const char *name) 1003 { 1004 size_t len = strlen(name); 1005 struct ref *ref = xcalloc(1, st_add4(sizeof(*ref), prefixlen, len, 1)); 1006 memcpy(ref->name, prefix, prefixlen); 1007 memcpy(ref->name + prefixlen, name, len); 1008 return ref; 1009 } 1010 1011 struct ref *alloc_ref(const char *name) 1012 { 1013 return alloc_ref_with_prefix("", 0, name); 1014 } 1015 1016 struct ref *copy_ref(const struct ref *ref) 1017 { 1018 struct ref *cpy; 1019 size_t len; 1020 if (!ref) 1021 return NULL; 1022 len = st_add3(sizeof(struct ref), strlen(ref->name), 1); 1023 cpy = xmalloc(len); 1024 memcpy(cpy, ref, len); 1025 cpy->next = NULL; 1026 cpy->symref = xstrdup_or_null(ref->symref); 1027 cpy->remote_status = xstrdup_or_null(ref->remote_status); 1028 cpy->peer_ref = copy_ref(ref->peer_ref); 1029 return cpy; 1030 } 1031 1032 struct ref *copy_ref_list(const struct ref *ref) 1033 { 1034 struct ref *ret = NULL; 1035 struct ref **tail = &ret; 1036 while (ref) { 1037 *tail = copy_ref(ref); 1038 ref = ref->next; 1039 tail = &((*tail)->next); 1040 } 1041 return ret; 1042 } 1043 1044 void free_one_ref(struct ref *ref) 1045 { 1046 if (!ref) 1047 return; 1048 free_one_ref(ref->peer_ref); 1049 ref_push_report_free(ref->report); 1050 free(ref->remote_status); 1051 free(ref->tracking_ref); 1052 free(ref->symref); 1053 free(ref); 1054 } 1055 1056 void free_refs(struct ref *ref) 1057 { 1058 struct ref *next; 1059 while (ref) { 1060 next = ref->next; 1061 free_one_ref(ref); 1062 ref = next; 1063 } 1064 } 1065 1066 int count_refspec_match(const char *pattern, 1067 struct ref *refs, 1068 struct ref **matched_ref) 1069 { 1070 int patlen = strlen(pattern); 1071 struct ref *matched_weak = NULL; 1072 struct ref *matched = NULL; 1073 int weak_match = 0; 1074 int match = 0; 1075 1076 for (weak_match = match = 0; refs; refs = refs->next) { 1077 char *name = refs->name; 1078 int namelen = strlen(name); 1079 1080 if (!refname_match(pattern, name)) 1081 continue; 1082 1083 /* A match is "weak" if it is with refs outside 1084 * heads or tags, and did not specify the pattern 1085 * in full (e.g. "refs/remotes/origin/master") or at 1086 * least from the toplevel (e.g. "remotes/origin/master"); 1087 * otherwise "git push $URL master" would result in 1088 * ambiguity between remotes/origin/master and heads/master 1089 * at the remote site. 1090 */ 1091 if (namelen != patlen && 1092 patlen != namelen - 5 && 1093 !starts_with(name, "refs/heads/") && 1094 !starts_with(name, "refs/tags/")) { 1095 /* We want to catch the case where only weak 1096 * matches are found and there are multiple 1097 * matches, and where more than one strong 1098 * matches are found, as ambiguous. One 1099 * strong match with zero or more weak matches 1100 * are acceptable as a unique match. 1101 */ 1102 matched_weak = refs; 1103 weak_match++; 1104 } 1105 else { 1106 matched = refs; 1107 match++; 1108 } 1109 } 1110 if (!matched) { 1111 if (matched_ref) 1112 *matched_ref = matched_weak; 1113 return weak_match; 1114 } 1115 else { 1116 if (matched_ref) 1117 *matched_ref = matched; 1118 return match; 1119 } 1120 } 1121 1122 void tail_link_ref(struct ref *ref, struct ref ***tail) 1123 { 1124 **tail = ref; 1125 while (ref->next) 1126 ref = ref->next; 1127 *tail = &ref->next; 1128 } 1129 1130 static struct ref *alloc_delete_ref(void) 1131 { 1132 struct ref *ref = alloc_ref("(delete)"); 1133 oidclr(&ref->new_oid, the_repository->hash_algo); 1134 return ref; 1135 } 1136 1137 static int try_explicit_object_name(const char *name, 1138 struct ref **match) 1139 { 1140 struct object_id oid; 1141 1142 if (!*name) { 1143 if (match) 1144 *match = alloc_delete_ref(); 1145 return 0; 1146 } 1147 1148 if (repo_get_oid(the_repository, name, &oid)) 1149 return -1; 1150 1151 if (match) { 1152 *match = alloc_ref(name); 1153 oidcpy(&(*match)->new_oid, &oid); 1154 } 1155 return 0; 1156 } 1157 1158 static struct ref *make_linked_ref(const char *name, struct ref ***tail) 1159 { 1160 struct ref *ret = alloc_ref(name); 1161 tail_link_ref(ret, tail); 1162 return ret; 1163 } 1164 1165 static char *guess_ref(const char *name, struct ref *peer) 1166 { 1167 struct strbuf buf = STRBUF_INIT; 1168 1169 const char *r = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), 1170 peer->name, 1171 RESOLVE_REF_READING, 1172 NULL, NULL); 1173 if (!r) 1174 return NULL; 1175 1176 if (starts_with(r, "refs/heads/")) { 1177 strbuf_addstr(&buf, "refs/heads/"); 1178 } else if (starts_with(r, "refs/tags/")) { 1179 strbuf_addstr(&buf, "refs/tags/"); 1180 } else { 1181 return NULL; 1182 } 1183 1184 strbuf_addstr(&buf, name); 1185 return strbuf_detach(&buf, NULL); 1186 } 1187 1188 static int match_explicit_lhs(struct ref *src, 1189 struct refspec_item *rs, 1190 struct ref **match, 1191 int *allocated_match) 1192 { 1193 switch (count_refspec_match(rs->src, src, match)) { 1194 case 1: 1195 if (allocated_match) 1196 *allocated_match = 0; 1197 return 0; 1198 case 0: 1199 /* The source could be in the get_sha1() format 1200 * not a reference name. :refs/other is a 1201 * way to delete 'other' ref at the remote end. 1202 */ 1203 if (try_explicit_object_name(rs->src, match) < 0) 1204 return error(_("src refspec %s does not match any"), rs->src); 1205 if (allocated_match) 1206 *allocated_match = 1; 1207 return 0; 1208 default: 1209 return error(_("src refspec %s matches more than one"), rs->src); 1210 } 1211 } 1212 1213 static void show_push_unqualified_ref_name_error(const char *dst_value, 1214 const char *matched_src_name) 1215 { 1216 struct object_id oid; 1217 1218 /* 1219 * TRANSLATORS: "matches '%s'%" is the part of "git push 1220 * :" push, and "being pushed ('%s')" is 1221 * the . 1222 */ 1223 error(_("The destination you provided is not a full refname (i.e.,\n" 1224 "starting with \"refs/\"). We tried to guess what you meant by:\n" 1225 "\n" 1226 "- Looking for a ref that matches '%s' on the remote side.\n" 1227 "- Checking if the being pushed ('%s')\n" 1228 " is a ref in \"refs/{heads,tags}/\". If so we add a corresponding\n" 1229 " refs/{heads,tags}/ prefix on the remote side.\n" 1230 "\n" 1231 "Neither worked, so we gave up. You must fully qualify the ref."), 1232 dst_value, matched_src_name); 1233 1234 if (!advice_enabled(ADVICE_PUSH_UNQUALIFIED_REF_NAME)) 1235 return; 1236 1237 if (repo_get_oid(the_repository, matched_src_name, &oid)) 1238 BUG("'%s' is not a valid object, " 1239 "match_explicit_lhs() should catch this!", 1240 matched_src_name); 1241 1242 switch (odb_read_object_info(the_repository->objects, &oid, NULL)) { 1243 case OBJ_COMMIT: 1244 advise(_("The part of the refspec is a commit object.\n" 1245 "Did you mean to create a new branch by pushing to\n" 1246 "'%s:refs/heads/%s'?"), 1247 matched_src_name, dst_value); 1248 break; 1249 case OBJ_TAG: 1250 advise(_("The part of the refspec is a tag object.\n" 1251 "Did you mean to create a new tag by pushing to\n" 1252 "'%s:refs/tags/%s'?"), 1253 matched_src_name, dst_value); 1254 break; 1255 case OBJ_TREE: 1256 advise(_("The part of the refspec is a tree object.\n" 1257 "Did you mean to tag a new tree by pushing to\n" 1258 "'%s:refs/tags/%s'?"), 1259 matched_src_name, dst_value); 1260 break; 1261 case OBJ_BLOB: 1262 advise(_("The part of the refspec is a blob object.\n" 1263 "Did you mean to tag a new blob by pushing to\n" 1264 "'%s:refs/tags/%s'?"), 1265 matched_src_name, dst_value); 1266 break; 1267 default: 1268 advise(_("The part of the refspec ('%s') " 1269 "is an object ID that doesn't exist.\n"), 1270 matched_src_name); 1271 break; 1272 } 1273 } 1274 1275 static int match_explicit(struct ref *src, struct ref *dst, 1276 struct ref ***dst_tail, 1277 struct refspec_item *rs) 1278 { 1279 struct ref *matched_src = NULL, *matched_dst = NULL; 1280 int allocated_src = 0, ret; 1281 1282 const char *dst_value = rs->dst; 1283 char *dst_guess; 1284 1285 if (rs->pattern || rs->matching || rs->negative) { 1286 ret = 0; 1287 goto out; 1288 } 1289 1290 if (match_explicit_lhs(src, rs, &matched_src, &allocated_src) < 0) { 1291 ret = -1; 1292 goto out; 1293 } 1294 1295 if (!dst_value) { 1296 int flag; 1297 1298 dst_value = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), 1299 matched_src->name, 1300 RESOLVE_REF_READING, 1301 NULL, &flag); 1302 if (!dst_value || 1303 ((flag & REF_ISSYMREF) && 1304 !starts_with(dst_value, "refs/heads/"))) 1305 die(_("%s cannot be resolved to branch"), 1306 matched_src->name); 1307 } 1308 1309 switch (count_refspec_match(dst_value, dst, &matched_dst)) { 1310 case 1: 1311 break; 1312 case 0: 1313 if (starts_with(dst_value, "refs/")) { 1314 matched_dst = make_linked_ref(dst_value, dst_tail); 1315 } else if (is_null_oid(&matched_src->new_oid)) { 1316 error(_("unable to delete '%s': remote ref does not exist"), 1317 dst_value); 1318 } else if ((dst_guess = guess_ref(dst_value, matched_src))) { 1319 matched_dst = make_linked_ref(dst_guess, dst_tail); 1320 free(dst_guess); 1321 } else { 1322 show_push_unqualified_ref_name_error(dst_value, 1323 matched_src->name); 1324 } 1325 break; 1326 default: 1327 matched_dst = NULL; 1328 error(_("dst refspec %s matches more than one"), 1329 dst_value); 1330 break; 1331 } 1332 1333 if (!matched_dst) { 1334 ret = -1; 1335 goto out; 1336 } 1337 1338 if (matched_dst->peer_ref) { 1339 ret = error(_("dst ref %s receives from more than one src"), 1340 matched_dst->name); 1341 goto out; 1342 } else { 1343 matched_dst->peer_ref = allocated_src ? 1344 matched_src : 1345 copy_ref(matched_src); 1346 matched_dst->force = rs->force; 1347 matched_src = NULL; 1348 } 1349 1350 ret = 0; 1351 1352 out: 1353 if (allocated_src) 1354 free_one_ref(matched_src); 1355 return ret; 1356 } 1357 1358 static int match_explicit_refs(struct ref *src, struct ref *dst, 1359 struct ref ***dst_tail, struct refspec *rs) 1360 { 1361 int i, errs; 1362 for (i = errs = 0; i < rs->nr; i++) 1363 errs += match_explicit(src, dst, dst_tail, &rs->items[i]); 1364 return errs; 1365 } 1366 1367 static char *get_ref_match(const struct refspec *rs, const struct ref *ref, 1368 int send_mirror, int direction, 1369 const struct refspec_item **ret_pat) 1370 { 1371 const struct refspec_item *pat; 1372 char *name; 1373 int i; 1374 int matching_refs = -1; 1375 for (i = 0; i < rs->nr; i++) { 1376 const struct refspec_item *item = &rs->items[i]; 1377 1378 if (item->negative) 1379 continue; 1380 1381 if (item->matching && 1382 (matching_refs == -1 || item->force)) { 1383 matching_refs = i; 1384 continue; 1385 } 1386 1387 if (item->pattern) { 1388 const char *dst_side = item->dst ? item->dst : item->src; 1389 int match; 1390 if (direction == FROM_SRC) 1391 match = match_refname_with_pattern(item->src, ref->name, dst_side, &name); 1392 else 1393 match = match_refname_with_pattern(dst_side, ref->name, item->src, &name); 1394 if (match) { 1395 matching_refs = i; 1396 break; 1397 } 1398 } 1399 } 1400 if (matching_refs == -1) 1401 return NULL; 1402 1403 pat = &rs->items[matching_refs]; 1404 if (pat->matching) { 1405 /* 1406 * "matching refs"; traditionally we pushed everything 1407 * including refs outside refs/heads/ hierarchy, but 1408 * that does not make much sense these days. 1409 */ 1410 if (!send_mirror && !starts_with(ref->name, "refs/heads/")) 1411 return NULL; 1412 name = xstrdup(ref->name); 1413 } 1414 if (ret_pat) 1415 *ret_pat = pat; 1416 return name; 1417 } 1418 1419 static struct ref **tail_ref(struct ref **head) 1420 { 1421 struct ref **tail = head; 1422 while (*tail) 1423 tail = &((*tail)->next); 1424 return tail; 1425 } 1426 1427 static void add_to_tips(struct commit_stack *tips, const struct object_id *oid) 1428 { 1429 struct commit *commit; 1430 1431 if (is_null_oid(oid)) 1432 return; 1433 commit = lookup_commit_reference_gently(the_repository, oid, 1); 1434 if (!commit || (commit->object.flags & TMP_MARK)) 1435 return; 1436 commit->object.flags |= TMP_MARK; 1437 commit_stack_push(tips, commit); 1438 } 1439 1440 static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***dst_tail) 1441 { 1442 struct string_list dst_tag = STRING_LIST_INIT_NODUP; 1443 struct string_list src_tag = STRING_LIST_INIT_NODUP; 1444 struct string_list_item *item; 1445 struct ref *ref; 1446 struct commit_stack sent_tips = COMMIT_STACK_INIT; 1447 1448 /* 1449 * Collect everything we know they would have at the end of 1450 * this push, and collect all tags they have. 1451 */ 1452 for (ref = *dst; ref; ref = ref->next) { 1453 if (ref->peer_ref && 1454 !is_null_oid(&ref->peer_ref->new_oid)) 1455 add_to_tips(&sent_tips, &ref->peer_ref->new_oid); 1456 else 1457 add_to_tips(&sent_tips, &ref->old_oid); 1458 if (starts_with(ref->name, "refs/tags/")) 1459 string_list_append(&dst_tag, ref->name); 1460 } 1461 clear_commit_marks_many(sent_tips.nr, sent_tips.items, TMP_MARK); 1462 1463 string_list_sort(&dst_tag); 1464 1465 /* Collect tags they do not have. */ 1466 for (ref = src; ref; ref = ref->next) { 1467 if (!starts_with(ref->name, "refs/tags/")) 1468 continue; /* not a tag */ 1469 if (string_list_has_string(&dst_tag, ref->name)) 1470 continue; /* they already have it */ 1471 if (odb_read_object_info(the_repository->objects, 1472 &ref->new_oid, NULL) != OBJ_TAG) 1473 continue; /* be conservative */ 1474 item = string_list_append(&src_tag, ref->name); 1475 item->util = ref; 1476 } 1477 string_list_clear(&dst_tag, 0); 1478 1479 /* 1480 * At this point, src_tag lists tags that are missing from 1481 * dst, and sent_tips lists the tips we are pushing or those 1482 * that we know they already have. An element in the src_tag 1483 * that is an ancestor of any of the sent_tips needs to be 1484 * sent to the other side. 1485 */ 1486 if (sent_tips.nr) { 1487 const int reachable_flag = 1; 1488 struct commit_list *found_commits; 1489 struct commit_stack src_commits = COMMIT_STACK_INIT; 1490 1491 for_each_string_list_item(item, &src_tag) { 1492 struct ref *ref = item->util; 1493 struct commit *commit; 1494 1495 if (is_null_oid(&ref->new_oid)) 1496 continue; 1497 commit = lookup_commit_reference_gently(the_repository, 1498 &ref->new_oid, 1499 1); 1500 if (!commit) 1501 /* not pushing a commit, which is not an error */ 1502 continue; 1503 1504 commit_stack_push(&src_commits, commit); 1505 } 1506 1507 found_commits = get_reachable_subset(sent_tips.items, 1508 sent_tips.nr, 1509 src_commits.items, 1510 src_commits.nr, 1511 reachable_flag); 1512 1513 for_each_string_list_item(item, &src_tag) { 1514 struct ref *dst_ref; 1515 struct ref *ref = item->util; 1516 struct commit *commit; 1517 1518 if (is_null_oid(&ref->new_oid)) 1519 continue; 1520 commit = lookup_commit_reference_gently(the_repository, 1521 &ref->new_oid, 1522 1); 1523 if (!commit) 1524 /* not pushing a commit, which is not an error */ 1525 continue; 1526 1527 /* 1528 * Is this tag, which they do not have, reachable from 1529 * any of the commits we are sending? 1530 */ 1531 if (!(commit->object.flags & reachable_flag)) 1532 continue; 1533 1534 /* Add it in */ 1535 dst_ref = make_linked_ref(ref->name, dst_tail); 1536 oidcpy(&dst_ref->new_oid, &ref->new_oid); 1537 dst_ref->peer_ref = copy_ref(ref); 1538 } 1539 1540 clear_commit_marks_many(src_commits.nr, src_commits.items, 1541 reachable_flag); 1542 commit_stack_clear(&src_commits); 1543 commit_list_free(found_commits); 1544 } 1545 1546 string_list_clear(&src_tag, 0); 1547 commit_stack_clear(&sent_tips); 1548 } 1549 1550 struct ref *find_ref_by_name(const struct ref *list, const char *name) 1551 { 1552 for ( ; list; list = list->next) 1553 if (!strcmp(list->name, name)) 1554 return (struct ref *)list; 1555 return NULL; 1556 } 1557 1558 static void prepare_ref_index(struct string_list *ref_index, struct ref *ref) 1559 { 1560 for ( ; ref; ref = ref->next) 1561 string_list_append_nodup(ref_index, ref->name)->util = ref; 1562 1563 string_list_sort(ref_index); 1564 } 1565 1566 /* 1567 * Given only the set of local refs, sanity-check the set of push 1568 * refspecs. We can't catch all errors that match_push_refs would, 1569 * but we can catch some errors early before even talking to the 1570 * remote side. 1571 */ 1572 int check_push_refs(struct ref *src, struct refspec *rs) 1573 { 1574 int ret = 0; 1575 int i; 1576 1577 for (i = 0; i < rs->nr; i++) { 1578 struct refspec_item *item = &rs->items[i]; 1579 1580 if (item->pattern || item->matching || item->negative) 1581 continue; 1582 1583 ret |= match_explicit_lhs(src, item, NULL, NULL); 1584 } 1585 1586 return ret; 1587 } 1588 1589 /* 1590 * Given the set of refs the local repository has, the set of refs the 1591 * remote repository has, and the refspec used for push, determine 1592 * what remote refs we will update and with what value by setting 1593 * peer_ref (which object is being pushed) and force (if the push is 1594 * forced) in elements of "dst". The function may add new elements to 1595 * dst (e.g. pushing to a new branch, done in match_explicit_refs). 1596 */ 1597 int match_push_refs(struct ref *src, struct ref **dst, 1598 struct refspec *rs, int flags) 1599 { 1600 int send_all = flags & MATCH_REFS_ALL; 1601 int send_mirror = flags & MATCH_REFS_MIRROR; 1602 int send_prune = flags & MATCH_REFS_PRUNE; 1603 int errs; 1604 struct ref *ref, **dst_tail = tail_ref(dst); 1605 struct string_list dst_ref_index = STRING_LIST_INIT_NODUP; 1606 1607 /* If no refspec is provided, use the default ":" */ 1608 if (!rs->nr) 1609 refspec_append(rs, ":"); 1610 1611 errs = match_explicit_refs(src, *dst, &dst_tail, rs); 1612 1613 /* pick the remainder */ 1614 for (ref = src; ref; ref = ref->next) { 1615 struct string_list_item *dst_item; 1616 struct ref *dst_peer; 1617 const struct refspec_item *pat = NULL; 1618 char *dst_name; 1619 1620 dst_name = get_ref_match(rs, ref, send_mirror, FROM_SRC, &pat); 1621 if (!dst_name) 1622 continue; 1623 1624 if (!dst_ref_index.nr) 1625 prepare_ref_index(&dst_ref_index, *dst); 1626 1627 dst_item = string_list_lookup(&dst_ref_index, dst_name); 1628 dst_peer = dst_item ? dst_item->util : NULL; 1629 if (dst_peer) { 1630 if (dst_peer->peer_ref) 1631 /* We're already sending something to this ref. */ 1632 goto free_name; 1633 } else { 1634 if (pat->matching && !(send_all || send_mirror)) 1635 /* 1636 * Remote doesn't have it, and we have no 1637 * explicit pattern, and we don't have 1638 * --all or --mirror. 1639 */ 1640 goto free_name; 1641 1642 /* Create a new one and link it */ 1643 dst_peer = make_linked_ref(dst_name, &dst_tail); 1644 oidcpy(&dst_peer->new_oid, &ref->new_oid); 1645 string_list_insert(&dst_ref_index, 1646 dst_peer->name)->util = dst_peer; 1647 } 1648 dst_peer->peer_ref = copy_ref(ref); 1649 dst_peer->force = pat->force; 1650 free_name: 1651 free(dst_name); 1652 } 1653 1654 string_list_clear(&dst_ref_index, 0); 1655 1656 if (flags & MATCH_REFS_FOLLOW_TAGS) 1657 add_missing_tags(src, dst, &dst_tail); 1658 1659 if (send_prune) { 1660 struct string_list src_ref_index = STRING_LIST_INIT_NODUP; 1661 /* check for missing refs on the remote */ 1662 for (ref = *dst; ref; ref = ref->next) { 1663 char *src_name; 1664 1665 if (ref->peer_ref) 1666 /* We're already sending something to this ref. */ 1667 continue; 1668 1669 src_name = get_ref_match(rs, ref, send_mirror, FROM_DST, NULL); 1670 if (src_name) { 1671 if (!src_ref_index.nr) 1672 prepare_ref_index(&src_ref_index, src); 1673 if (!string_list_has_string(&src_ref_index, 1674 src_name)) 1675 ref->peer_ref = alloc_delete_ref(); 1676 free(src_name); 1677 } 1678 } 1679 string_list_clear(&src_ref_index, 0); 1680 } 1681 1682 *dst = apply_negative_refspecs(*dst, rs); 1683 1684 if (errs) 1685 return -1; 1686 return 0; 1687 } 1688 1689 void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, 1690 int force_update) 1691 { 1692 struct ref *ref; 1693 1694 for (ref = remote_refs; ref; ref = ref->next) { 1695 int force_ref_update = ref->force || force_update; 1696 int reject_reason = 0; 1697 1698 if (ref->peer_ref) 1699 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid); 1700 else if (!send_mirror) 1701 continue; 1702 1703 ref->deletion = is_null_oid(&ref->new_oid); 1704 if (!ref->deletion && 1705 oideq(&ref->old_oid, &ref->new_oid)) { 1706 ref->status = REF_STATUS_UPTODATE; 1707 continue; 1708 } 1709 1710 /* 1711 * If the remote ref has moved and is now different 1712 * from what we expect, reject any push. 1713 * 1714 * It also is an error if the user told us to check 1715 * with the remote-tracking branch to find the value 1716 * to expect, but we did not have such a tracking 1717 * branch. 1718 * 1719 * If the tip of the remote-tracking ref is unreachable 1720 * from any reflog entry of its local ref indicating a 1721 * possible update since checkout; reject the push. 1722 */ 1723 if (ref->expect_old_sha1) { 1724 if (!oideq(&ref->old_oid, &ref->old_oid_expect)) 1725 reject_reason = REF_STATUS_REJECT_STALE; 1726 else if (ref->check_reachable && ref->unreachable) 1727 reject_reason = 1728 REF_STATUS_REJECT_REMOTE_UPDATED; 1729 else 1730 /* 1731 * If the ref isn't stale, and is reachable 1732 * from one of the reflog entries of 1733 * the local branch, force the update. 1734 */ 1735 force_ref_update = 1; 1736 } 1737 1738 /* 1739 * If the update isn't already rejected then check 1740 * the usual "must fast-forward" rules. 1741 * 1742 * Decide whether an individual refspec A:B can be 1743 * pushed. The push will succeed if any of the 1744 * following are true: 1745 * 1746 * (1) the remote reference B does not exist 1747 * 1748 * (2) the remote reference B is being removed (i.e., 1749 * pushing :B where no source is specified) 1750 * 1751 * (3) the destination is not under refs/tags/, and 1752 * if the old and new value is a commit, the new 1753 * is a descendant of the old. 1754 * 1755 * (4) it is forced using the +A:B notation, or by 1756 * passing the --force argument 1757 */ 1758 1759 if (!reject_reason && !ref->deletion && !is_null_oid(&ref->old_oid)) { 1760 if (starts_with(ref->name, "refs/tags/")) 1761 reject_reason = REF_STATUS_REJECT_ALREADY_EXISTS; 1762 else if (!odb_has_object(the_repository->objects, &ref->old_oid, ODB_HAS_OBJECT_RECHECK_PACKED)) 1763 reject_reason = REF_STATUS_REJECT_FETCH_FIRST; 1764 else if (!lookup_commit_reference_gently(the_repository, &ref->old_oid, 1) || 1765 !lookup_commit_reference_gently(the_repository, &ref->new_oid, 1)) 1766 reject_reason = REF_STATUS_REJECT_NEEDS_FORCE; 1767 else if (!ref_newer(&ref->new_oid, &ref->old_oid)) 1768 reject_reason = REF_STATUS_REJECT_NONFASTFORWARD; 1769 } 1770 1771 /* 1772 * "--force" will defeat any rejection implemented 1773 * by the rules above. 1774 */ 1775 if (!force_ref_update) 1776 ref->status = reject_reason; 1777 else if (reject_reason) 1778 ref->forced_update = 1; 1779 } 1780 } 1781 1782 static void set_merge(struct repository *repo, struct branch *ret) 1783 { 1784 struct remote *remote; 1785 char *ref; 1786 struct object_id oid; 1787 int i; 1788 1789 if (!ret) 1790 return; /* no branch */ 1791 if (ret->set_merge) 1792 return; /* already run */ 1793 if (!ret->remote_name || !ret->merge_nr) { 1794 /* 1795 * no merge config; let's make sure we don't confuse callers 1796 * with a non-zero merge_nr but a NULL merge 1797 */ 1798 merge_clear(ret); 1799 return; 1800 } 1801 ret->set_merge = 1; 1802 1803 remote = remotes_remote_get(repo, ret->remote_name); 1804 1805 for (i = 0; i < ret->merge_nr; i++) { 1806 if (!remote_find_tracking(remote, ret->merge[i]) || 1807 strcmp(ret->remote_name, ".")) 1808 continue; 1809 if (repo_dwim_ref(repo, ret->merge[i]->src, 1810 strlen(ret->merge[i]->src), &oid, &ref, 1811 0) == 1) 1812 ret->merge[i]->dst = ref; 1813 else 1814 ret->merge[i]->dst = xstrdup(ret->merge[i]->src); 1815 } 1816 } 1817 1818 static struct branch *repo_branch_get(struct repository *repo, const char *name) 1819 { 1820 struct branch *ret; 1821 1822 read_config(repo, 0); 1823 if (!name || !*name || !strcmp(name, "HEAD")) 1824 ret = repo->remote_state->current_branch; 1825 else 1826 ret = make_branch(repo->remote_state, name, 1827 strlen(name)); 1828 set_merge(repo, ret); 1829 return ret; 1830 } 1831 1832 struct branch *branch_get(const char *name) 1833 { 1834 return repo_branch_get(the_repository, name); 1835 } 1836 1837 const char *repo_default_remote(struct repository *repo) 1838 { 1839 struct branch *branch; 1840 1841 read_config(repo, 0); 1842 branch = repo_branch_get(repo, "HEAD"); 1843 1844 return remotes_remote_for_branch(repo->remote_state, branch, NULL); 1845 } 1846 1847 const char *repo_remote_from_url(struct repository *repo, const char *url) 1848 { 1849 char *rewritten_url; 1850 const char *remote_name = NULL; 1851 1852 read_config(repo, 0); 1853 if ((rewritten_url = alias_url(url, &repo->remote_state->rewrites))) 1854 url = rewritten_url; 1855 1856 for (int i = 0; i < repo->remote_state->remotes_nr; i++) { 1857 struct remote *remote = repo->remote_state->remotes[i]; 1858 if (!remote) 1859 continue; 1860 1861 if (remote_has_url(remote, url)) { 1862 remote_name = remote->name; 1863 break; 1864 } 1865 } 1866 free(rewritten_url); 1867 return remote_name; 1868 } 1869 1870 int branch_has_merge_config(struct branch *branch) 1871 { 1872 return branch && branch->set_merge; 1873 } 1874 1875 int branch_merge_matches(struct branch *branch, 1876 int i, 1877 const char *refname) 1878 { 1879 if (!branch || i < 0 || i >= branch->merge_nr) 1880 return 0; 1881 return refname_match(branch->merge[i]->src, refname); 1882 } 1883 1884 __attribute__((format (printf,2,3))) 1885 static char *error_buf(struct strbuf *err, const char *fmt, ...) 1886 { 1887 if (err) { 1888 va_list ap; 1889 va_start(ap, fmt); 1890 strbuf_vaddf(err, fmt, ap); 1891 va_end(ap); 1892 } 1893 return NULL; 1894 } 1895 1896 const char *branch_get_upstream(struct branch *branch, struct strbuf *err) 1897 { 1898 if (!branch) 1899 return error_buf(err, _("HEAD does not point to a branch")); 1900 1901 if (!branch->merge || !branch->merge[0]) { 1902 /* 1903 * no merge config; is it because the user didn't define any, 1904 * or because it is not a real branch, and get_branch 1905 * auto-vivified it? 1906 */ 1907 if (!refs_ref_exists(get_main_ref_store(the_repository), branch->refname)) 1908 return error_buf(err, _("no such branch: '%s'"), 1909 branch->name); 1910 return error_buf(err, 1911 _("no upstream configured for branch '%s'"), 1912 branch->name); 1913 } 1914 1915 if (!branch->merge[0]->dst) 1916 return error_buf(err, 1917 _("upstream branch '%s' not stored as a remote-tracking branch"), 1918 branch->merge[0]->src); 1919 1920 return branch->merge[0]->dst; 1921 } 1922 1923 struct remote *repo_remote_for_push_tracking(struct repository *repo, 1924 struct remote *remote) 1925 { 1926 const struct strvec *push_urls; 1927 struct remote *first_match = NULL; 1928 struct remote_state *remote_state = repo->remote_state; 1929 const char *check_url; 1930 1931 if (remote->origin != REMOTE_UNCONFIGURED) 1932 return remote; 1933 1934 push_urls = push_url_of_remote(remote); 1935 if (push_urls->nr != 1) 1936 return remote; 1937 check_url = push_urls->v[0]; 1938 1939 for (int i = 0; i < remote_state->remotes_nr; i++) { 1940 struct remote *candidate = remote_state->remotes[i]; 1941 1942 if (!candidate || candidate == remote || 1943 !remote_is_configured(candidate, 0) || 1944 !remote_has_push_url(candidate, check_url)) 1945 continue; 1946 if (first_match) 1947 return remote; 1948 first_match = candidate; 1949 } 1950 1951 return first_match ? first_match : remote; 1952 } 1953 1954 static char *tracking_for_push_dest(struct repository *repo, 1955 struct remote *remote, 1956 const char *refname, 1957 struct strbuf *err) 1958 { 1959 char *ret; 1960 1961 remote = repo_remote_for_push_tracking(repo, remote); 1962 ret = apply_refspecs(&remote->fetch, refname); 1963 if (!ret) 1964 return error_buf(err, 1965 _("push destination '%s' on remote '%s' has no local tracking branch"), 1966 refname, remote->name); 1967 return ret; 1968 } 1969 1970 static char *branch_get_push_1(struct repository *repo, 1971 struct branch *branch, struct strbuf *err) 1972 { 1973 struct remote_state *remote_state = repo->remote_state; 1974 struct remote *remote; 1975 1976 remote = remotes_remote_get( 1977 repo, 1978 remotes_pushremote_for_branch(remote_state, branch, NULL)); 1979 if (!remote) 1980 return error_buf(err, 1981 _("branch '%s' has no remote for pushing"), 1982 branch->name); 1983 1984 if (remote->push.nr) { 1985 char *dst; 1986 char *ret; 1987 1988 dst = apply_refspecs(&remote->push, branch->refname); 1989 if (!dst) 1990 return error_buf(err, 1991 _("push refspecs for '%s' do not include '%s'"), 1992 remote->name, branch->name); 1993 1994 ret = tracking_for_push_dest(repo, remote, dst, err); 1995 free(dst); 1996 return ret; 1997 } 1998 1999 if (remote->mirror) 2000 return tracking_for_push_dest(repo, remote, branch->refname, err); 2001 2002 switch (repo_config_values(repo)->push_default) { 2003 case PUSH_DEFAULT_NOTHING: 2004 return error_buf(err, _("push has no destination (push.default is 'nothing')")); 2005 2006 case PUSH_DEFAULT_MATCHING: 2007 case PUSH_DEFAULT_CURRENT: 2008 return tracking_for_push_dest(repo, remote, branch->refname, err); 2009 2010 case PUSH_DEFAULT_UPSTREAM: 2011 return xstrdup_or_null(branch_get_upstream(branch, err)); 2012 2013 case PUSH_DEFAULT_UNSPECIFIED: 2014 case PUSH_DEFAULT_SIMPLE: 2015 { 2016 const char *up; 2017 char *cur; 2018 2019 up = branch_get_upstream(branch, err); 2020 if (!up) 2021 return NULL; 2022 cur = tracking_for_push_dest(repo, remote, branch->refname, err); 2023 if (!cur) 2024 return NULL; 2025 if (strcmp(cur, up)) { 2026 free(cur); 2027 return error_buf(err, 2028 _("cannot resolve 'simple' push to a single destination")); 2029 } 2030 return cur; 2031 } 2032 } 2033 2034 BUG("unhandled push situation"); 2035 } 2036 2037 const char *branch_get_push(struct branch *branch, struct strbuf *err) 2038 { 2039 read_config(the_repository, 0); 2040 die_on_missing_branch(the_repository, branch); 2041 2042 if (!branch) 2043 return error_buf(err, _("HEAD does not point to a branch")); 2044 2045 if (!branch->push_tracking_ref) 2046 branch->push_tracking_ref = branch_get_push_1( 2047 the_repository, branch, err); 2048 return branch->push_tracking_ref; 2049 } 2050 2051 static int ignore_symref_update(const char *refname, struct strbuf *scratch) 2052 { 2053 return !refs_read_symbolic_ref(get_main_ref_store(the_repository), refname, scratch); 2054 } 2055 2056 /* 2057 * Create and return a list of (struct ref) consisting of copies of 2058 * each remote_ref that matches refspec. refspec must be a pattern. 2059 * Fill in the copies' peer_ref to describe the local tracking refs to 2060 * which they map. Omit any references that would map to an existing 2061 * local symbolic ref. 2062 */ 2063 static struct ref *get_expanded_map(const struct ref *remote_refs, 2064 const struct refspec_item *refspec) 2065 { 2066 struct strbuf scratch = STRBUF_INIT; 2067 const struct ref *ref; 2068 struct ref *ret = NULL; 2069 struct ref **tail = &ret; 2070 2071 for (ref = remote_refs; ref; ref = ref->next) { 2072 char *expn_name = NULL; 2073 2074 strbuf_reset(&scratch); 2075 2076 if (strchr(ref->name, '^')) 2077 continue; /* a dereference item */ 2078 if (match_refname_with_pattern(refspec->src, ref->name, 2079 refspec->dst, &expn_name) && 2080 !ignore_symref_update(expn_name, &scratch)) { 2081 struct ref *cpy = copy_ref(ref); 2082 2083 if (cpy->peer_ref) 2084 free_one_ref(cpy->peer_ref); 2085 cpy->peer_ref = alloc_ref(expn_name); 2086 if (refspec->force) 2087 cpy->peer_ref->force = 1; 2088 *tail = cpy; 2089 tail = &cpy->next; 2090 } 2091 free(expn_name); 2092 } 2093 2094 strbuf_release(&scratch); 2095 return ret; 2096 } 2097 2098 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name) 2099 { 2100 const struct ref *ref; 2101 const struct ref *best_match = NULL; 2102 int best_score = 0; 2103 2104 for (ref = refs; ref; ref = ref->next) { 2105 int score = refname_match(name, ref->name); 2106 2107 if (best_score < score) { 2108 best_match = ref; 2109 best_score = score; 2110 } 2111 } 2112 return best_match; 2113 } 2114 2115 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name) 2116 { 2117 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name); 2118 2119 if (!ref) 2120 return NULL; 2121 2122 return copy_ref(ref); 2123 } 2124 2125 static struct ref *get_local_ref(const char *name) 2126 { 2127 if (!name || name[0] == '\0') 2128 return NULL; 2129 2130 if (starts_with(name, "refs/")) 2131 return alloc_ref(name); 2132 2133 if (starts_with(name, "heads/") || 2134 starts_with(name, "tags/") || 2135 starts_with(name, "remotes/")) 2136 return alloc_ref_with_prefix("refs/", 5, name); 2137 2138 return alloc_ref_with_prefix("refs/heads/", 11, name); 2139 } 2140 2141 int get_fetch_map(const struct ref *remote_refs, 2142 const struct refspec_item *refspec, 2143 struct ref ***tail, 2144 int missing_ok) 2145 { 2146 struct ref *ref_map, **rmp; 2147 2148 if (refspec->negative) 2149 return 0; 2150 2151 if (refspec->pattern) { 2152 ref_map = get_expanded_map(remote_refs, refspec); 2153 } else { 2154 const char *name = refspec->src[0] ? refspec->src : "HEAD"; 2155 2156 if (refspec->exact_sha1) { 2157 ref_map = alloc_ref(name); 2158 get_oid_hex(name, &ref_map->old_oid); 2159 ref_map->exact_oid = 1; 2160 } else { 2161 ref_map = get_remote_ref(remote_refs, name); 2162 } 2163 if (!missing_ok && !ref_map) 2164 die(_("couldn't find remote ref %s"), name); 2165 if (ref_map) { 2166 ref_map->peer_ref = get_local_ref(refspec->dst); 2167 if (ref_map->peer_ref && refspec->force) 2168 ref_map->peer_ref->force = 1; 2169 } 2170 } 2171 2172 for (rmp = &ref_map; *rmp; ) { 2173 if ((*rmp)->peer_ref) { 2174 if (!starts_with((*rmp)->peer_ref->name, "refs/") || 2175 check_refname_format((*rmp)->peer_ref->name, 0)) { 2176 struct ref *ignore = *rmp; 2177 error(_("* Ignoring funny ref '%s' locally"), 2178 (*rmp)->peer_ref->name); 2179 *rmp = (*rmp)->next; 2180 free(ignore->peer_ref); 2181 free(ignore); 2182 continue; 2183 } 2184 } 2185 rmp = &((*rmp)->next); 2186 } 2187 2188 if (ref_map) 2189 tail_link_ref(ref_map, tail); 2190 2191 return 0; 2192 } 2193 2194 int get_remote_group(const char *key, const char *value, 2195 const struct config_context *ctx UNUSED, 2196 void *priv) 2197 { 2198 struct remote_group_data *g = priv; 2199 2200 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) { 2201 /* split list by white space */ 2202 while (*value) { 2203 size_t wordlen = strcspn(value, " \t\n"); 2204 2205 if (wordlen >= 1) 2206 string_list_append_nodup(g->list, 2207 xstrndup(value, wordlen)); 2208 value += wordlen + (value[wordlen] != '\0'); 2209 } 2210 } 2211 2212 return 0; 2213 } 2214 2215 int add_remote_or_group(const char *name, struct string_list *list) 2216 { 2217 int prev_nr = list->nr; 2218 struct remote_group_data g; 2219 g.name = name; g.list = list; 2220 2221 repo_config(the_repository, get_remote_group, &g); 2222 if (list->nr == prev_nr) { 2223 struct remote *remote = remote_get(name); 2224 if (!remote_is_configured(remote, 0)) 2225 return 0; 2226 string_list_append(list, remote->name); 2227 } 2228 return 1; 2229 } 2230 2231 int resolve_remote_symref(struct ref *ref, struct ref *list) 2232 { 2233 if (!ref->symref) 2234 return 0; 2235 for (; list; list = list->next) 2236 if (!strcmp(ref->symref, list->name)) { 2237 oidcpy(&ref->old_oid, &list->old_oid); 2238 return 0; 2239 } 2240 return 1; 2241 } 2242 2243 /* 2244 * Compute the commit ahead/behind values for the pair branch_name, base. 2245 * 2246 * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the 2247 * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip 2248 * the (potentially expensive) a/b computation (*num_ours and *num_theirs are 2249 * set to zero). 2250 * 2251 * Returns -1 if num_ours and num_theirs could not be filled in (e.g., ref 2252 * does not exist). Returns 0 if the commits are identical. Returns 1 if 2253 * commits are different. 2254 */ 2255 2256 static int stat_branch_pair(const char *branch_name, const char *base, 2257 int *num_ours, int *num_theirs, 2258 enum ahead_behind_flags abf) 2259 { 2260 struct object_id oid; 2261 struct commit *ours, *theirs; 2262 struct rev_info revs; 2263 struct strvec argv = STRVEC_INIT; 2264 2265 /* Cannot stat if what we used to build on no longer exists */ 2266 if (refs_read_ref(get_main_ref_store(the_repository), base, &oid)) 2267 return -1; 2268 theirs = lookup_commit_reference(the_repository, &oid); 2269 if (!theirs) 2270 return -1; 2271 2272 if (refs_read_ref(get_main_ref_store(the_repository), branch_name, &oid)) 2273 return -1; 2274 ours = lookup_commit_reference(the_repository, &oid); 2275 if (!ours) 2276 return -1; 2277 2278 *num_theirs = *num_ours = 0; 2279 2280 /* are we the same? */ 2281 if (theirs == ours) 2282 return 0; 2283 if (abf == AHEAD_BEHIND_QUICK) 2284 return 1; 2285 if (abf != AHEAD_BEHIND_FULL) 2286 BUG("stat_branch_pair: invalid abf '%d'", abf); 2287 2288 /* Run "rev-list --left-right ours...theirs" internally... */ 2289 strvec_push(&argv, ""); /* ignored */ 2290 strvec_push(&argv, "--left-right"); 2291 strvec_pushf(&argv, "%s...%s", 2292 oid_to_hex(&ours->object.oid), 2293 oid_to_hex(&theirs->object.oid)); 2294 strvec_push(&argv, "--"); 2295 2296 repo_init_revisions(the_repository, &revs, NULL); 2297 setup_revisions_from_strvec(&argv, &revs, NULL); 2298 if (prepare_revision_walk(&revs)) 2299 die(_("revision walk setup failed")); 2300 2301 /* ... and count the commits on each side. */ 2302 while (1) { 2303 struct commit *c = get_revision(&revs); 2304 if (!c) 2305 break; 2306 if (c->object.flags & SYMMETRIC_LEFT) 2307 (*num_ours)++; 2308 else 2309 (*num_theirs)++; 2310 } 2311 2312 /* clear object flags smudged by the above traversal */ 2313 clear_commit_marks(ours, ALL_REV_FLAGS); 2314 clear_commit_marks(theirs, ALL_REV_FLAGS); 2315 2316 strvec_clear(&argv); 2317 release_revisions(&revs); 2318 return 1; 2319 } 2320 2321 /* 2322 * Lookup the tracking branch for the given branch and if present, optionally 2323 * compute the commit ahead/behind values for the pair. 2324 * 2325 * If for_push is true, the tracking branch refers to the push branch, 2326 * otherwise it refers to the upstream branch. 2327 * 2328 * The name of the tracking branch (or NULL if it is not defined) is 2329 * returned via *tracking_name, if it is not itself NULL. 2330 * 2331 * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the 2332 * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip 2333 * the (potentially expensive) a/b computation (*num_ours and *num_theirs are 2334 * set to zero). 2335 * 2336 * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no 2337 * upstream defined, or ref does not exist). Returns 0 if the commits are 2338 * identical. Returns 1 if commits are different. 2339 */ 2340 int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, 2341 const char **tracking_name, int for_push, 2342 enum ahead_behind_flags abf) 2343 { 2344 const char *base; 2345 2346 /* Cannot stat unless we are marked to build on top of somebody else. */ 2347 base = for_push ? branch_get_push(branch, NULL) : 2348 branch_get_upstream(branch, NULL); 2349 if (tracking_name) 2350 *tracking_name = base; 2351 if (!base) 2352 return -1; 2353 2354 return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf); 2355 } 2356 2357 static char *resolve_compare_branch(struct branch *branch, const char *name) 2358 { 2359 const char *resolved = NULL; 2360 2361 if (!branch || !name) 2362 return NULL; 2363 2364 if (!strcasecmp(name, "@{upstream}")) { 2365 resolved = branch_get_upstream(branch, NULL); 2366 } else if (!strcasecmp(name, "@{push}")) { 2367 resolved = branch_get_push(branch, NULL); 2368 } else { 2369 warning(_("ignoring value '%s' for status.compareBranches, " 2370 "only @{upstream} and @{push} are supported"), 2371 name); 2372 return NULL; 2373 } 2374 2375 if (resolved) 2376 return xstrdup(resolved); 2377 return NULL; 2378 } 2379 2380 static void format_branch_comparison(struct strbuf *sb, 2381 bool up_to_date, 2382 int ours, int theirs, 2383 const char *branch_name, 2384 const char *push_remote_name, 2385 const char *push_branch_name, 2386 enum ahead_behind_flags abf, 2387 unsigned flags) 2388 { 2389 bool use_push_advice = (flags & ENABLE_ADVICE_PUSH); 2390 bool use_pull_advice = (flags & ENABLE_ADVICE_PULL); 2391 bool use_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE); 2392 2393 if (up_to_date) { 2394 strbuf_addf(sb, 2395 _("Your branch is up to date with '%s'.\n"), 2396 branch_name); 2397 } else if (abf == AHEAD_BEHIND_QUICK) { 2398 strbuf_addf(sb, 2399 _("Your branch and '%s' refer to different commits.\n"), 2400 branch_name); 2401 if (use_push_advice && advice_enabled(ADVICE_STATUS_HINTS)) 2402 strbuf_addf(sb, _(" (use \"%s\" for details)\n"), 2403 "git status --ahead-behind"); 2404 } else if (!theirs) { 2405 strbuf_addf(sb, 2406 Q_("Your branch is ahead of '%s' by %d commit.\n", 2407 "Your branch is ahead of '%s' by %d commits.\n", 2408 ours), 2409 branch_name, ours); 2410 if (use_push_advice && advice_enabled(ADVICE_STATUS_HINTS)) 2411 strbuf_addstr(sb, 2412 _(" (use \"git push\" to publish your local commits)\n")); 2413 } else if (!ours) { 2414 strbuf_addf(sb, 2415 Q_("Your branch is behind '%s' by %d commit, " 2416 "and can be fast-forwarded.\n", 2417 "Your branch is behind '%s' by %d commits, " 2418 "and can be fast-forwarded.\n", 2419 theirs), 2420 branch_name, theirs); 2421 if (use_pull_advice && advice_enabled(ADVICE_STATUS_HINTS)) { 2422 if (push_remote_name && push_branch_name) 2423 strbuf_addf(sb, 2424 _(" (use \"git pull %s %s\" to update your local branch)\n"), 2425 push_remote_name, push_branch_name); 2426 else 2427 strbuf_addstr(sb, 2428 _(" (use \"git pull\" to update your local branch)\n")); 2429 } 2430 } else { 2431 strbuf_addf(sb, 2432 Q_("Your branch and '%s' have diverged,\n" 2433 "and have %d and %d different commit each, " 2434 "respectively.\n", 2435 "Your branch and '%s' have diverged,\n" 2436 "and have %d and %d different commits each, " 2437 "respectively.\n", 2438 ours + theirs), 2439 branch_name, ours, theirs); 2440 if (use_divergence_advice && advice_enabled(ADVICE_STATUS_HINTS)) { 2441 if (push_remote_name && push_branch_name) 2442 strbuf_addf(sb, 2443 _(" (use \"git pull %s %s\" if you want to integrate the remote branch with yours)\n"), 2444 push_remote_name, push_branch_name); 2445 else 2446 strbuf_addstr(sb, 2447 _(" (use \"git pull\" if you want to integrate the remote branch with yours)\n")); 2448 } 2449 } 2450 } 2451 2452 /* 2453 * Return true when there is anything to report, otherwise false. 2454 */ 2455 int format_tracking_info(struct branch *branch, struct strbuf *sb, 2456 enum ahead_behind_flags abf, 2457 int show_divergence_advice) 2458 { 2459 char *compare_branches = NULL; 2460 struct string_list branches = STRING_LIST_INIT_DUP; 2461 struct strset processed_refs = STRSET_INIT; 2462 int reported = 0; 2463 size_t i; 2464 const char *upstream_ref; 2465 const char *push_ref; 2466 2467 repo_config_get_string(the_repository, "status.comparebranches", 2468 &compare_branches); 2469 2470 if (compare_branches) { 2471 string_list_split(&branches, compare_branches, " ", -1); 2472 string_list_remove_empty_items(&branches, 0); 2473 } else { 2474 string_list_append(&branches, "@{upstream}"); 2475 } 2476 2477 upstream_ref = branch_get_upstream(branch, NULL); 2478 push_ref = branch_get_push(branch, NULL); 2479 2480 for (i = 0; i < branches.nr; i++) { 2481 char *full_ref; 2482 char *short_ref; 2483 int ours, theirs, cmp; 2484 int is_upstream, is_push; 2485 unsigned flags = 0; 2486 const char *push_remote_name = NULL; 2487 const char *push_branch_name = NULL; 2488 2489 full_ref = resolve_compare_branch(branch, 2490 branches.items[i].string); 2491 if (!full_ref) 2492 continue; 2493 2494 if (!strset_add(&processed_refs, full_ref)) { 2495 free(full_ref); 2496 continue; 2497 } 2498 2499 short_ref = refs_shorten_unambiguous_ref( 2500 get_main_ref_store(the_repository), full_ref, 0); 2501 2502 is_upstream = upstream_ref && !strcmp(full_ref, upstream_ref); 2503 is_push = push_ref && !strcmp(full_ref, push_ref); 2504 2505 if (is_upstream && (!push_ref || !strcmp(upstream_ref, push_ref))) 2506 is_push = 1; 2507 2508 cmp = stat_branch_pair(branch->refname, full_ref, 2509 &ours, &theirs, abf); 2510 2511 if (cmp < 0) { 2512 if (is_upstream) { 2513 strbuf_addf(sb, 2514 _("Your branch is based on '%s', but the upstream is gone.\n"), 2515 short_ref); 2516 if (advice_enabled(ADVICE_STATUS_HINTS)) 2517 strbuf_addstr(sb, 2518 _(" (use \"git branch --unset-upstream\" to fixup)\n")); 2519 reported = 1; 2520 } 2521 free(full_ref); 2522 free(short_ref); 2523 continue; 2524 } 2525 2526 if (reported) 2527 strbuf_addstr(sb, "\n"); 2528 2529 if (is_upstream) 2530 flags |= ENABLE_ADVICE_PULL; 2531 if (show_divergence_advice && is_upstream) 2532 flags |= ENABLE_ADVICE_DIVERGENCE; 2533 if (is_push) { 2534 flags |= ENABLE_ADVICE_PUSH; 2535 if (!upstream_ref || strcmp(upstream_ref, full_ref)) { 2536 push_remote_name = pushremote_for_branch(branch, NULL); 2537 if (push_remote_name && 2538 skip_prefix(full_ref, "refs/remotes/", &push_branch_name) && 2539 skip_prefix(push_branch_name, push_remote_name, &push_branch_name) && 2540 *push_branch_name == '/') { 2541 push_branch_name++; 2542 flags |= ENABLE_ADVICE_PULL; 2543 } else { 2544 push_remote_name = NULL; 2545 } 2546 } else { 2547 flags |= ENABLE_ADVICE_PULL; 2548 } 2549 } 2550 format_branch_comparison(sb, !cmp, ours, theirs, short_ref, 2551 push_remote_name, push_branch_name, 2552 abf, flags); 2553 reported = 1; 2554 2555 free(full_ref); 2556 free(short_ref); 2557 } 2558 2559 string_list_clear(&branches, 0); 2560 strset_clear(&processed_refs); 2561 free(compare_branches); 2562 return reported; 2563 } 2564 2565 static int one_local_ref(const struct reference *ref, void *cb_data) 2566 { 2567 struct ref ***local_tail = cb_data; 2568 struct ref *local_ref; 2569 2570 /* we already know it starts with refs/ to get here */ 2571 if (check_refname_format(ref->name + 5, 0)) 2572 return 0; 2573 2574 local_ref = alloc_ref(ref->name); 2575 oidcpy(&local_ref->new_oid, ref->oid); 2576 **local_tail = local_ref; 2577 *local_tail = &local_ref->next; 2578 return 0; 2579 } 2580 2581 struct ref *get_local_heads(void) 2582 { 2583 struct ref *local_refs = NULL, **local_tail = &local_refs; 2584 2585 refs_for_each_ref(get_main_ref_store(the_repository), one_local_ref, 2586 &local_tail); 2587 return local_refs; 2588 } 2589 2590 struct ref *guess_remote_head(const struct ref *head, 2591 const struct ref *refs, 2592 unsigned flags) 2593 { 2594 const struct ref *r; 2595 struct ref *list = NULL; 2596 struct ref **tail = &list; 2597 2598 if (!head) 2599 return NULL; 2600 2601 /* 2602 * Some transports support directly peeking at 2603 * where HEAD points; if that is the case, then 2604 * we don't have to guess. 2605 */ 2606 if (head->symref) 2607 return copy_ref(find_ref_by_name(refs, head->symref)); 2608 2609 /* If a remote branch exists with the default branch name, let's use it. */ 2610 if (!(flags & REMOTE_GUESS_HEAD_ALL)) { 2611 char *default_branch = 2612 repo_default_branch_name(the_repository, 2613 flags & REMOTE_GUESS_HEAD_QUIET); 2614 char *ref = xstrfmt("refs/heads/%s", default_branch); 2615 2616 r = find_ref_by_name(refs, ref); 2617 free(ref); 2618 free(default_branch); 2619 2620 if (r && oideq(&r->old_oid, &head->old_oid)) 2621 return copy_ref(r); 2622 2623 /* Fall back to the hard-coded historical default */ 2624 r = find_ref_by_name(refs, "refs/heads/master"); 2625 if (r && oideq(&r->old_oid, &head->old_oid)) 2626 return copy_ref(r); 2627 } 2628 2629 /* Look for another ref that points there */ 2630 for (r = refs; r; r = r->next) { 2631 if (r != head && 2632 starts_with(r->name, "refs/heads/") && 2633 oideq(&r->old_oid, &head->old_oid)) { 2634 *tail = copy_ref(r); 2635 tail = &((*tail)->next); 2636 if (!(flags & REMOTE_GUESS_HEAD_ALL)) 2637 break; 2638 } 2639 } 2640 2641 return list; 2642 } 2643 2644 struct stale_heads_info { 2645 struct string_list *ref_names; 2646 struct ref **stale_refs_tail; 2647 struct refspec *rs; 2648 }; 2649 2650 static int get_stale_heads_cb(const struct reference *ref, void *cb_data) 2651 { 2652 struct stale_heads_info *info = cb_data; 2653 struct string_list matches = STRING_LIST_INIT_DUP; 2654 struct refspec_item query; 2655 int i, stale = 1; 2656 memset(&query, 0, sizeof(struct refspec_item)); 2657 query.dst = (char *)ref->name; 2658 2659 refspec_find_all_matches(info->rs, &query, &matches); 2660 if (matches.nr == 0) 2661 goto clean_exit; /* No matches */ 2662 2663 /* 2664 * If we did find a suitable refspec and it's not a symref and 2665 * it's not in the list of refs that currently exist in that 2666 * remote, we consider it to be stale. In order to deal with 2667 * overlapping refspecs, we need to go over all of the 2668 * matching refs. 2669 */ 2670 if (ref->flags & REF_ISSYMREF) 2671 goto clean_exit; 2672 2673 for (i = 0; stale && i < matches.nr; i++) 2674 if (string_list_has_string(info->ref_names, matches.items[i].string)) 2675 stale = 0; 2676 2677 if (stale) { 2678 struct ref *linked_ref = make_linked_ref(ref->name, &info->stale_refs_tail); 2679 oidcpy(&linked_ref->new_oid, ref->oid); 2680 } 2681 2682 clean_exit: 2683 string_list_clear(&matches, 0); 2684 return 0; 2685 } 2686 2687 struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map) 2688 { 2689 struct ref *ref, *stale_refs = NULL; 2690 struct string_list ref_names = STRING_LIST_INIT_NODUP; 2691 struct stale_heads_info info; 2692 2693 info.ref_names = &ref_names; 2694 info.stale_refs_tail = &stale_refs; 2695 info.rs = rs; 2696 for (ref = fetch_map; ref; ref = ref->next) 2697 string_list_append(&ref_names, ref->name); 2698 string_list_sort(&ref_names); 2699 refs_for_each_ref(get_main_ref_store(the_repository), 2700 get_stale_heads_cb, &info); 2701 string_list_clear(&ref_names, 0); 2702 return stale_refs; 2703 } 2704 2705 /* 2706 * Compare-and-swap 2707 */ 2708 void clear_cas_option(struct push_cas_option *cas) 2709 { 2710 int i; 2711 2712 for (i = 0; i < cas->nr; i++) 2713 free(cas->entry[i].refname); 2714 free(cas->entry); 2715 memset(cas, 0, sizeof(*cas)); 2716 } 2717 2718 static struct push_cas *add_cas_entry(struct push_cas_option *cas, 2719 const char *refname, 2720 size_t refnamelen) 2721 { 2722 struct push_cas *entry; 2723 ALLOC_GROW(cas->entry, cas->nr + 1, cas->alloc); 2724 entry = &cas->entry[cas->nr++]; 2725 memset(entry, 0, sizeof(*entry)); 2726 entry->refname = xmemdupz(refname, refnamelen); 2727 return entry; 2728 } 2729 2730 static int parse_push_cas_option(struct push_cas_option *cas, const char *arg, int unset) 2731 { 2732 const char *colon; 2733 struct push_cas *entry; 2734 2735 if (unset) { 2736 /* "--no-