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