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