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