8e433d1bf23e7c31aad43c13501c58bcd035cf2e
   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
  12static 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>...]";
  13
  14static int append, force, tags, no_tags, update_head_ok, verbose, quiet;
  15static char *default_rla = NULL;
  16
  17static void find_merge_config(struct ref *ref_map, struct remote *remote)
  18{
  19        struct ref *rm = ref_map;
  20        struct branch *branch = branch_get(NULL);
  21
  22        for (rm = ref_map; rm; rm = rm->next) {
  23                if (!branch_has_merge_config(branch)) {
  24                        if (remote && remote->fetch &&
  25                            !strcmp(remote->fetch[0].src, rm->name))
  26                                rm->merge = 1;
  27                } else {
  28                        if (branch_merges(branch, rm->name))
  29                                rm->merge = 1;
  30                }
  31        }
  32}
  33
  34static struct ref *get_ref_map(struct transport *transport,
  35                               struct refspec *refs, int ref_count, int tags,
  36                               int *autotags)
  37{
  38        int i;
  39        struct ref *rm;
  40        struct ref *ref_map = NULL;
  41        struct ref **tail = &ref_map;
  42
  43        struct ref *remote_refs = transport_get_remote_refs(transport);
  44
  45        if (ref_count || tags) {
  46                for (i = 0; i < ref_count; i++) {
  47                        get_fetch_map(remote_refs, &refs[i], &tail);
  48                        if (refs[i].dst && refs[i].dst[0])
  49                                *autotags = 1;
  50                }
  51                /* Merge everything on the command line, but not --tags */
  52                for (rm = ref_map; rm; rm = rm->next)
  53                        rm->merge = 1;
  54                if (tags) {
  55                        struct refspec refspec;
  56                        refspec.src = "refs/tags/";
  57                        refspec.dst = "refs/tags/";
  58                        refspec.pattern = 1;
  59                        refspec.force = 0;
  60                        get_fetch_map(remote_refs, &refspec, &tail);
  61                }
  62        } else {
  63                /* Use the defaults */
  64                struct remote *remote = transport->remote;
  65                if (remote->fetch_refspec_nr) {
  66                        for (i = 0; i < remote->fetch_refspec_nr; i++) {
  67                                get_fetch_map(remote_refs, &remote->fetch[i], &tail);
  68                                if (remote->fetch[i].dst &&
  69                                    remote->fetch[i].dst[0])
  70                                        *autotags = 1;
  71                        }
  72                        find_merge_config(ref_map, remote);
  73                } else {
  74                        ref_map = get_remote_ref(remote_refs, "HEAD");
  75
  76                        ref_map->merge = 1;
  77                }
  78        }
  79
  80        return ref_map;
  81}
  82
  83static void show_new(enum object_type type, unsigned char *sha1_new)
  84{
  85        fprintf(stderr, "  %s: %s\n", typename(type),
  86                find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
  87}
  88
  89static int s_update_ref(const char *action,
  90                        struct ref *ref,
  91                        int check_old)
  92{
  93        char msg[1024];
  94        char *rla = getenv("GIT_REFLOG_ACTION");
  95        static struct ref_lock *lock;
  96
  97        if (!rla)
  98                rla = default_rla;
  99        snprintf(msg, sizeof(msg), "%s: %s", rla, action);
 100        lock = lock_any_ref_for_update(ref->name,
 101                                       check_old ? ref->old_sha1 : NULL, 0);
 102        if (!lock)
 103                return 1;
 104        if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
 105                return 1;
 106        return 0;
 107}
 108
 109static int update_local_ref(struct ref *ref,
 110                            const char *note,
 111                            int verbose)
 112{
 113        char oldh[41], newh[41];
 114        struct commit *current = NULL, *updated;
 115        enum object_type type;
 116        struct branch *current_branch = branch_get(NULL);
 117
 118        type = sha1_object_info(ref->new_sha1, NULL);
 119        if (type < 0)
 120                die("object %s not found", sha1_to_hex(ref->new_sha1));
 121
 122        if (!*ref->name) {
 123                /* Not storing */
 124                if (verbose) {
 125                        fprintf(stderr, "* fetched %s\n", note);
 126                        show_new(type, ref->new_sha1);
 127                }
 128                return 0;
 129        }
 130
 131        if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
 132                if (verbose) {
 133                        fprintf(stderr, "* %s: same as %s\n",
 134                                ref->name, note);
 135                        show_new(type, ref->new_sha1);
 136                }
 137                return 0;
 138        }
 139
 140        if (!strcmp(ref->name, current_branch->name) &&
 141            !(update_head_ok || is_bare_repository()) &&
 142            !is_null_sha1(ref->old_sha1)) {
 143                /*
 144                 * If this is the head, and it's not okay to update
 145                 * the head, and the old value of the head isn't empty...
 146                 */
 147                fprintf(stderr,
 148                        " * %s: Cannot fetch into the current branch.\n",
 149                        ref->name);
 150                return 1;
 151        }
 152
 153        if (!is_null_sha1(ref->old_sha1) &&
 154            !prefixcmp(ref->name, "refs/tags/")) {
 155                fprintf(stderr, "* %s: updating with %s\n",
 156                        ref->name, note);
 157                show_new(type, ref->new_sha1);
 158                return s_update_ref("updating tag", ref, 0);
 159        }
 160
 161        current = lookup_commit_reference(ref->old_sha1);
 162        updated = lookup_commit_reference(ref->new_sha1);
 163        if (!current || !updated) {
 164                char *msg;
 165                if (!strncmp(ref->name, "refs/tags/", 10))
 166                        msg = "storing tag";
 167                else
 168                        msg = "storing head";
 169                fprintf(stderr, "* %s: storing %s\n",
 170                        ref->name, note);
 171                show_new(type, ref->new_sha1);
 172                return s_update_ref(msg, ref, 0);
 173        }
 174
 175        strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
 176        strcpy(newh, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
 177
 178        if (in_merge_bases(current, &updated, 1)) {
 179                fprintf(stderr, "* %s: fast forward to %s\n",
 180                        ref->name, note);
 181                fprintf(stderr, "  old..new: %s..%s\n", oldh, newh);
 182                return s_update_ref("fast forward", ref, 1);
 183        }
 184        if (!force && !ref->force) {
 185                fprintf(stderr,
 186                        "* %s: not updating to non-fast forward %s\n",
 187                        ref->name, note);
 188                fprintf(stderr,
 189                        "  old...new: %s...%s\n", oldh, newh);
 190                return 1;
 191        }
 192        fprintf(stderr,
 193                "* %s: forcing update to non-fast forward %s\n",
 194                ref->name, note);
 195        fprintf(stderr, "  old...new: %s...%s\n", oldh, newh);
 196        return s_update_ref("forced-update", ref, 1);
 197}
 198
 199static void store_updated_refs(const char *url, struct ref *ref_map)
 200{
 201        FILE *fp;
 202        struct commit *commit;
 203        int url_len, i, note_len;
 204        char note[1024];
 205        const char *what, *kind;
 206        struct ref *rm;
 207
 208        fp = fopen(git_path("FETCH_HEAD"), "a");
 209        for (rm = ref_map; rm; rm = rm->next) {
 210                struct ref *ref = NULL;
 211
 212                if (rm->peer_ref) {
 213                        ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
 214                        strcpy(ref->name, rm->peer_ref->name);
 215                        hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
 216                        hashcpy(ref->new_sha1, rm->old_sha1);
 217                        ref->force = rm->peer_ref->force;
 218                }
 219
 220                commit = lookup_commit_reference(rm->old_sha1);
 221                if (!commit)
 222                        rm->merge = 0;
 223
 224                if (!strcmp(rm->name, "HEAD")) {
 225                        kind = "";
 226                        what = "";
 227                }
 228                else if (!prefixcmp(rm->name, "refs/heads/")) {
 229                        kind = "branch";
 230                        what = rm->name + 11;
 231                }
 232                else if (!prefixcmp(rm->name, "refs/tags/")) {
 233                        kind = "tag";
 234                        what = rm->name + 10;
 235                }
 236                else if (!prefixcmp(rm->name, "refs/remotes/")) {
 237                        kind = "remote branch";
 238                        what = rm->name + 13;
 239                }
 240                else {
 241                        kind = "";
 242                        what = rm->name;
 243                }
 244
 245                url_len = strlen(url);
 246                for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
 247                        ;
 248                url_len = i + 1;
 249                if (4 < i && !strncmp(".git", url + i - 3, 4))
 250                        url_len = i - 3;
 251
 252                note_len = 0;
 253                if (*what) {
 254                        if (*kind)
 255                                note_len += sprintf(note + note_len, "%s ",
 256                                                    kind);
 257                        note_len += sprintf(note + note_len, "'%s' of ", what);
 258                }
 259                note_len += sprintf(note + note_len, "%.*s", url_len, url);
 260                fprintf(fp, "%s\t%s\t%s\n",
 261                        sha1_to_hex(commit ? commit->object.sha1 :
 262                                    rm->old_sha1),
 263                        rm->merge ? "" : "not-for-merge",
 264                        note);
 265
 266                if (ref)
 267                        update_local_ref(ref, note, verbose);
 268        }
 269        fclose(fp);
 270}
 271
 272static int fetch_refs(struct transport *transport, struct ref *ref_map)
 273{
 274        int ret = transport_fetch_refs(transport, ref_map);
 275        if (!ret)
 276                store_updated_refs(transport->url, ref_map);
 277        transport_unlock_pack(transport);
 278        return ret;
 279}
 280
 281static int add_existing(const char *refname, const unsigned char *sha1,
 282                        int flag, void *cbdata)
 283{
 284        struct path_list *list = (struct path_list *)cbdata;
 285        path_list_insert(refname, list);
 286        return 0;
 287}
 288
 289static struct ref *find_non_local_tags(struct transport *transport,
 290                                       struct ref *fetch_map)
 291{
 292        static struct path_list existing_refs = { NULL, 0, 0, 0 };
 293        struct path_list new_refs = { NULL, 0, 0, 1 };
 294        char *ref_name;
 295        int ref_name_len;
 296        unsigned char *ref_sha1;
 297        struct ref *tag_ref;
 298        struct ref *rm = NULL;
 299        struct ref *ref_map = NULL;
 300        struct ref **tail = &ref_map;
 301        struct ref *ref;
 302
 303        for_each_ref(add_existing, &existing_refs);
 304        for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
 305                if (prefixcmp(ref->name, "refs/tags"))
 306                        continue;
 307
 308                ref_name = xstrdup(ref->name);
 309                ref_name_len = strlen(ref_name);
 310                ref_sha1 = ref->old_sha1;
 311
 312                if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
 313                        ref_name[ref_name_len - 3] = 0;
 314                        tag_ref = transport_get_remote_refs(transport);
 315                        while (tag_ref) {
 316                                if (!strcmp(tag_ref->name, ref_name)) {
 317                                        ref_sha1 = tag_ref->old_sha1;
 318                                        break;
 319                                }
 320                                tag_ref = tag_ref->next;
 321                        }
 322                }
 323
 324                if (!path_list_has_path(&existing_refs, ref_name) &&
 325                    !path_list_has_path(&new_refs, ref_name) &&
 326                    lookup_object(ref->old_sha1)) {
 327                        fprintf(stderr, "Auto-following %s\n",
 328                                ref_name);
 329
 330                        path_list_insert(ref_name, &new_refs);
 331
 332                        rm = alloc_ref(strlen(ref_name) + 1);
 333                        strcpy(rm->name, ref_name);
 334                        rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
 335                        strcpy(rm->peer_ref->name, ref_name);
 336                        hashcpy(rm->old_sha1, ref_sha1);
 337
 338                        *tail = rm;
 339                        tail = &rm->next;
 340                }
 341                free(ref_name);
 342        }
 343
 344        return ref_map;
 345}
 346
 347static int do_fetch(struct transport *transport,
 348                    struct refspec *refs, int ref_count)
 349{
 350        struct ref *ref_map, *fetch_map;
 351        struct ref *rm;
 352        int autotags = (transport->remote->fetch_tags == 1);
 353        if (transport->remote->fetch_tags == 2 && !no_tags)
 354                tags = 1;
 355        if (transport->remote->fetch_tags == -1)
 356                no_tags = 1;
 357
 358        if (!transport->ops || !transport->ops->get_refs_list ||
 359            !transport->ops->fetch)
 360                die("Don't know how to fetch from %s", transport->url);
 361
 362        /* if not appending, truncate FETCH_HEAD */
 363        if (!append)
 364                fclose(fopen(git_path("FETCH_HEAD"), "w"));
 365
 366        ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
 367
 368        for (rm = ref_map; rm; rm = rm->next) {
 369                if (rm->peer_ref)
 370                        read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
 371        }
 372
 373        if (fetch_refs(transport, ref_map)) {
 374                free_refs(ref_map);
 375                return 1;
 376        }
 377
 378        fetch_map = ref_map;
 379
 380        /* if neither --no-tags nor --tags was specified, do automated tag
 381         * following ... */
 382        if (!(tags || no_tags) && autotags) {
 383                ref_map = find_non_local_tags(transport, fetch_map);
 384                if (ref_map) {
 385                        transport_set_option(transport, TRANS_OPT_DEPTH, "0");
 386                        fetch_refs(transport, ref_map);
 387                }
 388                free_refs(ref_map);
 389        }
 390
 391        free_refs(fetch_map);
 392
 393        return 0;
 394}
 395
 396int cmd_fetch(int argc, const char **argv, const char *prefix)
 397{
 398        struct remote *remote;
 399        struct transport *transport;
 400        int i, j, rla_offset;
 401        static const char **refs = NULL;
 402        int ref_nr = 0;
 403        int cmd_len = 0;
 404        const char *depth = NULL, *upload_pack = NULL;
 405        int keep = 0;
 406
 407        for (i = 1; i < argc; i++) {
 408                const char *arg = argv[i];
 409                cmd_len += strlen(arg);
 410
 411                if (arg[0] != '-')
 412                        break;
 413                if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
 414                        append = 1;
 415                        continue;
 416                }
 417                if (!prefixcmp(arg, "--upload-pack=")) {
 418                        upload_pack = arg + 14;
 419                        continue;
 420                }
 421                if (!strcmp(arg, "--upload-pack")) {
 422                        i++;
 423                        if (i == argc)
 424                                usage(fetch_usage);
 425                        upload_pack = argv[i];
 426                        continue;
 427                }
 428                if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
 429                        force = 1;
 430                        continue;
 431                }
 432                if (!strcmp(arg, "--no-tags")) {
 433                        no_tags = 1;
 434                        continue;
 435                }
 436                if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
 437                        tags = 1;
 438                        continue;
 439                }
 440                if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
 441                        keep = 1;
 442                        continue;
 443                }
 444                if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
 445                        update_head_ok = 1;
 446                        continue;
 447                }
 448                if (!prefixcmp(arg, "--depth=")) {
 449                        depth = arg + 8;
 450                        continue;
 451                }
 452                if (!strcmp(arg, "--depth")) {
 453                        i++;
 454                        if (i == argc)
 455                                usage(fetch_usage);
 456                        depth = argv[i];
 457                        continue;
 458                }
 459                if (!strcmp(arg, "--quiet")) {
 460                        quiet = 1;
 461                        continue;
 462                }
 463                if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
 464                        verbose++;
 465                        continue;
 466                }
 467                usage(fetch_usage);
 468        }
 469
 470        for (j = i; j < argc; j++)
 471                cmd_len += strlen(argv[j]);
 472
 473        default_rla = xmalloc(cmd_len + 5 + argc + 1);
 474        sprintf(default_rla, "fetch");
 475        rla_offset = strlen(default_rla);
 476        for (j = 1; j < argc; j++) {
 477                sprintf(default_rla + rla_offset, " %s", argv[j]);
 478                rla_offset += strlen(argv[j]) + 1;
 479        }
 480
 481        if (i == argc)
 482                remote = remote_get(NULL);
 483        else
 484                remote = remote_get(argv[i++]);
 485
 486        transport = transport_get(remote, remote->uri[0], 1);
 487        if (verbose >= 2)
 488                transport->verbose = 1;
 489        if (quiet)
 490                transport->verbose = 0;
 491        if (upload_pack)
 492                transport_set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
 493        if (keep)
 494                transport_set_option(transport, TRANS_OPT_KEEP, "yes");
 495        transport_set_option(transport, TRANS_OPT_DEPTH, depth);
 496
 497        if (!transport->url)
 498                die("Where do you want to fetch from today?");
 499
 500        if (i < argc) {
 501                int j = 0;
 502                refs = xcalloc(argc - i + 1, sizeof(const char *));
 503                while (i < argc) {
 504                        if (!strcmp(argv[i], "tag")) {
 505                                char *ref;
 506                                i++;
 507                                ref = xmalloc(strlen(argv[i]) * 2 + 22);
 508                                strcpy(ref, "refs/tags/");
 509                                strcat(ref, argv[i]);
 510                                strcat(ref, ":refs/tags/");
 511                                strcat(ref, argv[i]);
 512                                refs[j++] = ref;
 513                        } else
 514                                refs[j++] = argv[i];
 515                        i++;
 516                }
 517                refs[j] = NULL;
 518                ref_nr = j;
 519                for (j = 0; refs[j]; j++)
 520                        printf("ref: %s\n", refs[j]);
 521        }
 522
 523        return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);
 524}