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