builtin / remote.con commit Merge branch 'jt/transfer-fsck-with-promissor' into next (6d1ccc9)
   1#include "builtin.h"
   2#include "config.h"
   3#include "parse-options.h"
   4#include "transport.h"
   5#include "remote.h"
   6#include "string-list.h"
   7#include "strbuf.h"
   8#include "run-command.h"
   9#include "refs.h"
  10#include "argv-array.h"
  11
  12static const char * const builtin_remote_usage[] = {
  13        N_("git remote [-v | --verbose]"),
  14        N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"),
  15        N_("git remote rename <old> <new>"),
  16        N_("git remote remove <name>"),
  17        N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
  18        N_("git remote [-v | --verbose] show [-n] <name>"),
  19        N_("git remote prune [-n | --dry-run] <name>"),
  20        N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
  21        N_("git remote set-branches [--add] <name> <branch>..."),
  22        N_("git remote get-url [--push] [--all] <name>"),
  23        N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
  24        N_("git remote set-url --add <name> <newurl>"),
  25        N_("git remote set-url --delete <name> <url>"),
  26        NULL
  27};
  28
  29static const char * const builtin_remote_add_usage[] = {
  30        N_("git remote add [<options>] <name> <url>"),
  31        NULL
  32};
  33
  34static const char * const builtin_remote_rename_usage[] = {
  35        N_("git remote rename <old> <new>"),
  36        NULL
  37};
  38
  39static const char * const builtin_remote_rm_usage[] = {
  40        N_("git remote remove <name>"),
  41        NULL
  42};
  43
  44static const char * const builtin_remote_sethead_usage[] = {
  45        N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
  46        NULL
  47};
  48
  49static const char * const builtin_remote_setbranches_usage[] = {
  50        N_("git remote set-branches <name> <branch>..."),
  51        N_("git remote set-branches --add <name> <branch>..."),
  52        NULL
  53};
  54
  55static const char * const builtin_remote_show_usage[] = {
  56        N_("git remote show [<options>] <name>"),
  57        NULL
  58};
  59
  60static const char * const builtin_remote_prune_usage[] = {
  61        N_("git remote prune [<options>] <name>"),
  62        NULL
  63};
  64
  65static const char * const builtin_remote_update_usage[] = {
  66        N_("git remote update [<options>] [<group> | <remote>]..."),
  67        NULL
  68};
  69
  70static const char * const builtin_remote_geturl_usage[] = {
  71        N_("git remote get-url [--push] [--all] <name>"),
  72        NULL
  73};
  74
  75static const char * const builtin_remote_seturl_usage[] = {
  76        N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
  77        N_("git remote set-url --add <name> <newurl>"),
  78        N_("git remote set-url --delete <name> <url>"),
  79        NULL
  80};
  81
  82#define GET_REF_STATES (1<<0)
  83#define GET_HEAD_NAMES (1<<1)
  84#define GET_PUSH_REF_STATES (1<<2)
  85
  86static int verbose;
  87
  88static int fetch_remote(const char *name)
  89{
  90        const char *argv[] = { "fetch", name, NULL, NULL };
  91        if (verbose) {
  92                argv[1] = "-v";
  93                argv[2] = name;
  94        }
  95        printf_ln(_("Updating %s"), name);
  96        if (run_command_v_opt(argv, RUN_GIT_CMD))
  97                return error(_("Could not fetch %s"), name);
  98        return 0;
  99}
 100
 101enum {
 102        TAGS_UNSET = 0,
 103        TAGS_DEFAULT = 1,
 104        TAGS_SET = 2
 105};
 106
 107#define MIRROR_NONE 0
 108#define MIRROR_FETCH 1
 109#define MIRROR_PUSH 2
 110#define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
 111
 112static void add_branch(const char *key, const char *branchname,
 113                       const char *remotename, int mirror, struct strbuf *tmp)
 114{
 115        strbuf_reset(tmp);
 116        strbuf_addch(tmp, '+');
 117        if (mirror)
 118                strbuf_addf(tmp, "refs/%s:refs/%s",
 119                                branchname, branchname);
 120        else
 121                strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s",
 122                                branchname, remotename, branchname);
 123        git_config_set_multivar(key, tmp->buf, "^$", 0);
 124}
 125
 126static const char mirror_advice[] =
 127N_("--mirror is dangerous and deprecated; please\n"
 128   "\t use --mirror=fetch or --mirror=push instead");
 129
 130static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
 131{
 132        unsigned *mirror = opt->value;
 133        if (not)
 134                *mirror = MIRROR_NONE;
 135        else if (!arg) {
 136                warning("%s", _(mirror_advice));
 137                *mirror = MIRROR_BOTH;
 138        }
 139        else if (!strcmp(arg, "fetch"))
 140                *mirror = MIRROR_FETCH;
 141        else if (!strcmp(arg, "push"))
 142                *mirror = MIRROR_PUSH;
 143        else
 144                return error(_("unknown mirror argument: %s"), arg);
 145        return 0;
 146}
 147
 148static int add(int argc, const char **argv)
 149{
 150        int fetch = 0, fetch_tags = TAGS_DEFAULT;
 151        unsigned mirror = MIRROR_NONE;
 152        struct string_list track = STRING_LIST_INIT_NODUP;
 153        const char *master = NULL;
 154        struct remote *remote;
 155        struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
 156        const char *name, *url;
 157        int i;
 158
 159        struct option options[] = {
 160                OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")),
 161                OPT_SET_INT(0, "tags", &fetch_tags,
 162                            N_("import all tags and associated objects when fetching"),
 163                            TAGS_SET),
 164                OPT_SET_INT(0, NULL, &fetch_tags,
 165                            N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET),
 166                OPT_STRING_LIST('t', "track", &track, N_("branch"),
 167                                N_("branch(es) to track")),
 168                OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")),
 169                { OPTION_CALLBACK, 0, "mirror", &mirror, N_("push|fetch"),
 170                        N_("set up remote as a mirror to push to or fetch from"),
 171                        PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt },
 172                OPT_END()
 173        };
 174
 175        argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage,
 176                             0);
 177
 178        if (argc != 2)
 179                usage_with_options(builtin_remote_add_usage, options);
 180
 181        if (mirror && master)
 182                die(_("specifying a master branch makes no sense with --mirror"));
 183        if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
 184                die(_("specifying branches to track makes sense only with fetch mirrors"));
 185
 186        name = argv[0];
 187        url = argv[1];
 188
 189        remote = remote_get(name);
 190        if (remote_is_configured(remote, 1))
 191                die(_("remote %s already exists."), name);
 192
 193        strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
 194        if (!valid_fetch_refspec(buf2.buf))
 195                die(_("'%s' is not a valid remote name"), name);
 196
 197        strbuf_addf(&buf, "remote.%s.url", name);
 198        git_config_set(buf.buf, url);
 199
 200        if (!mirror || mirror & MIRROR_FETCH) {
 201                strbuf_reset(&buf);
 202                strbuf_addf(&buf, "remote.%s.fetch", name);
 203                if (track.nr == 0)
 204                        string_list_append(&track, "*");
 205                for (i = 0; i < track.nr; i++) {
 206                        add_branch(buf.buf, track.items[i].string,
 207                                   name, mirror, &buf2);
 208                }
 209        }
 210
 211        if (mirror & MIRROR_PUSH) {
 212                strbuf_reset(&buf);
 213                strbuf_addf(&buf, "remote.%s.mirror", name);
 214                git_config_set(buf.buf, "true");
 215        }
 216
 217        if (fetch_tags != TAGS_DEFAULT) {
 218                strbuf_reset(&buf);
 219                strbuf_addf(&buf, "remote.%s.tagopt", name);
 220                git_config_set(buf.buf,
 221                               fetch_tags == TAGS_SET ? "--tags" : "--no-tags");
 222        }
 223
 224        if (fetch && fetch_remote(name))
 225                return 1;
 226
 227        if (master) {
 228                strbuf_reset(&buf);
 229                strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
 230
 231                strbuf_reset(&buf2);
 232                strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
 233
 234                if (create_symref(buf.buf, buf2.buf, "remote add"))
 235                        return error(_("Could not setup master '%s'"), master);
 236        }
 237
 238        strbuf_release(&buf);
 239        strbuf_release(&buf2);
 240        string_list_clear(&track, 0);
 241
 242        return 0;
 243}
 244
 245struct branch_info {
 246        char *remote_name;
 247        struct string_list merge;
 248        enum { NO_REBASE, NORMAL_REBASE, INTERACTIVE_REBASE } rebase;
 249};
 250
 251static struct string_list branch_list = STRING_LIST_INIT_NODUP;
 252
 253static const char *abbrev_ref(const char *name, const char *prefix)
 254{
 255        skip_prefix(name, prefix, &name);
 256        return name;
 257}
 258#define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
 259
 260static int config_read_branches(const char *key, const char *value, void *cb)
 261{
 262        if (starts_with(key, "branch.")) {
 263                const char *orig_key = key;
 264                char *name;
 265                struct string_list_item *item;
 266                struct branch_info *info;
 267                enum { REMOTE, MERGE, REBASE } type;
 268                size_t key_len;
 269
 270                key += 7;
 271                if (strip_suffix(key, ".remote", &key_len)) {
 272                        name = xmemdupz(key, key_len);
 273                        type = REMOTE;
 274                } else if (strip_suffix(key, ".merge", &key_len)) {
 275                        name = xmemdupz(key, key_len);
 276                        type = MERGE;
 277                } else if (strip_suffix(key, ".rebase", &key_len)) {
 278                        name = xmemdupz(key, key_len);
 279                        type = REBASE;
 280                } else
 281                        return 0;
 282
 283                item = string_list_insert(&branch_list, name);
 284
 285                if (!item->util)
 286                        item->util = xcalloc(1, sizeof(struct branch_info));
 287                info = item->util;
 288                if (type == REMOTE) {
 289                        if (info->remote_name)
 290                                warning(_("more than one %s"), orig_key);
 291                        info->remote_name = xstrdup(value);
 292                } else if (type == MERGE) {
 293                        char *space = strchr(value, ' ');
 294                        value = abbrev_branch(value);
 295                        while (space) {
 296                                char *merge;
 297                                merge = xstrndup(value, space - value);
 298                                string_list_append(&info->merge, merge);
 299                                value = abbrev_branch(space + 1);
 300                                space = strchr(value, ' ');
 301                        }
 302                        string_list_append(&info->merge, xstrdup(value));
 303                } else {
 304                        int v = git_parse_maybe_bool(value);
 305                        if (v >= 0)
 306                                info->rebase = v;
 307                        else if (!strcmp(value, "preserve"))
 308                                info->rebase = NORMAL_REBASE;
 309                        else if (!strcmp(value, "recreate"))
 310                                info->rebase = NORMAL_REBASE;
 311                        else if (!strcmp(value, "interactive"))
 312                                info->rebase = INTERACTIVE_REBASE;
 313                }
 314        }
 315        return 0;
 316}
 317
 318static void read_branches(void)
 319{
 320        if (branch_list.nr)
 321                return;
 322        git_config(config_read_branches, NULL);
 323}
 324
 325struct ref_states {
 326        struct remote *remote;
 327        struct string_list new_refs, stale, tracked, heads, push;
 328        int queried;
 329};
 330
 331static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
 332{
 333        struct ref *fetch_map = NULL, **tail = &fetch_map;
 334        struct ref *ref, *stale_refs;
 335        int i;
 336
 337        for (i = 0; i < states->remote->fetch_refspec_nr; i++)
 338                if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
 339                        die(_("Could not get fetch map for refspec %s"),
 340                                states->remote->fetch_refspec[i]);
 341
 342        states->new_refs.strdup_strings = 1;
 343        states->tracked.strdup_strings = 1;
 344        states->stale.strdup_strings = 1;
 345        for (ref = fetch_map; ref; ref = ref->next) {
 346                if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
 347                        string_list_append(&states->new_refs, abbrev_branch(ref->name));
 348                else
 349                        string_list_append(&states->tracked, abbrev_branch(ref->name));
 350        }
 351        stale_refs = get_stale_heads(states->remote->fetch,
 352                                     states->remote->fetch_refspec_nr, fetch_map);
 353        for (ref = stale_refs; ref; ref = ref->next) {
 354                struct string_list_item *item =
 355                        string_list_append(&states->stale, abbrev_branch(ref->name));
 356                item->util = xstrdup(ref->name);
 357        }
 358        free_refs(stale_refs);
 359        free_refs(fetch_map);
 360
 361        string_list_sort(&states->new_refs);
 362        string_list_sort(&states->tracked);
 363        string_list_sort(&states->stale);
 364
 365        return 0;
 366}
 367
 368struct push_info {
 369        char *dest;
 370        int forced;
 371        enum {
 372                PUSH_STATUS_CREATE = 0,
 373                PUSH_STATUS_DELETE,
 374                PUSH_STATUS_UPTODATE,
 375                PUSH_STATUS_FASTFORWARD,
 376                PUSH_STATUS_OUTOFDATE,
 377                PUSH_STATUS_NOTQUERIED
 378        } status;
 379};
 380
 381static int get_push_ref_states(const struct ref *remote_refs,
 382        struct ref_states *states)
 383{
 384        struct remote *remote = states->remote;
 385        struct ref *ref, *local_refs, *push_map;
 386        if (remote->mirror)
 387                return 0;
 388
 389        local_refs = get_local_heads();
 390        push_map = copy_ref_list(remote_refs);
 391
 392        match_push_refs(local_refs, &push_map, remote->push_refspec_nr,
 393                        remote->push_refspec, MATCH_REFS_NONE);
 394
 395        states->push.strdup_strings = 1;
 396        for (ref = push_map; ref; ref = ref->next) {
 397                struct string_list_item *item;
 398                struct push_info *info;
 399
 400                if (!ref->peer_ref)
 401                        continue;
 402                oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
 403
 404                item = string_list_append(&states->push,
 405                                          abbrev_branch(ref->peer_ref->name));
 406                item->util = xcalloc(1, sizeof(struct push_info));
 407                info = item->util;
 408                info->forced = ref->force;
 409                info->dest = xstrdup(abbrev_branch(ref->name));
 410
 411                if (is_null_oid(&ref->new_oid)) {
 412                        info->status = PUSH_STATUS_DELETE;
 413                } else if (!oidcmp(&ref->old_oid, &ref->new_oid))
 414                        info->status = PUSH_STATUS_UPTODATE;
 415                else if (is_null_oid(&ref->old_oid))
 416                        info->status = PUSH_STATUS_CREATE;
 417                else if (has_object_file(&ref->old_oid) &&
 418                         ref_newer(&ref->new_oid, &ref->old_oid))
 419                        info->status = PUSH_STATUS_FASTFORWARD;
 420                else
 421                        info->status = PUSH_STATUS_OUTOFDATE;
 422        }
 423        free_refs(local_refs);
 424        free_refs(push_map);
 425        return 0;
 426}
 427
 428static int get_push_ref_states_noquery(struct ref_states *states)
 429{
 430        int i;
 431        struct remote *remote = states->remote;
 432        struct string_list_item *item;
 433        struct push_info *info;
 434
 435        if (remote->mirror)
 436                return 0;
 437
 438        states->push.strdup_strings = 1;
 439        if (!remote->push_refspec_nr) {
 440                item = string_list_append(&states->push, _("(matching)"));
 441                info = item->util = xcalloc(1, sizeof(struct push_info));
 442                info->status = PUSH_STATUS_NOTQUERIED;
 443                info->dest = xstrdup(item->string);
 444        }
 445        for (i = 0; i < remote->push_refspec_nr; i++) {
 446                struct refspec *spec = remote->push + i;
 447                if (spec->matching)
 448                        item = string_list_append(&states->push, _("(matching)"));
 449                else if (strlen(spec->src))
 450                        item = string_list_append(&states->push, spec->src);
 451                else
 452                        item = string_list_append(&states->push, _("(delete)"));
 453
 454                info = item->util = xcalloc(1, sizeof(struct push_info));
 455                info->forced = spec->force;
 456                info->status = PUSH_STATUS_NOTQUERIED;
 457                info->dest = xstrdup(spec->dst ? spec->dst : item->string);
 458        }
 459        return 0;
 460}
 461
 462static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
 463{
 464        struct ref *ref, *matches;
 465        struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
 466        struct refspec refspec;
 467
 468        refspec.force = 0;
 469        refspec.pattern = 1;
 470        refspec.src = refspec.dst = "refs/heads/*";
 471        states->heads.strdup_strings = 1;
 472        get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
 473        matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
 474                                    fetch_map, 1);
 475        for (ref = matches; ref; ref = ref->next)
 476                string_list_append(&states->heads, abbrev_branch(ref->name));
 477
 478        free_refs(fetch_map);
 479        free_refs(matches);
 480
 481        return 0;
 482}
 483
 484struct known_remote {
 485        struct known_remote *next;
 486        struct remote *remote;
 487};
 488
 489struct known_remotes {
 490        struct remote *to_delete;
 491        struct known_remote *list;
 492};
 493
 494static int add_known_remote(struct remote *remote, void *cb_data)
 495{
 496        struct known_remotes *all = cb_data;
 497        struct known_remote *r;
 498
 499        if (!strcmp(all->to_delete->name, remote->name))
 500                return 0;
 501
 502        r = xmalloc(sizeof(*r));
 503        r->remote = remote;
 504        r->next = all->list;
 505        all->list = r;
 506        return 0;
 507}
 508
 509struct branches_for_remote {
 510        struct remote *remote;
 511        struct string_list *branches, *skipped;
 512        struct known_remotes *keep;
 513};
 514
 515static int add_branch_for_removal(const char *refname,
 516        const struct object_id *oid, int flags, void *cb_data)
 517{
 518        struct branches_for_remote *branches = cb_data;
 519        struct refspec refspec;
 520        struct known_remote *kr;
 521
 522        memset(&refspec, 0, sizeof(refspec));
 523        refspec.dst = (char *)refname;
 524        if (remote_find_tracking(branches->remote, &refspec))
 525                return 0;
 526
 527        /* don't delete a branch if another remote also uses it */
 528        for (kr = branches->keep->list; kr; kr = kr->next) {
 529                memset(&refspec, 0, sizeof(refspec));
 530                refspec.dst = (char *)refname;
 531                if (!remote_find_tracking(kr->remote, &refspec))
 532                        return 0;
 533        }
 534
 535        /* don't delete non-remote-tracking refs */
 536        if (!starts_with(refname, "refs/remotes/")) {
 537                /* advise user how to delete local branches */
 538                if (starts_with(refname, "refs/heads/"))
 539                        string_list_append(branches->skipped,
 540                                           abbrev_branch(refname));
 541                /* silently skip over other non-remote refs */
 542                return 0;
 543        }
 544
 545        string_list_append(branches->branches, refname);
 546
 547        return 0;
 548}
 549
 550struct rename_info {
 551        const char *old_name;
 552        const char *new_name;
 553        struct string_list *remote_branches;
 554};
 555
 556static int read_remote_branches(const char *refname,
 557        const struct object_id *oid, int flags, void *cb_data)
 558{
 559        struct rename_info *rename = cb_data;
 560        struct strbuf buf = STRBUF_INIT;
 561        struct string_list_item *item;
 562        int flag;
 563        const char *symref;
 564
 565        strbuf_addf(&buf, "refs/remotes/%s/", rename->old_name);
 566        if (starts_with(refname, buf.buf)) {
 567                item = string_list_append(rename->remote_branches, xstrdup(refname));
 568                symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING,
 569                                            NULL, &flag);
 570                if (symref && (flag & REF_ISSYMREF))
 571                        item->util = xstrdup(symref);
 572                else
 573                        item->util = NULL;
 574        }
 575        strbuf_release(&buf);
 576
 577        return 0;
 578}
 579
 580static int migrate_file(struct remote *remote)
 581{
 582        struct strbuf buf = STRBUF_INIT;
 583        int i;
 584
 585        strbuf_addf(&buf, "remote.%s.url", remote->name);
 586        for (i = 0; i < remote->url_nr; i++)
 587                git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
 588        strbuf_reset(&buf);
 589        strbuf_addf(&buf, "remote.%s.push", remote->name);
 590        for (i = 0; i < remote->push_refspec_nr; i++)
 591                git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0);
 592        strbuf_reset(&buf);
 593        strbuf_addf(&buf, "remote.%s.fetch", remote->name);
 594        for (i = 0; i < remote->fetch_refspec_nr; i++)
 595                git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0);
 596        if (remote->origin == REMOTE_REMOTES)
 597                unlink_or_warn(git_path("remotes/%s", remote->name));
 598        else if (remote->origin == REMOTE_BRANCHES)
 599                unlink_or_warn(git_path("branches/%s", remote->name));
 600        strbuf_release(&buf);
 601
 602        return 0;
 603}
 604
 605static int mv(int argc, const char **argv)
 606{
 607        struct option options[] = {
 608                OPT_END()
 609        };
 610        struct remote *oldremote, *newremote;
 611        struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
 612                old_remote_context = STRBUF_INIT;
 613        struct string_list remote_branches = STRING_LIST_INIT_NODUP;
 614        struct rename_info rename;
 615        int i, refspec_updated = 0;
 616
 617        if (argc != 3)
 618                usage_with_options(builtin_remote_rename_usage, options);
 619
 620        rename.old_name = argv[1];
 621        rename.new_name = argv[2];
 622        rename.remote_branches = &remote_branches;
 623
 624        oldremote = remote_get(rename.old_name);
 625        if (!remote_is_configured(oldremote, 1))
 626                die(_("No such remote: %s"), rename.old_name);
 627
 628        if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG)
 629                return migrate_file(oldremote);
 630
 631        newremote = remote_get(rename.new_name);
 632        if (remote_is_configured(newremote, 1))
 633                die(_("remote %s already exists."), rename.new_name);
 634
 635        strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new_name);
 636        if (!valid_fetch_refspec(buf.buf))
 637                die(_("'%s' is not a valid remote name"), rename.new_name);
 638
 639        strbuf_reset(&buf);
 640        strbuf_addf(&buf, "remote.%s", rename.old_name);
 641        strbuf_addf(&buf2, "remote.%s", rename.new_name);
 642        if (git_config_rename_section(buf.buf, buf2.buf) < 1)
 643                return error(_("Could not rename config section '%s' to '%s'"),
 644                                buf.buf, buf2.buf);
 645
 646        strbuf_reset(&buf);
 647        strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
 648        git_config_set_multivar(buf.buf, NULL, NULL, 1);
 649        strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
 650        for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
 651                char *ptr;
 652
 653                strbuf_reset(&buf2);
 654                strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
 655                ptr = strstr(buf2.buf, old_remote_context.buf);
 656                if (ptr) {
 657                        refspec_updated = 1;
 658                        strbuf_splice(&buf2,
 659                                      ptr-buf2.buf + strlen(":refs/remotes/"),
 660                                      strlen(rename.old_name), rename.new_name,
 661                                      strlen(rename.new_name));
 662                } else
 663                        warning(_("Not updating non-default fetch refspec\n"
 664                                  "\t%s\n"
 665                                  "\tPlease update the configuration manually if necessary."),
 666                                buf2.buf);
 667
 668                git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
 669        }
 670
 671        read_branches();
 672        for (i = 0; i < branch_list.nr; i++) {
 673                struct string_list_item *item = branch_list.items + i;
 674                struct branch_info *info = item->util;
 675                if (info->remote_name && !strcmp(info->remote_name, rename.old_name)) {
 676                        strbuf_reset(&buf);
 677                        strbuf_addf(&buf, "branch.%s.remote", item->string);
 678                        git_config_set(buf.buf, rename.new_name);
 679                }
 680        }
 681
 682        if (!refspec_updated)
 683                return 0;
 684
 685        /*
 686         * First remove symrefs, then rename the rest, finally create
 687         * the new symrefs.
 688         */
 689        for_each_ref(read_remote_branches, &rename);
 690        for (i = 0; i < remote_branches.nr; i++) {
 691                struct string_list_item *item = remote_branches.items + i;
 692                int flag = 0;
 693                struct object_id oid;
 694
 695                read_ref_full(item->string, RESOLVE_REF_READING, &oid, &flag);
 696                if (!(flag & REF_ISSYMREF))
 697                        continue;
 698                if (delete_ref(NULL, item->string, NULL, REF_NO_DEREF))
 699                        die(_("deleting '%s' failed"), item->string);
 700        }
 701        for (i = 0; i < remote_branches.nr; i++) {
 702                struct string_list_item *item = remote_branches.items + i;
 703
 704                if (item->util)
 705                        continue;
 706                strbuf_reset(&buf);
 707                strbuf_addstr(&buf, item->string);
 708                strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
 709                                rename.new_name, strlen(rename.new_name));
 710                strbuf_reset(&buf2);
 711                strbuf_addf(&buf2, "remote: renamed %s to %s",
 712                                item->string, buf.buf);
 713                if (rename_ref(item->string, buf.buf, buf2.buf))
 714                        die(_("renaming '%s' failed"), item->string);
 715        }
 716        for (i = 0; i < remote_branches.nr; i++) {
 717                struct string_list_item *item = remote_branches.items + i;
 718
 719                if (!item->util)
 720                        continue;
 721                strbuf_reset(&buf);
 722                strbuf_addstr(&buf, item->string);
 723                strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
 724                                rename.new_name, strlen(rename.new_name));
 725                strbuf_reset(&buf2);
 726                strbuf_addstr(&buf2, item->util);
 727                strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old_name),
 728                                rename.new_name, strlen(rename.new_name));
 729                strbuf_reset(&buf3);
 730                strbuf_addf(&buf3, "remote: renamed %s to %s",
 731                                item->string, buf.buf);
 732                if (create_symref(buf.buf, buf2.buf, buf3.buf))
 733                        die(_("creating '%s' failed"), buf.buf);
 734        }
 735        return 0;
 736}
 737
 738static int rm(int argc, const char **argv)
 739{
 740        struct option options[] = {
 741                OPT_END()
 742        };
 743        struct remote *remote;
 744        struct strbuf buf = STRBUF_INIT;
 745        struct known_remotes known_remotes = { NULL, NULL };
 746        struct string_list branches = STRING_LIST_INIT_DUP;
 747        struct string_list skipped = STRING_LIST_INIT_DUP;
 748        struct branches_for_remote cb_data;
 749        int i, result;
 750
 751        memset(&cb_data, 0, sizeof(cb_data));
 752        cb_data.branches = &branches;
 753        cb_data.skipped = &skipped;
 754        cb_data.keep = &known_remotes;
 755
 756        if (argc != 2)
 757                usage_with_options(builtin_remote_rm_usage, options);
 758
 759        remote = remote_get(argv[1]);
 760        if (!remote_is_configured(remote, 1))
 761                die(_("No such remote: %s"), argv[1]);
 762
 763        known_remotes.to_delete = remote;
 764        for_each_remote(add_known_remote, &known_remotes);
 765
 766        read_branches();
 767        for (i = 0; i < branch_list.nr; i++) {
 768                struct string_list_item *item = branch_list.items + i;
 769                struct branch_info *info = item->util;
 770                if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
 771                        const char *keys[] = { "remote", "merge", NULL }, **k;
 772                        for (k = keys; *k; k++) {
 773                                strbuf_reset(&buf);
 774                                strbuf_addf(&buf, "branch.%s.%s",
 775                                                item->string, *k);
 776                                result = git_config_set_gently(buf.buf, NULL);
 777                                if (result && result != CONFIG_NOTHING_SET)
 778                                        die(_("could not unset '%s'"), buf.buf);
 779                        }
 780                }
 781        }
 782
 783        /*
 784         * We cannot just pass a function to for_each_ref() which deletes
 785         * the branches one by one, since for_each_ref() relies on cached
 786         * refs, which are invalidated when deleting a branch.
 787         */
 788        cb_data.remote = remote;
 789        result = for_each_ref(add_branch_for_removal, &cb_data);
 790        strbuf_release(&buf);
 791
 792        if (!result)
 793                result = delete_refs("remote: remove", &branches, REF_NO_DEREF);
 794        string_list_clear(&branches, 0);
 795
 796        if (skipped.nr) {
 797                fprintf_ln(stderr,
 798                           Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
 799                              "to delete it, use:",
 800                              "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
 801                              "to delete them, use:",
 802                              skipped.nr));
 803                for (i = 0; i < skipped.nr; i++)
 804                        fprintf(stderr, "  git branch -d %s\n",
 805                                skipped.items[i].string);
 806        }
 807        string_list_clear(&skipped, 0);
 808
 809        if (!result) {
 810                strbuf_addf(&buf, "remote.%s", remote->name);
 811                if (git_config_rename_section(buf.buf, NULL) < 1)
 812                        return error(_("Could not remove config section '%s'"), buf.buf);
 813        }
 814
 815        return result;
 816}
 817
 818static void clear_push_info(void *util, const char *string)
 819{
 820        struct push_info *info = util;
 821        free(info->dest);
 822        free(info);
 823}
 824
 825static void free_remote_ref_states(struct ref_states *states)
 826{
 827        string_list_clear(&states->new_refs, 0);
 828        string_list_clear(&states->stale, 1);
 829        string_list_clear(&states->tracked, 0);
 830        string_list_clear(&states->heads, 0);
 831        string_list_clear_func(&states->push, clear_push_info);
 832}
 833
 834static int append_ref_to_tracked_list(const char *refname,
 835        const struct object_id *oid, int flags, void *cb_data)
 836{
 837        struct ref_states *states = cb_data;
 838        struct refspec refspec;
 839
 840        if (flags & REF_ISSYMREF)
 841                return 0;
 842
 843        memset(&refspec, 0, sizeof(refspec));
 844        refspec.dst = (char *)refname;
 845        if (!remote_find_tracking(states->remote, &refspec))
 846                string_list_append(&states->tracked, abbrev_branch(refspec.src));
 847
 848        return 0;
 849}
 850
 851static int get_remote_ref_states(const char *name,
 852                                 struct ref_states *states,
 853                                 int query)
 854{
 855        struct transport *transport;
 856        const struct ref *remote_refs;
 857
 858        states->remote = remote_get(name);
 859        if (!states->remote)
 860                return error(_("No such remote: %s"), name);
 861
 862        read_branches();
 863
 864        if (query) {
 865                transport = transport_get(states->remote, states->remote->url_nr > 0 ?
 866                        states->remote->url[0] : NULL);
 867                remote_refs = transport_get_remote_refs(transport);
 868                transport_disconnect(transport);
 869
 870                states->queried = 1;
 871                if (query & GET_REF_STATES)
 872                        get_ref_states(remote_refs, states);
 873                if (query & GET_HEAD_NAMES)
 874                        get_head_names(remote_refs, states);
 875                if (query & GET_PUSH_REF_STATES)
 876                        get_push_ref_states(remote_refs, states);
 877        } else {
 878                for_each_ref(append_ref_to_tracked_list, states);
 879                string_list_sort(&states->tracked);
 880                get_push_ref_states_noquery(states);
 881        }
 882
 883        return 0;
 884}
 885
 886struct show_info {
 887        struct string_list *list;
 888        struct ref_states *states;
 889        int width, width2;
 890        int any_rebase;
 891};
 892
 893static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
 894{
 895        struct show_info *info = cb_data;
 896        int n = strlen(item->string);
 897        if (n > info->width)
 898                info->width = n;
 899        string_list_insert(info->list, item->string);
 900        return 0;
 901}
 902
 903static int show_remote_info_item(struct string_list_item *item, void *cb_data)
 904{
 905        struct show_info *info = cb_data;
 906        struct ref_states *states = info->states;
 907        const char *name = item->string;
 908
 909        if (states->queried) {
 910                const char *fmt = "%s";
 911                const char *arg = "";
 912                if (string_list_has_string(&states->new_refs, name)) {
 913                        fmt = _(" new (next fetch will store in remotes/%s)");
 914                        arg = states->remote->name;
 915                } else if (string_list_has_string(&states->tracked, name))
 916                        arg = _(" tracked");
 917                else if (string_list_has_string(&states->stale, name))
 918                        arg = _(" stale (use 'git remote prune' to remove)");
 919                else
 920                        arg = _(" ???");
 921                printf("    %-*s", info->width, name);
 922                printf(fmt, arg);
 923                printf("\n");
 924        } else
 925                printf("    %s\n", name);
 926
 927        return 0;
 928}
 929
 930static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
 931{
 932        struct show_info *show_info = cb_data;
 933        struct ref_states *states = show_info->states;
 934        struct branch_info *branch_info = branch_item->util;
 935        struct string_list_item *item;
 936        int n;
 937
 938        if (!branch_info->merge.nr || !branch_info->remote_name ||
 939            strcmp(states->remote->name, branch_info->remote_name))
 940                return 0;
 941        if ((n = strlen(branch_item->string)) > show_info->width)
 942                show_info->width = n;
 943        if (branch_info->rebase)
 944                show_info->any_rebase = 1;
 945
 946        item = string_list_insert(show_info->list, branch_item->string);
 947        item->util = branch_info;
 948
 949        return 0;
 950}
 951
 952static int show_local_info_item(struct string_list_item *item, void *cb_data)
 953{
 954        struct show_info *show_info = cb_data;
 955        struct branch_info *branch_info = item->util;
 956        struct string_list *merge = &branch_info->merge;
 957        int width = show_info->width + 4;
 958        int i;
 959
 960        if (branch_info->rebase && branch_info->merge.nr > 1) {
 961                error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
 962                        item->string);
 963                return 0;
 964        }
 965
 966        printf("    %-*s ", show_info->width, item->string);
 967        if (branch_info->rebase) {
 968                printf_ln(branch_info->rebase == INTERACTIVE_REBASE
 969                          ? _("rebases interactively onto remote %s")
 970                          : _("rebases onto remote %s"), merge->items[0].string);
 971                return 0;
 972        } else if (show_info->any_rebase) {
 973                printf_ln(_(" merges with remote %s"), merge->items[0].string);
 974                width++;
 975        } else {
 976                printf_ln(_("merges with remote %s"), merge->items[0].string);
 977        }
 978        for (i = 1; i < merge->nr; i++)
 979                printf(_("%-*s    and with remote %s\n"), width, "",
 980                       merge->items[i].string);
 981
 982        return 0;
 983}
 984
 985static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
 986{
 987        struct show_info *show_info = cb_data;
 988        struct push_info *push_info = push_item->util;
 989        struct string_list_item *item;
 990        int n;
 991        if ((n = strlen(push_item->string)) > show_info->width)
 992                show_info->width = n;
 993        if ((n = strlen(push_info->dest)) > show_info->width2)
 994                show_info->width2 = n;
 995        item = string_list_append(show_info->list, push_item->string);
 996        item->util = push_item->util;
 997        return 0;
 998}
 999
1000/*
1001 * Sorting comparison for a string list that has push_info
1002 * structs in its util field
1003 */
1004static int cmp_string_with_push(const void *va, const void *vb)
1005{
1006        const struct string_list_item *a = va;
1007        const struct string_list_item *b = vb;
1008        const struct push_info *a_push = a->util;
1009        const struct push_info *b_push = b->util;
1010        int cmp = strcmp(a->string, b->string);
1011        return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
1012}
1013
1014static int show_push_info_item(struct string_list_item *item, void *cb_data)
1015{
1016        struct show_info *show_info = cb_data;
1017        struct push_info *push_info = item->util;
1018        const char *src = item->string, *status = NULL;
1019
1020        switch (push_info->status) {
1021        case PUSH_STATUS_CREATE:
1022                status = _("create");
1023                break;
1024        case PUSH_STATUS_DELETE:
1025                status = _("delete");
1026                src = _("(none)");
1027                break;
1028        case PUSH_STATUS_UPTODATE:
1029                status = _("up to date");
1030                break;
1031        case PUSH_STATUS_FASTFORWARD:
1032                status = _("fast-forwardable");
1033                break;
1034        case PUSH_STATUS_OUTOFDATE:
1035                status = _("local out of date");
1036                break;
1037        case PUSH_STATUS_NOTQUERIED:
1038                break;
1039        }
1040        if (status) {
1041                if (push_info->forced)
1042                        printf_ln(_("    %-*s forces to %-*s (%s)"), show_info->width, src,
1043                               show_info->width2, push_info->dest, status);
1044                else
1045                        printf_ln(_("    %-*s pushes to %-*s (%s)"), show_info->width, src,
1046                               show_info->width2, push_info->dest, status);
1047        } else {
1048                if (push_info->forced)
1049                        printf_ln(_("    %-*s forces to %s"), show_info->width, src,
1050                               push_info->dest);
1051                else
1052                        printf_ln(_("    %-*s pushes to %s"), show_info->width, src,
1053                               push_info->dest);
1054        }
1055        return 0;
1056}
1057
1058static int get_one_entry(struct remote *remote, void *priv)
1059{
1060        struct string_list *list = priv;
1061        struct strbuf url_buf = STRBUF_INIT;
1062        const char **url;
1063        int i, url_nr;
1064
1065        if (remote->url_nr > 0) {
1066                strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
1067                string_list_append(list, remote->name)->util =
1068                                strbuf_detach(&url_buf, NULL);
1069        } else
1070                string_list_append(list, remote->name)->util = NULL;
1071        if (remote->pushurl_nr) {
1072                url = remote->pushurl;
1073                url_nr = remote->pushurl_nr;
1074        } else {
1075                url = remote->url;
1076                url_nr = remote->url_nr;
1077        }
1078        for (i = 0; i < url_nr; i++)
1079        {
1080                strbuf_addf(&url_buf, "%s (push)", url[i]);
1081                string_list_append(list, remote->name)->util =
1082                                strbuf_detach(&url_buf, NULL);
1083        }
1084
1085        return 0;
1086}
1087
1088static int show_all(void)
1089{
1090        struct string_list list = STRING_LIST_INIT_NODUP;
1091        int result;
1092
1093        list.strdup_strings = 1;
1094        result = for_each_remote(get_one_entry, &list);
1095
1096        if (!result) {
1097                int i;
1098
1099                string_list_sort(&list);
1100                for (i = 0; i < list.nr; i++) {
1101                        struct string_list_item *item = list.items + i;
1102                        if (verbose)
1103                                printf("%s\t%s\n", item->string,
1104                                        item->util ? (const char *)item->util : "");
1105                        else {
1106                                if (i && !strcmp((item - 1)->string, item->string))
1107                                        continue;
1108                                printf("%s\n", item->string);
1109                        }
1110                }
1111        }
1112        string_list_clear(&list, 1);
1113        return result;
1114}
1115
1116static int show(int argc, const char **argv)
1117{
1118        int no_query = 0, result = 0, query_flag = 0;
1119        struct option options[] = {
1120                OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1121                OPT_END()
1122        };
1123        struct ref_states states;
1124        struct string_list info_list = STRING_LIST_INIT_NODUP;
1125        struct show_info info;
1126
1127        argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage,
1128                             0);
1129
1130        if (argc < 1)
1131                return show_all();
1132
1133        if (!no_query)
1134                query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1135
1136        memset(&states, 0, sizeof(states));
1137        memset(&info, 0, sizeof(info));
1138        info.states = &states;
1139        info.list = &info_list;
1140        for (; argc; argc--, argv++) {
1141                int i;
1142                const char **url;
1143                int url_nr;
1144
1145                get_remote_ref_states(*argv, &states, query_flag);
1146
1147                printf_ln(_("* remote %s"), *argv);
1148                printf_ln(_("  Fetch URL: %s"), states.remote->url_nr > 0 ?
1149                       states.remote->url[0] : _("(no URL)"));
1150                if (states.remote->pushurl_nr) {
1151                        url = states.remote->pushurl;
1152                        url_nr = states.remote->pushurl_nr;
1153                } else {
1154                        url = states.remote->url;
1155                        url_nr = states.remote->url_nr;
1156                }
1157                for (i = 0; i < url_nr; i++)
1158                        /*
1159                         * TRANSLATORS: the colon ':' should align
1160                         * with the one in " Fetch URL: %s"
1161                         * translation.
1162                         */
1163                        printf_ln(_("  Push  URL: %s"), url[i]);
1164                if (!i)
1165                        printf_ln(_("  Push  URL: %s"), _("(no URL)"));
1166                if (no_query)
1167                        printf_ln(_("  HEAD branch: %s"), _("(not queried)"));
1168                else if (!states.heads.nr)
1169                        printf_ln(_("  HEAD branch: %s"), _("(unknown)"));
1170                else if (states.heads.nr == 1)
1171                        printf_ln(_("  HEAD branch: %s"), states.heads.items[0].string);
1172                else {
1173                        printf(_("  HEAD branch (remote HEAD is ambiguous,"
1174                                 " may be one of the following):\n"));
1175                        for (i = 0; i < states.heads.nr; i++)
1176                                printf("    %s\n", states.heads.items[i].string);
1177                }
1178
1179                /* remote branch info */
1180                info.width = 0;
1181                for_each_string_list(&states.new_refs, add_remote_to_show_info, &info);
1182                for_each_string_list(&states.tracked, add_remote_to_show_info, &info);
1183                for_each_string_list(&states.stale, add_remote_to_show_info, &info);
1184                if (info.list->nr)
1185                        printf_ln(Q_("  Remote branch:%s",
1186                                     "  Remote branches:%s",
1187                                     info.list->nr),
1188                                  no_query ? _(" (status not queried)") : "");
1189                for_each_string_list(info.list, show_remote_info_item, &info);
1190                string_list_clear(info.list, 0);
1191
1192                /* git pull info */
1193                info.width = 0;
1194                info.any_rebase = 0;
1195                for_each_string_list(&branch_list, add_local_to_show_info, &info);
1196                if (info.list->nr)
1197                        printf_ln(Q_("  Local branch configured for 'git pull':",
1198                                     "  Local branches configured for 'git pull':",
1199                                     info.list->nr));
1200                for_each_string_list(info.list, show_local_info_item, &info);
1201                string_list_clear(info.list, 0);
1202
1203                /* git push info */
1204                if (states.remote->mirror)
1205                        printf_ln(_("  Local refs will be mirrored by 'git push'"));
1206
1207                info.width = info.width2 = 0;
1208                for_each_string_list(&states.push, add_push_to_show_info, &info);
1209                QSORT(info.list->items, info.list->nr, cmp_string_with_push);
1210                if (info.list->nr)
1211                        printf_ln(Q_("  Local ref configured for 'git push'%s:",
1212                                     "  Local refs configured for 'git push'%s:",
1213                                     info.list->nr),
1214                                  no_query ? _(" (status not queried)") : "");
1215                for_each_string_list(info.list, show_push_info_item, &info);
1216                string_list_clear(info.list, 0);
1217
1218                free_remote_ref_states(&states);
1219        }
1220
1221        return result;
1222}
1223
1224static int set_head(int argc, const char **argv)
1225{
1226        int i, opt_a = 0, opt_d = 0, result = 0;
1227        struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1228        char *head_name = NULL;
1229
1230        struct option options[] = {
1231                OPT_BOOL('a', "auto", &opt_a,
1232                         N_("set refs/remotes/<name>/HEAD according to remote")),
1233                OPT_BOOL('d', "delete", &opt_d,
1234                         N_("delete refs/remotes/<name>/HEAD")),
1235                OPT_END()
1236        };
1237        argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
1238                             0);
1239        if (argc)
1240                strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1241
1242        if (!opt_a && !opt_d && argc == 2) {
1243                head_name = xstrdup(argv[1]);
1244        } else if (opt_a && !opt_d && argc == 1) {
1245                struct ref_states states;
1246                memset(&states, 0, sizeof(states));
1247                get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1248                if (!states.heads.nr)
1249                        result |= error(_("Cannot determine remote HEAD"));
1250                else if (states.heads.nr > 1) {
1251                        result |= error(_("Multiple remote HEAD branches. "
1252                                          "Please choose one explicitly with:"));
1253                        for (i = 0; i < states.heads.nr; i++)
1254                                fprintf(stderr, "  git remote set-head %s %s\n",
1255                                        argv[0], states.heads.items[i].string);
1256                } else
1257                        head_name = xstrdup(states.heads.items[0].string);
1258                free_remote_ref_states(&states);
1259        } else if (opt_d && !opt_a && argc == 1) {
1260                if (delete_ref(NULL, buf.buf, NULL, REF_NO_DEREF))
1261                        result |= error(_("Could not delete %s"), buf.buf);
1262        } else
1263                usage_with_options(builtin_remote_sethead_usage, options);
1264
1265        if (head_name) {
1266                strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1267                /* make sure it's valid */
1268                if (!ref_exists(buf2.buf))
1269                        result |= error(_("Not a valid ref: %s"), buf2.buf);
1270                else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1271                        result |= error(_("Could not setup %s"), buf.buf);
1272                if (opt_a)
1273                        printf("%s/HEAD set to %s\n", argv[0], head_name);
1274                free(head_name);
1275        }
1276
1277        strbuf_release(&buf);
1278        strbuf_release(&buf2);
1279        return result;
1280}
1281
1282static int prune_remote(const char *remote, int dry_run)
1283{
1284        int result = 0;
1285        struct ref_states states;
1286        struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1287        struct string_list_item *item;
1288        const char *dangling_msg = dry_run
1289                ? _(" %s will become dangling!")
1290                : _(" %s has become dangling!");
1291
1292        memset(&states, 0, sizeof(states));
1293        get_remote_ref_states(remote, &states, GET_REF_STATES);
1294
1295        if (!states.stale.nr) {
1296                free_remote_ref_states(&states);
1297                return 0;
1298        }
1299
1300        printf_ln(_("Pruning %s"), remote);
1301        printf_ln(_("URL: %s"),
1302                  states.remote->url_nr
1303                  ? states.remote->url[0]
1304                  : _("(no URL)"));
1305
1306        for_each_string_list_item(item, &states.stale)
1307                string_list_append(&refs_to_prune, item->util);
1308        string_list_sort(&refs_to_prune);
1309
1310        if (!dry_run)
1311                result |= delete_refs("remote: prune", &refs_to_prune, 0);
1312
1313        for_each_string_list_item(item, &states.stale) {
1314                const char *refname = item->util;
1315
1316                if (dry_run)
1317                        printf_ln(_(" * [would prune] %s"),
1318                               abbrev_ref(refname, "refs/remotes/"));
1319                else
1320                        printf_ln(_(" * [pruned] %s"),
1321                               abbrev_ref(refname, "refs/remotes/"));
1322        }
1323
1324        warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune);
1325
1326        string_list_clear(&refs_to_prune, 0);
1327        free_remote_ref_states(&states);
1328        return result;
1329}
1330
1331static int prune(int argc, const char **argv)
1332{
1333        int dry_run = 0, result = 0;
1334        struct option options[] = {
1335                OPT__DRY_RUN(&dry_run, N_("dry run")),
1336                OPT_END()
1337        };
1338
1339        argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage,
1340                             0);
1341
1342        if (argc < 1)
1343                usage_with_options(builtin_remote_prune_usage, options);
1344
1345        for (; argc; argc--, argv++)
1346                result |= prune_remote(*argv, dry_run);
1347
1348        return result;
1349}
1350
1351static int get_remote_default(const char *key, const char *value, void *priv)
1352{
1353        if (strcmp(key, "remotes.default") == 0) {
1354                int *found = priv;
1355                *found = 1;
1356        }
1357        return 0;
1358}
1359
1360static int update(int argc, const char **argv)
1361{
1362        int i, prune = -1;
1363        struct option options[] = {
1364                OPT_BOOL('p', "prune", &prune,
1365                         N_("prune remotes after fetching")),
1366                OPT_END()
1367        };
1368        struct argv_array fetch_argv = ARGV_ARRAY_INIT;
1369        int default_defined = 0;
1370        int retval;
1371
1372        argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
1373                             PARSE_OPT_KEEP_ARGV0);
1374
1375        argv_array_push(&fetch_argv, "fetch");
1376
1377        if (prune != -1)
1378                argv_array_push(&fetch_argv, prune ? "--prune" : "--no-prune");
1379        if (verbose)
1380                argv_array_push(&fetch_argv, "-v");
1381        argv_array_push(&fetch_argv, "--multiple");
1382        if (argc < 2)
1383                argv_array_push(&fetch_argv, "default");
1384        for (i = 1; i < argc; i++)
1385                argv_array_push(&fetch_argv, argv[i]);
1386
1387        if (strcmp(fetch_argv.argv[fetch_argv.argc-1], "default") == 0) {
1388                git_config(get_remote_default, &default_defined);
1389                if (!default_defined) {
1390                        argv_array_pop(&fetch_argv);
1391                        argv_array_push(&fetch_argv, "--all");
1392                }
1393        }
1394
1395        retval = run_command_v_opt(fetch_argv.argv, RUN_GIT_CMD);
1396        argv_array_clear(&fetch_argv);
1397        return retval;
1398}
1399
1400static int remove_all_fetch_refspecs(const char *remote, const char *key)
1401{
1402        return git_config_set_multivar_gently(key, NULL, NULL, 1);
1403}
1404
1405static void add_branches(struct remote *remote, const char **branches,
1406                         const char *key)
1407{
1408        const char *remotename = remote->name;
1409        int mirror = remote->mirror;
1410        struct strbuf refspec = STRBUF_INIT;
1411
1412        for (; *branches; branches++)
1413                add_branch(key, *branches, remotename, mirror, &refspec);
1414
1415        strbuf_release(&refspec);
1416}
1417
1418static int set_remote_branches(const char *remotename, const char **branches,
1419                                int add_mode)
1420{
1421        struct strbuf key = STRBUF_INIT;
1422        struct remote *remote;
1423
1424        strbuf_addf(&key, "remote.%s.fetch", remotename);
1425
1426        remote = remote_get(remotename);
1427        if (!remote_is_configured(remote, 1))
1428                die(_("No such remote '%s'"), remotename);
1429
1430        if (!add_mode && remove_all_fetch_refspecs(remotename, key.buf)) {
1431                strbuf_release(&key);
1432                return 1;
1433        }
1434        add_branches(remote, branches, key.buf);
1435
1436        strbuf_release(&key);
1437        return 0;
1438}
1439
1440static int set_branches(int argc, const char **argv)
1441{
1442        int add_mode = 0;
1443        struct option options[] = {
1444                OPT_BOOL('\0', "add", &add_mode, N_("add branch")),
1445                OPT_END()
1446        };
1447
1448        argc = parse_options(argc, argv, NULL, options,
1449                             builtin_remote_setbranches_usage, 0);
1450        if (argc == 0) {
1451                error(_("no remote specified"));
1452                usage_with_options(builtin_remote_setbranches_usage, options);
1453        }
1454        argv[argc] = NULL;
1455
1456        return set_remote_branches(argv[0], argv + 1, add_mode);
1457}
1458
1459static int get_url(int argc, const char **argv)
1460{
1461        int i, push_mode = 0, all_mode = 0;
1462        const char *remotename = NULL;
1463        struct remote *remote;
1464        const char **url;
1465        int url_nr;
1466        struct option options[] = {
1467                OPT_BOOL('\0', "push", &push_mode,
1468                         N_("query push URLs rather than fetch URLs")),
1469                OPT_BOOL('\0', "all", &all_mode,
1470                         N_("return all URLs")),
1471                OPT_END()
1472        };
1473        argc = parse_options(argc, argv, NULL, options, builtin_remote_geturl_usage, 0);
1474
1475        if (argc != 1)
1476                usage_with_options(builtin_remote_geturl_usage, options);
1477
1478        remotename = argv[0];
1479
1480        remote = remote_get(remotename);
1481        if (!remote_is_configured(remote, 1))
1482                die(_("No such remote '%s'"), remotename);
1483
1484        url_nr = 0;
1485        if (push_mode) {
1486                url = remote->pushurl;
1487                url_nr = remote->pushurl_nr;
1488        }
1489        /* else fetch mode */
1490
1491        /* Use the fetch URL when no push URLs were found or requested. */
1492        if (!url_nr) {
1493                url = remote->url;
1494                url_nr = remote->url_nr;
1495        }
1496
1497        if (!url_nr)
1498                die(_("no URLs configured for remote '%s'"), remotename);
1499
1500        if (all_mode) {
1501                for (i = 0; i < url_nr; i++)
1502                        printf_ln("%s", url[i]);
1503        } else {
1504                printf_ln("%s", *url);
1505        }
1506
1507        return 0;
1508}
1509
1510static int set_url(int argc, const char **argv)
1511{
1512        int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1513        int matches = 0, negative_matches = 0;
1514        const char *remotename = NULL;
1515        const char *newurl = NULL;
1516        const char *oldurl = NULL;
1517        struct remote *remote;
1518        regex_t old_regex;
1519        const char **urlset;
1520        int urlset_nr;
1521        struct strbuf name_buf = STRBUF_INIT;
1522        struct option options[] = {
1523                OPT_BOOL('\0', "push", &push_mode,
1524                         N_("manipulate push URLs")),
1525                OPT_BOOL('\0', "add", &add_mode,
1526                         N_("add URL")),
1527                OPT_BOOL('\0', "delete", &delete_mode,
1528                            N_("delete URLs")),
1529                OPT_END()
1530        };
1531        argc = parse_options(argc, argv, NULL, options, builtin_remote_seturl_usage,
1532                             PARSE_OPT_KEEP_ARGV0);
1533
1534        if (add_mode && delete_mode)
1535                die(_("--add --delete doesn't make sense"));
1536
1537        if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1538                usage_with_options(builtin_remote_seturl_usage, options);
1539
1540        remotename = argv[1];
1541        newurl = argv[2];
1542        if (argc > 3)
1543                oldurl = argv[3];
1544
1545        if (delete_mode)
1546                oldurl = newurl;
1547
1548        remote = remote_get(remotename);
1549        if (!remote_is_configured(remote, 1))
1550                die(_("No such remote '%s'"), remotename);
1551
1552        if (push_mode) {
1553                strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1554                urlset = remote->pushurl;
1555                urlset_nr = remote->pushurl_nr;
1556        } else {
1557                strbuf_addf(&name_buf, "remote.%s.url", remotename);
1558                urlset = remote->url;
1559                urlset_nr = remote->url_nr;
1560        }
1561
1562        /* Special cases that add new entry. */
1563        if ((!oldurl && !delete_mode) || add_mode) {
1564                if (add_mode)
1565                        git_config_set_multivar(name_buf.buf, newurl,
1566                                                       "^$", 0);
1567                else
1568                        git_config_set(name_buf.buf, newurl);
1569                goto out;
1570        }
1571
1572        /* Old URL specified. Demand that one matches. */
1573        if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1574                die(_("Invalid old URL pattern: %s"), oldurl);
1575
1576        for (i = 0; i < urlset_nr; i++)
1577                if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
1578                        matches++;
1579                else
1580                        negative_matches++;
1581        if (!delete_mode && !matches)
1582                die(_("No such URL found: %s"), oldurl);
1583        if (delete_mode && !negative_matches && !push_mode)
1584                die(_("Will not delete all non-push URLs"));
1585
1586        regfree(&old_regex);
1587
1588        if (!delete_mode)
1589                git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1590        else
1591                git_config_set_multivar(name_buf.buf, NULL, oldurl, 1);
1592out:
1593        strbuf_release(&name_buf);
1594        return 0;
1595}
1596
1597int cmd_remote(int argc, const char **argv, const char *prefix)
1598{
1599        struct option options[] = {
1600                OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
1601                OPT_END()
1602        };
1603        int result;
1604
1605        argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1606                PARSE_OPT_STOP_AT_NON_OPTION);
1607
1608        if (argc < 1)
1609                result = show_all();
1610        else if (!strcmp(argv[0], "add"))
1611                result = add(argc, argv);
1612        else if (!strcmp(argv[0], "rename"))
1613                result = mv(argc, argv);
1614        else if (!strcmp(argv[0], "rm") || !strcmp(argv[0], "remove"))
1615                result = rm(argc, argv);
1616        else if (!strcmp(argv[0], "set-head"))
1617                result = set_head(argc, argv);
1618        else if (!strcmp(argv[0], "set-branches"))
1619                result = set_branches(argc, argv);
1620        else if (!strcmp(argv[0], "get-url"))
1621                result = get_url(argc, argv);
1622        else if (!strcmp(argv[0], "set-url"))
1623                result = set_url(argc, argv);
1624        else if (!strcmp(argv[0], "show"))
1625                result = show(argc, argv);
1626        else if (!strcmp(argv[0], "prune"))
1627                result = prune(argc, argv);
1628        else if (!strcmp(argv[0], "update"))
1629                result = update(argc, argv);
1630        else {
1631                error(_("Unknown subcommand: %s"), argv[0]);
1632                usage_with_options(builtin_remote_usage, options);
1633        }
1634
1635        return result ? 1 : 0;
1636}