t3412: clean up GIT_EDITOR usage
[git/spearce.git] / builtin-clone.c
blob1e9c9aa8446370070d56126ad851701c4c418de0
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"
24 * Overall FIXMEs:
25 * - respect DB_ENVIRONMENT for .git/objects.
27 * Implementation notes:
28 * - dropping use-separate-remote and no-separate-remote compatibility
31 static const char * const builtin_clone_usage[] = {
32 "git clone [options] [--] <repo> [<dir>]",
33 NULL
36 static int option_quiet, option_no_checkout, option_bare, option_mirror;
37 static int option_local, option_no_hardlinks, option_shared;
38 static char *option_template, *option_reference, *option_depth;
39 static char *option_origin = NULL;
40 static char *option_upload_pack = "git-upload-pack";
41 static int option_verbose;
43 static struct option builtin_clone_options[] = {
44 OPT__QUIET(&option_quiet),
45 OPT__VERBOSE(&option_verbose),
46 OPT_BOOLEAN('n', "no-checkout", &option_no_checkout,
47 "don't create a checkout"),
48 OPT_BOOLEAN(0, "bare", &option_bare, "create a bare repository"),
49 OPT_BOOLEAN(0, "naked", &option_bare, "create a bare repository"),
50 OPT_BOOLEAN(0, "mirror", &option_mirror,
51 "create a mirror repository (implies bare)"),
52 OPT_BOOLEAN('l', "local", &option_local,
53 "to clone from a local repository"),
54 OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks,
55 "don't use local hardlinks, always copy"),
56 OPT_BOOLEAN('s', "shared", &option_shared,
57 "setup as shared repository"),
58 OPT_STRING(0, "template", &option_template, "path",
59 "path the template repository"),
60 OPT_STRING(0, "reference", &option_reference, "repo",
61 "reference repository"),
62 OPT_STRING('o', "origin", &option_origin, "branch",
63 "use <branch> instead of 'origin' to track upstream"),
64 OPT_STRING('u', "upload-pack", &option_upload_pack, "path",
65 "path to git-upload-pack on the remote"),
66 OPT_STRING(0, "depth", &option_depth, "depth",
67 "create a shallow clone of that depth"),
69 OPT_END()
72 static char *get_repo_path(const char *repo, int *is_bundle)
74 static char *suffix[] = { "/.git", ".git", "" };
75 static char *bundle_suffix[] = { ".bundle", "" };
76 struct stat st;
77 int i;
79 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
80 const char *path;
81 path = mkpath("%s%s", repo, suffix[i]);
82 if (is_directory(path)) {
83 *is_bundle = 0;
84 return xstrdup(make_nonrelative_path(path));
88 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
89 const char *path;
90 path = mkpath("%s%s", repo, bundle_suffix[i]);
91 if (!stat(path, &st) && S_ISREG(st.st_mode)) {
92 *is_bundle = 1;
93 return xstrdup(make_nonrelative_path(path));
97 return NULL;
100 static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
102 const char *end = repo + strlen(repo), *start;
105 * Strip trailing slashes and /.git
107 while (repo < end && is_dir_sep(end[-1]))
108 end--;
109 if (end - repo > 5 && is_dir_sep(end[-5]) &&
110 !strncmp(end - 4, ".git", 4)) {
111 end -= 5;
112 while (repo < end && is_dir_sep(end[-1]))
113 end--;
117 * Find last component, but be prepared that repo could have
118 * the form "remote.example.com:foo.git", i.e. no slash
119 * in the directory part.
121 start = end;
122 while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
123 start--;
126 * Strip .{bundle,git}.
128 if (is_bundle) {
129 if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
130 end -= 7;
131 } else {
132 if (end - start > 4 && !strncmp(end - 4, ".git", 4))
133 end -= 4;
136 if (is_bare) {
137 struct strbuf result = STRBUF_INIT;
138 strbuf_addf(&result, "%.*s.git", (int)(end - start), start);
139 return strbuf_detach(&result, 0);
142 return xstrndup(start, end - start);
145 static void strip_trailing_slashes(char *dir)
147 char *end = dir + strlen(dir);
149 while (dir < end - 1 && is_dir_sep(end[-1]))
150 end--;
151 *end = '\0';
154 static void setup_reference(const char *repo)
156 const char *ref_git;
157 char *ref_git_copy;
159 struct remote *remote;
160 struct transport *transport;
161 const struct ref *extra;
163 ref_git = make_absolute_path(option_reference);
165 if (is_directory(mkpath("%s/.git/objects", ref_git)))
166 ref_git = mkpath("%s/.git", ref_git);
167 else if (!is_directory(mkpath("%s/objects", ref_git)))
168 die("reference repository '%s' is not a local directory.",
169 option_reference);
171 ref_git_copy = xstrdup(ref_git);
173 add_to_alternates_file(ref_git_copy);
175 remote = remote_get(ref_git_copy);
176 transport = transport_get(remote, ref_git_copy);
177 for (extra = transport_get_remote_refs(transport); extra;
178 extra = extra->next)
179 add_extra_ref(extra->name, extra->old_sha1, 0);
181 transport_disconnect(transport);
183 free(ref_git_copy);
186 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
188 struct dirent *de;
189 struct stat buf;
190 int src_len, dest_len;
191 DIR *dir;
193 dir = opendir(src->buf);
194 if (!dir)
195 die("failed to open %s", src->buf);
197 if (mkdir(dest->buf, 0777)) {
198 if (errno != EEXIST)
199 die("failed to create directory %s", dest->buf);
200 else if (stat(dest->buf, &buf))
201 die("failed to stat %s", dest->buf);
202 else if (!S_ISDIR(buf.st_mode))
203 die("%s exists and is not a directory", dest->buf);
206 strbuf_addch(src, '/');
207 src_len = src->len;
208 strbuf_addch(dest, '/');
209 dest_len = dest->len;
211 while ((de = readdir(dir)) != NULL) {
212 strbuf_setlen(src, src_len);
213 strbuf_addstr(src, de->d_name);
214 strbuf_setlen(dest, dest_len);
215 strbuf_addstr(dest, de->d_name);
216 if (stat(src->buf, &buf)) {
217 warning ("failed to stat %s\n", src->buf);
218 continue;
220 if (S_ISDIR(buf.st_mode)) {
221 if (de->d_name[0] != '.')
222 copy_or_link_directory(src, dest);
223 continue;
226 if (unlink(dest->buf) && errno != ENOENT)
227 die("failed to unlink %s", dest->buf);
228 if (!option_no_hardlinks) {
229 if (!link(src->buf, dest->buf))
230 continue;
231 if (option_local)
232 die("failed to create link %s", dest->buf);
233 option_no_hardlinks = 1;
235 if (copy_file(dest->buf, src->buf, 0666))
236 die("failed to copy file to %s", dest->buf);
238 closedir(dir);
241 static const struct ref *clone_local(const char *src_repo,
242 const char *dest_repo)
244 const struct ref *ret;
245 struct strbuf src = STRBUF_INIT;
246 struct strbuf dest = STRBUF_INIT;
247 struct remote *remote;
248 struct transport *transport;
250 if (option_shared)
251 add_to_alternates_file(src_repo);
252 else {
253 strbuf_addf(&src, "%s/objects", src_repo);
254 strbuf_addf(&dest, "%s/objects", dest_repo);
255 copy_or_link_directory(&src, &dest);
256 strbuf_release(&src);
257 strbuf_release(&dest);
260 remote = remote_get(src_repo);
261 transport = transport_get(remote, src_repo);
262 ret = transport_get_remote_refs(transport);
263 transport_disconnect(transport);
264 return ret;
267 static const char *junk_work_tree;
268 static const char *junk_git_dir;
269 pid_t junk_pid;
271 static void remove_junk(void)
273 struct strbuf sb = STRBUF_INIT;
274 if (getpid() != junk_pid)
275 return;
276 if (junk_git_dir) {
277 strbuf_addstr(&sb, junk_git_dir);
278 remove_dir_recursively(&sb, 0);
279 strbuf_reset(&sb);
281 if (junk_work_tree) {
282 strbuf_addstr(&sb, junk_work_tree);
283 remove_dir_recursively(&sb, 0);
284 strbuf_reset(&sb);
288 static void remove_junk_on_signal(int signo)
290 remove_junk();
291 signal(SIGINT, SIG_DFL);
292 raise(signo);
295 static const struct ref *locate_head(const struct ref *refs,
296 const struct ref *mapped_refs,
297 const struct ref **remote_head_p)
299 const struct ref *remote_head = NULL;
300 const struct ref *remote_master = NULL;
301 const struct ref *r;
302 for (r = refs; r; r = r->next)
303 if (!strcmp(r->name, "HEAD"))
304 remote_head = r;
306 for (r = mapped_refs; r; r = r->next)
307 if (!strcmp(r->name, "refs/heads/master"))
308 remote_master = r;
310 if (remote_head_p)
311 *remote_head_p = remote_head;
313 /* If there's no HEAD value at all, never mind. */
314 if (!remote_head)
315 return NULL;
317 /* If refs/heads/master could be right, it is. */
318 if (remote_master && !hashcmp(remote_master->old_sha1,
319 remote_head->old_sha1))
320 return remote_master;
322 /* Look for another ref that points there */
323 for (r = mapped_refs; r; r = r->next)
324 if (r != remote_head &&
325 !hashcmp(r->old_sha1, remote_head->old_sha1))
326 return r;
328 /* Nothing is the same */
329 return NULL;
332 static struct ref *write_remote_refs(const struct ref *refs,
333 struct refspec *refspec, const char *reflog)
335 struct ref *local_refs = NULL;
336 struct ref **tail = &local_refs;
337 struct ref *r;
339 get_fetch_map(refs, refspec, &tail, 0);
340 if (!option_mirror)
341 get_fetch_map(refs, tag_refspec, &tail, 0);
343 for (r = local_refs; r; r = r->next)
344 add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
346 pack_refs(PACK_REFS_ALL);
347 clear_extra_refs();
349 return local_refs;
352 int cmd_clone(int argc, const char **argv, const char *prefix)
354 int use_local_hardlinks = 1;
355 int use_separate_remote = 1;
356 int is_bundle = 0;
357 struct stat buf;
358 const char *repo_name, *repo, *work_tree, *git_dir;
359 char *path, *dir;
360 int dest_exists;
361 const struct ref *refs, *head_points_at, *remote_head, *mapped_refs;
362 struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
363 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
364 struct transport *transport = NULL;
365 char *src_ref_prefix = "refs/heads/";
367 struct refspec refspec;
369 junk_pid = getpid();
371 argc = parse_options(argc, argv, builtin_clone_options,
372 builtin_clone_usage, 0);
374 if (argc == 0)
375 die("You must specify a repository to clone.");
377 if (option_no_hardlinks)
378 use_local_hardlinks = 0;
380 if (option_mirror)
381 option_bare = 1;
383 if (option_bare) {
384 if (option_origin)
385 die("--bare and --origin %s options are incompatible.",
386 option_origin);
387 option_no_checkout = 1;
388 use_separate_remote = 0;
391 if (!option_origin)
392 option_origin = "origin";
394 repo_name = argv[0];
396 path = get_repo_path(repo_name, &is_bundle);
397 if (path)
398 repo = xstrdup(make_nonrelative_path(repo_name));
399 else if (!strchr(repo_name, ':'))
400 repo = xstrdup(make_absolute_path(repo_name));
401 else
402 repo = repo_name;
404 if (argc == 2)
405 dir = xstrdup(argv[1]);
406 else
407 dir = guess_dir_name(repo_name, is_bundle, option_bare);
408 strip_trailing_slashes(dir);
410 dest_exists = !stat(dir, &buf);
411 if (dest_exists && !is_empty_dir(dir))
412 die("destination path '%s' already exists and is not "
413 "an empty directory.", dir);
415 strbuf_addf(&reflog_msg, "clone: from %s", repo);
417 if (option_bare)
418 work_tree = NULL;
419 else {
420 work_tree = getenv("GIT_WORK_TREE");
421 if (work_tree && !stat(work_tree, &buf))
422 die("working tree '%s' already exists.", work_tree);
425 if (option_bare || work_tree)
426 git_dir = xstrdup(dir);
427 else {
428 work_tree = dir;
429 git_dir = xstrdup(mkpath("%s/.git", dir));
432 if (!option_bare) {
433 junk_work_tree = work_tree;
434 if (safe_create_leading_directories_const(work_tree) < 0)
435 die("could not create leading directories of '%s': %s",
436 work_tree, strerror(errno));
437 if (!dest_exists && mkdir(work_tree, 0755))
438 die("could not create work tree dir '%s': %s.",
439 work_tree, strerror(errno));
440 set_git_work_tree(work_tree);
442 junk_git_dir = git_dir;
443 atexit(remove_junk);
444 signal(SIGINT, remove_junk_on_signal);
446 setenv(CONFIG_ENVIRONMENT, xstrdup(mkpath("%s/config", git_dir)), 1);
448 if (safe_create_leading_directories_const(git_dir) < 0)
449 die("could not create leading directories of '%s'", git_dir);
450 set_git_dir(make_absolute_path(git_dir));
452 init_db(option_template, option_quiet ? INIT_DB_QUIET : 0);
455 * At this point, the config exists, so we do not need the
456 * environment variable. We actually need to unset it, too, to
457 * re-enable parsing of the global configs.
459 unsetenv(CONFIG_ENVIRONMENT);
461 if (option_reference)
462 setup_reference(git_dir);
464 git_config(git_default_config, NULL);
466 if (option_bare) {
467 if (option_mirror)
468 src_ref_prefix = "refs/";
469 strbuf_addstr(&branch_top, src_ref_prefix);
471 git_config_set("core.bare", "true");
472 } else {
473 strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
476 if (option_mirror || !option_bare) {
477 /* Configure the remote */
478 if (option_mirror) {
479 strbuf_addf(&key, "remote.%s.mirror", option_origin);
480 git_config_set(key.buf, "true");
481 strbuf_reset(&key);
484 strbuf_addf(&key, "remote.%s.url", option_origin);
485 git_config_set(key.buf, repo);
486 strbuf_reset(&key);
488 strbuf_addf(&key, "remote.%s.fetch", option_origin);
489 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
490 git_config_set_multivar(key.buf, value.buf, "^$", 0);
491 strbuf_reset(&key);
492 strbuf_reset(&value);
495 refspec.force = 0;
496 refspec.pattern = 1;
497 refspec.src = src_ref_prefix;
498 refspec.dst = branch_top.buf;
500 if (path && !is_bundle)
501 refs = clone_local(path, git_dir);
502 else {
503 struct remote *remote = remote_get(argv[0]);
504 transport = transport_get(remote, remote->url[0]);
506 if (!transport->get_refs_list || !transport->fetch)
507 die("Don't know how to clone %s", transport->url);
509 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
511 if (option_depth)
512 transport_set_option(transport, TRANS_OPT_DEPTH,
513 option_depth);
515 if (option_quiet)
516 transport->verbose = -1;
517 else if (option_verbose)
518 transport->progress = 1;
520 if (option_upload_pack)
521 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
522 option_upload_pack);
524 refs = transport_get_remote_refs(transport);
525 if(refs)
526 transport_fetch_refs(transport, refs);
529 if (refs) {
530 clear_extra_refs();
532 mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
534 head_points_at = locate_head(refs, mapped_refs, &remote_head);
536 else {
537 warning("You appear to have cloned an empty repository.");
538 head_points_at = NULL;
539 remote_head = NULL;
540 option_no_checkout = 1;
543 if (head_points_at) {
544 /* Local default branch link */
545 create_symref("HEAD", head_points_at->name, NULL);
547 if (!option_bare) {
548 struct strbuf head_ref = STRBUF_INIT;
549 const char *head = head_points_at->name;
551 if (!prefixcmp(head, "refs/heads/"))
552 head += 11;
554 /* Set up the initial local branch */
556 /* Local branch initial value */
557 update_ref(reflog_msg.buf, "HEAD",
558 head_points_at->old_sha1,
559 NULL, 0, DIE_ON_ERR);
561 strbuf_addstr(&head_ref, branch_top.buf);
562 strbuf_addstr(&head_ref, "HEAD");
564 /* Remote branch link */
565 create_symref(head_ref.buf,
566 head_points_at->peer_ref->name,
567 reflog_msg.buf);
569 strbuf_addf(&key, "branch.%s.remote", head);
570 git_config_set(key.buf, option_origin);
571 strbuf_reset(&key);
572 strbuf_addf(&key, "branch.%s.merge", head);
573 git_config_set(key.buf, head_points_at->name);
575 } else if (remote_head) {
576 /* Source had detached HEAD pointing somewhere. */
577 if (!option_bare)
578 update_ref(reflog_msg.buf, "HEAD",
579 remote_head->old_sha1,
580 NULL, REF_NODEREF, DIE_ON_ERR);
581 } else {
582 /* Nothing to checkout out */
583 if (!option_no_checkout)
584 warning("remote HEAD refers to nonexistent ref, "
585 "unable to checkout.\n");
586 option_no_checkout = 1;
589 if (transport)
590 transport_unlock_pack(transport);
592 if (!option_no_checkout) {
593 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
594 struct unpack_trees_options opts;
595 struct tree *tree;
596 struct tree_desc t;
597 int fd;
599 /* We need to be in the new work tree for the checkout */
600 setup_work_tree();
602 fd = hold_locked_index(lock_file, 1);
604 memset(&opts, 0, sizeof opts);
605 opts.update = 1;
606 opts.merge = 1;
607 opts.fn = oneway_merge;
608 opts.verbose_update = !option_quiet;
609 opts.src_index = &the_index;
610 opts.dst_index = &the_index;
612 tree = parse_tree_indirect(remote_head->old_sha1);
613 parse_tree(tree);
614 init_tree_desc(&t, tree->buffer, tree->size);
615 unpack_trees(1, &t, &opts);
617 if (write_cache(fd, active_cache, active_nr) ||
618 commit_locked_index(lock_file))
619 die("unable to write new index file");
622 strbuf_release(&reflog_msg);
623 strbuf_release(&branch_top);
624 strbuf_release(&key);
625 strbuf_release(&value);
626 junk_pid = 0;
627 return 0;