githooks documentation: post-checkout hook is also called after clone
[git/jrn.git] / builtin-clone.c
bloba6d89bc625bb4bcab85b5a143e9f670c3012b98e
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 "run-command.h"
26 * Overall FIXMEs:
27 * - respect DB_ENVIRONMENT for .git/objects.
29 * Implementation notes:
30 * - dropping use-separate-remote and no-separate-remote compatibility
33 static const char * const builtin_clone_usage[] = {
34 "git clone [options] [--] <repo> [<dir>]",
35 NULL
38 static int option_quiet, option_no_checkout, option_bare, option_mirror;
39 static int option_local, option_no_hardlinks, option_shared;
40 static char *option_template, *option_reference, *option_depth;
41 static char *option_origin = NULL;
42 static char *option_upload_pack = "git-upload-pack";
43 static int option_verbose;
45 static struct option builtin_clone_options[] = {
46 OPT__QUIET(&option_quiet),
47 OPT__VERBOSE(&option_verbose),
48 OPT_BOOLEAN('n', "no-checkout", &option_no_checkout,
49 "don't create a checkout"),
50 OPT_BOOLEAN(0, "bare", &option_bare, "create a bare repository"),
51 OPT_BOOLEAN(0, "naked", &option_bare, "create a bare repository"),
52 OPT_BOOLEAN(0, "mirror", &option_mirror,
53 "create a mirror repository (implies bare)"),
54 OPT_BOOLEAN('l', "local", &option_local,
55 "to clone from a local repository"),
56 OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks,
57 "don't use local hardlinks, always copy"),
58 OPT_BOOLEAN('s', "shared", &option_shared,
59 "setup as shared repository"),
60 OPT_STRING(0, "template", &option_template, "path",
61 "path the template repository"),
62 OPT_STRING(0, "reference", &option_reference, "repo",
63 "reference repository"),
64 OPT_STRING('o', "origin", &option_origin, "branch",
65 "use <branch> instead of 'origin' to track upstream"),
66 OPT_STRING('u', "upload-pack", &option_upload_pack, "path",
67 "path to git-upload-pack on the remote"),
68 OPT_STRING(0, "depth", &option_depth, "depth",
69 "create a shallow clone of that depth"),
71 OPT_END()
74 static char *get_repo_path(const char *repo, int *is_bundle)
76 static char *suffix[] = { "/.git", ".git", "" };
77 static char *bundle_suffix[] = { ".bundle", "" };
78 struct stat st;
79 int i;
81 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
82 const char *path;
83 path = mkpath("%s%s", repo, suffix[i]);
84 if (is_directory(path)) {
85 *is_bundle = 0;
86 return xstrdup(make_nonrelative_path(path));
90 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
91 const char *path;
92 path = mkpath("%s%s", repo, bundle_suffix[i]);
93 if (!stat(path, &st) && S_ISREG(st.st_mode)) {
94 *is_bundle = 1;
95 return xstrdup(make_nonrelative_path(path));
99 return NULL;
102 static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
104 const char *end = repo + strlen(repo), *start;
107 * Strip trailing slashes and /.git
109 while (repo < end && is_dir_sep(end[-1]))
110 end--;
111 if (end - repo > 5 && is_dir_sep(end[-5]) &&
112 !strncmp(end - 4, ".git", 4)) {
113 end -= 5;
114 while (repo < end && is_dir_sep(end[-1]))
115 end--;
119 * Find last component, but be prepared that repo could have
120 * the form "remote.example.com:foo.git", i.e. no slash
121 * in the directory part.
123 start = end;
124 while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
125 start--;
128 * Strip .{bundle,git}.
130 if (is_bundle) {
131 if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
132 end -= 7;
133 } else {
134 if (end - start > 4 && !strncmp(end - 4, ".git", 4))
135 end -= 4;
138 if (is_bare) {
139 struct strbuf result = STRBUF_INIT;
140 strbuf_addf(&result, "%.*s.git", (int)(end - start), start);
141 return strbuf_detach(&result, 0);
144 return xstrndup(start, end - start);
147 static void strip_trailing_slashes(char *dir)
149 char *end = dir + strlen(dir);
151 while (dir < end - 1 && is_dir_sep(end[-1]))
152 end--;
153 *end = '\0';
156 static void setup_reference(const char *repo)
158 const char *ref_git;
159 char *ref_git_copy;
161 struct remote *remote;
162 struct transport *transport;
163 const struct ref *extra;
165 ref_git = make_absolute_path(option_reference);
167 if (is_directory(mkpath("%s/.git/objects", ref_git)))
168 ref_git = mkpath("%s/.git", ref_git);
169 else if (!is_directory(mkpath("%s/objects", ref_git)))
170 die("reference repository '%s' is not a local directory.",
171 option_reference);
173 ref_git_copy = xstrdup(ref_git);
175 add_to_alternates_file(ref_git_copy);
177 remote = remote_get(ref_git_copy);
178 transport = transport_get(remote, ref_git_copy);
179 for (extra = transport_get_remote_refs(transport); extra;
180 extra = extra->next)
181 add_extra_ref(extra->name, extra->old_sha1, 0);
183 transport_disconnect(transport);
185 free(ref_git_copy);
188 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
190 struct dirent *de;
191 struct stat buf;
192 int src_len, dest_len;
193 DIR *dir;
195 dir = opendir(src->buf);
196 if (!dir)
197 die("failed to open %s", src->buf);
199 if (mkdir(dest->buf, 0777)) {
200 if (errno != EEXIST)
201 die("failed to create directory %s", dest->buf);
202 else if (stat(dest->buf, &buf))
203 die("failed to stat %s", dest->buf);
204 else if (!S_ISDIR(buf.st_mode))
205 die("%s exists and is not a directory", dest->buf);
208 strbuf_addch(src, '/');
209 src_len = src->len;
210 strbuf_addch(dest, '/');
211 dest_len = dest->len;
213 while ((de = readdir(dir)) != NULL) {
214 strbuf_setlen(src, src_len);
215 strbuf_addstr(src, de->d_name);
216 strbuf_setlen(dest, dest_len);
217 strbuf_addstr(dest, de->d_name);
218 if (stat(src->buf, &buf)) {
219 warning ("failed to stat %s\n", src->buf);
220 continue;
222 if (S_ISDIR(buf.st_mode)) {
223 if (de->d_name[0] != '.')
224 copy_or_link_directory(src, dest);
225 continue;
228 if (unlink(dest->buf) && errno != ENOENT)
229 die("failed to unlink %s", dest->buf);
230 if (!option_no_hardlinks) {
231 if (!link(src->buf, dest->buf))
232 continue;
233 if (option_local)
234 die("failed to create link %s", dest->buf);
235 option_no_hardlinks = 1;
237 if (copy_file(dest->buf, src->buf, 0666))
238 die("failed to copy file to %s", dest->buf);
240 closedir(dir);
243 static const struct ref *clone_local(const char *src_repo,
244 const char *dest_repo)
246 const struct ref *ret;
247 struct strbuf src = STRBUF_INIT;
248 struct strbuf dest = STRBUF_INIT;
249 struct remote *remote;
250 struct transport *transport;
252 if (option_shared)
253 add_to_alternates_file(src_repo);
254 else {
255 strbuf_addf(&src, "%s/objects", src_repo);
256 strbuf_addf(&dest, "%s/objects", dest_repo);
257 copy_or_link_directory(&src, &dest);
258 strbuf_release(&src);
259 strbuf_release(&dest);
262 remote = remote_get(src_repo);
263 transport = transport_get(remote, src_repo);
264 ret = transport_get_remote_refs(transport);
265 transport_disconnect(transport);
266 return ret;
269 static const char *junk_work_tree;
270 static const char *junk_git_dir;
271 pid_t junk_pid;
273 static void remove_junk(void)
275 struct strbuf sb = STRBUF_INIT;
276 if (getpid() != junk_pid)
277 return;
278 if (junk_git_dir) {
279 strbuf_addstr(&sb, junk_git_dir);
280 remove_dir_recursively(&sb, 0);
281 strbuf_reset(&sb);
283 if (junk_work_tree) {
284 strbuf_addstr(&sb, junk_work_tree);
285 remove_dir_recursively(&sb, 0);
286 strbuf_reset(&sb);
290 static void remove_junk_on_signal(int signo)
292 remove_junk();
293 sigchain_pop(signo);
294 raise(signo);
297 static const struct ref *locate_head(const struct ref *refs,
298 const struct ref *mapped_refs,
299 const struct ref **remote_head_p)
301 const struct ref *remote_head = NULL;
302 const struct ref *remote_master = NULL;
303 const struct ref *r;
304 for (r = refs; r; r = r->next)
305 if (!strcmp(r->name, "HEAD"))
306 remote_head = r;
308 for (r = mapped_refs; r; r = r->next)
309 if (!strcmp(r->name, "refs/heads/master"))
310 remote_master = r;
312 if (remote_head_p)
313 *remote_head_p = remote_head;
315 /* If there's no HEAD value at all, never mind. */
316 if (!remote_head)
317 return NULL;
319 /* If refs/heads/master could be right, it is. */
320 if (remote_master && !hashcmp(remote_master->old_sha1,
321 remote_head->old_sha1))
322 return remote_master;
324 /* Look for another ref that points there */
325 for (r = mapped_refs; r; r = r->next)
326 if (r != remote_head &&
327 !hashcmp(r->old_sha1, remote_head->old_sha1))
328 return r;
330 /* Nothing is the same */
331 return NULL;
334 static struct ref *write_remote_refs(const struct ref *refs,
335 struct refspec *refspec, const char *reflog)
337 struct ref *local_refs = NULL;
338 struct ref **tail = &local_refs;
339 struct ref *r;
341 get_fetch_map(refs, refspec, &tail, 0);
342 if (!option_mirror)
343 get_fetch_map(refs, tag_refspec, &tail, 0);
345 for (r = local_refs; r; r = r->next)
346 add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
348 pack_refs(PACK_REFS_ALL);
349 clear_extra_refs();
351 return local_refs;
354 static void install_branch_config(const char *local,
355 const char *origin,
356 const char *remote)
358 struct strbuf key = STRBUF_INIT;
359 strbuf_addf(&key, "branch.%s.remote", local);
360 git_config_set(key.buf, origin);
361 strbuf_reset(&key);
362 strbuf_addf(&key, "branch.%s.merge", local);
363 git_config_set(key.buf, remote);
364 strbuf_release(&key);
367 int cmd_clone(int argc, const char **argv, const char *prefix)
369 int use_local_hardlinks = 1;
370 int use_separate_remote = 1;
371 int is_bundle = 0;
372 struct stat buf;
373 const char *repo_name, *repo, *work_tree, *git_dir;
374 char *path, *dir;
375 int dest_exists;
376 const struct ref *refs, *head_points_at, *remote_head, *mapped_refs;
377 struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
378 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
379 struct transport *transport = NULL;
380 char *src_ref_prefix = "refs/heads/";
381 int err = 0;
383 struct refspec refspec;
385 junk_pid = getpid();
387 argc = parse_options(argc, argv, builtin_clone_options,
388 builtin_clone_usage, 0);
390 if (argc == 0)
391 die("You must specify a repository to clone.");
393 if (option_no_hardlinks)
394 use_local_hardlinks = 0;
396 if (option_mirror)
397 option_bare = 1;
399 if (option_bare) {
400 if (option_origin)
401 die("--bare and --origin %s options are incompatible.",
402 option_origin);
403 option_no_checkout = 1;
404 use_separate_remote = 0;
407 if (!option_origin)
408 option_origin = "origin";
410 repo_name = argv[0];
412 path = get_repo_path(repo_name, &is_bundle);
413 if (path)
414 repo = xstrdup(make_nonrelative_path(repo_name));
415 else if (!strchr(repo_name, ':'))
416 repo = xstrdup(make_absolute_path(repo_name));
417 else
418 repo = repo_name;
420 if (argc == 2)
421 dir = xstrdup(argv[1]);
422 else
423 dir = guess_dir_name(repo_name, is_bundle, option_bare);
424 strip_trailing_slashes(dir);
426 dest_exists = !stat(dir, &buf);
427 if (dest_exists && !is_empty_dir(dir))
428 die("destination path '%s' already exists and is not "
429 "an empty directory.", dir);
431 strbuf_addf(&reflog_msg, "clone: from %s", repo);
433 if (option_bare)
434 work_tree = NULL;
435 else {
436 work_tree = getenv("GIT_WORK_TREE");
437 if (work_tree && !stat(work_tree, &buf))
438 die("working tree '%s' already exists.", work_tree);
441 if (option_bare || work_tree)
442 git_dir = xstrdup(dir);
443 else {
444 work_tree = dir;
445 git_dir = xstrdup(mkpath("%s/.git", dir));
448 if (!option_bare) {
449 junk_work_tree = work_tree;
450 if (safe_create_leading_directories_const(work_tree) < 0)
451 die("could not create leading directories of '%s': %s",
452 work_tree, strerror(errno));
453 if (!dest_exists && mkdir(work_tree, 0755))
454 die("could not create work tree dir '%s': %s.",
455 work_tree, strerror(errno));
456 set_git_work_tree(work_tree);
458 junk_git_dir = git_dir;
459 atexit(remove_junk);
460 sigchain_push_common(remove_junk_on_signal);
462 setenv(CONFIG_ENVIRONMENT, xstrdup(mkpath("%s/config", git_dir)), 1);
464 if (safe_create_leading_directories_const(git_dir) < 0)
465 die("could not create leading directories of '%s'", git_dir);
466 set_git_dir(make_absolute_path(git_dir));
468 init_db(option_template, option_quiet ? INIT_DB_QUIET : 0);
471 * At this point, the config exists, so we do not need the
472 * environment variable. We actually need to unset it, too, to
473 * re-enable parsing of the global configs.
475 unsetenv(CONFIG_ENVIRONMENT);
477 if (option_reference)
478 setup_reference(git_dir);
480 git_config(git_default_config, NULL);
482 if (option_bare) {
483 if (option_mirror)
484 src_ref_prefix = "refs/";
485 strbuf_addstr(&branch_top, src_ref_prefix);
487 git_config_set("core.bare", "true");
488 } else {
489 strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
492 if (option_mirror || !option_bare) {
493 /* Configure the remote */
494 if (option_mirror) {
495 strbuf_addf(&key, "remote.%s.mirror", option_origin);
496 git_config_set(key.buf, "true");
497 strbuf_reset(&key);
500 strbuf_addf(&key, "remote.%s.url", option_origin);
501 git_config_set(key.buf, repo);
502 strbuf_reset(&key);
504 strbuf_addf(&key, "remote.%s.fetch", option_origin);
505 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
506 git_config_set_multivar(key.buf, value.buf, "^$", 0);
507 strbuf_reset(&key);
508 strbuf_reset(&value);
511 refspec.force = 0;
512 refspec.pattern = 1;
513 refspec.src = src_ref_prefix;
514 refspec.dst = branch_top.buf;
516 if (path && !is_bundle)
517 refs = clone_local(path, git_dir);
518 else {
519 struct remote *remote = remote_get(argv[0]);
520 transport = transport_get(remote, remote->url[0]);
522 if (!transport->get_refs_list || !transport->fetch)
523 die("Don't know how to clone %s", transport->url);
525 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
527 if (option_depth)
528 transport_set_option(transport, TRANS_OPT_DEPTH,
529 option_depth);
531 if (option_quiet)
532 transport->verbose = -1;
533 else if (option_verbose)
534 transport->progress = 1;
536 if (option_upload_pack)
537 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
538 option_upload_pack);
540 refs = transport_get_remote_refs(transport);
541 if(refs)
542 transport_fetch_refs(transport, refs);
545 if (refs) {
546 clear_extra_refs();
548 mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
550 head_points_at = locate_head(refs, mapped_refs, &remote_head);
552 else {
553 warning("You appear to have cloned an empty repository.");
554 head_points_at = NULL;
555 remote_head = NULL;
556 option_no_checkout = 1;
557 if (!option_bare)
558 install_branch_config("master", option_origin,
559 "refs/heads/master");
562 if (head_points_at) {
563 /* Local default branch link */
564 create_symref("HEAD", head_points_at->name, NULL);
566 if (!option_bare) {
567 struct strbuf head_ref = STRBUF_INIT;
568 const char *head = head_points_at->name;
570 if (!prefixcmp(head, "refs/heads/"))
571 head += 11;
573 /* Set up the initial local branch */
575 /* Local branch initial value */
576 update_ref(reflog_msg.buf, "HEAD",
577 head_points_at->old_sha1,
578 NULL, 0, DIE_ON_ERR);
580 strbuf_addstr(&head_ref, branch_top.buf);
581 strbuf_addstr(&head_ref, "HEAD");
583 /* Remote branch link */
584 create_symref(head_ref.buf,
585 head_points_at->peer_ref->name,
586 reflog_msg.buf);
588 install_branch_config(head, option_origin,
589 head_points_at->name);
591 } else if (remote_head) {
592 /* Source had detached HEAD pointing somewhere. */
593 if (!option_bare)
594 update_ref(reflog_msg.buf, "HEAD",
595 remote_head->old_sha1,
596 NULL, REF_NODEREF, DIE_ON_ERR);
597 } else {
598 /* Nothing to checkout out */
599 if (!option_no_checkout)
600 warning("remote HEAD refers to nonexistent ref, "
601 "unable to checkout.\n");
602 option_no_checkout = 1;
605 if (transport)
606 transport_unlock_pack(transport);
608 if (!option_no_checkout) {
609 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
610 struct unpack_trees_options opts;
611 struct tree *tree;
612 struct tree_desc t;
613 int fd;
615 /* We need to be in the new work tree for the checkout */
616 setup_work_tree();
618 fd = hold_locked_index(lock_file, 1);
620 memset(&opts, 0, sizeof opts);
621 opts.update = 1;
622 opts.merge = 1;
623 opts.fn = oneway_merge;
624 opts.verbose_update = !option_quiet;
625 opts.src_index = &the_index;
626 opts.dst_index = &the_index;
628 tree = parse_tree_indirect(remote_head->old_sha1);
629 parse_tree(tree);
630 init_tree_desc(&t, tree->buffer, tree->size);
631 unpack_trees(1, &t, &opts);
633 if (write_cache(fd, active_cache, active_nr) ||
634 commit_locked_index(lock_file))
635 die("unable to write new index file");
637 err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1),
638 sha1_to_hex(remote_head->old_sha1), "1", NULL);
641 strbuf_release(&reflog_msg);
642 strbuf_release(&branch_top);
643 strbuf_release(&key);
644 strbuf_release(&value);
645 junk_pid = 0;
646 return err;