Merge branch 'fc/parseopt-config' into next
[git/dscho.git] / builtin-clone.c
blobceffecb68434e4e77a85d411e9e8ba31e315bee8
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 "cache.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 "remote.h"
24 #include "run-command.h"
27 * Overall FIXMEs:
28 * - respect DB_ENVIRONMENT for .git/objects.
30 * Implementation notes:
31 * - dropping use-separate-remote and no-separate-remote compatibility
34 static const char * const builtin_clone_usage[] = {
35 "git clone [options] [--] <repo> [<dir>]",
36 NULL
39 static int option_quiet, option_no_checkout, option_bare, option_mirror;
40 static int option_local, option_no_hardlinks, option_shared;
41 static char *option_template, *option_reference, *option_depth;
42 static char *option_origin = NULL;
43 static char *option_upload_pack = "git-upload-pack";
44 static int option_verbose;
46 static struct option builtin_clone_options[] = {
47 OPT__QUIET(&option_quiet),
48 OPT__VERBOSE(&option_verbose),
49 OPT_BOOLEAN('n', "no-checkout", &option_no_checkout,
50 "don't create a checkout"),
51 OPT_BOOLEAN(0, "bare", &option_bare, "create a bare repository"),
52 OPT_BOOLEAN(0, "naked", &option_bare, "create a bare repository"),
53 OPT_BOOLEAN(0, "mirror", &option_mirror,
54 "create a mirror repository (implies bare)"),
55 OPT_BOOLEAN('l', "local", &option_local,
56 "to clone from a local repository"),
57 OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks,
58 "don't use local hardlinks, always copy"),
59 OPT_BOOLEAN('s', "shared", &option_shared,
60 "setup as shared repository"),
61 OPT_STRING(0, "template", &option_template, "path",
62 "path the template repository"),
63 OPT_STRING(0, "reference", &option_reference, "repo",
64 "reference repository"),
65 OPT_STRING('o', "origin", &option_origin, "branch",
66 "use <branch> instead of 'origin' to track upstream"),
67 OPT_STRING('u', "upload-pack", &option_upload_pack, "path",
68 "path to git-upload-pack on the remote"),
69 OPT_STRING(0, "depth", &option_depth, "depth",
70 "create a shallow clone of that depth"),
72 OPT_END()
75 static char *get_repo_path(const char *repo, int *is_bundle)
77 static char *suffix[] = { "/.git", ".git", "" };
78 static char *bundle_suffix[] = { ".bundle", "" };
79 struct stat st;
80 int i;
82 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
83 const char *path;
84 path = mkpath("%s%s", repo, suffix[i]);
85 if (is_directory(path)) {
86 *is_bundle = 0;
87 return xstrdup(make_nonrelative_path(path));
91 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
92 const char *path;
93 path = mkpath("%s%s", repo, bundle_suffix[i]);
94 if (!stat(path, &st) && S_ISREG(st.st_mode)) {
95 *is_bundle = 1;
96 return xstrdup(make_nonrelative_path(path));
100 return NULL;
103 static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
105 const char *end = repo + strlen(repo), *start;
108 * Strip trailing slashes and /.git
110 while (repo < end && is_dir_sep(end[-1]))
111 end--;
112 if (end - repo > 5 && is_dir_sep(end[-5]) &&
113 !strncmp(end - 4, ".git", 4)) {
114 end -= 5;
115 while (repo < end && is_dir_sep(end[-1]))
116 end--;
120 * Find last component, but be prepared that repo could have
121 * the form "remote.example.com:foo.git", i.e. no slash
122 * in the directory part.
124 start = end;
125 while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
126 start--;
129 * Strip .{bundle,git}.
131 if (is_bundle) {
132 if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
133 end -= 7;
134 } else {
135 if (end - start > 4 && !strncmp(end - 4, ".git", 4))
136 end -= 4;
139 if (is_bare) {
140 struct strbuf result = STRBUF_INIT;
141 strbuf_addf(&result, "%.*s.git", (int)(end - start), start);
142 return strbuf_detach(&result, 0);
145 return xstrndup(start, end - start);
148 static void strip_trailing_slashes(char *dir)
150 char *end = dir + strlen(dir);
152 while (dir < end - 1 && is_dir_sep(end[-1]))
153 end--;
154 *end = '\0';
157 static void setup_reference(const char *repo)
159 const char *ref_git;
160 char *ref_git_copy;
162 struct remote *remote;
163 struct transport *transport;
164 const struct ref *extra;
166 ref_git = make_absolute_path(option_reference);
168 if (is_directory(mkpath("%s/.git/objects", ref_git)))
169 ref_git = mkpath("%s/.git", ref_git);
170 else if (!is_directory(mkpath("%s/objects", ref_git)))
171 die("reference repository '%s' is not a local directory.",
172 option_reference);
174 ref_git_copy = xstrdup(ref_git);
176 add_to_alternates_file(ref_git_copy);
178 remote = remote_get(ref_git_copy);
179 transport = transport_get(remote, ref_git_copy);
180 for (extra = transport_get_remote_refs(transport); extra;
181 extra = extra->next)
182 add_extra_ref(extra->name, extra->old_sha1, 0);
184 transport_disconnect(transport);
186 free(ref_git_copy);
189 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
191 struct dirent *de;
192 struct stat buf;
193 int src_len, dest_len;
194 DIR *dir;
196 dir = opendir(src->buf);
197 if (!dir)
198 die("failed to open %s", src->buf);
200 if (mkdir(dest->buf, 0777)) {
201 if (errno != EEXIST)
202 die("failed to create directory %s", dest->buf);
203 else if (stat(dest->buf, &buf))
204 die("failed to stat %s", dest->buf);
205 else if (!S_ISDIR(buf.st_mode))
206 die("%s exists and is not a directory", dest->buf);
209 strbuf_addch(src, '/');
210 src_len = src->len;
211 strbuf_addch(dest, '/');
212 dest_len = dest->len;
214 while ((de = readdir(dir)) != NULL) {
215 strbuf_setlen(src, src_len);
216 strbuf_addstr(src, de->d_name);
217 strbuf_setlen(dest, dest_len);
218 strbuf_addstr(dest, de->d_name);
219 if (stat(src->buf, &buf)) {
220 warning ("failed to stat %s\n", src->buf);
221 continue;
223 if (S_ISDIR(buf.st_mode)) {
224 if (de->d_name[0] != '.')
225 copy_or_link_directory(src, dest);
226 continue;
229 if (unlink(dest->buf) && errno != ENOENT)
230 die("failed to unlink %s", dest->buf);
231 if (!option_no_hardlinks) {
232 if (!link(src->buf, dest->buf))
233 continue;
234 if (option_local)
235 die("failed to create link %s", dest->buf);
236 option_no_hardlinks = 1;
238 if (copy_file(dest->buf, src->buf, 0666))
239 die("failed to copy file to %s", dest->buf);
241 closedir(dir);
244 static const struct ref *clone_local(const char *src_repo,
245 const char *dest_repo)
247 const struct ref *ret;
248 struct strbuf src = STRBUF_INIT;
249 struct strbuf dest = STRBUF_INIT;
250 struct remote *remote;
251 struct transport *transport;
253 if (option_shared)
254 add_to_alternates_file(src_repo);
255 else {
256 strbuf_addf(&src, "%s/objects", src_repo);
257 strbuf_addf(&dest, "%s/objects", dest_repo);
258 copy_or_link_directory(&src, &dest);
259 strbuf_release(&src);
260 strbuf_release(&dest);
263 remote = remote_get(src_repo);
264 transport = transport_get(remote, src_repo);
265 ret = transport_get_remote_refs(transport);
266 transport_disconnect(transport);
267 return ret;
270 static const char *junk_work_tree;
271 static const char *junk_git_dir;
272 pid_t junk_pid;
274 static void remove_junk(void)
276 struct strbuf sb = STRBUF_INIT;
277 if (getpid() != junk_pid)
278 return;
279 if (junk_git_dir) {
280 strbuf_addstr(&sb, junk_git_dir);
281 remove_dir_recursively(&sb, 0);
282 strbuf_reset(&sb);
284 if (junk_work_tree) {
285 strbuf_addstr(&sb, junk_work_tree);
286 remove_dir_recursively(&sb, 0);
287 strbuf_reset(&sb);
291 static void remove_junk_on_signal(int signo)
293 remove_junk();
294 sigchain_pop(signo);
295 raise(signo);
298 static struct ref *write_remote_refs(const struct ref *refs,
299 struct refspec *refspec, const char *reflog)
301 struct ref *local_refs = NULL;
302 struct ref **tail = &local_refs;
303 struct ref *r;
305 get_fetch_map(refs, refspec, &tail, 0);
306 if (!option_mirror)
307 get_fetch_map(refs, tag_refspec, &tail, 0);
309 for (r = local_refs; r; r = r->next)
310 add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
312 pack_refs(PACK_REFS_ALL);
313 clear_extra_refs();
315 return local_refs;
318 static void install_branch_config(const char *local,
319 const char *origin,
320 const char *remote)
322 struct strbuf key = STRBUF_INIT;
323 strbuf_addf(&key, "branch.%s.remote", local);
324 git_config_set(key.buf, origin);
325 strbuf_reset(&key);
326 strbuf_addf(&key, "branch.%s.merge", local);
327 git_config_set(key.buf, remote);
328 strbuf_release(&key);
331 int cmd_clone(int argc, const char **argv, const char *prefix)
333 int use_local_hardlinks = 1;
334 int use_separate_remote = 1;
335 int is_bundle = 0;
336 struct stat buf;
337 const char *repo_name, *repo, *work_tree, *git_dir;
338 char *path, *dir;
339 int dest_exists;
340 const struct ref *refs, *head_points_at, *remote_head, *mapped_refs;
341 struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
342 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
343 struct transport *transport = NULL;
344 char *src_ref_prefix = "refs/heads/";
345 int err = 0;
347 struct refspec refspec;
349 junk_pid = getpid();
351 argc = parse_options(argc, argv, builtin_clone_options,
352 builtin_clone_usage, 0);
354 if (argc == 0)
355 die("You must specify a repository to clone.");
357 if (option_no_hardlinks)
358 use_local_hardlinks = 0;
360 if (option_mirror)
361 option_bare = 1;
363 if (option_bare) {
364 if (option_origin)
365 die("--bare and --origin %s options are incompatible.",
366 option_origin);
367 option_no_checkout = 1;
368 use_separate_remote = 0;
371 if (!option_origin)
372 option_origin = "origin";
374 repo_name = argv[0];
376 path = get_repo_path(repo_name, &is_bundle);
377 if (path)
378 repo = xstrdup(make_nonrelative_path(repo_name));
379 else if (!strchr(repo_name, ':'))
380 repo = xstrdup(make_absolute_path(repo_name));
381 else
382 repo = repo_name;
384 if (argc == 2)
385 dir = xstrdup(argv[1]);
386 else
387 dir = guess_dir_name(repo_name, is_bundle, option_bare);
388 strip_trailing_slashes(dir);
390 dest_exists = !stat(dir, &buf);
391 if (dest_exists && !is_empty_dir(dir))
392 die("destination path '%s' already exists and is not "
393 "an empty directory.", dir);
395 strbuf_addf(&reflog_msg, "clone: from %s", repo);
397 if (option_bare)
398 work_tree = NULL;
399 else {
400 work_tree = getenv("GIT_WORK_TREE");
401 if (work_tree && !stat(work_tree, &buf))
402 die("working tree '%s' already exists.", work_tree);
405 if (option_bare || work_tree)
406 git_dir = xstrdup(dir);
407 else {
408 work_tree = dir;
409 git_dir = xstrdup(mkpath("%s/.git", dir));
412 if (!option_bare) {
413 junk_work_tree = work_tree;
414 if (safe_create_leading_directories_const(work_tree) < 0)
415 die("could not create leading directories of '%s': %s",
416 work_tree, strerror(errno));
417 if (!dest_exists && mkdir(work_tree, 0755))
418 die("could not create work tree dir '%s': %s.",
419 work_tree, strerror(errno));
420 set_git_work_tree(work_tree);
422 junk_git_dir = git_dir;
423 atexit(remove_junk);
424 sigchain_push_common(remove_junk_on_signal);
426 setenv(CONFIG_ENVIRONMENT, xstrdup(mkpath("%s/config", git_dir)), 1);
428 if (safe_create_leading_directories_const(git_dir) < 0)
429 die("could not create leading directories of '%s'", git_dir);
430 set_git_dir(make_absolute_path(git_dir));
432 init_db(option_template, option_quiet ? INIT_DB_QUIET : 0);
435 * At this point, the config exists, so we do not need the
436 * environment variable. We actually need to unset it, too, to
437 * re-enable parsing of the global configs.
439 unsetenv(CONFIG_ENVIRONMENT);
441 if (option_reference)
442 setup_reference(git_dir);
444 git_config(git_default_config, NULL);
446 if (option_bare) {
447 if (option_mirror)
448 src_ref_prefix = "refs/";
449 strbuf_addstr(&branch_top, src_ref_prefix);
451 git_config_set("core.bare", "true");
452 } else {
453 strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
456 if (option_mirror || !option_bare) {
457 /* Configure the remote */
458 if (option_mirror) {
459 strbuf_addf(&key, "remote.%s.mirror", option_origin);
460 git_config_set(key.buf, "true");
461 strbuf_reset(&key);
464 strbuf_addf(&key, "remote.%s.url", option_origin);
465 git_config_set(key.buf, repo);
466 strbuf_reset(&key);
468 strbuf_addf(&key, "remote.%s.fetch", option_origin);
469 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
470 git_config_set_multivar(key.buf, value.buf, "^$", 0);
471 strbuf_reset(&key);
472 strbuf_reset(&value);
475 refspec.force = 0;
476 refspec.pattern = 1;
477 refspec.src = src_ref_prefix;
478 refspec.dst = branch_top.buf;
480 if (path && !is_bundle)
481 refs = clone_local(path, git_dir);
482 else {
483 struct remote *remote = remote_get(argv[0]);
484 transport = transport_get(remote, remote->url[0]);
486 if (!transport->get_refs_list || !transport->fetch)
487 die("Don't know how to clone %s", transport->url);
489 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
491 if (option_depth)
492 transport_set_option(transport, TRANS_OPT_DEPTH,
493 option_depth);
495 if (option_quiet)
496 transport->verbose = -1;
497 else if (option_verbose)
498 transport->progress = 1;
500 if (option_upload_pack)
501 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
502 option_upload_pack);
504 refs = transport_get_remote_refs(transport);
505 if(refs)
506 transport_fetch_refs(transport, refs);
509 if (refs) {
510 clear_extra_refs();
512 mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
514 remote_head = find_ref_by_name(refs, "HEAD");
515 head_points_at = guess_remote_head(remote_head, mapped_refs, 0);
517 else {
518 warning("You appear to have cloned an empty repository.");
519 head_points_at = NULL;
520 remote_head = NULL;
521 option_no_checkout = 1;
522 if (!option_bare)
523 install_branch_config("master", option_origin,
524 "refs/heads/master");
527 if (head_points_at) {
528 /* Local default branch link */
529 create_symref("HEAD", head_points_at->name, NULL);
531 if (!option_bare) {
532 struct strbuf head_ref = STRBUF_INIT;
533 const char *head = head_points_at->name;
535 if (!prefixcmp(head, "refs/heads/"))
536 head += 11;
538 /* Set up the initial local branch */
540 /* Local branch initial value */
541 update_ref(reflog_msg.buf, "HEAD",
542 head_points_at->old_sha1,
543 NULL, 0, DIE_ON_ERR);
545 strbuf_addstr(&head_ref, branch_top.buf);
546 strbuf_addstr(&head_ref, "HEAD");
548 /* Remote branch link */
549 create_symref(head_ref.buf,
550 head_points_at->peer_ref->name,
551 reflog_msg.buf);
553 install_branch_config(head, option_origin,
554 head_points_at->name);
556 } else if (remote_head) {
557 /* Source had detached HEAD pointing somewhere. */
558 if (!option_bare)
559 update_ref(reflog_msg.buf, "HEAD",
560 remote_head->old_sha1,
561 NULL, REF_NODEREF, DIE_ON_ERR);
562 } else {
563 /* Nothing to checkout out */
564 if (!option_no_checkout)
565 warning("remote HEAD refers to nonexistent ref, "
566 "unable to checkout.\n");
567 option_no_checkout = 1;
570 if (transport)
571 transport_unlock_pack(transport);
573 if (!option_no_checkout) {
574 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
575 struct unpack_trees_options opts;
576 struct tree *tree;
577 struct tree_desc t;
578 int fd;
580 /* We need to be in the new work tree for the checkout */
581 setup_work_tree();
583 fd = hold_locked_index(lock_file, 1);
585 memset(&opts, 0, sizeof opts);
586 opts.update = 1;
587 opts.merge = 1;
588 opts.fn = oneway_merge;
589 opts.verbose_update = !option_quiet;
590 opts.src_index = &the_index;
591 opts.dst_index = &the_index;
593 tree = parse_tree_indirect(remote_head->old_sha1);
594 parse_tree(tree);
595 init_tree_desc(&t, tree->buffer, tree->size);
596 unpack_trees(1, &t, &opts);
598 if (write_cache(fd, active_cache, active_nr) ||
599 commit_locked_index(lock_file))
600 die("unable to write new index file");
602 err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1),
603 sha1_to_hex(remote_head->old_sha1), "1", NULL);
606 strbuf_release(&reflog_msg);
607 strbuf_release(&branch_top);
608 strbuf_release(&key);
609 strbuf_release(&value);
610 junk_pid = 0;
611 return err;