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.
11 #define USE_THE_INDEX_VARIABLE
17 #include "environment.h"
21 #include "parse-options.h"
22 #include "fetch-pack.h"
25 #include "object-file.h"
26 #include "object-store.h"
28 #include "tree-walk.h"
29 #include "unpack-trees.h"
30 #include "transport.h"
33 #include "dir-iterator.h"
38 #include "run-command.h"
40 #include "connected.h"
42 #include "list-objects-filter-options.h"
45 #include "bundle-uri.h"
50 * - respect DB_ENVIRONMENT for .git/objects.
52 * Implementation notes:
53 * - dropping use-separate-remote and no-separate-remote compatibility
56 static const char * const builtin_clone_usage
[] = {
57 N_("git clone [<options>] [--] <repo> [<dir>]"),
61 static int option_no_checkout
, option_bare
, option_mirror
, option_single_branch
= -1;
62 static int option_local
= -1, option_no_hardlinks
, option_shared
;
63 static int option_no_tags
;
64 static int option_shallow_submodules
;
65 static int option_reject_shallow
= -1; /* unspecified */
66 static int config_reject_shallow
= -1; /* unspecified */
68 static char *option_template
, *option_depth
, *option_since
;
69 static char *option_origin
= NULL
;
70 static char *remote_name
= NULL
;
71 static char *option_branch
= NULL
;
72 static struct string_list option_not
= STRING_LIST_INIT_NODUP
;
73 static const char *real_git_dir
;
74 static char *option_upload_pack
= "git-upload-pack";
75 static int option_verbosity
;
76 static int option_progress
= -1;
77 static int option_sparse_checkout
;
78 static enum transport_family family
;
79 static struct string_list option_config
= STRING_LIST_INIT_NODUP
;
80 static struct string_list option_required_reference
= STRING_LIST_INIT_NODUP
;
81 static struct string_list option_optional_reference
= STRING_LIST_INIT_NODUP
;
82 static int option_dissociate
;
83 static int max_jobs
= -1;
84 static struct string_list option_recurse_submodules
= STRING_LIST_INIT_NODUP
;
85 static struct list_objects_filter_options filter_options
= LIST_OBJECTS_FILTER_INIT
;
86 static int option_filter_submodules
= -1; /* unspecified */
87 static int config_filter_submodules
= -1; /* unspecified */
88 static struct string_list server_options
= STRING_LIST_INIT_NODUP
;
89 static int option_remote_submodules
;
90 static const char *bundle_uri
;
92 static int recurse_submodules_cb(const struct option
*opt
,
93 const char *arg
, int unset
)
96 string_list_clear((struct string_list
*)opt
->value
, 0);
98 string_list_append((struct string_list
*)opt
->value
, arg
);
100 string_list_append((struct string_list
*)opt
->value
,
101 (const char *)opt
->defval
);
106 static struct option builtin_clone_options
[] = {
107 OPT__VERBOSITY(&option_verbosity
),
108 OPT_BOOL(0, "progress", &option_progress
,
109 N_("force progress reporting")),
110 OPT_BOOL(0, "reject-shallow", &option_reject_shallow
,
111 N_("don't clone shallow repository")),
112 OPT_BOOL('n', "no-checkout", &option_no_checkout
,
113 N_("don't create a checkout")),
114 OPT_BOOL(0, "bare", &option_bare
, N_("create a bare repository")),
115 OPT_HIDDEN_BOOL(0, "naked", &option_bare
,
116 N_("create a bare repository")),
117 OPT_BOOL(0, "mirror", &option_mirror
,
118 N_("create a mirror repository (implies bare)")),
119 OPT_BOOL('l', "local", &option_local
,
120 N_("to clone from a local repository")),
121 OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks
,
122 N_("don't use local hardlinks, always copy")),
123 OPT_BOOL('s', "shared", &option_shared
,
124 N_("setup as shared repository")),
125 { OPTION_CALLBACK
, 0, "recurse-submodules", &option_recurse_submodules
,
126 N_("pathspec"), N_("initialize submodules in the clone"),
127 PARSE_OPT_OPTARG
, recurse_submodules_cb
, (intptr_t)"." },
128 OPT_ALIAS(0, "recursive", "recurse-submodules"),
129 OPT_INTEGER('j', "jobs", &max_jobs
,
130 N_("number of submodules cloned in parallel")),
131 OPT_STRING(0, "template", &option_template
, N_("template-directory"),
132 N_("directory from which templates will be used")),
133 OPT_STRING_LIST(0, "reference", &option_required_reference
, N_("repo"),
134 N_("reference repository")),
135 OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference
,
136 N_("repo"), N_("reference repository")),
137 OPT_BOOL(0, "dissociate", &option_dissociate
,
138 N_("use --reference only while cloning")),
139 OPT_STRING('o', "origin", &option_origin
, N_("name"),
140 N_("use <name> instead of 'origin' to track upstream")),
141 OPT_STRING('b', "branch", &option_branch
, N_("branch"),
142 N_("checkout <branch> instead of the remote's HEAD")),
143 OPT_STRING('u', "upload-pack", &option_upload_pack
, N_("path"),
144 N_("path to git-upload-pack on the remote")),
145 OPT_STRING(0, "depth", &option_depth
, N_("depth"),
146 N_("create a shallow clone of that depth")),
147 OPT_STRING(0, "shallow-since", &option_since
, N_("time"),
148 N_("create a shallow clone since a specific time")),
149 OPT_STRING_LIST(0, "shallow-exclude", &option_not
, N_("revision"),
150 N_("deepen history of shallow clone, excluding rev")),
151 OPT_BOOL(0, "single-branch", &option_single_branch
,
152 N_("clone only one branch, HEAD or --branch")),
153 OPT_BOOL(0, "no-tags", &option_no_tags
,
154 N_("don't clone any tags, and make later fetches not to follow them")),
155 OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules
,
156 N_("any cloned submodules will be shallow")),
157 OPT_STRING(0, "separate-git-dir", &real_git_dir
, N_("gitdir"),
158 N_("separate git dir from working tree")),
159 OPT_STRING_LIST('c', "config", &option_config
, N_("key=value"),
160 N_("set config inside the new repository")),
161 OPT_STRING_LIST(0, "server-option", &server_options
,
162 N_("server-specific"), N_("option to transmit")),
163 OPT_SET_INT('4', "ipv4", &family
, N_("use IPv4 addresses only"),
164 TRANSPORT_FAMILY_IPV4
),
165 OPT_SET_INT('6', "ipv6", &family
, N_("use IPv6 addresses only"),
166 TRANSPORT_FAMILY_IPV6
),
167 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
168 OPT_BOOL(0, "also-filter-submodules", &option_filter_submodules
,
169 N_("apply partial clone filters to submodules")),
170 OPT_BOOL(0, "remote-submodules", &option_remote_submodules
,
171 N_("any cloned submodules will use their remote-tracking branch")),
172 OPT_BOOL(0, "sparse", &option_sparse_checkout
,
173 N_("initialize sparse-checkout file to include only files at root")),
174 OPT_STRING(0, "bundle-uri", &bundle_uri
,
175 N_("uri"), N_("a URI for downloading bundles before fetching from origin remote")),
179 static const char *get_repo_path_1(struct strbuf
*path
, int *is_bundle
)
181 static char *suffix
[] = { "/.git", "", ".git/.git", ".git" };
182 static char *bundle_suffix
[] = { ".bundle", "" };
183 size_t baselen
= path
->len
;
187 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
188 strbuf_setlen(path
, baselen
);
189 strbuf_addstr(path
, suffix
[i
]);
190 if (stat(path
->buf
, &st
))
192 if (S_ISDIR(st
.st_mode
) && is_git_directory(path
->buf
)) {
195 } else if (S_ISREG(st
.st_mode
) && st
.st_size
> 8) {
196 /* Is it a "gitfile"? */
199 int len
, fd
= open(path
->buf
, O_RDONLY
);
202 len
= read_in_full(fd
, signature
, 8);
204 if (len
!= 8 || strncmp(signature
, "gitdir: ", 8))
206 dst
= read_gitfile(path
->buf
);
214 for (i
= 0; i
< ARRAY_SIZE(bundle_suffix
); i
++) {
215 strbuf_setlen(path
, baselen
);
216 strbuf_addstr(path
, bundle_suffix
[i
]);
217 if (!stat(path
->buf
, &st
) && S_ISREG(st
.st_mode
)) {
226 static char *get_repo_path(const char *repo
, int *is_bundle
)
228 struct strbuf path
= STRBUF_INIT
;
232 strbuf_addstr(&path
, repo
);
233 raw
= get_repo_path_1(&path
, is_bundle
);
234 canon
= raw
? absolute_pathdup(raw
) : NULL
;
235 strbuf_release(&path
);
239 static int add_one_reference(struct string_list_item
*item
, void *cb_data
)
241 struct strbuf err
= STRBUF_INIT
;
242 int *required
= cb_data
;
243 char *ref_git
= compute_alternate_path(item
->string
, &err
);
250 _("info: Could not add alternate for '%s': %s\n"),
251 item
->string
, err
.buf
);
253 struct strbuf sb
= STRBUF_INIT
;
254 strbuf_addf(&sb
, "%s/objects", ref_git
);
255 add_to_alternates_file(sb
.buf
);
259 strbuf_release(&err
);
264 static void setup_reference(void)
267 for_each_string_list(&option_required_reference
,
268 add_one_reference
, &required
);
270 for_each_string_list(&option_optional_reference
,
271 add_one_reference
, &required
);
274 static void copy_alternates(struct strbuf
*src
, const char *src_repo
)
277 * Read from the source objects/info/alternates file
278 * and copy the entries to corresponding file in the
279 * destination repository with add_to_alternates_file().
280 * Both src and dst have "$path/objects/info/alternates".
282 * Instead of copying bit-for-bit from the original,
283 * we need to append to existing one so that the already
284 * created entry via "clone -s" is not lost, and also
285 * to turn entries with paths relative to the original
286 * absolute, so that they can be used in the new repository.
288 FILE *in
= xfopen(src
->buf
, "r");
289 struct strbuf line
= STRBUF_INIT
;
291 while (strbuf_getline(&line
, in
) != EOF
) {
293 if (!line
.len
|| line
.buf
[0] == '#')
295 if (is_absolute_path(line
.buf
)) {
296 add_to_alternates_file(line
.buf
);
299 abs_path
= mkpathdup("%s/objects/%s", src_repo
, line
.buf
);
300 if (!normalize_path_copy(abs_path
, abs_path
))
301 add_to_alternates_file(abs_path
);
303 warning("skipping invalid relative alternate: %s/%s",
307 strbuf_release(&line
);
311 static void mkdir_if_missing(const char *pathname
, mode_t mode
)
315 if (!mkdir(pathname
, mode
))
319 die_errno(_("failed to create directory '%s'"), pathname
);
320 else if (stat(pathname
, &st
))
321 die_errno(_("failed to stat '%s'"), pathname
);
322 else if (!S_ISDIR(st
.st_mode
))
323 die(_("%s exists and is not a directory"), pathname
);
326 static void copy_or_link_directory(struct strbuf
*src
, struct strbuf
*dest
,
327 const char *src_repo
)
329 int src_len
, dest_len
;
330 struct dir_iterator
*iter
;
332 struct strbuf realpath
= STRBUF_INIT
;
334 mkdir_if_missing(dest
->buf
, 0777);
336 iter
= dir_iterator_begin(src
->buf
, DIR_ITERATOR_PEDANTIC
);
339 die_errno(_("failed to start iterator over '%s'"), src
->buf
);
341 strbuf_addch(src
, '/');
343 strbuf_addch(dest
, '/');
344 dest_len
= dest
->len
;
346 while ((iter_status
= dir_iterator_advance(iter
)) == ITER_OK
) {
347 strbuf_setlen(src
, src_len
);
348 strbuf_addstr(src
, iter
->relative_path
);
349 strbuf_setlen(dest
, dest_len
);
350 strbuf_addstr(dest
, iter
->relative_path
);
352 if (S_ISLNK(iter
->st
.st_mode
))
353 die(_("symlink '%s' exists, refusing to clone with --local"),
354 iter
->relative_path
);
356 if (S_ISDIR(iter
->st
.st_mode
)) {
357 mkdir_if_missing(dest
->buf
, 0777);
361 /* Files that cannot be copied bit-for-bit... */
362 if (!fspathcmp(iter
->relative_path
, "info/alternates")) {
363 copy_alternates(src
, src_repo
);
367 if (unlink(dest
->buf
) && errno
!= ENOENT
)
368 die_errno(_("failed to unlink '%s'"), dest
->buf
);
369 if (!option_no_hardlinks
) {
370 strbuf_realpath(&realpath
, src
->buf
, 1);
371 if (!link(realpath
.buf
, dest
->buf
))
373 if (option_local
> 0)
374 die_errno(_("failed to create link '%s'"), dest
->buf
);
375 option_no_hardlinks
= 1;
377 if (copy_file_with_time(dest
->buf
, src
->buf
, 0666))
378 die_errno(_("failed to copy file to '%s'"), dest
->buf
);
381 if (iter_status
!= ITER_DONE
) {
382 strbuf_setlen(src
, src_len
);
383 die(_("failed to iterate over '%s'"), src
->buf
);
386 strbuf_release(&realpath
);
389 static void clone_local(const char *src_repo
, const char *dest_repo
)
392 struct strbuf alt
= STRBUF_INIT
;
393 get_common_dir(&alt
, src_repo
);
394 strbuf_addstr(&alt
, "/objects");
395 add_to_alternates_file(alt
.buf
);
396 strbuf_release(&alt
);
398 struct strbuf src
= STRBUF_INIT
;
399 struct strbuf dest
= STRBUF_INIT
;
400 get_common_dir(&src
, src_repo
);
401 get_common_dir(&dest
, dest_repo
);
402 strbuf_addstr(&src
, "/objects");
403 strbuf_addstr(&dest
, "/objects");
404 copy_or_link_directory(&src
, &dest
, src_repo
);
405 strbuf_release(&src
);
406 strbuf_release(&dest
);
409 if (0 <= option_verbosity
)
410 fprintf(stderr
, _("done.\n"));
413 static const char *junk_work_tree
;
414 static int junk_work_tree_flags
;
415 static const char *junk_git_dir
;
416 static int junk_git_dir_flags
;
421 } junk_mode
= JUNK_LEAVE_NONE
;
423 static const char junk_leave_repo_msg
[] =
424 N_("Clone succeeded, but checkout failed.\n"
425 "You can inspect what was checked out with 'git status'\n"
426 "and retry with 'git restore --source=HEAD :/'\n");
428 static void remove_junk(void)
430 struct strbuf sb
= STRBUF_INIT
;
433 case JUNK_LEAVE_REPO
:
434 warning("%s", _(junk_leave_repo_msg
));
439 /* proceed to removal */
444 strbuf_addstr(&sb
, junk_git_dir
);
445 remove_dir_recursively(&sb
, junk_git_dir_flags
);
448 if (junk_work_tree
) {
449 strbuf_addstr(&sb
, junk_work_tree
);
450 remove_dir_recursively(&sb
, junk_work_tree_flags
);
455 static void remove_junk_on_signal(int signo
)
462 static struct ref
*find_remote_branch(const struct ref
*refs
, const char *branch
)
465 struct strbuf head
= STRBUF_INIT
;
466 strbuf_addstr(&head
, "refs/heads/");
467 strbuf_addstr(&head
, branch
);
468 ref
= find_ref_by_name(refs
, head
.buf
);
469 strbuf_release(&head
);
474 strbuf_addstr(&head
, "refs/tags/");
475 strbuf_addstr(&head
, branch
);
476 ref
= find_ref_by_name(refs
, head
.buf
);
477 strbuf_release(&head
);
482 static struct ref
*wanted_peer_refs(const struct ref
*refs
,
483 struct refspec
*refspec
)
485 struct ref
*head
= copy_ref(find_ref_by_name(refs
, "HEAD"));
486 struct ref
*local_refs
= head
;
487 struct ref
**tail
= head
? &head
->next
: &local_refs
;
489 if (option_single_branch
) {
490 struct ref
*remote_head
= NULL
;
493 remote_head
= guess_remote_head(head
, refs
, 0);
497 remote_head
= copy_ref(find_remote_branch(refs
, option_branch
));
500 if (!remote_head
&& option_branch
)
501 warning(_("Could not find remote branch %s to clone."),
505 for (i
= 0; i
< refspec
->nr
; i
++)
506 get_fetch_map(remote_head
, &refspec
->items
[i
],
509 /* if --branch=tag, pull the requested tag explicitly */
510 get_fetch_map(remote_head
, tag_refspec
, &tail
, 0);
512 free_refs(remote_head
);
515 for (i
= 0; i
< refspec
->nr
; i
++)
516 get_fetch_map(refs
, &refspec
->items
[i
], &tail
, 0);
519 if (!option_mirror
&& !option_single_branch
&& !option_no_tags
)
520 get_fetch_map(refs
, tag_refspec
, &tail
, 0);
525 static void write_remote_refs(const struct ref
*local_refs
)
529 struct ref_transaction
*t
;
530 struct strbuf err
= STRBUF_INIT
;
532 t
= ref_transaction_begin(&err
);
536 for (r
= local_refs
; r
; r
= r
->next
) {
539 if (ref_transaction_create(t
, r
->peer_ref
->name
, &r
->old_oid
,
544 if (initial_ref_transaction_commit(t
, &err
))
547 strbuf_release(&err
);
548 ref_transaction_free(t
);
551 static void write_followtags(const struct ref
*refs
, const char *msg
)
553 const struct ref
*ref
;
554 for (ref
= refs
; ref
; ref
= ref
->next
) {
555 if (!starts_with(ref
->name
, "refs/tags/"))
557 if (ends_with(ref
->name
, "^{}"))
559 if (!repo_has_object_file_with_flags(the_repository
, &ref
->old_oid
,
561 OBJECT_INFO_SKIP_FETCH_OBJECT
))
563 update_ref(msg
, ref
->name
, &ref
->old_oid
, NULL
, 0,
564 UPDATE_REFS_DIE_ON_ERR
);
568 static const struct object_id
*iterate_ref_map(void *cb_data
)
570 struct ref
**rm
= cb_data
;
571 struct ref
*ref
= *rm
;
574 * Skip anything missing a peer_ref, which we are not
575 * actually going to write a ref for.
577 while (ref
&& !ref
->peer_ref
)
583 return &ref
->old_oid
;
586 static void update_remote_refs(const struct ref
*refs
,
587 const struct ref
*mapped_refs
,
588 const struct ref
*remote_head_points_at
,
589 const char *branch_top
,
591 struct transport
*transport
,
592 int check_connectivity
)
594 const struct ref
*rm
= mapped_refs
;
596 if (check_connectivity
) {
597 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
599 opt
.transport
= transport
;
600 opt
.progress
= transport
->progress
;
602 if (check_connected(iterate_ref_map
, &rm
, &opt
))
603 die(_("remote did not send all necessary objects"));
607 write_remote_refs(mapped_refs
);
608 if (option_single_branch
&& !option_no_tags
)
609 write_followtags(refs
, msg
);
612 if (remote_head_points_at
&& !option_bare
) {
613 struct strbuf head_ref
= STRBUF_INIT
;
614 strbuf_addstr(&head_ref
, branch_top
);
615 strbuf_addstr(&head_ref
, "HEAD");
616 if (create_symref(head_ref
.buf
,
617 remote_head_points_at
->peer_ref
->name
,
619 die(_("unable to update %s"), head_ref
.buf
);
620 strbuf_release(&head_ref
);
624 static void update_head(const struct ref
*our
, const struct ref
*remote
,
625 const char *unborn
, const char *msg
)
628 if (our
&& skip_prefix(our
->name
, "refs/heads/", &head
)) {
629 /* Local default branch link */
630 if (create_symref("HEAD", our
->name
, NULL
) < 0)
631 die(_("unable to update HEAD"));
633 update_ref(msg
, "HEAD", &our
->old_oid
, NULL
, 0,
634 UPDATE_REFS_DIE_ON_ERR
);
635 install_branch_config(0, head
, remote_name
, our
->name
);
638 struct commit
*c
= lookup_commit_reference(the_repository
,
640 /* --branch specifies a non-branch (i.e. tags), detach HEAD */
641 update_ref(msg
, "HEAD", &c
->object
.oid
, NULL
, REF_NO_DEREF
,
642 UPDATE_REFS_DIE_ON_ERR
);
645 * We know remote HEAD points to a non-branch, or
646 * HEAD points to a branch but we don't know which one.
647 * Detach HEAD in all these cases.
649 update_ref(msg
, "HEAD", &remote
->old_oid
, NULL
, REF_NO_DEREF
,
650 UPDATE_REFS_DIE_ON_ERR
);
651 } else if (unborn
&& skip_prefix(unborn
, "refs/heads/", &head
)) {
653 * Unborn head from remote; same as "our" case above except
654 * that we have no ref to update.
656 if (create_symref("HEAD", unborn
, NULL
) < 0)
657 die(_("unable to update HEAD"));
659 install_branch_config(0, head
, remote_name
, unborn
);
663 static int git_sparse_checkout_init(const char *repo
)
665 struct child_process cmd
= CHILD_PROCESS_INIT
;
667 strvec_pushl(&cmd
.args
, "-C", repo
, "sparse-checkout", "set", NULL
);
670 * We must apply the setting in the current process
671 * for the later checkout to use the sparse-checkout file.
673 core_apply_sparse_checkout
= 1;
676 if (run_command(&cmd
)) {
677 error(_("failed to initialize sparse-checkout"));
684 static int checkout(int submodule_progress
, int filter_submodules
)
686 struct object_id oid
;
688 struct lock_file lock_file
= LOCK_INIT
;
689 struct unpack_trees_options opts
;
694 if (option_no_checkout
)
697 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, &oid
, NULL
);
699 warning(_("remote HEAD refers to nonexistent ref, "
700 "unable to checkout"));
703 if (!strcmp(head
, "HEAD")) {
704 if (advice_enabled(ADVICE_DETACHED_HEAD
))
705 detach_advice(oid_to_hex(&oid
));
708 if (!starts_with(head
, "refs/heads/"))
709 die(_("HEAD not found below refs/heads!"));
712 /* We need to be in the new work tree for the checkout */
715 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
717 memset(&opts
, 0, sizeof opts
);
721 opts
.preserve_ignored
= 0;
722 opts
.fn
= oneway_merge
;
723 opts
.verbose_update
= (option_verbosity
>= 0);
724 opts
.src_index
= &the_index
;
725 opts
.dst_index
= &the_index
;
726 init_checkout_metadata(&opts
.meta
, head
, &oid
, NULL
);
728 tree
= parse_tree_indirect(&oid
);
730 die(_("unable to parse commit %s"), oid_to_hex(&oid
));
732 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
733 if (unpack_trees(1, &t
, &opts
) < 0)
734 die(_("unable to checkout working tree"));
738 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
739 die(_("unable to write new index file"));
741 err
|= run_hooks_l("post-checkout", oid_to_hex(null_oid()),
742 oid_to_hex(&oid
), "1", NULL
);
744 if (!err
&& (option_recurse_submodules
.nr
> 0)) {
745 struct child_process cmd
= CHILD_PROCESS_INIT
;
746 strvec_pushl(&cmd
.args
, "submodule", "update", "--require-init",
747 "--recursive", NULL
);
749 if (option_shallow_submodules
== 1)
750 strvec_push(&cmd
.args
, "--depth=1");
753 strvec_pushf(&cmd
.args
, "--jobs=%d", max_jobs
);
755 if (submodule_progress
)
756 strvec_push(&cmd
.args
, "--progress");
758 if (option_verbosity
< 0)
759 strvec_push(&cmd
.args
, "--quiet");
761 if (option_remote_submodules
) {
762 strvec_push(&cmd
.args
, "--remote");
763 strvec_push(&cmd
.args
, "--no-fetch");
766 if (filter_submodules
&& filter_options
.choice
)
767 strvec_pushf(&cmd
.args
, "--filter=%s",
768 expand_list_objects_filter_spec(&filter_options
));
770 if (option_single_branch
>= 0)
771 strvec_push(&cmd
.args
, option_single_branch
?
773 "--no-single-branch");
776 err
= run_command(&cmd
);
782 static int git_clone_config(const char *k
, const char *v
, void *cb
)
784 if (!strcmp(k
, "clone.defaultremotename")) {
786 remote_name
= xstrdup(v
);
788 if (!strcmp(k
, "clone.rejectshallow"))
789 config_reject_shallow
= git_config_bool(k
, v
);
790 if (!strcmp(k
, "clone.filtersubmodules"))
791 config_filter_submodules
= git_config_bool(k
, v
);
793 return git_default_config(k
, v
, cb
);
796 static int write_one_config(const char *key
, const char *value
, void *data
)
799 * give git_clone_config a chance to write config values back to the
800 * environment, since git_config_set_multivar_gently only deals with
803 int apply_failed
= git_clone_config(key
, value
, data
);
807 return git_config_set_multivar_gently(key
,
808 value
? value
: "true",
809 CONFIG_REGEX_NONE
, 0);
812 static void write_config(struct string_list
*config
)
816 for (i
= 0; i
< config
->nr
; i
++) {
817 if (git_config_parse_parameter(config
->items
[i
].string
,
818 write_one_config
, NULL
) < 0)
819 die(_("unable to write parameters to config file"));
823 static void write_refspec_config(const char *src_ref_prefix
,
824 const struct ref
*our_head_points_at
,
825 const struct ref
*remote_head_points_at
,
826 struct strbuf
*branch_top
)
828 struct strbuf key
= STRBUF_INIT
;
829 struct strbuf value
= STRBUF_INIT
;
831 if (option_mirror
|| !option_bare
) {
832 if (option_single_branch
&& !option_mirror
) {
834 if (starts_with(our_head_points_at
->name
, "refs/tags/"))
835 strbuf_addf(&value
, "+%s:%s", our_head_points_at
->name
,
836 our_head_points_at
->name
);
838 strbuf_addf(&value
, "+%s:%s%s", our_head_points_at
->name
,
839 branch_top
->buf
, option_branch
);
840 } else if (remote_head_points_at
) {
841 const char *head
= remote_head_points_at
->name
;
842 if (!skip_prefix(head
, "refs/heads/", &head
))
843 BUG("remote HEAD points at non-head?");
845 strbuf_addf(&value
, "+%s:%s%s", remote_head_points_at
->name
,
846 branch_top
->buf
, head
);
849 * otherwise, the next "git fetch" will
850 * simply fetch from HEAD without updating
851 * any remote-tracking branch, which is what
855 strbuf_addf(&value
, "+%s*:%s*", src_ref_prefix
, branch_top
->buf
);
857 /* Configure the remote */
859 strbuf_addf(&key
, "remote.%s.fetch", remote_name
);
860 git_config_set_multivar(key
.buf
, value
.buf
, "^$", 0);
864 strbuf_addf(&key
, "remote.%s.mirror", remote_name
);
865 git_config_set(key
.buf
, "true");
871 strbuf_release(&key
);
872 strbuf_release(&value
);
875 static void dissociate_from_references(void)
877 char *alternates
= git_pathdup("objects/info/alternates");
879 if (!access(alternates
, F_OK
)) {
880 struct child_process cmd
= CHILD_PROCESS_INIT
;
884 strvec_pushl(&cmd
.args
, "repack", "-a", "-d", NULL
);
885 if (run_command(&cmd
))
886 die(_("cannot repack to clean up"));
887 if (unlink(alternates
) && errno
!= ENOENT
)
888 die_errno(_("cannot unlink temporary alternates file"));
893 static int path_exists(const char *path
)
896 return !stat(path
, &sb
);
899 int cmd_clone(int argc
, const char **argv
, const char *prefix
)
901 int is_bundle
= 0, is_local
;
902 int reject_shallow
= 0;
903 const char *repo_name
, *repo
, *work_tree
, *git_dir
;
904 char *repo_to_free
= NULL
;
905 char *path
= NULL
, *dir
, *display_repo
= NULL
;
906 int dest_exists
, real_dest_exists
= 0;
907 const struct ref
*refs
, *remote_head
;
908 struct ref
*remote_head_points_at
= NULL
;
909 const struct ref
*our_head_points_at
;
910 char *unborn_head
= NULL
;
911 struct ref
*mapped_refs
= NULL
;
912 const struct ref
*ref
;
913 struct strbuf key
= STRBUF_INIT
;
914 struct strbuf branch_top
= STRBUF_INIT
, reflog_msg
= STRBUF_INIT
;
915 struct transport
*transport
= NULL
;
916 const char *src_ref_prefix
= "refs/heads/";
917 struct remote
*remote
;
918 int err
= 0, complete_refs_before_fetch
= 1;
919 int submodule_progress
;
920 int filter_submodules
= 0;
922 struct transport_ls_refs_options transport_ls_refs_options
=
923 TRANSPORT_LS_REFS_OPTIONS_INIT
;
925 packet_trace_identity("clone");
927 git_config(git_clone_config
, NULL
);
929 argc
= parse_options(argc
, argv
, prefix
, builtin_clone_options
,
930 builtin_clone_usage
, 0);
933 usage_msg_opt(_("Too many arguments."),
934 builtin_clone_usage
, builtin_clone_options
);
937 usage_msg_opt(_("You must specify a repository to clone."),
938 builtin_clone_usage
, builtin_clone_options
);
940 if (option_depth
|| option_since
|| option_not
.nr
)
942 if (option_single_branch
== -1)
943 option_single_branch
= deepen
? 1 : 0;
950 die(_("options '%s' and '%s' cannot be used together"), "--bare", "--separate-git-dir");
951 option_no_checkout
= 1;
954 if (bundle_uri
&& deepen
)
955 die(_("--bundle-uri is incompatible with --depth, --shallow-since, and --shallow-exclude"));
959 path
= get_repo_path(repo_name
, &is_bundle
);
962 repo
= repo_to_free
= absolute_pathdup(repo_name
);
963 } else if (strchr(repo_name
, ':')) {
965 display_repo
= transport_anonymize_url(repo
);
967 die(_("repository '%s' does not exist"), repo_name
);
969 /* no need to be strict, transport_set_option() will validate it again */
970 if (option_depth
&& atoi(option_depth
) < 1)
971 die(_("depth %s is not a positive number"), option_depth
);
974 dir
= xstrdup(argv
[1]);
976 dir
= git_url_basename(repo_name
, is_bundle
, option_bare
);
977 strip_dir_trailing_slashes(dir
);
979 dest_exists
= path_exists(dir
);
980 if (dest_exists
&& !is_empty_dir(dir
))
981 die(_("destination path '%s' already exists and is not "
982 "an empty directory."), dir
);
985 real_dest_exists
= path_exists(real_git_dir
);
986 if (real_dest_exists
&& !is_empty_dir(real_git_dir
))
987 die(_("repository path '%s' already exists and is not "
988 "an empty directory."), real_git_dir
);
992 strbuf_addf(&reflog_msg
, "clone: from %s",
993 display_repo
? display_repo
: repo
);
999 work_tree
= getenv("GIT_WORK_TREE");
1000 if (work_tree
&& path_exists(work_tree
))
1001 die(_("working tree '%s' already exists."), work_tree
);
1004 if (option_bare
|| work_tree
)
1005 git_dir
= xstrdup(dir
);
1008 git_dir
= mkpathdup("%s/.git", dir
);
1011 atexit(remove_junk
);
1012 sigchain_push_common(remove_junk_on_signal
);
1015 if (safe_create_leading_directories_const(work_tree
) < 0)
1016 die_errno(_("could not create leading directories of '%s'"),
1019 junk_work_tree_flags
|= REMOVE_DIR_KEEP_TOPLEVEL
;
1020 else if (mkdir(work_tree
, 0777))
1021 die_errno(_("could not create work tree dir '%s'"),
1023 junk_work_tree
= work_tree
;
1024 set_git_work_tree(work_tree
);
1028 if (real_dest_exists
)
1029 junk_git_dir_flags
|= REMOVE_DIR_KEEP_TOPLEVEL
;
1030 junk_git_dir
= real_git_dir
;
1033 junk_git_dir_flags
|= REMOVE_DIR_KEEP_TOPLEVEL
;
1034 junk_git_dir
= git_dir
;
1036 if (safe_create_leading_directories_const(git_dir
) < 0)
1037 die(_("could not create leading directories of '%s'"), git_dir
);
1039 if (0 <= option_verbosity
) {
1041 fprintf(stderr
, _("Cloning into bare repository '%s'...\n"), dir
);
1043 fprintf(stderr
, _("Cloning into '%s'...\n"), dir
);
1046 if (option_recurse_submodules
.nr
> 0) {
1047 struct string_list_item
*item
;
1048 struct strbuf sb
= STRBUF_INIT
;
1051 /* remove duplicates */
1052 string_list_sort(&option_recurse_submodules
);
1053 string_list_remove_duplicates(&option_recurse_submodules
, 0);
1056 * NEEDSWORK: In a multi-working-tree world, this needs to be
1057 * set in the per-worktree config.
1059 for_each_string_list_item(item
, &option_recurse_submodules
) {
1060 strbuf_addf(&sb
, "submodule.active=%s",
1062 string_list_append(&option_config
,
1063 strbuf_detach(&sb
, NULL
));
1066 if (!git_config_get_bool("submodule.stickyRecursiveClone", &val
) &&
1068 string_list_append(&option_config
, "submodule.recurse=true");
1070 if (option_required_reference
.nr
&&
1071 option_optional_reference
.nr
)
1072 die(_("clone --recursive is not compatible with "
1073 "both --reference and --reference-if-able"));
1074 else if (option_required_reference
.nr
) {
1075 string_list_append(&option_config
,
1076 "submodule.alternateLocation=superproject");
1077 string_list_append(&option_config
,
1078 "submodule.alternateErrorStrategy=die");
1079 } else if (option_optional_reference
.nr
) {
1080 string_list_append(&option_config
,
1081 "submodule.alternateLocation=superproject");
1082 string_list_append(&option_config
,
1083 "submodule.alternateErrorStrategy=info");
1087 init_db(git_dir
, real_git_dir
, option_template
, GIT_HASH_UNKNOWN
, NULL
,
1091 free((char *)git_dir
);
1092 git_dir
= real_git_dir
;
1096 * additional config can be injected with -c, make sure it's included
1097 * after init_db, which clears the entire config environment.
1099 write_config(&option_config
);
1102 * re-read config after init_db and write_config to pick up any config
1103 * injected by --template and --config, respectively.
1105 git_config(git_clone_config
, NULL
);
1108 * If option_reject_shallow is specified from CLI option,
1109 * ignore config_reject_shallow from git_clone_config.
1111 if (config_reject_shallow
!= -1)
1112 reject_shallow
= config_reject_shallow
;
1113 if (option_reject_shallow
!= -1)
1114 reject_shallow
= option_reject_shallow
;
1117 * If option_filter_submodules is specified from CLI option,
1118 * ignore config_filter_submodules from git_clone_config.
1120 if (config_filter_submodules
!= -1)
1121 filter_submodules
= config_filter_submodules
;
1122 if (option_filter_submodules
!= -1)
1123 filter_submodules
= option_filter_submodules
;
1126 * Exit if the user seems to be doing something silly with submodule
1127 * filter flags (but not with filter configs, as those should be
1130 if (option_filter_submodules
> 0 && !filter_options
.choice
)
1131 die(_("the option '%s' requires '%s'"),
1132 "--also-filter-submodules", "--filter");
1133 if (option_filter_submodules
> 0 && !option_recurse_submodules
.nr
)
1134 die(_("the option '%s' requires '%s'"),
1135 "--also-filter-submodules", "--recurse-submodules");
1138 * apply the remote name provided by --origin only after this second
1139 * call to git_config, to ensure it overrides all config-based values.
1141 if (option_origin
) {
1143 remote_name
= xstrdup(option_origin
);
1147 remote_name
= xstrdup("origin");
1149 if (!valid_remote_name(remote_name
))
1150 die(_("'%s' is not a valid remote name"), remote_name
);
1154 src_ref_prefix
= "refs/";
1155 strbuf_addstr(&branch_top
, src_ref_prefix
);
1157 git_config_set("core.bare", "true");
1159 strbuf_addf(&branch_top
, "refs/remotes/%s/", remote_name
);
1162 strbuf_addf(&key
, "remote.%s.url", remote_name
);
1163 git_config_set(key
.buf
, repo
);
1166 if (option_no_tags
) {
1167 strbuf_addf(&key
, "remote.%s.tagOpt", remote_name
);
1168 git_config_set(key
.buf
, "--no-tags");
1172 if (option_required_reference
.nr
|| option_optional_reference
.nr
)
1175 if (option_sparse_checkout
&& git_sparse_checkout_init(dir
))
1178 remote
= remote_get(remote_name
);
1180 refspec_appendf(&remote
->fetch
, "+%s*:%s*", src_ref_prefix
,
1183 path
= get_repo_path(remote
->url
[0], &is_bundle
);
1184 is_local
= option_local
!= 0 && path
&& !is_bundle
;
1187 warning(_("--depth is ignored in local clones; use file:// instead."));
1189 warning(_("--shallow-since is ignored in local clones; use file:// instead."));
1191 warning(_("--shallow-exclude is ignored in local clones; use file:// instead."));
1192 if (filter_options
.choice
)
1193 warning(_("--filter is ignored in local clones; use file:// instead."));
1194 if (!access(mkpath("%s/shallow", path
), F_OK
)) {
1196 die(_("source repository is shallow, reject to clone."));
1197 if (option_local
> 0)
1198 warning(_("source repository is shallow, ignoring --local"));
1202 if (option_local
> 0 && !is_local
)
1203 warning(_("--local is ignored"));
1205 transport
= transport_get(remote
, path
? path
: remote
->url
[0]);
1206 transport_set_verbosity(transport
, option_verbosity
, option_progress
);
1207 transport
->family
= family
;
1208 transport
->cloning
= 1;
1211 struct bundle_header header
= BUNDLE_HEADER_INIT
;
1212 int fd
= read_bundle_header(path
, &header
);
1213 int has_filter
= header
.filter
.choice
!= LOFC_DISABLED
;
1217 bundle_header_release(&header
);
1219 die(_("cannot clone from filtered bundle"));
1222 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
1225 transport_set_option(transport
, TRANS_OPT_REJECT_SHALLOW
, "1");
1227 transport_set_option(transport
, TRANS_OPT_DEPTH
,
1230 transport_set_option(transport
, TRANS_OPT_DEEPEN_SINCE
,
1233 transport_set_option(transport
, TRANS_OPT_DEEPEN_NOT
,
1234 (const char *)&option_not
);
1235 if (option_single_branch
)
1236 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
1238 if (option_upload_pack
)
1239 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
,
1240 option_upload_pack
);
1242 if (server_options
.nr
)
1243 transport
->server_options
= &server_options
;
1245 if (filter_options
.choice
) {
1247 expand_list_objects_filter_spec(&filter_options
);
1248 transport_set_option(transport
, TRANS_OPT_LIST_OBJECTS_FILTER
,
1250 transport_set_option(transport
, TRANS_OPT_FROM_PROMISOR
, "1");
1253 if (transport
->smart_options
&& !deepen
&& !filter_options
.choice
)
1254 transport
->smart_options
->check_self_contained_and_connected
= 1;
1257 * Before fetching from the remote, download and install bundle
1258 * data from the --bundle-uri option.
1261 int has_heuristic
= 0;
1263 /* At this point, we need the_repository to match the cloned repo. */
1264 if (repo_init(the_repository
, git_dir
, work_tree
))
1265 warning(_("failed to initialize the repo, skipping bundle URI"));
1266 else if (fetch_bundle_uri(the_repository
, bundle_uri
, &has_heuristic
))
1267 warning(_("failed to fetch objects from bundle URI '%s'"),
1269 else if (has_heuristic
)
1270 git_config_set_gently("fetch.bundleuri", bundle_uri
);
1273 strvec_push(&transport_ls_refs_options
.ref_prefixes
, "HEAD");
1274 refspec_ref_prefixes(&remote
->fetch
,
1275 &transport_ls_refs_options
.ref_prefixes
);
1277 expand_ref_prefix(&transport_ls_refs_options
.ref_prefixes
,
1279 if (!option_no_tags
)
1280 strvec_push(&transport_ls_refs_options
.ref_prefixes
,
1283 refs
= transport_get_remote_refs(transport
, &transport_ls_refs_options
);
1286 mapped_refs
= wanted_peer_refs(refs
, &remote
->fetch
);
1290 * Populate transport->got_remote_bundle_uri and
1291 * transport->bundle_uri. We might get nothing.
1293 transport_get_remote_bundle_uri(transport
);
1295 if (transport
->bundles
&&
1296 hashmap_get_size(&transport
->bundles
->bundles
)) {
1297 /* At this point, we need the_repository to match the cloned repo. */
1298 if (repo_init(the_repository
, git_dir
, work_tree
))
1299 warning(_("failed to initialize the repo, skipping bundle URI"));
1300 else if (fetch_bundle_list(the_repository
,
1301 transport
->bundles
))
1302 warning(_("failed to fetch advertised bundles"));
1304 clear_bundle_list(transport
->bundles
);
1305 FREE_AND_NULL(transport
->bundles
);
1310 int hash_algo
= hash_algo_by_ptr(transport_get_hash_algo(transport
));
1313 * Now that we know what algorithm the remote side is using,
1314 * let's set ours to the same thing.
1316 initialize_repository_version(hash_algo
, 1);
1317 repo_set_hash_algo(the_repository
, hash_algo
);
1319 * transport_get_remote_refs() may return refs with null sha-1
1320 * in mapped_refs (see struct transport->get_refs_list
1321 * comment). In that case we need fetch it early because
1322 * remote_head code below relies on it.
1324 * for normal clones, transport_get_remote_refs() should
1325 * return reliable ref set, we can delay cloning until after
1326 * remote HEAD check.
1328 for (ref
= refs
; ref
; ref
= ref
->next
)
1329 if (is_null_oid(&ref
->old_oid
)) {
1330 complete_refs_before_fetch
= 0;
1334 if (!is_local
&& !complete_refs_before_fetch
) {
1335 if (transport_fetch_refs(transport
, mapped_refs
))
1336 die(_("remote transport reported error"));
1340 remote_head
= find_ref_by_name(refs
, "HEAD");
1341 remote_head_points_at
= guess_remote_head(remote_head
, mapped_refs
, 0);
1343 if (option_branch
) {
1344 our_head_points_at
= find_remote_branch(mapped_refs
, option_branch
);
1345 if (!our_head_points_at
)
1346 die(_("Remote branch %s not found in upstream %s"),
1347 option_branch
, remote_name
);
1348 } else if (remote_head_points_at
) {
1349 our_head_points_at
= remote_head_points_at
;
1350 } else if (remote_head
) {
1351 our_head_points_at
= NULL
;
1356 warning(_("You appear to have cloned an empty repository."));
1357 option_no_checkout
= 1;
1360 if (transport_ls_refs_options
.unborn_head_target
&&
1361 skip_prefix(transport_ls_refs_options
.unborn_head_target
,
1362 "refs/heads/", &branch
)) {
1363 unborn_head
= xstrdup(transport_ls_refs_options
.unborn_head_target
);
1365 branch
= git_default_branch_name(0);
1366 unborn_head
= xstrfmt("refs/heads/%s", branch
);
1370 * We may have selected a local default branch name "foo",
1371 * and even though the remote's HEAD does not point there,
1372 * it may still have a "foo" branch. If so, set it up so
1373 * that we can follow the usual checkout code later.
1375 * Note that for an empty repo we'll already have set
1376 * option_no_checkout above, which would work against us here.
1377 * But for an empty repo, find_remote_branch() can never find
1380 our_head_points_at
= find_remote_branch(mapped_refs
, branch
);
1383 write_refspec_config(src_ref_prefix
, our_head_points_at
,
1384 remote_head_points_at
, &branch_top
);
1386 if (filter_options
.choice
)
1387 partial_clone_register(remote_name
, &filter_options
);
1390 clone_local(path
, git_dir
);
1391 else if (mapped_refs
&& complete_refs_before_fetch
) {
1392 if (transport_fetch_refs(transport
, mapped_refs
))
1393 die(_("remote transport reported error"));
1396 update_remote_refs(refs
, mapped_refs
, remote_head_points_at
,
1397 branch_top
.buf
, reflog_msg
.buf
, transport
,
1400 update_head(our_head_points_at
, remote_head
, unborn_head
, reflog_msg
.buf
);
1403 * We want to show progress for recursive submodule clones iff
1404 * we did so for the main clone. But only the transport knows
1405 * the final decision for this flag, so we need to rescue the value
1406 * before we free the transport.
1408 submodule_progress
= transport
->progress
;
1410 transport_unlock_pack(transport
, 0);
1411 transport_disconnect(transport
);
1413 if (option_dissociate
) {
1414 close_object_store(the_repository
->objects
);
1415 dissociate_from_references();
1418 junk_mode
= JUNK_LEAVE_REPO
;
1419 err
= checkout(submodule_progress
, filter_submodules
);
1422 strbuf_release(&reflog_msg
);
1423 strbuf_release(&branch_top
);
1424 strbuf_release(&key
);
1425 free_refs(mapped_refs
);
1426 free_refs(remote_head_points_at
);
1431 junk_mode
= JUNK_LEAVE_ALL
;
1433 transport_ls_refs_options_release(&transport_ls_refs_options
);