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.
16 #include "environment.h"
20 #include "parse-options.h"
23 #include "object-file.h"
24 #include "object-store-ll.h"
26 #include "tree-walk.h"
27 #include "unpack-trees.h"
28 #include "transport.h"
31 #include "dir-iterator.h"
36 #include "run-command.h"
38 #include "connected.h"
42 #include "list-objects-filter-options.h"
45 #include "bundle-uri.h"
49 * - respect DB_ENVIRONMENT for .git/objects.
51 * Implementation notes:
52 * - dropping use-separate-remote and no-separate-remote compatibility
55 static const char * const builtin_clone_usage
[] = {
56 N_("git clone [<options>] [--] <repo> [<dir>]"),
60 static int option_no_checkout
, option_bare
, option_mirror
, option_single_branch
= -1;
61 static int option_local
= -1, option_no_hardlinks
, option_shared
;
62 static int option_no_tags
;
63 static int option_shallow_submodules
;
64 static int option_reject_shallow
= -1; /* unspecified */
65 static int config_reject_shallow
= -1; /* unspecified */
67 static char *option_template
, *option_depth
, *option_since
;
68 static char *option_origin
= NULL
;
69 static char *remote_name
= NULL
;
70 static char *option_branch
= NULL
;
71 static struct string_list option_not
= STRING_LIST_INIT_NODUP
;
72 static const char *real_git_dir
;
73 static const char *ref_format
;
74 static const 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(0, "ref-format", &ref_format
, N_("format"),
160 N_("specify the reference format to use")),
161 OPT_STRING_LIST('c', "config", &option_config
, N_("key=value"),
162 N_("set config inside the new repository")),
163 OPT_STRING_LIST(0, "server-option", &server_options
,
164 N_("server-specific"), N_("option to transmit")),
165 OPT_IPVERSION(&family
),
166 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
167 OPT_BOOL(0, "also-filter-submodules", &option_filter_submodules
,
168 N_("apply partial clone filters to submodules")),
169 OPT_BOOL(0, "remote-submodules", &option_remote_submodules
,
170 N_("any cloned submodules will use their remote-tracking branch")),
171 OPT_BOOL(0, "sparse", &option_sparse_checkout
,
172 N_("initialize sparse-checkout file to include only files at root")),
173 OPT_STRING(0, "bundle-uri", &bundle_uri
,
174 N_("uri"), N_("a URI for downloading bundles before fetching from origin remote")),
178 static const char *get_repo_path_1(struct strbuf
*path
, int *is_bundle
)
180 static const char *suffix
[] = { "/.git", "", ".git/.git", ".git" };
181 static const char *bundle_suffix
[] = { ".bundle", "" };
182 size_t baselen
= path
->len
;
186 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
187 strbuf_setlen(path
, baselen
);
188 strbuf_addstr(path
, suffix
[i
]);
189 if (stat(path
->buf
, &st
))
191 if (S_ISDIR(st
.st_mode
) && is_git_directory(path
->buf
)) {
194 } else if (S_ISREG(st
.st_mode
) && st
.st_size
> 8) {
195 /* Is it a "gitfile"? */
198 int len
, fd
= open(path
->buf
, O_RDONLY
);
201 len
= read_in_full(fd
, signature
, 8);
203 if (len
!= 8 || strncmp(signature
, "gitdir: ", 8))
205 dst
= read_gitfile(path
->buf
);
213 for (i
= 0; i
< ARRAY_SIZE(bundle_suffix
); i
++) {
214 strbuf_setlen(path
, baselen
);
215 strbuf_addstr(path
, bundle_suffix
[i
]);
216 if (!stat(path
->buf
, &st
) && S_ISREG(st
.st_mode
)) {
225 static char *get_repo_path(const char *repo
, int *is_bundle
)
227 struct strbuf path
= STRBUF_INIT
;
231 strbuf_addstr(&path
, repo
);
232 raw
= get_repo_path_1(&path
, is_bundle
);
233 canon
= raw
? absolute_pathdup(raw
) : NULL
;
234 strbuf_release(&path
);
238 static int add_one_reference(struct string_list_item
*item
, void *cb_data
)
240 struct strbuf err
= STRBUF_INIT
;
241 int *required
= cb_data
;
242 char *ref_git
= compute_alternate_path(item
->string
, &err
);
249 _("info: Could not add alternate for '%s': %s\n"),
250 item
->string
, err
.buf
);
252 struct strbuf sb
= STRBUF_INIT
;
253 strbuf_addf(&sb
, "%s/objects", ref_git
);
254 add_to_alternates_file(sb
.buf
);
258 strbuf_release(&err
);
263 static void setup_reference(void)
266 for_each_string_list(&option_required_reference
,
267 add_one_reference
, &required
);
269 for_each_string_list(&option_optional_reference
,
270 add_one_reference
, &required
);
273 static void copy_alternates(struct strbuf
*src
, const char *src_repo
)
276 * Read from the source objects/info/alternates file
277 * and copy the entries to corresponding file in the
278 * destination repository with add_to_alternates_file().
279 * Both src and dst have "$path/objects/info/alternates".
281 * Instead of copying bit-for-bit from the original,
282 * we need to append to existing one so that the already
283 * created entry via "clone -s" is not lost, and also
284 * to turn entries with paths relative to the original
285 * absolute, so that they can be used in the new repository.
287 FILE *in
= xfopen(src
->buf
, "r");
288 struct strbuf line
= STRBUF_INIT
;
290 while (strbuf_getline(&line
, in
) != EOF
) {
292 if (!line
.len
|| line
.buf
[0] == '#')
294 if (is_absolute_path(line
.buf
)) {
295 add_to_alternates_file(line
.buf
);
298 abs_path
= mkpathdup("%s/objects/%s", src_repo
, line
.buf
);
299 if (!normalize_path_copy(abs_path
, abs_path
))
300 add_to_alternates_file(abs_path
);
302 warning("skipping invalid relative alternate: %s/%s",
306 strbuf_release(&line
);
310 static void mkdir_if_missing(const char *pathname
, mode_t mode
)
314 if (!mkdir(pathname
, mode
))
318 die_errno(_("failed to create directory '%s'"), pathname
);
319 else if (stat(pathname
, &st
))
320 die_errno(_("failed to stat '%s'"), pathname
);
321 else if (!S_ISDIR(st
.st_mode
))
322 die(_("%s exists and is not a directory"), pathname
);
325 static void copy_or_link_directory(struct strbuf
*src
, struct strbuf
*dest
,
326 const char *src_repo
)
328 int src_len
, dest_len
;
329 struct dir_iterator
*iter
;
333 * Refuse copying directories by default which aren't owned by us. The
334 * code that performs either the copying or hardlinking is not prepared
335 * to handle various edge cases where an adversary may for example
336 * racily swap out files for symlinks. This can cause us to
337 * inadvertently use the wrong source file.
339 * Furthermore, even if we were prepared to handle such races safely,
340 * creating hardlinks across user boundaries is an inherently unsafe
341 * operation as the hardlinked files can be rewritten at will by the
342 * potentially-untrusted user. We thus refuse to do so by default.
344 die_upon_dubious_ownership(NULL
, NULL
, src_repo
);
346 mkdir_if_missing(dest
->buf
, 0777);
348 iter
= dir_iterator_begin(src
->buf
, DIR_ITERATOR_PEDANTIC
);
351 if (errno
== ENOTDIR
) {
352 int saved_errno
= errno
;
355 if (!lstat(src
->buf
, &st
) && S_ISLNK(st
.st_mode
))
356 die(_("'%s' is a symlink, refusing to clone with --local"),
360 die_errno(_("failed to start iterator over '%s'"), src
->buf
);
363 strbuf_addch(src
, '/');
365 strbuf_addch(dest
, '/');
366 dest_len
= dest
->len
;
368 while ((iter_status
= dir_iterator_advance(iter
)) == ITER_OK
) {
369 strbuf_setlen(src
, src_len
);
370 strbuf_addstr(src
, iter
->relative_path
);
371 strbuf_setlen(dest
, dest_len
);
372 strbuf_addstr(dest
, iter
->relative_path
);
374 if (S_ISLNK(iter
->st
.st_mode
))
375 die(_("symlink '%s' exists, refusing to clone with --local"),
376 iter
->relative_path
);
378 if (S_ISDIR(iter
->st
.st_mode
)) {
379 mkdir_if_missing(dest
->buf
, 0777);
383 /* Files that cannot be copied bit-for-bit... */
384 if (!fspathcmp(iter
->relative_path
, "info/alternates")) {
385 copy_alternates(src
, src_repo
);
389 if (unlink(dest
->buf
) && errno
!= ENOENT
)
390 die_errno(_("failed to unlink '%s'"), dest
->buf
);
391 if (!option_no_hardlinks
) {
392 if (!link(src
->buf
, dest
->buf
)) {
396 * Sanity-check whether the created hardlink
397 * actually links to the expected file now. This
398 * catches time-of-check-time-of-use bugs in
399 * case the source file was meanwhile swapped.
401 if (lstat(dest
->buf
, &st
))
402 die(_("hardlink cannot be checked at '%s'"), dest
->buf
);
403 if (st
.st_mode
!= iter
->st
.st_mode
||
404 st
.st_ino
!= iter
->st
.st_ino
||
405 st
.st_dev
!= iter
->st
.st_dev
||
406 st
.st_size
!= iter
->st
.st_size
||
407 st
.st_uid
!= iter
->st
.st_uid
||
408 st
.st_gid
!= iter
->st
.st_gid
)
409 die(_("hardlink different from source at '%s'"), dest
->buf
);
413 if (option_local
> 0)
414 die_errno(_("failed to create link '%s'"), dest
->buf
);
415 option_no_hardlinks
= 1;
417 if (copy_file_with_time(dest
->buf
, src
->buf
, 0666))
418 die_errno(_("failed to copy file to '%s'"), dest
->buf
);
421 if (iter_status
!= ITER_DONE
) {
422 strbuf_setlen(src
, src_len
);
423 die(_("failed to iterate over '%s'"), src
->buf
);
427 static void clone_local(const char *src_repo
, const char *dest_repo
)
430 struct strbuf alt
= STRBUF_INIT
;
431 get_common_dir(&alt
, src_repo
);
432 strbuf_addstr(&alt
, "/objects");
433 add_to_alternates_file(alt
.buf
);
434 strbuf_release(&alt
);
436 struct strbuf src
= STRBUF_INIT
;
437 struct strbuf dest
= STRBUF_INIT
;
438 get_common_dir(&src
, src_repo
);
439 get_common_dir(&dest
, dest_repo
);
440 strbuf_addstr(&src
, "/objects");
441 strbuf_addstr(&dest
, "/objects");
442 copy_or_link_directory(&src
, &dest
, src_repo
);
443 strbuf_release(&src
);
444 strbuf_release(&dest
);
447 if (0 <= option_verbosity
)
448 fprintf(stderr
, _("done.\n"));
451 static const char *junk_work_tree
;
452 static int junk_work_tree_flags
;
453 static const char *junk_git_dir
;
454 static int junk_git_dir_flags
;
459 } junk_mode
= JUNK_LEAVE_NONE
;
461 static const char junk_leave_repo_msg
[] =
462 N_("Clone succeeded, but checkout failed.\n"
463 "You can inspect what was checked out with 'git status'\n"
464 "and retry with 'git restore --source=HEAD :/'\n");
466 static void remove_junk(void)
468 struct strbuf sb
= STRBUF_INIT
;
471 case JUNK_LEAVE_REPO
:
472 warning("%s", _(junk_leave_repo_msg
));
477 /* proceed to removal */
482 strbuf_addstr(&sb
, junk_git_dir
);
483 remove_dir_recursively(&sb
, junk_git_dir_flags
);
486 if (junk_work_tree
) {
487 strbuf_addstr(&sb
, junk_work_tree
);
488 remove_dir_recursively(&sb
, junk_work_tree_flags
);
493 static void remove_junk_on_signal(int signo
)
500 static struct ref
*find_remote_branch(const struct ref
*refs
, const char *branch
)
503 struct strbuf head
= STRBUF_INIT
;
504 strbuf_addstr(&head
, "refs/heads/");
505 strbuf_addstr(&head
, branch
);
506 ref
= find_ref_by_name(refs
, head
.buf
);
507 strbuf_release(&head
);
512 strbuf_addstr(&head
, "refs/tags/");
513 strbuf_addstr(&head
, branch
);
514 ref
= find_ref_by_name(refs
, head
.buf
);
515 strbuf_release(&head
);
520 static struct ref
*wanted_peer_refs(const struct ref
*refs
,
521 struct refspec
*refspec
)
523 struct ref
*head
= copy_ref(find_ref_by_name(refs
, "HEAD"));
524 struct ref
*local_refs
= head
;
525 struct ref
**tail
= head
? &head
->next
: &local_refs
;
526 struct refspec_item tag_refspec
;
528 refspec_item_init(&tag_refspec
, TAG_REFSPEC
, 0);
530 if (option_single_branch
) {
531 struct ref
*remote_head
= NULL
;
534 remote_head
= guess_remote_head(head
, refs
, 0);
537 local_refs
= head
= NULL
;
539 remote_head
= copy_ref(find_remote_branch(refs
, option_branch
));
542 if (!remote_head
&& option_branch
)
543 warning(_("Could not find remote branch %s to clone."),
547 for (i
= 0; i
< refspec
->nr
; i
++)
548 get_fetch_map(remote_head
, &refspec
->items
[i
],
551 /* if --branch=tag, pull the requested tag explicitly */
552 get_fetch_map(remote_head
, &tag_refspec
, &tail
, 0);
554 free_refs(remote_head
);
557 for (i
= 0; i
< refspec
->nr
; i
++)
558 get_fetch_map(refs
, &refspec
->items
[i
], &tail
, 0);
561 if (!option_mirror
&& !option_single_branch
&& !option_no_tags
)
562 get_fetch_map(refs
, &tag_refspec
, &tail
, 0);
564 refspec_item_clear(&tag_refspec
);
568 static void write_remote_refs(const struct ref
*local_refs
)
572 struct ref_transaction
*t
;
573 struct strbuf err
= STRBUF_INIT
;
575 t
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
580 for (r
= local_refs
; r
; r
= r
->next
) {
583 if (ref_transaction_create(t
, r
->peer_ref
->name
, &r
->old_oid
,
584 NULL
, 0, NULL
, &err
))
588 if (initial_ref_transaction_commit(t
, &err
))
591 strbuf_release(&err
);
592 ref_transaction_free(t
);
595 static void write_followtags(const struct ref
*refs
, const char *msg
)
597 const struct ref
*ref
;
598 for (ref
= refs
; ref
; ref
= ref
->next
) {
599 if (!starts_with(ref
->name
, "refs/tags/"))
601 if (ends_with(ref
->name
, "^{}"))
603 if (!repo_has_object_file_with_flags(the_repository
, &ref
->old_oid
,
605 OBJECT_INFO_SKIP_FETCH_OBJECT
))
607 refs_update_ref(get_main_ref_store(the_repository
), msg
,
608 ref
->name
, &ref
->old_oid
, NULL
, 0,
609 UPDATE_REFS_DIE_ON_ERR
);
613 static const struct object_id
*iterate_ref_map(void *cb_data
)
615 struct ref
**rm
= cb_data
;
616 struct ref
*ref
= *rm
;
619 * Skip anything missing a peer_ref, which we are not
620 * actually going to write a ref for.
622 while (ref
&& !ref
->peer_ref
)
628 return &ref
->old_oid
;
631 static void update_remote_refs(const struct ref
*refs
,
632 const struct ref
*mapped_refs
,
633 const struct ref
*remote_head_points_at
,
634 const char *branch_top
,
636 struct transport
*transport
,
637 int check_connectivity
)
639 const struct ref
*rm
= mapped_refs
;
641 if (check_connectivity
) {
642 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
644 opt
.transport
= transport
;
645 opt
.progress
= transport
->progress
;
647 if (check_connected(iterate_ref_map
, &rm
, &opt
))
648 die(_("remote did not send all necessary objects"));
652 write_remote_refs(mapped_refs
);
653 if (option_single_branch
&& !option_no_tags
)
654 write_followtags(refs
, msg
);
657 if (remote_head_points_at
&& !option_bare
) {
658 struct strbuf head_ref
= STRBUF_INIT
;
659 strbuf_addstr(&head_ref
, branch_top
);
660 strbuf_addstr(&head_ref
, "HEAD");
661 if (refs_update_symref(get_main_ref_store(the_repository
), head_ref
.buf
,
662 remote_head_points_at
->peer_ref
->name
,
664 die(_("unable to update %s"), head_ref
.buf
);
665 strbuf_release(&head_ref
);
669 static void update_head(const struct ref
*our
, const struct ref
*remote
,
670 const char *unborn
, const char *msg
)
673 if (our
&& skip_prefix(our
->name
, "refs/heads/", &head
)) {
674 /* Local default branch link */
675 if (refs_update_symref(get_main_ref_store(the_repository
), "HEAD", our
->name
, NULL
) < 0)
676 die(_("unable to update HEAD"));
678 refs_update_ref(get_main_ref_store(the_repository
),
679 msg
, "HEAD", &our
->old_oid
, NULL
, 0,
680 UPDATE_REFS_DIE_ON_ERR
);
681 install_branch_config(0, head
, remote_name
, our
->name
);
684 struct commit
*c
= lookup_commit_reference(the_repository
,
686 /* --branch specifies a non-branch (i.e. tags), detach HEAD */
687 refs_update_ref(get_main_ref_store(the_repository
), msg
,
688 "HEAD", &c
->object
.oid
, NULL
, REF_NO_DEREF
,
689 UPDATE_REFS_DIE_ON_ERR
);
692 * We know remote HEAD points to a non-branch, or
693 * HEAD points to a branch but we don't know which one.
694 * Detach HEAD in all these cases.
696 refs_update_ref(get_main_ref_store(the_repository
), msg
,
697 "HEAD", &remote
->old_oid
, NULL
, REF_NO_DEREF
,
698 UPDATE_REFS_DIE_ON_ERR
);
699 } else if (unborn
&& skip_prefix(unborn
, "refs/heads/", &head
)) {
701 * Unborn head from remote; same as "our" case above except
702 * that we have no ref to update.
704 if (refs_update_symref(get_main_ref_store(the_repository
), "HEAD", unborn
, NULL
) < 0)
705 die(_("unable to update HEAD"));
707 install_branch_config(0, head
, remote_name
, unborn
);
711 static int git_sparse_checkout_init(const char *repo
)
713 struct child_process cmd
= CHILD_PROCESS_INIT
;
715 strvec_pushl(&cmd
.args
, "-C", repo
, "sparse-checkout", "set", NULL
);
718 * We must apply the setting in the current process
719 * for the later checkout to use the sparse-checkout file.
721 core_apply_sparse_checkout
= 1;
724 if (run_command(&cmd
)) {
725 error(_("failed to initialize sparse-checkout"));
732 static int checkout(int submodule_progress
, int filter_submodules
)
734 struct object_id oid
;
736 struct lock_file lock_file
= LOCK_INIT
;
737 struct unpack_trees_options opts
;
742 if (option_no_checkout
)
745 head
= refs_resolve_refdup(get_main_ref_store(the_repository
), "HEAD",
746 RESOLVE_REF_READING
, &oid
, NULL
);
748 warning(_("remote HEAD refers to nonexistent ref, "
749 "unable to checkout"));
752 if (!strcmp(head
, "HEAD")) {
753 if (advice_enabled(ADVICE_DETACHED_HEAD
))
754 detach_advice(oid_to_hex(&oid
));
757 if (!starts_with(head
, "refs/heads/"))
758 die(_("HEAD not found below refs/heads!"));
761 /* We need to be in the new work tree for the checkout */
764 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
766 memset(&opts
, 0, sizeof opts
);
770 opts
.preserve_ignored
= 0;
771 opts
.fn
= oneway_merge
;
772 opts
.verbose_update
= (option_verbosity
>= 0);
773 opts
.src_index
= the_repository
->index
;
774 opts
.dst_index
= the_repository
->index
;
775 init_checkout_metadata(&opts
.meta
, head
, &oid
, NULL
);
777 tree
= parse_tree_indirect(&oid
);
779 die(_("unable to parse commit %s"), oid_to_hex(&oid
));
780 if (parse_tree(tree
) < 0)
782 init_tree_desc(&t
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
783 if (unpack_trees(1, &t
, &opts
) < 0)
784 die(_("unable to checkout working tree"));
788 if (write_locked_index(the_repository
->index
, &lock_file
, COMMIT_LOCK
))
789 die(_("unable to write new index file"));
791 err
|= run_hooks_l("post-checkout", oid_to_hex(null_oid()),
792 oid_to_hex(&oid
), "1", NULL
);
794 if (!err
&& (option_recurse_submodules
.nr
> 0)) {
795 struct child_process cmd
= CHILD_PROCESS_INIT
;
796 strvec_pushl(&cmd
.args
, "submodule", "update", "--require-init",
797 "--recursive", NULL
);
799 if (option_shallow_submodules
== 1)
800 strvec_push(&cmd
.args
, "--depth=1");
803 strvec_pushf(&cmd
.args
, "--jobs=%d", max_jobs
);
805 if (submodule_progress
)
806 strvec_push(&cmd
.args
, "--progress");
808 if (option_verbosity
< 0)
809 strvec_push(&cmd
.args
, "--quiet");
811 if (option_remote_submodules
) {
812 strvec_push(&cmd
.args
, "--remote");
813 strvec_push(&cmd
.args
, "--no-fetch");
816 if (filter_submodules
&& filter_options
.choice
)
817 strvec_pushf(&cmd
.args
, "--filter=%s",
818 expand_list_objects_filter_spec(&filter_options
));
820 if (option_single_branch
>= 0)
821 strvec_push(&cmd
.args
, option_single_branch
?
823 "--no-single-branch");
826 err
= run_command(&cmd
);
832 static int git_clone_config(const char *k
, const char *v
,
833 const struct config_context
*ctx
, void *cb
)
835 if (!strcmp(k
, "clone.defaultremotename")) {
837 return config_error_nonbool(k
);
839 remote_name
= xstrdup(v
);
841 if (!strcmp(k
, "clone.rejectshallow"))
842 config_reject_shallow
= git_config_bool(k
, v
);
843 if (!strcmp(k
, "clone.filtersubmodules"))
844 config_filter_submodules
= git_config_bool(k
, v
);
846 return git_default_config(k
, v
, ctx
, cb
);
849 static int write_one_config(const char *key
, const char *value
,
850 const struct config_context
*ctx
,
854 * give git_clone_config a chance to write config values back to the
855 * environment, since git_config_set_multivar_gently only deals with
858 int apply_failed
= git_clone_config(key
, value
, ctx
, data
);
862 return git_config_set_multivar_gently(key
,
863 value
? value
: "true",
864 CONFIG_REGEX_NONE
, 0);
867 static void write_config(struct string_list
*config
)
871 for (i
= 0; i
< config
->nr
; i
++) {
872 if (git_config_parse_parameter(config
->items
[i
].string
,
873 write_one_config
, NULL
) < 0)
874 die(_("unable to write parameters to config file"));
878 static void write_refspec_config(const char *src_ref_prefix
,
879 const struct ref
*our_head_points_at
,
880 const struct ref
*remote_head_points_at
,
881 struct strbuf
*branch_top
)
883 struct strbuf key
= STRBUF_INIT
;
884 struct strbuf value
= STRBUF_INIT
;
886 if (option_mirror
|| !option_bare
) {
887 if (option_single_branch
&& !option_mirror
) {
889 if (starts_with(our_head_points_at
->name
, "refs/tags/"))
890 strbuf_addf(&value
, "+%s:%s", our_head_points_at
->name
,
891 our_head_points_at
->name
);
893 strbuf_addf(&value
, "+%s:%s%s", our_head_points_at
->name
,
894 branch_top
->buf
, option_branch
);
895 } else if (remote_head_points_at
) {
896 const char *head
= remote_head_points_at
->name
;
897 if (!skip_prefix(head
, "refs/heads/", &head
))
898 BUG("remote HEAD points at non-head?");
900 strbuf_addf(&value
, "+%s:%s%s", remote_head_points_at
->name
,
901 branch_top
->buf
, head
);
904 * otherwise, the next "git fetch" will
905 * simply fetch from HEAD without updating
906 * any remote-tracking branch, which is what
910 strbuf_addf(&value
, "+%s*:%s*", src_ref_prefix
, branch_top
->buf
);
912 /* Configure the remote */
914 strbuf_addf(&key
, "remote.%s.fetch", remote_name
);
915 git_config_set_multivar(key
.buf
, value
.buf
, "^$", 0);
919 strbuf_addf(&key
, "remote.%s.mirror", remote_name
);
920 git_config_set(key
.buf
, "true");
926 strbuf_release(&key
);
927 strbuf_release(&value
);
930 static void dissociate_from_references(void)
932 char *alternates
= git_pathdup("objects/info/alternates");
934 if (!access(alternates
, F_OK
)) {
935 struct child_process cmd
= CHILD_PROCESS_INIT
;
939 strvec_pushl(&cmd
.args
, "repack", "-a", "-d", NULL
);
940 if (run_command(&cmd
))
941 die(_("cannot repack to clean up"));
942 if (unlink(alternates
) && errno
!= ENOENT
)
943 die_errno(_("cannot unlink temporary alternates file"));
948 static int path_exists(const char *path
)
951 return !stat(path
, &sb
);
954 int cmd_clone(int argc
, const char **argv
, const char *prefix
)
956 int is_bundle
= 0, is_local
;
957 int reject_shallow
= 0;
958 const char *repo_name
, *repo
, *work_tree
, *git_dir
;
959 char *repo_to_free
= NULL
;
960 char *path
= NULL
, *dir
, *display_repo
= NULL
;
961 int dest_exists
, real_dest_exists
= 0;
962 const struct ref
*refs
, *remote_head
;
963 struct ref
*remote_head_points_at
= NULL
;
964 const struct ref
*our_head_points_at
;
965 char *unborn_head
= NULL
;
966 struct ref
*mapped_refs
= NULL
;
967 const struct ref
*ref
;
968 struct strbuf key
= STRBUF_INIT
;
969 struct strbuf buf
= STRBUF_INIT
;
970 struct strbuf branch_top
= STRBUF_INIT
, reflog_msg
= STRBUF_INIT
;
971 struct transport
*transport
= NULL
;
972 const char *src_ref_prefix
= "refs/heads/";
973 struct remote
*remote
;
974 int err
= 0, complete_refs_before_fetch
= 1;
975 int submodule_progress
;
976 int filter_submodules
= 0;
978 enum ref_storage_format ref_storage_format
= REF_STORAGE_FORMAT_UNKNOWN
;
979 const int do_not_override_repo_unix_permissions
= -1;
981 struct transport_ls_refs_options transport_ls_refs_options
=
982 TRANSPORT_LS_REFS_OPTIONS_INIT
;
984 packet_trace_identity("clone");
986 git_config(git_clone_config
, NULL
);
988 argc
= parse_options(argc
, argv
, prefix
, builtin_clone_options
,
989 builtin_clone_usage
, 0);
992 usage_msg_opt(_("Too many arguments."),
993 builtin_clone_usage
, builtin_clone_options
);
996 usage_msg_opt(_("You must specify a repository to clone."),
997 builtin_clone_usage
, builtin_clone_options
);
999 if (option_depth
|| option_since
|| option_not
.nr
)
1001 if (option_single_branch
== -1)
1002 option_single_branch
= deepen
? 1 : 0;
1005 ref_storage_format
= ref_storage_format_by_name(ref_format
);
1006 if (ref_storage_format
== REF_STORAGE_FORMAT_UNKNOWN
)
1007 die(_("unknown ref storage format '%s'"), ref_format
);
1015 die(_("options '%s' and '%s' cannot be used together"), "--bare", "--separate-git-dir");
1016 option_no_checkout
= 1;
1019 if (bundle_uri
&& deepen
)
1020 die(_("options '%s' and '%s' cannot be used together"),
1022 "--depth/--shallow-since/--shallow-exclude");
1024 repo_name
= argv
[0];
1026 path
= get_repo_path(repo_name
, &is_bundle
);
1028 FREE_AND_NULL(path
);
1029 repo
= repo_to_free
= absolute_pathdup(repo_name
);
1030 } else if (strchr(repo_name
, ':')) {
1032 display_repo
= transport_anonymize_url(repo
);
1034 die(_("repository '%s' does not exist"), repo_name
);
1036 /* no need to be strict, transport_set_option() will validate it again */
1037 if (option_depth
&& atoi(option_depth
) < 1)
1038 die(_("depth %s is not a positive number"), option_depth
);
1041 dir
= xstrdup(argv
[1]);
1043 dir
= git_url_basename(repo_name
, is_bundle
, option_bare
);
1044 strip_dir_trailing_slashes(dir
);
1046 dest_exists
= path_exists(dir
);
1047 if (dest_exists
&& !is_empty_dir(dir
))
1048 die(_("destination path '%s' already exists and is not "
1049 "an empty directory."), dir
);
1052 real_dest_exists
= path_exists(real_git_dir
);
1053 if (real_dest_exists
&& !is_empty_dir(real_git_dir
))
1054 die(_("repository path '%s' already exists and is not "
1055 "an empty directory."), real_git_dir
);
1059 strbuf_addf(&reflog_msg
, "clone: from %s",
1060 display_repo
? display_repo
: repo
);
1066 work_tree
= getenv("GIT_WORK_TREE");
1067 if (work_tree
&& path_exists(work_tree
))
1068 die(_("working tree '%s' already exists."), work_tree
);
1071 if (option_bare
|| work_tree
)
1072 git_dir
= xstrdup(dir
);
1075 git_dir
= mkpathdup("%s/.git", dir
);
1078 atexit(remove_junk
);
1079 sigchain_push_common(remove_junk_on_signal
);
1082 if (safe_create_leading_directories_const(work_tree
) < 0)
1083 die_errno(_("could not create leading directories of '%s'"),
1086 junk_work_tree_flags
|= REMOVE_DIR_KEEP_TOPLEVEL
;
1087 else if (mkdir(work_tree
, 0777))
1088 die_errno(_("could not create work tree dir '%s'"),
1090 junk_work_tree
= work_tree
;
1091 set_git_work_tree(work_tree
);
1095 if (real_dest_exists
)
1096 junk_git_dir_flags
|= REMOVE_DIR_KEEP_TOPLEVEL
;
1097 junk_git_dir
= real_git_dir
;
1100 junk_git_dir_flags
|= REMOVE_DIR_KEEP_TOPLEVEL
;
1101 junk_git_dir
= git_dir
;
1103 if (safe_create_leading_directories_const(git_dir
) < 0)
1104 die(_("could not create leading directories of '%s'"), git_dir
);
1106 if (0 <= option_verbosity
) {
1108 fprintf(stderr
, _("Cloning into bare repository '%s'...\n"), dir
);
1110 fprintf(stderr
, _("Cloning into '%s'...\n"), dir
);
1113 if (option_recurse_submodules
.nr
> 0) {
1114 struct string_list_item
*item
;
1115 struct strbuf sb
= STRBUF_INIT
;
1118 /* remove duplicates */
1119 string_list_sort(&option_recurse_submodules
);
1120 string_list_remove_duplicates(&option_recurse_submodules
, 0);
1123 * NEEDSWORK: In a multi-working-tree world, this needs to be
1124 * set in the per-worktree config.
1126 for_each_string_list_item(item
, &option_recurse_submodules
) {
1127 strbuf_addf(&sb
, "submodule.active=%s",
1129 string_list_append(&option_config
,
1130 strbuf_detach(&sb
, NULL
));
1133 if (!git_config_get_bool("submodule.stickyRecursiveClone", &val
) &&
1135 string_list_append(&option_config
, "submodule.recurse=true");
1137 if (option_required_reference
.nr
&&
1138 option_optional_reference
.nr
)
1139 die(_("clone --recursive is not compatible with "
1140 "both --reference and --reference-if-able"));
1141 else if (option_required_reference
.nr
) {
1142 string_list_append(&option_config
,
1143 "submodule.alternateLocation=superproject");
1144 string_list_append(&option_config
,
1145 "submodule.alternateErrorStrategy=die");
1146 } else if (option_optional_reference
.nr
) {
1147 string_list_append(&option_config
,
1148 "submodule.alternateLocation=superproject");
1149 string_list_append(&option_config
,
1150 "submodule.alternateErrorStrategy=info");
1155 * Initialize the repository, but skip initializing the reference
1156 * database. We do not yet know about the object format of the
1157 * repository, and reference backends may persist that information into
1158 * their on-disk data structures.
1160 init_db(git_dir
, real_git_dir
, option_template
, GIT_HASH_UNKNOWN
,
1161 ref_storage_format
, NULL
,
1162 do_not_override_repo_unix_permissions
, INIT_DB_QUIET
| INIT_DB_SKIP_REFDB
);
1165 free((char *)git_dir
);
1166 git_dir
= real_git_dir
;
1170 * We have a chicken-and-egg situation between initializing the refdb
1171 * and spawning transport helpers:
1173 * - Initializing the refdb requires us to know about the object
1174 * format. We thus have to spawn the transport helper to learn
1177 * - The transport helper may want to access the Git repository. But
1178 * because the refdb has not been initialized, we don't have "HEAD"
1179 * or "refs/". Thus, the helper cannot find the Git repository.
1181 * Ideally, we would have structured the helper protocol such that it's
1182 * mandatory for the helper to first announce its capabilities without
1183 * yet assuming a fully initialized repository. Like that, we could
1184 * have added a "lazy-refdb-init" capability that announces whether the
1185 * helper is ready to handle not-yet-initialized refdbs. If any helper
1186 * didn't support them, we would have fully initialized the refdb with
1187 * the SHA1 object format, but later on bailed out if we found out that
1188 * the remote repository used a different object format.
1190 * But we didn't, and thus we use the following workaround to partially
1191 * initialize the repository's refdb such that it can be discovered by
1192 * Git commands. To do so, we:
1194 * - Create an invalid HEAD ref pointing at "refs/heads/.invalid".
1196 * - Create the "refs/" directory.
1198 * - Set up the ref storage format and repository version as
1201 * This is sufficient for Git commands to discover the Git directory.
1203 initialize_repository_version(GIT_HASH_UNKNOWN
,
1204 the_repository
->ref_storage_format
, 1);
1206 strbuf_addf(&buf
, "%s/HEAD", git_dir
);
1207 write_file(buf
.buf
, "ref: refs/heads/.invalid");
1210 strbuf_addf(&buf
, "%s/refs", git_dir
);
1211 safe_create_dir(buf
.buf
, 1);
1214 * additional config can be injected with -c, make sure it's included
1215 * after init_db, which clears the entire config environment.
1217 write_config(&option_config
);
1220 * re-read config after init_db and write_config to pick up any config
1221 * injected by --template and --config, respectively.
1223 git_config(git_clone_config
, NULL
);
1226 * If option_reject_shallow is specified from CLI option,
1227 * ignore config_reject_shallow from git_clone_config.
1229 if (config_reject_shallow
!= -1)
1230 reject_shallow
= config_reject_shallow
;
1231 if (option_reject_shallow
!= -1)
1232 reject_shallow
= option_reject_shallow
;
1235 * If option_filter_submodules is specified from CLI option,
1236 * ignore config_filter_submodules from git_clone_config.
1238 if (config_filter_submodules
!= -1)
1239 filter_submodules
= config_filter_submodules
;
1240 if (option_filter_submodules
!= -1)
1241 filter_submodules
= option_filter_submodules
;
1244 * Exit if the user seems to be doing something silly with submodule
1245 * filter flags (but not with filter configs, as those should be
1248 if (option_filter_submodules
> 0 && !filter_options
.choice
)
1249 die(_("the option '%s' requires '%s'"),
1250 "--also-filter-submodules", "--filter");
1251 if (option_filter_submodules
> 0 && !option_recurse_submodules
.nr
)
1252 die(_("the option '%s' requires '%s'"),
1253 "--also-filter-submodules", "--recurse-submodules");
1256 * apply the remote name provided by --origin only after this second
1257 * call to git_config, to ensure it overrides all config-based values.
1259 if (option_origin
) {
1261 remote_name
= xstrdup(option_origin
);
1265 remote_name
= xstrdup("origin");
1267 if (!valid_remote_name(remote_name
))
1268 die(_("'%s' is not a valid remote name"), remote_name
);
1272 src_ref_prefix
= "refs/";
1273 strbuf_addstr(&branch_top
, src_ref_prefix
);
1275 git_config_set("core.bare", "true");
1277 strbuf_addf(&branch_top
, "refs/remotes/%s/", remote_name
);
1280 strbuf_addf(&key
, "remote.%s.url", remote_name
);
1281 git_config_set(key
.buf
, repo
);
1284 if (option_no_tags
) {
1285 strbuf_addf(&key
, "remote.%s.tagOpt", remote_name
);
1286 git_config_set(key
.buf
, "--no-tags");
1290 if (option_required_reference
.nr
|| option_optional_reference
.nr
)
1293 remote
= remote_get_early(remote_name
);
1295 refspec_appendf(&remote
->fetch
, "+%s*:%s*", src_ref_prefix
,
1298 path
= get_repo_path(remote
->url
.v
[0], &is_bundle
);
1299 is_local
= option_local
!= 0 && path
&& !is_bundle
;
1302 warning(_("--depth is ignored in local clones; use file:// instead."));
1304 warning(_("--shallow-since is ignored in local clones; use file:// instead."));
1306 warning(_("--shallow-exclude is ignored in local clones; use file:// instead."));
1307 if (filter_options
.choice
)
1308 warning(_("--filter is ignored in local clones; use file:// instead."));
1309 if (!access(mkpath("%s/shallow", path
), F_OK
)) {
1311 die(_("source repository is shallow, reject to clone."));
1312 if (option_local
> 0)
1313 warning(_("source repository is shallow, ignoring --local"));
1317 if (option_local
> 0 && !is_local
)
1318 warning(_("--local is ignored"));
1320 transport
= transport_get(remote
, path
? path
: remote
->url
.v
[0]);
1321 transport_set_verbosity(transport
, option_verbosity
, option_progress
);
1322 transport
->family
= family
;
1323 transport
->cloning
= 1;
1326 struct bundle_header header
= BUNDLE_HEADER_INIT
;
1327 int fd
= read_bundle_header(path
, &header
);
1328 int has_filter
= header
.filter
.choice
!= LOFC_DISABLED
;
1332 bundle_header_release(&header
);
1334 die(_("cannot clone from filtered bundle"));
1337 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
1340 transport_set_option(transport
, TRANS_OPT_REJECT_SHALLOW
, "1");
1342 transport_set_option(transport
, TRANS_OPT_DEPTH
,
1345 transport_set_option(transport
, TRANS_OPT_DEEPEN_SINCE
,
1348 transport_set_option(transport
, TRANS_OPT_DEEPEN_NOT
,
1349 (const char *)&option_not
);
1350 if (option_single_branch
)
1351 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
1353 if (option_upload_pack
)
1354 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
,
1355 option_upload_pack
);
1357 if (server_options
.nr
)
1358 transport
->server_options
= &server_options
;
1360 if (filter_options
.choice
) {
1362 expand_list_objects_filter_spec(&filter_options
);
1363 transport_set_option(transport
, TRANS_OPT_LIST_OBJECTS_FILTER
,
1365 transport_set_option(transport
, TRANS_OPT_FROM_PROMISOR
, "1");
1368 if (transport
->smart_options
&& !deepen
&& !filter_options
.choice
)
1369 transport
->smart_options
->check_self_contained_and_connected
= 1;
1371 strvec_push(&transport_ls_refs_options
.ref_prefixes
, "HEAD");
1372 refspec_ref_prefixes(&remote
->fetch
,
1373 &transport_ls_refs_options
.ref_prefixes
);
1375 expand_ref_prefix(&transport_ls_refs_options
.ref_prefixes
,
1377 if (!option_no_tags
)
1378 strvec_push(&transport_ls_refs_options
.ref_prefixes
,
1381 refs
= transport_get_remote_refs(transport
, &transport_ls_refs_options
);
1384 * Now that we know what algorithm the remote side is using, let's set
1385 * ours to the same thing.
1387 hash_algo
= hash_algo_by_ptr(transport_get_hash_algo(transport
));
1388 initialize_repository_version(hash_algo
, the_repository
->ref_storage_format
, 1);
1389 repo_set_hash_algo(the_repository
, hash_algo
);
1390 create_reference_database(the_repository
->ref_storage_format
, NULL
, 1);
1393 * Before fetching from the remote, download and install bundle
1394 * data from the --bundle-uri option.
1397 int has_heuristic
= 0;
1399 /* At this point, we need the_repository to match the cloned repo. */
1400 if (repo_init(the_repository
, git_dir
, work_tree
))
1401 warning(_("failed to initialize the repo, skipping bundle URI"));
1402 else if (fetch_bundle_uri(the_repository
, bundle_uri
, &has_heuristic
))
1403 warning(_("failed to fetch objects from bundle URI '%s'"),
1405 else if (has_heuristic
)
1406 git_config_set_gently("fetch.bundleuri", bundle_uri
);
1409 * Populate transport->got_remote_bundle_uri and
1410 * transport->bundle_uri. We might get nothing.
1412 transport_get_remote_bundle_uri(transport
);
1414 if (transport
->bundles
&&
1415 hashmap_get_size(&transport
->bundles
->bundles
)) {
1416 /* At this point, we need the_repository to match the cloned repo. */
1417 if (repo_init(the_repository
, git_dir
, work_tree
))
1418 warning(_("failed to initialize the repo, skipping bundle URI"));
1419 else if (fetch_bundle_list(the_repository
,
1420 transport
->bundles
))
1421 warning(_("failed to fetch advertised bundles"));
1423 clear_bundle_list(transport
->bundles
);
1424 FREE_AND_NULL(transport
->bundles
);
1429 mapped_refs
= wanted_peer_refs(refs
, &remote
->fetch
);
1433 * transport_get_remote_refs() may return refs with null sha-1
1434 * in mapped_refs (see struct transport->get_refs_list
1435 * comment). In that case we need fetch it early because
1436 * remote_head code below relies on it.
1438 * for normal clones, transport_get_remote_refs() should
1439 * return reliable ref set, we can delay cloning until after
1440 * remote HEAD check.
1442 for (ref
= refs
; ref
; ref
= ref
->next
)
1443 if (is_null_oid(&ref
->old_oid
)) {
1444 complete_refs_before_fetch
= 0;
1448 if (!is_local
&& !complete_refs_before_fetch
) {
1449 if (transport_fetch_refs(transport
, mapped_refs
))
1450 die(_("remote transport reported error"));
1454 remote_head
= find_ref_by_name(refs
, "HEAD");
1455 remote_head_points_at
= guess_remote_head(remote_head
, mapped_refs
, 0);
1457 if (option_branch
) {
1458 our_head_points_at
= find_remote_branch(mapped_refs
, option_branch
);
1459 if (!our_head_points_at
)
1460 die(_("Remote branch %s not found in upstream %s"),
1461 option_branch
, remote_name
);
1462 } else if (remote_head_points_at
) {
1463 our_head_points_at
= remote_head_points_at
;
1464 } else if (remote_head
) {
1465 our_head_points_at
= NULL
;
1467 char *to_free
= NULL
;
1471 warning(_("You appear to have cloned an empty repository."));
1472 option_no_checkout
= 1;
1475 if (transport_ls_refs_options
.unborn_head_target
&&
1476 skip_prefix(transport_ls_refs_options
.unborn_head_target
,
1477 "refs/heads/", &branch
)) {
1478 unborn_head
= xstrdup(transport_ls_refs_options
.unborn_head_target
);
1480 branch
= to_free
= repo_default_branch_name(the_repository
, 0);
1481 unborn_head
= xstrfmt("refs/heads/%s", branch
);
1485 * We may have selected a local default branch name "foo",
1486 * and even though the remote's HEAD does not point there,
1487 * it may still have a "foo" branch. If so, set it up so
1488 * that we can follow the usual checkout code later.
1490 * Note that for an empty repo we'll already have set
1491 * option_no_checkout above, which would work against us here.
1492 * But for an empty repo, find_remote_branch() can never find
1495 our_head_points_at
= find_remote_branch(mapped_refs
, branch
);
1500 write_refspec_config(src_ref_prefix
, our_head_points_at
,
1501 remote_head_points_at
, &branch_top
);
1503 if (filter_options
.choice
)
1504 partial_clone_register(remote_name
, &filter_options
);
1507 clone_local(path
, git_dir
);
1508 else if (mapped_refs
&& complete_refs_before_fetch
) {
1509 if (transport_fetch_refs(transport
, mapped_refs
))
1510 die(_("remote transport reported error"));
1513 update_remote_refs(refs
, mapped_refs
, remote_head_points_at
,
1514 branch_top
.buf
, reflog_msg
.buf
, transport
,
1517 update_head(our_head_points_at
, remote_head
, unborn_head
, reflog_msg
.buf
);
1520 * We want to show progress for recursive submodule clones iff
1521 * we did so for the main clone. But only the transport knows
1522 * the final decision for this flag, so we need to rescue the value
1523 * before we free the transport.
1525 submodule_progress
= transport
->progress
;
1527 transport_unlock_pack(transport
, 0);
1528 transport_disconnect(transport
);
1530 if (option_dissociate
) {
1531 close_object_store(the_repository
->objects
);
1532 dissociate_from_references();
1535 if (option_sparse_checkout
&& git_sparse_checkout_init(dir
))
1538 junk_mode
= JUNK_LEAVE_REPO
;
1539 err
= checkout(submodule_progress
, filter_submodules
);
1542 strbuf_release(&reflog_msg
);
1543 strbuf_release(&branch_top
);
1544 strbuf_release(&buf
);
1545 strbuf_release(&key
);
1546 free_refs(mapped_refs
);
1547 free_refs(remote_head_points_at
);
1553 junk_mode
= JUNK_LEAVE_ALL
;
1555 transport_ls_refs_options_release(&transport_ls_refs_options
);