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.
12 #include "parse-options.h"
13 #include "fetch-pack.h"
16 #include "tree-walk.h"
17 #include "unpack-trees.h"
18 #include "transport.h"
24 #include "run-command.h"
25 #include "connected.h"
29 * - respect DB_ENVIRONMENT for .git/objects.
31 * Implementation notes:
32 * - dropping use-separate-remote and no-separate-remote compatibility
35 static const char * const builtin_clone_usage[] = {
36 N_("git clone [options] [--] <repo> [<dir>]"),
40 static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
41 static int option_local = -1, option_no_hardlinks, option_shared, option_recursive;
42 static char *option_template, *option_depth;
43 static char *option_origin = NULL;
44 static char *option_branch = NULL;
45 static const char *real_git_dir;
46 static char *option_upload_pack = "git-upload-pack";
47 static int option_verbosity;
48 static int option_progress = -1;
49 static struct string_list option_config;
50 static struct string_list option_reference;
51 static int option_dissociate;
53 static int opt_parse_reference(const struct option *opt, const char *arg, int unset)
55 struct string_list *option_reference = opt->value;
58 string_list_append(option_reference, arg);
62 static struct option builtin_clone_options[] = {
63 OPT__VERBOSITY(&option_verbosity),
64 OPT_BOOL(0, "progress", &option_progress,
65 N_("force progress reporting")),
66 OPT_BOOL('n', "no-checkout", &option_no_checkout,
67 N_("don't create a checkout")),
68 OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
69 OPT_HIDDEN_BOOL(0, "naked", &option_bare,
70 N_("create a bare repository")),
71 OPT_BOOL(0, "mirror", &option_mirror,
72 N_("create a mirror repository (implies bare)")),
73 OPT_BOOL('l', "local", &option_local,
74 N_("to clone from a local repository")),
75 OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
76 N_("don't use local hardlinks, always copy")),
77 OPT_BOOL('s', "shared", &option_shared,
78 N_("setup as shared repository")),
79 OPT_BOOL(0, "recursive", &option_recursive,
80 N_("initialize submodules in the clone")),
81 OPT_BOOL(0, "recurse-submodules", &option_recursive,
82 N_("initialize submodules in the clone")),
83 OPT_STRING(0, "template", &option_template, N_("template-directory"),
84 N_("directory from which templates will be used")),
85 OPT_CALLBACK(0 , "reference", &option_reference, N_("repo"),
86 N_("reference repository"), &opt_parse_reference),
87 OPT_STRING('o', "origin", &option_origin, N_("name"),
88 N_("use <name> instead of 'origin' to track upstream")),
89 OPT_STRING('b', "branch", &option_branch, N_("branch"),
90 N_("checkout <branch> instead of the remote's HEAD")),
91 OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
92 N_("path to git-upload-pack on the remote")),
93 OPT_STRING(0, "depth", &option_depth, N_("depth"),
94 N_("create a shallow clone of that depth")),
95 OPT_BOOL(0, "single-branch", &option_single_branch,
96 N_("clone only one branch, HEAD or --branch")),
97 OPT_BOOL(0, "dissociate", &option_dissociate,
98 N_("use --reference only while cloning")),
99 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
100 N_("separate git dir from working tree")),
101 OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
102 N_("set config inside the new repository")),
106 static const char *argv_submodule[] = {
107 "submodule", "update", "--init", "--recursive", NULL
110 static char *get_repo_path(const char *repo, int *is_bundle)
112 static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
113 static char *bundle_suffix[] = { ".bundle", "" };
117 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
119 path = mkpath("%s%s", repo, suffix[i]);
122 if (S_ISDIR(st.st_mode) && is_git_directory(path)) {
124 return xstrdup(absolute_path(path));
125 } else if (S_ISREG(st.st_mode) && st.st_size > 8) {
126 /* Is it a "gitfile"? */
128 int len, fd = open(path, O_RDONLY);
131 len = read_in_full(fd, signature, 8);
133 if (len != 8 || strncmp(signature, "gitdir: ", 8))
135 path = read_gitfile(path);
138 return xstrdup(absolute_path(path));
143 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
145 path = mkpath("%s%s", repo, bundle_suffix[i]);
146 if (!stat(path, &st) && S_ISREG(st.st_mode)) {
148 return xstrdup(absolute_path(path));
155 static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
157 const char *end = repo + strlen(repo), *start;
161 * Strip trailing spaces, slashes and /.git
163 while (repo < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
165 if (end - repo > 5 && is_dir_sep(end[-5]) &&
166 !strncmp(end - 4, ".git", 4)) {
168 while (repo < end && is_dir_sep(end[-1]))
173 * Find last component, but be prepared that repo could have
174 * the form "remote.example.com:foo.git", i.e. no slash
175 * in the directory part.
178 while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
182 * Strip .{bundle,git}.
185 if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
188 if (end - start > 4 && !strncmp(end - 4, ".git", 4))
193 struct strbuf result = STRBUF_INIT;
194 strbuf_addf(&result, "%.*s.git", (int)(end - start), start);
195 dir = strbuf_detach(&result, NULL);
197 dir = xstrndup(start, end - start);
199 * Replace sequences of 'control' characters and whitespace
200 * with one ascii space, remove leading and trailing spaces.
204 int prev_space = 1 /* strip leading whitespace */;
205 for (end = dir; *end; ++end) {
207 if ((unsigned char)ch < '\x20')
218 if (out > dir && prev_space)
224 static void strip_trailing_slashes(char *dir)
226 char *end = dir + strlen(dir);
228 while (dir < end - 1 && is_dir_sep(end[-1]))
233 static int add_one_reference(struct string_list_item *item, void *cb_data)
237 struct strbuf alternate = STRBUF_INIT;
239 /* Beware: read_gitfile(), real_path() and mkpath() return static buffer */
240 ref_git = xstrdup(real_path(item->string));
242 repo = read_gitfile(ref_git);
244 repo = read_gitfile(mkpath("%s/.git", ref_git));
247 ref_git = xstrdup(repo);
250 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
251 char *ref_git_git = mkpathdup("%s/.git", ref_git);
253 ref_git = ref_git_git;
254 } else if (!is_directory(mkpath("%s/objects", ref_git)))
255 die(_("reference repository '%s' is not a local repository."),
258 if (!access(mkpath("%s/shallow", ref_git), F_OK))
259 die(_("reference repository '%s' is shallow"), item->string);
261 if (!access(mkpath("%s/info/grafts", ref_git), F_OK))
262 die(_("reference repository '%s' is grafted"), item->string);
264 strbuf_addf(&alternate, "%s/objects", ref_git);
265 add_to_alternates_file(alternate.buf);
266 strbuf_release(&alternate);
271 static void setup_reference(void)
273 for_each_string_list(&option_reference, add_one_reference, NULL);
276 static void copy_alternates(struct strbuf *src, struct strbuf *dst,
277 const char *src_repo)
280 * Read from the source objects/info/alternates file
281 * and copy the entries to corresponding file in the
282 * destination repository with add_to_alternates_file().
283 * Both src and dst have "$path/objects/info/alternates".
285 * Instead of copying bit-for-bit from the original,
286 * we need to append to existing one so that the already
287 * created entry via "clone -s" is not lost, and also
288 * to turn entries with paths relative to the original
289 * absolute, so that they can be used in the new repository.
291 FILE *in = fopen(src->buf, "r");
292 struct strbuf line = STRBUF_INIT;
294 while (strbuf_getline(&line, in, '\n') != EOF) {
295 char *abs_path, abs_buf[PATH_MAX];
296 if (!line.len || line.buf[0] == '#')
298 if (is_absolute_path(line.buf)) {
299 add_to_alternates_file(line.buf);
302 abs_path = mkpath("%s/objects/%s", src_repo, line.buf);
303 normalize_path_copy(abs_buf, abs_path);
304 add_to_alternates_file(abs_buf);
306 strbuf_release(&line);
310 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
311 const char *src_repo, int src_baselen)
315 int src_len, dest_len;
318 dir = opendir(src->buf);
320 die_errno(_("failed to open '%s'"), src->buf);
322 if (mkdir(dest->buf, 0777)) {
324 die_errno(_("failed to create directory '%s'"), dest->buf);
325 else if (stat(dest->buf, &buf))
326 die_errno(_("failed to stat '%s'"), dest->buf);
327 else if (!S_ISDIR(buf.st_mode))
328 die(_("%s exists and is not a directory"), dest->buf);
331 strbuf_addch(src, '/');
333 strbuf_addch(dest, '/');
334 dest_len = dest->len;
336 while ((de = readdir(dir)) != NULL) {
337 strbuf_setlen(src, src_len);
338 strbuf_addstr(src, de->d_name);
339 strbuf_setlen(dest, dest_len);
340 strbuf_addstr(dest, de->d_name);
341 if (stat(src->buf, &buf)) {
342 warning (_("failed to stat %s\n"), src->buf);
345 if (S_ISDIR(buf.st_mode)) {
346 if (de->d_name[0] != '.')
347 copy_or_link_directory(src, dest,
348 src_repo, src_baselen);
352 /* Files that cannot be copied bit-for-bit... */
353 if (!strcmp(src->buf + src_baselen, "/info/alternates")) {
354 copy_alternates(src, dest, src_repo);
358 if (unlink(dest->buf) && errno != ENOENT)
359 die_errno(_("failed to unlink '%s'"), dest->buf);
360 if (!option_no_hardlinks) {
361 if (!link(src->buf, dest->buf))
363 if (option_local > 0)
364 die_errno(_("failed to create link '%s'"), dest->buf);
365 option_no_hardlinks = 1;
367 if (copy_file_with_time(dest->buf, src->buf, 0666))
368 die_errno(_("failed to copy file to '%s'"), dest->buf);
373 static void clone_local(const char *src_repo, const char *dest_repo)
376 struct strbuf alt = STRBUF_INIT;
377 strbuf_addf(&alt, "%s/objects", src_repo);
378 add_to_alternates_file(alt.buf);
379 strbuf_release(&alt);
381 struct strbuf src = STRBUF_INIT;
382 struct strbuf dest = STRBUF_INIT;
383 strbuf_addf(&src, "%s/objects", src_repo);
384 strbuf_addf(&dest, "%s/objects", dest_repo);
385 copy_or_link_directory(&src, &dest, src_repo, src.len);
386 strbuf_release(&src);
387 strbuf_release(&dest);
390 if (0 <= option_verbosity)
391 fprintf(stderr, _("done.\n"));
394 static const char *junk_work_tree;
395 static const char *junk_git_dir;
396 static pid_t junk_pid;
401 } junk_mode = JUNK_LEAVE_NONE;
403 static const char junk_leave_repo_msg[] =
404 N_("Clone succeeded, but checkout failed.\n"
405 "You can inspect what was checked out with 'git status'\n"
406 "and retry the checkout with 'git checkout -f HEAD'\n");
408 static void remove_junk(void)
410 struct strbuf sb = STRBUF_INIT;
413 case JUNK_LEAVE_REPO:
414 warning("%s", _(junk_leave_repo_msg));
419 /* proceed to removal */
423 if (getpid() != junk_pid)
426 strbuf_addstr(&sb, junk_git_dir);
427 remove_dir_recursively(&sb, 0);
430 if (junk_work_tree) {
431 strbuf_addstr(&sb, junk_work_tree);
432 remove_dir_recursively(&sb, 0);
437 static void remove_junk_on_signal(int signo)
444 static struct ref *find_remote_branch(const struct ref *refs, const char *branch)
447 struct strbuf head = STRBUF_INIT;
448 strbuf_addstr(&head, "refs/heads/");
449 strbuf_addstr(&head, branch);
450 ref = find_ref_by_name(refs, head.buf);
451 strbuf_release(&head);
456 strbuf_addstr(&head, "refs/tags/");
457 strbuf_addstr(&head, branch);
458 ref = find_ref_by_name(refs, head.buf);
459 strbuf_release(&head);
464 static struct ref *wanted_peer_refs(const struct ref *refs,
465 struct refspec *refspec)
467 struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD"));
468 struct ref *local_refs = head;
469 struct ref **tail = head ? &head->next : &local_refs;
471 if (option_single_branch) {
472 struct ref *remote_head = NULL;
475 remote_head = guess_remote_head(head, refs, 0);
479 remote_head = copy_ref(find_remote_branch(refs, option_branch));
482 if (!remote_head && option_branch)
483 warning(_("Could not find remote branch %s to clone."),
486 get_fetch_map(remote_head, refspec, &tail, 0);
488 /* if --branch=tag, pull the requested tag explicitly */
489 get_fetch_map(remote_head, tag_refspec, &tail, 0);
492 get_fetch_map(refs, refspec, &tail, 0);
494 if (!option_mirror && !option_single_branch)
495 get_fetch_map(refs, tag_refspec, &tail, 0);
500 static void write_remote_refs(const struct ref *local_refs)
504 lock_packed_refs(LOCK_DIE_ON_ERROR);
506 for (r = local_refs; r; r = r->next) {
509 add_packed_ref(r->peer_ref->name, r->old_sha1);
512 if (commit_packed_refs())
513 die_errno("unable to overwrite old ref-pack file");
516 static void write_followtags(const struct ref *refs, const char *msg)
518 const struct ref *ref;
519 for (ref = refs; ref; ref = ref->next) {
520 if (!starts_with(ref->name, "refs/tags/"))
522 if (ends_with(ref->name, "^{}"))
524 if (!has_sha1_file(ref->old_sha1))
526 update_ref(msg, ref->name, ref->old_sha1,
527 NULL, 0, UPDATE_REFS_DIE_ON_ERR);
531 static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
533 struct ref **rm = cb_data;
534 struct ref *ref = *rm;
537 * Skip anything missing a peer_ref, which we are not
538 * actually going to write a ref for.
540 while (ref && !ref->peer_ref)
542 /* Returning -1 notes "end of list" to the caller. */
546 hashcpy(sha1, ref->old_sha1);
551 static void update_remote_refs(const struct ref *refs,
552 const struct ref *mapped_refs,
553 const struct ref *remote_head_points_at,
554 const char *branch_top,
556 struct transport *transport,
557 int check_connectivity)
559 const struct ref *rm = mapped_refs;
561 if (check_connectivity) {
562 if (transport->progress)
563 fprintf(stderr, _("Checking connectivity... "));
564 if (check_everything_connected_with_transport(iterate_ref_map,
566 die(_("remote did not send all necessary objects"));
567 if (transport->progress)
568 fprintf(stderr, _("done.\n"));
572 write_remote_refs(mapped_refs);
573 if (option_single_branch)
574 write_followtags(refs, msg);
577 if (remote_head_points_at && !option_bare) {
578 struct strbuf head_ref = STRBUF_INIT;
579 strbuf_addstr(&head_ref, branch_top);
580 strbuf_addstr(&head_ref, "HEAD");
581 create_symref(head_ref.buf,
582 remote_head_points_at->peer_ref->name,
587 static void update_head(const struct ref *our, const struct ref *remote,
591 if (our && skip_prefix(our->name, "refs/heads/", &head)) {
592 /* Local default branch link */
593 create_symref("HEAD", our->name, NULL);
595 update_ref(msg, "HEAD", our->old_sha1, NULL, 0,
596 UPDATE_REFS_DIE_ON_ERR);
597 install_branch_config(0, head, option_origin, our->name);
600 struct commit *c = lookup_commit_reference(our->old_sha1);
601 /* --branch specifies a non-branch (i.e. tags), detach HEAD */
602 update_ref(msg, "HEAD", c->object.sha1,
603 NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
606 * We know remote HEAD points to a non-branch, or
607 * HEAD points to a branch but we don't know which one.
608 * Detach HEAD in all these cases.
610 update_ref(msg, "HEAD", remote->old_sha1,
611 NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
615 static int checkout(void)
617 unsigned char sha1[20];
619 struct lock_file *lock_file;
620 struct unpack_trees_options opts;
625 if (option_no_checkout)
628 head = resolve_refdup("HEAD", sha1, 1, NULL);
630 warning(_("remote HEAD refers to nonexistent ref, "
631 "unable to checkout.\n"));
634 if (!strcmp(head, "HEAD")) {
635 if (advice_detached_head)
636 detach_advice(sha1_to_hex(sha1));
638 if (!starts_with(head, "refs/heads/"))
639 die(_("HEAD not found below refs/heads!"));
643 /* We need to be in the new work tree for the checkout */
646 lock_file = xcalloc(1, sizeof(struct lock_file));
647 hold_locked_index(lock_file, 1);
649 memset(&opts, 0, sizeof opts);
652 opts.fn = oneway_merge;
653 opts.verbose_update = (option_verbosity >= 0);
654 opts.src_index = &the_index;
655 opts.dst_index = &the_index;
657 tree = parse_tree_indirect(sha1);
659 init_tree_desc(&t, tree->buffer, tree->size);
660 if (unpack_trees(1, &t, &opts) < 0)
661 die(_("unable to checkout working tree"));
663 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
664 die(_("unable to write new index file"));
666 err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
667 sha1_to_hex(sha1), "1", NULL);
669 if (!err && option_recursive)
670 err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
675 static int write_one_config(const char *key, const char *value, void *data)
677 return git_config_set_multivar(key, value ? value : "true", "^$", 0);
680 static void write_config(struct string_list *config)
684 for (i = 0; i < config->nr; i++) {
685 if (git_config_parse_parameter(config->items[i].string,
686 write_one_config, NULL) < 0)
687 die("unable to write parameters to config file");
691 static void write_refspec_config(const char* src_ref_prefix,
692 const struct ref* our_head_points_at,
693 const struct ref* remote_head_points_at, struct strbuf* branch_top)
695 struct strbuf key = STRBUF_INIT;
696 struct strbuf value = STRBUF_INIT;
698 if (option_mirror || !option_bare) {
699 if (option_single_branch && !option_mirror) {
701 if (starts_with(our_head_points_at->name, "refs/tags/"))
702 strbuf_addf(&value, "+%s:%s", our_head_points_at->name,
703 our_head_points_at->name);
705 strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
706 branch_top->buf, option_branch);
707 } else if (remote_head_points_at) {
708 const char *head = remote_head_points_at->name;
709 if (!skip_prefix(head, "refs/heads/", &head))
710 die("BUG: remote HEAD points at non-head?");
712 strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name,
713 branch_top->buf, head);
716 * otherwise, the next "git fetch" will
717 * simply fetch from HEAD without updating
718 * any remote-tracking branch, which is what
722 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf);
724 /* Configure the remote */
726 strbuf_addf(&key, "remote.%s.fetch", option_origin);
727 git_config_set_multivar(key.buf, value.buf, "^$", 0);
731 strbuf_addf(&key, "remote.%s.mirror", option_origin);
732 git_config_set(key.buf, "true");
738 strbuf_release(&key);
739 strbuf_release(&value);
742 static void dissociate_from_references(void)
744 static const char* argv[] = { "repack", "-a", "-d", NULL };
746 if (run_command_v_opt(argv, RUN_GIT_CMD|RUN_COMMAND_NO_STDIN))
747 die(_("cannot repack to clean up"));
748 if (unlink(git_path("objects/info/alternates")) && errno != ENOENT)
749 die_errno(_("cannot unlink temporary alternates file"));
752 int cmd_clone(int argc, const char **argv, const char *prefix)
754 int is_bundle = 0, is_local;
756 const char *repo_name, *repo, *work_tree, *git_dir;
759 const struct ref *refs, *remote_head;
760 const struct ref *remote_head_points_at;
761 const struct ref *our_head_points_at;
762 struct ref *mapped_refs;
763 const struct ref *ref;
764 struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
765 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
766 struct transport *transport = NULL;
767 const char *src_ref_prefix = "refs/heads/";
768 struct remote *remote;
769 int err = 0, complete_refs_before_fetch = 1;
771 struct refspec *refspec;
772 const char *fetch_pattern;
776 packet_trace_identity("clone");
777 argc = parse_options(argc, argv, prefix, builtin_clone_options,
778 builtin_clone_usage, 0);
781 usage_msg_opt(_("Too many arguments."),
782 builtin_clone_usage, builtin_clone_options);
785 usage_msg_opt(_("You must specify a repository to clone."),
786 builtin_clone_usage, builtin_clone_options);
788 if (option_single_branch == -1)
789 option_single_branch = option_depth ? 1 : 0;
796 die(_("--bare and --origin %s options are incompatible."),
799 die(_("--bare and --separate-git-dir are incompatible."));
800 option_no_checkout = 1;
804 option_origin = "origin";
808 path = get_repo_path(repo_name, &is_bundle);
810 repo = xstrdup(absolute_path(repo_name));
811 else if (!strchr(repo_name, ':'))
812 die(_("repository '%s' does not exist"), repo_name);
816 /* no need to be strict, transport_set_option() will validate it again */
817 if (option_depth && atoi(option_depth) < 1)
818 die(_("depth %s is not a positive number"), option_depth);
821 dir = xstrdup(argv[1]);
823 dir = guess_dir_name(repo_name, is_bundle, option_bare);
824 strip_trailing_slashes(dir);
826 dest_exists = !stat(dir, &buf);
827 if (dest_exists && !is_empty_dir(dir))
828 die(_("destination path '%s' already exists and is not "
829 "an empty directory."), dir);
831 strbuf_addf(&reflog_msg, "clone: from %s", repo);
836 work_tree = getenv("GIT_WORK_TREE");
837 if (work_tree && !stat(work_tree, &buf))
838 die(_("working tree '%s' already exists."), work_tree);
841 if (option_bare || work_tree)
842 git_dir = xstrdup(dir);
845 git_dir = mkpathdup("%s/.git", dir);
849 junk_work_tree = work_tree;
850 if (safe_create_leading_directories_const(work_tree) < 0)
851 die_errno(_("could not create leading directories of '%s'"),
853 if (!dest_exists && mkdir(work_tree, 0777))
854 die_errno(_("could not create work tree dir '%s'."),
856 set_git_work_tree(work_tree);
858 junk_git_dir = git_dir;
860 sigchain_push_common(remove_junk_on_signal);
862 if (safe_create_leading_directories_const(git_dir) < 0)
863 die(_("could not create leading directories of '%s'"), git_dir);
865 set_git_dir_init(git_dir, real_git_dir, 0);
867 git_dir = real_git_dir;
868 junk_git_dir = real_git_dir;
871 if (0 <= option_verbosity) {
873 fprintf(stderr, _("Cloning into bare repository '%s'...\n"), dir);
875 fprintf(stderr, _("Cloning into '%s'...\n"), dir);
877 init_db(option_template, INIT_DB_QUIET);
878 write_config(&option_config);
880 git_config(git_default_config, NULL);
884 src_ref_prefix = "refs/";
885 strbuf_addstr(&branch_top, src_ref_prefix);
887 git_config_set("core.bare", "true");
889 strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
892 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
893 strbuf_addf(&key, "remote.%s.url", option_origin);
894 git_config_set(key.buf, repo);
897 if (option_reference.nr)
899 else if (option_dissociate) {
900 warning(_("--dissociate given, but there is no --reference"));
901 option_dissociate = 0;
904 fetch_pattern = value.buf;
905 refspec = parse_fetch_refspec(1, &fetch_pattern);
907 strbuf_reset(&value);
909 remote = remote_get(option_origin);
910 transport = transport_get(remote, remote->url[0]);
911 path = get_repo_path(remote->url[0], &is_bundle);
912 is_local = option_local != 0 && path && !is_bundle;
915 warning(_("--depth is ignored in local clones; use file:// instead."));
916 if (!access(mkpath("%s/shallow", path), F_OK)) {
917 if (option_local > 0)
918 warning(_("source repository is shallow, ignoring --local"));
922 if (option_local > 0 && !is_local)
923 warning(_("--local is ignored"));
924 transport->cloning = 1;
926 if (!transport->get_refs_list || (!is_local && !transport->fetch))
927 die(_("Don't know how to clone %s"), transport->url);
929 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
932 transport_set_option(transport, TRANS_OPT_DEPTH,
934 if (option_single_branch)
935 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
937 transport_set_verbosity(transport, option_verbosity, option_progress);
939 if (option_upload_pack)
940 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
943 if (transport->smart_options && !option_depth)
944 transport->smart_options->check_self_contained_and_connected = 1;
946 refs = transport_get_remote_refs(transport);
949 mapped_refs = wanted_peer_refs(refs, refspec);
951 * transport_get_remote_refs() may return refs with null sha-1
952 * in mapped_refs (see struct transport->get_refs_list
953 * comment). In that case we need fetch it early because
954 * remote_head code below relies on it.
956 * for normal clones, transport_get_remote_refs() should
957 * return reliable ref set, we can delay cloning until after
960 for (ref = refs; ref; ref = ref->next)
961 if (is_null_sha1(ref->old_sha1)) {
962 complete_refs_before_fetch = 0;
966 if (!is_local && !complete_refs_before_fetch)
967 transport_fetch_refs(transport, mapped_refs);
969 remote_head = find_ref_by_name(refs, "HEAD");
970 remote_head_points_at =
971 guess_remote_head(remote_head, mapped_refs, 0);
975 find_remote_branch(mapped_refs, option_branch);
977 if (!our_head_points_at)
978 die(_("Remote branch %s not found in upstream %s"),
979 option_branch, option_origin);
982 our_head_points_at = remote_head_points_at;
986 die(_("Remote branch %s not found in upstream %s"),
987 option_branch, option_origin);
989 warning(_("You appear to have cloned an empty repository."));
991 our_head_points_at = NULL;
992 remote_head_points_at = NULL;
994 option_no_checkout = 1;
996 install_branch_config(0, "master", option_origin,
997 "refs/heads/master");
1000 write_refspec_config(src_ref_prefix, our_head_points_at,
1001 remote_head_points_at, &branch_top);
1004 clone_local(path, git_dir);
1005 else if (refs && complete_refs_before_fetch)
1006 transport_fetch_refs(transport, mapped_refs);
1008 update_remote_refs(refs, mapped_refs, remote_head_points_at,
1009 branch_top.buf, reflog_msg.buf, transport, !is_local);
1011 update_head(our_head_points_at, remote_head, reflog_msg.buf);
1013 transport_unlock_pack(transport);
1014 transport_disconnect(transport);
1016 if (option_dissociate)
1017 dissociate_from_references();
1019 junk_mode = JUNK_LEAVE_REPO;
1022 strbuf_release(&reflog_msg);
1023 strbuf_release(&branch_top);
1024 strbuf_release(&key);
1025 strbuf_release(&value);
1026 junk_mode = JUNK_LEAVE_ALL;