1ca6cdbe2a64a5c4b8d97b30183d48c929b4be53
   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",
  12        "git remote add <name> <url>",
  13        "git remote rename <old> <new>",
  14        "git remote rm <name>",
  15        "git remote show <name>",
  16        "git remote prune <name>",
  17        "git remote update [group]",
  18        NULL
  19};
  20
  21static int verbose;
  22
  23static int show_all(void);
  24
  25static inline int postfixcmp(const char *string, const char *postfix)
  26{
  27        int len1 = strlen(string), len2 = strlen(postfix);
  28        if (len1 < len2)
  29                return 1;
  30        return strcmp(string + len1 - len2, postfix);
  31}
  32
  33static int opt_parse_track(const struct option *opt, const char *arg, int not)
  34{
  35        struct string_list *list = opt->value;
  36        if (not)
  37                string_list_clear(list, 0);
  38        else
  39                string_list_append(arg, list);
  40        return 0;
  41}
  42
  43static int fetch_remote(const char *name)
  44{
  45        const char *argv[] = { "fetch", name, NULL };
  46        printf("Updating %s\n", name);
  47        if (run_command_v_opt(argv, RUN_GIT_CMD))
  48                return error("Could not fetch %s", name);
  49        return 0;
  50}
  51
  52static int add(int argc, const char **argv)
  53{
  54        int fetch = 0, mirror = 0;
  55        struct string_list track = { NULL, 0, 0 };
  56        const char *master = NULL;
  57        struct remote *remote;
  58        struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
  59        const char *name, *url;
  60        int i;
  61
  62        struct option options[] = {
  63                OPT_GROUP("add specific options"),
  64                OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
  65                OPT_CALLBACK('t', "track", &track, "branch",
  66                        "branch(es) to track", opt_parse_track),
  67                OPT_STRING('m', "master", &master, "branch", "master branch"),
  68                OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
  69                OPT_END()
  70        };
  71
  72        argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
  73
  74        if (argc < 2)
  75                usage_with_options(builtin_remote_usage, options);
  76
  77        name = argv[0];
  78        url = argv[1];
  79
  80        remote = remote_get(name);
  81        if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
  82                        remote->fetch_refspec_nr))
  83                die("remote %s already exists.", name);
  84
  85        strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
  86        if (!valid_fetch_refspec(buf2.buf))
  87                die("'%s' is not a valid remote name", name);
  88
  89        strbuf_addf(&buf, "remote.%s.url", name);
  90        if (git_config_set(buf.buf, url))
  91                return 1;
  92
  93        strbuf_reset(&buf);
  94        strbuf_addf(&buf, "remote.%s.fetch", name);
  95
  96        if (track.nr == 0)
  97                string_list_append("*", &track);
  98        for (i = 0; i < track.nr; i++) {
  99                struct string_list_item *item = track.items + i;
 100
 101                strbuf_reset(&buf2);
 102                strbuf_addch(&buf2, '+');
 103                if (mirror)
 104                        strbuf_addf(&buf2, "refs/%s:refs/%s",
 105                                        item->string, item->string);
 106                else
 107                        strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
 108                                        item->string, name, item->string);
 109                if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
 110                        return 1;
 111        }
 112
 113        if (mirror) {
 114                strbuf_reset(&buf);
 115                strbuf_addf(&buf, "remote.%s.mirror", name);
 116                if (git_config_set(buf.buf, "true"))
 117                        return 1;
 118        }
 119
 120        if (fetch && fetch_remote(name))
 121                return 1;
 122
 123        if (master) {
 124                strbuf_reset(&buf);
 125                strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
 126
 127                strbuf_reset(&buf2);
 128                strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
 129
 130                if (create_symref(buf.buf, buf2.buf, "remote add"))
 131                        return error("Could not setup master '%s'", master);
 132        }
 133
 134        strbuf_release(&buf);
 135        strbuf_release(&buf2);
 136        string_list_clear(&track, 0);
 137
 138        return 0;
 139}
 140
 141struct branch_info {
 142        char *remote;
 143        struct string_list merge;
 144};
 145
 146static struct string_list branch_list;
 147
 148static const char *abbrev_ref(const char *name, const char *prefix)
 149{
 150        const char *abbrev = skip_prefix(name, prefix);
 151        if (abbrev)
 152                return abbrev;
 153        return name;
 154}
 155#define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
 156
 157static int config_read_branches(const char *key, const char *value, void *cb)
 158{
 159        if (!prefixcmp(key, "branch.")) {
 160                char *name;
 161                struct string_list_item *item;
 162                struct branch_info *info;
 163                enum { REMOTE, MERGE } type;
 164
 165                key += 7;
 166                if (!postfixcmp(key, ".remote")) {
 167                        name = xstrndup(key, strlen(key) - 7);
 168                        type = REMOTE;
 169                } else if (!postfixcmp(key, ".merge")) {
 170                        name = xstrndup(key, strlen(key) - 6);
 171                        type = MERGE;
 172                } else
 173                        return 0;
 174
 175                item = string_list_insert(name, &branch_list);
 176
 177                if (!item->util)
 178                        item->util = xcalloc(sizeof(struct branch_info), 1);
 179                info = item->util;
 180                if (type == REMOTE) {
 181                        if (info->remote)
 182                                warning("more than one branch.%s", key);
 183                        info->remote = xstrdup(value);
 184                } else {
 185                        char *space = strchr(value, ' ');
 186                        value = abbrev_branch(value);
 187                        while (space) {
 188                                char *merge;
 189                                merge = xstrndup(value, space - value);
 190                                string_list_append(merge, &info->merge);
 191                                value = abbrev_branch(space + 1);
 192                                space = strchr(value, ' ');
 193                        }
 194                        string_list_append(xstrdup(value), &info->merge);
 195                }
 196        }
 197        return 0;
 198}
 199
 200static void read_branches(void)
 201{
 202        if (branch_list.nr)
 203                return;
 204        git_config(config_read_branches, NULL);
 205        sort_string_list(&branch_list);
 206}
 207
 208struct ref_states {
 209        struct remote *remote;
 210        struct string_list new, stale, tracked;
 211};
 212
 213static int handle_one_branch(const char *refname,
 214        const unsigned char *sha1, int flags, void *cb_data)
 215{
 216        struct ref_states *states = cb_data;
 217        struct refspec refspec;
 218
 219        memset(&refspec, 0, sizeof(refspec));
 220        refspec.dst = (char *)refname;
 221        if (!remote_find_tracking(states->remote, &refspec)) {
 222                struct string_list_item *item;
 223                const char *name = abbrev_branch(refspec.src);
 224                /* symbolic refs pointing nowhere were handled already */
 225                if ((flags & REF_ISSYMREF) ||
 226                                unsorted_string_list_has_string(&states->tracked,
 227                                        name) ||
 228                                unsorted_string_list_has_string(&states->new,
 229                                        name))
 230                        return 0;
 231                item = string_list_append(name, &states->stale);
 232                item->util = xstrdup(refname);
 233        }
 234        return 0;
 235}
 236
 237static int get_ref_states(const struct ref *ref, struct ref_states *states)
 238{
 239        struct ref *fetch_map = NULL, **tail = &fetch_map;
 240        int i;
 241
 242        for (i = 0; i < states->remote->fetch_refspec_nr; i++)
 243                if (get_fetch_map(ref, states->remote->fetch + i, &tail, 1))
 244                        die("Could not get fetch map for refspec %s",
 245                                states->remote->fetch_refspec[i]);
 246
 247        states->new.strdup_strings = states->tracked.strdup_strings = 1;
 248        for (ref = fetch_map; ref; ref = ref->next) {
 249                struct string_list *target = &states->tracked;
 250                unsigned char sha1[20];
 251                void *util = NULL;
 252
 253                if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
 254                        target = &states->new;
 255                else {
 256                        target = &states->tracked;
 257                        if (hashcmp(sha1, ref->new_sha1))
 258                                util = &states;
 259                }
 260                string_list_append(abbrev_branch(ref->name), target)->util = util;
 261        }
 262        free_refs(fetch_map);
 263
 264        for_each_ref(handle_one_branch, states);
 265        sort_string_list(&states->stale);
 266
 267        return 0;
 268}
 269
 270struct known_remote {
 271        struct known_remote *next;
 272        struct remote *remote;
 273};
 274
 275struct known_remotes {
 276        struct remote *to_delete;
 277        struct known_remote *list;
 278};
 279
 280static int add_known_remote(struct remote *remote, void *cb_data)
 281{
 282        struct known_remotes *all = cb_data;
 283        struct known_remote *r;
 284
 285        if (!strcmp(all->to_delete->name, remote->name))
 286                return 0;
 287
 288        r = xmalloc(sizeof(*r));
 289        r->remote = remote;
 290        r->next = all->list;
 291        all->list = r;
 292        return 0;
 293}
 294
 295struct branches_for_remote {
 296        struct remote *remote;
 297        struct string_list *branches;
 298        struct known_remotes *keep;
 299};
 300
 301static int add_branch_for_removal(const char *refname,
 302        const unsigned char *sha1, int flags, void *cb_data)
 303{
 304        struct branches_for_remote *branches = cb_data;
 305        struct refspec refspec;
 306        struct string_list_item *item;
 307        struct known_remote *kr;
 308
 309        memset(&refspec, 0, sizeof(refspec));
 310        refspec.dst = (char *)refname;
 311        if (remote_find_tracking(branches->remote, &refspec))
 312                return 0;
 313
 314        /* don't delete a branch if another remote also uses it */
 315        for (kr = branches->keep->list; kr; kr = kr->next) {
 316                memset(&refspec, 0, sizeof(refspec));
 317                refspec.dst = (char *)refname;
 318                if (!remote_find_tracking(kr->remote, &refspec))
 319                        return 0;
 320        }
 321
 322        /* make sure that symrefs are deleted */
 323        if (flags & REF_ISSYMREF)
 324                return unlink(git_path(refname));
 325
 326        item = string_list_append(refname, branches->branches);
 327        item->util = xmalloc(20);
 328        hashcpy(item->util, sha1);
 329
 330        return 0;
 331}
 332
 333struct rename_info {
 334        const char *old;
 335        const char *new;
 336        struct string_list *remote_branches;
 337};
 338
 339static int read_remote_branches(const char *refname,
 340        const unsigned char *sha1, int flags, void *cb_data)
 341{
 342        struct rename_info *rename = cb_data;
 343        struct strbuf buf = STRBUF_INIT;
 344        struct string_list_item *item;
 345        int flag;
 346        unsigned char orig_sha1[20];
 347        const char *symref;
 348
 349        strbuf_addf(&buf, "refs/remotes/%s", rename->old);
 350        if(!prefixcmp(refname, buf.buf)) {
 351                item = string_list_append(xstrdup(refname), rename->remote_branches);
 352                symref = resolve_ref(refname, orig_sha1, 1, &flag);
 353                if (flag & REF_ISSYMREF)
 354                        item->util = xstrdup(symref);
 355                else
 356                        item->util = NULL;
 357        }
 358
 359        return 0;
 360}
 361
 362static int mv(int argc, const char **argv)
 363{
 364        struct option options[] = {
 365                OPT_END()
 366        };
 367        struct remote *oldremote, *newremote;
 368        struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
 369        struct string_list remote_branches = { NULL, 0, 0, 0 };
 370        struct rename_info rename;
 371        int i;
 372
 373        if (argc != 3)
 374                usage_with_options(builtin_remote_usage, options);
 375
 376        rename.old = argv[1];
 377        rename.new = argv[2];
 378        rename.remote_branches = &remote_branches;
 379
 380        oldremote = remote_get(rename.old);
 381        if (!oldremote)
 382                die("No such remote: %s", rename.old);
 383
 384        newremote = remote_get(rename.new);
 385        if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
 386                die("remote %s already exists.", rename.new);
 387
 388        strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
 389        if (!valid_fetch_refspec(buf.buf))
 390                die("'%s' is not a valid remote name", rename.new);
 391
 392        strbuf_reset(&buf);
 393        strbuf_addf(&buf, "remote.%s", rename.old);
 394        strbuf_addf(&buf2, "remote.%s", rename.new);
 395        if (git_config_rename_section(buf.buf, buf2.buf) < 1)
 396                return error("Could not rename config section '%s' to '%s'",
 397                                buf.buf, buf2.buf);
 398
 399        strbuf_reset(&buf);
 400        strbuf_addf(&buf, "remote.%s.fetch", rename.new);
 401        if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
 402                return error("Could not remove config section '%s'", buf.buf);
 403        for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
 404                char *ptr;
 405
 406                strbuf_reset(&buf2);
 407                strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
 408                ptr = strstr(buf2.buf, rename.old);
 409                if (ptr)
 410                        strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
 411                                        rename.new, strlen(rename.new));
 412                if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
 413                        return error("Could not append '%s'", buf.buf);
 414        }
 415
 416        read_branches();
 417        for (i = 0; i < branch_list.nr; i++) {
 418                struct string_list_item *item = branch_list.items + i;
 419                struct branch_info *info = item->util;
 420                if (info->remote && !strcmp(info->remote, rename.old)) {
 421                        strbuf_reset(&buf);
 422                        strbuf_addf(&buf, "branch.%s.remote", item->string);
 423                        if (git_config_set(buf.buf, rename.new)) {
 424                                return error("Could not set '%s'", buf.buf);
 425                        }
 426                }
 427        }
 428
 429        /*
 430         * First remove symrefs, then rename the rest, finally create
 431         * the new symrefs.
 432         */
 433        for_each_ref(read_remote_branches, &rename);
 434        for (i = 0; i < remote_branches.nr; i++) {
 435                struct string_list_item *item = remote_branches.items + i;
 436                int flag = 0;
 437                unsigned char sha1[20];
 438                const char *symref;
 439
 440                symref = resolve_ref(item->string, sha1, 1, &flag);
 441                if (!(flag & REF_ISSYMREF))
 442                        continue;
 443                if (delete_ref(item->string, NULL, REF_NODEREF))
 444                        die("deleting '%s' failed", item->string);
 445        }
 446        for (i = 0; i < remote_branches.nr; i++) {
 447                struct string_list_item *item = remote_branches.items + i;
 448
 449                if (item->util)
 450                        continue;
 451                strbuf_reset(&buf);
 452                strbuf_addstr(&buf, item->string);
 453                strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
 454                                rename.new, strlen(rename.new));
 455                strbuf_reset(&buf2);
 456                strbuf_addf(&buf2, "remote: renamed %s to %s",
 457                                item->string, buf.buf);
 458                if (rename_ref(item->string, buf.buf, buf2.buf))
 459                        die("renaming '%s' failed", item->string);
 460        }
 461        for (i = 0; i < remote_branches.nr; i++) {
 462                struct string_list_item *item = remote_branches.items + i;
 463
 464                if (!item->util)
 465                        continue;
 466                strbuf_reset(&buf);
 467                strbuf_addstr(&buf, item->string);
 468                strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
 469                                rename.new, strlen(rename.new));
 470                strbuf_reset(&buf2);
 471                strbuf_addstr(&buf2, item->util);
 472                strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
 473                                rename.new, strlen(rename.new));
 474                strbuf_reset(&buf3);
 475                strbuf_addf(&buf3, "remote: renamed %s to %s",
 476                                item->string, buf.buf);
 477                if (create_symref(buf.buf, buf2.buf, buf3.buf))
 478                        die("creating '%s' failed", buf.buf);
 479        }
 480        return 0;
 481}
 482
 483static int remove_branches(struct string_list *branches)
 484{
 485        int i, result = 0;
 486        for (i = 0; i < branches->nr; i++) {
 487                struct string_list_item *item = branches->items + i;
 488                const char *refname = item->string;
 489                unsigned char *sha1 = item->util;
 490
 491                if (delete_ref(refname, sha1, 0))
 492                        result |= error("Could not remove branch %s", refname);
 493        }
 494        return result;
 495}
 496
 497static int rm(int argc, const char **argv)
 498{
 499        struct option options[] = {
 500                OPT_END()
 501        };
 502        struct remote *remote;
 503        struct strbuf buf = STRBUF_INIT;
 504        struct known_remotes known_remotes = { NULL, NULL };
 505        struct string_list branches = { NULL, 0, 0, 1 };
 506        struct branches_for_remote cb_data = { NULL, &branches, &known_remotes };
 507        int i;
 508
 509        if (argc != 2)
 510                usage_with_options(builtin_remote_usage, options);
 511
 512        remote = remote_get(argv[1]);
 513        if (!remote)
 514                die("No such remote: %s", argv[1]);
 515
 516        known_remotes.to_delete = remote;
 517        for_each_remote(add_known_remote, &known_remotes);
 518
 519        strbuf_addf(&buf, "remote.%s", remote->name);
 520        if (git_config_rename_section(buf.buf, NULL) < 1)
 521                return error("Could not remove config section '%s'", buf.buf);
 522
 523        read_branches();
 524        for (i = 0; i < branch_list.nr; i++) {
 525                struct string_list_item *item = branch_list.items + i;
 526                struct branch_info *info = item->util;
 527                if (info->remote && !strcmp(info->remote, remote->name)) {
 528                        const char *keys[] = { "remote", "merge", NULL }, **k;
 529                        for (k = keys; *k; k++) {
 530                                strbuf_reset(&buf);
 531                                strbuf_addf(&buf, "branch.%s.%s",
 532                                                item->string, *k);
 533                                if (git_config_set(buf.buf, NULL)) {
 534                                        strbuf_release(&buf);
 535                                        return -1;
 536                                }
 537                        }
 538                }
 539        }
 540
 541        /*
 542         * We cannot just pass a function to for_each_ref() which deletes
 543         * the branches one by one, since for_each_ref() relies on cached
 544         * refs, which are invalidated when deleting a branch.
 545         */
 546        cb_data.remote = remote;
 547        i = for_each_ref(add_branch_for_removal, &cb_data);
 548        strbuf_release(&buf);
 549
 550        if (!i)
 551                i = remove_branches(&branches);
 552        string_list_clear(&branches, 1);
 553
 554        return i;
 555}
 556
 557static void show_list(const char *title, struct string_list *list,
 558                      const char *extra_arg)
 559{
 560        int i;
 561
 562        if (!list->nr)
 563                return;
 564
 565        printf(title, list->nr > 1 ? "es" : "", extra_arg);
 566        printf("\n");
 567        for (i = 0; i < list->nr; i++)
 568                printf("    %s\n", list->items[i].string);
 569}
 570
 571static int get_remote_ref_states(const char *name,
 572                                 struct ref_states *states,
 573                                 int query)
 574{
 575        struct transport *transport;
 576        const struct ref *ref;
 577
 578        states->remote = remote_get(name);
 579        if (!states->remote)
 580                return error("No such remote: %s", name);
 581
 582        read_branches();
 583
 584        if (query) {
 585                transport = transport_get(NULL, states->remote->url_nr > 0 ?
 586                        states->remote->url[0] : NULL);
 587                ref = transport_get_remote_refs(transport);
 588                transport_disconnect(transport);
 589
 590                get_ref_states(ref, states);
 591        }
 592
 593        return 0;
 594}
 595
 596static int append_ref_to_tracked_list(const char *refname,
 597        const unsigned char *sha1, int flags, void *cb_data)
 598{
 599        struct ref_states *states = cb_data;
 600        struct refspec refspec;
 601
 602        memset(&refspec, 0, sizeof(refspec));
 603        refspec.dst = (char *)refname;
 604        if (!remote_find_tracking(states->remote, &refspec))
 605                string_list_append(abbrev_branch(refspec.src), &states->tracked);
 606
 607        return 0;
 608}
 609
 610static int show(int argc, const char **argv)
 611{
 612        int no_query = 0, result = 0;
 613        struct option options[] = {
 614                OPT_GROUP("show specific options"),
 615                OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
 616                OPT_END()
 617        };
 618        struct ref_states states;
 619
 620        argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
 621
 622        if (argc < 1)
 623                return show_all();
 624
 625        memset(&states, 0, sizeof(states));
 626        for (; argc; argc--, argv++) {
 627                int i;
 628
 629                get_remote_ref_states(*argv, &states, !no_query);
 630
 631                printf("* remote %s\n  URL: %s\n", *argv,
 632                        states.remote->url_nr > 0 ?
 633                                states.remote->url[0] : "(no URL)");
 634
 635                for (i = 0; i < branch_list.nr; i++) {
 636                        struct string_list_item *branch = branch_list.items + i;
 637                        struct branch_info *info = branch->util;
 638                        int j;
 639
 640                        if (!info->merge.nr || strcmp(*argv, info->remote))
 641                                continue;
 642                        printf("  Remote branch%s merged with 'git pull' "
 643                                "while on branch %s\n   ",
 644                                info->merge.nr > 1 ? "es" : "",
 645                                branch->string);
 646                        for (j = 0; j < info->merge.nr; j++)
 647                                printf(" %s", info->merge.items[j].string);
 648                        printf("\n");
 649                }
 650
 651                if (!no_query) {
 652                        show_list("  New remote branch%s (next fetch "
 653                                "will store in remotes/%s)",
 654                                &states.new, states.remote->name);
 655                        show_list("  Stale tracking branch%s (use 'git remote "
 656                                "prune')", &states.stale, "");
 657                }
 658
 659                if (no_query)
 660                        for_each_ref(append_ref_to_tracked_list, &states);
 661                show_list("  Tracked remote branch%s", &states.tracked, "");
 662
 663                if (states.remote->push_refspec_nr) {
 664                        printf("  Local branch%s pushed with 'git push'\n",
 665                                states.remote->push_refspec_nr > 1 ?
 666                                        "es" : "");
 667                        for (i = 0; i < states.remote->push_refspec_nr; i++) {
 668                                struct refspec *spec = states.remote->push + i;
 669                                printf("    %s%s%s%s\n",
 670                                       spec->force ? "+" : "",
 671                                       abbrev_branch(spec->src),
 672                                       spec->dst ? ":" : "",
 673                                       spec->dst ? abbrev_branch(spec->dst) : "");
 674                        }
 675                }
 676
 677                /* NEEDSWORK: free remote */
 678                string_list_clear(&states.new, 0);
 679                string_list_clear(&states.stale, 0);
 680                string_list_clear(&states.tracked, 0);
 681        }
 682
 683        return result;
 684}
 685
 686static int prune(int argc, const char **argv)
 687{
 688        int dry_run = 0, result = 0;
 689        struct option options[] = {
 690                OPT_GROUP("prune specific options"),
 691                OPT__DRY_RUN(&dry_run),
 692                OPT_END()
 693        };
 694        struct ref_states states;
 695
 696        argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
 697
 698        if (argc < 1)
 699                usage_with_options(builtin_remote_usage, options);
 700
 701        memset(&states, 0, sizeof(states));
 702        for (; argc; argc--, argv++) {
 703                int i;
 704
 705                get_remote_ref_states(*argv, &states, 1);
 706
 707                if (states.stale.nr) {
 708                        printf("Pruning %s\n", *argv);
 709                        printf("URL: %s\n",
 710                               states.remote->url_nr
 711                               ? states.remote->url[0]
 712                               : "(no URL)");
 713                }
 714
 715                for (i = 0; i < states.stale.nr; i++) {
 716                        const char *refname = states.stale.items[i].util;
 717
 718                        if (!dry_run)
 719                                result |= delete_ref(refname, NULL, 0);
 720
 721                        printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
 722                               abbrev_ref(refname, "refs/remotes/"));
 723                }
 724
 725                /* NEEDSWORK: free remote */
 726                string_list_clear(&states.new, 0);
 727                string_list_clear(&states.stale, 0);
 728                string_list_clear(&states.tracked, 0);
 729        }
 730
 731        return result;
 732}
 733
 734static int get_one_remote_for_update(struct remote *remote, void *priv)
 735{
 736        struct string_list *list = priv;
 737        if (!remote->skip_default_update)
 738                string_list_append(xstrdup(remote->name), list);
 739        return 0;
 740}
 741
 742struct remote_group {
 743        const char *name;
 744        struct string_list *list;
 745} remote_group;
 746
 747static int get_remote_group(const char *key, const char *value, void *cb)
 748{
 749        if (!prefixcmp(key, "remotes.") &&
 750                        !strcmp(key + 8, remote_group.name)) {
 751                /* split list by white space */
 752                int space = strcspn(value, " \t\n");
 753                while (*value) {
 754                        if (space > 1)
 755                                string_list_append(xstrndup(value, space),
 756                                                remote_group.list);
 757                        value += space + (value[space] != '\0');
 758                        space = strcspn(value, " \t\n");
 759                }
 760        }
 761
 762        return 0;
 763}
 764
 765static int update(int argc, const char **argv)
 766{
 767        int i, result = 0;
 768        struct string_list list = { NULL, 0, 0, 0 };
 769        static const char *default_argv[] = { NULL, "default", NULL };
 770
 771        if (argc < 2) {
 772                argc = 2;
 773                argv = default_argv;
 774        }
 775
 776        remote_group.list = &list;
 777        for (i = 1; i < argc; i++) {
 778                remote_group.name = argv[i];
 779                result = git_config(get_remote_group, NULL);
 780        }
 781
 782        if (!result && !list.nr  && argc == 2 && !strcmp(argv[1], "default"))
 783                result = for_each_remote(get_one_remote_for_update, &list);
 784
 785        for (i = 0; i < list.nr; i++)
 786                result |= fetch_remote(list.items[i].string);
 787
 788        /* all names were strdup()ed or strndup()ed */
 789        list.strdup_strings = 1;
 790        string_list_clear(&list, 0);
 791
 792        return result;
 793}
 794
 795static int get_one_entry(struct remote *remote, void *priv)
 796{
 797        struct string_list *list = priv;
 798
 799        if (remote->url_nr > 0) {
 800                int i;
 801
 802                for (i = 0; i < remote->url_nr; i++)
 803                        string_list_append(remote->name, list)->util = (void *)remote->url[i];
 804        } else
 805                string_list_append(remote->name, list)->util = NULL;
 806
 807        return 0;
 808}
 809
 810static int show_all(void)
 811{
 812        struct string_list list = { NULL, 0, 0 };
 813        int result = for_each_remote(get_one_entry, &list);
 814
 815        if (!result) {
 816                int i;
 817
 818                sort_string_list(&list);
 819                for (i = 0; i < list.nr; i++) {
 820                        struct string_list_item *item = list.items + i;
 821                        if (verbose)
 822                                printf("%s\t%s\n", item->string,
 823                                        item->util ? (const char *)item->util : "");
 824                        else {
 825                                if (i && !strcmp((item - 1)->string, item->string))
 826                                        continue;
 827                                printf("%s\n", item->string);
 828                        }
 829                }
 830        }
 831        return result;
 832}
 833
 834int cmd_remote(int argc, const char **argv, const char *prefix)
 835{
 836        struct option options[] = {
 837                OPT__VERBOSE(&verbose),
 838                OPT_END()
 839        };
 840        int result;
 841
 842        argc = parse_options(argc, argv, options, builtin_remote_usage,
 843                PARSE_OPT_STOP_AT_NON_OPTION);
 844
 845        if (argc < 1)
 846                result = show_all();
 847        else if (!strcmp(argv[0], "add"))
 848                result = add(argc, argv);
 849        else if (!strcmp(argv[0], "rename"))
 850                result = mv(argc, argv);
 851        else if (!strcmp(argv[0], "rm"))
 852                result = rm(argc, argv);
 853        else if (!strcmp(argv[0], "show"))
 854                result = show(argc, argv);
 855        else if (!strcmp(argv[0], "prune"))
 856                result = prune(argc, argv);
 857        else if (!strcmp(argv[0], "update"))
 858                result = update(argc, argv);
 859        else {
 860                error("Unknown subcommand: %s", argv[0]);
 861                usage_with_options(builtin_remote_usage, options);
 862        }
 863
 864        return result ? 1 : 0;
 865}