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
8 * Clone a repository into a different directory that does not yet exist.
13 #include "parse-options.h"
14 #include "fetch-pack.h"
17 #include "tree-walk.h"
18 #include "unpack-trees.h"
19 #include "transport.h"
25 #include "run-command.h"
26 #include "connected.h"
30 * - respect DB_ENVIRONMENT for .git/objects.
32 * Implementation notes:
33 * - dropping use-separate-remote and no-separate-remote compatibility
36 static const char * const builtin_clone_usage[] = {
37 N_("git clone [<options>] [--] <repo> [<dir>]"),
41 static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
42 static int option_local = -1, option_no_hardlinks, option_shared, option_recursive;
43 static int option_shallow_submodules = -1;
44 static char *option_template, *option_depth;
45 static char *option_origin = NULL;
46 static char *option_branch = NULL;
47 static const char *real_git_dir;
48 static char *option_upload_pack = "git-upload-pack";
49 static int option_verbosity;
50 static int option_progress = -1;
51 static enum transport_family family;
52 static struct string_list option_config;
53 static struct string_list option_reference;
54 static int option_dissociate;
55 static int max_jobs = -1;
57 static struct option builtin_clone_options[] = {
58 OPT__VERBOSITY(&option_verbosity),
59 OPT_BOOL(0, "progress", &option_progress,
60 N_("force progress reporting")),
61 OPT_BOOL('n', "no-checkout", &option_no_checkout,
62 N_("don't create a checkout")),
63 OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
64 OPT_HIDDEN_BOOL(0, "naked", &option_bare,
65 N_("create a bare repository")),
66 OPT_BOOL(0, "mirror", &option_mirror,
67 N_("create a mirror repository (implies bare)")),
68 OPT_BOOL('l', "local", &option_local,
69 N_("to clone from a local repository")),
70 OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
71 N_("don't use local hardlinks, always copy")),
72 OPT_BOOL('s', "shared", &option_shared,
73 N_("setup as shared repository")),
74 OPT_BOOL(0, "recursive", &option_recursive,
75 N_("initialize submodules in the clone")),
76 OPT_BOOL(0, "recurse-submodules", &option_recursive,
77 N_("initialize submodules in the clone")),
78 OPT_INTEGER('j', "jobs", &max_jobs,
79 N_("number of submodules cloned in parallel")),
80 OPT_STRING(0, "template", &option_template, N_("template-directory"),
81 N_("directory from which templates will be used")),
82 OPT_STRING_LIST(0, "reference", &option_reference, N_("repo"),
83 N_("reference repository")),
84 OPT_BOOL(0, "dissociate", &option_dissociate,
85 N_("use --reference only while cloning")),
86 OPT_STRING('o', "origin", &option_origin, N_("name"),
87 N_("use <name> instead of 'origin' to track upstream")),
88 OPT_STRING('b', "branch", &option_branch, N_("branch"),
89 N_("checkout <branch> instead of the remote's HEAD")),
90 OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
91 N_("path to git-upload-pack on the remote")),
92 OPT_STRING(0, "depth", &option_depth, N_("depth"),
93 N_("create a shallow clone of that depth")),
94 OPT_BOOL(0, "single-branch", &option_single_branch,
95 N_("clone only one branch, HEAD or --branch")),
96 OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules,
97 N_("any cloned submodules will be shallow")),
98 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
99 N_("separate git dir from working tree")),
100 OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
101 N_("set config inside the new repository")),
102 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
103 TRANSPORT_FAMILY_IPV4),
104 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
105 TRANSPORT_FAMILY_IPV6),
109 static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
111 static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
112 static char *bundle_suffix[] = { ".bundle", "" };
113 size_t baselen = path->len;
117 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
118 strbuf_setlen(path, baselen);
119 strbuf_addstr(path, suffix[i]);
120 if (stat(path->buf, &st))
122 if (S_ISDIR(st.st_mode) && is_git_directory(path->buf)) {
125 } else if (S_ISREG(st.st_mode) && st.st_size > 8) {
126 /* Is it a "gitfile"? */
129 int len, fd = open(path->buf, O_RDONLY);
132 len = read_in_full(fd, signature, 8);
134 if (len != 8 || strncmp(signature, "gitdir: ", 8))
136 dst = read_gitfile(path->buf);
144 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
145 strbuf_setlen(path, baselen);
146 strbuf_addstr(path, bundle_suffix[i]);
147 if (!stat(path->buf, &st) && S_ISREG(st.st_mode)) {
156 static char *get_repo_path(const char *repo, int *is_bundle)
158 struct strbuf path = STRBUF_INIT;
162 strbuf_addstr(&path, repo);
163 raw = get_repo_path_1(&path, is_bundle);
164 canon = raw ? xstrdup(absolute_path(raw)) : NULL;
165 strbuf_release(&path);
169 static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
171 const char *end = repo + strlen(repo), *start, *ptr;
178 start = strstr(repo, "://");
185 * Skip authentication data. The stripping does happen
186 * greedily, such that we strip up to the last '@' inside
189 for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) {
195 * Strip trailing spaces, slashes and /.git
197 while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
199 if (end - start > 5 && is_dir_sep(end[-5]) &&
200 !strncmp(end - 4, ".git", 4)) {
202 while (start < end && is_dir_sep(end[-1]))
207 * Strip trailing port number if we've got only a
208 * hostname (that is, there is no dir separator but a
209 * colon). This check is required such that we do not
210 * strip URI's like '/foo/bar:2222.git', which should
211 * result in a dir '2222' being guessed due to backwards
214 if (memchr(start, '/', end - start) == NULL
215 && memchr(start, ':', end - start) != NULL) {
217 while (start < ptr && isdigit(ptr[-1]) && ptr[-1] != ':')
219 if (start < ptr && ptr[-1] == ':')
224 * Find last component. To remain backwards compatible we
225 * also regard colons as path separators, such that
226 * cloning a repository 'foo:bar.git' would result in a
227 * directory 'bar' being guessed.
230 while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':')
235 * Strip .{bundle,git}.
238 strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git");
240 if (!len || (len == 1 && *start == '/'))
241 die(_("No directory name could be guessed.\n"
242 "Please specify a directory on the command line"));
245 dir = xstrfmt("%.*s.git", (int)len, start);
247 dir = xstrndup(start, len);
249 * Replace sequences of 'control' characters and whitespace
250 * with one ascii space, remove leading and trailing spaces.
254 int prev_space = 1 /* strip leading whitespace */;
255 for (end = dir; *end; ++end) {
257 if ((unsigned char)ch < '\x20')
268 if (out > dir && prev_space)
274 static void strip_trailing_slashes(char *dir)
276 char *end = dir + strlen(dir);
278 while (dir < end - 1 && is_dir_sep(end[-1]))
283 static int add_one_reference(struct string_list_item *item, void *cb_data)
287 struct strbuf alternate = STRBUF_INIT;
289 /* Beware: read_gitfile(), real_path() and mkpath() return static buffer */
290 ref_git = xstrdup(real_path(item->string));
292 repo = read_gitfile(ref_git);
294 repo = read_gitfile(mkpath("%s/.git", ref_git));
297 ref_git = xstrdup(repo);
300 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
301 char *ref_git_git = mkpathdup("%s/.git", ref_git);
303 ref_git = ref_git_git;
304 } else if (!is_directory(mkpath("%s/objects", ref_git))) {
305 struct strbuf sb = STRBUF_INIT;
306 if (get_common_dir(&sb, ref_git))
307 die(_("reference repository '%s' as a linked checkout is not supported yet."),
309 die(_("reference repository '%s' is not a local repository."),
313 if (!access(mkpath("%s/shallow", ref_git), F_OK))
314 die(_("reference repository '%s' is shallow"), item->string);
316 if (!access(mkpath("%s/info/grafts", ref_git), F_OK))
317 die(_("reference repository '%s' is grafted"), item->string);
319 strbuf_addf(&alternate, "%s/objects", ref_git);
320 add_to_alternates_file(alternate.buf);
321 strbuf_release(&alternate);
326 static void setup_reference(void)
328 for_each_string_list(&option_reference, add_one_reference, NULL);
331 static void copy_alternates(struct strbuf *src, struct strbuf *dst,
332 const char *src_repo)
335 * Read from the source objects/info/alternates file
336 * and copy the entries to corresponding file in the
337 * destination repository with add_to_alternates_file().
338 * Both src and dst have "$path/objects/info/alternates".
340 * Instead of copying bit-for-bit from the original,
341 * we need to append to existing one so that the already
342 * created entry via "clone -s" is not lost, and also
343 * to turn entries with paths relative to the original
344 * absolute, so that they can be used in the new repository.
346 FILE *in = fopen(src->buf, "r");
347 struct strbuf line = STRBUF_INIT;
349 while (strbuf_getline(&line, in) != EOF) {
351 if (!line.len || line.buf[0] == '#')
353 if (is_absolute_path(line.buf)) {
354 add_to_alternates_file(line.buf);
357 abs_path = mkpathdup("%s/objects/%s", src_repo, line.buf);
358 normalize_path_copy(abs_path, abs_path);
359 add_to_alternates_file(abs_path);
362 strbuf_release(&line);
366 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
367 const char *src_repo, int src_baselen)
371 int src_len, dest_len;
374 dir = opendir(src->buf);
376 die_errno(_("failed to open '%s'"), src->buf);
378 if (mkdir(dest->buf, 0777)) {
380 die_errno(_("failed to create directory '%s'"), dest->buf);
381 else if (stat(dest->buf, &buf))
382 die_errno(_("failed to stat '%s'"), dest->buf);
383 else if (!S_ISDIR(buf.st_mode))
384 die(_("%s exists and is not a directory"), dest->buf);
387 strbuf_addch(src, '/');
389 strbuf_addch(dest, '/');
390 dest_len = dest->len;
392 while ((de = readdir(dir)) != NULL) {
393 strbuf_setlen(src, src_len);
394 strbuf_addstr(src, de->d_name);
395 strbuf_setlen(dest, dest_len);
396 strbuf_addstr(dest, de->d_name);
397 if (stat(src->buf, &buf)) {
398 warning (_("failed to stat %s\n"), src->buf);
401 if (S_ISDIR(buf.st_mode)) {
402 if (de->d_name[0] != '.')
403 copy_or_link_directory(src, dest,
404 src_repo, src_baselen);
408 /* Files that cannot be copied bit-for-bit... */
409 if (!strcmp(src->buf + src_baselen, "/info/alternates")) {
410 copy_alternates(src, dest, src_repo);
414 if (unlink(dest->buf) && errno != ENOENT)
415 die_errno(_("failed to unlink '%s'"), dest->buf);
416 if (!option_no_hardlinks) {
417 if (!link(src->buf, dest->buf))
419 if (option_local > 0)
420 die_errno(_("failed to create link '%s'"), dest->buf);
421 option_no_hardlinks = 1;
423 if (copy_file_with_time(dest->buf, src->buf, 0666))
424 die_errno(_("failed to copy file to '%s'"), dest->buf);
429 static void clone_local(const char *src_repo, const char *dest_repo)
432 struct strbuf alt = STRBUF_INIT;
433 strbuf_addf(&alt, "%s/objects", src_repo);
434 add_to_alternates_file(alt.buf);
435 strbuf_release(&alt);
437 struct strbuf src = STRBUF_INIT;
438 struct strbuf dest = STRBUF_INIT;
439 get_common_dir(&src, src_repo);
440 get_common_dir(&dest, dest_repo);
441 strbuf_addstr(&src, "/objects");
442 strbuf_addstr(&dest, "/objects");
443 copy_or_link_directory(&src, &dest, src_repo, src.len);
444 strbuf_release(&src);
445 strbuf_release(&dest);
448 if (0 <= option_verbosity)
449 fprintf(stderr, _("done.\n"));
452 static const char *junk_work_tree;
453 static const char *junk_git_dir;
458 } junk_mode = JUNK_LEAVE_NONE;
460 static const char junk_leave_repo_msg[] =
461 N_("Clone succeeded, but checkout failed.\n"
462 "You can inspect what was checked out with 'git status'\n"
463 "and retry the checkout with 'git checkout -f HEAD'\n");
465 static void remove_junk(void)
467 struct strbuf sb = STRBUF_INIT;
470 case JUNK_LEAVE_REPO:
471 warning("%s", _(junk_leave_repo_msg));
476 /* proceed to removal */
481 strbuf_addstr(&sb, junk_git_dir);
482 remove_dir_recursively(&sb, 0);
485 if (junk_work_tree) {
486 strbuf_addstr(&sb, junk_work_tree);
487 remove_dir_recursively(&sb, 0);
492 static void remove_junk_on_signal(int signo)
499 static struct ref *find_remote_branch(const struct ref *refs, const char *branch)
502 struct strbuf head = STRBUF_INIT;
503 strbuf_addstr(&head, "refs/heads/");
504 strbuf_addstr(&head, branch);
505 ref = find_ref_by_name(refs, head.buf);
506 strbuf_release(&head);
511 strbuf_addstr(&head, "refs/tags/");
512 strbuf_addstr(&head, branch);
513 ref = find_ref_by_name(refs, head.buf);
514 strbuf_release(&head);
519 static struct ref *wanted_peer_refs(const struct ref *refs,
520 struct refspec *refspec)
522 struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD"));
523 struct ref *local_refs = head;
524 struct ref **tail = head ? &head->next : &local_refs;
526 if (option_single_branch) {
527 struct ref *remote_head = NULL;
530 remote_head = guess_remote_head(head, refs, 0);
534 remote_head = copy_ref(find_remote_branch(refs, option_branch));
537 if (!remote_head && option_branch)
538 warning(_("Could not find remote branch %s to clone."),
541 get_fetch_map(remote_head, refspec, &tail, 0);
543 /* if --branch=tag, pull the requested tag explicitly */
544 get_fetch_map(remote_head, tag_refspec, &tail, 0);
547 get_fetch_map(refs, refspec, &tail, 0);
549 if (!option_mirror && !option_single_branch)
550 get_fetch_map(refs, tag_refspec, &tail, 0);
555 static void write_remote_refs(const struct ref *local_refs)
559 struct ref_transaction *t;
560 struct strbuf err = STRBUF_INIT;
562 t = ref_transaction_begin(&err);
566 for (r = local_refs; r; r = r->next) {
569 if (ref_transaction_create(t, r->peer_ref->name, r->old_oid.hash,
574 if (initial_ref_transaction_commit(t, &err))
577 strbuf_release(&err);
578 ref_transaction_free(t);
581 static void write_followtags(const struct ref *refs, const char *msg)
583 const struct ref *ref;
584 for (ref = refs; ref; ref = ref->next) {
585 if (!starts_with(ref->name, "refs/tags/"))
587 if (ends_with(ref->name, "^{}"))
589 if (!has_object_file(&ref->old_oid))
591 update_ref(msg, ref->name, ref->old_oid.hash,
592 NULL, 0, UPDATE_REFS_DIE_ON_ERR);
596 static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
598 struct ref **rm = cb_data;
599 struct ref *ref = *rm;
602 * Skip anything missing a peer_ref, which we are not
603 * actually going to write a ref for.
605 while (ref && !ref->peer_ref)
607 /* Returning -1 notes "end of list" to the caller. */
611 hashcpy(sha1, ref->old_oid.hash);
616 static void update_remote_refs(const struct ref *refs,
617 const struct ref *mapped_refs,
618 const struct ref *remote_head_points_at,
619 const char *branch_top,
621 struct transport *transport,
622 int check_connectivity)
624 const struct ref *rm = mapped_refs;
626 if (check_connectivity) {
627 if (transport->progress)
628 fprintf(stderr, _("Checking connectivity... "));
629 if (check_everything_connected_with_transport(iterate_ref_map,
631 die(_("remote did not send all necessary objects"));
632 if (transport->progress)
633 fprintf(stderr, _("done.\n"));
637 write_remote_refs(mapped_refs);
638 if (option_single_branch)
639 write_followtags(refs, msg);
642 if (remote_head_points_at && !option_bare) {
643 struct strbuf head_ref = STRBUF_INIT;
644 strbuf_addstr(&head_ref, branch_top);
645 strbuf_addstr(&head_ref, "HEAD");
646 if (create_symref(head_ref.buf,
647 remote_head_points_at->peer_ref->name,
649 die(_("unable to update %s"), head_ref.buf);
650 strbuf_release(&head_ref);
654 static void update_head(const struct ref *our, const struct ref *remote,
658 if (our && skip_prefix(our->name, "refs/heads/", &head)) {
659 /* Local default branch link */
660 if (create_symref("HEAD", our->name, NULL) < 0)
661 die(_("unable to update HEAD"));
663 update_ref(msg, "HEAD", our->old_oid.hash, NULL, 0,
664 UPDATE_REFS_DIE_ON_ERR);
665 install_branch_config(0, head, option_origin, our->name);
668 struct commit *c = lookup_commit_reference(our->old_oid.hash);
669 /* --branch specifies a non-branch (i.e. tags), detach HEAD */
670 update_ref(msg, "HEAD", c->object.oid.hash,
671 NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
674 * We know remote HEAD points to a non-branch, or
675 * HEAD points to a branch but we don't know which one.
676 * Detach HEAD in all these cases.
678 update_ref(msg, "HEAD", remote->old_oid.hash,
679 NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
683 static int checkout(void)
685 unsigned char sha1[20];
687 struct lock_file *lock_file;
688 struct unpack_trees_options opts;
693 if (option_no_checkout)
696 head = resolve_refdup("HEAD", RESOLVE_REF_READING, sha1, NULL);
698 warning(_("remote HEAD refers to nonexistent ref, "
699 "unable to checkout.\n"));
702 if (!strcmp(head, "HEAD")) {
703 if (advice_detached_head)
704 detach_advice(sha1_to_hex(sha1));
706 if (!starts_with(head, "refs/heads/"))
707 die(_("HEAD not found below refs/heads!"));
711 /* We need to be in the new work tree for the checkout */
714 lock_file = xcalloc(1, sizeof(struct lock_file));
715 hold_locked_index(lock_file, 1);
717 memset(&opts, 0, sizeof opts);
720 opts.fn = oneway_merge;
721 opts.verbose_update = (option_verbosity >= 0);
722 opts.src_index = &the_index;
723 opts.dst_index = &the_index;
725 tree = parse_tree_indirect(sha1);
727 init_tree_desc(&t, tree->buffer, tree->size);
728 if (unpack_trees(1, &t, &opts) < 0)
729 die(_("unable to checkout working tree"));
731 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
732 die(_("unable to write new index file"));
734 err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
735 sha1_to_hex(sha1), "1", NULL);
737 if (!err && option_recursive) {
738 struct argv_array args = ARGV_ARRAY_INIT;
739 argv_array_pushl(&args, "submodule", "update", "--init", "--recursive", NULL);
741 if (option_shallow_submodules == 1
742 || (option_shallow_submodules == -1 && option_depth))
743 argv_array_push(&args, "--depth=1");
746 argv_array_pushf(&args, "--jobs=%d", max_jobs);
748 err = run_command_v_opt(args.argv, RUN_GIT_CMD);
749 argv_array_clear(&args);
755 static int write_one_config(const char *key, const char *value, void *data)
757 return git_config_set_multivar_gently(key, value ? value : "true", "^$", 0);
760 static void write_config(struct string_list *config)
764 for (i = 0; i < config->nr; i++) {
765 if (git_config_parse_parameter(config->items[i].string,
766 write_one_config, NULL) < 0)
767 die(_("unable to write parameters to config file"));
771 static void write_refspec_config(const char *src_ref_prefix,
772 const struct ref *our_head_points_at,
773 const struct ref *remote_head_points_at,
774 struct strbuf *branch_top)
776 struct strbuf key = STRBUF_INIT;
777 struct strbuf value = STRBUF_INIT;
779 if (option_mirror || !option_bare) {
780 if (option_single_branch && !option_mirror) {
782 if (starts_with(our_head_points_at->name, "refs/tags/"))
783 strbuf_addf(&value, "+%s:%s", our_head_points_at->name,
784 our_head_points_at->name);
786 strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
787 branch_top->buf, option_branch);
788 } else if (remote_head_points_at) {
789 const char *head = remote_head_points_at->name;
790 if (!skip_prefix(head, "refs/heads/", &head))
791 die("BUG: remote HEAD points at non-head?");
793 strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name,
794 branch_top->buf, head);
797 * otherwise, the next "git fetch" will
798 * simply fetch from HEAD without updating
799 * any remote-tracking branch, which is what
803 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf);
805 /* Configure the remote */
807 strbuf_addf(&key, "remote.%s.fetch", option_origin);
808 git_config_set_multivar(key.buf, value.buf, "^$", 0);
812 strbuf_addf(&key, "remote.%s.mirror", option_origin);
813 git_config_set(key.buf, "true");
819 strbuf_release(&key);
820 strbuf_release(&value);
823 static void dissociate_from_references(void)
825 static const char* argv[] = { "repack", "-a", "-d", NULL };
826 char *alternates = git_pathdup("objects/info/alternates");
828 if (!access(alternates, F_OK)) {
829 if (run_command_v_opt(argv, RUN_GIT_CMD|RUN_COMMAND_NO_STDIN))
830 die(_("cannot repack to clean up"));
831 if (unlink(alternates) && errno != ENOENT)
832 die_errno(_("cannot unlink temporary alternates file"));
837 int cmd_clone(int argc, const char **argv, const char *prefix)
839 int is_bundle = 0, is_local;
841 const char *repo_name, *repo, *work_tree, *git_dir;
844 const struct ref *refs, *remote_head;
845 const struct ref *remote_head_points_at;
846 const struct ref *our_head_points_at;
847 struct ref *mapped_refs;
848 const struct ref *ref;
849 struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
850 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
851 struct transport *transport = NULL;
852 const char *src_ref_prefix = "refs/heads/";
853 struct remote *remote;
854 int err = 0, complete_refs_before_fetch = 1;
856 struct refspec *refspec;
857 const char *fetch_pattern;
859 packet_trace_identity("clone");
860 argc = parse_options(argc, argv, prefix, builtin_clone_options,
861 builtin_clone_usage, 0);
864 usage_msg_opt(_("Too many arguments."),
865 builtin_clone_usage, builtin_clone_options);
868 usage_msg_opt(_("You must specify a repository to clone."),
869 builtin_clone_usage, builtin_clone_options);
871 if (option_single_branch == -1)
872 option_single_branch = option_depth ? 1 : 0;
879 die(_("--bare and --origin %s options are incompatible."),
882 die(_("--bare and --separate-git-dir are incompatible."));
883 option_no_checkout = 1;
887 option_origin = "origin";
891 path = get_repo_path(repo_name, &is_bundle);
893 repo = xstrdup(absolute_path(repo_name));
894 else if (!strchr(repo_name, ':'))
895 die(_("repository '%s' does not exist"), repo_name);
899 /* no need to be strict, transport_set_option() will validate it again */
900 if (option_depth && atoi(option_depth) < 1)
901 die(_("depth %s is not a positive number"), option_depth);
904 dir = xstrdup(argv[1]);
906 dir = guess_dir_name(repo_name, is_bundle, option_bare);
907 strip_trailing_slashes(dir);
909 dest_exists = !stat(dir, &buf);
910 if (dest_exists && !is_empty_dir(dir))
911 die(_("destination path '%s' already exists and is not "
912 "an empty directory."), dir);
914 strbuf_addf(&reflog_msg, "clone: from %s", repo);
919 work_tree = getenv("GIT_WORK_TREE");
920 if (work_tree && !stat(work_tree, &buf))
921 die(_("working tree '%s' already exists."), work_tree);
924 if (option_bare || work_tree)
925 git_dir = xstrdup(dir);
928 git_dir = mkpathdup("%s/.git", dir);
932 sigchain_push_common(remove_junk_on_signal);
935 if (safe_create_leading_directories_const(work_tree) < 0)
936 die_errno(_("could not create leading directories of '%s'"),
938 if (!dest_exists && mkdir(work_tree, 0777))
939 die_errno(_("could not create work tree dir '%s'"),
941 junk_work_tree = work_tree;
942 set_git_work_tree(work_tree);
945 junk_git_dir = git_dir;
946 if (safe_create_leading_directories_const(git_dir) < 0)
947 die(_("could not create leading directories of '%s'"), git_dir);
949 set_git_dir_init(git_dir, real_git_dir, 0);
951 git_dir = real_git_dir;
952 junk_git_dir = real_git_dir;
955 if (0 <= option_verbosity) {
957 fprintf(stderr, _("Cloning into bare repository '%s'...\n"), dir);
959 fprintf(stderr, _("Cloning into '%s'...\n"), dir);
961 init_db(option_template, INIT_DB_QUIET);
962 write_config(&option_config);
964 git_config(git_default_config, NULL);
968 src_ref_prefix = "refs/";
969 strbuf_addstr(&branch_top, src_ref_prefix);
971 git_config_set("core.bare", "true");
973 strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
976 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
977 strbuf_addf(&key, "remote.%s.url", option_origin);
978 git_config_set(key.buf, repo);
981 if (option_reference.nr)
984 fetch_pattern = value.buf;
985 refspec = parse_fetch_refspec(1, &fetch_pattern);
987 strbuf_reset(&value);
989 remote = remote_get(option_origin);
990 transport = transport_get(remote, remote->url[0]);
991 transport_set_verbosity(transport, option_verbosity, option_progress);
992 transport->family = family;
994 path = get_repo_path(remote->url[0], &is_bundle);
995 is_local = option_local != 0 && path && !is_bundle;
998 warning(_("--depth is ignored in local clones; use file:// instead."));
999 if (!access(mkpath("%s/shallow", path), F_OK)) {
1000 if (option_local > 0)
1001 warning(_("source repository is shallow, ignoring --local"));
1005 if (option_local > 0 && !is_local)
1006 warning(_("--local is ignored"));
1007 transport->cloning = 1;
1009 if (!transport->get_refs_list || (!is_local && !transport->fetch))
1010 die(_("Don't know how to clone %s"), transport->url);
1012 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
1015 transport_set_option(transport, TRANS_OPT_DEPTH,
1017 if (option_single_branch)
1018 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
1020 if (option_upload_pack)
1021 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
1022 option_upload_pack);
1024 if (transport->smart_options && !option_depth)
1025 transport->smart_options->check_self_contained_and_connected = 1;
1027 refs = transport_get_remote_refs(transport);
1030 mapped_refs = wanted_peer_refs(refs, refspec);
1032 * transport_get_remote_refs() may return refs with null sha-1
1033 * in mapped_refs (see struct transport->get_refs_list
1034 * comment). In that case we need fetch it early because
1035 * remote_head code below relies on it.
1037 * for normal clones, transport_get_remote_refs() should
1038 * return reliable ref set, we can delay cloning until after
1039 * remote HEAD check.
1041 for (ref = refs; ref; ref = ref->next)
1042 if (is_null_oid(&ref->old_oid)) {
1043 complete_refs_before_fetch = 0;
1047 if (!is_local && !complete_refs_before_fetch)
1048 transport_fetch_refs(transport, mapped_refs);
1050 remote_head = find_ref_by_name(refs, "HEAD");
1051 remote_head_points_at =
1052 guess_remote_head(remote_head, mapped_refs, 0);
1054 if (option_branch) {
1055 our_head_points_at =
1056 find_remote_branch(mapped_refs, option_branch);
1058 if (!our_head_points_at)
1059 die(_("Remote branch %s not found in upstream %s"),
1060 option_branch, option_origin);
1063 our_head_points_at = remote_head_points_at;
1067 die(_("Remote branch %s not found in upstream %s"),
1068 option_branch, option_origin);
1070 warning(_("You appear to have cloned an empty repository."));
1072 our_head_points_at = NULL;
1073 remote_head_points_at = NULL;
1075 option_no_checkout = 1;
1077 install_branch_config(0, "master", option_origin,
1078 "refs/heads/master");
1081 write_refspec_config(src_ref_prefix, our_head_points_at,
1082 remote_head_points_at, &branch_top);
1085 clone_local(path, git_dir);
1086 else if (refs && complete_refs_before_fetch)
1087 transport_fetch_refs(transport, mapped_refs);
1089 update_remote_refs(refs, mapped_refs, remote_head_points_at,
1090 branch_top.buf, reflog_msg.buf, transport, !is_local);
1092 update_head(our_head_points_at, remote_head, reflog_msg.buf);
1094 transport_unlock_pack(transport);
1095 transport_disconnect(transport);
1097 if (option_dissociate) {
1099 dissociate_from_references();
1102 junk_mode = JUNK_LEAVE_REPO;
1105 strbuf_release(&reflog_msg);
1106 strbuf_release(&branch_top);
1107 strbuf_release(&key);
1108 strbuf_release(&value);
1109 junk_mode = JUNK_LEAVE_ALL;