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