builtin-fetch.con commit Remove unused unpacklimit variable from builtin-fetch (f1ae391)
   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        return ret;
 278}
 279
 280static int add_existing(const char *refname, const unsigned char *sha1,
 281                        int flag, void *cbdata)
 282{
 283        struct path_list *list = (struct path_list *)cbdata;
 284        path_list_insert(refname, list);
 285        return 0;
 286}
 287
 288static struct ref *find_non_local_tags(struct transport *transport,
 289                                       struct ref *fetch_map)
 290{
 291        static struct path_list existing_refs = { NULL, 0, 0, 0 };
 292        struct path_list new_refs = { NULL, 0, 0, 1 };
 293        char *ref_name;
 294        int ref_name_len;
 295        unsigned char *ref_sha1;
 296        struct ref *tag_ref;
 297        struct ref *rm = NULL;
 298        struct ref *ref_map = NULL;
 299        struct ref **tail = &ref_map;
 300        struct ref *ref;
 301
 302        for_each_ref(add_existing, &existing_refs);
 303        for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
 304                if (prefixcmp(ref->name, "refs/tags"))
 305                        continue;
 306
 307                ref_name = xstrdup(ref->name);
 308                ref_name_len = strlen(ref_name);
 309                ref_sha1 = ref->old_sha1;
 310
 311                if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
 312                        ref_name[ref_name_len - 3] = 0;
 313                        tag_ref = transport_get_remote_refs(transport);
 314                        while (tag_ref) {
 315                                if (!strcmp(tag_ref->name, ref_name)) {
 316                                        ref_sha1 = tag_ref->old_sha1;
 317                                        break;
 318                                }
 319                                tag_ref = tag_ref->next;
 320                        }
 321                }
 322
 323                if (!path_list_has_path(&existing_refs, ref_name) &&
 324                    !path_list_has_path(&new_refs, ref_name) &&
 325                    lookup_object(ref->old_sha1)) {
 326                        fprintf(stderr, "Auto-following %s\n",
 327                                ref_name);
 328
 329                        path_list_insert(ref_name, &new_refs);
 330
 331                        rm = alloc_ref(strlen(ref_name) + 1);
 332                        strcpy(rm->name, ref_name);
 333                        rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
 334                        strcpy(rm->peer_ref->name, ref_name);
 335                        hashcpy(rm->old_sha1, ref_sha1);
 336
 337                        *tail = rm;
 338                        tail = &rm->next;
 339                }
 340                free(ref_name);
 341        }
 342
 343        return ref_map;
 344}
 345
 346static int do_fetch(struct transport *transport,
 347                    struct refspec *refs, int ref_count)
 348{
 349        struct ref *ref_map, *fetch_map;
 350        struct ref *rm;
 351        int autotags = (transport->remote->fetch_tags == 1);
 352        if (transport->remote->fetch_tags == 2 && !no_tags)
 353                tags = 1;
 354        if (transport->remote->fetch_tags == -1)
 355                no_tags = 1;
 356
 357        if (!transport->ops || !transport->ops->get_refs_list ||
 358            !(transport->ops->fetch_refs || transport->ops->fetch_objs))
 359                die("Don't know how to fetch from %s", transport->url);
 360
 361        /* if not appending, truncate FETCH_HEAD */
 362        if (!append)
 363                fclose(fopen(git_path("FETCH_HEAD"), "w"));
 364
 365        ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
 366
 367        for (rm = ref_map; rm; rm = rm->next) {
 368                if (rm->peer_ref)
 369                        read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
 370        }
 371
 372        if (fetch_refs(transport, ref_map)) {
 373                free_refs(ref_map);
 374                return 1;
 375        }
 376
 377        fetch_map = ref_map;
 378
 379        /* if neither --no-tags nor --tags was specified, do automated tag
 380         * following ... */
 381        if (!(tags || no_tags) && autotags) {
 382                ref_map = find_non_local_tags(transport, fetch_map);
 383                if (ref_map) {
 384                        transport_set_option(transport, TRANS_OPT_DEPTH, "0");
 385                        fetch_refs(transport, ref_map);
 386                }
 387                free_refs(ref_map);
 388        }
 389
 390        free_refs(fetch_map);
 391
 392        return 0;
 393}
 394
 395int cmd_fetch(int argc, const char **argv, const char *prefix)
 396{
 397        struct remote *remote;
 398        struct transport *transport;
 399        int i, j, rla_offset;
 400        static const char **refs = NULL;
 401        int ref_nr = 0;
 402        int cmd_len = 0;
 403        const char *depth = NULL, *upload_pack = NULL;
 404        int keep = 0;
 405
 406        for (i = 1; i < argc; i++) {
 407                const char *arg = argv[i];
 408                cmd_len += strlen(arg);
 409
 410                if (arg[0] != '-')
 411                        break;
 412                if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
 413                        append = 1;
 414                        continue;
 415                }
 416                if (!prefixcmp(arg, "--upload-pack=")) {
 417                        upload_pack = arg + 14;
 418                        continue;
 419                }
 420                if (!strcmp(arg, "--upload-pack")) {
 421                        i++;
 422                        if (i == argc)
 423                                usage(fetch_usage);
 424                        upload_pack = argv[i];
 425                        continue;
 426                }
 427                if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
 428                        force = 1;
 429                        continue;
 430                }
 431                if (!strcmp(arg, "--no-tags")) {
 432                        no_tags = 1;
 433                        continue;
 434                }
 435                if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
 436                        tags = 1;
 437                        continue;
 438                }
 439                if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
 440                        keep = 1;
 441                        continue;
 442                }
 443                if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
 444                        update_head_ok = 1;
 445                        continue;
 446                }
 447                if (!prefixcmp(arg, "--depth=")) {
 448                        depth = arg + 8;
 449                        continue;
 450                }
 451                if (!strcmp(arg, "--depth")) {
 452                        i++;
 453                        if (i == argc)
 454                                usage(fetch_usage);
 455                        depth = argv[i];
 456                        continue;
 457                }
 458                if (!strcmp(arg, "--quiet")) {
 459                        quiet = 1;
 460                        continue;
 461                }
 462                if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
 463                        verbose++;
 464                        continue;
 465                }
 466                usage(fetch_usage);
 467        }
 468
 469        for (j = i; j < argc; j++)
 470                cmd_len += strlen(argv[j]);
 471
 472        default_rla = xmalloc(cmd_len + 5 + argc + 1);
 473        sprintf(default_rla, "fetch");
 474        rla_offset = strlen(default_rla);
 475        for (j = 1; j < argc; j++) {
 476                sprintf(default_rla + rla_offset, " %s", argv[j]);
 477                rla_offset += strlen(argv[j]) + 1;
 478        }
 479
 480        if (i == argc)
 481                remote = remote_get(NULL);
 482        else
 483                remote = remote_get(argv[i++]);
 484
 485        transport = transport_get(remote, remote->uri[0], 1);
 486        if (verbose >= 2)
 487                transport->verbose = 1;
 488        if (quiet)
 489                transport->verbose = 0;
 490        if (upload_pack)
 491                transport_set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
 492        if (keep)
 493                transport_set_option(transport, TRANS_OPT_KEEP, "yes");
 494        transport_set_option(transport, TRANS_OPT_DEPTH, depth);
 495
 496        if (!transport->url)
 497                die("Where do you want to fetch from today?");
 498
 499        if (i < argc) {
 500                int j = 0;
 501                refs = xcalloc(argc - i + 1, sizeof(const char *));
 502                while (i < argc) {
 503                        if (!strcmp(argv[i], "tag")) {
 504                                char *ref;
 505                                i++;
 506                                ref = xmalloc(strlen(argv[i]) * 2 + 22);
 507                                strcpy(ref, "refs/tags/");
 508                                strcat(ref, argv[i]);
 509                                strcat(ref, ":refs/tags/");
 510                                strcat(ref, argv[i]);
 511                                refs[j++] = ref;
 512                        } else
 513                                refs[j++] = argv[i];
 514                        i++;
 515                }
 516                refs[j] = NULL;
 517                ref_nr = j;
 518                for (j = 0; refs[j]; j++)
 519                        printf("ref: %s\n", refs[j]);
 520        }
 521
 522        return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);
 523}