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