submodule: Fix t7400, t7405, t7406 for msysGit
[git/dscho.git] / builtin / clone.c
blob7663bc22c9a3cb35b803815762791fec44d4c477
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 "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"
21 #include "pack-refs.h"
22 #include "sigchain.h"
23 #include "branch.h"
24 #include "remote.h"
25 #include "run-command.h"
28 * Overall FIXMEs:
29 * - respect DB_ENVIRONMENT for .git/objects.
31 * Implementation notes:
32 * - dropping use-separate-remote and no-separate-remote compatibility
35 static const char * const builtin_clone_usage[] = {
36 "git clone [options] [--] <repo> [<dir>]",
37 NULL
40 static int option_no_checkout, option_bare, option_mirror;
41 static int option_local, option_no_hardlinks, option_shared, option_recursive;
42 static char *option_template, *option_reference, *option_depth;
43 static char *option_origin = NULL;
44 static char *option_branch = NULL;
45 static const char *real_git_dir;
46 static char *option_upload_pack = "git-upload-pack";
47 static int option_verbosity;
48 static int option_progress;
49 static struct string_list option_config;
51 static struct option builtin_clone_options[] = {
52 OPT__VERBOSITY(&option_verbosity),
53 OPT_BOOLEAN(0, "progress", &option_progress,
54 "force progress reporting"),
55 OPT_BOOLEAN('n', "no-checkout", &option_no_checkout,
56 "don't create a checkout"),
57 OPT_BOOLEAN(0, "bare", &option_bare, "create a bare repository"),
58 { OPTION_BOOLEAN, 0, "naked", &option_bare, NULL,
59 "create a bare repository",
60 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
61 OPT_BOOLEAN(0, "mirror", &option_mirror,
62 "create a mirror repository (implies bare)"),
63 OPT_BOOLEAN('l', "local", &option_local,
64 "to clone from a local repository"),
65 OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks,
66 "don't use local hardlinks, always copy"),
67 OPT_BOOLEAN('s', "shared", &option_shared,
68 "setup as shared repository"),
69 OPT_BOOLEAN(0, "recursive", &option_recursive,
70 "initialize submodules in the clone"),
71 OPT_BOOLEAN(0, "recurse-submodules", &option_recursive,
72 "initialize submodules in the clone"),
73 OPT_STRING(0, "template", &option_template, "template-directory",
74 "directory from which templates will be used"),
75 OPT_STRING(0, "reference", &option_reference, "repo",
76 "reference repository"),
77 OPT_STRING('o', "origin", &option_origin, "branch",
78 "use <branch> instead of 'origin' to track upstream"),
79 OPT_STRING('b', "branch", &option_branch, "branch",
80 "checkout <branch> instead of the remote's HEAD"),
81 OPT_STRING('u', "upload-pack", &option_upload_pack, "path",
82 "path to git-upload-pack on the remote"),
83 OPT_STRING(0, "depth", &option_depth, "depth",
84 "create a shallow clone of that depth"),
85 OPT_STRING(0, "separate-git-dir", &real_git_dir, "gitdir",
86 "separate git dir from working tree"),
87 OPT_STRING_LIST('c', "config", &option_config, "key=value",
88 "set config inside the new repository"),
89 OPT_END()
92 static const char *argv_submodule[] = {
93 "submodule", "update", "--init", "--recursive", NULL
96 static char *get_repo_path(const char *repo, int *is_bundle)
98 static char *suffix[] = { "/.git", ".git", "" };
99 static char *bundle_suffix[] = { ".bundle", "" };
100 struct stat st;
101 int i;
103 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
104 const char *path;
105 path = mkpath("%s%s", repo, suffix[i]);
106 if (is_directory(path)) {
107 *is_bundle = 0;
108 return xstrdup(absolute_path(path));
112 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
113 const char *path;
114 path = mkpath("%s%s", repo, bundle_suffix[i]);
115 if (!stat(path, &st) && S_ISREG(st.st_mode)) {
116 *is_bundle = 1;
117 return xstrdup(absolute_path(path));
121 return NULL;
124 static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
126 const char *end = repo + strlen(repo), *start;
127 char *dir;
130 * Strip trailing spaces, slashes and /.git
132 while (repo < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
133 end--;
134 if (end - repo > 5 && is_dir_sep(end[-5]) &&
135 !strncmp(end - 4, ".git", 4)) {
136 end -= 5;
137 while (repo < end && is_dir_sep(end[-1]))
138 end--;
142 * Find last component, but be prepared that repo could have
143 * the form "remote.example.com:foo.git", i.e. no slash
144 * in the directory part.
146 start = end;
147 while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
148 start--;
151 * Strip .{bundle,git}.
153 if (is_bundle) {
154 if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
155 end -= 7;
156 } else {
157 if (end - start > 4 && !strncmp(end - 4, ".git", 4))
158 end -= 4;
161 if (is_bare) {
162 struct strbuf result = STRBUF_INIT;
163 strbuf_addf(&result, "%.*s.git", (int)(end - start), start);
164 dir = strbuf_detach(&result, NULL);
165 } else
166 dir = xstrndup(start, end - start);
168 * Replace sequences of 'control' characters and whitespace
169 * with one ascii space, remove leading and trailing spaces.
171 if (*dir) {
172 char *out = dir;
173 int prev_space = 1 /* strip leading whitespace */;
174 for (end = dir; *end; ++end) {
175 char ch = *end;
176 if ((unsigned char)ch < '\x20')
177 ch = '\x20';
178 if (isspace(ch)) {
179 if (prev_space)
180 continue;
181 prev_space = 1;
182 } else
183 prev_space = 0;
184 *out++ = ch;
186 *out = '\0';
187 if (out > dir && prev_space)
188 out[-1] = '\0';
190 return dir;
193 static void strip_trailing_slashes(char *dir)
195 char *end = dir + strlen(dir);
197 while (dir < end - 1 && is_dir_sep(end[-1]))
198 end--;
199 *end = '\0';
202 static void setup_reference(const char *repo)
204 const char *ref_git;
205 char *ref_git_copy;
207 struct remote *remote;
208 struct transport *transport;
209 const struct ref *extra;
211 ref_git = real_path(option_reference);
213 if (is_directory(mkpath("%s/.git/objects", ref_git)))
214 ref_git = mkpath("%s/.git", ref_git);
215 else if (!is_directory(mkpath("%s/objects", ref_git)))
216 die(_("reference repository '%s' is not a local directory."),
217 option_reference);
219 ref_git_copy = xstrdup(ref_git);
221 add_to_alternates_file(ref_git_copy);
223 remote = remote_get(ref_git_copy);
224 transport = transport_get(remote, ref_git_copy);
225 for (extra = transport_get_remote_refs(transport); extra;
226 extra = extra->next)
227 add_extra_ref(extra->name, extra->old_sha1, 0);
229 transport_disconnect(transport);
231 free(ref_git_copy);
234 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
236 struct dirent *de;
237 struct stat buf;
238 int src_len, dest_len;
239 DIR *dir;
241 dir = opendir(src->buf);
242 if (!dir)
243 die_errno(_("failed to open '%s'"), src->buf);
245 if (mkdir(dest->buf, 0777)) {
246 if (errno != EEXIST)
247 die_errno(_("failed to create directory '%s'"), dest->buf);
248 else if (stat(dest->buf, &buf))
249 die_errno(_("failed to stat '%s'"), dest->buf);
250 else if (!S_ISDIR(buf.st_mode))
251 die(_("%s exists and is not a directory"), dest->buf);
254 strbuf_addch(src, '/');
255 src_len = src->len;
256 strbuf_addch(dest, '/');
257 dest_len = dest->len;
259 while ((de = readdir(dir)) != NULL) {
260 strbuf_setlen(src, src_len);
261 strbuf_addstr(src, de->d_name);
262 strbuf_setlen(dest, dest_len);
263 strbuf_addstr(dest, de->d_name);
264 if (stat(src->buf, &buf)) {
265 warning (_("failed to stat %s\n"), src->buf);
266 continue;
268 if (S_ISDIR(buf.st_mode)) {
269 if (de->d_name[0] != '.')
270 copy_or_link_directory(src, dest);
271 continue;
274 if (unlink(dest->buf) && errno != ENOENT)
275 die_errno(_("failed to unlink '%s'"), dest->buf);
276 if (!option_no_hardlinks) {
277 if (!link(src->buf, dest->buf))
278 continue;
279 if (option_local)
280 die_errno(_("failed to create link '%s'"), dest->buf);
281 option_no_hardlinks = 1;
283 if (copy_file_with_time(dest->buf, src->buf, 0666))
284 die_errno(_("failed to copy file to '%s'"), dest->buf);
286 closedir(dir);
289 static const struct ref *clone_local(const char *src_repo,
290 const char *dest_repo)
292 const struct ref *ret;
293 struct strbuf src = STRBUF_INIT;
294 struct strbuf dest = STRBUF_INIT;
295 struct remote *remote;
296 struct transport *transport;
298 if (option_shared)
299 add_to_alternates_file(src_repo);
300 else {
301 strbuf_addf(&src, "%s/objects", src_repo);
302 strbuf_addf(&dest, "%s/objects", dest_repo);
303 copy_or_link_directory(&src, &dest);
304 strbuf_release(&src);
305 strbuf_release(&dest);
308 remote = remote_get(src_repo);
309 transport = transport_get(remote, src_repo);
310 ret = transport_get_remote_refs(transport);
311 transport_disconnect(transport);
312 if (0 <= option_verbosity)
313 printf(_("done.\n"));
314 return ret;
317 static const char *junk_work_tree;
318 static const char *junk_git_dir;
319 static pid_t junk_pid;
321 static void remove_junk(void)
323 struct strbuf sb = STRBUF_INIT;
324 if (getpid() != junk_pid)
325 return;
326 if (junk_git_dir) {
327 strbuf_addstr(&sb, junk_git_dir);
328 remove_dir_recursively(&sb, 0);
329 strbuf_reset(&sb);
331 if (junk_work_tree) {
332 strbuf_addstr(&sb, junk_work_tree);
333 remove_dir_recursively(&sb, 0);
334 strbuf_reset(&sb);
338 static void remove_junk_on_signal(int signo)
340 remove_junk();
341 sigchain_pop(signo);
342 raise(signo);
345 static struct ref *wanted_peer_refs(const struct ref *refs,
346 struct refspec *refspec)
348 struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD"));
349 struct ref *local_refs = head;
350 struct ref **tail = head ? &head->next : &local_refs;
352 get_fetch_map(refs, refspec, &tail, 0);
353 if (!option_mirror)
354 get_fetch_map(refs, tag_refspec, &tail, 0);
356 return local_refs;
359 static void write_remote_refs(const struct ref *local_refs)
361 const struct ref *r;
363 for (r = local_refs; r; r = r->next) {
364 if (!r->peer_ref)
365 continue;
366 add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
369 pack_refs(PACK_REFS_ALL);
370 clear_extra_refs();
373 static int write_one_config(const char *key, const char *value, void *data)
375 return git_config_set_multivar(key, value ? value : "true", "^$", 0);
378 static void write_config(struct string_list *config)
380 int i;
382 for (i = 0; i < config->nr; i++) {
383 if (git_config_parse_parameter(config->items[i].string,
384 write_one_config, NULL) < 0)
385 die("unable to write parameters to config file");
389 int cmd_clone(int argc, const char **argv, const char *prefix)
391 int is_bundle = 0, is_local;
392 struct stat buf;
393 const char *repo_name, *repo, *work_tree, *git_dir;
394 char *path, *dir;
395 int dest_exists;
396 const struct ref *refs, *remote_head;
397 const struct ref *remote_head_points_at;
398 const struct ref *our_head_points_at;
399 struct ref *mapped_refs;
400 struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
401 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
402 struct transport *transport = NULL;
403 char *src_ref_prefix = "refs/heads/";
404 int err = 0;
406 struct refspec *refspec;
407 const char *fetch_pattern;
409 junk_pid = getpid();
411 packet_trace_identity("clone");
412 argc = parse_options(argc, argv, prefix, builtin_clone_options,
413 builtin_clone_usage, 0);
415 if (argc > 2)
416 usage_msg_opt(_("Too many arguments."),
417 builtin_clone_usage, builtin_clone_options);
419 if (argc == 0)
420 usage_msg_opt(_("You must specify a repository to clone."),
421 builtin_clone_usage, builtin_clone_options);
423 if (option_mirror)
424 option_bare = 1;
426 if (option_bare) {
427 if (option_origin)
428 die(_("--bare and --origin %s options are incompatible."),
429 option_origin);
430 option_no_checkout = 1;
433 if (!option_origin)
434 option_origin = "origin";
436 repo_name = argv[0];
438 path = get_repo_path(repo_name, &is_bundle);
439 if (path)
440 repo = xstrdup(absolute_path(repo_name));
441 else if (!strchr(repo_name, ':'))
442 die(_("repository '%s' does not exist"), repo_name);
443 else
444 repo = repo_name;
445 is_local = path && !is_bundle;
446 if (is_local && option_depth)
447 warning(_("--depth is ignored in local clones; use file:// instead."));
449 if (argc == 2)
450 dir = xstrdup(argv[1]);
451 else
452 dir = guess_dir_name(repo_name, is_bundle, option_bare);
453 strip_trailing_slashes(dir);
455 dest_exists = !stat(dir, &buf);
456 if (dest_exists && !is_empty_dir(dir))
457 die(_("destination path '%s' already exists and is not "
458 "an empty directory."), dir);
460 strbuf_addf(&reflog_msg, "clone: from %s", repo);
462 if (option_bare)
463 work_tree = NULL;
464 else {
465 work_tree = getenv("GIT_WORK_TREE");
466 if (work_tree && !stat(work_tree, &buf))
467 die(_("working tree '%s' already exists."), work_tree);
470 if (option_bare || work_tree)
471 git_dir = xstrdup(dir);
472 else {
473 work_tree = dir;
474 git_dir = xstrdup(mkpath("%s/.git", dir));
477 if (!option_bare) {
478 junk_work_tree = work_tree;
479 if (safe_create_leading_directories_const(work_tree) < 0)
480 die_errno(_("could not create leading directories of '%s'"),
481 work_tree);
482 if (!dest_exists && mkdir(work_tree, 0755))
483 die_errno(_("could not create work tree dir '%s'."),
484 work_tree);
485 set_git_work_tree(work_tree);
487 junk_git_dir = git_dir;
488 atexit(remove_junk);
489 sigchain_push_common(remove_junk_on_signal);
491 setenv(CONFIG_ENVIRONMENT, mkpath("%s/config", git_dir), 1);
493 if (safe_create_leading_directories_const(git_dir) < 0)
494 die(_("could not create leading directories of '%s'"), git_dir);
496 set_git_dir_init(git_dir, real_git_dir, 0);
497 if (real_git_dir)
498 git_dir = real_git_dir;
500 if (0 <= option_verbosity) {
501 if (option_bare)
502 printf(_("Cloning into bare repository %s...\n"), dir);
503 else
504 printf(_("Cloning into %s...\n"), dir);
506 init_db(option_template, INIT_DB_QUIET);
507 write_config(&option_config);
510 * At this point, the config exists, so we do not need the
511 * environment variable. We actually need to unset it, too, to
512 * re-enable parsing of the global configs.
514 unsetenv(CONFIG_ENVIRONMENT);
516 git_config(git_default_config, NULL);
518 if (option_bare) {
519 if (option_mirror)
520 src_ref_prefix = "refs/";
521 strbuf_addstr(&branch_top, src_ref_prefix);
523 git_config_set("core.bare", "true");
524 } else {
525 strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
528 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
530 if (option_mirror || !option_bare) {
531 /* Configure the remote */
532 strbuf_addf(&key, "remote.%s.fetch", option_origin);
533 git_config_set_multivar(key.buf, value.buf, "^$", 0);
534 strbuf_reset(&key);
536 if (option_mirror) {
537 strbuf_addf(&key, "remote.%s.mirror", option_origin);
538 git_config_set(key.buf, "true");
539 strbuf_reset(&key);
543 strbuf_addf(&key, "remote.%s.url", option_origin);
544 git_config_set(key.buf, repo);
545 strbuf_reset(&key);
547 if (option_reference)
548 setup_reference(git_dir);
550 fetch_pattern = value.buf;
551 refspec = parse_fetch_refspec(1, &fetch_pattern);
553 strbuf_reset(&value);
555 if (is_local) {
556 refs = clone_local(path, git_dir);
557 mapped_refs = wanted_peer_refs(refs, refspec);
558 } else {
559 struct remote *remote = remote_get(option_origin);
560 transport = transport_get(remote, remote->url[0]);
562 if (!transport->get_refs_list || !transport->fetch)
563 die(_("Don't know how to clone %s"), transport->url);
565 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
567 if (option_depth)
568 transport_set_option(transport, TRANS_OPT_DEPTH,
569 option_depth);
571 transport_set_verbosity(transport, option_verbosity, option_progress);
573 if (option_upload_pack)
574 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
575 option_upload_pack);
577 refs = transport_get_remote_refs(transport);
578 if (refs) {
579 mapped_refs = wanted_peer_refs(refs, refspec);
580 transport_fetch_refs(transport, mapped_refs);
584 if (refs) {
585 clear_extra_refs();
587 write_remote_refs(mapped_refs);
589 remote_head = find_ref_by_name(refs, "HEAD");
590 remote_head_points_at =
591 guess_remote_head(remote_head, mapped_refs, 0);
593 if (option_branch) {
594 struct strbuf head = STRBUF_INIT;
595 strbuf_addstr(&head, src_ref_prefix);
596 strbuf_addstr(&head, option_branch);
597 our_head_points_at =
598 find_ref_by_name(mapped_refs, head.buf);
599 strbuf_release(&head);
601 if (!our_head_points_at) {
602 warning(_("Remote branch %s not found in "
603 "upstream %s, using HEAD instead"),
604 option_branch, option_origin);
605 our_head_points_at = remote_head_points_at;
608 else
609 our_head_points_at = remote_head_points_at;
611 else {
612 warning(_("You appear to have cloned an empty repository."));
613 our_head_points_at = NULL;
614 remote_head_points_at = NULL;
615 remote_head = NULL;
616 option_no_checkout = 1;
617 if (!option_bare)
618 install_branch_config(0, "master", option_origin,
619 "refs/heads/master");
622 if (remote_head_points_at && !option_bare) {
623 struct strbuf head_ref = STRBUF_INIT;
624 strbuf_addstr(&head_ref, branch_top.buf);
625 strbuf_addstr(&head_ref, "HEAD");
626 create_symref(head_ref.buf,
627 remote_head_points_at->peer_ref->name,
628 reflog_msg.buf);
631 if (our_head_points_at) {
632 /* Local default branch link */
633 create_symref("HEAD", our_head_points_at->name, NULL);
634 if (!option_bare) {
635 const char *head = skip_prefix(our_head_points_at->name,
636 "refs/heads/");
637 update_ref(reflog_msg.buf, "HEAD",
638 our_head_points_at->old_sha1,
639 NULL, 0, DIE_ON_ERR);
640 install_branch_config(0, head, option_origin,
641 our_head_points_at->name);
643 } else if (remote_head) {
644 /* Source had detached HEAD pointing somewhere. */
645 if (!option_bare) {
646 update_ref(reflog_msg.buf, "HEAD",
647 remote_head->old_sha1,
648 NULL, REF_NODEREF, DIE_ON_ERR);
649 our_head_points_at = remote_head;
651 } else {
652 /* Nothing to checkout out */
653 if (!option_no_checkout)
654 warning(_("remote HEAD refers to nonexistent ref, "
655 "unable to checkout.\n"));
656 option_no_checkout = 1;
659 if (transport) {
660 transport_unlock_pack(transport);
661 transport_disconnect(transport);
664 if (!option_no_checkout) {
665 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
666 struct unpack_trees_options opts;
667 struct tree *tree;
668 struct tree_desc t;
669 int fd;
671 /* We need to be in the new work tree for the checkout */
672 setup_work_tree();
674 fd = hold_locked_index(lock_file, 1);
676 memset(&opts, 0, sizeof opts);
677 opts.update = 1;
678 opts.merge = 1;
679 opts.fn = oneway_merge;
680 opts.verbose_update = (option_verbosity > 0);
681 opts.src_index = &the_index;
682 opts.dst_index = &the_index;
684 tree = parse_tree_indirect(our_head_points_at->old_sha1);
685 parse_tree(tree);
686 init_tree_desc(&t, tree->buffer, tree->size);
687 unpack_trees(1, &t, &opts);
689 if (write_cache(fd, active_cache, active_nr) ||
690 commit_locked_index(lock_file))
691 die(_("unable to write new index file"));
693 err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1),
694 sha1_to_hex(our_head_points_at->old_sha1), "1",
695 NULL);
697 if (!err && option_recursive)
698 err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
701 strbuf_release(&reflog_msg);
702 strbuf_release(&branch_top);
703 strbuf_release(&key);
704 strbuf_release(&value);
705 junk_pid = 0;
706 return err;