clone: clone from a repository with relative alternates
[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"
8434c2f1
DB
12#include "parse-options.h"
13#include "fetch-pack.h"
14#include "refs.h"
15#include "tree.h"
16#include "tree-walk.h"
17#include "unpack-trees.h"
18#include "transport.h"
19#include "strbuf.h"
20#include "dir.h"
3e8aded2 21#include "pack-refs.h"
4a16d072 22#include "sigchain.h"
a9f2c136 23#include "branch.h"
8ef51733 24#include "remote.h"
dfa7a6c5 25#include "run-command.h"
8434c2f1
DB
26
27/*
28 * Overall FIXMEs:
29 * - respect DB_ENVIRONMENT for .git/objects.
30 *
31 * Implementation notes:
32 * - dropping use-separate-remote and no-separate-remote compatibility
33 *
34 */
35static const char * const builtin_clone_usage[] = {
1b1dd23f 36 "git clone [options] [--] <repo> [<dir>]",
8434c2f1
DB
37 NULL
38};
39
5bd631b3 40static int option_no_checkout, option_bare, option_mirror;
e7fed18a 41static int option_local, option_no_hardlinks, option_shared, option_recursive;
dbc92b07 42static char *option_template, *option_depth;
8434c2f1 43static char *option_origin = NULL;
7a4ee28f 44static char *option_branch = NULL;
b57fb80a 45static const char *real_git_dir;
8434c2f1 46static char *option_upload_pack = "git-upload-pack";
5bd631b3 47static int option_verbosity;
5a518ad4 48static int option_progress;
dbc92b07
JH
49static struct string_list option_reference;
50
51static int opt_parse_reference(const struct option *opt, const char *arg, int unset)
52{
53 struct string_list *option_reference = opt->value;
54 if (!arg)
55 return -1;
56 string_list_append(option_reference, arg);
57 return 0;
58}
8434c2f1
DB
59
60static struct option builtin_clone_options[] = {
5bd631b3 61 OPT__VERBOSITY(&option_verbosity),
5a518ad4
TRC
62 OPT_BOOLEAN(0, "progress", &option_progress,
63 "force progress reporting"),
8434c2f1
DB
64 OPT_BOOLEAN('n', "no-checkout", &option_no_checkout,
65 "don't create a checkout"),
66 OPT_BOOLEAN(0, "bare", &option_bare, "create a bare repository"),
ebc9d420
JH
67 { OPTION_BOOLEAN, 0, "naked", &option_bare, NULL,
68 "create a bare repository",
69 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
bc699afc
JS
70 OPT_BOOLEAN(0, "mirror", &option_mirror,
71 "create a mirror repository (implies bare)"),
8434c2f1
DB
72 OPT_BOOLEAN('l', "local", &option_local,
73 "to clone from a local repository"),
74 OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks,
75 "don't use local hardlinks, always copy"),
76 OPT_BOOLEAN('s', "shared", &option_shared,
77 "setup as shared repository"),
e7fed18a 78 OPT_BOOLEAN(0, "recursive", &option_recursive,
ebc9d420 79 "initialize submodules in the clone"),
3446a54c 80 OPT_BOOLEAN(0, "recurse-submodules", &option_recursive,
ccdd3da6 81 "initialize submodules in the clone"),
5027fa86
MG
82 OPT_STRING(0, "template", &option_template, "template-directory",
83 "directory from which templates will be used"),
dbc92b07
JH
84 OPT_CALLBACK(0 , "reference", &option_reference, "repo",
85 "reference repository", &opt_parse_reference),
8434c2f1 86 OPT_STRING('o', "origin", &option_origin, "branch",
02ed2458 87 "use <branch> instead of 'origin' to track upstream"),
7a4ee28f
JK
88 OPT_STRING('b', "branch", &option_branch, "branch",
89 "checkout <branch> instead of the remote's HEAD"),
8434c2f1
DB
90 OPT_STRING('u', "upload-pack", &option_upload_pack, "path",
91 "path to git-upload-pack on the remote"),
92 OPT_STRING(0, "depth", &option_depth, "depth",
93 "create a shallow clone of that depth"),
09ffc706 94 OPT_STRING(0, "separate-git-dir", &real_git_dir, "gitdir",
b57fb80a 95 "separate git dir from working tree"),
8434c2f1
DB
96
97 OPT_END()
98};
99
e7fed18a
JH
100static const char *argv_submodule[] = {
101 "submodule", "update", "--init", "--recursive", NULL
102};
103
8434c2f1
DB
104static char *get_repo_path(const char *repo, int *is_bundle)
105{
106 static char *suffix[] = { "/.git", ".git", "" };
107 static char *bundle_suffix[] = { ".bundle", "" };
108 struct stat st;
109 int i;
110
111 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
112 const char *path;
113 path = mkpath("%s%s", repo, suffix[i]);
90b4a71c 114 if (is_directory(path)) {
8434c2f1 115 *is_bundle = 0;
e2a57aac 116 return xstrdup(absolute_path(path));
8434c2f1
DB
117 }
118 }
119
120 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
121 const char *path;
122 path = mkpath("%s%s", repo, bundle_suffix[i]);
123 if (!stat(path, &st) && S_ISREG(st.st_mode)) {
124 *is_bundle = 1;
e2a57aac 125 return xstrdup(absolute_path(path));
8434c2f1
DB
126 }
127 }
128
129 return NULL;
130}
131
6612f877 132static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
8434c2f1 133{
b8c5db35 134 const char *end = repo + strlen(repo), *start;
8a94bc7b 135 char *dir;
b8c5db35
JS
136
137 /*
8a94bc7b 138 * Strip trailing spaces, slashes and /.git
b8c5db35 139 */
8a94bc7b 140 while (repo < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
b8c5db35
JS
141 end--;
142 if (end - repo > 5 && is_dir_sep(end[-5]) &&
143 !strncmp(end - 4, ".git", 4)) {
144 end -= 5;
145 while (repo < end && is_dir_sep(end[-1]))
146 end--;
147 }
148
149 /*
150 * Find last component, but be prepared that repo could have
151 * the form "remote.example.com:foo.git", i.e. no slash
152 * in the directory part.
153 */
154 start = end;
155 while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
156 start--;
157
158 /*
159 * Strip .{bundle,git}.
160 */
161 if (is_bundle) {
162 if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
163 end -= 7;
164 } else {
165 if (end - start > 4 && !strncmp(end - 4, ".git", 4))
166 end -= 4;
8434c2f1
DB
167 }
168
6612f877 169 if (is_bare) {
32716a2c
MV
170 struct strbuf result = STRBUF_INIT;
171 strbuf_addf(&result, "%.*s.git", (int)(end - start), start);
2af202be 172 dir = strbuf_detach(&result, NULL);
8a94bc7b
AR
173 } else
174 dir = xstrndup(start, end - start);
175 /*
176 * Replace sequences of 'control' characters and whitespace
177 * with one ascii space, remove leading and trailing spaces.
178 */
179 if (*dir) {
180 char *out = dir;
181 int prev_space = 1 /* strip leading whitespace */;
182 for (end = dir; *end; ++end) {
183 char ch = *end;
184 if ((unsigned char)ch < '\x20')
185 ch = '\x20';
186 if (isspace(ch)) {
187 if (prev_space)
188 continue;
189 prev_space = 1;
190 } else
191 prev_space = 0;
192 *out++ = ch;
193 }
194 *out = '\0';
195 if (out > dir && prev_space)
196 out[-1] = '\0';
6612f877 197 }
8a94bc7b 198 return dir;
8434c2f1
DB
199}
200
44a68fd5
CB
201static void strip_trailing_slashes(char *dir)
202{
203 char *end = dir + strlen(dir);
204
205 while (dir < end - 1 && is_dir_sep(end[-1]))
206 end--;
207 *end = '\0';
208}
209
dbc92b07 210static int add_one_reference(struct string_list_item *item, void *cb_data)
8434c2f1 211{
e6baf4a1
JH
212 char *ref_git;
213 struct strbuf alternate = STRBUF_INIT;
8434c2f1
DB
214 struct remote *remote;
215 struct transport *transport;
216 const struct ref *extra;
217
e6baf4a1
JH
218 /* Beware: real_path() and mkpath() return static buffer */
219 ref_git = xstrdup(real_path(item->string));
220 if (is_directory(mkpath("%s/.git/objects", ref_git))) {
221 char *ref_git_git = xstrdup(mkpath("%s/.git", ref_git));
222 free(ref_git);
223 ref_git = ref_git_git;
224 } else if (!is_directory(mkpath("%s/objects", ref_git)))
e84d7b74 225 die(_("reference repository '%s' is not a local directory."),
dbc92b07 226 item->string);
8434c2f1 227
e6baf4a1
JH
228 strbuf_addf(&alternate, "%s/objects", ref_git);
229 add_to_alternates_file(alternate.buf);
230 strbuf_release(&alternate);
8434c2f1 231
e6baf4a1
JH
232 remote = remote_get(ref_git);
233 transport = transport_get(remote, ref_git);
8434c2f1
DB
234 for (extra = transport_get_remote_refs(transport); extra;
235 extra = extra->next)
236 add_extra_ref(extra->name, extra->old_sha1, 0);
237
238 transport_disconnect(transport);
e6baf4a1 239 free(ref_git);
dbc92b07
JH
240 return 0;
241}
242
243static void setup_reference(void)
244{
245 for_each_string_list(&option_reference, add_one_reference, NULL);
8434c2f1
DB
246}
247
e6baf4a1
JH
248static void copy_alternates(struct strbuf *src, struct strbuf *dst,
249 const char *src_repo)
250{
251 /*
252 * Read from the source objects/info/alternates file
253 * and copy the entries to corresponding file in the
254 * destination repository with add_to_alternates_file().
255 * Both src and dst have "$path/objects/info/alternates".
256 *
257 * Instead of copying bit-for-bit from the original,
258 * we need to append to existing one so that the already
259 * created entry via "clone -s" is not lost, and also
260 * to turn entries with paths relative to the original
261 * absolute, so that they can be used in the new repository.
262 */
263 FILE *in = fopen(src->buf, "r");
264 struct strbuf line = STRBUF_INIT;
265
266 while (strbuf_getline(&line, in, '\n') != EOF) {
267 char *abs_path, abs_buf[PATH_MAX];
268 if (!line.len || line.buf[0] == '#')
269 continue;
270 if (is_absolute_path(line.buf)) {
271 add_to_alternates_file(line.buf);
272 continue;
273 }
274 abs_path = mkpath("%s/objects/%s", src_repo, line.buf);
275 normalize_path_copy(abs_buf, abs_path);
276 add_to_alternates_file(abs_buf);
277 }
278 strbuf_release(&line);
279 fclose(in);
280}
281
282static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
283 const char *src_repo, int src_baselen)
8434c2f1
DB
284{
285 struct dirent *de;
286 struct stat buf;
287 int src_len, dest_len;
288 DIR *dir;
289
b9e125e0 290 dir = opendir(src->buf);
8434c2f1 291 if (!dir)
e84d7b74 292 die_errno(_("failed to open '%s'"), src->buf);
8434c2f1 293
b9e125e0 294 if (mkdir(dest->buf, 0777)) {
8434c2f1 295 if (errno != EEXIST)
e84d7b74 296 die_errno(_("failed to create directory '%s'"), dest->buf);
b9e125e0 297 else if (stat(dest->buf, &buf))
e84d7b74 298 die_errno(_("failed to stat '%s'"), dest->buf);
8434c2f1 299 else if (!S_ISDIR(buf.st_mode))
e84d7b74 300 die(_("%s exists and is not a directory"), dest->buf);
8434c2f1
DB
301 }
302
b9e125e0
MV
303 strbuf_addch(src, '/');
304 src_len = src->len;
305 strbuf_addch(dest, '/');
306 dest_len = dest->len;
8434c2f1
DB
307
308 while ((de = readdir(dir)) != NULL) {
b9e125e0
MV
309 strbuf_setlen(src, src_len);
310 strbuf_addstr(src, de->d_name);
311 strbuf_setlen(dest, dest_len);
312 strbuf_addstr(dest, de->d_name);
313 if (stat(src->buf, &buf)) {
e84d7b74 314 warning (_("failed to stat %s\n"), src->buf);
8434c2f1
DB
315 continue;
316 }
317 if (S_ISDIR(buf.st_mode)) {
318 if (de->d_name[0] != '.')
e6baf4a1
JH
319 copy_or_link_directory(src, dest,
320 src_repo, src_baselen);
321 continue;
322 }
323
324 /* Files that cannot be copied bit-for-bit... */
325 if (!strcmp(src->buf + src_baselen, "/info/alternates")) {
326 copy_alternates(src, dest, src_repo);
8434c2f1
DB
327 continue;
328 }
329
b9e125e0 330 if (unlink(dest->buf) && errno != ENOENT)
e84d7b74 331 die_errno(_("failed to unlink '%s'"), dest->buf);
fdabc242 332 if (!option_no_hardlinks) {
b9e125e0 333 if (!link(src->buf, dest->buf))
fdabc242
DB
334 continue;
335 if (option_local)
e84d7b74 336 die_errno(_("failed to create link '%s'"), dest->buf);
fdabc242 337 option_no_hardlinks = 1;
8434c2f1 338 }
f7835a25 339 if (copy_file_with_time(dest->buf, src->buf, 0666))
e84d7b74 340 die_errno(_("failed to copy file to '%s'"), dest->buf);
8434c2f1 341 }
689ef4d4 342 closedir(dir);
8434c2f1
DB
343}
344
345static const struct ref *clone_local(const char *src_repo,
346 const char *dest_repo)
347{
348 const struct ref *ret;
8434c2f1
DB
349 struct remote *remote;
350 struct transport *transport;
351
e6baf4a1
JH
352 if (option_shared) {
353 struct strbuf alt = STRBUF_INIT;
354 strbuf_addf(&alt, "%s/objects", src_repo);
355 add_to_alternates_file(alt.buf);
356 strbuf_release(&alt);
357 } else {
358 struct strbuf src = STRBUF_INIT;
359 struct strbuf dest = STRBUF_INIT;
b9e125e0
MV
360 strbuf_addf(&src, "%s/objects", src_repo);
361 strbuf_addf(&dest, "%s/objects", dest_repo);
e6baf4a1 362 copy_or_link_directory(&src, &dest, src_repo, src.len);
b9e125e0
MV
363 strbuf_release(&src);
364 strbuf_release(&dest);
8434c2f1
DB
365 }
366
367 remote = remote_get(src_repo);
368 transport = transport_get(remote, src_repo);
369 ret = transport_get_remote_refs(transport);
370 transport_disconnect(transport);
28ba96ab 371 if (0 <= option_verbosity)
e84d7b74 372 printf(_("done.\n"));
8434c2f1
DB
373 return ret;
374}
375
376static const char *junk_work_tree;
377static const char *junk_git_dir;
e161acd1 378static pid_t junk_pid;
8434c2f1
DB
379
380static void remove_junk(void)
381{
f285a2d7 382 struct strbuf sb = STRBUF_INIT;
8434c2f1
DB
383 if (getpid() != junk_pid)
384 return;
8434c2f1
DB
385 if (junk_git_dir) {
386 strbuf_addstr(&sb, junk_git_dir);
387 remove_dir_recursively(&sb, 0);
388 strbuf_reset(&sb);
389 }
390 if (junk_work_tree) {
391 strbuf_addstr(&sb, junk_work_tree);
392 remove_dir_recursively(&sb, 0);
393 strbuf_reset(&sb);
394 }
395}
396
397static void remove_junk_on_signal(int signo)
398{
399 remove_junk();
4a16d072 400 sigchain_pop(signo);
8434c2f1
DB
401 raise(signo);
402}
403
5bdc32d3
NP
404static struct ref *wanted_peer_refs(const struct ref *refs,
405 struct refspec *refspec)
8434c2f1
DB
406{
407 struct ref *local_refs = NULL;
408 struct ref **tail = &local_refs;
8434c2f1
DB
409
410 get_fetch_map(refs, refspec, &tail, 0);
468386a9
JS
411 if (!option_mirror)
412 get_fetch_map(refs, tag_refspec, &tail, 0);
8434c2f1 413
5bdc32d3
NP
414 return local_refs;
415}
416
417static void write_remote_refs(const struct ref *local_refs)
418{
419 const struct ref *r;
420
8434c2f1 421 for (r = local_refs; r; r = r->next)
3e8aded2
JH
422 add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
423
424 pack_refs(PACK_REFS_ALL);
425 clear_extra_refs();
8434c2f1
DB
426}
427
428int cmd_clone(int argc, const char **argv, const char *prefix)
429{
24c61c44 430 int is_bundle = 0, is_local;
8434c2f1
DB
431 struct stat buf;
432 const char *repo_name, *repo, *work_tree, *git_dir;
433 char *path, *dir;
55892d23 434 int dest_exists;
37148311 435 const struct ref *refs, *remote_head;
7a4ee28f
JK
436 const struct ref *remote_head_points_at;
437 const struct ref *our_head_points_at;
37148311 438 struct ref *mapped_refs;
b5ff37ac
MV
439 struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
440 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
1db4a75c 441 struct transport *transport = NULL;
bc699afc 442 char *src_ref_prefix = "refs/heads/";
dfa7a6c5 443 int err = 0;
8434c2f1 444
689f0396
DB
445 struct refspec *refspec;
446 const char *fetch_pattern;
8434c2f1
DB
447
448 junk_pid = getpid();
449
bbc30f99 450 packet_trace_identity("clone");
37782920 451 argc = parse_options(argc, argv, prefix, builtin_clone_options,
8434c2f1
DB
452 builtin_clone_usage, 0);
453
d52dc4b1 454 if (argc > 2)
e84d7b74 455 usage_msg_opt(_("Too many arguments."),
d52dc4b1
JN
456 builtin_clone_usage, builtin_clone_options);
457
8434c2f1 458 if (argc == 0)
e84d7b74 459 usage_msg_opt(_("You must specify a repository to clone."),
d52dc4b1 460 builtin_clone_usage, builtin_clone_options);
8434c2f1 461
bc699afc
JS
462 if (option_mirror)
463 option_bare = 1;
464
8434c2f1
DB
465 if (option_bare) {
466 if (option_origin)
e84d7b74 467 die(_("--bare and --origin %s options are incompatible."),
8434c2f1
DB
468 option_origin);
469 option_no_checkout = 1;
8434c2f1
DB
470 }
471
472 if (!option_origin)
473 option_origin = "origin";
474
475 repo_name = argv[0];
476
477 path = get_repo_path(repo_name, &is_bundle);
478 if (path)
e2a57aac 479 repo = xstrdup(absolute_path(repo_name));
8434c2f1 480 else if (!strchr(repo_name, ':'))
d3e1ddfd 481 die(_("repository '%s' does not exist"), repo_name);
8434c2f1
DB
482 else
483 repo = repo_name;
24c61c44
NTND
484 is_local = path && !is_bundle;
485 if (is_local && option_depth)
e84d7b74 486 warning(_("--depth is ignored in local clones; use file:// instead."));
8434c2f1
DB
487
488 if (argc == 2)
489 dir = xstrdup(argv[1]);
490 else
6612f877 491 dir = guess_dir_name(repo_name, is_bundle, option_bare);
44a68fd5 492 strip_trailing_slashes(dir);
8434c2f1 493
55892d23
AP
494 dest_exists = !stat(dir, &buf);
495 if (dest_exists && !is_empty_dir(dir))
e84d7b74
ÆAB
496 die(_("destination path '%s' already exists and is not "
497 "an empty directory."), dir);
8434c2f1 498
8434c2f1
DB
499 strbuf_addf(&reflog_msg, "clone: from %s", repo);
500
501 if (option_bare)
502 work_tree = NULL;
503 else {
504 work_tree = getenv("GIT_WORK_TREE");
505 if (work_tree && !stat(work_tree, &buf))
e84d7b74 506 die(_("working tree '%s' already exists."), work_tree);
8434c2f1
DB
507 }
508
509 if (option_bare || work_tree)
510 git_dir = xstrdup(dir);
511 else {
512 work_tree = dir;
513 git_dir = xstrdup(mkpath("%s/.git", dir));
514 }
515
516 if (!option_bare) {
517 junk_work_tree = work_tree;
8e21d63b 518 if (safe_create_leading_directories_const(work_tree) < 0)
e84d7b74 519 die_errno(_("could not create leading directories of '%s'"),
d824cbba 520 work_tree);
55892d23 521 if (!dest_exists && mkdir(work_tree, 0755))
e84d7b74 522 die_errno(_("could not create work tree dir '%s'."),
d824cbba 523 work_tree);
8434c2f1
DB
524 set_git_work_tree(work_tree);
525 }
526 junk_git_dir = git_dir;
527 atexit(remove_junk);
57b235a4 528 sigchain_push_common(remove_junk_on_signal);
8434c2f1 529
50b5f420 530 setenv(CONFIG_ENVIRONMENT, mkpath("%s/config", git_dir), 1);
8434c2f1 531
8e21d63b 532 if (safe_create_leading_directories_const(git_dir) < 0)
e84d7b74 533 die(_("could not create leading directories of '%s'"), git_dir);
b57fb80a
NTND
534
535 set_git_dir_init(git_dir, real_git_dir, 0);
536 if (real_git_dir)
537 git_dir = real_git_dir;
8434c2f1 538
3781fcce
ÆAB
539 if (0 <= option_verbosity) {
540 if (option_bare)
5cde5989 541 printf(_("Cloning into bare repository %s...\n"), dir);
3781fcce 542 else
5cde5989 543 printf(_("Cloning into %s...\n"), dir);
3781fcce 544 }
28ba96ab 545 init_db(option_template, INIT_DB_QUIET);
8434c2f1 546
5b8063b5
JS
547 /*
548 * At this point, the config exists, so we do not need the
549 * environment variable. We actually need to unset it, too, to
550 * re-enable parsing of the global configs.
551 */
552 unsetenv(CONFIG_ENVIRONMENT);
553
9bd81e42 554 git_config(git_default_config, NULL);
8434c2f1
DB
555
556 if (option_bare) {
bc699afc
JS
557 if (option_mirror)
558 src_ref_prefix = "refs/";
b5ff37ac 559 strbuf_addstr(&branch_top, src_ref_prefix);
8434c2f1
DB
560
561 git_config_set("core.bare", "true");
562 } else {
b5ff37ac 563 strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
bc699afc 564 }
8434c2f1 565
689f0396
DB
566 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
567
bc699afc 568 if (option_mirror || !option_bare) {
8434c2f1 569 /* Configure the remote */
689f0396
DB
570 strbuf_addf(&key, "remote.%s.fetch", option_origin);
571 git_config_set_multivar(key.buf, value.buf, "^$", 0);
572 strbuf_reset(&key);
573
bc699afc 574 if (option_mirror) {
b5ff37ac
MV
575 strbuf_addf(&key, "remote.%s.mirror", option_origin);
576 git_config_set(key.buf, "true");
577 strbuf_reset(&key);
bc699afc 578 }
8434c2f1
DB
579 }
580
df61c889
SR
581 strbuf_addf(&key, "remote.%s.url", option_origin);
582 git_config_set(key.buf, repo);
583 strbuf_reset(&key);
584
dbc92b07
JH
585 if (option_reference.nr)
586 setup_reference();
766ac6a6 587
689f0396
DB
588 fetch_pattern = value.buf;
589 refspec = parse_fetch_refspec(1, &fetch_pattern);
590
591 strbuf_reset(&value);
8434c2f1 592
24c61c44 593 if (is_local) {
8434c2f1 594 refs = clone_local(path, git_dir);
5bdc32d3
NP
595 mapped_refs = wanted_peer_refs(refs, refspec);
596 } else {
766ac6a6 597 struct remote *remote = remote_get(option_origin);
1db4a75c 598 transport = transport_get(remote, remote->url[0]);
8434c2f1 599
37b78c25 600 if (!transport->get_refs_list || !transport->fetch)
e84d7b74 601 die(_("Don't know how to clone %s"), transport->url);
37b78c25 602
8434c2f1
DB
603 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
604
605 if (option_depth)
606 transport_set_option(transport, TRANS_OPT_DEPTH,
607 option_depth);
608
d01b3c02 609 transport_set_verbosity(transport, option_verbosity, option_progress);
8434c2f1 610
837c8767
SH
611 if (option_upload_pack)
612 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
613 option_upload_pack);
614
8434c2f1 615 refs = transport_get_remote_refs(transport);
5bdc32d3
NP
616 if (refs) {
617 mapped_refs = wanted_peer_refs(refs, refspec);
618 transport_fetch_refs(transport, mapped_refs);
619 }
8434c2f1
DB
620 }
621
86ac7518
SR
622 if (refs) {
623 clear_extra_refs();
8434c2f1 624
5bdc32d3 625 write_remote_refs(mapped_refs);
8434c2f1 626
6cb4e6cc 627 remote_head = find_ref_by_name(refs, "HEAD");
7a4ee28f
JK
628 remote_head_points_at =
629 guess_remote_head(remote_head, mapped_refs, 0);
630
631 if (option_branch) {
632 struct strbuf head = STRBUF_INIT;
633 strbuf_addstr(&head, src_ref_prefix);
634 strbuf_addstr(&head, option_branch);
635 our_head_points_at =
636 find_ref_by_name(mapped_refs, head.buf);
637 strbuf_release(&head);
638
639 if (!our_head_points_at) {
e84d7b74
ÆAB
640 warning(_("Remote branch %s not found in "
641 "upstream %s, using HEAD instead"),
7a4ee28f
JK
642 option_branch, option_origin);
643 our_head_points_at = remote_head_points_at;
644 }
645 }
646 else
647 our_head_points_at = remote_head_points_at;
86ac7518
SR
648 }
649 else {
e84d7b74 650 warning(_("You appear to have cloned an empty repository."));
7a4ee28f
JK
651 our_head_points_at = NULL;
652 remote_head_points_at = NULL;
86ac7518
SR
653 remote_head = NULL;
654 option_no_checkout = 1;
5cd12b85 655 if (!option_bare)
a9f2c136 656 install_branch_config(0, "master", option_origin,
5cd12b85 657 "refs/heads/master");
86ac7518 658 }
8434c2f1 659
7a4ee28f
JK
660 if (remote_head_points_at && !option_bare) {
661 struct strbuf head_ref = STRBUF_INIT;
662 strbuf_addstr(&head_ref, branch_top.buf);
663 strbuf_addstr(&head_ref, "HEAD");
664 create_symref(head_ref.buf,
665 remote_head_points_at->peer_ref->name,
666 reflog_msg.buf);
667 }
8434c2f1 668
7a4ee28f
JK
669 if (our_head_points_at) {
670 /* Local default branch link */
671 create_symref("HEAD", our_head_points_at->name, NULL);
8434c2f1 672 if (!option_bare) {
7a4ee28f
JK
673 const char *head = skip_prefix(our_head_points_at->name,
674 "refs/heads/");
8434c2f1 675 update_ref(reflog_msg.buf, "HEAD",
7a4ee28f 676 our_head_points_at->old_sha1,
8434c2f1 677 NULL, 0, DIE_ON_ERR);
a9f2c136 678 install_branch_config(0, head, option_origin,
7a4ee28f 679 our_head_points_at->name);
8434c2f1
DB
680 }
681 } else if (remote_head) {
682 /* Source had detached HEAD pointing somewhere. */
7a4ee28f 683 if (!option_bare) {
8434c2f1
DB
684 update_ref(reflog_msg.buf, "HEAD",
685 remote_head->old_sha1,
686 NULL, REF_NODEREF, DIE_ON_ERR);
7a4ee28f
JK
687 our_head_points_at = remote_head;
688 }
8434c2f1
DB
689 } else {
690 /* Nothing to checkout out */
691 if (!option_no_checkout)
e84d7b74
ÆAB
692 warning(_("remote HEAD refers to nonexistent ref, "
693 "unable to checkout.\n"));
8434c2f1
DB
694 option_no_checkout = 1;
695 }
696
12d49966 697 if (transport) {
1db4a75c 698 transport_unlock_pack(transport);
12d49966
JK
699 transport_disconnect(transport);
700 }
1db4a75c 701
8434c2f1
DB
702 if (!option_no_checkout) {
703 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
704 struct unpack_trees_options opts;
705 struct tree *tree;
706 struct tree_desc t;
707 int fd;
708
709 /* We need to be in the new work tree for the checkout */
710 setup_work_tree();
711
712 fd = hold_locked_index(lock_file, 1);
713
714 memset(&opts, 0, sizeof opts);
715 opts.update = 1;
a73bc127
JS
716 opts.merge = 1;
717 opts.fn = oneway_merge;
5bd631b3 718 opts.verbose_update = (option_verbosity > 0);
a73bc127 719 opts.src_index = &the_index;
8434c2f1
DB
720 opts.dst_index = &the_index;
721
7a4ee28f 722 tree = parse_tree_indirect(our_head_points_at->old_sha1);
8434c2f1
DB
723 parse_tree(tree);
724 init_tree_desc(&t, tree->buffer, tree->size);
725 unpack_trees(1, &t, &opts);
726
727 if (write_cache(fd, active_cache, active_nr) ||
728 commit_locked_index(lock_file))
e84d7b74 729 die(_("unable to write new index file"));
dfa7a6c5
JK
730
731 err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1),
d01a8e32
BS
732 sha1_to_hex(our_head_points_at->old_sha1), "1",
733 NULL);
e7fed18a
JH
734
735 if (!err && option_recursive)
736 err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
8434c2f1
DB
737 }
738
739 strbuf_release(&reflog_msg);
b5ff37ac
MV
740 strbuf_release(&branch_top);
741 strbuf_release(&key);
742 strbuf_release(&value);
8434c2f1 743 junk_pid = 0;
dfa7a6c5 744 return err;
8434c2f1 745}