builtin-remote.con commit Merge branch 'sp/maint-index-pack' (7ed3fed)
   1#include "cache.h"
   2#include "parse-options.h"
   3#include "transport.h"
   4#include "remote.h"
   5#include "path-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",
  12        "git remote add <name> <url>",
  13        "git remote rm <name>",
  14        "git remote show <name>",
  15        "git remote prune <name>",
  16        "git remote update [group]",
  17        NULL
  18};
  19
  20static int verbose;
  21
  22static int show_all(void);
  23
  24static inline int postfixcmp(const char *string, const char *postfix)
  25{
  26        int len1 = strlen(string), len2 = strlen(postfix);
  27        if (len1 < len2)
  28                return 1;
  29        return strcmp(string + len1 - len2, postfix);
  30}
  31
  32static int opt_parse_track(const struct option *opt, const char *arg, int not)
  33{
  34        struct path_list *list = opt->value;
  35        if (not)
  36                path_list_clear(list, 0);
  37        else
  38                path_list_append(arg, list);
  39        return 0;
  40}
  41
  42static int fetch_remote(const char *name)
  43{
  44        const char *argv[] = { "fetch", name, NULL };
  45        printf("Updating %s\n", name);
  46        if (run_command_v_opt(argv, RUN_GIT_CMD))
  47                return error("Could not fetch %s", name);
  48        return 0;
  49}
  50
  51static int add(int argc, const char **argv)
  52{
  53        int fetch = 0, mirror = 0;
  54        struct path_list track = { NULL, 0, 0 };
  55        const char *master = NULL;
  56        struct remote *remote;
  57        struct strbuf buf, buf2;
  58        const char *name, *url;
  59        int i;
  60
  61        struct option options[] = {
  62                OPT_GROUP("add specific options"),
  63                OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
  64                OPT_CALLBACK('t', "track", &track, "branch",
  65                        "branch(es) to track", opt_parse_track),
  66                OPT_STRING('m', "master", &master, "branch", "master branch"),
  67                OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
  68                OPT_END()
  69        };
  70
  71        argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
  72
  73        if (argc < 2)
  74                usage_with_options(builtin_remote_usage, options);
  75
  76        name = argv[0];
  77        url = argv[1];
  78
  79        remote = remote_get(name);
  80        if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
  81                        remote->fetch_refspec_nr))
  82                die("remote %s already exists.", name);
  83
  84        strbuf_init(&buf, 0);
  85        strbuf_init(&buf2, 0);
  86
  87        strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
  88        if (!valid_fetch_refspec(buf2.buf))
  89                die("'%s' is not a valid remote name", name);
  90
  91        strbuf_addf(&buf, "remote.%s.url", name);
  92        if (git_config_set(buf.buf, url))
  93                return 1;
  94
  95        strbuf_reset(&buf);
  96        strbuf_addf(&buf, "remote.%s.fetch", name);
  97
  98        if (track.nr == 0)
  99                path_list_append("*", &track);
 100        for (i = 0; i < track.nr; i++) {
 101                struct path_list_item *item = track.items + i;
 102
 103                strbuf_reset(&buf2);
 104                strbuf_addch(&buf2, '+');
 105                if (mirror)
 106                        strbuf_addf(&buf2, "refs/%s:refs/%s",
 107                                        item->path, item->path);
 108                else
 109                        strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
 110                                        item->path, name, item->path);
 111                if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
 112                        return 1;
 113        }
 114
 115        if (mirror) {
 116                strbuf_reset(&buf);
 117                strbuf_addf(&buf, "remote.%s.mirror", name);
 118                if (git_config_set(buf.buf, "yes"))
 119                        return 1;
 120        }
 121
 122        if (fetch && fetch_remote(name))
 123                return 1;
 124
 125        if (master) {
 126                strbuf_reset(&buf);
 127                strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
 128
 129                strbuf_reset(&buf2);
 130                strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
 131
 132                if (create_symref(buf.buf, buf2.buf, "remote add"))
 133                        return error("Could not setup master '%s'", master);
 134        }
 135
 136        strbuf_release(&buf);
 137        strbuf_release(&buf2);
 138        path_list_clear(&track, 0);
 139
 140        return 0;
 141}
 142
 143struct branch_info {
 144        char *remote;
 145        struct path_list merge;
 146};
 147
 148static struct path_list branch_list;
 149
 150static int config_read_branches(const char *key, const char *value, void *cb)
 151{
 152        if (!prefixcmp(key, "branch.")) {
 153                char *name;
 154                struct path_list_item *item;
 155                struct branch_info *info;
 156                enum { REMOTE, MERGE } type;
 157
 158                key += 7;
 159                if (!postfixcmp(key, ".remote")) {
 160                        name = xstrndup(key, strlen(key) - 7);
 161                        type = REMOTE;
 162                } else if (!postfixcmp(key, ".merge")) {
 163                        name = xstrndup(key, strlen(key) - 6);
 164                        type = MERGE;
 165                } else
 166                        return 0;
 167
 168                item = path_list_insert(name, &branch_list);
 169
 170                if (!item->util)
 171                        item->util = xcalloc(sizeof(struct branch_info), 1);
 172                info = item->util;
 173                if (type == REMOTE) {
 174                        if (info->remote)
 175                                warning("more than one branch.%s", key);
 176                        info->remote = xstrdup(value);
 177                } else {
 178                        char *space = strchr(value, ' ');
 179                        const char *ptr = skip_prefix(value, "refs/heads/");
 180                        if (ptr)
 181                                value = ptr;
 182                        while (space) {
 183                                char *merge;
 184                                merge = xstrndup(value, space - value);
 185                                path_list_append(merge, &info->merge);
 186                                ptr = skip_prefix(space + 1, "refs/heads/");
 187                                if (ptr)
 188                                        value = ptr;
 189                                else
 190                                        value = space + 1;
 191                                space = strchr(value, ' ');
 192                        }
 193                        path_list_append(xstrdup(value), &info->merge);
 194                }
 195        }
 196        return 0;
 197}
 198
 199static void read_branches(void)
 200{
 201        if (branch_list.nr)
 202                return;
 203        git_config(config_read_branches, NULL);
 204        sort_path_list(&branch_list);
 205}
 206
 207struct ref_states {
 208        struct remote *remote;
 209        struct path_list new, stale, tracked;
 210};
 211
 212static int handle_one_branch(const char *refname,
 213        const unsigned char *sha1, int flags, void *cb_data)
 214{
 215        struct ref_states *states = cb_data;
 216        struct refspec refspec;
 217
 218        memset(&refspec, 0, sizeof(refspec));
 219        refspec.dst = (char *)refname;
 220        if (!remote_find_tracking(states->remote, &refspec)) {
 221                struct path_list_item *item;
 222                const char *name, *ptr;
 223                ptr = skip_prefix(refspec.src, "refs/heads/");
 224                if (ptr)
 225                        name = ptr;
 226                else
 227                        name = refspec.src;
 228                /* symbolic refs pointing nowhere were handled already */
 229                if ((flags & REF_ISSYMREF) ||
 230                                unsorted_path_list_has_path(&states->tracked,
 231                                        name) ||
 232                                unsorted_path_list_has_path(&states->new,
 233                                        name))
 234                        return 0;
 235                item = path_list_append(name, &states->stale);
 236                item->util = xstrdup(refname);
 237        }
 238        return 0;
 239}
 240
 241static int get_ref_states(const struct ref *ref, struct ref_states *states)
 242{
 243        struct ref *fetch_map = NULL, **tail = &fetch_map;
 244        int i;
 245
 246        for (i = 0; i < states->remote->fetch_refspec_nr; i++)
 247                if (get_fetch_map(ref, states->remote->fetch + i, &tail, 1))
 248                        die("Could not get fetch map for refspec %s",
 249                                states->remote->fetch_refspec[i]);
 250
 251        states->new.strdup_paths = states->tracked.strdup_paths = 1;
 252        for (ref = fetch_map; ref; ref = ref->next) {
 253                struct path_list *target = &states->tracked;
 254                unsigned char sha1[20];
 255                void *util = NULL;
 256                const char *ptr;
 257
 258                if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
 259                        target = &states->new;
 260                else {
 261                        target = &states->tracked;
 262                        if (hashcmp(sha1, ref->new_sha1))
 263                                util = &states;
 264                }
 265                ptr = skip_prefix(ref->name, "refs/heads/");
 266                if (!ptr)
 267                        ptr = ref->name;
 268                path_list_append(ptr, target)->util = util;
 269        }
 270        free_refs(fetch_map);
 271
 272        for_each_ref(handle_one_branch, states);
 273        sort_path_list(&states->stale);
 274
 275        return 0;
 276}
 277
 278struct known_remote {
 279        struct known_remote *next;
 280        struct remote *remote;
 281};
 282
 283struct known_remotes {
 284        struct remote *to_delete;
 285        struct known_remote *list;
 286};
 287
 288static int add_known_remote(struct remote *remote, void *cb_data)
 289{
 290        struct known_remotes *all = cb_data;
 291        struct known_remote *r;
 292
 293        if (!strcmp(all->to_delete->name, remote->name))
 294                return 0;
 295
 296        r = xmalloc(sizeof(*r));
 297        r->remote = remote;
 298        r->next = all->list;
 299        all->list = r;
 300        return 0;
 301}
 302
 303struct branches_for_remote {
 304        struct remote *remote;
 305        struct path_list *branches;
 306        struct known_remotes *keep;
 307};
 308
 309static int add_branch_for_removal(const char *refname,
 310        const unsigned char *sha1, int flags, void *cb_data)
 311{
 312        struct branches_for_remote *branches = cb_data;
 313        struct refspec refspec;
 314        struct path_list_item *item;
 315        struct known_remote *kr;
 316
 317        memset(&refspec, 0, sizeof(refspec));
 318        refspec.dst = (char *)refname;
 319        if (remote_find_tracking(branches->remote, &refspec))
 320                return 0;
 321
 322        /* don't delete a branch if another remote also uses it */
 323        for (kr = branches->keep->list; kr; kr = kr->next) {
 324                memset(&refspec, 0, sizeof(refspec));
 325                refspec.dst = (char *)refname;
 326                if (!remote_find_tracking(kr->remote, &refspec))
 327                        return 0;
 328        }
 329
 330        /* make sure that symrefs are deleted */
 331        if (flags & REF_ISSYMREF)
 332                return unlink(git_path(refname));
 333
 334        item = path_list_append(refname, branches->branches);
 335        item->util = xmalloc(20);
 336        hashcpy(item->util, sha1);
 337
 338        return 0;
 339}
 340
 341static int remove_branches(struct path_list *branches)
 342{
 343        int i, result = 0;
 344        for (i = 0; i < branches->nr; i++) {
 345                struct path_list_item *item = branches->items + i;
 346                const char *refname = item->path;
 347                unsigned char *sha1 = item->util;
 348
 349                if (delete_ref(refname, sha1))
 350                        result |= error("Could not remove branch %s", refname);
 351        }
 352        return result;
 353}
 354
 355static int rm(int argc, const char **argv)
 356{
 357        struct option options[] = {
 358                OPT_END()
 359        };
 360        struct remote *remote;
 361        struct strbuf buf;
 362        struct known_remotes known_remotes = { NULL, NULL };
 363        struct path_list branches = { NULL, 0, 0, 1 };
 364        struct branches_for_remote cb_data = { NULL, &branches, &known_remotes };
 365        int i;
 366
 367        if (argc != 2)
 368                usage_with_options(builtin_remote_usage, options);
 369
 370        remote = remote_get(argv[1]);
 371        if (!remote)
 372                die("No such remote: %s", argv[1]);
 373
 374        known_remotes.to_delete = remote;
 375        for_each_remote(add_known_remote, &known_remotes);
 376
 377        strbuf_init(&buf, 0);
 378        strbuf_addf(&buf, "remote.%s", remote->name);
 379        if (git_config_rename_section(buf.buf, NULL) < 1)
 380                return error("Could not remove config section '%s'", buf.buf);
 381
 382        read_branches();
 383        for (i = 0; i < branch_list.nr; i++) {
 384                struct path_list_item *item = branch_list.items + i;
 385                struct branch_info *info = item->util;
 386                if (info->remote && !strcmp(info->remote, remote->name)) {
 387                        const char *keys[] = { "remote", "merge", NULL }, **k;
 388                        for (k = keys; *k; k++) {
 389                                strbuf_reset(&buf);
 390                                strbuf_addf(&buf, "branch.%s.%s",
 391                                                item->path, *k);
 392                                if (git_config_set(buf.buf, NULL)) {
 393                                        strbuf_release(&buf);
 394                                        return -1;
 395                                }
 396                        }
 397                }
 398        }
 399
 400        /*
 401         * We cannot just pass a function to for_each_ref() which deletes
 402         * the branches one by one, since for_each_ref() relies on cached
 403         * refs, which are invalidated when deleting a branch.
 404         */
 405        cb_data.remote = remote;
 406        i = for_each_ref(add_branch_for_removal, &cb_data);
 407        strbuf_release(&buf);
 408
 409        if (!i)
 410                i = remove_branches(&branches);
 411        path_list_clear(&branches, 1);
 412
 413        return i;
 414}
 415
 416static void show_list(const char *title, struct path_list *list)
 417{
 418        int i;
 419
 420        if (!list->nr)
 421                return;
 422
 423        printf(title, list->nr > 1 ? "es" : "");
 424        printf("\n    ");
 425        for (i = 0; i < list->nr; i++)
 426                printf("%s%s", i ? " " : "", list->items[i].path);
 427        printf("\n");
 428}
 429
 430static int get_remote_ref_states(const char *name,
 431                                 struct ref_states *states,
 432                                 int query)
 433{
 434        struct transport *transport;
 435        const struct ref *ref;
 436
 437        states->remote = remote_get(name);
 438        if (!states->remote)
 439                return error("No such remote: %s", name);
 440
 441        read_branches();
 442
 443        if (query) {
 444                transport = transport_get(NULL, states->remote->url_nr > 0 ?
 445                        states->remote->url[0] : NULL);
 446                ref = transport_get_remote_refs(transport);
 447                transport_disconnect(transport);
 448
 449                get_ref_states(ref, states);
 450        }
 451
 452        return 0;
 453}
 454
 455static int append_ref_to_tracked_list(const char *refname,
 456        const unsigned char *sha1, int flags, void *cb_data)
 457{
 458        struct ref_states *states = cb_data;
 459        struct refspec refspec;
 460
 461        memset(&refspec, 0, sizeof(refspec));
 462        refspec.dst = (char *)refname;
 463        if (!remote_find_tracking(states->remote, &refspec)) {
 464                path_list_append(skip_prefix(refspec.src, "refs/heads/"),
 465                        &states->tracked);
 466        }
 467
 468        return 0;
 469}
 470
 471static int show(int argc, const char **argv)
 472{
 473        int no_query = 0, result = 0;
 474        struct option options[] = {
 475                OPT_GROUP("show specific options"),
 476                OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
 477                OPT_END()
 478        };
 479        struct ref_states states;
 480
 481        argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
 482
 483        if (argc < 1)
 484                return show_all();
 485
 486        memset(&states, 0, sizeof(states));
 487        for (; argc; argc--, argv++) {
 488                struct strbuf buf;
 489                int i;
 490
 491                get_remote_ref_states(*argv, &states, !no_query);
 492
 493                printf("* remote %s\n  URL: %s\n", *argv,
 494                        states.remote->url_nr > 0 ?
 495                                states.remote->url[0] : "(no URL)");
 496
 497                for (i = 0; i < branch_list.nr; i++) {
 498                        struct path_list_item *branch = branch_list.items + i;
 499                        struct branch_info *info = branch->util;
 500                        int j;
 501
 502                        if (!info->merge.nr || strcmp(*argv, info->remote))
 503                                continue;
 504                        printf("  Remote branch%s merged with 'git pull' "
 505                                "while on branch %s\n   ",
 506                                info->merge.nr > 1 ? "es" : "",
 507                                branch->path);
 508                        for (j = 0; j < info->merge.nr; j++)
 509                                printf(" %s", info->merge.items[j].path);
 510                        printf("\n");
 511                }
 512
 513                if (!no_query) {
 514                        strbuf_init(&buf, 0);
 515                        strbuf_addf(&buf, "  New remote branch%%s (next fetch "
 516                                "will store in remotes/%s)", states.remote->name);
 517                        show_list(buf.buf, &states.new);
 518                        strbuf_release(&buf);
 519                        show_list("  Stale tracking branch%s (use 'git remote "
 520                                "prune')", &states.stale);
 521                }
 522
 523                if (no_query)
 524                        for_each_ref(append_ref_to_tracked_list, &states);
 525                show_list("  Tracked remote branch%s", &states.tracked);
 526
 527                if (states.remote->push_refspec_nr) {
 528                        printf("  Local branch%s pushed with 'git push'\n   ",
 529                                states.remote->push_refspec_nr > 1 ?
 530                                        "es" : "");
 531                        for (i = 0; i < states.remote->push_refspec_nr; i++) {
 532                                struct refspec *spec = states.remote->push + i;
 533                                const char *p = "", *q = "";
 534                                if (spec->src)
 535                                        p = skip_prefix(spec->src, "refs/heads/");
 536                                if (spec->dst)
 537                                        q = skip_prefix(spec->dst, "refs/heads/");
 538                                printf(" %s%s%s%s", spec->force ? "+" : "",
 539                                        p ? p : spec->src,
 540                                        spec->dst ? ":" : "",
 541                                        q ? q : spec->dst);
 542                        }
 543                        printf("\n");
 544                }
 545
 546                /* NEEDSWORK: free remote */
 547                path_list_clear(&states.new, 0);
 548                path_list_clear(&states.stale, 0);
 549                path_list_clear(&states.tracked, 0);
 550        }
 551
 552        return result;
 553}
 554
 555static int prune(int argc, const char **argv)
 556{
 557        int dry_run = 0, result = 0;
 558        struct option options[] = {
 559                OPT_GROUP("prune specific options"),
 560                OPT__DRY_RUN(&dry_run),
 561                OPT_END()
 562        };
 563        struct ref_states states;
 564
 565        argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
 566
 567        if (argc < 1)
 568                usage_with_options(builtin_remote_usage, options);
 569
 570        memset(&states, 0, sizeof(states));
 571        for (; argc; argc--, argv++) {
 572                int i;
 573
 574                get_remote_ref_states(*argv, &states, 1);
 575
 576                if (states.stale.nr) {
 577                        printf("Pruning %s\n", *argv);
 578                        printf("URL: %s\n",
 579                               states.remote->url_nr
 580                               ? states.remote->url[0]
 581                               : "(no URL)");
 582                }
 583
 584                for (i = 0; i < states.stale.nr; i++) {
 585                        const char *refname = states.stale.items[i].util;
 586
 587                        if (!dry_run)
 588                                result |= delete_ref(refname, NULL);
 589
 590                        printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
 591                               skip_prefix(refname, "refs/remotes/"));
 592                }
 593
 594                /* NEEDSWORK: free remote */
 595                path_list_clear(&states.new, 0);
 596                path_list_clear(&states.stale, 0);
 597                path_list_clear(&states.tracked, 0);
 598        }
 599
 600        return result;
 601}
 602
 603static int get_one_remote_for_update(struct remote *remote, void *priv)
 604{
 605        struct path_list *list = priv;
 606        if (!remote->skip_default_update)
 607                path_list_append(xstrdup(remote->name), list);
 608        return 0;
 609}
 610
 611struct remote_group {
 612        const char *name;
 613        struct path_list *list;
 614} remote_group;
 615
 616static int get_remote_group(const char *key, const char *value, void *cb)
 617{
 618        if (!prefixcmp(key, "remotes.") &&
 619                        !strcmp(key + 8, remote_group.name)) {
 620                /* split list by white space */
 621                int space = strcspn(value, " \t\n");
 622                while (*value) {
 623                        if (space > 1)
 624                                path_list_append(xstrndup(value, space),
 625                                                remote_group.list);
 626                        value += space + (value[space] != '\0');
 627                        space = strcspn(value, " \t\n");
 628                }
 629        }
 630
 631        return 0;
 632}
 633
 634static int update(int argc, const char **argv)
 635{
 636        int i, result = 0;
 637        struct path_list list = { NULL, 0, 0, 0 };
 638        static const char *default_argv[] = { NULL, "default", NULL };
 639
 640        if (argc < 2) {
 641                argc = 2;
 642                argv = default_argv;
 643        }
 644
 645        remote_group.list = &list;
 646        for (i = 1; i < argc; i++) {
 647                remote_group.name = argv[i];
 648                result = git_config(get_remote_group, NULL);
 649        }
 650
 651        if (!result && !list.nr  && argc == 2 && !strcmp(argv[1], "default"))
 652                result = for_each_remote(get_one_remote_for_update, &list);
 653
 654        for (i = 0; i < list.nr; i++)
 655                result |= fetch_remote(list.items[i].path);
 656
 657        /* all names were strdup()ed or strndup()ed */
 658        list.strdup_paths = 1;
 659        path_list_clear(&list, 0);
 660
 661        return result;
 662}
 663
 664static int get_one_entry(struct remote *remote, void *priv)
 665{
 666        struct path_list *list = priv;
 667
 668        path_list_append(remote->name, list)->util = remote->url_nr ?
 669                (void *)remote->url[0] : NULL;
 670        if (remote->url_nr > 1)
 671                warning("Remote %s has more than one URL", remote->name);
 672
 673        return 0;
 674}
 675
 676static int show_all(void)
 677{
 678        struct path_list list = { NULL, 0, 0 };
 679        int result = for_each_remote(get_one_entry, &list);
 680
 681        if (!result) {
 682                int i;
 683
 684                sort_path_list(&list);
 685                for (i = 0; i < list.nr; i++) {
 686                        struct path_list_item *item = list.items + i;
 687                        printf("%s%s%s\n", item->path,
 688                                verbose ? "\t" : "",
 689                                verbose && item->util ?
 690                                        (const char *)item->util : "");
 691                }
 692        }
 693        return result;
 694}
 695
 696int cmd_remote(int argc, const char **argv, const char *prefix)
 697{
 698        struct option options[] = {
 699                OPT__VERBOSE(&verbose),
 700                OPT_END()
 701        };
 702        int result;
 703
 704        argc = parse_options(argc, argv, options, builtin_remote_usage,
 705                PARSE_OPT_STOP_AT_NON_OPTION);
 706
 707        if (argc < 1)
 708                result = show_all();
 709        else if (!strcmp(argv[0], "add"))
 710                result = add(argc, argv);
 711        else if (!strcmp(argv[0], "rm"))
 712                result = rm(argc, argv);
 713        else if (!strcmp(argv[0], "show"))
 714                result = show(argc, argv);
 715        else if (!strcmp(argv[0], "prune"))
 716                result = prune(argc, argv);
 717        else if (!strcmp(argv[0], "update"))
 718                result = update(argc, argv);
 719        else {
 720                error("Unknown subcommand: %s", argv[0]);
 721                usage_with_options(builtin_remote_usage, options);
 722        }
 723
 724        return result ? 1 : 0;
 725}