Git fork
at reftables-rust 1969 lines 57 kB view raw
1#define USE_THE_REPOSITORY_VARIABLE 2 3#include "builtin.h" 4#include "advice.h" 5#include "config.h" 6#include "date.h" 7#include "gettext.h" 8#include "ident.h" 9#include "parse-options.h" 10#include "path.h" 11#include "transport.h" 12#include "remote.h" 13#include "string-list.h" 14#include "strbuf.h" 15#include "run-command.h" 16#include "rebase.h" 17#include "refs.h" 18#include "refspec.h" 19#include "odb.h" 20#include "strvec.h" 21#include "commit-reach.h" 22#include "progress.h" 23 24static const char * const builtin_remote_usage[] = { 25 "git remote [-v | --verbose]", 26 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"), 27 N_("git remote rename [--[no-]progress] <old> <new>"), 28 N_("git remote remove <name>"), 29 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"), 30 N_("git remote [-v | --verbose] show [-n] <name>"), 31 N_("git remote prune [-n | --dry-run] <name>"), 32 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"), 33 N_("git remote set-branches [--add] <name> <branch>..."), 34 N_("git remote get-url [--push] [--all] <name>"), 35 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"), 36 N_("git remote set-url --add <name> <newurl>"), 37 N_("git remote set-url --delete <name> <url>"), 38 NULL 39}; 40 41static const char * const builtin_remote_add_usage[] = { 42 N_("git remote add [<options>] <name> <url>"), 43 NULL 44}; 45 46static const char * const builtin_remote_rename_usage[] = { 47 N_("git remote rename [--[no-]progress] <old> <new>"), 48 NULL 49}; 50 51static const char * const builtin_remote_rm_usage[] = { 52 N_("git remote remove <name>"), 53 NULL 54}; 55 56static const char * const builtin_remote_sethead_usage[] = { 57 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"), 58 NULL 59}; 60 61static const char * const builtin_remote_setbranches_usage[] = { 62 N_("git remote set-branches <name> <branch>..."), 63 N_("git remote set-branches --add <name> <branch>..."), 64 NULL 65}; 66 67static const char * const builtin_remote_show_usage[] = { 68 N_("git remote show [<options>] <name>"), 69 NULL 70}; 71 72static const char * const builtin_remote_prune_usage[] = { 73 N_("git remote prune [<options>] <name>"), 74 NULL 75}; 76 77static const char * const builtin_remote_update_usage[] = { 78 N_("git remote update [<options>] [<group> | <remote>]..."), 79 NULL 80}; 81 82static const char * const builtin_remote_geturl_usage[] = { 83 N_("git remote get-url [--push] [--all] <name>"), 84 NULL 85}; 86 87static const char * const builtin_remote_seturl_usage[] = { 88 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"), 89 N_("git remote set-url --add <name> <newurl>"), 90 N_("git remote set-url --delete <name> <url>"), 91 NULL 92}; 93 94#define GET_REF_STATES (1<<0) 95#define GET_HEAD_NAMES (1<<1) 96#define GET_PUSH_REF_STATES (1<<2) 97 98static int verbose; 99 100static int fetch_remote(const char *name) 101{ 102 struct child_process cmd = CHILD_PROCESS_INIT; 103 104 strvec_push(&cmd.args, "fetch"); 105 if (verbose) 106 strvec_push(&cmd.args, "-v"); 107 strvec_push(&cmd.args, name); 108 cmd.git_cmd = 1; 109 printf_ln(_("Updating %s"), name); 110 if (run_command(&cmd)) 111 return error(_("Could not fetch %s"), name); 112 return 0; 113} 114 115enum { 116 TAGS_UNSET = 0, 117 TAGS_DEFAULT = 1, 118 TAGS_SET = 2 119}; 120 121#define MIRROR_NONE 0 122#define MIRROR_FETCH 1 123#define MIRROR_PUSH 2 124#define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH) 125 126static void add_branch(const char *key, const char *branchname, 127 const char *remotename, int mirror, struct strbuf *tmp) 128{ 129 strbuf_reset(tmp); 130 strbuf_addch(tmp, '+'); 131 if (mirror) 132 strbuf_addf(tmp, "refs/%s:refs/%s", 133 branchname, branchname); 134 else 135 strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s", 136 branchname, remotename, branchname); 137 repo_config_set_multivar(the_repository, key, tmp->buf, "^$", 0); 138} 139 140static const char mirror_advice[] = 141N_("--mirror is dangerous and deprecated; please\n" 142 "\t use --mirror=fetch or --mirror=push instead"); 143 144static int parse_mirror_opt(const struct option *opt, const char *arg, int not) 145{ 146 unsigned *mirror = opt->value; 147 if (not) 148 *mirror = MIRROR_NONE; 149 else if (!arg) { 150 warning("%s", _(mirror_advice)); 151 *mirror = MIRROR_BOTH; 152 } 153 else if (!strcmp(arg, "fetch")) 154 *mirror = MIRROR_FETCH; 155 else if (!strcmp(arg, "push")) 156 *mirror = MIRROR_PUSH; 157 else 158 return error(_("unknown --mirror argument: %s"), arg); 159 return 0; 160} 161 162static int check_remote_collision(struct remote *remote, void *data) 163{ 164 const char *name = data; 165 const char *p; 166 167 if (skip_prefix(name, remote->name, &p) && *p == '/') 168 die(_("remote name '%s' is a subset of existing remote '%s'"), 169 name, remote->name); 170 if (skip_prefix(remote->name, name, &p) && *p == '/') 171 die(_("remote name '%s' is a superset of existing remote '%s'"), 172 name, remote->name); 173 174 return 0; 175} 176 177static int add(int argc, const char **argv, const char *prefix, 178 struct repository *repo UNUSED) 179{ 180 int fetch = 0, fetch_tags = TAGS_DEFAULT; 181 unsigned mirror = MIRROR_NONE; 182 struct string_list track = STRING_LIST_INIT_NODUP; 183 const char *master = NULL; 184 struct remote *remote; 185 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT; 186 const char *name, *url; 187 int result = 0; 188 189 struct option options[] = { 190 OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")), 191 OPT_SET_INT(0, "tags", &fetch_tags, 192 N_("import all tags and associated objects when fetching\n" 193 "or do not fetch any tag at all (--no-tags)"), 194 TAGS_SET), 195 OPT_STRING_LIST('t', "track", &track, N_("branch"), 196 N_("branch(es) to track")), 197 OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")), 198 OPT_CALLBACK_F(0, "mirror", &mirror, "(push|fetch)", 199 N_("set up remote as a mirror to push to or fetch from"), 200 PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt), 201 OPT_END() 202 }; 203 204 argc = parse_options(argc, argv, prefix, options, 205 builtin_remote_add_usage, 0); 206 207 if (argc != 2) 208 usage_with_options(builtin_remote_add_usage, options); 209 210 if (mirror && master) 211 die(_("specifying a master branch makes no sense with --mirror")); 212 if (mirror && !(mirror & MIRROR_FETCH) && track.nr) 213 die(_("specifying branches to track makes sense only with fetch mirrors")); 214 215 name = argv[0]; 216 url = argv[1]; 217 218 remote = remote_get(name); 219 if (remote_is_configured(remote, 1)) { 220 error(_("remote %s already exists."), name); 221 exit(3); 222 } 223 224 if (!valid_remote_name(name)) 225 die(_("'%s' is not a valid remote name"), name); 226 227 for_each_remote(check_remote_collision, (void *)name); 228 229 strbuf_addf(&buf, "remote.%s.url", name); 230 repo_config_set(the_repository, buf.buf, url); 231 232 if (!mirror || mirror & MIRROR_FETCH) { 233 strbuf_reset(&buf); 234 strbuf_addf(&buf, "remote.%s.fetch", name); 235 if (track.nr == 0) 236 string_list_append(&track, "*"); 237 for (size_t i = 0; i < track.nr; i++) { 238 add_branch(buf.buf, track.items[i].string, 239 name, mirror, &buf2); 240 } 241 } 242 243 if (mirror & MIRROR_PUSH) { 244 strbuf_reset(&buf); 245 strbuf_addf(&buf, "remote.%s.mirror", name); 246 repo_config_set(the_repository, buf.buf, "true"); 247 } 248 249 if (fetch_tags != TAGS_DEFAULT) { 250 strbuf_reset(&buf); 251 strbuf_addf(&buf, "remote.%s.tagOpt", name); 252 repo_config_set(the_repository, buf.buf, 253 fetch_tags == TAGS_SET ? "--tags" : "--no-tags"); 254 } 255 256 if (fetch && fetch_remote(name)) { 257 result = 1; 258 goto out; 259 } 260 261 if (master) { 262 strbuf_reset(&buf); 263 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name); 264 265 strbuf_reset(&buf2); 266 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master); 267 268 if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote add")) 269 result = error(_("Could not setup master '%s'"), master); 270 } 271 272out: 273 strbuf_release(&buf); 274 strbuf_release(&buf2); 275 string_list_clear(&track, 0); 276 277 return result; 278} 279 280struct branch_info { 281 char *remote_name; 282 struct string_list merge; 283 enum rebase_type rebase; 284 char *push_remote_name; 285}; 286 287static struct string_list branch_list = STRING_LIST_INIT_DUP; 288 289static const char *abbrev_ref(const char *name, const char *prefix) 290{ 291 skip_prefix(name, prefix, &name); 292 return name; 293} 294#define abbrev_branch(name) abbrev_ref((name), "refs/heads/") 295 296static int config_read_branches(const char *key, const char *value, 297 const struct config_context *ctx UNUSED, 298 void *data UNUSED) 299{ 300 const char *orig_key = key; 301 char *name; 302 struct string_list_item *item; 303 struct branch_info *info; 304 enum { REMOTE, MERGE, REBASE, PUSH_REMOTE } type; 305 size_t key_len; 306 307 if (!starts_with(key, "branch.")) 308 return 0; 309 310 key += strlen("branch."); 311 if (strip_suffix(key, ".remote", &key_len)) 312 type = REMOTE; 313 else if (strip_suffix(key, ".merge", &key_len)) 314 type = MERGE; 315 else if (strip_suffix(key, ".rebase", &key_len)) 316 type = REBASE; 317 else if (strip_suffix(key, ".pushremote", &key_len)) 318 type = PUSH_REMOTE; 319 else 320 return 0; 321 322 name = xmemdupz(key, key_len); 323 item = string_list_insert(&branch_list, name); 324 325 if (!item->util) 326 item->util = xcalloc(1, sizeof(struct branch_info)); 327 info = item->util; 328 switch (type) { 329 case REMOTE: 330 if (info->remote_name) 331 warning(_("more than one %s"), orig_key); 332 info->remote_name = xstrdup(value); 333 break; 334 case MERGE: { 335 char *space = strchr(value, ' '); 336 value = abbrev_branch(value); 337 while (space) { 338 char *merge; 339 merge = xstrndup(value, space - value); 340 string_list_append(&info->merge, merge); 341 value = abbrev_branch(space + 1); 342 space = strchr(value, ' '); 343 } 344 string_list_append(&info->merge, xstrdup(value)); 345 break; 346 } 347 case REBASE: 348 /* 349 * Consider invalid values as false and check the 350 * truth value with >= REBASE_TRUE. 351 */ 352 info->rebase = rebase_parse_value(value); 353 if (info->rebase == REBASE_INVALID) 354 warning(_("unhandled branch.%s.rebase=%s; assuming " 355 "'true'"), name, value); 356 break; 357 case PUSH_REMOTE: 358 if (info->push_remote_name) 359 warning(_("more than one %s"), orig_key); 360 info->push_remote_name = xstrdup(value); 361 break; 362 default: 363 BUG("unexpected type=%d", type); 364 } 365 366 free(name); 367 return 0; 368} 369 370static void read_branches(void) 371{ 372 if (branch_list.nr) 373 return; 374 repo_config(the_repository, config_read_branches, NULL); 375} 376 377struct ref_states { 378 struct remote *remote; 379 struct string_list new_refs, skipped, stale, tracked, heads, push; 380 int queried; 381}; 382 383#define REF_STATES_INIT { \ 384 .new_refs = STRING_LIST_INIT_DUP, \ 385 .skipped = STRING_LIST_INIT_DUP, \ 386 .stale = STRING_LIST_INIT_DUP, \ 387 .tracked = STRING_LIST_INIT_DUP, \ 388 .heads = STRING_LIST_INIT_DUP, \ 389 .push = STRING_LIST_INIT_DUP, \ 390} 391 392static int get_ref_states(const struct ref *remote_refs, struct ref_states *states) 393{ 394 struct ref *fetch_map = NULL, **tail = &fetch_map; 395 struct ref *ref, *stale_refs; 396 int i; 397 398 for (i = 0; i < states->remote->fetch.nr; i++) 399 if (get_fetch_map(remote_refs, &states->remote->fetch.items[i], &tail, 1)) 400 die(_("Could not get fetch map for refspec %s"), 401 states->remote->fetch.items[i].raw); 402 403 for (ref = fetch_map; ref; ref = ref->next) { 404 if (refname_matches_negative_refspec_item(ref->name, &states->remote->fetch)) 405 string_list_append(&states->skipped, abbrev_branch(ref->name)); 406 else if (!ref->peer_ref || !refs_ref_exists(get_main_ref_store(the_repository), ref->peer_ref->name)) 407 string_list_append(&states->new_refs, abbrev_branch(ref->name)); 408 else 409 string_list_append(&states->tracked, abbrev_branch(ref->name)); 410 } 411 stale_refs = get_stale_heads(&states->remote->fetch, fetch_map); 412 for (ref = stale_refs; ref; ref = ref->next) { 413 struct string_list_item *item = 414 string_list_append(&states->stale, abbrev_branch(ref->name)); 415 item->util = xstrdup(ref->name); 416 } 417 free_refs(stale_refs); 418 free_refs(fetch_map); 419 420 string_list_sort(&states->new_refs); 421 string_list_sort(&states->skipped); 422 string_list_sort(&states->tracked); 423 string_list_sort(&states->stale); 424 425 return 0; 426} 427 428struct push_info { 429 char *dest; 430 int forced; 431 enum { 432 PUSH_STATUS_CREATE = 0, 433 PUSH_STATUS_DELETE, 434 PUSH_STATUS_UPTODATE, 435 PUSH_STATUS_FASTFORWARD, 436 PUSH_STATUS_OUTOFDATE, 437 PUSH_STATUS_NOTQUERIED 438 } status; 439}; 440 441static int get_push_ref_states(const struct ref *remote_refs, 442 struct ref_states *states) 443{ 444 struct remote *remote = states->remote; 445 struct ref *ref, *local_refs, *push_map; 446 if (remote->mirror) 447 return 0; 448 449 local_refs = get_local_heads(); 450 push_map = copy_ref_list(remote_refs); 451 452 match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE); 453 454 for (ref = push_map; ref; ref = ref->next) { 455 struct string_list_item *item; 456 struct push_info *info; 457 458 if (!ref->peer_ref) 459 continue; 460 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid); 461 462 item = string_list_append(&states->push, 463 abbrev_branch(ref->peer_ref->name)); 464 item->util = xcalloc(1, sizeof(struct push_info)); 465 info = item->util; 466 info->forced = ref->force; 467 info->dest = xstrdup(abbrev_branch(ref->name)); 468 469 if (is_null_oid(&ref->new_oid)) { 470 info->status = PUSH_STATUS_DELETE; 471 } else if (oideq(&ref->old_oid, &ref->new_oid)) 472 info->status = PUSH_STATUS_UPTODATE; 473 else if (is_null_oid(&ref->old_oid)) 474 info->status = PUSH_STATUS_CREATE; 475 else if (odb_has_object(the_repository->objects, &ref->old_oid, 476 HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR) && 477 ref_newer(&ref->new_oid, &ref->old_oid)) 478 info->status = PUSH_STATUS_FASTFORWARD; 479 else 480 info->status = PUSH_STATUS_OUTOFDATE; 481 } 482 free_refs(local_refs); 483 free_refs(push_map); 484 return 0; 485} 486 487static int get_push_ref_states_noquery(struct ref_states *states) 488{ 489 int i; 490 struct remote *remote = states->remote; 491 struct string_list_item *item; 492 struct push_info *info; 493 494 if (remote->mirror) 495 return 0; 496 497 if (!remote->push.nr) { 498 item = string_list_append(&states->push, _("(matching)")); 499 info = item->util = xcalloc(1, sizeof(struct push_info)); 500 info->status = PUSH_STATUS_NOTQUERIED; 501 info->dest = xstrdup(item->string); 502 } 503 for (i = 0; i < remote->push.nr; i++) { 504 const struct refspec_item *spec = &remote->push.items[i]; 505 if (spec->matching) 506 item = string_list_append(&states->push, _("(matching)")); 507 else if (strlen(spec->src)) 508 item = string_list_append(&states->push, spec->src); 509 else 510 item = string_list_append(&states->push, _("(delete)")); 511 512 info = item->util = xcalloc(1, sizeof(struct push_info)); 513 info->forced = spec->force; 514 info->status = PUSH_STATUS_NOTQUERIED; 515 info->dest = xstrdup(spec->dst ? spec->dst : item->string); 516 } 517 return 0; 518} 519 520static int get_head_names(const struct ref *remote_refs, struct ref_states *states) 521{ 522 struct ref *ref, *matches; 523 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map; 524 struct refspec_item refspec = { 525 .force = 0, 526 .pattern = 1, 527 .src = (char *) "refs/heads/*", 528 .dst = (char *) "refs/heads/*", 529 }; 530 531 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0); 532 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"), 533 fetch_map, REMOTE_GUESS_HEAD_ALL); 534 for (ref = matches; ref; ref = ref->next) 535 string_list_append(&states->heads, abbrev_branch(ref->name)); 536 537 free_refs(fetch_map); 538 free_refs(matches); 539 return 0; 540} 541 542struct known_remote { 543 struct known_remote *next; 544 struct remote *remote; 545}; 546 547struct known_remotes { 548 struct remote *to_delete; 549 struct known_remote *list; 550}; 551 552static int add_known_remote(struct remote *remote, void *cb_data) 553{ 554 struct known_remotes *all = cb_data; 555 struct known_remote *r; 556 557 if (!strcmp(all->to_delete->name, remote->name)) 558 return 0; 559 560 r = xmalloc(sizeof(*r)); 561 r->remote = remote; 562 r->next = all->list; 563 all->list = r; 564 return 0; 565} 566 567struct branches_for_remote { 568 struct remote *remote; 569 struct string_list *branches, *skipped; 570 struct known_remotes *keep; 571}; 572 573static int add_branch_for_removal(const char *refname, 574 const char *referent UNUSED, 575 const struct object_id *oid UNUSED, 576 int flags UNUSED, void *cb_data) 577{ 578 struct branches_for_remote *branches = cb_data; 579 struct refspec_item refspec; 580 struct known_remote *kr; 581 582 memset(&refspec, 0, sizeof(refspec)); 583 refspec.dst = (char *)refname; 584 if (remote_find_tracking(branches->remote, &refspec)) 585 return 0; 586 free(refspec.src); 587 588 /* don't delete a branch if another remote also uses it */ 589 for (kr = branches->keep->list; kr; kr = kr->next) { 590 memset(&refspec, 0, sizeof(refspec)); 591 refspec.dst = (char *)refname; 592 if (!remote_find_tracking(kr->remote, &refspec)) { 593 free(refspec.src); 594 return 0; 595 } 596 } 597 598 /* don't delete non-remote-tracking refs */ 599 if (!starts_with(refname, "refs/remotes/")) { 600 /* advise user how to delete local branches */ 601 if (starts_with(refname, "refs/heads/")) 602 string_list_append(branches->skipped, 603 abbrev_branch(refname)); 604 /* silently skip over other non-remote refs */ 605 return 0; 606 } 607 608 string_list_append(branches->branches, refname); 609 610 return 0; 611} 612 613struct rename_info { 614 const char *old_name; 615 const char *new_name; 616 struct ref_transaction *transaction; 617 struct progress *progress; 618 struct strbuf *err; 619 uint32_t progress_nr; 620 uint64_t index; 621}; 622 623static void compute_renamed_ref(struct rename_info *rename, 624 const char *refname, 625 struct strbuf *out) 626{ 627 strbuf_reset(out); 628 strbuf_addstr(out, refname); 629 strbuf_splice(out, strlen("refs/remotes/"), strlen(rename->old_name), 630 rename->new_name, strlen(rename->new_name)); 631} 632 633static int rename_one_reflog_entry(const char *old_refname, 634 struct object_id *old_oid, 635 struct object_id *new_oid, 636 const char *committer, 637 timestamp_t timestamp, int tz, 638 const char *msg, void *cb_data) 639{ 640 struct rename_info *rename = cb_data; 641 struct strbuf new_refname = STRBUF_INIT; 642 struct strbuf identity = STRBUF_INIT; 643 struct strbuf name = STRBUF_INIT; 644 struct strbuf mail = STRBUF_INIT; 645 struct ident_split ident; 646 const char *date; 647 int error; 648 649 compute_renamed_ref(rename, old_refname, &new_refname); 650 651 if (split_ident_line(&ident, committer, strlen(committer)) < 0) { 652 error = -1; 653 goto out; 654 } 655 656 strbuf_add(&name, ident.name_begin, ident.name_end - ident.name_begin); 657 strbuf_add(&mail, ident.mail_begin, ident.mail_end - ident.mail_begin); 658 659 date = show_date(timestamp, tz, DATE_MODE(NORMAL)); 660 strbuf_addstr(&identity, fmt_ident(name.buf, mail.buf, 661 WANT_BLANK_IDENT, date, 0)); 662 663 error = ref_transaction_update_reflog(rename->transaction, new_refname.buf, 664 new_oid, old_oid, identity.buf, msg, 665 rename->index++, rename->err); 666 667out: 668 strbuf_release(&new_refname); 669 strbuf_release(&identity); 670 strbuf_release(&name); 671 strbuf_release(&mail); 672 return error; 673} 674 675static int rename_one_reflog(const char *old_refname, 676 const struct object_id *old_oid, 677 struct rename_info *rename) 678{ 679 struct strbuf new_refname = STRBUF_INIT; 680 struct strbuf message = STRBUF_INIT; 681 int error; 682 683 if (!refs_reflog_exists(get_main_ref_store(the_repository), old_refname)) 684 return 0; 685 686 error = refs_for_each_reflog_ent(get_main_ref_store(the_repository), 687 old_refname, rename_one_reflog_entry, rename); 688 if (error < 0) 689 goto out; 690 691 compute_renamed_ref(rename, old_refname, &new_refname); 692 693 /* 694 * Manually write the reflog entry for the now-renamed ref. We cannot 695 * rely on `rename_one_ref()` to do this for us as that would screw 696 * over order in which reflog entries are being written. 697 * 698 * Furthermore, we only append the entry in case the reference 699 * resolves. Missing references shouldn't have reflogs anyway. 700 */ 701 strbuf_addf(&message, "remote: renamed %s to %s", old_refname, 702 new_refname.buf); 703 704 error = ref_transaction_update_reflog(rename->transaction, new_refname.buf, 705 old_oid, old_oid, git_committer_info(0), 706 message.buf, rename->index++, rename->err); 707 if (error < 0) 708 return error; 709 710out: 711 strbuf_release(&new_refname); 712 strbuf_release(&message); 713 return error; 714} 715 716static int rename_one_ref(const char *old_refname, const char *referent, 717 const struct object_id *oid, 718 int flags, void *cb_data) 719{ 720 struct strbuf new_referent = STRBUF_INIT; 721 struct strbuf new_refname = STRBUF_INIT; 722 struct rename_info *rename = cb_data; 723 int error; 724 725 compute_renamed_ref(rename, old_refname, &new_refname); 726 727 if (flags & REF_ISSYMREF) { 728 /* 729 * Stupidly enough `referent` is not pointing to the immediate 730 * target of a symref, but it's the recursively resolved value. 731 * So symrefs pointing to symrefs would be misresolved, and 732 * unborn symrefs don't have any value for the `referent` at all. 733 */ 734 referent = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), 735 old_refname, RESOLVE_REF_NO_RECURSE, 736 NULL, NULL); 737 compute_renamed_ref(rename, referent, &new_referent); 738 oid = NULL; 739 } 740 741 error = ref_transaction_delete(rename->transaction, old_refname, 742 oid, referent, REF_NO_DEREF, NULL, rename->err); 743 if (error < 0) 744 goto out; 745 746 error = ref_transaction_update(rename->transaction, new_refname.buf, oid, null_oid(the_hash_algo), 747 (flags & REF_ISSYMREF) ? new_referent.buf : NULL, NULL, 748 REF_SKIP_CREATE_REFLOG | REF_NO_DEREF | REF_SKIP_OID_VERIFICATION, 749 NULL, rename->err); 750 if (error < 0) 751 goto out; 752 753 error = rename_one_reflog(old_refname, oid, rename); 754 if (error < 0) 755 goto out; 756 757 display_progress(rename->progress, ++rename->progress_nr); 758 759out: 760 strbuf_release(&new_referent); 761 strbuf_release(&new_refname); 762 return error; 763} 764 765static int migrate_file(struct remote *remote) 766{ 767 struct strbuf buf = STRBUF_INIT; 768 769 strbuf_addf(&buf, "remote.%s.url", remote->name); 770 for (size_t i = 0; i < remote->url.nr; i++) 771 repo_config_set_multivar(the_repository, buf.buf, remote->url.v[i], "^$", 0); 772 strbuf_reset(&buf); 773 strbuf_addf(&buf, "remote.%s.push", remote->name); 774 for (int i = 0; i < remote->push.nr; i++) 775 repo_config_set_multivar(the_repository, buf.buf, remote->push.items[i].raw, "^$", 0); 776 strbuf_reset(&buf); 777 strbuf_addf(&buf, "remote.%s.fetch", remote->name); 778 for (int i = 0; i < remote->fetch.nr; i++) 779 repo_config_set_multivar(the_repository, buf.buf, remote->fetch.items[i].raw, "^$", 0); 780#ifndef WITH_BREAKING_CHANGES 781 if (remote->origin == REMOTE_REMOTES) 782 unlink_or_warn(repo_git_path_replace(the_repository, &buf, 783 "remotes/%s", remote->name)); 784 else if (remote->origin == REMOTE_BRANCHES) 785 unlink_or_warn(repo_git_path_replace(the_repository, &buf, 786 "branches/%s", remote->name)); 787#endif /* WITH_BREAKING_CHANGES */ 788 strbuf_release(&buf); 789 790 return 0; 791} 792 793struct push_default_info 794{ 795 const char *old_name; 796 enum config_scope scope; 797 struct strbuf origin; 798 int linenr; 799}; 800 801static int config_read_push_default(const char *key, const char *value, 802 const struct config_context *ctx, void *cb) 803{ 804 const struct key_value_info *kvi = ctx->kvi; 805 806 struct push_default_info* info = cb; 807 if (strcmp(key, "remote.pushdefault") || 808 !value || strcmp(value, info->old_name)) 809 return 0; 810 811 info->scope = kvi->scope; 812 strbuf_reset(&info->origin); 813 strbuf_addstr(&info->origin, config_origin_type_name(kvi->origin_type)); 814 info->linenr = kvi->linenr; 815 816 return 0; 817} 818 819static void handle_push_default(const char* old_name, const char* new_name) 820{ 821 struct push_default_info push_default = { 822 .old_name = old_name, 823 .scope = CONFIG_SCOPE_UNKNOWN, 824 .origin = STRBUF_INIT, 825 .linenr = -1, 826 }; 827 repo_config(the_repository, config_read_push_default, &push_default); 828 if (push_default.scope >= CONFIG_SCOPE_COMMAND) 829 ; /* pass */ 830 else if (push_default.scope >= CONFIG_SCOPE_LOCAL) { 831 int result = repo_config_set_gently(the_repository, "remote.pushDefault", 832 new_name); 833 if (new_name && result && result != CONFIG_NOTHING_SET) 834 die(_("could not set '%s'"), "remote.pushDefault"); 835 else if (!new_name && result && result != CONFIG_NOTHING_SET) 836 die(_("could not unset '%s'"), "remote.pushDefault"); 837 } else if (push_default.scope >= CONFIG_SCOPE_SYSTEM) { 838 /* warn */ 839 warning(_("The %s configuration remote.pushDefault in:\n" 840 "\t%s:%d\n" 841 "now names the non-existent remote '%s'"), 842 config_scope_name(push_default.scope), 843 push_default.origin.buf, push_default.linenr, 844 old_name); 845 } 846 847 strbuf_release(&push_default.origin); 848} 849 850static const char conflicting_remote_refs_advice[] = N_( 851 "The remote you are trying to rename has conflicting references in the\n" 852 "new target refspec. This is most likely caused by you trying to nest\n" 853 "a remote into itself, e.g. by renaming 'parent' into 'parent/child'\n" 854 "or by unnesting a remote, e.g. the other way round.\n" 855 "\n" 856 "If that is the case, you can address this by first renaming the\n" 857 "remote to a different name.\n"); 858 859static int mv(int argc, const char **argv, const char *prefix, 860 struct repository *repo UNUSED) 861{ 862 int show_progress = isatty(2); 863 struct option options[] = { 864 OPT_BOOL(0, "progress", &show_progress, N_("force progress reporting")), 865 OPT_END() 866 }; 867 struct remote *oldremote, *newremote; 868 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT, 869 old_remote_context = STRBUF_INIT, err = STRBUF_INIT; 870 struct rename_info rename = { 871 .err = &err, 872 }; 873 int refspecs_need_update = 0; 874 int result = 0; 875 876 argc = parse_options(argc, argv, prefix, options, 877 builtin_remote_rename_usage, 0); 878 879 if (argc != 2) 880 usage_with_options(builtin_remote_rename_usage, options); 881 882 rename.old_name = argv[0]; 883 rename.new_name = argv[1]; 884 885 oldremote = remote_get(rename.old_name); 886 if (!remote_is_configured(oldremote, 1)) { 887 error(_("No such remote: '%s'"), rename.old_name); 888 exit(2); 889 } 890 891 if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG) 892 return migrate_file(oldremote); 893 894 newremote = remote_get(rename.new_name); 895 if (remote_is_configured(newremote, 1)) { 896 error(_("remote %s already exists."), rename.new_name); 897 exit(3); 898 } 899 900 if (!valid_remote_name(rename.new_name)) 901 die(_("'%s' is not a valid remote name"), rename.new_name); 902 903 strbuf_addf(&buf, "remote.%s", rename.old_name); 904 strbuf_addf(&buf2, "remote.%s", rename.new_name); 905 if (repo_config_rename_section(the_repository, buf.buf, buf2.buf) < 1) { 906 result = error(_("Could not rename config section '%s' to '%s'"), 907 buf.buf, buf2.buf); 908 goto out; 909 } 910 911 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name); 912 913 for (int i = 0; i < oldremote->fetch.nr && !refspecs_need_update; i++) 914 refspecs_need_update = !!strstr(oldremote->fetch.items[i].raw, 915 old_remote_context.buf); 916 917 if (refspecs_need_update) { 918 rename.transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), 919 0, &err); 920 if (!rename.transaction) 921 goto out; 922 923 if (show_progress) 924 rename.progress = start_delayed_progress(the_repository, 925 _("Renaming remote references"), 0); 926 927 strbuf_reset(&buf); 928 strbuf_addf(&buf, "refs/remotes/%s/", rename.old_name); 929 930 result = refs_for_each_rawref_in(get_main_ref_store(the_repository), buf.buf, 931 rename_one_ref, &rename); 932 if (result < 0) 933 die(_("queueing remote ref renames failed: %s"), rename.err->buf); 934 935 result = ref_transaction_prepare(rename.transaction, &err); 936 if (result < 0) { 937 error("renaming remote references failed: %s", err.buf); 938 if (result == REF_TRANSACTION_ERROR_NAME_CONFLICT) 939 advise(conflicting_remote_refs_advice); 940 die(NULL); 941 } 942 } 943 944 if (oldremote->fetch.nr) { 945 strbuf_reset(&buf); 946 strbuf_addf(&buf, "remote.%s.fetch", rename.new_name); 947 repo_config_set_multivar(the_repository, buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE); 948 for (int i = 0; i < oldremote->fetch.nr; i++) { 949 char *ptr; 950 951 strbuf_reset(&buf2); 952 strbuf_addstr(&buf2, oldremote->fetch.items[i].raw); 953 ptr = strstr(buf2.buf, old_remote_context.buf); 954 if (ptr) { 955 strbuf_splice(&buf2, 956 ptr-buf2.buf + strlen(":refs/remotes/"), 957 strlen(rename.old_name), rename.new_name, 958 strlen(rename.new_name)); 959 } else 960 warning(_("Not updating non-default fetch refspec\n" 961 "\t%s\n" 962 "\tPlease update the configuration manually if necessary."), 963 buf2.buf); 964 965 repo_config_set_multivar(the_repository, buf.buf, buf2.buf, "^$", 0); 966 } 967 } 968 969 read_branches(); 970 for (size_t i = 0; i < branch_list.nr; i++) { 971 struct string_list_item *item = branch_list.items + i; 972 struct branch_info *info = item->util; 973 if (info->remote_name && !strcmp(info->remote_name, rename.old_name)) { 974 strbuf_reset(&buf); 975 strbuf_addf(&buf, "branch.%s.remote", item->string); 976 repo_config_set(the_repository, buf.buf, rename.new_name); 977 } 978 if (info->push_remote_name && !strcmp(info->push_remote_name, rename.old_name)) { 979 strbuf_reset(&buf); 980 strbuf_addf(&buf, "branch.%s.pushRemote", item->string); 981 repo_config_set(the_repository, buf.buf, rename.new_name); 982 } 983 } 984 985 if (refspecs_need_update) { 986 result = ref_transaction_commit(rename.transaction, &err); 987 if (result < 0) 988 die(_("renaming remote refs failed: %s"), rename.err->buf); 989 990 stop_progress(&rename.progress); 991 992 handle_push_default(rename.old_name, rename.new_name); 993 } 994 995out: 996 ref_transaction_free(rename.transaction); 997 strbuf_release(&old_remote_context); 998 strbuf_release(&buf); 999 strbuf_release(&buf2); 1000 strbuf_release(&buf3); 1001 strbuf_release(&err); 1002 return result; 1003} 1004 1005static int rm(int argc, const char **argv, const char *prefix, 1006 struct repository *repo UNUSED) 1007{ 1008 struct option options[] = { 1009 OPT_END() 1010 }; 1011 struct remote *remote; 1012 struct strbuf buf = STRBUF_INIT; 1013 struct known_remotes known_remotes = { NULL, NULL }; 1014 struct string_list branches = STRING_LIST_INIT_DUP; 1015 struct string_list skipped = STRING_LIST_INIT_DUP; 1016 struct branches_for_remote cb_data; 1017 int result; 1018 1019 memset(&cb_data, 0, sizeof(cb_data)); 1020 cb_data.branches = &branches; 1021 cb_data.skipped = &skipped; 1022 cb_data.keep = &known_remotes; 1023 1024 argc = parse_options(argc, argv, prefix, options, 1025 builtin_remote_rm_usage, 0); 1026 if (argc != 1) 1027 usage_with_options(builtin_remote_rm_usage, options); 1028 1029 remote = remote_get(argv[0]); 1030 if (!remote_is_configured(remote, 1)) { 1031 error(_("No such remote: '%s'"), argv[0]); 1032 exit(2); 1033 } 1034 1035 known_remotes.to_delete = remote; 1036 for_each_remote(add_known_remote, &known_remotes); 1037 1038 read_branches(); 1039 for (size_t i = 0; i < branch_list.nr; i++) { 1040 struct string_list_item *item = branch_list.items + i; 1041 struct branch_info *info = item->util; 1042 if (info->remote_name && !strcmp(info->remote_name, remote->name)) { 1043 const char *keys[] = { "remote", "merge", NULL }, **k; 1044 for (k = keys; *k; k++) { 1045 strbuf_reset(&buf); 1046 strbuf_addf(&buf, "branch.%s.%s", 1047 item->string, *k); 1048 result = repo_config_set_gently(the_repository, buf.buf, NULL); 1049 if (result && result != CONFIG_NOTHING_SET) 1050 die(_("could not unset '%s'"), buf.buf); 1051 } 1052 } 1053 if (info->push_remote_name && !strcmp(info->push_remote_name, remote->name)) { 1054 strbuf_reset(&buf); 1055 strbuf_addf(&buf, "branch.%s.pushremote", item->string); 1056 result = repo_config_set_gently(the_repository, buf.buf, NULL); 1057 if (result && result != CONFIG_NOTHING_SET) 1058 die(_("could not unset '%s'"), buf.buf); 1059 } 1060 } 1061 1062 /* 1063 * We cannot just pass a function to for_each_ref() which deletes 1064 * the branches one by one, since for_each_ref() relies on cached 1065 * refs, which are invalidated when deleting a branch. 1066 */ 1067 cb_data.remote = remote; 1068 result = refs_for_each_ref(get_main_ref_store(the_repository), 1069 add_branch_for_removal, &cb_data); 1070 strbuf_release(&buf); 1071 1072 if (!result) 1073 result = refs_delete_refs(get_main_ref_store(the_repository), 1074 "remote: remove", &branches, 1075 REF_NO_DEREF); 1076 string_list_clear(&branches, 0); 1077 1078 if (skipped.nr) { 1079 fprintf_ln(stderr, 1080 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n" 1081 "to delete it, use:", 1082 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n" 1083 "to delete them, use:", 1084 skipped.nr)); 1085 for (size_t i = 0; i < skipped.nr; i++) 1086 fprintf(stderr, " git branch -d %s\n", 1087 skipped.items[i].string); 1088 } 1089 string_list_clear(&skipped, 0); 1090 1091 if (!result) { 1092 strbuf_addf(&buf, "remote.%s", remote->name); 1093 if (repo_config_rename_section(the_repository, buf.buf, NULL) < 1) { 1094 result = error(_("Could not remove config section '%s'"), buf.buf); 1095 goto out; 1096 } 1097 1098 handle_push_default(remote->name, NULL); 1099 } 1100 1101out: 1102 for (struct known_remote *r = known_remotes.list; r;) { 1103 struct known_remote *next = r->next; 1104 free(r); 1105 r = next; 1106 } 1107 strbuf_release(&buf); 1108 return result; 1109} 1110 1111static void clear_push_info(void *util, const char *string UNUSED) 1112{ 1113 struct push_info *info = util; 1114 free(info->dest); 1115 free(info); 1116} 1117 1118static void free_remote_ref_states(struct ref_states *states) 1119{ 1120 string_list_clear(&states->new_refs, 0); 1121 string_list_clear(&states->skipped, 0); 1122 string_list_clear(&states->stale, 1); 1123 string_list_clear(&states->tracked, 0); 1124 string_list_clear(&states->heads, 0); 1125 string_list_clear_func(&states->push, clear_push_info); 1126} 1127 1128static int append_ref_to_tracked_list(const char *refname, 1129 const char *referent UNUSED, 1130 const struct object_id *oid UNUSED, 1131 int flags, void *cb_data) 1132{ 1133 struct ref_states *states = cb_data; 1134 struct refspec_item refspec; 1135 1136 if (flags & REF_ISSYMREF) 1137 return 0; 1138 1139 memset(&refspec, 0, sizeof(refspec)); 1140 refspec.dst = (char *)refname; 1141 if (!remote_find_tracking(states->remote, &refspec)) { 1142 string_list_append(&states->tracked, abbrev_branch(refspec.src)); 1143 free(refspec.src); 1144 } 1145 1146 return 0; 1147} 1148 1149static int get_remote_ref_states(const char *name, 1150 struct ref_states *states, 1151 int query) 1152{ 1153 states->remote = remote_get(name); 1154 if (!states->remote) 1155 return error(_("No such remote: '%s'"), name); 1156 1157 read_branches(); 1158 1159 if (query) { 1160 struct transport *transport; 1161 const struct ref *remote_refs; 1162 1163 transport = transport_get(states->remote, states->remote->url.v[0]); 1164 remote_refs = transport_get_remote_refs(transport, NULL); 1165 1166 states->queried = 1; 1167 if (query & GET_REF_STATES) 1168 get_ref_states(remote_refs, states); 1169 if (query & GET_HEAD_NAMES) 1170 get_head_names(remote_refs, states); 1171 if (query & GET_PUSH_REF_STATES) 1172 get_push_ref_states(remote_refs, states); 1173 transport_disconnect(transport); 1174 } else { 1175 refs_for_each_ref(get_main_ref_store(the_repository), 1176 append_ref_to_tracked_list, states); 1177 string_list_sort(&states->tracked); 1178 get_push_ref_states_noquery(states); 1179 } 1180 1181 return 0; 1182} 1183 1184struct show_info { 1185 struct string_list list; 1186 struct ref_states states; 1187 int width, width2; 1188 int any_rebase; 1189}; 1190 1191#define SHOW_INFO_INIT { \ 1192 .list = STRING_LIST_INIT_DUP, \ 1193 .states = REF_STATES_INIT, \ 1194} 1195 1196static int add_remote_to_show_info(struct string_list_item *item, void *cb_data) 1197{ 1198 struct show_info *info = cb_data; 1199 int n = strlen(item->string); 1200 if (n > info->width) 1201 info->width = n; 1202 string_list_insert(&info->list, item->string); 1203 return 0; 1204} 1205 1206static int show_remote_info_item(struct string_list_item *item, void *cb_data) 1207{ 1208 struct show_info *info = cb_data; 1209 struct ref_states *states = &info->states; 1210 const char *name = item->string; 1211 1212 if (states->queried) { 1213 const char *fmt = "%s"; 1214 const char *arg = ""; 1215 if (string_list_has_string(&states->new_refs, name)) { 1216 fmt = _(" new (next fetch will store in remotes/%s)"); 1217 arg = states->remote->name; 1218 } else if (string_list_has_string(&states->tracked, name)) 1219 arg = _(" tracked"); 1220 else if (string_list_has_string(&states->skipped, name)) 1221 arg = _(" skipped"); 1222 else if (string_list_has_string(&states->stale, name)) 1223 arg = _(" stale (use 'git remote prune' to remove)"); 1224 else 1225 arg = _(" ???"); 1226 printf(" %-*s", info->width, name); 1227 printf(fmt, arg); 1228 printf("\n"); 1229 } else 1230 printf(" %s\n", name); 1231 1232 return 0; 1233} 1234 1235static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data) 1236{ 1237 struct show_info *show_info = cb_data; 1238 struct ref_states *states = &show_info->states; 1239 struct branch_info *branch_info = branch_item->util; 1240 struct string_list_item *item; 1241 int n; 1242 1243 if (!branch_info->merge.nr || !branch_info->remote_name || 1244 strcmp(states->remote->name, branch_info->remote_name)) 1245 return 0; 1246 if ((n = strlen(branch_item->string)) > show_info->width) 1247 show_info->width = n; 1248 if (branch_info->rebase >= REBASE_TRUE) 1249 show_info->any_rebase = 1; 1250 1251 item = string_list_insert(&show_info->list, branch_item->string); 1252 item->util = branch_info; 1253 1254 return 0; 1255} 1256 1257static int show_local_info_item(struct string_list_item *item, void *cb_data) 1258{ 1259 struct show_info *show_info = cb_data; 1260 struct branch_info *branch_info = item->util; 1261 struct string_list *merge = &branch_info->merge; 1262 int width = show_info->width + 4; 1263 1264 if (branch_info->rebase >= REBASE_TRUE && branch_info->merge.nr > 1) { 1265 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"), 1266 item->string); 1267 return 0; 1268 } 1269 1270 printf(" %-*s ", show_info->width, item->string); 1271 if (branch_info->rebase >= REBASE_TRUE) { 1272 const char *msg; 1273 if (branch_info->rebase == REBASE_INTERACTIVE) 1274 msg = _("rebases interactively onto remote %s"); 1275 else if (branch_info->rebase == REBASE_MERGES) 1276 msg = _("rebases interactively (with merges) onto " 1277 "remote %s"); 1278 else 1279 msg = _("rebases onto remote %s"); 1280 printf_ln(msg, merge->items[0].string); 1281 return 0; 1282 } else if (show_info->any_rebase) { 1283 printf_ln(_(" merges with remote %s"), merge->items[0].string); 1284 width++; 1285 } else { 1286 printf_ln(_("merges with remote %s"), merge->items[0].string); 1287 } 1288 for (size_t i = 1; i < merge->nr; i++) 1289 printf(_("%-*s and with remote %s\n"), width, "", 1290 merge->items[i].string); 1291 1292 return 0; 1293} 1294 1295static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data) 1296{ 1297 struct show_info *show_info = cb_data; 1298 struct push_info *push_info = push_item->util; 1299 struct string_list_item *item; 1300 int n; 1301 if ((n = strlen(push_item->string)) > show_info->width) 1302 show_info->width = n; 1303 if ((n = strlen(push_info->dest)) > show_info->width2) 1304 show_info->width2 = n; 1305 item = string_list_append(&show_info->list, push_item->string); 1306 item->util = push_item->util; 1307 return 0; 1308} 1309 1310/* 1311 * Sorting comparison for a string list that has push_info 1312 * structs in its util field 1313 */ 1314static int cmp_string_with_push(const void *va, const void *vb) 1315{ 1316 const struct string_list_item *a = va; 1317 const struct string_list_item *b = vb; 1318 const struct push_info *a_push = a->util; 1319 const struct push_info *b_push = b->util; 1320 int cmp = strcmp(a->string, b->string); 1321 return cmp ? cmp : strcmp(a_push->dest, b_push->dest); 1322} 1323 1324static int show_push_info_item(struct string_list_item *item, void *cb_data) 1325{ 1326 struct show_info *show_info = cb_data; 1327 struct push_info *push_info = item->util; 1328 const char *src = item->string, *status = NULL; 1329 1330 switch (push_info->status) { 1331 case PUSH_STATUS_CREATE: 1332 status = _("create"); 1333 break; 1334 case PUSH_STATUS_DELETE: 1335 status = _("delete"); 1336 src = _("(none)"); 1337 break; 1338 case PUSH_STATUS_UPTODATE: 1339 status = _("up to date"); 1340 break; 1341 case PUSH_STATUS_FASTFORWARD: 1342 status = _("fast-forwardable"); 1343 break; 1344 case PUSH_STATUS_OUTOFDATE: 1345 status = _("local out of date"); 1346 break; 1347 case PUSH_STATUS_NOTQUERIED: 1348 break; 1349 } 1350 if (status) { 1351 if (push_info->forced) 1352 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src, 1353 show_info->width2, push_info->dest, status); 1354 else 1355 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src, 1356 show_info->width2, push_info->dest, status); 1357 } else { 1358 if (push_info->forced) 1359 printf_ln(_(" %-*s forces to %s"), show_info->width, src, 1360 push_info->dest); 1361 else 1362 printf_ln(_(" %-*s pushes to %s"), show_info->width, src, 1363 push_info->dest); 1364 } 1365 return 0; 1366} 1367 1368static int get_one_entry(struct remote *remote, void *priv) 1369{ 1370 struct string_list *list = priv; 1371 struct strbuf remote_info_buf = STRBUF_INIT; 1372 struct strvec *url; 1373 1374 if (remote->url.nr > 0) { 1375 struct strbuf promisor_config = STRBUF_INIT; 1376 const char *partial_clone_filter = NULL; 1377 1378 strbuf_addf(&promisor_config, "remote.%s.partialclonefilter", remote->name); 1379 strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url.v[0]); 1380 if (!repo_config_get_string_tmp(the_repository, promisor_config.buf, &partial_clone_filter)) 1381 strbuf_addf(&remote_info_buf, " [%s]", partial_clone_filter); 1382 1383 strbuf_release(&promisor_config); 1384 string_list_append(list, remote->name)->util = 1385 strbuf_detach(&remote_info_buf, NULL); 1386 } else 1387 string_list_append(list, remote->name)->util = NULL; 1388 url = push_url_of_remote(remote); 1389 for (size_t i = 0; i < url->nr; i++) { 1390 strbuf_addf(&remote_info_buf, "%s (push)", url->v[i]); 1391 string_list_append(list, remote->name)->util = 1392 strbuf_detach(&remote_info_buf, NULL); 1393 } 1394 1395 return 0; 1396} 1397 1398static int show_all(void) 1399{ 1400 struct string_list list = STRING_LIST_INIT_DUP; 1401 int result; 1402 1403 result = for_each_remote(get_one_entry, &list); 1404 1405 if (!result) { 1406 string_list_sort(&list); 1407 for (size_t i = 0; i < list.nr; i++) { 1408 struct string_list_item *item = list.items + i; 1409 if (verbose) 1410 printf("%s\t%s\n", item->string, 1411 item->util ? (const char *)item->util : ""); 1412 else { 1413 if (i && !strcmp((item - 1)->string, item->string)) 1414 continue; 1415 printf("%s\n", item->string); 1416 } 1417 } 1418 } 1419 string_list_clear(&list, 1); 1420 return result; 1421} 1422 1423static int show(int argc, const char **argv, const char *prefix, 1424 struct repository *repo UNUSED) 1425{ 1426 int no_query = 0, result = 0, query_flag = 0; 1427 struct option options[] = { 1428 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")), 1429 OPT_END() 1430 }; 1431 struct show_info info = SHOW_INFO_INIT; 1432 1433 argc = parse_options(argc, argv, prefix, options, 1434 builtin_remote_show_usage, 1435 0); 1436 1437 if (argc < 1) 1438 return show_all(); 1439 1440 if (!no_query) 1441 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES); 1442 1443 for (; argc; argc--, argv++) { 1444 size_t i; 1445 struct strvec *url; 1446 1447 get_remote_ref_states(*argv, &info.states, query_flag); 1448 1449 printf_ln(_("* remote %s"), *argv); 1450 printf_ln(_(" Fetch URL: %s"), info.states.remote->url.v[0]); 1451 url = push_url_of_remote(info.states.remote); 1452 for (i = 0; i < url->nr; i++) 1453 /* 1454 * TRANSLATORS: the colon ':' should align 1455 * with the one in " Fetch URL: %s" 1456 * translation. 1457 */ 1458 printf_ln(_(" Push URL: %s"), url->v[i]); 1459 if (!i) 1460 printf_ln(_(" Push URL: %s"), _("(no URL)")); 1461 if (no_query) 1462 printf_ln(_(" HEAD branch: %s"), _("(not queried)")); 1463 else if (!info.states.heads.nr) 1464 printf_ln(_(" HEAD branch: %s"), _("(unknown)")); 1465 else if (info.states.heads.nr == 1) 1466 printf_ln(_(" HEAD branch: %s"), info.states.heads.items[0].string); 1467 else { 1468 printf(_(" HEAD branch (remote HEAD is ambiguous," 1469 " may be one of the following):\n")); 1470 for (i = 0; i < info.states.heads.nr; i++) 1471 printf(" %s\n", info.states.heads.items[i].string); 1472 } 1473 1474 /* remote branch info */ 1475 info.width = 0; 1476 for_each_string_list(&info.states.new_refs, add_remote_to_show_info, &info); 1477 for_each_string_list(&info.states.skipped, add_remote_to_show_info, &info); 1478 for_each_string_list(&info.states.tracked, add_remote_to_show_info, &info); 1479 for_each_string_list(&info.states.stale, add_remote_to_show_info, &info); 1480 if (info.list.nr) 1481 printf_ln(Q_(" Remote branch:%s", 1482 " Remote branches:%s", 1483 info.list.nr), 1484 no_query ? _(" (status not queried)") : ""); 1485 for_each_string_list(&info.list, show_remote_info_item, &info); 1486 string_list_clear(&info.list, 0); 1487 1488 /* git pull info */ 1489 info.width = 0; 1490 info.any_rebase = 0; 1491 for_each_string_list(&branch_list, add_local_to_show_info, &info); 1492 if (info.list.nr) 1493 printf_ln(Q_(" Local branch configured for 'git pull':", 1494 " Local branches configured for 'git pull':", 1495 info.list.nr)); 1496 for_each_string_list(&info.list, show_local_info_item, &info); 1497 string_list_clear(&info.list, 0); 1498 1499 /* git push info */ 1500 if (info.states.remote->mirror) 1501 printf_ln(_(" Local refs will be mirrored by 'git push'")); 1502 1503 info.width = info.width2 = 0; 1504 for_each_string_list(&info.states.push, add_push_to_show_info, &info); 1505 QSORT(info.list.items, info.list.nr, cmp_string_with_push); 1506 if (info.list.nr) 1507 printf_ln(Q_(" Local ref configured for 'git push'%s:", 1508 " Local refs configured for 'git push'%s:", 1509 info.list.nr), 1510 no_query ? _(" (status not queried)") : ""); 1511 for_each_string_list(&info.list, show_push_info_item, &info); 1512 string_list_clear(&info.list, 0); 1513 1514 free_remote_ref_states(&info.states); 1515 } 1516 1517 return result; 1518} 1519 1520static void report_set_head_auto(const char *remote, const char *head_name, 1521 struct strbuf *b_local_head, int was_detached) { 1522 struct strbuf buf_prefix = STRBUF_INIT; 1523 const char *prev_head = NULL; 1524 1525 strbuf_addf(&buf_prefix, "refs/remotes/%s/", remote); 1526 skip_prefix(b_local_head->buf, buf_prefix.buf, &prev_head); 1527 1528 if (prev_head && !strcmp(prev_head, head_name)) 1529 printf(_("'%s/HEAD' is unchanged and points to '%s'\n"), 1530 remote, head_name); 1531 else if (prev_head) 1532 printf(_("'%s/HEAD' has changed from '%s' and now points to '%s'\n"), 1533 remote, prev_head, head_name); 1534 else if (!b_local_head->len) 1535 printf(_("'%s/HEAD' is now created and points to '%s'\n"), 1536 remote, head_name); 1537 else if (was_detached && b_local_head->len) 1538 printf(_("'%s/HEAD' was detached at '%s' and now points to '%s'\n"), 1539 remote, b_local_head->buf, head_name); 1540 else 1541 printf(_("'%s/HEAD' used to point to '%s' " 1542 "(which is not a remote branch), but now points to '%s'\n"), 1543 remote, b_local_head->buf, head_name); 1544 strbuf_release(&buf_prefix); 1545} 1546 1547static int set_head(int argc, const char **argv, const char *prefix, 1548 struct repository *repo UNUSED) 1549{ 1550 int opt_a = 0, opt_d = 0, result = 0, was_detached; 1551 struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT, 1552 b_local_head = STRBUF_INIT; 1553 char *head_name = NULL; 1554 struct ref_store *refs = get_main_ref_store(the_repository); 1555 struct remote *remote; 1556 1557 struct option options[] = { 1558 OPT_BOOL('a', "auto", &opt_a, 1559 N_("set refs/remotes/<name>/HEAD according to remote")), 1560 OPT_BOOL('d', "delete", &opt_d, 1561 N_("delete refs/remotes/<name>/HEAD")), 1562 OPT_END() 1563 }; 1564 argc = parse_options(argc, argv, prefix, options, 1565 builtin_remote_sethead_usage, 0); 1566 1567 /* All modes require at least a remote name. */ 1568 if (!argc) 1569 usage_with_options(builtin_remote_sethead_usage, options); 1570 1571 strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]); 1572 remote = remote_get(argv[0]); 1573 1574 if (!opt_a && !opt_d && argc == 2) { 1575 head_name = xstrdup(argv[1]); 1576 } else if (opt_a && !opt_d && argc == 1) { 1577 struct ref_states states = REF_STATES_INIT; 1578 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES); 1579 if (!states.heads.nr) 1580 result |= error(_("Cannot determine remote HEAD")); 1581 else if (states.heads.nr > 1) { 1582 result |= error(_("Multiple remote HEAD branches. " 1583 "Please choose one explicitly with:")); 1584 for (size_t i = 0; i < states.heads.nr; i++) 1585 fprintf(stderr, " git remote set-head %s %s\n", 1586 argv[0], states.heads.items[i].string); 1587 } else 1588 head_name = xstrdup(states.heads.items[0].string); 1589 free_remote_ref_states(&states); 1590 } else if (opt_d && !opt_a && argc == 1) { 1591 if (refs_delete_ref(refs, NULL, b_head.buf, NULL, REF_NO_DEREF)) 1592 result |= error(_("Could not delete %s"), b_head.buf); 1593 } else 1594 usage_with_options(builtin_remote_sethead_usage, options); 1595 1596 if (!head_name) 1597 goto cleanup; 1598 strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name); 1599 if (!refs_ref_exists(refs, b_remote_head.buf)) { 1600 result |= error(_("Not a valid ref: %s"), b_remote_head.buf); 1601 goto cleanup; 1602 } 1603 was_detached = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf, 1604 "remote set-head", &b_local_head, 0); 1605 if (was_detached == -1) { 1606 result |= error(_("Could not set up %s"), b_head.buf); 1607 goto cleanup; 1608 } 1609 if (opt_a) 1610 report_set_head_auto(argv[0], head_name, &b_local_head, was_detached); 1611 if (remote->follow_remote_head == FOLLOW_REMOTE_ALWAYS) { 1612 struct strbuf config_name = STRBUF_INIT; 1613 strbuf_addf(&config_name, 1614 "remote.%s.followremotehead", remote->name); 1615 repo_config_set(the_repository, config_name.buf, "warn"); 1616 strbuf_release(&config_name); 1617 } 1618 1619cleanup: 1620 free(head_name); 1621 strbuf_release(&b_head); 1622 strbuf_release(&b_remote_head); 1623 strbuf_release(&b_local_head); 1624 return result; 1625} 1626 1627static int prune_remote(const char *remote, int dry_run) 1628{ 1629 int result = 0; 1630 struct ref_states states = REF_STATES_INIT; 1631 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP; 1632 struct string_list_item *item; 1633 1634 get_remote_ref_states(remote, &states, GET_REF_STATES); 1635 1636 if (!states.stale.nr) { 1637 free_remote_ref_states(&states); 1638 return 0; 1639 } 1640 1641 printf_ln(_("Pruning %s"), remote); 1642 printf_ln(_("URL: %s"), states.remote->url.v[0]); 1643 1644 for_each_string_list_item(item, &states.stale) 1645 string_list_append(&refs_to_prune, item->util); 1646 string_list_sort(&refs_to_prune); 1647 1648 if (!dry_run) 1649 result |= refs_delete_refs(get_main_ref_store(the_repository), 1650 "remote: prune", &refs_to_prune, 0); 1651 1652 for_each_string_list_item(item, &states.stale) { 1653 const char *refname = item->util; 1654 1655 if (dry_run) 1656 printf_ln(_(" * [would prune] %s"), 1657 abbrev_ref(refname, "refs/remotes/")); 1658 else 1659 printf_ln(_(" * [pruned] %s"), 1660 abbrev_ref(refname, "refs/remotes/")); 1661 } 1662 1663 refs_warn_dangling_symrefs(get_main_ref_store(the_repository), 1664 stdout, " ", dry_run, &refs_to_prune); 1665 1666 string_list_clear(&refs_to_prune, 0); 1667 free_remote_ref_states(&states); 1668 return result; 1669} 1670 1671static int prune(int argc, const char **argv, const char *prefix, 1672 struct repository *repo UNUSED) 1673{ 1674 int dry_run = 0, result = 0; 1675 struct option options[] = { 1676 OPT__DRY_RUN(&dry_run, N_("dry run")), 1677 OPT_END() 1678 }; 1679 1680 argc = parse_options(argc, argv, prefix, options, 1681 builtin_remote_prune_usage, 0); 1682 1683 if (argc < 1) 1684 usage_with_options(builtin_remote_prune_usage, options); 1685 1686 for (; argc; argc--, argv++) 1687 result |= prune_remote(*argv, dry_run); 1688 1689 return result; 1690} 1691 1692static int get_remote_default(const char *key, const char *value UNUSED, 1693 const struct config_context *ctx UNUSED, 1694 void *priv) 1695{ 1696 if (strcmp(key, "remotes.default") == 0) { 1697 int *found = priv; 1698 *found = 1; 1699 } 1700 return 0; 1701} 1702 1703static int update(int argc, const char **argv, const char *prefix, 1704 struct repository *repo UNUSED) 1705{ 1706 int i, prune = -1; 1707 struct option options[] = { 1708 OPT_BOOL('p', "prune", &prune, 1709 N_("prune remotes after fetching")), 1710 OPT_END() 1711 }; 1712 struct child_process cmd = CHILD_PROCESS_INIT; 1713 int default_defined = 0; 1714 1715 argc = parse_options(argc, argv, prefix, options, 1716 builtin_remote_update_usage, 1717 PARSE_OPT_KEEP_ARGV0); 1718 1719 strvec_push(&cmd.args, "fetch"); 1720 1721 if (prune != -1) 1722 strvec_push(&cmd.args, prune ? "--prune" : "--no-prune"); 1723 if (verbose) 1724 strvec_push(&cmd.args, "-v"); 1725 strvec_push(&cmd.args, "--multiple"); 1726 if (argc < 2) 1727 strvec_push(&cmd.args, "default"); 1728 for (i = 1; i < argc; i++) 1729 strvec_push(&cmd.args, argv[i]); 1730 1731 if (strcmp(cmd.args.v[cmd.args.nr-1], "default") == 0) { 1732 repo_config(the_repository, get_remote_default, &default_defined); 1733 if (!default_defined) { 1734 strvec_pop(&cmd.args); 1735 strvec_push(&cmd.args, "--all"); 1736 } 1737 } 1738 1739 cmd.git_cmd = 1; 1740 return run_command(&cmd); 1741} 1742 1743static int remove_all_fetch_refspecs(const char *key) 1744{ 1745 return repo_config_set_multivar_gently(the_repository, key, NULL, NULL, 1746 CONFIG_FLAGS_MULTI_REPLACE); 1747} 1748 1749static void add_branches(struct remote *remote, const char **branches, 1750 const char *key) 1751{ 1752 const char *remotename = remote->name; 1753 int mirror = remote->mirror; 1754 struct strbuf refspec = STRBUF_INIT; 1755 1756 for (; *branches; branches++) 1757 add_branch(key, *branches, remotename, mirror, &refspec); 1758 1759 strbuf_release(&refspec); 1760} 1761 1762static int set_remote_branches(const char *remotename, const char **branches, 1763 int add_mode) 1764{ 1765 struct strbuf key = STRBUF_INIT; 1766 struct remote *remote; 1767 1768 strbuf_addf(&key, "remote.%s.fetch", remotename); 1769 1770 remote = remote_get(remotename); 1771 if (!remote_is_configured(remote, 1)) { 1772 error(_("No such remote '%s'"), remotename); 1773 exit(2); 1774 } 1775 1776 if (!add_mode && remove_all_fetch_refspecs(key.buf)) { 1777 strbuf_release(&key); 1778 return 1; 1779 } 1780 add_branches(remote, branches, key.buf); 1781 1782 strbuf_release(&key); 1783 return 0; 1784} 1785 1786static int set_branches(int argc, const char **argv, const char *prefix, 1787 struct repository *repo UNUSED) 1788{ 1789 int add_mode = 0; 1790 struct option options[] = { 1791 OPT_BOOL('\0', "add", &add_mode, N_("add branch")), 1792 OPT_END() 1793 }; 1794 1795 argc = parse_options(argc, argv, prefix, options, 1796 builtin_remote_setbranches_usage, 0); 1797 if (argc == 0) { 1798 error(_("no remote specified")); 1799 usage_with_options(builtin_remote_setbranches_usage, options); 1800 } 1801 argv[argc] = NULL; 1802 1803 return set_remote_branches(argv[0], argv + 1, add_mode); 1804} 1805 1806static int get_url(int argc, const char **argv, const char *prefix, 1807 struct repository *repo UNUSED) 1808{ 1809 int push_mode = 0, all_mode = 0; 1810 const char *remotename = NULL; 1811 struct remote *remote; 1812 struct strvec *url; 1813 struct option options[] = { 1814 OPT_BOOL('\0', "push", &push_mode, 1815 N_("query push URLs rather than fetch URLs")), 1816 OPT_BOOL('\0', "all", &all_mode, 1817 N_("return all URLs")), 1818 OPT_END() 1819 }; 1820 argc = parse_options(argc, argv, prefix, options, 1821 builtin_remote_geturl_usage, 0); 1822 1823 if (argc != 1) 1824 usage_with_options(builtin_remote_geturl_usage, options); 1825 1826 remotename = argv[0]; 1827 1828 remote = remote_get(remotename); 1829 if (!remote_is_configured(remote, 1)) { 1830 error(_("No such remote '%s'"), remotename); 1831 exit(2); 1832 } 1833 1834 url = push_mode ? push_url_of_remote(remote) : &remote->url; 1835 1836 if (all_mode) { 1837 for (size_t i = 0; i < url->nr; i++) 1838 printf_ln("%s", url->v[i]); 1839 } else { 1840 printf_ln("%s", url->v[0]); 1841 } 1842 1843 return 0; 1844} 1845 1846static int set_url(int argc, const char **argv, const char *prefix, 1847 struct repository *repo UNUSED) 1848{ 1849 int push_mode = 0, add_mode = 0, delete_mode = 0; 1850 int matches = 0, negative_matches = 0; 1851 const char *remotename = NULL; 1852 const char *newurl = NULL; 1853 const char *oldurl = NULL; 1854 struct remote *remote; 1855 regex_t old_regex; 1856 struct strvec *urlset; 1857 struct strbuf name_buf = STRBUF_INIT; 1858 struct option options[] = { 1859 OPT_BOOL('\0', "push", &push_mode, 1860 N_("manipulate push URLs")), 1861 OPT_BOOL('\0', "add", &add_mode, 1862 N_("add URL")), 1863 OPT_BOOL('\0', "delete", &delete_mode, 1864 N_("delete URLs")), 1865 OPT_END() 1866 }; 1867 argc = parse_options(argc, argv, prefix, options, 1868 builtin_remote_seturl_usage, 1869 PARSE_OPT_KEEP_ARGV0); 1870 1871 if (add_mode && delete_mode) 1872 die(_("--add --delete doesn't make sense")); 1873 1874 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3)) 1875 usage_with_options(builtin_remote_seturl_usage, options); 1876 1877 remotename = argv[1]; 1878 newurl = argv[2]; 1879 if (argc > 3) 1880 oldurl = argv[3]; 1881 1882 if (delete_mode) 1883 oldurl = newurl; 1884 1885 remote = remote_get(remotename); 1886 if (!remote_is_configured(remote, 1)) { 1887 error(_("No such remote '%s'"), remotename); 1888 exit(2); 1889 } 1890 1891 if (push_mode) { 1892 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename); 1893 urlset = &remote->pushurl; 1894 } else { 1895 strbuf_addf(&name_buf, "remote.%s.url", remotename); 1896 urlset = &remote->url; 1897 } 1898 1899 /* Special cases that add new entry. */ 1900 if ((!oldurl && !delete_mode) || add_mode) { 1901 if (add_mode) 1902 repo_config_set_multivar(the_repository, name_buf.buf, newurl, 1903 "^$", 0); 1904 else 1905 repo_config_set(the_repository, name_buf.buf, newurl); 1906 goto out; 1907 } 1908 1909 /* Old URL specified. Demand that one matches. */ 1910 if (regcomp(&old_regex, oldurl, REG_EXTENDED)) 1911 die(_("Invalid old URL pattern: %s"), oldurl); 1912 1913 for (size_t i = 0; i < urlset->nr; i++) 1914 if (!regexec(&old_regex, urlset->v[i], 0, NULL, 0)) 1915 matches++; 1916 else 1917 negative_matches++; 1918 if (!delete_mode && !matches) 1919 die(_("No such URL found: %s"), oldurl); 1920 if (delete_mode && !negative_matches && !push_mode) 1921 die(_("Will not delete all non-push URLs")); 1922 1923 regfree(&old_regex); 1924 1925 if (!delete_mode) 1926 repo_config_set_multivar(the_repository, name_buf.buf, newurl, oldurl, 0); 1927 else 1928 repo_config_set_multivar(the_repository, name_buf.buf, NULL, oldurl, 1929 CONFIG_FLAGS_MULTI_REPLACE); 1930out: 1931 strbuf_release(&name_buf); 1932 return 0; 1933} 1934 1935int cmd_remote(int argc, 1936 const char **argv, 1937 const char *prefix, 1938 struct repository *repo) 1939{ 1940 parse_opt_subcommand_fn *fn = NULL; 1941 struct option options[] = { 1942 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")), 1943 OPT_SUBCOMMAND("add", &fn, add), 1944 OPT_SUBCOMMAND("rename", &fn, mv), 1945 OPT_SUBCOMMAND_F("rm", &fn, rm, PARSE_OPT_NOCOMPLETE), 1946 OPT_SUBCOMMAND("remove", &fn, rm), 1947 OPT_SUBCOMMAND("set-head", &fn, set_head), 1948 OPT_SUBCOMMAND("set-branches", &fn, set_branches), 1949 OPT_SUBCOMMAND("get-url", &fn, get_url), 1950 OPT_SUBCOMMAND("set-url", &fn, set_url), 1951 OPT_SUBCOMMAND("show", &fn, show), 1952 OPT_SUBCOMMAND("prune", &fn, prune), 1953 OPT_SUBCOMMAND("update", &fn, update), 1954 OPT_END() 1955 }; 1956 1957 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage, 1958 PARSE_OPT_SUBCOMMAND_OPTIONAL); 1959 1960 if (fn) { 1961 return !!fn(argc, argv, prefix, repo); 1962 } else { 1963 if (argc) { 1964 error(_("unknown subcommand: `%s'"), argv[0]); 1965 usage_with_options(builtin_remote_usage, options); 1966 } 1967 return !!show_all(); 1968 } 1969}