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