builtin-clone.con commit send-email: signedoffcc -> signedoffbycc, but handle both (ddc3d4f)
   1/*
   2 * Builtin "git clone"
   3 *
   4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>,
   5 *               2008 Daniel Barkalow <barkalow@iabervon.org>
   6 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
   7 *
   8 * Clone a repository into a different directory that does not yet exist.
   9 */
  10
  11#include "cache.h"
  12#include "parse-options.h"
  13#include "fetch-pack.h"
  14#include "refs.h"
  15#include "tree.h"
  16#include "tree-walk.h"
  17#include "unpack-trees.h"
  18#include "transport.h"
  19#include "strbuf.h"
  20#include "dir.h"
  21#include "pack-refs.h"
  22
  23/*
  24 * Overall FIXMEs:
  25 *  - respect DB_ENVIRONMENT for .git/objects.
  26 *
  27 * Implementation notes:
  28 *  - dropping use-separate-remote and no-separate-remote compatibility
  29 *
  30 */
  31static const char * const builtin_clone_usage[] = {
  32        "git clone [options] [--] <repo> [<dir>]",
  33        NULL
  34};
  35
  36static int option_quiet, option_no_checkout, option_bare, option_mirror;
  37static int option_local, option_no_hardlinks, option_shared;
  38static char *option_template, *option_reference, *option_depth;
  39static char *option_origin = NULL;
  40static char *option_upload_pack = "git-upload-pack";
  41
  42static struct option builtin_clone_options[] = {
  43        OPT__QUIET(&option_quiet),
  44        OPT_BOOLEAN('n', "no-checkout", &option_no_checkout,
  45                    "don't create a checkout"),
  46        OPT_BOOLEAN(0, "bare", &option_bare, "create a bare repository"),
  47        OPT_BOOLEAN(0, "naked", &option_bare, "create a bare repository"),
  48        OPT_BOOLEAN(0, "mirror", &option_mirror,
  49                    "create a mirror repository (implies bare)"),
  50        OPT_BOOLEAN('l', "local", &option_local,
  51                    "to clone from a local repository"),
  52        OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks,
  53                    "don't use local hardlinks, always copy"),
  54        OPT_BOOLEAN('s', "shared", &option_shared,
  55                    "setup as shared repository"),
  56        OPT_STRING(0, "template", &option_template, "path",
  57                   "path the template repository"),
  58        OPT_STRING(0, "reference", &option_reference, "repo",
  59                   "reference repository"),
  60        OPT_STRING('o', "origin", &option_origin, "branch",
  61                   "use <branch> instead of 'origin' to track upstream"),
  62        OPT_STRING('u', "upload-pack", &option_upload_pack, "path",
  63                   "path to git-upload-pack on the remote"),
  64        OPT_STRING(0, "depth", &option_depth, "depth",
  65                    "create a shallow clone of that depth"),
  66
  67        OPT_END()
  68};
  69
  70static char *get_repo_path(const char *repo, int *is_bundle)
  71{
  72        static char *suffix[] = { "/.git", ".git", "" };
  73        static char *bundle_suffix[] = { ".bundle", "" };
  74        struct stat st;
  75        int i;
  76
  77        for (i = 0; i < ARRAY_SIZE(suffix); i++) {
  78                const char *path;
  79                path = mkpath("%s%s", repo, suffix[i]);
  80                if (is_directory(path)) {
  81                        *is_bundle = 0;
  82                        return xstrdup(make_nonrelative_path(path));
  83                }
  84        }
  85
  86        for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
  87                const char *path;
  88                path = mkpath("%s%s", repo, bundle_suffix[i]);
  89                if (!stat(path, &st) && S_ISREG(st.st_mode)) {
  90                        *is_bundle = 1;
  91                        return xstrdup(make_nonrelative_path(path));
  92                }
  93        }
  94
  95        return NULL;
  96}
  97
  98static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
  99{
 100        const char *end = repo + strlen(repo), *start;
 101
 102        /*
 103         * Strip trailing slashes and /.git
 104         */
 105        while (repo < end && is_dir_sep(end[-1]))
 106                end--;
 107        if (end - repo > 5 && is_dir_sep(end[-5]) &&
 108            !strncmp(end - 4, ".git", 4)) {
 109                end -= 5;
 110                while (repo < end && is_dir_sep(end[-1]))
 111                        end--;
 112        }
 113
 114        /*
 115         * Find last component, but be prepared that repo could have
 116         * the form  "remote.example.com:foo.git", i.e. no slash
 117         * in the directory part.
 118         */
 119        start = end;
 120        while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
 121                start--;
 122
 123        /*
 124         * Strip .{bundle,git}.
 125         */
 126        if (is_bundle) {
 127                if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
 128                        end -= 7;
 129        } else {
 130                if (end - start > 4 && !strncmp(end - 4, ".git", 4))
 131                        end -= 4;
 132        }
 133
 134        if (is_bare) {
 135                char *result = xmalloc(end - start + 5);
 136                sprintf(result, "%.*s.git", (int)(end - start), start);
 137                return result;
 138        }
 139
 140        return xstrndup(start, end - start);
 141}
 142
 143static void strip_trailing_slashes(char *dir)
 144{
 145        char *end = dir + strlen(dir);
 146
 147        while (dir < end - 1 && is_dir_sep(end[-1]))
 148                end--;
 149        *end = '\0';
 150}
 151
 152static void setup_reference(const char *repo)
 153{
 154        const char *ref_git;
 155        char *ref_git_copy;
 156
 157        struct remote *remote;
 158        struct transport *transport;
 159        const struct ref *extra;
 160
 161        ref_git = make_absolute_path(option_reference);
 162
 163        if (is_directory(mkpath("%s/.git/objects", ref_git)))
 164                ref_git = mkpath("%s/.git", ref_git);
 165        else if (!is_directory(mkpath("%s/objects", ref_git)))
 166                die("reference repository '%s' is not a local directory.",
 167                    option_reference);
 168
 169        ref_git_copy = xstrdup(ref_git);
 170
 171        add_to_alternates_file(ref_git_copy);
 172
 173        remote = remote_get(ref_git_copy);
 174        transport = transport_get(remote, ref_git_copy);
 175        for (extra = transport_get_remote_refs(transport); extra;
 176             extra = extra->next)
 177                add_extra_ref(extra->name, extra->old_sha1, 0);
 178
 179        transport_disconnect(transport);
 180
 181        free(ref_git_copy);
 182}
 183
 184static void copy_or_link_directory(char *src, char *dest)
 185{
 186        struct dirent *de;
 187        struct stat buf;
 188        int src_len, dest_len;
 189        DIR *dir;
 190
 191        dir = opendir(src);
 192        if (!dir)
 193                die("failed to open %s\n", src);
 194
 195        if (mkdir(dest, 0777)) {
 196                if (errno != EEXIST)
 197                        die("failed to create directory %s\n", dest);
 198                else if (stat(dest, &buf))
 199                        die("failed to stat %s\n", dest);
 200                else if (!S_ISDIR(buf.st_mode))
 201                        die("%s exists and is not a directory\n", dest);
 202        }
 203
 204        src_len = strlen(src);
 205        src[src_len] = '/';
 206        dest_len = strlen(dest);
 207        dest[dest_len] = '/';
 208
 209        while ((de = readdir(dir)) != NULL) {
 210                strcpy(src + src_len + 1, de->d_name);
 211                strcpy(dest + dest_len + 1, de->d_name);
 212                if (stat(src, &buf)) {
 213                        warning ("failed to stat %s\n", src);
 214                        continue;
 215                }
 216                if (S_ISDIR(buf.st_mode)) {
 217                        if (de->d_name[0] != '.')
 218                                copy_or_link_directory(src, dest);
 219                        continue;
 220                }
 221
 222                if (unlink(dest) && errno != ENOENT)
 223                        die("failed to unlink %s\n", dest);
 224                if (!option_no_hardlinks) {
 225                        if (!link(src, dest))
 226                                continue;
 227                        if (option_local)
 228                                die("failed to create link %s\n", dest);
 229                        option_no_hardlinks = 1;
 230                }
 231                if (copy_file(dest, src, 0666))
 232                        die("failed to copy file to %s\n", dest);
 233        }
 234        closedir(dir);
 235}
 236
 237static const struct ref *clone_local(const char *src_repo,
 238                                     const char *dest_repo)
 239{
 240        const struct ref *ret;
 241        char src[PATH_MAX];
 242        char dest[PATH_MAX];
 243        struct remote *remote;
 244        struct transport *transport;
 245
 246        if (option_shared)
 247                add_to_alternates_file(src_repo);
 248        else {
 249                snprintf(src, PATH_MAX, "%s/objects", src_repo);
 250                snprintf(dest, PATH_MAX, "%s/objects", dest_repo);
 251                copy_or_link_directory(src, dest);
 252        }
 253
 254        remote = remote_get(src_repo);
 255        transport = transport_get(remote, src_repo);
 256        ret = transport_get_remote_refs(transport);
 257        transport_disconnect(transport);
 258        return ret;
 259}
 260
 261static const char *junk_work_tree;
 262static const char *junk_git_dir;
 263pid_t junk_pid;
 264
 265static void remove_junk(void)
 266{
 267        struct strbuf sb;
 268        if (getpid() != junk_pid)
 269                return;
 270        strbuf_init(&sb, 0);
 271        if (junk_git_dir) {
 272                strbuf_addstr(&sb, junk_git_dir);
 273                remove_dir_recursively(&sb, 0);
 274                strbuf_reset(&sb);
 275        }
 276        if (junk_work_tree) {
 277                strbuf_addstr(&sb, junk_work_tree);
 278                remove_dir_recursively(&sb, 0);
 279                strbuf_reset(&sb);
 280        }
 281}
 282
 283static void remove_junk_on_signal(int signo)
 284{
 285        remove_junk();
 286        signal(SIGINT, SIG_DFL);
 287        raise(signo);
 288}
 289
 290static const struct ref *locate_head(const struct ref *refs,
 291                                     const struct ref *mapped_refs,
 292                                     const struct ref **remote_head_p)
 293{
 294        const struct ref *remote_head = NULL;
 295        const struct ref *remote_master = NULL;
 296        const struct ref *r;
 297        for (r = refs; r; r = r->next)
 298                if (!strcmp(r->name, "HEAD"))
 299                        remote_head = r;
 300
 301        for (r = mapped_refs; r; r = r->next)
 302                if (!strcmp(r->name, "refs/heads/master"))
 303                        remote_master = r;
 304
 305        if (remote_head_p)
 306                *remote_head_p = remote_head;
 307
 308        /* If there's no HEAD value at all, never mind. */
 309        if (!remote_head)
 310                return NULL;
 311
 312        /* If refs/heads/master could be right, it is. */
 313        if (remote_master && !hashcmp(remote_master->old_sha1,
 314                                      remote_head->old_sha1))
 315                return remote_master;
 316
 317        /* Look for another ref that points there */
 318        for (r = mapped_refs; r; r = r->next)
 319                if (r != remote_head &&
 320                    !hashcmp(r->old_sha1, remote_head->old_sha1))
 321                        return r;
 322
 323        /* Nothing is the same */
 324        return NULL;
 325}
 326
 327static struct ref *write_remote_refs(const struct ref *refs,
 328                struct refspec *refspec, const char *reflog)
 329{
 330        struct ref *local_refs = NULL;
 331        struct ref **tail = &local_refs;
 332        struct ref *r;
 333
 334        get_fetch_map(refs, refspec, &tail, 0);
 335        if (!option_mirror)
 336                get_fetch_map(refs, tag_refspec, &tail, 0);
 337
 338        for (r = local_refs; r; r = r->next)
 339                add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
 340
 341        pack_refs(PACK_REFS_ALL);
 342        clear_extra_refs();
 343
 344        return local_refs;
 345}
 346
 347int cmd_clone(int argc, const char **argv, const char *prefix)
 348{
 349        int use_local_hardlinks = 1;
 350        int use_separate_remote = 1;
 351        int is_bundle = 0;
 352        struct stat buf;
 353        const char *repo_name, *repo, *work_tree, *git_dir;
 354        char *path, *dir;
 355        const struct ref *refs, *head_points_at, *remote_head, *mapped_refs;
 356        char branch_top[256], key[256], value[256];
 357        struct strbuf reflog_msg;
 358        struct transport *transport = NULL;
 359        char *src_ref_prefix = "refs/heads/";
 360
 361        struct refspec refspec;
 362
 363        junk_pid = getpid();
 364
 365        argc = parse_options(argc, argv, builtin_clone_options,
 366                             builtin_clone_usage, 0);
 367
 368        if (argc == 0)
 369                die("You must specify a repository to clone.");
 370
 371        if (option_no_hardlinks)
 372                use_local_hardlinks = 0;
 373
 374        if (option_mirror)
 375                option_bare = 1;
 376
 377        if (option_bare) {
 378                if (option_origin)
 379                        die("--bare and --origin %s options are incompatible.",
 380                            option_origin);
 381                option_no_checkout = 1;
 382                use_separate_remote = 0;
 383        }
 384
 385        if (!option_origin)
 386                option_origin = "origin";
 387
 388        repo_name = argv[0];
 389
 390        path = get_repo_path(repo_name, &is_bundle);
 391        if (path)
 392                repo = xstrdup(make_nonrelative_path(repo_name));
 393        else if (!strchr(repo_name, ':'))
 394                repo = xstrdup(make_absolute_path(repo_name));
 395        else
 396                repo = repo_name;
 397
 398        if (argc == 2)
 399                dir = xstrdup(argv[1]);
 400        else
 401                dir = guess_dir_name(repo_name, is_bundle, option_bare);
 402        strip_trailing_slashes(dir);
 403
 404        if (!stat(dir, &buf))
 405                die("destination directory '%s' already exists.", dir);
 406
 407        strbuf_init(&reflog_msg, 0);
 408        strbuf_addf(&reflog_msg, "clone: from %s", repo);
 409
 410        if (option_bare)
 411                work_tree = NULL;
 412        else {
 413                work_tree = getenv("GIT_WORK_TREE");
 414                if (work_tree && !stat(work_tree, &buf))
 415                        die("working tree '%s' already exists.", work_tree);
 416        }
 417
 418        if (option_bare || work_tree)
 419                git_dir = xstrdup(dir);
 420        else {
 421                work_tree = dir;
 422                git_dir = xstrdup(mkpath("%s/.git", dir));
 423        }
 424
 425        if (!option_bare) {
 426                junk_work_tree = work_tree;
 427                if (safe_create_leading_directories_const(work_tree) < 0)
 428                        die("could not create leading directories of '%s': %s",
 429                                        work_tree, strerror(errno));
 430                if (mkdir(work_tree, 0755))
 431                        die("could not create work tree dir '%s': %s.",
 432                                        work_tree, strerror(errno));
 433                set_git_work_tree(work_tree);
 434        }
 435        junk_git_dir = git_dir;
 436        atexit(remove_junk);
 437        signal(SIGINT, remove_junk_on_signal);
 438
 439        setenv(CONFIG_ENVIRONMENT, xstrdup(mkpath("%s/config", git_dir)), 1);
 440
 441        if (safe_create_leading_directories_const(git_dir) < 0)
 442                die("could not create leading directories of '%s'", git_dir);
 443        set_git_dir(make_absolute_path(git_dir));
 444
 445        init_db(option_template, option_quiet ? INIT_DB_QUIET : 0);
 446
 447        /*
 448         * At this point, the config exists, so we do not need the
 449         * environment variable.  We actually need to unset it, too, to
 450         * re-enable parsing of the global configs.
 451         */
 452        unsetenv(CONFIG_ENVIRONMENT);
 453
 454        if (option_reference)
 455                setup_reference(git_dir);
 456
 457        git_config(git_default_config, NULL);
 458
 459        if (option_bare) {
 460                if (option_mirror)
 461                        src_ref_prefix = "refs/";
 462                strcpy(branch_top, src_ref_prefix);
 463
 464                git_config_set("core.bare", "true");
 465        } else {
 466                snprintf(branch_top, sizeof(branch_top),
 467                         "refs/remotes/%s/", option_origin);
 468        }
 469
 470        if (option_mirror || !option_bare) {
 471                /* Configure the remote */
 472                if (option_mirror) {
 473                        snprintf(key, sizeof(key),
 474                                        "remote.%s.mirror", option_origin);
 475                        git_config_set(key, "true");
 476                }
 477
 478                snprintf(key, sizeof(key), "remote.%s.url", option_origin);
 479                git_config_set(key, repo);
 480
 481                snprintf(key, sizeof(key), "remote.%s.fetch", option_origin);
 482                snprintf(value, sizeof(value),
 483                                "+%s*:%s*", src_ref_prefix, branch_top);
 484                git_config_set_multivar(key, value, "^$", 0);
 485        }
 486
 487        refspec.force = 0;
 488        refspec.pattern = 1;
 489        refspec.src = src_ref_prefix;
 490        refspec.dst = branch_top;
 491
 492        if (path && !is_bundle)
 493                refs = clone_local(path, git_dir);
 494        else {
 495                struct remote *remote = remote_get(argv[0]);
 496                transport = transport_get(remote, remote->url[0]);
 497
 498                if (!transport->get_refs_list || !transport->fetch)
 499                        die("Don't know how to clone %s", transport->url);
 500
 501                transport_set_option(transport, TRANS_OPT_KEEP, "yes");
 502
 503                if (option_depth)
 504                        transport_set_option(transport, TRANS_OPT_DEPTH,
 505                                             option_depth);
 506
 507                if (option_quiet)
 508                        transport->verbose = -1;
 509
 510                if (option_upload_pack)
 511                        transport_set_option(transport, TRANS_OPT_UPLOADPACK,
 512                                             option_upload_pack);
 513
 514                refs = transport_get_remote_refs(transport);
 515                transport_fetch_refs(transport, refs);
 516        }
 517
 518        clear_extra_refs();
 519
 520        mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
 521
 522        head_points_at = locate_head(refs, mapped_refs, &remote_head);
 523
 524        if (head_points_at) {
 525                /* Local default branch link */
 526                create_symref("HEAD", head_points_at->name, NULL);
 527
 528                if (!option_bare) {
 529                        struct strbuf head_ref;
 530                        const char *head = head_points_at->name;
 531
 532                        if (!prefixcmp(head, "refs/heads/"))
 533                                head += 11;
 534
 535                        /* Set up the initial local branch */
 536
 537                        /* Local branch initial value */
 538                        update_ref(reflog_msg.buf, "HEAD",
 539                                   head_points_at->old_sha1,
 540                                   NULL, 0, DIE_ON_ERR);
 541
 542                        strbuf_init(&head_ref, 0);
 543                        strbuf_addstr(&head_ref, branch_top);
 544                        strbuf_addstr(&head_ref, "HEAD");
 545
 546                        /* Remote branch link */
 547                        create_symref(head_ref.buf,
 548                                      head_points_at->peer_ref->name,
 549                                      reflog_msg.buf);
 550
 551                        snprintf(key, sizeof(key), "branch.%s.remote", head);
 552                        git_config_set(key, option_origin);
 553                        snprintf(key, sizeof(key), "branch.%s.merge", head);
 554                        git_config_set(key, head_points_at->name);
 555                }
 556        } else if (remote_head) {
 557                /* Source had detached HEAD pointing somewhere. */
 558                if (!option_bare)
 559                        update_ref(reflog_msg.buf, "HEAD",
 560                                   remote_head->old_sha1,
 561                                   NULL, REF_NODEREF, DIE_ON_ERR);
 562        } else {
 563                /* Nothing to checkout out */
 564                if (!option_no_checkout)
 565                        warning("remote HEAD refers to nonexistent ref, "
 566                                "unable to checkout.\n");
 567                option_no_checkout = 1;
 568        }
 569
 570        if (transport)
 571                transport_unlock_pack(transport);
 572
 573        if (!option_no_checkout) {
 574                struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
 575                struct unpack_trees_options opts;
 576                struct tree *tree;
 577                struct tree_desc t;
 578                int fd;
 579
 580                /* We need to be in the new work tree for the checkout */
 581                setup_work_tree();
 582
 583                fd = hold_locked_index(lock_file, 1);
 584
 585                memset(&opts, 0, sizeof opts);
 586                opts.update = 1;
 587                opts.merge = 1;
 588                opts.fn = oneway_merge;
 589                opts.verbose_update = !option_quiet;
 590                opts.src_index = &the_index;
 591                opts.dst_index = &the_index;
 592
 593                tree = parse_tree_indirect(remote_head->old_sha1);
 594                parse_tree(tree);
 595                init_tree_desc(&t, tree->buffer, tree->size);
 596                unpack_trees(1, &t, &opts);
 597
 598                if (write_cache(fd, active_cache, active_nr) ||
 599                    commit_locked_index(lock_file))
 600                        die("unable to write new index file");
 601        }
 602
 603        strbuf_release(&reflog_msg);
 604        junk_pid = 0;
 605        return 0;
 606}