Merge branch 'ps/guess-repo-name-at-root'
[git/mjg.git] / builtin / clone.c
blob5169746b64888b3f8cf9d4fabd6ea52a45fff4d3
1 /*
2 * Builtin "git clone"
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
8 * Clone a repository into a different directory that does not yet exist.
9 */
11 #include "builtin.h"
12 #include "lockfile.h"
13 #include "parse-options.h"
14 #include "fetch-pack.h"
15 #include "refs.h"
16 #include "tree.h"
17 #include "tree-walk.h"
18 #include "unpack-trees.h"
19 #include "transport.h"
20 #include "strbuf.h"
21 #include "dir.h"
22 #include "sigchain.h"
23 #include "branch.h"
24 #include "remote.h"
25 #include "run-command.h"
26 #include "connected.h"
29 * Overall FIXMEs:
30 * - respect DB_ENVIRONMENT for .git/objects.
32 * Implementation notes:
33 * - dropping use-separate-remote and no-separate-remote compatibility
36 static const char * const builtin_clone_usage[] = {
37 N_("git clone [<options>] [--] <repo> [<dir>]"),
38 NULL
41 static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
42 static int option_local = -1, option_no_hardlinks, option_shared, option_recursive;
43 static char *option_template, *option_depth;
44 static char *option_origin = NULL;
45 static char *option_branch = NULL;
46 static const char *real_git_dir;
47 static char *option_upload_pack = "git-upload-pack";
48 static int option_verbosity;
49 static int option_progress = -1;
50 static struct string_list option_config;
51 static struct string_list option_reference;
52 static int option_dissociate;
54 static struct option builtin_clone_options[] = {
55 OPT__VERBOSITY(&option_verbosity),
56 OPT_BOOL(0, "progress", &option_progress,
57 N_("force progress reporting")),
58 OPT_BOOL('n', "no-checkout", &option_no_checkout,
59 N_("don't create a checkout")),
60 OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
61 OPT_HIDDEN_BOOL(0, "naked", &option_bare,
62 N_("create a bare repository")),
63 OPT_BOOL(0, "mirror", &option_mirror,
64 N_("create a mirror repository (implies bare)")),
65 OPT_BOOL('l', "local", &option_local,
66 N_("to clone from a local repository")),
67 OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
68 N_("don't use local hardlinks, always copy")),
69 OPT_BOOL('s', "shared", &option_shared,
70 N_("setup as shared repository")),
71 OPT_BOOL(0, "recursive", &option_recursive,
72 N_("initialize submodules in the clone")),
73 OPT_BOOL(0, "recurse-submodules", &option_recursive,
74 N_("initialize submodules in the clone")),
75 OPT_STRING(0, "template", &option_template, N_("template-directory"),
76 N_("directory from which templates will be used")),
77 OPT_STRING_LIST(0, "reference", &option_reference, N_("repo"),
78 N_("reference repository")),
79 OPT_BOOL(0, "dissociate", &option_dissociate,
80 N_("use --reference only while cloning")),
81 OPT_STRING('o', "origin", &option_origin, N_("name"),
82 N_("use <name> instead of 'origin' to track upstream")),
83 OPT_STRING('b', "branch", &option_branch, N_("branch"),
84 N_("checkout <branch> instead of the remote's HEAD")),
85 OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
86 N_("path to git-upload-pack on the remote")),
87 OPT_STRING(0, "depth", &option_depth, N_("depth"),
88 N_("create a shallow clone of that depth")),
89 OPT_BOOL(0, "single-branch", &option_single_branch,
90 N_("clone only one branch, HEAD or --branch")),
91 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
92 N_("separate git dir from working tree")),
93 OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
94 N_("set config inside the new repository")),
95 OPT_END()
98 static const char *argv_submodule[] = {
99 "submodule", "update", "--init", "--recursive", NULL
102 static char *get_repo_path(const char *repo, int *is_bundle)
104 static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
105 static char *bundle_suffix[] = { ".bundle", "" };
106 struct stat st;
107 int i;
109 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
110 const char *path;
111 path = mkpath("%s%s", repo, suffix[i]);
112 if (stat(path, &st))
113 continue;
114 if (S_ISDIR(st.st_mode) && is_git_directory(path)) {
115 *is_bundle = 0;
116 return xstrdup(absolute_path(path));
117 } else if (S_ISREG(st.st_mode) && st.st_size > 8) {
118 /* Is it a "gitfile"? */
119 char signature[8];
120 int len, fd = open(path, O_RDONLY);
121 if (fd < 0)
122 continue;
123 len = read_in_full(fd, signature, 8);
124 close(fd);
125 if (len != 8 || strncmp(signature, "gitdir: ", 8))
126 continue;
127 path = read_gitfile(path);
128 if (path) {
129 *is_bundle = 0;
130 return xstrdup(absolute_path(path));
135 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
136 const char *path;
137 path = mkpath("%s%s", repo, bundle_suffix[i]);
138 if (!stat(path, &st) && S_ISREG(st.st_mode)) {
139 *is_bundle = 1;
140 return xstrdup(absolute_path(path));
144 return NULL;
147 static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
149 const char *end = repo + strlen(repo), *start, *ptr;
150 size_t len;
151 char *dir;
154 * Skip scheme.
156 start = strstr(repo, "://");
157 if (start == NULL)
158 start = repo;
159 else
160 start += 3;
163 * Skip authentication data. The stripping does happen
164 * greedily, such that we strip up to the last '@' inside
165 * the host part.
167 for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) {
168 if (*ptr == '@')
169 start = ptr + 1;
173 * Strip trailing spaces, slashes and /.git
175 while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
176 end--;
177 if (end - start > 5 && is_dir_sep(end[-5]) &&
178 !strncmp(end - 4, ".git", 4)) {
179 end -= 5;
180 while (start < end && is_dir_sep(end[-1]))
181 end--;
185 * Strip trailing port number if we've got only a
186 * hostname (that is, there is no dir separator but a
187 * colon). This check is required such that we do not
188 * strip URI's like '/foo/bar:2222.git', which should
189 * result in a dir '2222' being guessed due to backwards
190 * compatibility.
192 if (memchr(start, '/', end - start) == NULL
193 && memchr(start, ':', end - start) != NULL) {
194 ptr = end;
195 while (start < ptr && isdigit(ptr[-1]) && ptr[-1] != ':')
196 ptr--;
197 if (start < ptr && ptr[-1] == ':')
198 end = ptr - 1;
202 * Find last component. To remain backwards compatible we
203 * also regard colons as path separators, such that
204 * cloning a repository 'foo:bar.git' would result in a
205 * directory 'bar' being guessed.
207 ptr = end;
208 while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':')
209 ptr--;
210 start = ptr;
213 * Strip .{bundle,git}.
215 len = end - start;
216 strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git");
218 if (!len || (len == 1 && *start == '/'))
219 die("No directory name could be guessed.\n"
220 "Please specify a directory on the command line");
222 if (is_bare)
223 dir = xstrfmt("%.*s.git", (int)len, start);
224 else
225 dir = xstrndup(start, len);
227 * Replace sequences of 'control' characters and whitespace
228 * with one ascii space, remove leading and trailing spaces.
230 if (*dir) {
231 char *out = dir;
232 int prev_space = 1 /* strip leading whitespace */;
233 for (end = dir; *end; ++end) {
234 char ch = *end;
235 if ((unsigned char)ch < '\x20')
236 ch = '\x20';
237 if (isspace(ch)) {
238 if (prev_space)
239 continue;
240 prev_space = 1;
241 } else
242 prev_space = 0;
243 *out++ = ch;
245 *out = '\0';
246 if (out > dir && prev_space)
247 out[-1] = '\0';
249 return dir;
252 static void strip_trailing_slashes(char *dir)
254 char *end = dir + strlen(dir);
256 while (dir < end - 1 && is_dir_sep(end[-1]))
257 end--;
258 *end = '\0';
261 static int add_one_reference(struct string_list_item *item, void *cb_data)
263 char *ref_git;
264 const char *repo;
265 struct strbuf alternate = STRBUF_INIT;
267 /* Beware: read_gitfile(), real_path() and mkpath() return static buffer */
268 ref_git = xstrdup(real_path(item->string));
270 repo = read_gitfile(ref_git);
271 if (!repo)
272 repo = read_gitfile(mkpath("%s/.git", ref_git));
273 if (repo) {
274 free(ref_git);
275 ref_git = xstrdup(repo);
278 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
279 char *ref_git_git = mkpathdup("%s/.git", ref_git);
280 free(ref_git);
281 ref_git = ref_git_git;
282 } else if (!is_directory(mkpath("%s/objects", ref_git)))
283 die(_("reference repository '%s' is not a local repository."),
284 item->string);
286 if (!access(mkpath("%s/shallow", ref_git), F_OK))
287 die(_("reference repository '%s' is shallow"), item->string);
289 if (!access(mkpath("%s/info/grafts", ref_git), F_OK))
290 die(_("reference repository '%s' is grafted"), item->string);
292 strbuf_addf(&alternate, "%s/objects", ref_git);
293 add_to_alternates_file(alternate.buf);
294 strbuf_release(&alternate);
295 free(ref_git);
296 return 0;
299 static void setup_reference(void)
301 for_each_string_list(&option_reference, add_one_reference, NULL);
304 static void copy_alternates(struct strbuf *src, struct strbuf *dst,
305 const char *src_repo)
308 * Read from the source objects/info/alternates file
309 * and copy the entries to corresponding file in the
310 * destination repository with add_to_alternates_file().
311 * Both src and dst have "$path/objects/info/alternates".
313 * Instead of copying bit-for-bit from the original,
314 * we need to append to existing one so that the already
315 * created entry via "clone -s" is not lost, and also
316 * to turn entries with paths relative to the original
317 * absolute, so that they can be used in the new repository.
319 FILE *in = fopen(src->buf, "r");
320 struct strbuf line = STRBUF_INIT;
322 while (strbuf_getline(&line, in, '\n') != EOF) {
323 char *abs_path;
324 if (!line.len || line.buf[0] == '#')
325 continue;
326 if (is_absolute_path(line.buf)) {
327 add_to_alternates_file(line.buf);
328 continue;
330 abs_path = mkpathdup("%s/objects/%s", src_repo, line.buf);
331 normalize_path_copy(abs_path, abs_path);
332 add_to_alternates_file(abs_path);
333 free(abs_path);
335 strbuf_release(&line);
336 fclose(in);
339 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
340 const char *src_repo, int src_baselen)
342 struct dirent *de;
343 struct stat buf;
344 int src_len, dest_len;
345 DIR *dir;
347 dir = opendir(src->buf);
348 if (!dir)
349 die_errno(_("failed to open '%s'"), src->buf);
351 if (mkdir(dest->buf, 0777)) {
352 if (errno != EEXIST)
353 die_errno(_("failed to create directory '%s'"), dest->buf);
354 else if (stat(dest->buf, &buf))
355 die_errno(_("failed to stat '%s'"), dest->buf);
356 else if (!S_ISDIR(buf.st_mode))
357 die(_("%s exists and is not a directory"), dest->buf);
360 strbuf_addch(src, '/');
361 src_len = src->len;
362 strbuf_addch(dest, '/');
363 dest_len = dest->len;
365 while ((de = readdir(dir)) != NULL) {
366 strbuf_setlen(src, src_len);
367 strbuf_addstr(src, de->d_name);
368 strbuf_setlen(dest, dest_len);
369 strbuf_addstr(dest, de->d_name);
370 if (stat(src->buf, &buf)) {
371 warning (_("failed to stat %s\n"), src->buf);
372 continue;
374 if (S_ISDIR(buf.st_mode)) {
375 if (de->d_name[0] != '.')
376 copy_or_link_directory(src, dest,
377 src_repo, src_baselen);
378 continue;
381 /* Files that cannot be copied bit-for-bit... */
382 if (!strcmp(src->buf + src_baselen, "/info/alternates")) {
383 copy_alternates(src, dest, src_repo);
384 continue;
387 if (unlink(dest->buf) && errno != ENOENT)
388 die_errno(_("failed to unlink '%s'"), dest->buf);
389 if (!option_no_hardlinks) {
390 if (!link(src->buf, dest->buf))
391 continue;
392 if (option_local > 0)
393 die_errno(_("failed to create link '%s'"), dest->buf);
394 option_no_hardlinks = 1;
396 if (copy_file_with_time(dest->buf, src->buf, 0666))
397 die_errno(_("failed to copy file to '%s'"), dest->buf);
399 closedir(dir);
402 static void clone_local(const char *src_repo, const char *dest_repo)
404 if (option_shared) {
405 struct strbuf alt = STRBUF_INIT;
406 strbuf_addf(&alt, "%s/objects", src_repo);
407 add_to_alternates_file(alt.buf);
408 strbuf_release(&alt);
409 } else {
410 struct strbuf src = STRBUF_INIT;
411 struct strbuf dest = STRBUF_INIT;
412 strbuf_addf(&src, "%s/objects", src_repo);
413 strbuf_addf(&dest, "%s/objects", dest_repo);
414 copy_or_link_directory(&src, &dest, src_repo, src.len);
415 strbuf_release(&src);
416 strbuf_release(&dest);
419 if (0 <= option_verbosity)
420 fprintf(stderr, _("done.\n"));
423 static const char *junk_work_tree;
424 static const char *junk_git_dir;
425 static enum {
426 JUNK_LEAVE_NONE,
427 JUNK_LEAVE_REPO,
428 JUNK_LEAVE_ALL
429 } junk_mode = JUNK_LEAVE_NONE;
431 static const char junk_leave_repo_msg[] =
432 N_("Clone succeeded, but checkout failed.\n"
433 "You can inspect what was checked out with 'git status'\n"
434 "and retry the checkout with 'git checkout -f HEAD'\n");
436 static void remove_junk(void)
438 struct strbuf sb = STRBUF_INIT;
440 switch (junk_mode) {
441 case JUNK_LEAVE_REPO:
442 warning("%s", _(junk_leave_repo_msg));
443 /* fall-through */
444 case JUNK_LEAVE_ALL:
445 return;
446 default:
447 /* proceed to removal */
448 break;
451 if (junk_git_dir) {
452 strbuf_addstr(&sb, junk_git_dir);
453 remove_dir_recursively(&sb, 0);
454 strbuf_reset(&sb);
456 if (junk_work_tree) {
457 strbuf_addstr(&sb, junk_work_tree);
458 remove_dir_recursively(&sb, 0);
459 strbuf_reset(&sb);
463 static void remove_junk_on_signal(int signo)
465 remove_junk();
466 sigchain_pop(signo);
467 raise(signo);
470 static struct ref *find_remote_branch(const struct ref *refs, const char *branch)
472 struct ref *ref;
473 struct strbuf head = STRBUF_INIT;
474 strbuf_addstr(&head, "refs/heads/");
475 strbuf_addstr(&head, branch);
476 ref = find_ref_by_name(refs, head.buf);
477 strbuf_release(&head);
479 if (ref)
480 return ref;
482 strbuf_addstr(&head, "refs/tags/");
483 strbuf_addstr(&head, branch);
484 ref = find_ref_by_name(refs, head.buf);
485 strbuf_release(&head);
487 return ref;
490 static struct ref *wanted_peer_refs(const struct ref *refs,
491 struct refspec *refspec)
493 struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD"));
494 struct ref *local_refs = head;
495 struct ref **tail = head ? &head->next : &local_refs;
497 if (option_single_branch) {
498 struct ref *remote_head = NULL;
500 if (!option_branch)
501 remote_head = guess_remote_head(head, refs, 0);
502 else {
503 local_refs = NULL;
504 tail = &local_refs;
505 remote_head = copy_ref(find_remote_branch(refs, option_branch));
508 if (!remote_head && option_branch)
509 warning(_("Could not find remote branch %s to clone."),
510 option_branch);
511 else {
512 get_fetch_map(remote_head, refspec, &tail, 0);
514 /* if --branch=tag, pull the requested tag explicitly */
515 get_fetch_map(remote_head, tag_refspec, &tail, 0);
517 } else
518 get_fetch_map(refs, refspec, &tail, 0);
520 if (!option_mirror && !option_single_branch)
521 get_fetch_map(refs, tag_refspec, &tail, 0);
523 return local_refs;
526 static void write_remote_refs(const struct ref *local_refs)
528 const struct ref *r;
530 struct ref_transaction *t;
531 struct strbuf err = STRBUF_INIT;
533 t = ref_transaction_begin(&err);
534 if (!t)
535 die("%s", err.buf);
537 for (r = local_refs; r; r = r->next) {
538 if (!r->peer_ref)
539 continue;
540 if (ref_transaction_create(t, r->peer_ref->name, r->old_sha1,
541 0, NULL, &err))
542 die("%s", err.buf);
545 if (initial_ref_transaction_commit(t, &err))
546 die("%s", err.buf);
548 strbuf_release(&err);
549 ref_transaction_free(t);
552 static void write_followtags(const struct ref *refs, const char *msg)
554 const struct ref *ref;
555 for (ref = refs; ref; ref = ref->next) {
556 if (!starts_with(ref->name, "refs/tags/"))
557 continue;
558 if (ends_with(ref->name, "^{}"))
559 continue;
560 if (!has_sha1_file(ref->old_sha1))
561 continue;
562 update_ref(msg, ref->name, ref->old_sha1,
563 NULL, 0, UPDATE_REFS_DIE_ON_ERR);
567 static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
569 struct ref **rm = cb_data;
570 struct ref *ref = *rm;
573 * Skip anything missing a peer_ref, which we are not
574 * actually going to write a ref for.
576 while (ref && !ref->peer_ref)
577 ref = ref->next;
578 /* Returning -1 notes "end of list" to the caller. */
579 if (!ref)
580 return -1;
582 hashcpy(sha1, ref->old_sha1);
583 *rm = ref->next;
584 return 0;
587 static void update_remote_refs(const struct ref *refs,
588 const struct ref *mapped_refs,
589 const struct ref *remote_head_points_at,
590 const char *branch_top,
591 const char *msg,
592 struct transport *transport,
593 int check_connectivity)
595 const struct ref *rm = mapped_refs;
597 if (check_connectivity) {
598 if (transport->progress)
599 fprintf(stderr, _("Checking connectivity... "));
600 if (check_everything_connected_with_transport(iterate_ref_map,
601 0, &rm, transport))
602 die(_("remote did not send all necessary objects"));
603 if (transport->progress)
604 fprintf(stderr, _("done.\n"));
607 if (refs) {
608 write_remote_refs(mapped_refs);
609 if (option_single_branch)
610 write_followtags(refs, msg);
613 if (remote_head_points_at && !option_bare) {
614 struct strbuf head_ref = STRBUF_INIT;
615 strbuf_addstr(&head_ref, branch_top);
616 strbuf_addstr(&head_ref, "HEAD");
617 create_symref(head_ref.buf,
618 remote_head_points_at->peer_ref->name,
619 msg);
623 static void update_head(const struct ref *our, const struct ref *remote,
624 const char *msg)
626 const char *head;
627 if (our && skip_prefix(our->name, "refs/heads/", &head)) {
628 /* Local default branch link */
629 create_symref("HEAD", our->name, NULL);
630 if (!option_bare) {
631 update_ref(msg, "HEAD", our->old_sha1, NULL, 0,
632 UPDATE_REFS_DIE_ON_ERR);
633 install_branch_config(0, head, option_origin, our->name);
635 } else if (our) {
636 struct commit *c = lookup_commit_reference(our->old_sha1);
637 /* --branch specifies a non-branch (i.e. tags), detach HEAD */
638 update_ref(msg, "HEAD", c->object.sha1,
639 NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
640 } else if (remote) {
642 * We know remote HEAD points to a non-branch, or
643 * HEAD points to a branch but we don't know which one.
644 * Detach HEAD in all these cases.
646 update_ref(msg, "HEAD", remote->old_sha1,
647 NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
651 static int checkout(void)
653 unsigned char sha1[20];
654 char *head;
655 struct lock_file *lock_file;
656 struct unpack_trees_options opts;
657 struct tree *tree;
658 struct tree_desc t;
659 int err = 0;
661 if (option_no_checkout)
662 return 0;
664 head = resolve_refdup("HEAD", RESOLVE_REF_READING, sha1, NULL);
665 if (!head) {
666 warning(_("remote HEAD refers to nonexistent ref, "
667 "unable to checkout.\n"));
668 return 0;
670 if (!strcmp(head, "HEAD")) {
671 if (advice_detached_head)
672 detach_advice(sha1_to_hex(sha1));
673 } else {
674 if (!starts_with(head, "refs/heads/"))
675 die(_("HEAD not found below refs/heads!"));
677 free(head);
679 /* We need to be in the new work tree for the checkout */
680 setup_work_tree();
682 lock_file = xcalloc(1, sizeof(struct lock_file));
683 hold_locked_index(lock_file, 1);
685 memset(&opts, 0, sizeof opts);
686 opts.update = 1;
687 opts.merge = 1;
688 opts.fn = oneway_merge;
689 opts.verbose_update = (option_verbosity >= 0);
690 opts.src_index = &the_index;
691 opts.dst_index = &the_index;
693 tree = parse_tree_indirect(sha1);
694 parse_tree(tree);
695 init_tree_desc(&t, tree->buffer, tree->size);
696 if (unpack_trees(1, &t, &opts) < 0)
697 die(_("unable to checkout working tree"));
699 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
700 die(_("unable to write new index file"));
702 err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
703 sha1_to_hex(sha1), "1", NULL);
705 if (!err && option_recursive)
706 err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
708 return err;
711 static int write_one_config(const char *key, const char *value, void *data)
713 return git_config_set_multivar(key, value ? value : "true", "^$", 0);
716 static void write_config(struct string_list *config)
718 int i;
720 for (i = 0; i < config->nr; i++) {
721 if (git_config_parse_parameter(config->items[i].string,
722 write_one_config, NULL) < 0)
723 die("unable to write parameters to config file");
727 static void write_refspec_config(const char *src_ref_prefix,
728 const struct ref *our_head_points_at,
729 const struct ref *remote_head_points_at,
730 struct strbuf *branch_top)
732 struct strbuf key = STRBUF_INIT;
733 struct strbuf value = STRBUF_INIT;
735 if (option_mirror || !option_bare) {
736 if (option_single_branch && !option_mirror) {
737 if (option_branch) {
738 if (starts_with(our_head_points_at->name, "refs/tags/"))
739 strbuf_addf(&value, "+%s:%s", our_head_points_at->name,
740 our_head_points_at->name);
741 else
742 strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
743 branch_top->buf, option_branch);
744 } else if (remote_head_points_at) {
745 const char *head = remote_head_points_at->name;
746 if (!skip_prefix(head, "refs/heads/", &head))
747 die("BUG: remote HEAD points at non-head?");
749 strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name,
750 branch_top->buf, head);
753 * otherwise, the next "git fetch" will
754 * simply fetch from HEAD without updating
755 * any remote-tracking branch, which is what
756 * we want.
758 } else {
759 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf);
761 /* Configure the remote */
762 if (value.len) {
763 strbuf_addf(&key, "remote.%s.fetch", option_origin);
764 git_config_set_multivar(key.buf, value.buf, "^$", 0);
765 strbuf_reset(&key);
767 if (option_mirror) {
768 strbuf_addf(&key, "remote.%s.mirror", option_origin);
769 git_config_set(key.buf, "true");
770 strbuf_reset(&key);
775 strbuf_release(&key);
776 strbuf_release(&value);
779 static void dissociate_from_references(void)
781 static const char* argv[] = { "repack", "-a", "-d", NULL };
783 if (run_command_v_opt(argv, RUN_GIT_CMD|RUN_COMMAND_NO_STDIN))
784 die(_("cannot repack to clean up"));
785 if (unlink(git_path("objects/info/alternates")) && errno != ENOENT)
786 die_errno(_("cannot unlink temporary alternates file"));
789 int cmd_clone(int argc, const char **argv, const char *prefix)
791 int is_bundle = 0, is_local;
792 struct stat buf;
793 const char *repo_name, *repo, *work_tree, *git_dir;
794 char *path, *dir;
795 int dest_exists;
796 const struct ref *refs, *remote_head;
797 const struct ref *remote_head_points_at;
798 const struct ref *our_head_points_at;
799 struct ref *mapped_refs;
800 const struct ref *ref;
801 struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
802 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
803 struct transport *transport = NULL;
804 const char *src_ref_prefix = "refs/heads/";
805 struct remote *remote;
806 int err = 0, complete_refs_before_fetch = 1;
808 struct refspec *refspec;
809 const char *fetch_pattern;
811 packet_trace_identity("clone");
812 argc = parse_options(argc, argv, prefix, builtin_clone_options,
813 builtin_clone_usage, 0);
815 if (argc > 2)
816 usage_msg_opt(_("Too many arguments."),
817 builtin_clone_usage, builtin_clone_options);
819 if (argc == 0)
820 usage_msg_opt(_("You must specify a repository to clone."),
821 builtin_clone_usage, builtin_clone_options);
823 if (option_single_branch == -1)
824 option_single_branch = option_depth ? 1 : 0;
826 if (option_mirror)
827 option_bare = 1;
829 if (option_bare) {
830 if (option_origin)
831 die(_("--bare and --origin %s options are incompatible."),
832 option_origin);
833 if (real_git_dir)
834 die(_("--bare and --separate-git-dir are incompatible."));
835 option_no_checkout = 1;
838 if (!option_origin)
839 option_origin = "origin";
841 repo_name = argv[0];
843 path = get_repo_path(repo_name, &is_bundle);
844 if (path)
845 repo = xstrdup(absolute_path(repo_name));
846 else if (!strchr(repo_name, ':'))
847 die(_("repository '%s' does not exist"), repo_name);
848 else
849 repo = repo_name;
851 /* no need to be strict, transport_set_option() will validate it again */
852 if (option_depth && atoi(option_depth) < 1)
853 die(_("depth %s is not a positive number"), option_depth);
855 if (argc == 2)
856 dir = xstrdup(argv[1]);
857 else
858 dir = guess_dir_name(repo_name, is_bundle, option_bare);
859 strip_trailing_slashes(dir);
861 dest_exists = !stat(dir, &buf);
862 if (dest_exists && !is_empty_dir(dir))
863 die(_("destination path '%s' already exists and is not "
864 "an empty directory."), dir);
866 strbuf_addf(&reflog_msg, "clone: from %s", repo);
868 if (option_bare)
869 work_tree = NULL;
870 else {
871 work_tree = getenv("GIT_WORK_TREE");
872 if (work_tree && !stat(work_tree, &buf))
873 die(_("working tree '%s' already exists."), work_tree);
876 if (option_bare || work_tree)
877 git_dir = xstrdup(dir);
878 else {
879 work_tree = dir;
880 git_dir = mkpathdup("%s/.git", dir);
883 atexit(remove_junk);
884 sigchain_push_common(remove_junk_on_signal);
886 if (!option_bare) {
887 if (safe_create_leading_directories_const(work_tree) < 0)
888 die_errno(_("could not create leading directories of '%s'"),
889 work_tree);
890 if (!dest_exists && mkdir(work_tree, 0777))
891 die_errno(_("could not create work tree dir '%s'"),
892 work_tree);
893 junk_work_tree = work_tree;
894 set_git_work_tree(work_tree);
897 junk_git_dir = git_dir;
898 if (safe_create_leading_directories_const(git_dir) < 0)
899 die(_("could not create leading directories of '%s'"), git_dir);
901 set_git_dir_init(git_dir, real_git_dir, 0);
902 if (real_git_dir) {
903 git_dir = real_git_dir;
904 junk_git_dir = real_git_dir;
907 if (0 <= option_verbosity) {
908 if (option_bare)
909 fprintf(stderr, _("Cloning into bare repository '%s'...\n"), dir);
910 else
911 fprintf(stderr, _("Cloning into '%s'...\n"), dir);
913 init_db(option_template, INIT_DB_QUIET);
914 write_config(&option_config);
916 git_config(git_default_config, NULL);
918 if (option_bare) {
919 if (option_mirror)
920 src_ref_prefix = "refs/";
921 strbuf_addstr(&branch_top, src_ref_prefix);
923 git_config_set("core.bare", "true");
924 } else {
925 strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
928 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
929 strbuf_addf(&key, "remote.%s.url", option_origin);
930 git_config_set(key.buf, repo);
931 strbuf_reset(&key);
933 if (option_reference.nr)
934 setup_reference();
935 else if (option_dissociate) {
936 warning(_("--dissociate given, but there is no --reference"));
937 option_dissociate = 0;
940 fetch_pattern = value.buf;
941 refspec = parse_fetch_refspec(1, &fetch_pattern);
943 strbuf_reset(&value);
945 remote = remote_get(option_origin);
946 transport = transport_get(remote, remote->url[0]);
947 transport_set_verbosity(transport, option_verbosity, option_progress);
949 path = get_repo_path(remote->url[0], &is_bundle);
950 is_local = option_local != 0 && path && !is_bundle;
951 if (is_local) {
952 if (option_depth)
953 warning(_("--depth is ignored in local clones; use file:// instead."));
954 if (!access(mkpath("%s/shallow", path), F_OK)) {
955 if (option_local > 0)
956 warning(_("source repository is shallow, ignoring --local"));
957 is_local = 0;
960 if (option_local > 0 && !is_local)
961 warning(_("--local is ignored"));
962 transport->cloning = 1;
964 if (!transport->get_refs_list || (!is_local && !transport->fetch))
965 die(_("Don't know how to clone %s"), transport->url);
967 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
969 if (option_depth)
970 transport_set_option(transport, TRANS_OPT_DEPTH,
971 option_depth);
972 if (option_single_branch)
973 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
975 if (option_upload_pack)
976 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
977 option_upload_pack);
979 if (transport->smart_options && !option_depth)
980 transport->smart_options->check_self_contained_and_connected = 1;
982 refs = transport_get_remote_refs(transport);
984 if (refs) {
985 mapped_refs = wanted_peer_refs(refs, refspec);
987 * transport_get_remote_refs() may return refs with null sha-1
988 * in mapped_refs (see struct transport->get_refs_list
989 * comment). In that case we need fetch it early because
990 * remote_head code below relies on it.
992 * for normal clones, transport_get_remote_refs() should
993 * return reliable ref set, we can delay cloning until after
994 * remote HEAD check.
996 for (ref = refs; ref; ref = ref->next)
997 if (is_null_sha1(ref->old_sha1)) {
998 complete_refs_before_fetch = 0;
999 break;
1002 if (!is_local && !complete_refs_before_fetch)
1003 transport_fetch_refs(transport, mapped_refs);
1005 remote_head = find_ref_by_name(refs, "HEAD");
1006 remote_head_points_at =
1007 guess_remote_head(remote_head, mapped_refs, 0);
1009 if (option_branch) {
1010 our_head_points_at =
1011 find_remote_branch(mapped_refs, option_branch);
1013 if (!our_head_points_at)
1014 die(_("Remote branch %s not found in upstream %s"),
1015 option_branch, option_origin);
1017 else
1018 our_head_points_at = remote_head_points_at;
1020 else {
1021 if (option_branch)
1022 die(_("Remote branch %s not found in upstream %s"),
1023 option_branch, option_origin);
1025 warning(_("You appear to have cloned an empty repository."));
1026 mapped_refs = NULL;
1027 our_head_points_at = NULL;
1028 remote_head_points_at = NULL;
1029 remote_head = NULL;
1030 option_no_checkout = 1;
1031 if (!option_bare)
1032 install_branch_config(0, "master", option_origin,
1033 "refs/heads/master");
1036 write_refspec_config(src_ref_prefix, our_head_points_at,
1037 remote_head_points_at, &branch_top);
1039 if (is_local)
1040 clone_local(path, git_dir);
1041 else if (refs && complete_refs_before_fetch)
1042 transport_fetch_refs(transport, mapped_refs);
1044 update_remote_refs(refs, mapped_refs, remote_head_points_at,
1045 branch_top.buf, reflog_msg.buf, transport, !is_local);
1047 update_head(our_head_points_at, remote_head, reflog_msg.buf);
1049 transport_unlock_pack(transport);
1050 transport_disconnect(transport);
1052 if (option_dissociate)
1053 dissociate_from_references();
1055 junk_mode = JUNK_LEAVE_REPO;
1056 err = checkout();
1058 strbuf_release(&reflog_msg);
1059 strbuf_release(&branch_top);
1060 strbuf_release(&key);
1061 strbuf_release(&value);
1062 junk_mode = JUNK_LEAVE_ALL;
1064 free(refspec);
1065 return err;