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