builtin-fetch.con commit git-cvsserver runs hooks/post-receive (cdf6328)
   1/*
   2 * "git fetch"
   3 */
   4#include "cache.h"
   5#include "refs.h"
   6#include "commit.h"
   7#include "builtin.h"
   8#include "path-list.h"
   9#include "remote.h"
  10#include "transport.h"
  11#include "run-command.h"
  12
  13static const char fetch_usage[] = "git-fetch [-a | --append] [--upload-pack <upload-pack>] [-f | --force] [--no-tags] [-t | --tags] [-k | --keep] [-u | --update-head-ok] [--depth <depth>] [-v | --verbose] [<repository> <refspec>...]";
  14
  15static int append, force, tags, no_tags, update_head_ok, verbose, quiet;
  16static const char *depth;
  17static struct strbuf default_rla = STRBUF_INIT;
  18static struct transport *transport;
  19
  20static void unlock_pack(void)
  21{
  22        if (transport)
  23                transport_unlock_pack(transport);
  24}
  25
  26static void unlock_pack_on_signal(int signo)
  27{
  28        unlock_pack();
  29        signal(SIGINT, SIG_DFL);
  30        raise(signo);
  31}
  32
  33static void add_merge_config(struct ref **head,
  34                           const struct ref *remote_refs,
  35                           struct branch *branch,
  36                           struct ref ***tail)
  37{
  38        int i;
  39
  40        for (i = 0; i < branch->merge_nr; i++) {
  41                struct ref *rm, **old_tail = *tail;
  42                struct refspec refspec;
  43
  44                for (rm = *head; rm; rm = rm->next) {
  45                        if (branch_merge_matches(branch, i, rm->name)) {
  46                                rm->merge = 1;
  47                                break;
  48                        }
  49                }
  50                if (rm)
  51                        continue;
  52
  53                /*
  54                 * Not fetched to a tracking branch?  We need to fetch
  55                 * it anyway to allow this branch's "branch.$name.merge"
  56                 * to be honored by git-pull, but we do not have to
  57                 * fail if branch.$name.merge is misconfigured to point
  58                 * at a nonexisting branch.  If we were indeed called by
  59                 * git-pull, it will notice the misconfiguration because
  60                 * there is no entry in the resulting FETCH_HEAD marked
  61                 * for merging.
  62                 */
  63                refspec.src = branch->merge[i]->src;
  64                refspec.dst = NULL;
  65                refspec.pattern = 0;
  66                refspec.force = 0;
  67                get_fetch_map(remote_refs, &refspec, tail, 1);
  68                for (rm = *old_tail; rm; rm = rm->next)
  69                        rm->merge = 1;
  70        }
  71}
  72
  73static struct ref *get_ref_map(struct transport *transport,
  74                               struct refspec *refs, int ref_count, int tags,
  75                               int *autotags)
  76{
  77        int i;
  78        struct ref *rm;
  79        struct ref *ref_map = NULL;
  80        struct ref **tail = &ref_map;
  81
  82        const struct ref *remote_refs = transport_get_remote_refs(transport);
  83
  84        if (ref_count || tags) {
  85                for (i = 0; i < ref_count; i++) {
  86                        get_fetch_map(remote_refs, &refs[i], &tail, 0);
  87                        if (refs[i].dst && refs[i].dst[0])
  88                                *autotags = 1;
  89                }
  90                /* Merge everything on the command line, but not --tags */
  91                for (rm = ref_map; rm; rm = rm->next)
  92                        rm->merge = 1;
  93                if (tags) {
  94                        struct refspec refspec;
  95                        refspec.src = "refs/tags/";
  96                        refspec.dst = "refs/tags/";
  97                        refspec.pattern = 1;
  98                        refspec.force = 0;
  99                        get_fetch_map(remote_refs, &refspec, &tail, 0);
 100                }
 101        } else {
 102                /* Use the defaults */
 103                struct remote *remote = transport->remote;
 104                struct branch *branch = branch_get(NULL);
 105                int has_merge = branch_has_merge_config(branch);
 106                if (remote && (remote->fetch_refspec_nr || has_merge)) {
 107                        for (i = 0; i < remote->fetch_refspec_nr; i++) {
 108                                get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
 109                                if (remote->fetch[i].dst &&
 110                                    remote->fetch[i].dst[0])
 111                                        *autotags = 1;
 112                                if (!i && !has_merge && ref_map &&
 113                                    !remote->fetch[0].pattern)
 114                                        ref_map->merge = 1;
 115                        }
 116                        /*
 117                         * if the remote we're fetching from is the same
 118                         * as given in branch.<name>.remote, we add the
 119                         * ref given in branch.<name>.merge, too.
 120                         */
 121                        if (has_merge &&
 122                            !strcmp(branch->remote_name, remote->name))
 123                                add_merge_config(&ref_map, remote_refs, branch, &tail);
 124                } else {
 125                        ref_map = get_remote_ref(remote_refs, "HEAD");
 126                        if (!ref_map)
 127                                die("Couldn't find remote ref HEAD");
 128                        ref_map->merge = 1;
 129                }
 130        }
 131        ref_remove_duplicates(ref_map);
 132
 133        return ref_map;
 134}
 135
 136static int s_update_ref(const char *action,
 137                        struct ref *ref,
 138                        int check_old)
 139{
 140        char msg[1024];
 141        char *rla = getenv("GIT_REFLOG_ACTION");
 142        static struct ref_lock *lock;
 143
 144        if (!rla)
 145                rla = default_rla.buf;
 146        snprintf(msg, sizeof(msg), "%s: %s", rla, action);
 147        lock = lock_any_ref_for_update(ref->name,
 148                                       check_old ? ref->old_sha1 : NULL, 0);
 149        if (!lock)
 150                return 1;
 151        if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
 152                return 1;
 153        return 0;
 154}
 155
 156#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
 157#define REFCOL_WIDTH  10
 158
 159static int update_local_ref(struct ref *ref,
 160                            const char *remote,
 161                            int verbose,
 162                            char *display)
 163{
 164        struct commit *current = NULL, *updated;
 165        enum object_type type;
 166        struct branch *current_branch = branch_get(NULL);
 167        const char *pretty_ref = ref->name + (
 168                !prefixcmp(ref->name, "refs/heads/") ? 11 :
 169                !prefixcmp(ref->name, "refs/tags/") ? 10 :
 170                !prefixcmp(ref->name, "refs/remotes/") ? 13 :
 171                0);
 172
 173        *display = 0;
 174        type = sha1_object_info(ref->new_sha1, NULL);
 175        if (type < 0)
 176                die("object %s not found", sha1_to_hex(ref->new_sha1));
 177
 178        if (!*ref->name) {
 179                /* Not storing */
 180                if (verbose)
 181                        sprintf(display, "* branch %s -> FETCH_HEAD", remote);
 182                return 0;
 183        }
 184
 185        if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
 186                if (verbose)
 187                        sprintf(display, "= %-*s %-*s -> %s", SUMMARY_WIDTH,
 188                                "[up to date]", REFCOL_WIDTH, remote,
 189                                pretty_ref);
 190                return 0;
 191        }
 192
 193        if (current_branch &&
 194            !strcmp(ref->name, current_branch->name) &&
 195            !(update_head_ok || is_bare_repository()) &&
 196            !is_null_sha1(ref->old_sha1)) {
 197                /*
 198                 * If this is the head, and it's not okay to update
 199                 * the head, and the old value of the head isn't empty...
 200                 */
 201                sprintf(display, "! %-*s %-*s -> %s  (can't fetch in current branch)",
 202                        SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
 203                        pretty_ref);
 204                return 1;
 205        }
 206
 207        if (!is_null_sha1(ref->old_sha1) &&
 208            !prefixcmp(ref->name, "refs/tags/")) {
 209                sprintf(display, "- %-*s %-*s -> %s",
 210                        SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote,
 211                        pretty_ref);
 212                return s_update_ref("updating tag", ref, 0);
 213        }
 214
 215        current = lookup_commit_reference_gently(ref->old_sha1, 1);
 216        updated = lookup_commit_reference_gently(ref->new_sha1, 1);
 217        if (!current || !updated) {
 218                const char *msg;
 219                const char *what;
 220                if (!strncmp(ref->name, "refs/tags/", 10)) {
 221                        msg = "storing tag";
 222                        what = "[new tag]";
 223                }
 224                else {
 225                        msg = "storing head";
 226                        what = "[new branch]";
 227                }
 228
 229                sprintf(display, "* %-*s %-*s -> %s", SUMMARY_WIDTH, what,
 230                        REFCOL_WIDTH, remote, pretty_ref);
 231                return s_update_ref(msg, ref, 0);
 232        }
 233
 234        if (in_merge_bases(current, &updated, 1)) {
 235                char quickref[83];
 236                strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
 237                strcat(quickref, "..");
 238                strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
 239                sprintf(display, "  %-*s %-*s -> %s", SUMMARY_WIDTH, quickref,
 240                        REFCOL_WIDTH, remote, pretty_ref);
 241                return s_update_ref("fast forward", ref, 1);
 242        } else if (force || ref->force) {
 243                char quickref[84];
 244                strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
 245                strcat(quickref, "...");
 246                strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
 247                sprintf(display, "+ %-*s %-*s -> %s  (forced update)",
 248                        SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote, pretty_ref);
 249                return s_update_ref("forced-update", ref, 1);
 250        } else {
 251                sprintf(display, "! %-*s %-*s -> %s  (non fast forward)",
 252                        SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
 253                        pretty_ref);
 254                return 1;
 255        }
 256}
 257
 258static int store_updated_refs(const char *url, struct ref *ref_map)
 259{
 260        FILE *fp;
 261        struct commit *commit;
 262        int url_len, i, note_len, shown_url = 0;
 263        char note[1024];
 264        const char *what, *kind;
 265        struct ref *rm;
 266        char *filename = git_path("FETCH_HEAD");
 267
 268        fp = fopen(filename, "a");
 269        if (!fp)
 270                return error("cannot open %s: %s\n", filename, strerror(errno));
 271        for (rm = ref_map; rm; rm = rm->next) {
 272                struct ref *ref = NULL;
 273
 274                if (rm->peer_ref) {
 275                        ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
 276                        strcpy(ref->name, rm->peer_ref->name);
 277                        hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
 278                        hashcpy(ref->new_sha1, rm->old_sha1);
 279                        ref->force = rm->peer_ref->force;
 280                }
 281
 282                commit = lookup_commit_reference_gently(rm->old_sha1, 1);
 283                if (!commit)
 284                        rm->merge = 0;
 285
 286                if (!strcmp(rm->name, "HEAD")) {
 287                        kind = "";
 288                        what = "";
 289                }
 290                else if (!prefixcmp(rm->name, "refs/heads/")) {
 291                        kind = "branch";
 292                        what = rm->name + 11;
 293                }
 294                else if (!prefixcmp(rm->name, "refs/tags/")) {
 295                        kind = "tag";
 296                        what = rm->name + 10;
 297                }
 298                else if (!prefixcmp(rm->name, "refs/remotes/")) {
 299                        kind = "remote branch";
 300                        what = rm->name + 13;
 301                }
 302                else {
 303                        kind = "";
 304                        what = rm->name;
 305                }
 306
 307                url_len = strlen(url);
 308                for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
 309                        ;
 310                url_len = i + 1;
 311                if (4 < i && !strncmp(".git", url + i - 3, 4))
 312                        url_len = i - 3;
 313
 314                note_len = 0;
 315                if (*what) {
 316                        if (*kind)
 317                                note_len += sprintf(note + note_len, "%s ",
 318                                                    kind);
 319                        note_len += sprintf(note + note_len, "'%s' of ", what);
 320                }
 321                note_len += sprintf(note + note_len, "%.*s", url_len, url);
 322                fprintf(fp, "%s\t%s\t%s\n",
 323                        sha1_to_hex(commit ? commit->object.sha1 :
 324                                    rm->old_sha1),
 325                        rm->merge ? "" : "not-for-merge",
 326                        note);
 327
 328                if (ref) {
 329                        update_local_ref(ref, what, verbose, note);
 330                        if (*note) {
 331                                if (!shown_url) {
 332                                        fprintf(stderr, "From %.*s\n",
 333                                                        url_len, url);
 334                                        shown_url = 1;
 335                                }
 336                                fprintf(stderr, " %s\n", note);
 337                        }
 338                }
 339        }
 340        fclose(fp);
 341        return 0;
 342}
 343
 344/*
 345 * We would want to bypass the object transfer altogether if
 346 * everything we are going to fetch already exists and connected
 347 * locally.
 348 *
 349 * The refs we are going to fetch are in to_fetch (nr_heads in
 350 * total).  If running
 351 *
 352 *  $ git-rev-list --objects to_fetch[0] to_fetch[1] ... --not --all
 353 *
 354 * does not error out, that means everything reachable from the
 355 * refs we are going to fetch exists and is connected to some of
 356 * our existing refs.
 357 */
 358static int quickfetch(struct ref *ref_map)
 359{
 360        struct child_process revlist;
 361        struct ref *ref;
 362        char **argv;
 363        int i, err;
 364
 365        /*
 366         * If we are deepening a shallow clone we already have these
 367         * objects reachable.  Running rev-list here will return with
 368         * a good (0) exit status and we'll bypass the fetch that we
 369         * really need to perform.  Claiming failure now will ensure
 370         * we perform the network exchange to deepen our history.
 371         */
 372        if (depth)
 373                return -1;
 374
 375        for (i = 0, ref = ref_map; ref; ref = ref->next)
 376                i++;
 377        if (!i)
 378                return 0;
 379
 380        argv = xmalloc(sizeof(*argv) * (i + 6));
 381        i = 0;
 382        argv[i++] = xstrdup("rev-list");
 383        argv[i++] = xstrdup("--quiet");
 384        argv[i++] = xstrdup("--objects");
 385        for (ref = ref_map; ref; ref = ref->next)
 386                argv[i++] = xstrdup(sha1_to_hex(ref->old_sha1));
 387        argv[i++] = xstrdup("--not");
 388        argv[i++] = xstrdup("--all");
 389        argv[i++] = NULL;
 390
 391        memset(&revlist, 0, sizeof(revlist));
 392        revlist.argv = (const char**)argv;
 393        revlist.git_cmd = 1;
 394        revlist.no_stdin = 1;
 395        revlist.no_stdout = 1;
 396        revlist.no_stderr = 1;
 397        err = run_command(&revlist);
 398
 399        for (i = 0; argv[i]; i++)
 400                free(argv[i]);
 401        free(argv);
 402        return err;
 403}
 404
 405static int fetch_refs(struct transport *transport, struct ref *ref_map)
 406{
 407        int ret = quickfetch(ref_map);
 408        if (ret)
 409                ret = transport_fetch_refs(transport, ref_map);
 410        if (!ret)
 411                ret |= store_updated_refs(transport->url, ref_map);
 412        transport_unlock_pack(transport);
 413        return ret;
 414}
 415
 416static int add_existing(const char *refname, const unsigned char *sha1,
 417                        int flag, void *cbdata)
 418{
 419        struct path_list *list = (struct path_list *)cbdata;
 420        path_list_insert(refname, list);
 421        return 0;
 422}
 423
 424static struct ref *find_non_local_tags(struct transport *transport,
 425                                       struct ref *fetch_map)
 426{
 427        static struct path_list existing_refs = { NULL, 0, 0, 0 };
 428        struct path_list new_refs = { NULL, 0, 0, 1 };
 429        char *ref_name;
 430        int ref_name_len;
 431        const unsigned char *ref_sha1;
 432        const struct ref *tag_ref;
 433        struct ref *rm = NULL;
 434        struct ref *ref_map = NULL;
 435        struct ref **tail = &ref_map;
 436        const struct ref *ref;
 437
 438        for_each_ref(add_existing, &existing_refs);
 439        for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
 440                if (prefixcmp(ref->name, "refs/tags"))
 441                        continue;
 442
 443                ref_name = xstrdup(ref->name);
 444                ref_name_len = strlen(ref_name);
 445                ref_sha1 = ref->old_sha1;
 446
 447                if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
 448                        ref_name[ref_name_len - 3] = 0;
 449                        tag_ref = transport_get_remote_refs(transport);
 450                        while (tag_ref) {
 451                                if (!strcmp(tag_ref->name, ref_name)) {
 452                                        ref_sha1 = tag_ref->old_sha1;
 453                                        break;
 454                                }
 455                                tag_ref = tag_ref->next;
 456                        }
 457                }
 458
 459                if (!path_list_has_path(&existing_refs, ref_name) &&
 460                    !path_list_has_path(&new_refs, ref_name) &&
 461                    has_sha1_file(ref->old_sha1)) {
 462                        path_list_insert(ref_name, &new_refs);
 463
 464                        rm = alloc_ref(strlen(ref_name) + 1);
 465                        strcpy(rm->name, ref_name);
 466                        rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
 467                        strcpy(rm->peer_ref->name, ref_name);
 468                        hashcpy(rm->old_sha1, ref_sha1);
 469
 470                        *tail = rm;
 471                        tail = &rm->next;
 472                }
 473                free(ref_name);
 474        }
 475
 476        return ref_map;
 477}
 478
 479static int do_fetch(struct transport *transport,
 480                    struct refspec *refs, int ref_count)
 481{
 482        struct ref *ref_map, *fetch_map;
 483        struct ref *rm;
 484        int autotags = (transport->remote->fetch_tags == 1);
 485        if (transport->remote->fetch_tags == 2 && !no_tags)
 486                tags = 1;
 487        if (transport->remote->fetch_tags == -1)
 488                no_tags = 1;
 489
 490        if (!transport->get_refs_list || !transport->fetch)
 491                die("Don't know how to fetch from %s", transport->url);
 492
 493        /* if not appending, truncate FETCH_HEAD */
 494        if (!append) {
 495                char *filename = git_path("FETCH_HEAD");
 496                FILE *fp = fopen(filename, "w");
 497                if (!fp)
 498                        return error("cannot open %s: %s\n", filename, strerror(errno));
 499                fclose(fp);
 500        }
 501
 502        ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
 503
 504        for (rm = ref_map; rm; rm = rm->next) {
 505                if (rm->peer_ref)
 506                        read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
 507        }
 508
 509        if (fetch_refs(transport, ref_map)) {
 510                free_refs(ref_map);
 511                return 1;
 512        }
 513
 514        fetch_map = ref_map;
 515
 516        /* if neither --no-tags nor --tags was specified, do automated tag
 517         * following ... */
 518        if (!(tags || no_tags) && autotags) {
 519                ref_map = find_non_local_tags(transport, fetch_map);
 520                if (ref_map) {
 521                        transport_set_option(transport, TRANS_OPT_DEPTH, "0");
 522                        fetch_refs(transport, ref_map);
 523                }
 524                free_refs(ref_map);
 525        }
 526
 527        free_refs(fetch_map);
 528
 529        return 0;
 530}
 531
 532static void set_option(const char *name, const char *value)
 533{
 534        int r = transport_set_option(transport, name, value);
 535        if (r < 0)
 536                die("Option \"%s\" value \"%s\" is not valid for %s\n",
 537                        name, value, transport->url);
 538        if (r > 0)
 539                warning("Option \"%s\" is ignored for %s\n",
 540                        name, transport->url);
 541}
 542
 543int cmd_fetch(int argc, const char **argv, const char *prefix)
 544{
 545        struct remote *remote;
 546        int i;
 547        static const char **refs = NULL;
 548        int ref_nr = 0;
 549        const char *upload_pack = NULL;
 550        int keep = 0;
 551
 552        /* Record the command line for the reflog */
 553        strbuf_addstr(&default_rla, "fetch");
 554        for (i = 1; i < argc; i++)
 555                strbuf_addf(&default_rla, " %s", argv[i]);
 556
 557        for (i = 1; i < argc; i++) {
 558                const char *arg = argv[i];
 559
 560                if (arg[0] != '-')
 561                        break;
 562                if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
 563                        append = 1;
 564                        continue;
 565                }
 566                if (!prefixcmp(arg, "--upload-pack=")) {
 567                        upload_pack = arg + 14;
 568                        continue;
 569                }
 570                if (!strcmp(arg, "--upload-pack")) {
 571                        i++;
 572                        if (i == argc)
 573                                usage(fetch_usage);
 574                        upload_pack = argv[i];
 575                        continue;
 576                }
 577                if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
 578                        force = 1;
 579                        continue;
 580                }
 581                if (!strcmp(arg, "--no-tags")) {
 582                        no_tags = 1;
 583                        continue;
 584                }
 585                if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
 586                        tags = 1;
 587                        continue;
 588                }
 589                if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
 590                        keep = 1;
 591                        continue;
 592                }
 593                if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
 594                        update_head_ok = 1;
 595                        continue;
 596                }
 597                if (!prefixcmp(arg, "--depth=")) {
 598                        depth = arg + 8;
 599                        continue;
 600                }
 601                if (!strcmp(arg, "--depth")) {
 602                        i++;
 603                        if (i == argc)
 604                                usage(fetch_usage);
 605                        depth = argv[i];
 606                        continue;
 607                }
 608                if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
 609                        quiet = 1;
 610                        continue;
 611                }
 612                if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
 613                        verbose++;
 614                        continue;
 615                }
 616                usage(fetch_usage);
 617        }
 618
 619        if (i == argc)
 620                remote = remote_get(NULL);
 621        else
 622                remote = remote_get(argv[i++]);
 623
 624        transport = transport_get(remote, remote->url[0]);
 625        if (verbose >= 2)
 626                transport->verbose = 1;
 627        if (quiet)
 628                transport->verbose = -1;
 629        if (upload_pack)
 630                set_option(TRANS_OPT_UPLOADPACK, upload_pack);
 631        if (keep)
 632                set_option(TRANS_OPT_KEEP, "yes");
 633        if (depth)
 634                set_option(TRANS_OPT_DEPTH, depth);
 635
 636        if (!transport->url)
 637                die("Where do you want to fetch from today?");
 638
 639        if (i < argc) {
 640                int j = 0;
 641                refs = xcalloc(argc - i + 1, sizeof(const char *));
 642                while (i < argc) {
 643                        if (!strcmp(argv[i], "tag")) {
 644                                char *ref;
 645                                i++;
 646                                ref = xmalloc(strlen(argv[i]) * 2 + 22);
 647                                strcpy(ref, "refs/tags/");
 648                                strcat(ref, argv[i]);
 649                                strcat(ref, ":refs/tags/");
 650                                strcat(ref, argv[i]);
 651                                refs[j++] = ref;
 652                        } else
 653                                refs[j++] = argv[i];
 654                        i++;
 655                }
 656                refs[j] = NULL;
 657                ref_nr = j;
 658        }
 659
 660        signal(SIGINT, unlock_pack_on_signal);
 661        atexit(unlock_pack);
 662        return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);
 663}