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