]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/clone.c
Git 2.35.1
[thirdparty/git.git] / builtin / clone.c
CommitLineData
8434c2f1
DB
1/*
2 * Builtin "git clone"
3 *
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
7 *
8 * Clone a repository into a different directory that does not yet exist.
9 */
10
f8adbec9 11#define USE_THE_INDEX_COMPATIBILITY_MACROS
c2e86add 12#include "builtin.h"
b2141fc1 13#include "config.h"
697cc8ef 14#include "lockfile.h"
8434c2f1
DB
15#include "parse-options.h"
16#include "fetch-pack.h"
17#include "refs.h"
ec0cb496 18#include "refspec.h"
cbd53a21 19#include "object-store.h"
8434c2f1
DB
20#include "tree.h"
21#include "tree-walk.h"
22#include "unpack-trees.h"
23#include "transport.h"
24#include "strbuf.h"
25#include "dir.h"
ff7ccc8c
MT
26#include "dir-iterator.h"
27#include "iterator.h"
4a16d072 28#include "sigchain.h"
a9f2c136 29#include "branch.h"
8ef51733 30#include "remote.h"
dfa7a6c5 31#include "run-command.h"
0433ad12 32#include "connected.h"
3836d88a 33#include "packfile.h"
548719fb 34#include "list-objects-filter-options.h"
8434c2f1
DB
35
36/*
37 * Overall FIXMEs:
38 * - respect DB_ENVIRONMENT for .git/objects.
39 *
40 * Implementation notes:
41 * - dropping use-separate-remote and no-separate-remote compatibility
42 *
43 */
44static const char * const builtin_clone_usage[] = {
9c9b4f2f 45 N_("git clone [<options>] [--] <repo> [<dir>]"),
8434c2f1
DB
46 NULL
47};
48
3e6e0edd 49static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
bb62e0a9 50static int option_local = -1, option_no_hardlinks, option_shared;
0dab2468 51static int option_no_tags;
18a74a09 52static int option_shallow_submodules;
4fe788b1
LL
53static int option_reject_shallow = -1; /* unspecified */
54static int config_reject_shallow = -1; /* unspecified */
994c2aaf
NTND
55static int deepen;
56static char *option_template, *option_depth, *option_since;
8434c2f1 57static char *option_origin = NULL;
de9ed3ef 58static char *remote_name = NULL;
7a4ee28f 59static char *option_branch = NULL;
859e5df9 60static struct string_list option_not = STRING_LIST_INIT_NODUP;
b57fb80a 61static const char *real_git_dir;
8434c2f1 62static char *option_upload_pack = "git-upload-pack";
5bd631b3 63static int option_verbosity;
01fdc21f 64static int option_progress = -1;
d89f09c8 65static int option_sparse_checkout;
c915f11e 66static enum transport_family family;
2721ce21 67static struct string_list option_config = STRING_LIST_INIT_NODUP;
5e40800d 68static struct string_list option_required_reference = STRING_LIST_INIT_NODUP;
f7415b4d 69static struct string_list option_optional_reference = STRING_LIST_INIT_NODUP;
fb1d6dab 70static int option_dissociate;
72290d6a 71static int max_jobs = -1;
bb62e0a9 72static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP;
548719fb 73static struct list_objects_filter_options filter_options;
6e983059 74static struct string_list server_options = STRING_LIST_INIT_NODUP;
4c691016 75static int option_remote_submodules;
bb62e0a9
BW
76
77static int recurse_submodules_cb(const struct option *opt,
78 const char *arg, int unset)
79{
80 if (unset)
81 string_list_clear((struct string_list *)opt->value, 0);
82 else if (arg)
83 string_list_append((struct string_list *)opt->value, arg);
84 else
85 string_list_append((struct string_list *)opt->value,
86 (const char *)opt->defval);
87
88 return 0;
89}
dbc92b07 90
8434c2f1 91static struct option builtin_clone_options[] = {
5bd631b3 92 OPT__VERBOSITY(&option_verbosity),
01fdc21f 93 OPT_BOOL(0, "progress", &option_progress,
32b77add 94 N_("force progress reporting")),
4fe788b1
LL
95 OPT_BOOL(0, "reject-shallow", &option_reject_shallow,
96 N_("don't clone shallow repository")),
d5d09d47
SB
97 OPT_BOOL('n', "no-checkout", &option_no_checkout,
98 N_("don't create a checkout")),
4741edd5
SB
99 OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
100 OPT_HIDDEN_BOOL(0, "naked", &option_bare,
101 N_("create a bare repository")),
d5d09d47
SB
102 OPT_BOOL(0, "mirror", &option_mirror,
103 N_("create a mirror repository (implies bare)")),
189260b1 104 OPT_BOOL('l', "local", &option_local,
32b77add 105 N_("to clone from a local repository")),
d5d09d47 106 OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
32b77add 107 N_("don't use local hardlinks, always copy")),
d5d09d47 108 OPT_BOOL('s', "shared", &option_shared,
32b77add 109 N_("setup as shared repository")),
bb62e0a9
BW
110 { OPTION_CALLBACK, 0, "recurse-submodules", &option_recurse_submodules,
111 N_("pathspec"), N_("initialize submodules in the clone"),
112 PARSE_OPT_OPTARG, recurse_submodules_cb, (intptr_t)"." },
c28b036f 113 OPT_ALIAS(0, "recursive", "recurse-submodules"),
72290d6a
SB
114 OPT_INTEGER('j', "jobs", &max_jobs,
115 N_("number of submodules cloned in parallel")),
32b77add
NTND
116 OPT_STRING(0, "template", &option_template, N_("template-directory"),
117 N_("directory from which templates will be used")),
5e40800d 118 OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"),
8ade009c 119 N_("reference repository")),
f7415b4d
SB
120 OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference,
121 N_("repo"), N_("reference repository")),
14f8b9b4
JK
122 OPT_BOOL(0, "dissociate", &option_dissociate,
123 N_("use --reference only while cloning")),
32b77add
NTND
124 OPT_STRING('o', "origin", &option_origin, N_("name"),
125 N_("use <name> instead of 'origin' to track upstream")),
126 OPT_STRING('b', "branch", &option_branch, N_("branch"),
127 N_("checkout <branch> instead of the remote's HEAD")),
128 OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
129 N_("path to git-upload-pack on the remote")),
130 OPT_STRING(0, "depth", &option_depth, N_("depth"),
131 N_("create a shallow clone of that depth")),
994c2aaf
NTND
132 OPT_STRING(0, "shallow-since", &option_since, N_("time"),
133 N_("create a shallow clone since a specific time")),
859e5df9 134 OPT_STRING_LIST(0, "shallow-exclude", &option_not, N_("revision"),
6d873865 135 N_("deepen history of shallow clone, excluding rev")),
3e6e0edd 136 OPT_BOOL(0, "single-branch", &option_single_branch,
32b77add 137 N_("clone only one branch, HEAD or --branch")),
0dab2468
ÆAB
138 OPT_BOOL(0, "no-tags", &option_no_tags,
139 N_("don't clone any tags, and make later fetches not to follow them")),
d22eb044
SB
140 OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules,
141 N_("any cloned submodules will be shallow")),
32b77add
NTND
142 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
143 N_("separate git dir from working tree")),
144 OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
145 N_("set config inside the new repository")),
6e983059
JT
146 OPT_STRING_LIST(0, "server-option", &server_options,
147 N_("server-specific"), N_("option to transmit")),
c915f11e
EW
148 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
149 TRANSPORT_FAMILY_IPV4),
150 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
151 TRANSPORT_FAMILY_IPV6),
548719fb 152 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
4c691016
BA
153 OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
154 N_("any cloned submodules will use their remote-tracking branch")),
d89f09c8
DS
155 OPT_BOOL(0, "sparse", &option_sparse_checkout,
156 N_("initialize sparse-checkout file to include only files at root")),
8434c2f1
DB
157 OPT_END()
158};
159
0ea68e42 160static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
8434c2f1 161{
b3256eb8 162 static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
8434c2f1 163 static char *bundle_suffix[] = { ".bundle", "" };
0ea68e42 164 size_t baselen = path->len;
8434c2f1
DB
165 struct stat st;
166 int i;
167
168 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
0ea68e42
JK
169 strbuf_setlen(path, baselen);
170 strbuf_addstr(path, suffix[i]);
171 if (stat(path->buf, &st))
9b0ebc72 172 continue;
0ea68e42 173 if (S_ISDIR(st.st_mode) && is_git_directory(path->buf)) {
8434c2f1 174 *is_bundle = 0;
0ea68e42 175 return path->buf;
9b0ebc72
NTND
176 } else if (S_ISREG(st.st_mode) && st.st_size > 8) {
177 /* Is it a "gitfile"? */
178 char signature[8];
0ea68e42
JK
179 const char *dst;
180 int len, fd = open(path->buf, O_RDONLY);
9b0ebc72
NTND
181 if (fd < 0)
182 continue;
183 len = read_in_full(fd, signature, 8);
184 close(fd);
185 if (len != 8 || strncmp(signature, "gitdir: ", 8))
186 continue;
0ea68e42
JK
187 dst = read_gitfile(path->buf);
188 if (dst) {
9b0ebc72 189 *is_bundle = 0;
0ea68e42 190 return dst;
9b0ebc72 191 }
8434c2f1
DB
192 }
193 }
194
195 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
0ea68e42
JK
196 strbuf_setlen(path, baselen);
197 strbuf_addstr(path, bundle_suffix[i]);
198 if (!stat(path->buf, &st) && S_ISREG(st.st_mode)) {
8434c2f1 199 *is_bundle = 1;
0ea68e42 200 return path->buf;
8434c2f1
DB
201 }
202 }
203
204 return NULL;
205}
206
0ea68e42
JK
207static char *get_repo_path(const char *repo, int *is_bundle)
208{
209 struct strbuf path = STRBUF_INIT;
210 const char *raw;
211 char *canon;
212
213 strbuf_addstr(&path, repo);
214 raw = get_repo_path_1(&path, is_bundle);
0aaad415 215 canon = raw ? absolute_pathdup(raw) : NULL;
0ea68e42
JK
216 strbuf_release(&path);
217 return canon;
218}
219
dbc92b07 220static int add_one_reference(struct string_list_item *item, void *cb_data)
8434c2f1 221{
9eeea7d2 222 struct strbuf err = STRBUF_INIT;
f7415b4d 223 int *required = cb_data;
9eeea7d2 224 char *ref_git = compute_alternate_path(item->string, &err);
8434c2f1 225
f7415b4d
SB
226 if (!ref_git) {
227 if (*required)
228 die("%s", err.buf);
229 else
230 fprintf(stderr,
231 _("info: Could not add alternate for '%s': %s\n"),
232 item->string, err.buf);
233 } else {
234 struct strbuf sb = STRBUF_INIT;
235 strbuf_addf(&sb, "%s/objects", ref_git);
236 add_to_alternates_file(sb.buf);
237 strbuf_release(&sb);
238 }
606e435a 239
9eeea7d2 240 strbuf_release(&err);
f7415b4d 241 free(ref_git);
dbc92b07
JH
242 return 0;
243}
8434c2f1 244
dbc92b07
JH
245static void setup_reference(void)
246{
f7415b4d
SB
247 int required = 1;
248 for_each_string_list(&option_required_reference,
249 add_one_reference, &required);
250 required = 0;
251 for_each_string_list(&option_optional_reference,
252 add_one_reference, &required);
8434c2f1
DB
253}
254
3c1dce88 255static void copy_alternates(struct strbuf *src, const char *src_repo)
e6baf4a1
JH
256{
257 /*
258 * Read from the source objects/info/alternates file
259 * and copy the entries to corresponding file in the
260 * destination repository with add_to_alternates_file().
261 * Both src and dst have "$path/objects/info/alternates".
262 *
263 * Instead of copying bit-for-bit from the original,
264 * we need to append to existing one so that the already
265 * created entry via "clone -s" is not lost, and also
266 * to turn entries with paths relative to the original
267 * absolute, so that they can be used in the new repository.
268 */
02912f47 269 FILE *in = xfopen(src->buf, "r");
e6baf4a1
JH
270 struct strbuf line = STRBUF_INIT;
271
3f163962 272 while (strbuf_getline(&line, in) != EOF) {
dcf69262 273 char *abs_path;
e6baf4a1
JH
274 if (!line.len || line.buf[0] == '#')
275 continue;
276 if (is_absolute_path(line.buf)) {
277 add_to_alternates_file(line.buf);
278 continue;
279 }
dcf69262 280 abs_path = mkpathdup("%s/objects/%s", src_repo, line.buf);
22d3b8de
JK
281 if (!normalize_path_copy(abs_path, abs_path))
282 add_to_alternates_file(abs_path);
283 else
284 warning("skipping invalid relative alternate: %s/%s",
285 src_repo, line.buf);
dcf69262 286 free(abs_path);
e6baf4a1
JH
287 }
288 strbuf_release(&line);
289 fclose(in);
290}
291
14954b79
MT
292static void mkdir_if_missing(const char *pathname, mode_t mode)
293{
294 struct stat st;
295
296 if (!mkdir(pathname, mode))
297 return;
298
299 if (errno != EEXIST)
300 die_errno(_("failed to create directory '%s'"), pathname);
301 else if (stat(pathname, &st))
302 die_errno(_("failed to stat '%s'"), pathname);
303 else if (!S_ISDIR(st.st_mode))
304 die(_("%s exists and is not a directory"), pathname);
305}
306
e6baf4a1 307static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
ff7ccc8c 308 const char *src_repo)
8434c2f1 309{
8434c2f1 310 int src_len, dest_len;
ff7ccc8c
MT
311 struct dir_iterator *iter;
312 int iter_status;
313 unsigned int flags;
3d7747e3 314 struct strbuf realpath = STRBUF_INIT;
8434c2f1 315
14954b79 316 mkdir_if_missing(dest->buf, 0777);
8434c2f1 317
ff7ccc8c
MT
318 flags = DIR_ITERATOR_PEDANTIC | DIR_ITERATOR_FOLLOW_SYMLINKS;
319 iter = dir_iterator_begin(src->buf, flags);
320
321 if (!iter)
322 die_errno(_("failed to start iterator over '%s'"), src->buf);
8434c2f1 323
b9e125e0
MV
324 strbuf_addch(src, '/');
325 src_len = src->len;
326 strbuf_addch(dest, '/');
327 dest_len = dest->len;
8434c2f1 328
ff7ccc8c 329 while ((iter_status = dir_iterator_advance(iter)) == ITER_OK) {
b9e125e0 330 strbuf_setlen(src, src_len);
ff7ccc8c 331 strbuf_addstr(src, iter->relative_path);
b9e125e0 332 strbuf_setlen(dest, dest_len);
ff7ccc8c
MT
333 strbuf_addstr(dest, iter->relative_path);
334
335 if (S_ISDIR(iter->st.st_mode)) {
336 mkdir_if_missing(dest->buf, 0777);
e6baf4a1
JH
337 continue;
338 }
339
340 /* Files that cannot be copied bit-for-bit... */
c4d9c506 341 if (!fspathcmp(iter->relative_path, "info/alternates")) {
3c1dce88 342 copy_alternates(src, src_repo);
8434c2f1
DB
343 continue;
344 }
345
b9e125e0 346 if (unlink(dest->buf) && errno != ENOENT)
e84d7b74 347 die_errno(_("failed to unlink '%s'"), dest->buf);
fdabc242 348 if (!option_no_hardlinks) {
3d7747e3
AM
349 strbuf_realpath(&realpath, src->buf, 1);
350 if (!link(realpath.buf, dest->buf))
fdabc242 351 continue;
189260b1 352 if (option_local > 0)
e84d7b74 353 die_errno(_("failed to create link '%s'"), dest->buf);
fdabc242 354 option_no_hardlinks = 1;
8434c2f1 355 }
f7835a25 356 if (copy_file_with_time(dest->buf, src->buf, 0666))
e84d7b74 357 die_errno(_("failed to copy file to '%s'"), dest->buf);
8434c2f1 358 }
ff7ccc8c
MT
359
360 if (iter_status != ITER_DONE) {
361 strbuf_setlen(src, src_len);
362 die(_("failed to iterate over '%s'"), src->buf);
363 }
3d7747e3
AM
364
365 strbuf_release(&realpath);
8434c2f1
DB
366}
367
6f48d39f 368static void clone_local(const char *src_repo, const char *dest_repo)
8434c2f1 369{
e6baf4a1
JH
370 if (option_shared) {
371 struct strbuf alt = STRBUF_INIT;
b3b05971
ES
372 get_common_dir(&alt, src_repo);
373 strbuf_addstr(&alt, "/objects");
e6baf4a1
JH
374 add_to_alternates_file(alt.buf);
375 strbuf_release(&alt);
376 } else {
377 struct strbuf src = STRBUF_INIT;
378 struct strbuf dest = STRBUF_INIT;
744e4697
NTND
379 get_common_dir(&src, src_repo);
380 get_common_dir(&dest, dest_repo);
381 strbuf_addstr(&src, "/objects");
382 strbuf_addstr(&dest, "/objects");
ff7ccc8c 383 copy_or_link_directory(&src, &dest, src_repo);
b9e125e0
MV
384 strbuf_release(&src);
385 strbuf_release(&dest);
8434c2f1
DB
386 }
387
28ba96ab 388 if (0 <= option_verbosity)
68b939b2 389 fprintf(stderr, _("done.\n"));
8434c2f1
DB
390}
391
392static const char *junk_work_tree;
d45420c1 393static int junk_work_tree_flags;
8434c2f1 394static const char *junk_git_dir;
d45420c1 395static int junk_git_dir_flags;
85064630 396static enum {
d3b34622
JK
397 JUNK_LEAVE_NONE,
398 JUNK_LEAVE_REPO,
399 JUNK_LEAVE_ALL
400} junk_mode = JUNK_LEAVE_NONE;
401
402static const char junk_leave_repo_msg[] =
403N_("Clone succeeded, but checkout failed.\n"
404 "You can inspect what was checked out with 'git status'\n"
80f537f7 405 "and retry with 'git restore --source=HEAD :/'\n");
8434c2f1
DB
406
407static void remove_junk(void)
408{
f285a2d7 409 struct strbuf sb = STRBUF_INIT;
d3b34622
JK
410
411 switch (junk_mode) {
412 case JUNK_LEAVE_REPO:
413 warning("%s", _(junk_leave_repo_msg));
414 /* fall-through */
415 case JUNK_LEAVE_ALL:
416 return;
417 default:
418 /* proceed to removal */
419 break;
420 }
421
8434c2f1
DB
422 if (junk_git_dir) {
423 strbuf_addstr(&sb, junk_git_dir);
d45420c1 424 remove_dir_recursively(&sb, junk_git_dir_flags);
8434c2f1
DB
425 strbuf_reset(&sb);
426 }
427 if (junk_work_tree) {
428 strbuf_addstr(&sb, junk_work_tree);
d45420c1 429 remove_dir_recursively(&sb, junk_work_tree_flags);
8434c2f1 430 }
9c18b548 431 strbuf_release(&sb);
8434c2f1
DB
432}
433
434static void remove_junk_on_signal(int signo)
435{
436 remove_junk();
4a16d072 437 sigchain_pop(signo);
8434c2f1
DB
438 raise(signo);
439}
440
9e585046
NTND
441static struct ref *find_remote_branch(const struct ref *refs, const char *branch)
442{
443 struct ref *ref;
444 struct strbuf head = STRBUF_INIT;
445 strbuf_addstr(&head, "refs/heads/");
446 strbuf_addstr(&head, branch);
447 ref = find_ref_by_name(refs, head.buf);
448 strbuf_release(&head);
5a7d5b68
NTND
449
450 if (ref)
451 return ref;
452
453 strbuf_addstr(&head, "refs/tags/");
454 strbuf_addstr(&head, branch);
455 ref = find_ref_by_name(refs, head.buf);
456 strbuf_release(&head);
457
9e585046
NTND
458 return ref;
459}
460
5bdc32d3 461static struct ref *wanted_peer_refs(const struct ref *refs,
515be833 462 struct refspec *refspec)
8434c2f1 463{
c1921c18
JK
464 struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD"));
465 struct ref *local_refs = head;
466 struct ref **tail = head ? &head->next : &local_refs;
8434c2f1 467
3e6e0edd
NTND
468 if (option_single_branch) {
469 struct ref *remote_head = NULL;
470
471 if (!option_branch)
472 remote_head = guess_remote_head(head, refs, 0);
0ec4b165
NTND
473 else {
474 local_refs = NULL;
475 tail = &local_refs;
476 remote_head = copy_ref(find_remote_branch(refs, option_branch));
477 }
3e6e0edd
NTND
478
479 if (!remote_head && option_branch)
480 warning(_("Could not find remote branch %s to clone."),
481 option_branch);
5a7d5b68 482 else {
515be833
SG
483 int i;
484 for (i = 0; i < refspec->nr; i++)
485 get_fetch_map(remote_head, &refspec->items[i],
486 &tail, 0);
5a7d5b68
NTND
487
488 /* if --branch=tag, pull the requested tag explicitly */
489 get_fetch_map(remote_head, tag_refspec, &tail, 0);
490 }
515be833
SG
491 } else {
492 int i;
493 for (i = 0; i < refspec->nr; i++)
494 get_fetch_map(refs, &refspec->items[i], &tail, 0);
495 }
3e6e0edd 496
0dab2468 497 if (!option_mirror && !option_single_branch && !option_no_tags)
468386a9 498 get_fetch_map(refs, tag_refspec, &tail, 0);
8434c2f1 499
5bdc32d3
NP
500 return local_refs;
501}
502
503static void write_remote_refs(const struct ref *local_refs)
504{
505 const struct ref *r;
506
58f233ce
MH
507 struct ref_transaction *t;
508 struct strbuf err = STRBUF_INIT;
509
510 t = ref_transaction_begin(&err);
511 if (!t)
512 die("%s", err.buf);
9f69d297 513
c1921c18
JK
514 for (r = local_refs; r; r = r->next) {
515 if (!r->peer_ref)
516 continue;
89f3bbdd 517 if (ref_transaction_create(t, r->peer_ref->name, &r->old_oid,
58f233ce
MH
518 0, NULL, &err))
519 die("%s", err.buf);
c1921c18 520 }
3e8aded2 521
58f233ce
MH
522 if (initial_ref_transaction_commit(t, &err))
523 die("%s", err.buf);
524
525 strbuf_release(&err);
526 ref_transaction_free(t);
8434c2f1
DB
527}
528
3e6e0edd
NTND
529static void write_followtags(const struct ref *refs, const char *msg)
530{
531 const struct ref *ref;
532 for (ref = refs; ref; ref = ref->next) {
59556548 533 if (!starts_with(ref->name, "refs/tags/"))
3e6e0edd 534 continue;
59556548 535 if (ends_with(ref->name, "^{}"))
3e6e0edd 536 continue;
167a575e
JK
537 if (!has_object_file_with_flags(&ref->old_oid,
538 OBJECT_INFO_QUICK |
539 OBJECT_INFO_SKIP_FETCH_OBJECT))
3e6e0edd 540 continue;
ae077771 541 update_ref(msg, ref->name, &ref->old_oid, NULL, 0,
542 UPDATE_REFS_DIE_ON_ERR);
3e6e0edd
NTND
543 }
544}
545
9fec7b21 546static const struct object_id *iterate_ref_map(void *cb_data)
0433ad12
JK
547{
548 struct ref **rm = cb_data;
549 struct ref *ref = *rm;
550
551 /*
552 * Skip anything missing a peer_ref, which we are not
553 * actually going to write a ref for.
554 */
555 while (ref && !ref->peer_ref)
556 ref = ref->next;
0433ad12 557 if (!ref)
9fec7b21 558 return NULL;
0433ad12 559
0433ad12 560 *rm = ref->next;
9fec7b21 561 return &ref->old_oid;
0433ad12
JK
562}
563
960b7d1c
NTND
564static void update_remote_refs(const struct ref *refs,
565 const struct ref *mapped_refs,
566 const struct ref *remote_head_points_at,
567 const char *branch_top,
c6807a40 568 const char *msg,
45ed4afa 569 struct transport *transport,
2b98478c 570 int check_connectivity)
960b7d1c 571{
0433ad12
JK
572 const struct ref *rm = mapped_refs;
573
125a05fd 574 if (check_connectivity) {
7043c707
JK
575 struct check_connected_options opt = CHECK_CONNECTED_INIT;
576
577 opt.transport = transport;
38e590ea 578 opt.progress = transport->progress;
7043c707 579
7043c707 580 if (check_connected(iterate_ref_map, &rm, &opt))
125a05fd
JK
581 die(_("remote did not send all necessary objects"));
582 }
0433ad12 583
960b7d1c 584 if (refs) {
960b7d1c 585 write_remote_refs(mapped_refs);
0dab2468 586 if (option_single_branch && !option_no_tags)
960b7d1c
NTND
587 write_followtags(refs, msg);
588 }
589
590 if (remote_head_points_at && !option_bare) {
591 struct strbuf head_ref = STRBUF_INIT;
592 strbuf_addstr(&head_ref, branch_top);
593 strbuf_addstr(&head_ref, "HEAD");
4be49d75
JK
594 if (create_symref(head_ref.buf,
595 remote_head_points_at->peer_ref->name,
596 msg) < 0)
39ad4f39 597 die(_("unable to update %s"), head_ref.buf);
4be49d75 598 strbuf_release(&head_ref);
960b7d1c
NTND
599 }
600}
601
f034d354
NTND
602static void update_head(const struct ref *our, const struct ref *remote,
603 const char *msg)
604{
cf4fff57
JK
605 const char *head;
606 if (our && skip_prefix(our->name, "refs/heads/", &head)) {
f034d354 607 /* Local default branch link */
4be49d75 608 if (create_symref("HEAD", our->name, NULL) < 0)
39ad4f39 609 die(_("unable to update HEAD"));
f034d354 610 if (!option_bare) {
ae077771 611 update_ref(msg, "HEAD", &our->old_oid, NULL, 0,
f4124112 612 UPDATE_REFS_DIE_ON_ERR);
75ca3906 613 install_branch_config(0, head, remote_name, our->name);
f034d354 614 }
5a7d5b68 615 } else if (our) {
2122f675
SB
616 struct commit *c = lookup_commit_reference(the_repository,
617 &our->old_oid);
5a7d5b68 618 /* --branch specifies a non-branch (i.e. tags), detach HEAD */
91774afc 619 update_ref(msg, "HEAD", &c->object.oid, NULL, REF_NO_DEREF,
ae077771 620 UPDATE_REFS_DIE_ON_ERR);
f034d354
NTND
621 } else if (remote) {
622 /*
623 * We know remote HEAD points to a non-branch, or
920b691f 624 * HEAD points to a branch but we don't know which one.
f034d354
NTND
625 * Detach HEAD in all these cases.
626 */
91774afc 627 update_ref(msg, "HEAD", &remote->old_oid, NULL, REF_NO_DEREF,
ae077771 628 UPDATE_REFS_DIE_ON_ERR);
f034d354
NTND
629 }
630}
631
d89f09c8
DS
632static int git_sparse_checkout_init(const char *repo)
633{
22f9b7f3 634 struct strvec argv = STRVEC_INIT;
d89f09c8 635 int result = 0;
d3595416 636 strvec_pushl(&argv, "-C", repo, "sparse-checkout", "set", NULL);
d89f09c8
DS
637
638 /*
639 * We must apply the setting in the current process
640 * for the later checkout to use the sparse-checkout file.
641 */
642 core_apply_sparse_checkout = 1;
643
d70a9eb6 644 if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
d89f09c8
DS
645 error(_("failed to initialize sparse-checkout"));
646 result = 1;
647 }
648
22f9b7f3 649 strvec_clear(&argv);
d89f09c8
DS
650 return result;
651}
652
72c5f883 653static int checkout(int submodule_progress)
c39852c1 654{
ddc2cc64 655 struct object_id oid;
c39852c1 656 char *head;
837e34eb 657 struct lock_file lock_file = LOCK_INIT;
c39852c1
NTND
658 struct unpack_trees_options opts;
659 struct tree *tree;
660 struct tree_desc t;
03b86647 661 int err = 0;
c39852c1
NTND
662
663 if (option_no_checkout)
664 return 0;
665
0f2dc722 666 head = resolve_refdup("HEAD", RESOLVE_REF_READING, &oid, NULL);
c39852c1
NTND
667 if (!head) {
668 warning(_("remote HEAD refers to nonexistent ref, "
669 "unable to checkout.\n"));
670 return 0;
671 }
2857093b 672 if (!strcmp(head, "HEAD")) {
ed9bff08 673 if (advice_enabled(ADVICE_DETACHED_HEAD))
ddc2cc64 674 detach_advice(oid_to_hex(&oid));
dfc8cdc6 675 FREE_AND_NULL(head);
2857093b 676 } else {
59556548 677 if (!starts_with(head, "refs/heads/"))
c39852c1
NTND
678 die(_("HEAD not found below refs/heads!"));
679 }
c39852c1
NTND
680
681 /* We need to be in the new work tree for the checkout */
682 setup_work_tree();
683
837e34eb 684 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
c39852c1
NTND
685
686 memset(&opts, 0, sizeof opts);
687 opts.update = 1;
688 opts.merge = 1;
b878579a 689 opts.clone = 1;
1b5f3733 690 opts.preserve_ignored = 0;
c39852c1 691 opts.fn = oneway_merge;
8f63da13 692 opts.verbose_update = (option_verbosity >= 0);
c39852c1
NTND
693 opts.src_index = &the_index;
694 opts.dst_index = &the_index;
dfc8cdc6 695 init_checkout_metadata(&opts.meta, head, &oid, NULL);
c39852c1 696
a9dbc179 697 tree = parse_tree_indirect(&oid);
c39852c1
NTND
698 parse_tree(tree);
699 init_tree_desc(&t, tree->buffer, tree->size);
0aac7bb2
JK
700 if (unpack_trees(1, &t, &opts) < 0)
701 die(_("unable to checkout working tree"));
c39852c1 702
dfc8cdc6 703 free(head);
704
837e34eb 705 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
c39852c1
NTND
706 die(_("unable to write new index file"));
707
14228447 708 err |= run_hook_le(NULL, "post-checkout", oid_to_hex(null_oid()),
ddc2cc64 709 oid_to_hex(&oid), "1", NULL);
c39852c1 710
bb62e0a9 711 if (!err && (option_recurse_submodules.nr > 0)) {
22f9b7f3
JK
712 struct strvec args = STRVEC_INIT;
713 strvec_pushl(&args, "submodule", "update", "--require-init", "--recursive", NULL);
72290d6a 714
18a74a09 715 if (option_shallow_submodules == 1)
22f9b7f3 716 strvec_push(&args, "--depth=1");
d22eb044 717
72290d6a 718 if (max_jobs != -1)
22f9b7f3 719 strvec_pushf(&args, "--jobs=%d", max_jobs);
72290d6a 720
72c5f883 721 if (submodule_progress)
22f9b7f3 722 strvec_push(&args, "--progress");
72c5f883 723
03c004c5 724 if (option_verbosity < 0)
22f9b7f3 725 strvec_push(&args, "--quiet");
03c004c5 726
4c691016 727 if (option_remote_submodules) {
22f9b7f3
JK
728 strvec_push(&args, "--remote");
729 strvec_push(&args, "--no-fetch");
4c691016
BA
730 }
731
132f600b 732 if (option_single_branch >= 0)
22f9b7f3 733 strvec_push(&args, option_single_branch ?
132f600b
ES
734 "--single-branch" :
735 "--no-single-branch");
736
d70a9eb6 737 err = run_command_v_opt(args.v, RUN_GIT_CMD);
22f9b7f3 738 strvec_clear(&args);
72290d6a 739 }
c39852c1
NTND
740
741 return err;
742}
743
552955ed
SB
744static int git_clone_config(const char *k, const char *v, void *cb)
745{
de9ed3ef
SB
746 if (!strcmp(k, "clone.defaultremotename")) {
747 free(remote_name);
748 remote_name = xstrdup(v);
749 }
4fe788b1
LL
750 if (!strcmp(k, "clone.rejectshallow"))
751 config_reject_shallow = git_config_bool(k, v);
752
552955ed
SB
753 return git_default_config(k, v, cb);
754}
755
84054f79
JK
756static int write_one_config(const char *key, const char *value, void *data)
757{
552955ed
SB
758 /*
759 * give git_clone_config a chance to write config values back to the
760 * environment, since git_config_set_multivar_gently only deals with
761 * config-file writes
762 */
763 int apply_failed = git_clone_config(key, value, data);
764 if (apply_failed)
765 return apply_failed;
766
db4eca1f
JN
767 return git_config_set_multivar_gently(key,
768 value ? value : "true",
769 CONFIG_REGEX_NONE, 0);
84054f79
JK
770}
771
772static void write_config(struct string_list *config)
773{
774 int i;
775
776 for (i = 0; i < config->nr; i++) {
777 if (git_config_parse_parameter(config->items[i].string,
778 write_one_config, NULL) < 0)
39ad4f39 779 die(_("unable to write parameters to config file"));
84054f79
JK
780 }
781}
782
24d36f14
DA
783static void write_refspec_config(const char *src_ref_prefix,
784 const struct ref *our_head_points_at,
785 const struct ref *remote_head_points_at,
786 struct strbuf *branch_top)
31b808a0
RT
787{
788 struct strbuf key = STRBUF_INIT;
789 struct strbuf value = STRBUF_INIT;
790
791 if (option_mirror || !option_bare) {
792 if (option_single_branch && !option_mirror) {
793 if (option_branch) {
60a5f5fc 794 if (starts_with(our_head_points_at->name, "refs/tags/"))
31b808a0
RT
795 strbuf_addf(&value, "+%s:%s", our_head_points_at->name,
796 our_head_points_at->name);
797 else
798 strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
799 branch_top->buf, option_branch);
800 } else if (remote_head_points_at) {
cf4fff57
JK
801 const char *head = remote_head_points_at->name;
802 if (!skip_prefix(head, "refs/heads/", &head))
033abf97 803 BUG("remote HEAD points at non-head?");
cf4fff57 804
31b808a0 805 strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name,
cf4fff57 806 branch_top->buf, head);
31b808a0
RT
807 }
808 /*
809 * otherwise, the next "git fetch" will
810 * simply fetch from HEAD without updating
d6ac1d21 811 * any remote-tracking branch, which is what
31b808a0
RT
812 * we want.
813 */
814 } else {
815 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf);
816 }
817 /* Configure the remote */
818 if (value.len) {
75ca3906 819 strbuf_addf(&key, "remote.%s.fetch", remote_name);
31b808a0
RT
820 git_config_set_multivar(key.buf, value.buf, "^$", 0);
821 strbuf_reset(&key);
822
823 if (option_mirror) {
75ca3906 824 strbuf_addf(&key, "remote.%s.mirror", remote_name);
31b808a0
RT
825 git_config_set(key.buf, "true");
826 strbuf_reset(&key);
827 }
828 }
829 }
830
831 strbuf_release(&key);
832 strbuf_release(&value);
833}
834
fb1d6dab
JH
835static void dissociate_from_references(void)
836{
837 static const char* argv[] = { "repack", "-a", "-d", NULL };
0181681e 838 char *alternates = git_pathdup("objects/info/alternates");
fb1d6dab 839
0181681e
AR
840 if (!access(alternates, F_OK)) {
841 if (run_command_v_opt(argv, RUN_GIT_CMD|RUN_COMMAND_NO_STDIN))
842 die(_("cannot repack to clean up"));
843 if (unlink(alternates) && errno != ENOENT)
844 die_errno(_("cannot unlink temporary alternates file"));
845 }
846 free(alternates);
fb1d6dab
JH
847}
848
6c020421 849static int path_exists(const char *path)
f9e377ad
JK
850{
851 struct stat sb;
852 return !stat(path, &sb);
853}
854
8434c2f1
DB
855int cmd_clone(int argc, const char **argv, const char *prefix)
856{
24c61c44 857 int is_bundle = 0, is_local;
4fe788b1 858 int reject_shallow = 0;
8434c2f1 859 const char *repo_name, *repo, *work_tree, *git_dir;
0c454273 860 char *path = NULL, *dir, *display_repo = NULL;
dfaa209a 861 int dest_exists, real_dest_exists = 0;
37148311 862 const struct ref *refs, *remote_head;
0c454273 863 struct ref *remote_head_points_at = NULL;
7a4ee28f 864 const struct ref *our_head_points_at;
37148311 865 struct ref *mapped_refs;
90498161 866 const struct ref *ref;
3e42cb3b 867 struct strbuf key = STRBUF_INIT;
b5ff37ac 868 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
1db4a75c 869 struct transport *transport = NULL;
9e585046 870 const char *src_ref_prefix = "refs/heads/";
6f48d39f 871 struct remote *remote;
90498161 872 int err = 0, complete_refs_before_fetch = 1;
72c5f883 873 int submodule_progress;
8434c2f1 874
39835409
JT
875 struct transport_ls_refs_options transport_ls_refs_options =
876 TRANSPORT_LS_REFS_OPTIONS_INIT;
8434c2f1 877
bbc30f99 878 packet_trace_identity("clone");
552955ed
SB
879
880 git_config(git_clone_config, NULL);
881
37782920 882 argc = parse_options(argc, argv, prefix, builtin_clone_options,
8434c2f1
DB
883 builtin_clone_usage, 0);
884
d52dc4b1 885 if (argc > 2)
e84d7b74 886 usage_msg_opt(_("Too many arguments."),
d52dc4b1
JN
887 builtin_clone_usage, builtin_clone_options);
888
8434c2f1 889 if (argc == 0)
e84d7b74 890 usage_msg_opt(_("You must specify a repository to clone."),
d52dc4b1 891 builtin_clone_usage, builtin_clone_options);
8434c2f1 892
859e5df9 893 if (option_depth || option_since || option_not.nr)
994c2aaf 894 deepen = 1;
3e6e0edd 895 if (option_single_branch == -1)
994c2aaf 896 option_single_branch = deepen ? 1 : 0;
3e6e0edd 897
bc699afc
JS
898 if (option_mirror)
899 option_bare = 1;
900
8434c2f1
DB
901 if (option_bare) {
902 if (option_origin)
246cac85
JNA
903 die(_("options '%s' and '%s %s' cannot be used together"),
904 "--bare", "--origin", option_origin);
95b63f1e 905 if (real_git_dir)
12909b6b 906 die(_("options '%s' and '%s' cannot be used together"), "--bare", "--separate-git-dir");
8434c2f1 907 option_no_checkout = 1;
8434c2f1
DB
908 }
909
8434c2f1
DB
910 repo_name = argv[0];
911
912 path = get_repo_path(repo_name, &is_bundle);
0c454273
AH
913 if (path) {
914 FREE_AND_NULL(path);
0aaad415 915 repo = absolute_pathdup(repo_name);
0c454273 916 } else if (strchr(repo_name, ':')) {
8434c2f1 917 repo = repo_name;
46da295a
JS
918 display_repo = transport_anonymize_url(repo);
919 } else
920 die(_("repository '%s' does not exist"), repo_name);
8434c2f1 921
5594bcad
NTND
922 /* no need to be strict, transport_set_option() will validate it again */
923 if (option_depth && atoi(option_depth) < 1)
924 die(_("depth %s is not a positive number"), option_depth);
925
8434c2f1
DB
926 if (argc == 2)
927 dir = xstrdup(argv[1]);
928 else
ed86301f
AR
929 dir = git_url_basename(repo_name, is_bundle, option_bare);
930 strip_dir_trailing_slashes(dir);
8434c2f1 931
6c020421 932 dest_exists = path_exists(dir);
55892d23 933 if (dest_exists && !is_empty_dir(dir))
e84d7b74
ÆAB
934 die(_("destination path '%s' already exists and is not "
935 "an empty directory."), dir);
8434c2f1 936
dfaa209a
BW
937 if (real_git_dir) {
938 real_dest_exists = path_exists(real_git_dir);
939 if (real_dest_exists && !is_empty_dir(real_git_dir))
940 die(_("repository path '%s' already exists and is not "
941 "an empty directory."), real_git_dir);
942 }
943
944
46da295a
JS
945 strbuf_addf(&reflog_msg, "clone: from %s",
946 display_repo ? display_repo : repo);
947 free(display_repo);
8434c2f1
DB
948
949 if (option_bare)
950 work_tree = NULL;
951 else {
952 work_tree = getenv("GIT_WORK_TREE");
6c020421 953 if (work_tree && path_exists(work_tree))
e84d7b74 954 die(_("working tree '%s' already exists."), work_tree);
8434c2f1
DB
955 }
956
957 if (option_bare || work_tree)
958 git_dir = xstrdup(dir);
959 else {
960 work_tree = dir;
4e2d094d 961 git_dir = mkpathdup("%s/.git", dir);
8434c2f1
DB
962 }
963
ee0e3872
JK
964 atexit(remove_junk);
965 sigchain_push_common(remove_junk_on_signal);
966
8434c2f1 967 if (!option_bare) {
8e21d63b 968 if (safe_create_leading_directories_const(work_tree) < 0)
e84d7b74 969 die_errno(_("could not create leading directories of '%s'"),
d824cbba 970 work_tree);
d45420c1
JK
971 if (dest_exists)
972 junk_work_tree_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
973 else if (mkdir(work_tree, 0777))
16eff6c0 974 die_errno(_("could not create work tree dir '%s'"),
d824cbba 975 work_tree);
ee0e3872 976 junk_work_tree = work_tree;
8434c2f1
DB
977 set_git_work_tree(work_tree);
978 }
8434c2f1 979
d45420c1 980 if (real_git_dir) {
dfaa209a 981 if (real_dest_exists)
d45420c1
JK
982 junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
983 junk_git_dir = real_git_dir;
984 } else {
985 if (dest_exists)
986 junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
987 junk_git_dir = git_dir;
988 }
8e21d63b 989 if (safe_create_leading_directories_const(git_dir) < 0)
e84d7b74 990 die(_("could not create leading directories of '%s'"), git_dir);
b57fb80a 991
3781fcce
ÆAB
992 if (0 <= option_verbosity) {
993 if (option_bare)
68b939b2 994 fprintf(stderr, _("Cloning into bare repository '%s'...\n"), dir);
3781fcce 995 else
68b939b2 996 fprintf(stderr, _("Cloning into '%s'...\n"), dir);
3781fcce 997 }
31224cbd 998
bb62e0a9
BW
999 if (option_recurse_submodules.nr > 0) {
1000 struct string_list_item *item;
1001 struct strbuf sb = STRBUF_INIT;
48072e3d 1002 int val;
bb62e0a9
BW
1003
1004 /* remove duplicates */
1005 string_list_sort(&option_recurse_submodules);
1006 string_list_remove_duplicates(&option_recurse_submodules, 0);
1007
1008 /*
1009 * NEEDSWORK: In a multi-working-tree world, this needs to be
1010 * set in the per-worktree config.
1011 */
1012 for_each_string_list_item(item, &option_recurse_submodules) {
1013 strbuf_addf(&sb, "submodule.active=%s",
1014 item->string);
1015 string_list_append(&option_config,
1016 strbuf_detach(&sb, NULL));
1017 }
1018
48072e3d
MK
1019 if (!git_config_get_bool("submodule.stickyRecursiveClone", &val) &&
1020 val)
1021 string_list_append(&option_config, "submodule.recurse=true");
1022
31224cbd
SB
1023 if (option_required_reference.nr &&
1024 option_optional_reference.nr)
1025 die(_("clone --recursive is not compatible with "
1026 "both --reference and --reference-if-able"));
1027 else if (option_required_reference.nr) {
1028 string_list_append(&option_config,
1029 "submodule.alternateLocation=superproject");
1030 string_list_append(&option_config,
1031 "submodule.alternateErrorStrategy=die");
1032 } else if (option_optional_reference.nr) {
1033 string_list_append(&option_config,
1034 "submodule.alternateLocation=superproject");
1035 string_list_append(&option_config,
1036 "submodule.alternateErrorStrategy=info");
1037 }
1038 }
1039
32ba12da
JS
1040 init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, NULL,
1041 INIT_DB_QUIET);
33158701 1042
27ff1fbc
ÆAB
1043 if (real_git_dir) {
1044 free((char *)git_dir);
33158701 1045 git_dir = real_git_dir;
27ff1fbc 1046 }
33158701 1047
552955ed
SB
1048 /*
1049 * additional config can be injected with -c, make sure it's included
1050 * after init_db, which clears the entire config environment.
1051 */
84054f79 1052 write_config(&option_config);
8434c2f1 1053
552955ed
SB
1054 /*
1055 * re-read config after init_db and write_config to pick up any config
1056 * injected by --template and --config, respectively.
1057 */
1058 git_config(git_clone_config, NULL);
8434c2f1 1059
4fe788b1
LL
1060 /*
1061 * If option_reject_shallow is specified from CLI option,
1062 * ignore config_reject_shallow from git_clone_config.
1063 */
1064 if (config_reject_shallow != -1)
1065 reject_shallow = config_reject_shallow;
1066 if (option_reject_shallow != -1)
1067 reject_shallow = option_reject_shallow;
1068
de9ed3ef
SB
1069 /*
1070 * apply the remote name provided by --origin only after this second
1071 * call to git_config, to ensure it overrides all config-based values.
1072 */
1073 if (option_origin != NULL)
1074 remote_name = xstrdup(option_origin);
1075
1076 if (remote_name == NULL)
1077 remote_name = xstrdup("origin");
1078
1079 if (!valid_remote_name(remote_name))
1080 die(_("'%s' is not a valid remote name"), remote_name);
8434c2f1
DB
1081
1082 if (option_bare) {
bc699afc
JS
1083 if (option_mirror)
1084 src_ref_prefix = "refs/";
b5ff37ac 1085 strbuf_addstr(&branch_top, src_ref_prefix);
8434c2f1
DB
1086
1087 git_config_set("core.bare", "true");
1088 } else {
75ca3906 1089 strbuf_addf(&branch_top, "refs/remotes/%s/", remote_name);
bc699afc 1090 }
8434c2f1 1091
75ca3906 1092 strbuf_addf(&key, "remote.%s.url", remote_name);
df61c889
SR
1093 git_config_set(key.buf, repo);
1094 strbuf_reset(&key);
1095
0dab2468 1096 if (option_no_tags) {
75ca3906 1097 strbuf_addf(&key, "remote.%s.tagOpt", remote_name);
0dab2468
ÆAB
1098 git_config_set(key.buf, "--no-tags");
1099 strbuf_reset(&key);
1100 }
1101
f7415b4d 1102 if (option_required_reference.nr || option_optional_reference.nr)
dbc92b07 1103 setup_reference();
766ac6a6 1104
47dbf10d 1105 if (option_sparse_checkout && git_sparse_checkout_init(dir))
d89f09c8
DS
1106 return 1;
1107
75ca3906 1108 remote = remote_get(remote_name);
689f0396 1109
1af8b8c0
RS
1110 refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix,
1111 branch_top.buf);
8434c2f1 1112
6f48d39f 1113 transport = transport_get(remote, remote->url[0]);
822f0c4f 1114 transport_set_verbosity(transport, option_verbosity, option_progress);
c915f11e 1115 transport->family = family;
822f0c4f 1116
f38aa83f
MB
1117 path = get_repo_path(remote->url[0], &is_bundle);
1118 is_local = option_local != 0 && path && !is_bundle;
1119 if (is_local) {
1120 if (option_depth)
1121 warning(_("--depth is ignored in local clones; use file:// instead."));
994c2aaf
NTND
1122 if (option_since)
1123 warning(_("--shallow-since is ignored in local clones; use file:// instead."));
859e5df9
NTND
1124 if (option_not.nr)
1125 warning(_("--shallow-exclude is ignored in local clones; use file:// instead."));
548719fb
JT
1126 if (filter_options.choice)
1127 warning(_("--filter is ignored in local clones; use file:// instead."));
f38aa83f 1128 if (!access(mkpath("%s/shallow", path), F_OK)) {
4fe788b1
LL
1129 if (reject_shallow)
1130 die(_("source repository is shallow, reject to clone."));
f38aa83f
MB
1131 if (option_local > 0)
1132 warning(_("source repository is shallow, ignoring --local"));
1133 is_local = 0;
1134 }
1135 }
1136 if (option_local > 0 && !is_local)
1137 warning(_("--local is ignored"));
beea4152 1138 transport->cloning = 1;
8434c2f1 1139
643f918d 1140 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
8434c2f1 1141
4fe788b1
LL
1142 if (reject_shallow)
1143 transport_set_option(transport, TRANS_OPT_REJECT_SHALLOW, "1");
643f918d
JK
1144 if (option_depth)
1145 transport_set_option(transport, TRANS_OPT_DEPTH,
1146 option_depth);
994c2aaf
NTND
1147 if (option_since)
1148 transport_set_option(transport, TRANS_OPT_DEEPEN_SINCE,
1149 option_since);
859e5df9
NTND
1150 if (option_not.nr)
1151 transport_set_option(transport, TRANS_OPT_DEEPEN_NOT,
1152 (const char *)&option_not);
643f918d
JK
1153 if (option_single_branch)
1154 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
8434c2f1 1155
643f918d
JK
1156 if (option_upload_pack)
1157 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
1158 option_upload_pack);
c6807a40 1159
6e983059
JT
1160 if (server_options.nr)
1161 transport->server_options = &server_options;
1162
548719fb 1163 if (filter_options.choice) {
cf9ceb5a
MD
1164 const char *spec =
1165 expand_list_objects_filter_spec(&filter_options);
548719fb 1166 transport_set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER,
cf9ceb5a 1167 spec);
548719fb
JT
1168 transport_set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1169 }
1170
1171 if (transport->smart_options && !deepen && !filter_options.choice)
643f918d 1172 transport->smart_options->check_self_contained_and_connected = 1;
8434c2f1 1173
402c47d9 1174
39835409
JT
1175 strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
1176 refspec_ref_prefixes(&remote->fetch,
1177 &transport_ls_refs_options.ref_prefixes);
402c47d9 1178 if (option_branch)
39835409
JT
1179 expand_ref_prefix(&transport_ls_refs_options.ref_prefixes,
1180 option_branch);
402c47d9 1181 if (!option_no_tags)
39835409
JT
1182 strvec_push(&transport_ls_refs_options.ref_prefixes,
1183 "refs/tags/");
402c47d9 1184
39835409 1185 refs = transport_get_remote_refs(transport, &transport_ls_refs_options);
8434c2f1 1186
5b05795c 1187 if (refs) {
b65dc2ce 1188 int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
1189
1190 /*
1191 * Now that we know what algorithm the remote side is using,
1192 * let's set ours to the same thing.
1193 */
47ac9703 1194 initialize_repository_version(hash_algo, 1);
b65dc2ce 1195 repo_set_hash_algo(the_repository, hash_algo);
1196
515be833 1197 mapped_refs = wanted_peer_refs(refs, &remote->fetch);
5b05795c
MH
1198 /*
1199 * transport_get_remote_refs() may return refs with null sha-1
1200 * in mapped_refs (see struct transport->get_refs_list
1201 * comment). In that case we need fetch it early because
1202 * remote_head code below relies on it.
1203 *
1204 * for normal clones, transport_get_remote_refs() should
1205 * return reliable ref set, we can delay cloning until after
1206 * remote HEAD check.
1207 */
1208 for (ref = refs; ref; ref = ref->next)
f4e54d02 1209 if (is_null_oid(&ref->old_oid)) {
5b05795c
MH
1210 complete_refs_before_fetch = 0;
1211 break;
1212 }
90498161 1213
aab179d9 1214 if (!is_local && !complete_refs_before_fetch) {
6aacb7d8
JK
1215 if (transport_fetch_refs(transport, mapped_refs))
1216 die(_("remote transport reported error"));
aab179d9 1217 }
8434c2f1 1218
6cb4e6cc 1219 remote_head = find_ref_by_name(refs, "HEAD");
7a4ee28f
JK
1220 remote_head_points_at =
1221 guess_remote_head(remote_head, mapped_refs, 0);
1222
1223 if (option_branch) {
7a4ee28f 1224 our_head_points_at =
9e585046 1225 find_remote_branch(mapped_refs, option_branch);
7a4ee28f 1226
920b691f
NTND
1227 if (!our_head_points_at)
1228 die(_("Remote branch %s not found in upstream %s"),
75ca3906 1229 option_branch, remote_name);
7a4ee28f
JK
1230 }
1231 else
1232 our_head_points_at = remote_head_points_at;
86ac7518
SR
1233 }
1234 else {
6b58df54
JK
1235 const char *branch;
1236 char *ref;
1237
a3552aba
RT
1238 if (option_branch)
1239 die(_("Remote branch %s not found in upstream %s"),
75ca3906 1240 option_branch, remote_name);
a3552aba 1241
e84d7b74 1242 warning(_("You appear to have cloned an empty repository."));
5b05795c 1243 mapped_refs = NULL;
7a4ee28f
JK
1244 our_head_points_at = NULL;
1245 remote_head_points_at = NULL;
86ac7518
SR
1246 remote_head = NULL;
1247 option_no_checkout = 1;
0cc1b475 1248
6b58df54
JK
1249 if (transport_ls_refs_options.unborn_head_target &&
1250 skip_prefix(transport_ls_refs_options.unborn_head_target,
1251 "refs/heads/", &branch)) {
1252 ref = transport_ls_refs_options.unborn_head_target;
1253 transport_ls_refs_options.unborn_head_target = NULL;
1254 create_symref("HEAD", ref, reflog_msg.buf);
1255 } else {
1256 branch = git_default_branch_name(0);
1257 ref = xstrfmt("refs/heads/%s", branch);
0cc1b475 1258 }
6b58df54
JK
1259
1260 if (!option_bare)
1261 install_branch_config(0, branch, remote_name, ref);
1262
1263 free(ref);
86ac7518 1264 }
8434c2f1 1265
31b808a0
RT
1266 write_refspec_config(src_ref_prefix, our_head_points_at,
1267 remote_head_points_at, &branch_top);
1268
548719fb 1269 if (filter_options.choice)
75ca3906 1270 partial_clone_register(remote_name, &filter_options);
548719fb 1271
6f48d39f
NTND
1272 if (is_local)
1273 clone_local(path, git_dir);
aab179d9 1274 else if (refs && complete_refs_before_fetch) {
6aacb7d8
JK
1275 if (transport_fetch_refs(transport, mapped_refs))
1276 die(_("remote transport reported error"));
aab179d9 1277 }
8434c2f1 1278
960b7d1c 1279 update_remote_refs(refs, mapped_refs, remote_head_points_at,
548719fb 1280 branch_top.buf, reflog_msg.buf, transport,
2b98478c 1281 !is_local);
8434c2f1 1282
f034d354 1283 update_head(our_head_points_at, remote_head, reflog_msg.buf);
1db4a75c 1284
72c5f883
JK
1285 /*
1286 * We want to show progress for recursive submodule clones iff
1287 * we did so for the main clone. But only the transport knows
1288 * the final decision for this flag, so we need to rescue the value
1289 * before we free the transport.
1290 */
1291 submodule_progress = transport->progress;
1292
58d4d7f1 1293 transport_unlock_pack(transport, 0);
6f48d39f 1294 transport_disconnect(transport);
1db4a75c 1295
786b150c 1296 if (option_dissociate) {
2d511cfc 1297 close_object_store(the_repository->objects);
fb1d6dab 1298 dissociate_from_references();
786b150c 1299 }
fb1d6dab 1300
d3b34622 1301 junk_mode = JUNK_LEAVE_REPO;
72c5f883 1302 err = checkout(submodule_progress);
8434c2f1 1303
de9ed3ef 1304 free(remote_name);
8434c2f1 1305 strbuf_release(&reflog_msg);
b5ff37ac
MV
1306 strbuf_release(&branch_top);
1307 strbuf_release(&key);
0c454273
AH
1308 free_refs(mapped_refs);
1309 free_refs(remote_head_points_at);
1310 free(dir);
1311 free(path);
1312 UNLEAK(repo);
d3b34622 1313 junk_mode = JUNK_LEAVE_ALL;
50b67732 1314
39835409 1315 strvec_clear(&transport_ls_refs_options.ref_prefixes);
4f37d457 1316 free(transport_ls_refs_options.unborn_head_target);
dfa7a6c5 1317 return err;
8434c2f1 1318}