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.
14 #include "parse-options.h"
15 #include "fetch-pack.h"
18 #include "tree-walk.h"
19 #include "unpack-trees.h"
20 #include "transport.h"
26 #include "run-command.h"
27 #include "connected.h"
32 * - respect DB_ENVIRONMENT for .git/objects.
34 * Implementation notes:
35 * - dropping use-separate-remote and no-separate-remote compatibility
38 static const char * const builtin_clone_usage
[] = {
39 N_("git clone [<options>] [--] <repo> [<dir>]"),
43 static int option_no_checkout
, option_bare
, option_mirror
, option_single_branch
= -1;
44 static int option_local
= -1, option_no_hardlinks
, option_shared
;
45 static int option_no_tags
;
46 static int option_shallow_submodules
;
48 static char *option_template
, *option_depth
, *option_since
;
49 static char *option_origin
= NULL
;
50 static char *option_branch
= NULL
;
51 static struct string_list option_not
= STRING_LIST_INIT_NODUP
;
52 static const char *real_git_dir
;
53 static char *option_upload_pack
= "git-upload-pack";
54 static int option_verbosity
;
55 static int option_progress
= -1;
56 static enum transport_family family
;
57 static struct string_list option_config
= STRING_LIST_INIT_NODUP
;
58 static struct string_list option_required_reference
= STRING_LIST_INIT_NODUP
;
59 static struct string_list option_optional_reference
= STRING_LIST_INIT_NODUP
;
60 static int option_dissociate
;
61 static int max_jobs
= -1;
62 static struct string_list option_recurse_submodules
= STRING_LIST_INIT_NODUP
;
64 static int recurse_submodules_cb(const struct option
*opt
,
65 const char *arg
, int unset
)
68 string_list_clear((struct string_list
*)opt
->value
, 0);
70 string_list_append((struct string_list
*)opt
->value
, arg
);
72 string_list_append((struct string_list
*)opt
->value
,
73 (const char *)opt
->defval
);
78 static struct option builtin_clone_options
[] = {
79 OPT__VERBOSITY(&option_verbosity
),
80 OPT_BOOL(0, "progress", &option_progress
,
81 N_("force progress reporting")),
82 OPT_BOOL('n', "no-checkout", &option_no_checkout
,
83 N_("don't create a checkout")),
84 OPT_BOOL(0, "bare", &option_bare
, N_("create a bare repository")),
85 OPT_HIDDEN_BOOL(0, "naked", &option_bare
,
86 N_("create a bare repository")),
87 OPT_BOOL(0, "mirror", &option_mirror
,
88 N_("create a mirror repository (implies bare)")),
89 OPT_BOOL('l', "local", &option_local
,
90 N_("to clone from a local repository")),
91 OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks
,
92 N_("don't use local hardlinks, always copy")),
93 OPT_BOOL('s', "shared", &option_shared
,
94 N_("setup as shared repository")),
95 { OPTION_CALLBACK
, 0, "recursive", &option_recurse_submodules
,
96 N_("pathspec"), N_("initialize submodules in the clone"),
97 PARSE_OPT_OPTARG
| PARSE_OPT_HIDDEN
, recurse_submodules_cb
,
99 { OPTION_CALLBACK
, 0, "recurse-submodules", &option_recurse_submodules
,
100 N_("pathspec"), N_("initialize submodules in the clone"),
101 PARSE_OPT_OPTARG
, recurse_submodules_cb
, (intptr_t)"." },
102 OPT_INTEGER('j', "jobs", &max_jobs
,
103 N_("number of submodules cloned in parallel")),
104 OPT_STRING(0, "template", &option_template
, N_("template-directory"),
105 N_("directory from which templates will be used")),
106 OPT_STRING_LIST(0, "reference", &option_required_reference
, N_("repo"),
107 N_("reference repository")),
108 OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference
,
109 N_("repo"), N_("reference repository")),
110 OPT_BOOL(0, "dissociate", &option_dissociate
,
111 N_("use --reference only while cloning")),
112 OPT_STRING('o', "origin", &option_origin
, N_("name"),
113 N_("use <name> instead of 'origin' to track upstream")),
114 OPT_STRING('b', "branch", &option_branch
, N_("branch"),
115 N_("checkout <branch> instead of the remote's HEAD")),
116 OPT_STRING('u', "upload-pack", &option_upload_pack
, N_("path"),
117 N_("path to git-upload-pack on the remote")),
118 OPT_STRING(0, "depth", &option_depth
, N_("depth"),
119 N_("create a shallow clone of that depth")),
120 OPT_STRING(0, "shallow-since", &option_since
, N_("time"),
121 N_("create a shallow clone since a specific time")),
122 OPT_STRING_LIST(0, "shallow-exclude", &option_not
, N_("revision"),
123 N_("deepen history of shallow clone, excluding rev")),
124 OPT_BOOL(0, "single-branch", &option_single_branch
,
125 N_("clone only one branch, HEAD or --branch")),
126 OPT_BOOL(0, "no-tags", &option_no_tags
,
127 N_("don't clone any tags, and make later fetches not to follow them")),
128 OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules
,
129 N_("any cloned submodules will be shallow")),
130 OPT_STRING(0, "separate-git-dir", &real_git_dir
, N_("gitdir"),
131 N_("separate git dir from working tree")),
132 OPT_STRING_LIST('c', "config", &option_config
, N_("key=value"),
133 N_("set config inside the new repository")),
134 OPT_SET_INT('4', "ipv4", &family
, N_("use IPv4 addresses only"),
135 TRANSPORT_FAMILY_IPV4
),
136 OPT_SET_INT('6', "ipv6", &family
, N_("use IPv6 addresses only"),
137 TRANSPORT_FAMILY_IPV6
),
141 static const char *get_repo_path_1(struct strbuf
*path
, int *is_bundle
)
143 static char *suffix
[] = { "/.git", "", ".git/.git", ".git" };
144 static char *bundle_suffix
[] = { ".bundle", "" };
145 size_t baselen
= path
->len
;
149 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
150 strbuf_setlen(path
, baselen
);
151 strbuf_addstr(path
, suffix
[i
]);
152 if (stat(path
->buf
, &st
))
154 if (S_ISDIR(st
.st_mode
) && is_git_directory(path
->buf
)) {
157 } else if (S_ISREG(st
.st_mode
) && st
.st_size
> 8) {
158 /* Is it a "gitfile"? */
161 int len
, fd
= open(path
->buf
, O_RDONLY
);
164 len
= read_in_full(fd
, signature
, 8);
166 if (len
!= 8 || strncmp(signature
, "gitdir: ", 8))
168 dst
= read_gitfile(path
->buf
);
176 for (i
= 0; i
< ARRAY_SIZE(bundle_suffix
); i
++) {
177 strbuf_setlen(path
, baselen
);
178 strbuf_addstr(path
, bundle_suffix
[i
]);
179 if (!stat(path
->buf
, &st
) && S_ISREG(st
.st_mode
)) {
188 static char *get_repo_path(const char *repo
, int *is_bundle
)
190 struct strbuf path
= STRBUF_INIT
;
194 strbuf_addstr(&path
, repo
);
195 raw
= get_repo_path_1(&path
, is_bundle
);
196 canon
= raw
? absolute_pathdup(raw
) : NULL
;
197 strbuf_release(&path
);
201 static char *guess_dir_name(const char *repo
, int is_bundle
, int is_bare
)
203 const char *end
= repo
+ strlen(repo
), *start
, *ptr
;
210 start
= strstr(repo
, "://");
217 * Skip authentication data. The stripping does happen
218 * greedily, such that we strip up to the last '@' inside
221 for (ptr
= start
; ptr
< end
&& !is_dir_sep(*ptr
); ptr
++) {
227 * Strip trailing spaces, slashes and /.git
229 while (start
< end
&& (is_dir_sep(end
[-1]) || isspace(end
[-1])))
231 if (end
- start
> 5 && is_dir_sep(end
[-5]) &&
232 !strncmp(end
- 4, ".git", 4)) {
234 while (start
< end
&& is_dir_sep(end
[-1]))
239 * Strip trailing port number if we've got only a
240 * hostname (that is, there is no dir separator but a
241 * colon). This check is required such that we do not
242 * strip URI's like '/foo/bar:2222.git', which should
243 * result in a dir '2222' being guessed due to backwards
246 if (memchr(start
, '/', end
- start
) == NULL
247 && memchr(start
, ':', end
- start
) != NULL
) {
249 while (start
< ptr
&& isdigit(ptr
[-1]) && ptr
[-1] != ':')
251 if (start
< ptr
&& ptr
[-1] == ':')
256 * Find last component. To remain backwards compatible we
257 * also regard colons as path separators, such that
258 * cloning a repository 'foo:bar.git' would result in a
259 * directory 'bar' being guessed.
262 while (start
< ptr
&& !is_dir_sep(ptr
[-1]) && ptr
[-1] != ':')
267 * Strip .{bundle,git}.
270 strip_suffix_mem(start
, &len
, is_bundle
? ".bundle" : ".git");
272 if (!len
|| (len
== 1 && *start
== '/'))
273 die(_("No directory name could be guessed.\n"
274 "Please specify a directory on the command line"));
277 dir
= xstrfmt("%.*s.git", (int)len
, start
);
279 dir
= xstrndup(start
, len
);
281 * Replace sequences of 'control' characters and whitespace
282 * with one ascii space, remove leading and trailing spaces.
286 int prev_space
= 1 /* strip leading whitespace */;
287 for (end
= dir
; *end
; ++end
) {
289 if ((unsigned char)ch
< '\x20')
300 if (out
> dir
&& prev_space
)
306 static void strip_trailing_slashes(char *dir
)
308 char *end
= dir
+ strlen(dir
);
310 while (dir
< end
- 1 && is_dir_sep(end
[-1]))
315 static int add_one_reference(struct string_list_item
*item
, void *cb_data
)
317 struct strbuf err
= STRBUF_INIT
;
318 int *required
= cb_data
;
319 char *ref_git
= compute_alternate_path(item
->string
, &err
);
326 _("info: Could not add alternate for '%s': %s\n"),
327 item
->string
, err
.buf
);
329 struct strbuf sb
= STRBUF_INIT
;
330 strbuf_addf(&sb
, "%s/objects", ref_git
);
331 add_to_alternates_file(sb
.buf
);
335 strbuf_release(&err
);
340 static void setup_reference(void)
343 for_each_string_list(&option_required_reference
,
344 add_one_reference
, &required
);
346 for_each_string_list(&option_optional_reference
,
347 add_one_reference
, &required
);
350 static void copy_alternates(struct strbuf
*src
, struct strbuf
*dst
,
351 const char *src_repo
)
354 * Read from the source objects/info/alternates file
355 * and copy the entries to corresponding file in the
356 * destination repository with add_to_alternates_file().
357 * Both src and dst have "$path/objects/info/alternates".
359 * Instead of copying bit-for-bit from the original,
360 * we need to append to existing one so that the already
361 * created entry via "clone -s" is not lost, and also
362 * to turn entries with paths relative to the original
363 * absolute, so that they can be used in the new repository.
365 FILE *in
= xfopen(src
->buf
, "r");
366 struct strbuf line
= STRBUF_INIT
;
368 while (strbuf_getline(&line
, in
) != EOF
) {
370 if (!line
.len
|| line
.buf
[0] == '#')
372 if (is_absolute_path(line
.buf
)) {
373 add_to_alternates_file(line
.buf
);
376 abs_path
= mkpathdup("%s/objects/%s", src_repo
, line
.buf
);
377 if (!normalize_path_copy(abs_path
, abs_path
))
378 add_to_alternates_file(abs_path
);
380 warning("skipping invalid relative alternate: %s/%s",
384 strbuf_release(&line
);
388 static void copy_or_link_directory(struct strbuf
*src
, struct strbuf
*dest
,
389 const char *src_repo
, int src_baselen
)
393 int src_len
, dest_len
;
396 dir
= opendir(src
->buf
);
398 die_errno(_("failed to open '%s'"), src
->buf
);
400 if (mkdir(dest
->buf
, 0777)) {
402 die_errno(_("failed to create directory '%s'"), dest
->buf
);
403 else if (stat(dest
->buf
, &buf
))
404 die_errno(_("failed to stat '%s'"), dest
->buf
);
405 else if (!S_ISDIR(buf
.st_mode
))
406 die(_("%s exists and is not a directory"), dest
->buf
);
409 strbuf_addch(src
, '/');
411 strbuf_addch(dest
, '/');
412 dest_len
= dest
->len
;
414 while ((de
= readdir(dir
)) != NULL
) {
415 strbuf_setlen(src
, src_len
);
416 strbuf_addstr(src
, de
->d_name
);
417 strbuf_setlen(dest
, dest_len
);
418 strbuf_addstr(dest
, de
->d_name
);
419 if (stat(src
->buf
, &buf
)) {
420 warning (_("failed to stat %s\n"), src
->buf
);
423 if (S_ISDIR(buf
.st_mode
)) {
424 if (de
->d_name
[0] != '.')
425 copy_or_link_directory(src
, dest
,
426 src_repo
, src_baselen
);
430 /* Files that cannot be copied bit-for-bit... */
431 if (!strcmp(src
->buf
+ src_baselen
, "/info/alternates")) {
432 copy_alternates(src
, dest
, src_repo
);
436 if (unlink(dest
->buf
) && errno
!= ENOENT
)
437 die_errno(_("failed to unlink '%s'"), dest
->buf
);
438 if (!option_no_hardlinks
) {
439 if (!link(src
->buf
, dest
->buf
))
441 if (option_local
> 0)
442 die_errno(_("failed to create link '%s'"), dest
->buf
);
443 option_no_hardlinks
= 1;
445 if (copy_file_with_time(dest
->buf
, src
->buf
, 0666))
446 die_errno(_("failed to copy file to '%s'"), dest
->buf
);
451 static void clone_local(const char *src_repo
, const char *dest_repo
)
454 struct strbuf alt
= STRBUF_INIT
;
455 get_common_dir(&alt
, src_repo
);
456 strbuf_addstr(&alt
, "/objects");
457 add_to_alternates_file(alt
.buf
);
458 strbuf_release(&alt
);
460 struct strbuf src
= STRBUF_INIT
;
461 struct strbuf dest
= STRBUF_INIT
;
462 get_common_dir(&src
, src_repo
);
463 get_common_dir(&dest
, dest_repo
);
464 strbuf_addstr(&src
, "/objects");
465 strbuf_addstr(&dest
, "/objects");
466 copy_or_link_directory(&src
, &dest
, src_repo
, src
.len
);
467 strbuf_release(&src
);
468 strbuf_release(&dest
);
471 if (0 <= option_verbosity
)
472 fprintf(stderr
, _("done.\n"));
475 static const char *junk_work_tree
;
476 static int junk_work_tree_flags
;
477 static const char *junk_git_dir
;
478 static int junk_git_dir_flags
;
483 } junk_mode
= JUNK_LEAVE_NONE
;
485 static const char junk_leave_repo_msg
[] =
486 N_("Clone succeeded, but checkout failed.\n"
487 "You can inspect what was checked out with 'git status'\n"
488 "and retry the checkout with 'git checkout -f HEAD'\n");
490 static void remove_junk(void)
492 struct strbuf sb
= STRBUF_INIT
;
495 case JUNK_LEAVE_REPO
:
496 warning("%s", _(junk_leave_repo_msg
));
501 /* proceed to removal */
506 strbuf_addstr(&sb
, junk_git_dir
);
507 remove_dir_recursively(&sb
, junk_git_dir_flags
);
510 if (junk_work_tree
) {
511 strbuf_addstr(&sb
, junk_work_tree
);
512 remove_dir_recursively(&sb
, junk_work_tree_flags
);
517 static void remove_junk_on_signal(int signo
)
524 static struct ref
*find_remote_branch(const struct ref
*refs
, const char *branch
)
527 struct strbuf head
= STRBUF_INIT
;
528 strbuf_addstr(&head
, "refs/heads/");
529 strbuf_addstr(&head
, branch
);
530 ref
= find_ref_by_name(refs
, head
.buf
);
531 strbuf_release(&head
);
536 strbuf_addstr(&head
, "refs/tags/");
537 strbuf_addstr(&head
, branch
);
538 ref
= find_ref_by_name(refs
, head
.buf
);
539 strbuf_release(&head
);
544 static struct ref
*wanted_peer_refs(const struct ref
*refs
,
545 struct refspec
*refspec
)
547 struct ref
*head
= copy_ref(find_ref_by_name(refs
, "HEAD"));
548 struct ref
*local_refs
= head
;
549 struct ref
**tail
= head
? &head
->next
: &local_refs
;
551 if (option_single_branch
) {
552 struct ref
*remote_head
= NULL
;
555 remote_head
= guess_remote_head(head
, refs
, 0);
559 remote_head
= copy_ref(find_remote_branch(refs
, option_branch
));
562 if (!remote_head
&& option_branch
)
563 warning(_("Could not find remote branch %s to clone."),
566 get_fetch_map(remote_head
, refspec
, &tail
, 0);
568 /* if --branch=tag, pull the requested tag explicitly */
569 get_fetch_map(remote_head
, tag_refspec
, &tail
, 0);
572 get_fetch_map(refs
, refspec
, &tail
, 0);
574 if (!option_mirror
&& !option_single_branch
&& !option_no_tags
)
575 get_fetch_map(refs
, tag_refspec
, &tail
, 0);
580 static void write_remote_refs(const struct ref
*local_refs
)
584 struct ref_transaction
*t
;
585 struct strbuf err
= STRBUF_INIT
;
587 t
= ref_transaction_begin(&err
);
591 for (r
= local_refs
; r
; r
= r
->next
) {
594 if (ref_transaction_create(t
, r
->peer_ref
->name
, &r
->old_oid
,
599 if (initial_ref_transaction_commit(t
, &err
))
602 strbuf_release(&err
);
603 ref_transaction_free(t
);
606 static void write_followtags(const struct ref
*refs
, const char *msg
)
608 const struct ref
*ref
;
609 for (ref
= refs
; ref
; ref
= ref
->next
) {
610 if (!starts_with(ref
->name
, "refs/tags/"))
612 if (ends_with(ref
->name
, "^{}"))
614 if (!has_object_file(&ref
->old_oid
))
616 update_ref(msg
, ref
->name
, &ref
->old_oid
, NULL
, 0,
617 UPDATE_REFS_DIE_ON_ERR
);
621 static int iterate_ref_map(void *cb_data
, struct object_id
*oid
)
623 struct ref
**rm
= cb_data
;
624 struct ref
*ref
= *rm
;
627 * Skip anything missing a peer_ref, which we are not
628 * actually going to write a ref for.
630 while (ref
&& !ref
->peer_ref
)
632 /* Returning -1 notes "end of list" to the caller. */
636 oidcpy(oid
, &ref
->old_oid
);
641 static void update_remote_refs(const struct ref
*refs
,
642 const struct ref
*mapped_refs
,
643 const struct ref
*remote_head_points_at
,
644 const char *branch_top
,
646 struct transport
*transport
,
647 int check_connectivity
)
649 const struct ref
*rm
= mapped_refs
;
651 if (check_connectivity
) {
652 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
654 opt
.transport
= transport
;
655 opt
.progress
= transport
->progress
;
657 if (check_connected(iterate_ref_map
, &rm
, &opt
))
658 die(_("remote did not send all necessary objects"));
662 write_remote_refs(mapped_refs
);
663 if (option_single_branch
&& !option_no_tags
)
664 write_followtags(refs
, msg
);
667 if (remote_head_points_at
&& !option_bare
) {
668 struct strbuf head_ref
= STRBUF_INIT
;
669 strbuf_addstr(&head_ref
, branch_top
);
670 strbuf_addstr(&head_ref
, "HEAD");
671 if (create_symref(head_ref
.buf
,
672 remote_head_points_at
->peer_ref
->name
,
674 die(_("unable to update %s"), head_ref
.buf
);
675 strbuf_release(&head_ref
);
679 static void update_head(const struct ref
*our
, const struct ref
*remote
,
683 if (our
&& skip_prefix(our
->name
, "refs/heads/", &head
)) {
684 /* Local default branch link */
685 if (create_symref("HEAD", our
->name
, NULL
) < 0)
686 die(_("unable to update HEAD"));
688 update_ref(msg
, "HEAD", &our
->old_oid
, NULL
, 0,
689 UPDATE_REFS_DIE_ON_ERR
);
690 install_branch_config(0, head
, option_origin
, our
->name
);
693 struct commit
*c
= lookup_commit_reference(&our
->old_oid
);
694 /* --branch specifies a non-branch (i.e. tags), detach HEAD */
695 update_ref(msg
, "HEAD", &c
->object
.oid
, NULL
, REF_NO_DEREF
,
696 UPDATE_REFS_DIE_ON_ERR
);
699 * We know remote HEAD points to a non-branch, or
700 * HEAD points to a branch but we don't know which one.
701 * Detach HEAD in all these cases.
703 update_ref(msg
, "HEAD", &remote
->old_oid
, NULL
, REF_NO_DEREF
,
704 UPDATE_REFS_DIE_ON_ERR
);
708 static int checkout(int submodule_progress
)
710 struct object_id oid
;
712 struct lock_file lock_file
= LOCK_INIT
;
713 struct unpack_trees_options opts
;
718 if (option_no_checkout
)
721 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, &oid
, NULL
);
723 warning(_("remote HEAD refers to nonexistent ref, "
724 "unable to checkout.\n"));
727 if (!strcmp(head
, "HEAD")) {
728 if (advice_detached_head
)
729 detach_advice(oid_to_hex(&oid
));
731 if (!starts_with(head
, "refs/heads/"))
732 die(_("HEAD not found below refs/heads!"));
736 /* We need to be in the new work tree for the checkout */
739 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
741 memset(&opts
, 0, sizeof opts
);
744 opts
.fn
= oneway_merge
;
745 opts
.verbose_update
= (option_verbosity
>= 0);
746 opts
.src_index
= &the_index
;
747 opts
.dst_index
= &the_index
;
749 tree
= parse_tree_indirect(&oid
);
751 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
752 if (unpack_trees(1, &t
, &opts
) < 0)
753 die(_("unable to checkout working tree"));
755 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
756 die(_("unable to write new index file"));
758 err
|= run_hook_le(NULL
, "post-checkout", sha1_to_hex(null_sha1
),
759 oid_to_hex(&oid
), "1", NULL
);
761 if (!err
&& (option_recurse_submodules
.nr
> 0)) {
762 struct argv_array args
= ARGV_ARRAY_INIT
;
763 argv_array_pushl(&args
, "submodule", "update", "--init", "--recursive", NULL
);
765 if (option_shallow_submodules
== 1)
766 argv_array_push(&args
, "--depth=1");
769 argv_array_pushf(&args
, "--jobs=%d", max_jobs
);
771 if (submodule_progress
)
772 argv_array_push(&args
, "--progress");
774 if (option_verbosity
< 0)
775 argv_array_push(&args
, "--quiet");
777 err
= run_command_v_opt(args
.argv
, RUN_GIT_CMD
);
778 argv_array_clear(&args
);
784 static int write_one_config(const char *key
, const char *value
, void *data
)
786 return git_config_set_multivar_gently(key
,
787 value
? value
: "true",
788 CONFIG_REGEX_NONE
, 0);
791 static void write_config(struct string_list
*config
)
795 for (i
= 0; i
< config
->nr
; i
++) {
796 if (git_config_parse_parameter(config
->items
[i
].string
,
797 write_one_config
, NULL
) < 0)
798 die(_("unable to write parameters to config file"));
802 static void write_refspec_config(const char *src_ref_prefix
,
803 const struct ref
*our_head_points_at
,
804 const struct ref
*remote_head_points_at
,
805 struct strbuf
*branch_top
)
807 struct strbuf key
= STRBUF_INIT
;
808 struct strbuf value
= STRBUF_INIT
;
810 if (option_mirror
|| !option_bare
) {
811 if (option_single_branch
&& !option_mirror
) {
813 if (starts_with(our_head_points_at
->name
, "refs/tags/"))
814 strbuf_addf(&value
, "+%s:%s", our_head_points_at
->name
,
815 our_head_points_at
->name
);
817 strbuf_addf(&value
, "+%s:%s%s", our_head_points_at
->name
,
818 branch_top
->buf
, option_branch
);
819 } else if (remote_head_points_at
) {
820 const char *head
= remote_head_points_at
->name
;
821 if (!skip_prefix(head
, "refs/heads/", &head
))
822 die("BUG: remote HEAD points at non-head?");
824 strbuf_addf(&value
, "+%s:%s%s", remote_head_points_at
->name
,
825 branch_top
->buf
, head
);
828 * otherwise, the next "git fetch" will
829 * simply fetch from HEAD without updating
830 * any remote-tracking branch, which is what
834 strbuf_addf(&value
, "+%s*:%s*", src_ref_prefix
, branch_top
->buf
);
836 /* Configure the remote */
838 strbuf_addf(&key
, "remote.%s.fetch", option_origin
);
839 git_config_set_multivar(key
.buf
, value
.buf
, "^$", 0);
843 strbuf_addf(&key
, "remote.%s.mirror", option_origin
);
844 git_config_set(key
.buf
, "true");
850 strbuf_release(&key
);
851 strbuf_release(&value
);
854 static void dissociate_from_references(void)
856 static const char* argv
[] = { "repack", "-a", "-d", NULL
};
857 char *alternates
= git_pathdup("objects/info/alternates");
859 if (!access(alternates
, F_OK
)) {
860 if (run_command_v_opt(argv
, RUN_GIT_CMD
|RUN_COMMAND_NO_STDIN
))
861 die(_("cannot repack to clean up"));
862 if (unlink(alternates
) && errno
!= ENOENT
)
863 die_errno(_("cannot unlink temporary alternates file"));
868 static int dir_exists(const char *path
)
871 return !stat(path
, &sb
);
874 int cmd_clone(int argc
, const char **argv
, const char *prefix
)
876 int is_bundle
= 0, is_local
;
877 const char *repo_name
, *repo
, *work_tree
, *git_dir
;
880 const struct ref
*refs
, *remote_head
;
881 const struct ref
*remote_head_points_at
;
882 const struct ref
*our_head_points_at
;
883 struct ref
*mapped_refs
;
884 const struct ref
*ref
;
885 struct strbuf key
= STRBUF_INIT
, value
= STRBUF_INIT
;
886 struct strbuf branch_top
= STRBUF_INIT
, reflog_msg
= STRBUF_INIT
;
887 struct transport
*transport
= NULL
;
888 const char *src_ref_prefix
= "refs/heads/";
889 struct remote
*remote
;
890 int err
= 0, complete_refs_before_fetch
= 1;
891 int submodule_progress
;
893 struct refspec
*refspec
;
894 const char *fetch_pattern
;
896 packet_trace_identity("clone");
897 argc
= parse_options(argc
, argv
, prefix
, builtin_clone_options
,
898 builtin_clone_usage
, 0);
901 usage_msg_opt(_("Too many arguments."),
902 builtin_clone_usage
, builtin_clone_options
);
905 usage_msg_opt(_("You must specify a repository to clone."),
906 builtin_clone_usage
, builtin_clone_options
);
908 if (option_depth
|| option_since
|| option_not
.nr
)
910 if (option_single_branch
== -1)
911 option_single_branch
= deepen
? 1 : 0;
918 die(_("--bare and --origin %s options are incompatible."),
921 die(_("--bare and --separate-git-dir are incompatible."));
922 option_no_checkout
= 1;
926 option_origin
= "origin";
930 path
= get_repo_path(repo_name
, &is_bundle
);
932 repo
= absolute_pathdup(repo_name
);
933 else if (!strchr(repo_name
, ':'))
934 die(_("repository '%s' does not exist"), repo_name
);
938 /* no need to be strict, transport_set_option() will validate it again */
939 if (option_depth
&& atoi(option_depth
) < 1)
940 die(_("depth %s is not a positive number"), option_depth
);
943 dir
= xstrdup(argv
[1]);
945 dir
= guess_dir_name(repo_name
, is_bundle
, option_bare
);
946 strip_trailing_slashes(dir
);
948 dest_exists
= dir_exists(dir
);
949 if (dest_exists
&& !is_empty_dir(dir
))
950 die(_("destination path '%s' already exists and is not "
951 "an empty directory."), dir
);
953 strbuf_addf(&reflog_msg
, "clone: from %s", repo
);
958 work_tree
= getenv("GIT_WORK_TREE");
959 if (work_tree
&& dir_exists(work_tree
))
960 die(_("working tree '%s' already exists."), work_tree
);
963 if (option_bare
|| work_tree
)
964 git_dir
= xstrdup(dir
);
967 git_dir
= mkpathdup("%s/.git", dir
);
971 sigchain_push_common(remove_junk_on_signal
);
974 if (safe_create_leading_directories_const(work_tree
) < 0)
975 die_errno(_("could not create leading directories of '%s'"),
978 junk_work_tree_flags
|= REMOVE_DIR_KEEP_TOPLEVEL
;
979 else if (mkdir(work_tree
, 0777))
980 die_errno(_("could not create work tree dir '%s'"),
982 junk_work_tree
= work_tree
;
983 set_git_work_tree(work_tree
);
987 if (dir_exists(real_git_dir
))
988 junk_git_dir_flags
|= REMOVE_DIR_KEEP_TOPLEVEL
;
989 junk_git_dir
= real_git_dir
;
992 junk_git_dir_flags
|= REMOVE_DIR_KEEP_TOPLEVEL
;
993 junk_git_dir
= git_dir
;
995 if (safe_create_leading_directories_const(git_dir
) < 0)
996 die(_("could not create leading directories of '%s'"), git_dir
);
998 if (0 <= option_verbosity
) {
1000 fprintf(stderr
, _("Cloning into bare repository '%s'...\n"), dir
);
1002 fprintf(stderr
, _("Cloning into '%s'...\n"), dir
);
1005 if (option_recurse_submodules
.nr
> 0) {
1006 struct string_list_item
*item
;
1007 struct strbuf sb
= STRBUF_INIT
;
1009 /* remove duplicates */
1010 string_list_sort(&option_recurse_submodules
);
1011 string_list_remove_duplicates(&option_recurse_submodules
, 0);
1014 * NEEDSWORK: In a multi-working-tree world, this needs to be
1015 * set in the per-worktree config.
1017 for_each_string_list_item(item
, &option_recurse_submodules
) {
1018 strbuf_addf(&sb
, "submodule.active=%s",
1020 string_list_append(&option_config
,
1021 strbuf_detach(&sb
, NULL
));
1024 if (option_required_reference
.nr
&&
1025 option_optional_reference
.nr
)
1026 die(_("clone --recursive is not compatible with "
1027 "both --reference and --reference-if-able"));
1028 else if (option_required_reference
.nr
) {
1029 string_list_append(&option_config
,
1030 "submodule.alternateLocation=superproject");
1031 string_list_append(&option_config
,
1032 "submodule.alternateErrorStrategy=die");
1033 } else if (option_optional_reference
.nr
) {
1034 string_list_append(&option_config
,
1035 "submodule.alternateLocation=superproject");
1036 string_list_append(&option_config
,
1037 "submodule.alternateErrorStrategy=info");
1041 init_db(git_dir
, real_git_dir
, option_template
, INIT_DB_QUIET
);
1044 git_dir
= real_git_dir
;
1046 write_config(&option_config
);
1048 git_config(git_default_config
, NULL
);
1052 src_ref_prefix
= "refs/";
1053 strbuf_addstr(&branch_top
, src_ref_prefix
);
1055 git_config_set("core.bare", "true");
1057 strbuf_addf(&branch_top
, "refs/remotes/%s/", option_origin
);
1060 strbuf_addf(&value
, "+%s*:%s*", src_ref_prefix
, branch_top
.buf
);
1061 strbuf_addf(&key
, "remote.%s.url", option_origin
);
1062 git_config_set(key
.buf
, repo
);
1065 if (option_no_tags
) {
1066 strbuf_addf(&key
, "remote.%s.tagOpt", option_origin
);
1067 git_config_set(key
.buf
, "--no-tags");
1071 if (option_required_reference
.nr
|| option_optional_reference
.nr
)
1074 fetch_pattern
= value
.buf
;
1075 refspec
= parse_fetch_refspec(1, &fetch_pattern
);
1077 strbuf_reset(&value
);
1079 remote
= remote_get(option_origin
);
1080 transport
= transport_get(remote
, remote
->url
[0]);
1081 transport_set_verbosity(transport
, option_verbosity
, option_progress
);
1082 transport
->family
= family
;
1084 path
= get_repo_path(remote
->url
[0], &is_bundle
);
1085 is_local
= option_local
!= 0 && path
&& !is_bundle
;
1088 warning(_("--depth is ignored in local clones; use file:// instead."));
1090 warning(_("--shallow-since is ignored in local clones; use file:// instead."));
1092 warning(_("--shallow-exclude is ignored in local clones; use file:// instead."));
1093 if (!access(mkpath("%s/shallow", path
), F_OK
)) {
1094 if (option_local
> 0)
1095 warning(_("source repository is shallow, ignoring --local"));
1099 if (option_local
> 0 && !is_local
)
1100 warning(_("--local is ignored"));
1101 transport
->cloning
= 1;
1103 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
1106 transport_set_option(transport
, TRANS_OPT_DEPTH
,
1109 transport_set_option(transport
, TRANS_OPT_DEEPEN_SINCE
,
1112 transport_set_option(transport
, TRANS_OPT_DEEPEN_NOT
,
1113 (const char *)&option_not
);
1114 if (option_single_branch
)
1115 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
1117 if (option_upload_pack
)
1118 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
,
1119 option_upload_pack
);
1121 if (transport
->smart_options
&& !deepen
)
1122 transport
->smart_options
->check_self_contained_and_connected
= 1;
1124 refs
= transport_get_remote_refs(transport
);
1127 mapped_refs
= wanted_peer_refs(refs
, refspec
);
1129 * transport_get_remote_refs() may return refs with null sha-1
1130 * in mapped_refs (see struct transport->get_refs_list
1131 * comment). In that case we need fetch it early because
1132 * remote_head code below relies on it.
1134 * for normal clones, transport_get_remote_refs() should
1135 * return reliable ref set, we can delay cloning until after
1136 * remote HEAD check.
1138 for (ref
= refs
; ref
; ref
= ref
->next
)
1139 if (is_null_oid(&ref
->old_oid
)) {
1140 complete_refs_before_fetch
= 0;
1144 if (!is_local
&& !complete_refs_before_fetch
)
1145 transport_fetch_refs(transport
, mapped_refs
);
1147 remote_head
= find_ref_by_name(refs
, "HEAD");
1148 remote_head_points_at
=
1149 guess_remote_head(remote_head
, mapped_refs
, 0);
1151 if (option_branch
) {
1152 our_head_points_at
=
1153 find_remote_branch(mapped_refs
, option_branch
);
1155 if (!our_head_points_at
)
1156 die(_("Remote branch %s not found in upstream %s"),
1157 option_branch
, option_origin
);
1160 our_head_points_at
= remote_head_points_at
;
1164 die(_("Remote branch %s not found in upstream %s"),
1165 option_branch
, option_origin
);
1167 warning(_("You appear to have cloned an empty repository."));
1169 our_head_points_at
= NULL
;
1170 remote_head_points_at
= NULL
;
1172 option_no_checkout
= 1;
1174 install_branch_config(0, "master", option_origin
,
1175 "refs/heads/master");
1178 write_refspec_config(src_ref_prefix
, our_head_points_at
,
1179 remote_head_points_at
, &branch_top
);
1182 clone_local(path
, git_dir
);
1183 else if (refs
&& complete_refs_before_fetch
)
1184 transport_fetch_refs(transport
, mapped_refs
);
1186 update_remote_refs(refs
, mapped_refs
, remote_head_points_at
,
1187 branch_top
.buf
, reflog_msg
.buf
, transport
, !is_local
);
1189 update_head(our_head_points_at
, remote_head
, reflog_msg
.buf
);
1192 * We want to show progress for recursive submodule clones iff
1193 * we did so for the main clone. But only the transport knows
1194 * the final decision for this flag, so we need to rescue the value
1195 * before we free the transport.
1197 submodule_progress
= transport
->progress
;
1199 transport_unlock_pack(transport
);
1200 transport_disconnect(transport
);
1202 if (option_dissociate
) {
1204 dissociate_from_references();
1207 junk_mode
= JUNK_LEAVE_REPO
;
1208 err
= checkout(submodule_progress
);
1210 strbuf_release(&reflog_msg
);
1211 strbuf_release(&branch_top
);
1212 strbuf_release(&key
);
1213 strbuf_release(&value
);
1214 junk_mode
= JUNK_LEAVE_ALL
;