object-store-ll.h: split this header out of object-store.h
[git/debian.git] / builtin / clone.c
blob687a686269a4ab241a168e7675080791f0c7b065
1 /*
2 * Builtin "git clone"
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.
9 */
11 #define USE_THE_INDEX_VARIABLE
12 #include "builtin.h"
13 #include "abspath.h"
14 #include "advice.h"
15 #include "config.h"
16 #include "copy.h"
17 #include "environment.h"
18 #include "gettext.h"
19 #include "hex.h"
20 #include "lockfile.h"
21 #include "parse-options.h"
22 #include "fetch-pack.h"
23 #include "refs.h"
24 #include "refspec.h"
25 #include "object-file.h"
26 #include "object-store-ll.h"
27 #include "tree.h"
28 #include "tree-walk.h"
29 #include "unpack-trees.h"
30 #include "transport.h"
31 #include "strbuf.h"
32 #include "dir.h"
33 #include "dir-iterator.h"
34 #include "iterator.h"
35 #include "sigchain.h"
36 #include "branch.h"
37 #include "remote.h"
38 #include "run-command.h"
39 #include "setup.h"
40 #include "connected.h"
41 #include "packfile.h"
42 #include "path.h"
43 #include "pkt-line.h"
44 #include "list-objects-filter-options.h"
45 #include "hook.h"
46 #include "bundle.h"
47 #include "bundle-uri.h"
48 #include "wrapper.h"
51 * Overall FIXMEs:
52 * - respect DB_ENVIRONMENT for .git/objects.
54 * Implementation notes:
55 * - dropping use-separate-remote and no-separate-remote compatibility
58 static const char * const builtin_clone_usage[] = {
59 N_("git clone [<options>] [--] <repo> [<dir>]"),
60 NULL
63 static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
64 static int option_local = -1, option_no_hardlinks, option_shared;
65 static int option_no_tags;
66 static int option_shallow_submodules;
67 static int option_reject_shallow = -1; /* unspecified */
68 static int config_reject_shallow = -1; /* unspecified */
69 static int deepen;
70 static char *option_template, *option_depth, *option_since;
71 static char *option_origin = NULL;
72 static char *remote_name = NULL;
73 static char *option_branch = NULL;
74 static struct string_list option_not = STRING_LIST_INIT_NODUP;
75 static const char *real_git_dir;
76 static char *option_upload_pack = "git-upload-pack";
77 static int option_verbosity;
78 static int option_progress = -1;
79 static int option_sparse_checkout;
80 static enum transport_family family;
81 static struct string_list option_config = STRING_LIST_INIT_NODUP;
82 static struct string_list option_required_reference = STRING_LIST_INIT_NODUP;
83 static struct string_list option_optional_reference = STRING_LIST_INIT_NODUP;
84 static int option_dissociate;
85 static int max_jobs = -1;
86 static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP;
87 static struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT;
88 static int option_filter_submodules = -1; /* unspecified */
89 static int config_filter_submodules = -1; /* unspecified */
90 static struct string_list server_options = STRING_LIST_INIT_NODUP;
91 static int option_remote_submodules;
92 static const char *bundle_uri;
94 static int recurse_submodules_cb(const struct option *opt,
95 const char *arg, int unset)
97 if (unset)
98 string_list_clear((struct string_list *)opt->value, 0);
99 else if (arg)
100 string_list_append((struct string_list *)opt->value, arg);
101 else
102 string_list_append((struct string_list *)opt->value,
103 (const char *)opt->defval);
105 return 0;
108 static struct option builtin_clone_options[] = {
109 OPT__VERBOSITY(&option_verbosity),
110 OPT_BOOL(0, "progress", &option_progress,
111 N_("force progress reporting")),
112 OPT_BOOL(0, "reject-shallow", &option_reject_shallow,
113 N_("don't clone shallow repository")),
114 OPT_BOOL('n', "no-checkout", &option_no_checkout,
115 N_("don't create a checkout")),
116 OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
117 OPT_HIDDEN_BOOL(0, "naked", &option_bare,
118 N_("create a bare repository")),
119 OPT_BOOL(0, "mirror", &option_mirror,
120 N_("create a mirror repository (implies bare)")),
121 OPT_BOOL('l', "local", &option_local,
122 N_("to clone from a local repository")),
123 OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
124 N_("don't use local hardlinks, always copy")),
125 OPT_BOOL('s', "shared", &option_shared,
126 N_("setup as shared repository")),
127 { OPTION_CALLBACK, 0, "recurse-submodules", &option_recurse_submodules,
128 N_("pathspec"), N_("initialize submodules in the clone"),
129 PARSE_OPT_OPTARG, recurse_submodules_cb, (intptr_t)"." },
130 OPT_ALIAS(0, "recursive", "recurse-submodules"),
131 OPT_INTEGER('j', "jobs", &max_jobs,
132 N_("number of submodules cloned in parallel")),
133 OPT_STRING(0, "template", &option_template, N_("template-directory"),
134 N_("directory from which templates will be used")),
135 OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"),
136 N_("reference repository")),
137 OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference,
138 N_("repo"), N_("reference repository")),
139 OPT_BOOL(0, "dissociate", &option_dissociate,
140 N_("use --reference only while cloning")),
141 OPT_STRING('o', "origin", &option_origin, N_("name"),
142 N_("use <name> instead of 'origin' to track upstream")),
143 OPT_STRING('b', "branch", &option_branch, N_("branch"),
144 N_("checkout <branch> instead of the remote's HEAD")),
145 OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
146 N_("path to git-upload-pack on the remote")),
147 OPT_STRING(0, "depth", &option_depth, N_("depth"),
148 N_("create a shallow clone of that depth")),
149 OPT_STRING(0, "shallow-since", &option_since, N_("time"),
150 N_("create a shallow clone since a specific time")),
151 OPT_STRING_LIST(0, "shallow-exclude", &option_not, N_("revision"),
152 N_("deepen history of shallow clone, excluding rev")),
153 OPT_BOOL(0, "single-branch", &option_single_branch,
154 N_("clone only one branch, HEAD or --branch")),
155 OPT_BOOL(0, "no-tags", &option_no_tags,
156 N_("don't clone any tags, and make later fetches not to follow them")),
157 OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules,
158 N_("any cloned submodules will be shallow")),
159 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
160 N_("separate git dir from working tree")),
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_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
166 TRANSPORT_FAMILY_IPV4),
167 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
168 TRANSPORT_FAMILY_IPV6),
169 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
170 OPT_BOOL(0, "also-filter-submodules", &option_filter_submodules,
171 N_("apply partial clone filters to submodules")),
172 OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
173 N_("any cloned submodules will use their remote-tracking branch")),
174 OPT_BOOL(0, "sparse", &option_sparse_checkout,
175 N_("initialize sparse-checkout file to include only files at root")),
176 OPT_STRING(0, "bundle-uri", &bundle_uri,
177 N_("uri"), N_("a URI for downloading bundles before fetching from origin remote")),
178 OPT_END()
181 static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
183 static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
184 static char *bundle_suffix[] = { ".bundle", "" };
185 size_t baselen = path->len;
186 struct stat st;
187 int i;
189 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
190 strbuf_setlen(path, baselen);
191 strbuf_addstr(path, suffix[i]);
192 if (stat(path->buf, &st))
193 continue;
194 if (S_ISDIR(st.st_mode) && is_git_directory(path->buf)) {
195 *is_bundle = 0;
196 return path->buf;
197 } else if (S_ISREG(st.st_mode) && st.st_size > 8) {
198 /* Is it a "gitfile"? */
199 char signature[8];
200 const char *dst;
201 int len, fd = open(path->buf, O_RDONLY);
202 if (fd < 0)
203 continue;
204 len = read_in_full(fd, signature, 8);
205 close(fd);
206 if (len != 8 || strncmp(signature, "gitdir: ", 8))
207 continue;
208 dst = read_gitfile(path->buf);
209 if (dst) {
210 *is_bundle = 0;
211 return dst;
216 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
217 strbuf_setlen(path, baselen);
218 strbuf_addstr(path, bundle_suffix[i]);
219 if (!stat(path->buf, &st) && S_ISREG(st.st_mode)) {
220 *is_bundle = 1;
221 return path->buf;
225 return NULL;
228 static char *get_repo_path(const char *repo, int *is_bundle)
230 struct strbuf path = STRBUF_INIT;
231 const char *raw;
232 char *canon;
234 strbuf_addstr(&path, repo);
235 raw = get_repo_path_1(&path, is_bundle);
236 canon = raw ? absolute_pathdup(raw) : NULL;
237 strbuf_release(&path);
238 return canon;
241 static int add_one_reference(struct string_list_item *item, void *cb_data)
243 struct strbuf err = STRBUF_INIT;
244 int *required = cb_data;
245 char *ref_git = compute_alternate_path(item->string, &err);
247 if (!ref_git) {
248 if (*required)
249 die("%s", err.buf);
250 else
251 fprintf(stderr,
252 _("info: Could not add alternate for '%s': %s\n"),
253 item->string, err.buf);
254 } else {
255 struct strbuf sb = STRBUF_INIT;
256 strbuf_addf(&sb, "%s/objects", ref_git);
257 add_to_alternates_file(sb.buf);
258 strbuf_release(&sb);
261 strbuf_release(&err);
262 free(ref_git);
263 return 0;
266 static void setup_reference(void)
268 int required = 1;
269 for_each_string_list(&option_required_reference,
270 add_one_reference, &required);
271 required = 0;
272 for_each_string_list(&option_optional_reference,
273 add_one_reference, &required);
276 static void copy_alternates(struct strbuf *src, const char *src_repo)
279 * Read from the source objects/info/alternates file
280 * and copy the entries to corresponding file in the
281 * destination repository with add_to_alternates_file().
282 * Both src and dst have "$path/objects/info/alternates".
284 * Instead of copying bit-for-bit from the original,
285 * we need to append to existing one so that the already
286 * created entry via "clone -s" is not lost, and also
287 * to turn entries with paths relative to the original
288 * absolute, so that they can be used in the new repository.
290 FILE *in = xfopen(src->buf, "r");
291 struct strbuf line = STRBUF_INIT;
293 while (strbuf_getline(&line, in) != EOF) {
294 char *abs_path;
295 if (!line.len || line.buf[0] == '#')
296 continue;
297 if (is_absolute_path(line.buf)) {
298 add_to_alternates_file(line.buf);
299 continue;
301 abs_path = mkpathdup("%s/objects/%s", src_repo, line.buf);
302 if (!normalize_path_copy(abs_path, abs_path))
303 add_to_alternates_file(abs_path);
304 else
305 warning("skipping invalid relative alternate: %s/%s",
306 src_repo, line.buf);
307 free(abs_path);
309 strbuf_release(&line);
310 fclose(in);
313 static void mkdir_if_missing(const char *pathname, mode_t mode)
315 struct stat st;
317 if (!mkdir(pathname, mode))
318 return;
320 if (errno != EEXIST)
321 die_errno(_("failed to create directory '%s'"), pathname);
322 else if (stat(pathname, &st))
323 die_errno(_("failed to stat '%s'"), pathname);
324 else if (!S_ISDIR(st.st_mode))
325 die(_("%s exists and is not a directory"), pathname);
328 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
329 const char *src_repo)
331 int src_len, dest_len;
332 struct dir_iterator *iter;
333 int iter_status;
334 struct strbuf realpath = STRBUF_INIT;
336 mkdir_if_missing(dest->buf, 0777);
338 iter = dir_iterator_begin(src->buf, DIR_ITERATOR_PEDANTIC);
340 if (!iter) {
341 if (errno == ENOTDIR) {
342 int saved_errno = errno;
343 struct stat st;
345 if (!lstat(src->buf, &st) && S_ISLNK(st.st_mode))
346 die(_("'%s' is a symlink, refusing to clone with --local"),
347 src->buf);
348 errno = saved_errno;
350 die_errno(_("failed to start iterator over '%s'"), src->buf);
353 strbuf_addch(src, '/');
354 src_len = src->len;
355 strbuf_addch(dest, '/');
356 dest_len = dest->len;
358 while ((iter_status = dir_iterator_advance(iter)) == ITER_OK) {
359 strbuf_setlen(src, src_len);
360 strbuf_addstr(src, iter->relative_path);
361 strbuf_setlen(dest, dest_len);
362 strbuf_addstr(dest, iter->relative_path);
364 if (S_ISLNK(iter->st.st_mode))
365 die(_("symlink '%s' exists, refusing to clone with --local"),
366 iter->relative_path);
368 if (S_ISDIR(iter->st.st_mode)) {
369 mkdir_if_missing(dest->buf, 0777);
370 continue;
373 /* Files that cannot be copied bit-for-bit... */
374 if (!fspathcmp(iter->relative_path, "info/alternates")) {
375 copy_alternates(src, src_repo);
376 continue;
379 if (unlink(dest->buf) && errno != ENOENT)
380 die_errno(_("failed to unlink '%s'"), dest->buf);
381 if (!option_no_hardlinks) {
382 strbuf_realpath(&realpath, src->buf, 1);
383 if (!link(realpath.buf, dest->buf))
384 continue;
385 if (option_local > 0)
386 die_errno(_("failed to create link '%s'"), dest->buf);
387 option_no_hardlinks = 1;
389 if (copy_file_with_time(dest->buf, src->buf, 0666))
390 die_errno(_("failed to copy file to '%s'"), dest->buf);
393 if (iter_status != ITER_DONE) {
394 strbuf_setlen(src, src_len);
395 die(_("failed to iterate over '%s'"), src->buf);
398 strbuf_release(&realpath);
401 static void clone_local(const char *src_repo, const char *dest_repo)
403 if (option_shared) {
404 struct strbuf alt = STRBUF_INIT;
405 get_common_dir(&alt, src_repo);
406 strbuf_addstr(&alt, "/objects");
407 add_to_alternates_file(alt.buf);
408 strbuf_release(&alt);
409 } else {
410 struct strbuf src = STRBUF_INIT;
411 struct strbuf dest = STRBUF_INIT;
412 get_common_dir(&src, src_repo);
413 get_common_dir(&dest, dest_repo);
414 strbuf_addstr(&src, "/objects");
415 strbuf_addstr(&dest, "/objects");
416 copy_or_link_directory(&src, &dest, src_repo);
417 strbuf_release(&src);
418 strbuf_release(&dest);
421 if (0 <= option_verbosity)
422 fprintf(stderr, _("done.\n"));
425 static const char *junk_work_tree;
426 static int junk_work_tree_flags;
427 static const char *junk_git_dir;
428 static int junk_git_dir_flags;
429 static enum {
430 JUNK_LEAVE_NONE,
431 JUNK_LEAVE_REPO,
432 JUNK_LEAVE_ALL
433 } junk_mode = JUNK_LEAVE_NONE;
435 static const char junk_leave_repo_msg[] =
436 N_("Clone succeeded, but checkout failed.\n"
437 "You can inspect what was checked out with 'git status'\n"
438 "and retry with 'git restore --source=HEAD :/'\n");
440 static void remove_junk(void)
442 struct strbuf sb = STRBUF_INIT;
444 switch (junk_mode) {
445 case JUNK_LEAVE_REPO:
446 warning("%s", _(junk_leave_repo_msg));
447 /* fall-through */
448 case JUNK_LEAVE_ALL:
449 return;
450 default:
451 /* proceed to removal */
452 break;
455 if (junk_git_dir) {
456 strbuf_addstr(&sb, junk_git_dir);
457 remove_dir_recursively(&sb, junk_git_dir_flags);
458 strbuf_reset(&sb);
460 if (junk_work_tree) {
461 strbuf_addstr(&sb, junk_work_tree);
462 remove_dir_recursively(&sb, junk_work_tree_flags);
464 strbuf_release(&sb);
467 static void remove_junk_on_signal(int signo)
469 remove_junk();
470 sigchain_pop(signo);
471 raise(signo);
474 static struct ref *find_remote_branch(const struct ref *refs, const char *branch)
476 struct ref *ref;
477 struct strbuf head = STRBUF_INIT;
478 strbuf_addstr(&head, "refs/heads/");
479 strbuf_addstr(&head, branch);
480 ref = find_ref_by_name(refs, head.buf);
481 strbuf_release(&head);
483 if (ref)
484 return ref;
486 strbuf_addstr(&head, "refs/tags/");
487 strbuf_addstr(&head, branch);
488 ref = find_ref_by_name(refs, head.buf);
489 strbuf_release(&head);
491 return ref;
494 static struct ref *wanted_peer_refs(const struct ref *refs,
495 struct refspec *refspec)
497 struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD"));
498 struct ref *local_refs = head;
499 struct ref **tail = head ? &head->next : &local_refs;
501 if (option_single_branch) {
502 struct ref *remote_head = NULL;
504 if (!option_branch)
505 remote_head = guess_remote_head(head, refs, 0);
506 else {
507 local_refs = NULL;
508 tail = &local_refs;
509 remote_head = copy_ref(find_remote_branch(refs, option_branch));
512 if (!remote_head && option_branch)
513 warning(_("Could not find remote branch %s to clone."),
514 option_branch);
515 else {
516 int i;
517 for (i = 0; i < refspec->nr; i++)
518 get_fetch_map(remote_head, &refspec->items[i],
519 &tail, 0);
521 /* if --branch=tag, pull the requested tag explicitly */
522 get_fetch_map(remote_head, tag_refspec, &tail, 0);
524 free_refs(remote_head);
525 } else {
526 int i;
527 for (i = 0; i < refspec->nr; i++)
528 get_fetch_map(refs, &refspec->items[i], &tail, 0);
531 if (!option_mirror && !option_single_branch && !option_no_tags)
532 get_fetch_map(refs, tag_refspec, &tail, 0);
534 return local_refs;
537 static void write_remote_refs(const struct ref *local_refs)
539 const struct ref *r;
541 struct ref_transaction *t;
542 struct strbuf err = STRBUF_INIT;
544 t = ref_transaction_begin(&err);
545 if (!t)
546 die("%s", err.buf);
548 for (r = local_refs; r; r = r->next) {
549 if (!r->peer_ref)
550 continue;
551 if (ref_transaction_create(t, r->peer_ref->name, &r->old_oid,
552 0, NULL, &err))
553 die("%s", err.buf);
556 if (initial_ref_transaction_commit(t, &err))
557 die("%s", err.buf);
559 strbuf_release(&err);
560 ref_transaction_free(t);
563 static void write_followtags(const struct ref *refs, const char *msg)
565 const struct ref *ref;
566 for (ref = refs; ref; ref = ref->next) {
567 if (!starts_with(ref->name, "refs/tags/"))
568 continue;
569 if (ends_with(ref->name, "^{}"))
570 continue;
571 if (!repo_has_object_file_with_flags(the_repository, &ref->old_oid,
572 OBJECT_INFO_QUICK |
573 OBJECT_INFO_SKIP_FETCH_OBJECT))
574 continue;
575 update_ref(msg, ref->name, &ref->old_oid, NULL, 0,
576 UPDATE_REFS_DIE_ON_ERR);
580 static const struct object_id *iterate_ref_map(void *cb_data)
582 struct ref **rm = cb_data;
583 struct ref *ref = *rm;
586 * Skip anything missing a peer_ref, which we are not
587 * actually going to write a ref for.
589 while (ref && !ref->peer_ref)
590 ref = ref->next;
591 if (!ref)
592 return NULL;
594 *rm = ref->next;
595 return &ref->old_oid;
598 static void update_remote_refs(const struct ref *refs,
599 const struct ref *mapped_refs,
600 const struct ref *remote_head_points_at,
601 const char *branch_top,
602 const char *msg,
603 struct transport *transport,
604 int check_connectivity)
606 const struct ref *rm = mapped_refs;
608 if (check_connectivity) {
609 struct check_connected_options opt = CHECK_CONNECTED_INIT;
611 opt.transport = transport;
612 opt.progress = transport->progress;
614 if (check_connected(iterate_ref_map, &rm, &opt))
615 die(_("remote did not send all necessary objects"));
618 if (refs) {
619 write_remote_refs(mapped_refs);
620 if (option_single_branch && !option_no_tags)
621 write_followtags(refs, msg);
624 if (remote_head_points_at && !option_bare) {
625 struct strbuf head_ref = STRBUF_INIT;
626 strbuf_addstr(&head_ref, branch_top);
627 strbuf_addstr(&head_ref, "HEAD");
628 if (create_symref(head_ref.buf,
629 remote_head_points_at->peer_ref->name,
630 msg) < 0)
631 die(_("unable to update %s"), head_ref.buf);
632 strbuf_release(&head_ref);
636 static void update_head(const struct ref *our, const struct ref *remote,
637 const char *unborn, const char *msg)
639 const char *head;
640 if (our && skip_prefix(our->name, "refs/heads/", &head)) {
641 /* Local default branch link */
642 if (create_symref("HEAD", our->name, NULL) < 0)
643 die(_("unable to update HEAD"));
644 if (!option_bare) {
645 update_ref(msg, "HEAD", &our->old_oid, NULL, 0,
646 UPDATE_REFS_DIE_ON_ERR);
647 install_branch_config(0, head, remote_name, our->name);
649 } else if (our) {
650 struct commit *c = lookup_commit_reference(the_repository,
651 &our->old_oid);
652 /* --branch specifies a non-branch (i.e. tags), detach HEAD */
653 update_ref(msg, "HEAD", &c->object.oid, NULL, REF_NO_DEREF,
654 UPDATE_REFS_DIE_ON_ERR);
655 } else if (remote) {
657 * We know remote HEAD points to a non-branch, or
658 * HEAD points to a branch but we don't know which one.
659 * Detach HEAD in all these cases.
661 update_ref(msg, "HEAD", &remote->old_oid, NULL, REF_NO_DEREF,
662 UPDATE_REFS_DIE_ON_ERR);
663 } else if (unborn && skip_prefix(unborn, "refs/heads/", &head)) {
665 * Unborn head from remote; same as "our" case above except
666 * that we have no ref to update.
668 if (create_symref("HEAD", unborn, NULL) < 0)
669 die(_("unable to update HEAD"));
670 if (!option_bare)
671 install_branch_config(0, head, remote_name, unborn);
675 static int git_sparse_checkout_init(const char *repo)
677 struct child_process cmd = CHILD_PROCESS_INIT;
678 int result = 0;
679 strvec_pushl(&cmd.args, "-C", repo, "sparse-checkout", "set", NULL);
682 * We must apply the setting in the current process
683 * for the later checkout to use the sparse-checkout file.
685 core_apply_sparse_checkout = 1;
687 cmd.git_cmd = 1;
688 if (run_command(&cmd)) {
689 error(_("failed to initialize sparse-checkout"));
690 result = 1;
693 return result;
696 static int checkout(int submodule_progress, int filter_submodules)
698 struct object_id oid;
699 char *head;
700 struct lock_file lock_file = LOCK_INIT;
701 struct unpack_trees_options opts;
702 struct tree *tree;
703 struct tree_desc t;
704 int err = 0;
706 if (option_no_checkout)
707 return 0;
709 head = resolve_refdup("HEAD", RESOLVE_REF_READING, &oid, NULL);
710 if (!head) {
711 warning(_("remote HEAD refers to nonexistent ref, "
712 "unable to checkout"));
713 return 0;
715 if (!strcmp(head, "HEAD")) {
716 if (advice_enabled(ADVICE_DETACHED_HEAD))
717 detach_advice(oid_to_hex(&oid));
718 FREE_AND_NULL(head);
719 } else {
720 if (!starts_with(head, "refs/heads/"))
721 die(_("HEAD not found below refs/heads!"));
724 /* We need to be in the new work tree for the checkout */
725 setup_work_tree();
727 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
729 memset(&opts, 0, sizeof opts);
730 opts.update = 1;
731 opts.merge = 1;
732 opts.clone = 1;
733 opts.preserve_ignored = 0;
734 opts.fn = oneway_merge;
735 opts.verbose_update = (option_verbosity >= 0);
736 opts.src_index = &the_index;
737 opts.dst_index = &the_index;
738 init_checkout_metadata(&opts.meta, head, &oid, NULL);
740 tree = parse_tree_indirect(&oid);
741 if (!tree)
742 die(_("unable to parse commit %s"), oid_to_hex(&oid));
743 parse_tree(tree);
744 init_tree_desc(&t, tree->buffer, tree->size);
745 if (unpack_trees(1, &t, &opts) < 0)
746 die(_("unable to checkout working tree"));
748 free(head);
750 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
751 die(_("unable to write new index file"));
753 err |= run_hooks_l("post-checkout", oid_to_hex(null_oid()),
754 oid_to_hex(&oid), "1", NULL);
756 if (!err && (option_recurse_submodules.nr > 0)) {
757 struct child_process cmd = CHILD_PROCESS_INIT;
758 strvec_pushl(&cmd.args, "submodule", "update", "--require-init",
759 "--recursive", NULL);
761 if (option_shallow_submodules == 1)
762 strvec_push(&cmd.args, "--depth=1");
764 if (max_jobs != -1)
765 strvec_pushf(&cmd.args, "--jobs=%d", max_jobs);
767 if (submodule_progress)
768 strvec_push(&cmd.args, "--progress");
770 if (option_verbosity < 0)
771 strvec_push(&cmd.args, "--quiet");
773 if (option_remote_submodules) {
774 strvec_push(&cmd.args, "--remote");
775 strvec_push(&cmd.args, "--no-fetch");
778 if (filter_submodules && filter_options.choice)
779 strvec_pushf(&cmd.args, "--filter=%s",
780 expand_list_objects_filter_spec(&filter_options));
782 if (option_single_branch >= 0)
783 strvec_push(&cmd.args, option_single_branch ?
784 "--single-branch" :
785 "--no-single-branch");
787 cmd.git_cmd = 1;
788 err = run_command(&cmd);
791 return err;
794 static int git_clone_config(const char *k, const char *v, void *cb)
796 if (!strcmp(k, "clone.defaultremotename")) {
797 free(remote_name);
798 remote_name = xstrdup(v);
800 if (!strcmp(k, "clone.rejectshallow"))
801 config_reject_shallow = git_config_bool(k, v);
802 if (!strcmp(k, "clone.filtersubmodules"))
803 config_filter_submodules = git_config_bool(k, v);
805 return git_default_config(k, v, cb);
808 static int write_one_config(const char *key, const char *value, void *data)
811 * give git_clone_config a chance to write config values back to the
812 * environment, since git_config_set_multivar_gently only deals with
813 * config-file writes
815 int apply_failed = git_clone_config(key, value, data);
816 if (apply_failed)
817 return apply_failed;
819 return git_config_set_multivar_gently(key,
820 value ? value : "true",
821 CONFIG_REGEX_NONE, 0);
824 static void write_config(struct string_list *config)
826 int i;
828 for (i = 0; i < config->nr; i++) {
829 if (git_config_parse_parameter(config->items[i].string,
830 write_one_config, NULL) < 0)
831 die(_("unable to write parameters to config file"));
835 static void write_refspec_config(const char *src_ref_prefix,
836 const struct ref *our_head_points_at,
837 const struct ref *remote_head_points_at,
838 struct strbuf *branch_top)
840 struct strbuf key = STRBUF_INIT;
841 struct strbuf value = STRBUF_INIT;
843 if (option_mirror || !option_bare) {
844 if (option_single_branch && !option_mirror) {
845 if (option_branch) {
846 if (starts_with(our_head_points_at->name, "refs/tags/"))
847 strbuf_addf(&value, "+%s:%s", our_head_points_at->name,
848 our_head_points_at->name);
849 else
850 strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
851 branch_top->buf, option_branch);
852 } else if (remote_head_points_at) {
853 const char *head = remote_head_points_at->name;
854 if (!skip_prefix(head, "refs/heads/", &head))
855 BUG("remote HEAD points at non-head?");
857 strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name,
858 branch_top->buf, head);
861 * otherwise, the next "git fetch" will
862 * simply fetch from HEAD without updating
863 * any remote-tracking branch, which is what
864 * we want.
866 } else {
867 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf);
869 /* Configure the remote */
870 if (value.len) {
871 strbuf_addf(&key, "remote.%s.fetch", remote_name);
872 git_config_set_multivar(key.buf, value.buf, "^$", 0);
873 strbuf_reset(&key);
875 if (option_mirror) {
876 strbuf_addf(&key, "remote.%s.mirror", remote_name);
877 git_config_set(key.buf, "true");
878 strbuf_reset(&key);
883 strbuf_release(&key);
884 strbuf_release(&value);
887 static void dissociate_from_references(void)
889 char *alternates = git_pathdup("objects/info/alternates");
891 if (!access(alternates, F_OK)) {
892 struct child_process cmd = CHILD_PROCESS_INIT;
894 cmd.git_cmd = 1;
895 cmd.no_stdin = 1;
896 strvec_pushl(&cmd.args, "repack", "-a", "-d", NULL);
897 if (run_command(&cmd))
898 die(_("cannot repack to clean up"));
899 if (unlink(alternates) && errno != ENOENT)
900 die_errno(_("cannot unlink temporary alternates file"));
902 free(alternates);
905 static int path_exists(const char *path)
907 struct stat sb;
908 return !stat(path, &sb);
911 int cmd_clone(int argc, const char **argv, const char *prefix)
913 int is_bundle = 0, is_local;
914 int reject_shallow = 0;
915 const char *repo_name, *repo, *work_tree, *git_dir;
916 char *repo_to_free = NULL;
917 char *path = NULL, *dir, *display_repo = NULL;
918 int dest_exists, real_dest_exists = 0;
919 const struct ref *refs, *remote_head;
920 struct ref *remote_head_points_at = NULL;
921 const struct ref *our_head_points_at;
922 char *unborn_head = NULL;
923 struct ref *mapped_refs = NULL;
924 const struct ref *ref;
925 struct strbuf key = STRBUF_INIT;
926 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
927 struct transport *transport = NULL;
928 const char *src_ref_prefix = "refs/heads/";
929 struct remote *remote;
930 int err = 0, complete_refs_before_fetch = 1;
931 int submodule_progress;
932 int filter_submodules = 0;
933 int hash_algo;
934 const int do_not_override_repo_unix_permissions = -1;
936 struct transport_ls_refs_options transport_ls_refs_options =
937 TRANSPORT_LS_REFS_OPTIONS_INIT;
939 packet_trace_identity("clone");
941 git_config(git_clone_config, NULL);
943 argc = parse_options(argc, argv, prefix, builtin_clone_options,
944 builtin_clone_usage, 0);
946 if (argc > 2)
947 usage_msg_opt(_("Too many arguments."),
948 builtin_clone_usage, builtin_clone_options);
950 if (argc == 0)
951 usage_msg_opt(_("You must specify a repository to clone."),
952 builtin_clone_usage, builtin_clone_options);
954 if (option_depth || option_since || option_not.nr)
955 deepen = 1;
956 if (option_single_branch == -1)
957 option_single_branch = deepen ? 1 : 0;
959 if (option_mirror)
960 option_bare = 1;
962 if (option_bare) {
963 if (real_git_dir)
964 die(_("options '%s' and '%s' cannot be used together"), "--bare", "--separate-git-dir");
965 option_no_checkout = 1;
968 if (bundle_uri && deepen)
969 die(_("--bundle-uri is incompatible with --depth, --shallow-since, and --shallow-exclude"));
971 repo_name = argv[0];
973 path = get_repo_path(repo_name, &is_bundle);
974 if (path) {
975 FREE_AND_NULL(path);
976 repo = repo_to_free = absolute_pathdup(repo_name);
977 } else if (strchr(repo_name, ':')) {
978 repo = repo_name;
979 display_repo = transport_anonymize_url(repo);
980 } else
981 die(_("repository '%s' does not exist"), repo_name);
983 /* no need to be strict, transport_set_option() will validate it again */
984 if (option_depth && atoi(option_depth) < 1)
985 die(_("depth %s is not a positive number"), option_depth);
987 if (argc == 2)
988 dir = xstrdup(argv[1]);
989 else
990 dir = git_url_basename(repo_name, is_bundle, option_bare);
991 strip_dir_trailing_slashes(dir);
993 dest_exists = path_exists(dir);
994 if (dest_exists && !is_empty_dir(dir))
995 die(_("destination path '%s' already exists and is not "
996 "an empty directory."), dir);
998 if (real_git_dir) {
999 real_dest_exists = path_exists(real_git_dir);
1000 if (real_dest_exists && !is_empty_dir(real_git_dir))
1001 die(_("repository path '%s' already exists and is not "
1002 "an empty directory."), real_git_dir);
1006 strbuf_addf(&reflog_msg, "clone: from %s",
1007 display_repo ? display_repo : repo);
1008 free(display_repo);
1010 if (option_bare)
1011 work_tree = NULL;
1012 else {
1013 work_tree = getenv("GIT_WORK_TREE");
1014 if (work_tree && path_exists(work_tree))
1015 die(_("working tree '%s' already exists."), work_tree);
1018 if (option_bare || work_tree)
1019 git_dir = xstrdup(dir);
1020 else {
1021 work_tree = dir;
1022 git_dir = mkpathdup("%s/.git", dir);
1025 atexit(remove_junk);
1026 sigchain_push_common(remove_junk_on_signal);
1028 if (!option_bare) {
1029 if (safe_create_leading_directories_const(work_tree) < 0)
1030 die_errno(_("could not create leading directories of '%s'"),
1031 work_tree);
1032 if (dest_exists)
1033 junk_work_tree_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
1034 else if (mkdir(work_tree, 0777))
1035 die_errno(_("could not create work tree dir '%s'"),
1036 work_tree);
1037 junk_work_tree = work_tree;
1038 set_git_work_tree(work_tree);
1041 if (real_git_dir) {
1042 if (real_dest_exists)
1043 junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
1044 junk_git_dir = real_git_dir;
1045 } else {
1046 if (dest_exists)
1047 junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
1048 junk_git_dir = git_dir;
1050 if (safe_create_leading_directories_const(git_dir) < 0)
1051 die(_("could not create leading directories of '%s'"), git_dir);
1053 if (0 <= option_verbosity) {
1054 if (option_bare)
1055 fprintf(stderr, _("Cloning into bare repository '%s'...\n"), dir);
1056 else
1057 fprintf(stderr, _("Cloning into '%s'...\n"), dir);
1060 if (option_recurse_submodules.nr > 0) {
1061 struct string_list_item *item;
1062 struct strbuf sb = STRBUF_INIT;
1063 int val;
1065 /* remove duplicates */
1066 string_list_sort(&option_recurse_submodules);
1067 string_list_remove_duplicates(&option_recurse_submodules, 0);
1070 * NEEDSWORK: In a multi-working-tree world, this needs to be
1071 * set in the per-worktree config.
1073 for_each_string_list_item(item, &option_recurse_submodules) {
1074 strbuf_addf(&sb, "submodule.active=%s",
1075 item->string);
1076 string_list_append(&option_config,
1077 strbuf_detach(&sb, NULL));
1080 if (!git_config_get_bool("submodule.stickyRecursiveClone", &val) &&
1081 val)
1082 string_list_append(&option_config, "submodule.recurse=true");
1084 if (option_required_reference.nr &&
1085 option_optional_reference.nr)
1086 die(_("clone --recursive is not compatible with "
1087 "both --reference and --reference-if-able"));
1088 else if (option_required_reference.nr) {
1089 string_list_append(&option_config,
1090 "submodule.alternateLocation=superproject");
1091 string_list_append(&option_config,
1092 "submodule.alternateErrorStrategy=die");
1093 } else if (option_optional_reference.nr) {
1094 string_list_append(&option_config,
1095 "submodule.alternateLocation=superproject");
1096 string_list_append(&option_config,
1097 "submodule.alternateErrorStrategy=info");
1101 init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, NULL,
1102 do_not_override_repo_unix_permissions, INIT_DB_QUIET);
1104 if (real_git_dir) {
1105 free((char *)git_dir);
1106 git_dir = real_git_dir;
1110 * additional config can be injected with -c, make sure it's included
1111 * after init_db, which clears the entire config environment.
1113 write_config(&option_config);
1116 * re-read config after init_db and write_config to pick up any config
1117 * injected by --template and --config, respectively.
1119 git_config(git_clone_config, NULL);
1122 * If option_reject_shallow is specified from CLI option,
1123 * ignore config_reject_shallow from git_clone_config.
1125 if (config_reject_shallow != -1)
1126 reject_shallow = config_reject_shallow;
1127 if (option_reject_shallow != -1)
1128 reject_shallow = option_reject_shallow;
1131 * If option_filter_submodules is specified from CLI option,
1132 * ignore config_filter_submodules from git_clone_config.
1134 if (config_filter_submodules != -1)
1135 filter_submodules = config_filter_submodules;
1136 if (option_filter_submodules != -1)
1137 filter_submodules = option_filter_submodules;
1140 * Exit if the user seems to be doing something silly with submodule
1141 * filter flags (but not with filter configs, as those should be
1142 * set-and-forget).
1144 if (option_filter_submodules > 0 && !filter_options.choice)
1145 die(_("the option '%s' requires '%s'"),
1146 "--also-filter-submodules", "--filter");
1147 if (option_filter_submodules > 0 && !option_recurse_submodules.nr)
1148 die(_("the option '%s' requires '%s'"),
1149 "--also-filter-submodules", "--recurse-submodules");
1152 * apply the remote name provided by --origin only after this second
1153 * call to git_config, to ensure it overrides all config-based values.
1155 if (option_origin) {
1156 free(remote_name);
1157 remote_name = xstrdup(option_origin);
1160 if (!remote_name)
1161 remote_name = xstrdup("origin");
1163 if (!valid_remote_name(remote_name))
1164 die(_("'%s' is not a valid remote name"), remote_name);
1166 if (option_bare) {
1167 if (option_mirror)
1168 src_ref_prefix = "refs/";
1169 strbuf_addstr(&branch_top, src_ref_prefix);
1171 git_config_set("core.bare", "true");
1172 } else {
1173 strbuf_addf(&branch_top, "refs/remotes/%s/", remote_name);
1176 strbuf_addf(&key, "remote.%s.url", remote_name);
1177 git_config_set(key.buf, repo);
1178 strbuf_reset(&key);
1180 if (option_no_tags) {
1181 strbuf_addf(&key, "remote.%s.tagOpt", remote_name);
1182 git_config_set(key.buf, "--no-tags");
1183 strbuf_reset(&key);
1186 if (option_required_reference.nr || option_optional_reference.nr)
1187 setup_reference();
1189 if (option_sparse_checkout && git_sparse_checkout_init(dir))
1190 return 1;
1192 remote = remote_get(remote_name);
1194 refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix,
1195 branch_top.buf);
1197 path = get_repo_path(remote->url[0], &is_bundle);
1198 is_local = option_local != 0 && path && !is_bundle;
1199 if (is_local) {
1200 if (option_depth)
1201 warning(_("--depth is ignored in local clones; use file:// instead."));
1202 if (option_since)
1203 warning(_("--shallow-since is ignored in local clones; use file:// instead."));
1204 if (option_not.nr)
1205 warning(_("--shallow-exclude is ignored in local clones; use file:// instead."));
1206 if (filter_options.choice)
1207 warning(_("--filter is ignored in local clones; use file:// instead."));
1208 if (!access(mkpath("%s/shallow", path), F_OK)) {
1209 if (reject_shallow)
1210 die(_("source repository is shallow, reject to clone."));
1211 if (option_local > 0)
1212 warning(_("source repository is shallow, ignoring --local"));
1213 is_local = 0;
1216 if (option_local > 0 && !is_local)
1217 warning(_("--local is ignored"));
1219 transport = transport_get(remote, path ? path : remote->url[0]);
1220 transport_set_verbosity(transport, option_verbosity, option_progress);
1221 transport->family = family;
1222 transport->cloning = 1;
1224 if (is_bundle) {
1225 struct bundle_header header = BUNDLE_HEADER_INIT;
1226 int fd = read_bundle_header(path, &header);
1227 int has_filter = header.filter.choice != LOFC_DISABLED;
1229 if (fd > 0)
1230 close(fd);
1231 bundle_header_release(&header);
1232 if (has_filter)
1233 die(_("cannot clone from filtered bundle"));
1236 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
1238 if (reject_shallow)
1239 transport_set_option(transport, TRANS_OPT_REJECT_SHALLOW, "1");
1240 if (option_depth)
1241 transport_set_option(transport, TRANS_OPT_DEPTH,
1242 option_depth);
1243 if (option_since)
1244 transport_set_option(transport, TRANS_OPT_DEEPEN_SINCE,
1245 option_since);
1246 if (option_not.nr)
1247 transport_set_option(transport, TRANS_OPT_DEEPEN_NOT,
1248 (const char *)&option_not);
1249 if (option_single_branch)
1250 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
1252 if (option_upload_pack)
1253 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
1254 option_upload_pack);
1256 if (server_options.nr)
1257 transport->server_options = &server_options;
1259 if (filter_options.choice) {
1260 const char *spec =
1261 expand_list_objects_filter_spec(&filter_options);
1262 transport_set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER,
1263 spec);
1264 transport_set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1267 if (transport->smart_options && !deepen && !filter_options.choice)
1268 transport->smart_options->check_self_contained_and_connected = 1;
1271 * Before fetching from the remote, download and install bundle
1272 * data from the --bundle-uri option.
1274 if (bundle_uri) {
1275 int has_heuristic = 0;
1277 /* At this point, we need the_repository to match the cloned repo. */
1278 if (repo_init(the_repository, git_dir, work_tree))
1279 warning(_("failed to initialize the repo, skipping bundle URI"));
1280 else if (fetch_bundle_uri(the_repository, bundle_uri, &has_heuristic))
1281 warning(_("failed to fetch objects from bundle URI '%s'"),
1282 bundle_uri);
1283 else if (has_heuristic)
1284 git_config_set_gently("fetch.bundleuri", bundle_uri);
1287 strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
1288 refspec_ref_prefixes(&remote->fetch,
1289 &transport_ls_refs_options.ref_prefixes);
1290 if (option_branch)
1291 expand_ref_prefix(&transport_ls_refs_options.ref_prefixes,
1292 option_branch);
1293 if (!option_no_tags)
1294 strvec_push(&transport_ls_refs_options.ref_prefixes,
1295 "refs/tags/");
1297 refs = transport_get_remote_refs(transport, &transport_ls_refs_options);
1299 if (refs)
1300 mapped_refs = wanted_peer_refs(refs, &remote->fetch);
1302 if (!bundle_uri) {
1304 * Populate transport->got_remote_bundle_uri and
1305 * transport->bundle_uri. We might get nothing.
1307 transport_get_remote_bundle_uri(transport);
1309 if (transport->bundles &&
1310 hashmap_get_size(&transport->bundles->bundles)) {
1311 /* At this point, we need the_repository to match the cloned repo. */
1312 if (repo_init(the_repository, git_dir, work_tree))
1313 warning(_("failed to initialize the repo, skipping bundle URI"));
1314 else if (fetch_bundle_list(the_repository,
1315 transport->bundles))
1316 warning(_("failed to fetch advertised bundles"));
1317 } else {
1318 clear_bundle_list(transport->bundles);
1319 FREE_AND_NULL(transport->bundles);
1324 * Now that we know what algorithm the remote side is using,
1325 * let's set ours to the same thing.
1327 hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
1328 initialize_repository_version(hash_algo, 1);
1329 repo_set_hash_algo(the_repository, hash_algo);
1331 if (mapped_refs) {
1333 * transport_get_remote_refs() may return refs with null sha-1
1334 * in mapped_refs (see struct transport->get_refs_list
1335 * comment). In that case we need fetch it early because
1336 * remote_head code below relies on it.
1338 * for normal clones, transport_get_remote_refs() should
1339 * return reliable ref set, we can delay cloning until after
1340 * remote HEAD check.
1342 for (ref = refs; ref; ref = ref->next)
1343 if (is_null_oid(&ref->old_oid)) {
1344 complete_refs_before_fetch = 0;
1345 break;
1348 if (!is_local && !complete_refs_before_fetch) {
1349 if (transport_fetch_refs(transport, mapped_refs))
1350 die(_("remote transport reported error"));
1354 remote_head = find_ref_by_name(refs, "HEAD");
1355 remote_head_points_at = guess_remote_head(remote_head, mapped_refs, 0);
1357 if (option_branch) {
1358 our_head_points_at = find_remote_branch(mapped_refs, option_branch);
1359 if (!our_head_points_at)
1360 die(_("Remote branch %s not found in upstream %s"),
1361 option_branch, remote_name);
1362 } else if (remote_head_points_at) {
1363 our_head_points_at = remote_head_points_at;
1364 } else if (remote_head) {
1365 our_head_points_at = NULL;
1366 } else {
1367 const char *branch;
1369 if (!mapped_refs) {
1370 warning(_("You appear to have cloned an empty repository."));
1371 option_no_checkout = 1;
1374 if (transport_ls_refs_options.unborn_head_target &&
1375 skip_prefix(transport_ls_refs_options.unborn_head_target,
1376 "refs/heads/", &branch)) {
1377 unborn_head = xstrdup(transport_ls_refs_options.unborn_head_target);
1378 } else {
1379 branch = git_default_branch_name(0);
1380 unborn_head = xstrfmt("refs/heads/%s", branch);
1384 * We may have selected a local default branch name "foo",
1385 * and even though the remote's HEAD does not point there,
1386 * it may still have a "foo" branch. If so, set it up so
1387 * that we can follow the usual checkout code later.
1389 * Note that for an empty repo we'll already have set
1390 * option_no_checkout above, which would work against us here.
1391 * But for an empty repo, find_remote_branch() can never find
1392 * a match.
1394 our_head_points_at = find_remote_branch(mapped_refs, branch);
1397 write_refspec_config(src_ref_prefix, our_head_points_at,
1398 remote_head_points_at, &branch_top);
1400 if (filter_options.choice)
1401 partial_clone_register(remote_name, &filter_options);
1403 if (is_local)
1404 clone_local(path, git_dir);
1405 else if (mapped_refs && complete_refs_before_fetch) {
1406 if (transport_fetch_refs(transport, mapped_refs))
1407 die(_("remote transport reported error"));
1410 update_remote_refs(refs, mapped_refs, remote_head_points_at,
1411 branch_top.buf, reflog_msg.buf, transport,
1412 !is_local);
1414 update_head(our_head_points_at, remote_head, unborn_head, reflog_msg.buf);
1417 * We want to show progress for recursive submodule clones iff
1418 * we did so for the main clone. But only the transport knows
1419 * the final decision for this flag, so we need to rescue the value
1420 * before we free the transport.
1422 submodule_progress = transport->progress;
1424 transport_unlock_pack(transport, 0);
1425 transport_disconnect(transport);
1427 if (option_dissociate) {
1428 close_object_store(the_repository->objects);
1429 dissociate_from_references();
1432 junk_mode = JUNK_LEAVE_REPO;
1433 err = checkout(submodule_progress, filter_submodules);
1435 free(remote_name);
1436 strbuf_release(&reflog_msg);
1437 strbuf_release(&branch_top);
1438 strbuf_release(&key);
1439 free_refs(mapped_refs);
1440 free_refs(remote_head_points_at);
1441 free(unborn_head);
1442 free(dir);
1443 free(path);
1444 free(repo_to_free);
1445 junk_mode = JUNK_LEAVE_ALL;
1447 transport_ls_refs_options_release(&transport_ls_refs_options);
1448 return err;