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