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