]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/clone.c
clone: do not clean up directories we didn't create
[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"
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;
d45420c1 475static int junk_work_tree_flags;
8434c2f1 476static const char *junk_git_dir;
d45420c1 477static int junk_git_dir_flags;
85064630 478static enum {
d3b34622
JK
479 JUNK_LEAVE_NONE,
480 JUNK_LEAVE_REPO,
481 JUNK_LEAVE_ALL
482} junk_mode = JUNK_LEAVE_NONE;
483
484static const char junk_leave_repo_msg[] =
485N_("Clone succeeded, but checkout failed.\n"
486 "You can inspect what was checked out with 'git status'\n"
487 "and retry the checkout with 'git checkout -f HEAD'\n");
8434c2f1
DB
488
489static void remove_junk(void)
490{
f285a2d7 491 struct strbuf sb = STRBUF_INIT;
d3b34622
JK
492
493 switch (junk_mode) {
494 case JUNK_LEAVE_REPO:
495 warning("%s", _(junk_leave_repo_msg));
496 /* fall-through */
497 case JUNK_LEAVE_ALL:
498 return;
499 default:
500 /* proceed to removal */
501 break;
502 }
503
8434c2f1
DB
504 if (junk_git_dir) {
505 strbuf_addstr(&sb, junk_git_dir);
d45420c1 506 remove_dir_recursively(&sb, junk_git_dir_flags);
8434c2f1
DB
507 strbuf_reset(&sb);
508 }
509 if (junk_work_tree) {
510 strbuf_addstr(&sb, junk_work_tree);
d45420c1 511 remove_dir_recursively(&sb, junk_work_tree_flags);
8434c2f1 512 }
9c18b548 513 strbuf_release(&sb);
8434c2f1
DB
514}
515
516static void remove_junk_on_signal(int signo)
517{
518 remove_junk();
4a16d072 519 sigchain_pop(signo);
8434c2f1
DB
520 raise(signo);
521}
522
9e585046
NTND
523static struct ref *find_remote_branch(const struct ref *refs, const char *branch)
524{
525 struct ref *ref;
526 struct strbuf head = STRBUF_INIT;
527 strbuf_addstr(&head, "refs/heads/");
528 strbuf_addstr(&head, branch);
529 ref = find_ref_by_name(refs, head.buf);
530 strbuf_release(&head);
5a7d5b68
NTND
531
532 if (ref)
533 return ref;
534
535 strbuf_addstr(&head, "refs/tags/");
536 strbuf_addstr(&head, branch);
537 ref = find_ref_by_name(refs, head.buf);
538 strbuf_release(&head);
539
9e585046
NTND
540 return ref;
541}
542
5bdc32d3
NP
543static struct ref *wanted_peer_refs(const struct ref *refs,
544 struct refspec *refspec)
8434c2f1 545{
c1921c18
JK
546 struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD"));
547 struct ref *local_refs = head;
548 struct ref **tail = head ? &head->next : &local_refs;
8434c2f1 549
3e6e0edd
NTND
550 if (option_single_branch) {
551 struct ref *remote_head = NULL;
552
553 if (!option_branch)
554 remote_head = guess_remote_head(head, refs, 0);
0ec4b165
NTND
555 else {
556 local_refs = NULL;
557 tail = &local_refs;
558 remote_head = copy_ref(find_remote_branch(refs, option_branch));
559 }
3e6e0edd
NTND
560
561 if (!remote_head && option_branch)
562 warning(_("Could not find remote branch %s to clone."),
563 option_branch);
5a7d5b68 564 else {
3e6e0edd 565 get_fetch_map(remote_head, refspec, &tail, 0);
5a7d5b68
NTND
566
567 /* if --branch=tag, pull the requested tag explicitly */
568 get_fetch_map(remote_head, tag_refspec, &tail, 0);
569 }
3e6e0edd
NTND
570 } else
571 get_fetch_map(refs, refspec, &tail, 0);
572
0dab2468 573 if (!option_mirror && !option_single_branch && !option_no_tags)
468386a9 574 get_fetch_map(refs, tag_refspec, &tail, 0);
8434c2f1 575
5bdc32d3
NP
576 return local_refs;
577}
578
579static void write_remote_refs(const struct ref *local_refs)
580{
581 const struct ref *r;
582
58f233ce
MH
583 struct ref_transaction *t;
584 struct strbuf err = STRBUF_INIT;
585
586 t = ref_transaction_begin(&err);
587 if (!t)
588 die("%s", err.buf);
9f69d297 589
c1921c18
JK
590 for (r = local_refs; r; r = r->next) {
591 if (!r->peer_ref)
592 continue;
f4e54d02 593 if (ref_transaction_create(t, r->peer_ref->name, r->old_oid.hash,
58f233ce
MH
594 0, NULL, &err))
595 die("%s", err.buf);
c1921c18 596 }
3e8aded2 597
58f233ce
MH
598 if (initial_ref_transaction_commit(t, &err))
599 die("%s", err.buf);
600
601 strbuf_release(&err);
602 ref_transaction_free(t);
8434c2f1
DB
603}
604
3e6e0edd
NTND
605static void write_followtags(const struct ref *refs, const char *msg)
606{
607 const struct ref *ref;
608 for (ref = refs; ref; ref = ref->next) {
59556548 609 if (!starts_with(ref->name, "refs/tags/"))
3e6e0edd 610 continue;
59556548 611 if (ends_with(ref->name, "^{}"))
3e6e0edd 612 continue;
f4e54d02 613 if (!has_object_file(&ref->old_oid))
3e6e0edd 614 continue;
f4e54d02 615 update_ref(msg, ref->name, ref->old_oid.hash,
f4124112 616 NULL, 0, UPDATE_REFS_DIE_ON_ERR);
3e6e0edd
NTND
617 }
618}
619
0433ad12
JK
620static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
621{
622 struct ref **rm = cb_data;
623 struct ref *ref = *rm;
624
625 /*
626 * Skip anything missing a peer_ref, which we are not
627 * actually going to write a ref for.
628 */
629 while (ref && !ref->peer_ref)
630 ref = ref->next;
631 /* Returning -1 notes "end of list" to the caller. */
632 if (!ref)
633 return -1;
634
f4e54d02 635 hashcpy(sha1, ref->old_oid.hash);
0433ad12
JK
636 *rm = ref->next;
637 return 0;
638}
639
960b7d1c
NTND
640static void update_remote_refs(const struct ref *refs,
641 const struct ref *mapped_refs,
642 const struct ref *remote_head_points_at,
643 const char *branch_top,
c6807a40 644 const char *msg,
45ed4afa 645 struct transport *transport,
125a05fd 646 int check_connectivity)
960b7d1c 647{
0433ad12
JK
648 const struct ref *rm = mapped_refs;
649
125a05fd 650 if (check_connectivity) {
7043c707
JK
651 struct check_connected_options opt = CHECK_CONNECTED_INIT;
652
653 opt.transport = transport;
38e590ea 654 opt.progress = transport->progress;
7043c707 655
7043c707 656 if (check_connected(iterate_ref_map, &rm, &opt))
125a05fd
JK
657 die(_("remote did not send all necessary objects"));
658 }
0433ad12 659
960b7d1c 660 if (refs) {
960b7d1c 661 write_remote_refs(mapped_refs);
0dab2468 662 if (option_single_branch && !option_no_tags)
960b7d1c
NTND
663 write_followtags(refs, msg);
664 }
665
666 if (remote_head_points_at && !option_bare) {
667 struct strbuf head_ref = STRBUF_INIT;
668 strbuf_addstr(&head_ref, branch_top);
669 strbuf_addstr(&head_ref, "HEAD");
4be49d75
JK
670 if (create_symref(head_ref.buf,
671 remote_head_points_at->peer_ref->name,
672 msg) < 0)
39ad4f39 673 die(_("unable to update %s"), head_ref.buf);
4be49d75 674 strbuf_release(&head_ref);
960b7d1c
NTND
675 }
676}
677
f034d354
NTND
678static void update_head(const struct ref *our, const struct ref *remote,
679 const char *msg)
680{
cf4fff57
JK
681 const char *head;
682 if (our && skip_prefix(our->name, "refs/heads/", &head)) {
f034d354 683 /* Local default branch link */
4be49d75 684 if (create_symref("HEAD", our->name, NULL) < 0)
39ad4f39 685 die(_("unable to update HEAD"));
f034d354 686 if (!option_bare) {
f4e54d02 687 update_ref(msg, "HEAD", our->old_oid.hash, NULL, 0,
f4124112 688 UPDATE_REFS_DIE_ON_ERR);
f034d354
NTND
689 install_branch_config(0, head, option_origin, our->name);
690 }
5a7d5b68 691 } else if (our) {
bc83266a 692 struct commit *c = lookup_commit_reference(&our->old_oid);
5a7d5b68 693 /* --branch specifies a non-branch (i.e. tags), detach HEAD */
ed1c9977 694 update_ref(msg, "HEAD", c->object.oid.hash,
f4124112 695 NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
f034d354
NTND
696 } else if (remote) {
697 /*
698 * We know remote HEAD points to a non-branch, or
920b691f 699 * HEAD points to a branch but we don't know which one.
f034d354
NTND
700 * Detach HEAD in all these cases.
701 */
f4e54d02 702 update_ref(msg, "HEAD", remote->old_oid.hash,
f4124112 703 NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
f034d354
NTND
704 }
705}
706
72c5f883 707static int checkout(int submodule_progress)
c39852c1 708{
ddc2cc64 709 struct object_id oid;
c39852c1
NTND
710 char *head;
711 struct lock_file *lock_file;
712 struct unpack_trees_options opts;
713 struct tree *tree;
714 struct tree_desc t;
03b86647 715 int err = 0;
c39852c1
NTND
716
717 if (option_no_checkout)
718 return 0;
719
ddc2cc64 720 head = resolve_refdup("HEAD", RESOLVE_REF_READING, oid.hash, NULL);
c39852c1
NTND
721 if (!head) {
722 warning(_("remote HEAD refers to nonexistent ref, "
723 "unable to checkout.\n"));
724 return 0;
725 }
2857093b
NTND
726 if (!strcmp(head, "HEAD")) {
727 if (advice_detached_head)
ddc2cc64 728 detach_advice(oid_to_hex(&oid));
2857093b 729 } else {
59556548 730 if (!starts_with(head, "refs/heads/"))
c39852c1
NTND
731 die(_("HEAD not found below refs/heads!"));
732 }
733 free(head);
734
735 /* We need to be in the new work tree for the checkout */
736 setup_work_tree();
737
738 lock_file = xcalloc(1, sizeof(struct lock_file));
b3e83cc7 739 hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
c39852c1
NTND
740
741 memset(&opts, 0, sizeof opts);
742 opts.update = 1;
743 opts.merge = 1;
744 opts.fn = oneway_merge;
8f63da13 745 opts.verbose_update = (option_verbosity >= 0);
c39852c1
NTND
746 opts.src_index = &the_index;
747 opts.dst_index = &the_index;
748
a9dbc179 749 tree = parse_tree_indirect(&oid);
c39852c1
NTND
750 parse_tree(tree);
751 init_tree_desc(&t, tree->buffer, tree->size);
0aac7bb2
JK
752 if (unpack_trees(1, &t, &opts) < 0)
753 die(_("unable to checkout working tree"));
c39852c1 754
03b86647 755 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
c39852c1
NTND
756 die(_("unable to write new index file"));
757
15048f8a 758 err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
ddc2cc64 759 oid_to_hex(&oid), "1", NULL);
c39852c1 760
bb62e0a9 761 if (!err && (option_recurse_submodules.nr > 0)) {
72290d6a
SB
762 struct argv_array args = ARGV_ARRAY_INIT;
763 argv_array_pushl(&args, "submodule", "update", "--init", "--recursive", NULL);
764
18a74a09 765 if (option_shallow_submodules == 1)
d22eb044
SB
766 argv_array_push(&args, "--depth=1");
767
72290d6a
SB
768 if (max_jobs != -1)
769 argv_array_pushf(&args, "--jobs=%d", max_jobs);
770
72c5f883
JK
771 if (submodule_progress)
772 argv_array_push(&args, "--progress");
773
03c004c5
BW
774 if (option_verbosity < 0)
775 argv_array_push(&args, "--quiet");
776
72290d6a
SB
777 err = run_command_v_opt(args.argv, RUN_GIT_CMD);
778 argv_array_clear(&args);
779 }
c39852c1
NTND
780
781 return err;
782}
783
84054f79
JK
784static int write_one_config(const char *key, const char *value, void *data)
785{
db4eca1f
JN
786 return git_config_set_multivar_gently(key,
787 value ? value : "true",
788 CONFIG_REGEX_NONE, 0);
84054f79
JK
789}
790
791static void write_config(struct string_list *config)
792{
793 int i;
794
795 for (i = 0; i < config->nr; i++) {
796 if (git_config_parse_parameter(config->items[i].string,
797 write_one_config, NULL) < 0)
39ad4f39 798 die(_("unable to write parameters to config file"));
84054f79
JK
799 }
800}
801
24d36f14
DA
802static void write_refspec_config(const char *src_ref_prefix,
803 const struct ref *our_head_points_at,
804 const struct ref *remote_head_points_at,
805 struct strbuf *branch_top)
31b808a0
RT
806{
807 struct strbuf key = STRBUF_INIT;
808 struct strbuf value = STRBUF_INIT;
809
810 if (option_mirror || !option_bare) {
811 if (option_single_branch && !option_mirror) {
812 if (option_branch) {
60a5f5fc 813 if (starts_with(our_head_points_at->name, "refs/tags/"))
31b808a0
RT
814 strbuf_addf(&value, "+%s:%s", our_head_points_at->name,
815 our_head_points_at->name);
816 else
817 strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
818 branch_top->buf, option_branch);
819 } else if (remote_head_points_at) {
cf4fff57
JK
820 const char *head = remote_head_points_at->name;
821 if (!skip_prefix(head, "refs/heads/", &head))
822 die("BUG: remote HEAD points at non-head?");
823
31b808a0 824 strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name,
cf4fff57 825 branch_top->buf, head);
31b808a0
RT
826 }
827 /*
828 * otherwise, the next "git fetch" will
829 * simply fetch from HEAD without updating
d6ac1d21 830 * any remote-tracking branch, which is what
31b808a0
RT
831 * we want.
832 */
833 } else {
834 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf);
835 }
836 /* Configure the remote */
837 if (value.len) {
838 strbuf_addf(&key, "remote.%s.fetch", option_origin);
839 git_config_set_multivar(key.buf, value.buf, "^$", 0);
840 strbuf_reset(&key);
841
842 if (option_mirror) {
843 strbuf_addf(&key, "remote.%s.mirror", option_origin);
844 git_config_set(key.buf, "true");
845 strbuf_reset(&key);
846 }
847 }
848 }
849
850 strbuf_release(&key);
851 strbuf_release(&value);
852}
853
fb1d6dab
JH
854static void dissociate_from_references(void)
855{
856 static const char* argv[] = { "repack", "-a", "-d", NULL };
0181681e 857 char *alternates = git_pathdup("objects/info/alternates");
fb1d6dab 858
0181681e
AR
859 if (!access(alternates, F_OK)) {
860 if (run_command_v_opt(argv, RUN_GIT_CMD|RUN_COMMAND_NO_STDIN))
861 die(_("cannot repack to clean up"));
862 if (unlink(alternates) && errno != ENOENT)
863 die_errno(_("cannot unlink temporary alternates file"));
864 }
865 free(alternates);
fb1d6dab
JH
866}
867
f9e377ad
JK
868static int dir_exists(const char *path)
869{
870 struct stat sb;
871 return !stat(path, &sb);
872}
873
8434c2f1
DB
874int cmd_clone(int argc, const char **argv, const char *prefix)
875{
24c61c44 876 int is_bundle = 0, is_local;
8434c2f1
DB
877 const char *repo_name, *repo, *work_tree, *git_dir;
878 char *path, *dir;
55892d23 879 int dest_exists;
37148311 880 const struct ref *refs, *remote_head;
7a4ee28f
JK
881 const struct ref *remote_head_points_at;
882 const struct ref *our_head_points_at;
37148311 883 struct ref *mapped_refs;
90498161 884 const struct ref *ref;
b5ff37ac
MV
885 struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
886 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
1db4a75c 887 struct transport *transport = NULL;
9e585046 888 const char *src_ref_prefix = "refs/heads/";
6f48d39f 889 struct remote *remote;
90498161 890 int err = 0, complete_refs_before_fetch = 1;
72c5f883 891 int submodule_progress;
8434c2f1 892
689f0396
DB
893 struct refspec *refspec;
894 const char *fetch_pattern;
8434c2f1 895
bbc30f99 896 packet_trace_identity("clone");
37782920 897 argc = parse_options(argc, argv, prefix, builtin_clone_options,
8434c2f1
DB
898 builtin_clone_usage, 0);
899
d52dc4b1 900 if (argc > 2)
e84d7b74 901 usage_msg_opt(_("Too many arguments."),
d52dc4b1
JN
902 builtin_clone_usage, builtin_clone_options);
903
8434c2f1 904 if (argc == 0)
e84d7b74 905 usage_msg_opt(_("You must specify a repository to clone."),
d52dc4b1 906 builtin_clone_usage, builtin_clone_options);
8434c2f1 907
859e5df9 908 if (option_depth || option_since || option_not.nr)
994c2aaf 909 deepen = 1;
3e6e0edd 910 if (option_single_branch == -1)
994c2aaf 911 option_single_branch = deepen ? 1 : 0;
3e6e0edd 912
bc699afc
JS
913 if (option_mirror)
914 option_bare = 1;
915
8434c2f1
DB
916 if (option_bare) {
917 if (option_origin)
e84d7b74 918 die(_("--bare and --origin %s options are incompatible."),
8434c2f1 919 option_origin);
95b63f1e
NTND
920 if (real_git_dir)
921 die(_("--bare and --separate-git-dir are incompatible."));
8434c2f1 922 option_no_checkout = 1;
8434c2f1
DB
923 }
924
925 if (!option_origin)
926 option_origin = "origin";
927
928 repo_name = argv[0];
929
930 path = get_repo_path(repo_name, &is_bundle);
931 if (path)
0aaad415 932 repo = absolute_pathdup(repo_name);
8434c2f1 933 else if (!strchr(repo_name, ':'))
d3e1ddfd 934 die(_("repository '%s' does not exist"), repo_name);
8434c2f1
DB
935 else
936 repo = repo_name;
937
5594bcad
NTND
938 /* no need to be strict, transport_set_option() will validate it again */
939 if (option_depth && atoi(option_depth) < 1)
940 die(_("depth %s is not a positive number"), option_depth);
941
8434c2f1
DB
942 if (argc == 2)
943 dir = xstrdup(argv[1]);
944 else
6612f877 945 dir = guess_dir_name(repo_name, is_bundle, option_bare);
44a68fd5 946 strip_trailing_slashes(dir);
8434c2f1 947
f9e377ad 948 dest_exists = dir_exists(dir);
55892d23 949 if (dest_exists && !is_empty_dir(dir))
e84d7b74
ÆAB
950 die(_("destination path '%s' already exists and is not "
951 "an empty directory."), dir);
8434c2f1 952
8434c2f1
DB
953 strbuf_addf(&reflog_msg, "clone: from %s", repo);
954
955 if (option_bare)
956 work_tree = NULL;
957 else {
958 work_tree = getenv("GIT_WORK_TREE");
f9e377ad 959 if (work_tree && dir_exists(work_tree))
e84d7b74 960 die(_("working tree '%s' already exists."), work_tree);
8434c2f1
DB
961 }
962
963 if (option_bare || work_tree)
964 git_dir = xstrdup(dir);
965 else {
966 work_tree = dir;
4e2d094d 967 git_dir = mkpathdup("%s/.git", dir);
8434c2f1
DB
968 }
969
ee0e3872
JK
970 atexit(remove_junk);
971 sigchain_push_common(remove_junk_on_signal);
972
8434c2f1 973 if (!option_bare) {
8e21d63b 974 if (safe_create_leading_directories_const(work_tree) < 0)
e84d7b74 975 die_errno(_("could not create leading directories of '%s'"),
d824cbba 976 work_tree);
d45420c1
JK
977 if (dest_exists)
978 junk_work_tree_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
979 else if (mkdir(work_tree, 0777))
16eff6c0 980 die_errno(_("could not create work tree dir '%s'"),
d824cbba 981 work_tree);
ee0e3872 982 junk_work_tree = work_tree;
8434c2f1
DB
983 set_git_work_tree(work_tree);
984 }
8434c2f1 985
d45420c1
JK
986 if (real_git_dir) {
987 if (dir_exists(real_git_dir))
988 junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
989 junk_git_dir = real_git_dir;
990 } else {
991 if (dest_exists)
992 junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
993 junk_git_dir = git_dir;
994 }
8e21d63b 995 if (safe_create_leading_directories_const(git_dir) < 0)
e84d7b74 996 die(_("could not create leading directories of '%s'"), git_dir);
b57fb80a 997
3781fcce
ÆAB
998 if (0 <= option_verbosity) {
999 if (option_bare)
68b939b2 1000 fprintf(stderr, _("Cloning into bare repository '%s'...\n"), dir);
3781fcce 1001 else
68b939b2 1002 fprintf(stderr, _("Cloning into '%s'...\n"), dir);
3781fcce 1003 }
31224cbd 1004
bb62e0a9
BW
1005 if (option_recurse_submodules.nr > 0) {
1006 struct string_list_item *item;
1007 struct strbuf sb = STRBUF_INIT;
1008
1009 /* remove duplicates */
1010 string_list_sort(&option_recurse_submodules);
1011 string_list_remove_duplicates(&option_recurse_submodules, 0);
1012
1013 /*
1014 * NEEDSWORK: In a multi-working-tree world, this needs to be
1015 * set in the per-worktree config.
1016 */
1017 for_each_string_list_item(item, &option_recurse_submodules) {
1018 strbuf_addf(&sb, "submodule.active=%s",
1019 item->string);
1020 string_list_append(&option_config,
1021 strbuf_detach(&sb, NULL));
1022 }
1023
31224cbd
SB
1024 if (option_required_reference.nr &&
1025 option_optional_reference.nr)
1026 die(_("clone --recursive is not compatible with "
1027 "both --reference and --reference-if-able"));
1028 else if (option_required_reference.nr) {
1029 string_list_append(&option_config,
1030 "submodule.alternateLocation=superproject");
1031 string_list_append(&option_config,
1032 "submodule.alternateErrorStrategy=die");
1033 } else if (option_optional_reference.nr) {
1034 string_list_append(&option_config,
1035 "submodule.alternateLocation=superproject");
1036 string_list_append(&option_config,
1037 "submodule.alternateErrorStrategy=info");
1038 }
1039 }
1040
33158701
NTND
1041 init_db(git_dir, real_git_dir, option_template, INIT_DB_QUIET);
1042
1043 if (real_git_dir)
1044 git_dir = real_git_dir;
1045
84054f79 1046 write_config(&option_config);
8434c2f1 1047
9bd81e42 1048 git_config(git_default_config, NULL);
8434c2f1
DB
1049
1050 if (option_bare) {
bc699afc
JS
1051 if (option_mirror)
1052 src_ref_prefix = "refs/";
b5ff37ac 1053 strbuf_addstr(&branch_top, src_ref_prefix);
8434c2f1
DB
1054
1055 git_config_set("core.bare", "true");
1056 } else {
b5ff37ac 1057 strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
bc699afc 1058 }
8434c2f1 1059
689f0396 1060 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
df61c889
SR
1061 strbuf_addf(&key, "remote.%s.url", option_origin);
1062 git_config_set(key.buf, repo);
1063 strbuf_reset(&key);
1064
0dab2468
ÆAB
1065 if (option_no_tags) {
1066 strbuf_addf(&key, "remote.%s.tagOpt", option_origin);
1067 git_config_set(key.buf, "--no-tags");
1068 strbuf_reset(&key);
1069 }
1070
f7415b4d 1071 if (option_required_reference.nr || option_optional_reference.nr)
dbc92b07 1072 setup_reference();
766ac6a6 1073
689f0396
DB
1074 fetch_pattern = value.buf;
1075 refspec = parse_fetch_refspec(1, &fetch_pattern);
1076
1077 strbuf_reset(&value);
8434c2f1 1078
6f48d39f
NTND
1079 remote = remote_get(option_origin);
1080 transport = transport_get(remote, remote->url[0]);
822f0c4f 1081 transport_set_verbosity(transport, option_verbosity, option_progress);
c915f11e 1082 transport->family = family;
822f0c4f 1083
f38aa83f
MB
1084 path = get_repo_path(remote->url[0], &is_bundle);
1085 is_local = option_local != 0 && path && !is_bundle;
1086 if (is_local) {
1087 if (option_depth)
1088 warning(_("--depth is ignored in local clones; use file:// instead."));
994c2aaf
NTND
1089 if (option_since)
1090 warning(_("--shallow-since is ignored in local clones; use file:// instead."));
859e5df9
NTND
1091 if (option_not.nr)
1092 warning(_("--shallow-exclude is ignored in local clones; use file:// instead."));
f38aa83f
MB
1093 if (!access(mkpath("%s/shallow", path), F_OK)) {
1094 if (option_local > 0)
1095 warning(_("source repository is shallow, ignoring --local"));
1096 is_local = 0;
1097 }
1098 }
1099 if (option_local > 0 && !is_local)
1100 warning(_("--local is ignored"));
beea4152 1101 transport->cloning = 1;
8434c2f1 1102
643f918d
JK
1103 if (!transport->get_refs_list || (!is_local && !transport->fetch))
1104 die(_("Don't know how to clone %s"), transport->url);
37b78c25 1105
643f918d 1106 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
8434c2f1 1107
643f918d
JK
1108 if (option_depth)
1109 transport_set_option(transport, TRANS_OPT_DEPTH,
1110 option_depth);
994c2aaf
NTND
1111 if (option_since)
1112 transport_set_option(transport, TRANS_OPT_DEEPEN_SINCE,
1113 option_since);
859e5df9
NTND
1114 if (option_not.nr)
1115 transport_set_option(transport, TRANS_OPT_DEEPEN_NOT,
1116 (const char *)&option_not);
643f918d
JK
1117 if (option_single_branch)
1118 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
8434c2f1 1119
643f918d
JK
1120 if (option_upload_pack)
1121 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
1122 option_upload_pack);
c6807a40 1123
994c2aaf 1124 if (transport->smart_options && !deepen)
643f918d 1125 transport->smart_options->check_self_contained_and_connected = 1;
8434c2f1 1126
6f48d39f 1127 refs = transport_get_remote_refs(transport);
8434c2f1 1128
5b05795c
MH
1129 if (refs) {
1130 mapped_refs = wanted_peer_refs(refs, refspec);
1131 /*
1132 * transport_get_remote_refs() may return refs with null sha-1
1133 * in mapped_refs (see struct transport->get_refs_list
1134 * comment). In that case we need fetch it early because
1135 * remote_head code below relies on it.
1136 *
1137 * for normal clones, transport_get_remote_refs() should
1138 * return reliable ref set, we can delay cloning until after
1139 * remote HEAD check.
1140 */
1141 for (ref = refs; ref; ref = ref->next)
f4e54d02 1142 if (is_null_oid(&ref->old_oid)) {
5b05795c
MH
1143 complete_refs_before_fetch = 0;
1144 break;
1145 }
90498161 1146
5b05795c
MH
1147 if (!is_local && !complete_refs_before_fetch)
1148 transport_fetch_refs(transport, mapped_refs);
8434c2f1 1149
6cb4e6cc 1150 remote_head = find_ref_by_name(refs, "HEAD");
7a4ee28f
JK
1151 remote_head_points_at =
1152 guess_remote_head(remote_head, mapped_refs, 0);
1153
1154 if (option_branch) {
7a4ee28f 1155 our_head_points_at =
9e585046 1156 find_remote_branch(mapped_refs, option_branch);
7a4ee28f 1157
920b691f
NTND
1158 if (!our_head_points_at)
1159 die(_("Remote branch %s not found in upstream %s"),
1160 option_branch, option_origin);
7a4ee28f
JK
1161 }
1162 else
1163 our_head_points_at = remote_head_points_at;
86ac7518
SR
1164 }
1165 else {
a3552aba
RT
1166 if (option_branch)
1167 die(_("Remote branch %s not found in upstream %s"),
1168 option_branch, option_origin);
1169
e84d7b74 1170 warning(_("You appear to have cloned an empty repository."));
5b05795c 1171 mapped_refs = NULL;
7a4ee28f
JK
1172 our_head_points_at = NULL;
1173 remote_head_points_at = NULL;
86ac7518
SR
1174 remote_head = NULL;
1175 option_no_checkout = 1;
5cd12b85 1176 if (!option_bare)
a9f2c136 1177 install_branch_config(0, "master", option_origin,
5cd12b85 1178 "refs/heads/master");
86ac7518 1179 }
8434c2f1 1180
31b808a0
RT
1181 write_refspec_config(src_ref_prefix, our_head_points_at,
1182 remote_head_points_at, &branch_top);
1183
6f48d39f
NTND
1184 if (is_local)
1185 clone_local(path, git_dir);
90498161 1186 else if (refs && complete_refs_before_fetch)
6f48d39f 1187 transport_fetch_refs(transport, mapped_refs);
8434c2f1 1188
960b7d1c 1189 update_remote_refs(refs, mapped_refs, remote_head_points_at,
45ed4afa 1190 branch_top.buf, reflog_msg.buf, transport, !is_local);
8434c2f1 1191
f034d354 1192 update_head(our_head_points_at, remote_head, reflog_msg.buf);
1db4a75c 1193
72c5f883
JK
1194 /*
1195 * We want to show progress for recursive submodule clones iff
1196 * we did so for the main clone. But only the transport knows
1197 * the final decision for this flag, so we need to rescue the value
1198 * before we free the transport.
1199 */
1200 submodule_progress = transport->progress;
1201
6f48d39f
NTND
1202 transport_unlock_pack(transport);
1203 transport_disconnect(transport);
1db4a75c 1204
786b150c
JS
1205 if (option_dissociate) {
1206 close_all_packs();
fb1d6dab 1207 dissociate_from_references();
786b150c 1208 }
fb1d6dab 1209
d3b34622 1210 junk_mode = JUNK_LEAVE_REPO;
72c5f883 1211 err = checkout(submodule_progress);
8434c2f1
DB
1212
1213 strbuf_release(&reflog_msg);
b5ff37ac
MV
1214 strbuf_release(&branch_top);
1215 strbuf_release(&key);
1216 strbuf_release(&value);
d3b34622 1217 junk_mode = JUNK_LEAVE_ALL;
50b67732
SB
1218
1219 free(refspec);
dfa7a6c5 1220 return err;
8434c2f1 1221}