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