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