diff.renamelimit is a basic diff configuration
[git/gitweb-caching.git] / builtin-clone.c
blob863b22eeb2d22f031a9243155ba9df7be9a3b119
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"
23 * Overall FIXMEs:
24 * - respect DB_ENVIRONMENT for .git/objects.
26 * Implementation notes:
27 * - dropping use-separate-remote and no-separate-remote compatibility
30 static const char * const builtin_clone_usage[] = {
31 "git-clone [options] [--] <repo> [<dir>]",
32 NULL
35 static int option_quiet, option_no_checkout, option_bare;
36 static int option_local, option_no_hardlinks, option_shared;
37 static char *option_template, *option_reference, *option_depth;
38 static char *option_origin = NULL;
39 static char *option_upload_pack = "git-upload-pack";
41 static struct option builtin_clone_options[] = {
42 OPT__QUIET(&option_quiet),
43 OPT_BOOLEAN('n', "no-checkout", &option_no_checkout,
44 "don't create a checkout"),
45 OPT_BOOLEAN(0, "bare", &option_bare, "create a bare repository"),
46 OPT_BOOLEAN(0, "naked", &option_bare, "create a bare repository"),
47 OPT_BOOLEAN('l', "local", &option_local,
48 "to clone from a local repository"),
49 OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks,
50 "don't use local hardlinks, always copy"),
51 OPT_BOOLEAN('s', "shared", &option_shared,
52 "setup as shared repository"),
53 OPT_STRING(0, "template", &option_template, "path",
54 "path the template repository"),
55 OPT_STRING(0, "reference", &option_reference, "repo",
56 "reference repository"),
57 OPT_STRING('o', "origin", &option_origin, "branch",
58 "use <branch> instead or 'origin' to track upstream"),
59 OPT_STRING('u', "upload-pack", &option_upload_pack, "path",
60 "path to git-upload-pack on the remote"),
61 OPT_STRING(0, "depth", &option_depth, "depth",
62 "create a shallow clone of that depth"),
64 OPT_END()
67 static char *get_repo_path(const char *repo, int *is_bundle)
69 static char *suffix[] = { "/.git", ".git", "" };
70 static char *bundle_suffix[] = { ".bundle", "" };
71 struct stat st;
72 int i;
74 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
75 const char *path;
76 path = mkpath("%s%s", repo, suffix[i]);
77 if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
78 *is_bundle = 0;
79 return xstrdup(make_nonrelative_path(path));
83 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
84 const char *path;
85 path = mkpath("%s%s", repo, bundle_suffix[i]);
86 if (!stat(path, &st) && S_ISREG(st.st_mode)) {
87 *is_bundle = 1;
88 return xstrdup(make_nonrelative_path(path));
92 return NULL;
95 static char *guess_dir_name(const char *repo, int is_bundle)
97 const char *p, *start, *end, *limit;
98 int after_slash_or_colon;
100 /* Guess dir name from repository: strip trailing '/',
101 * strip trailing '[:/]*.{git,bundle}', strip leading '.*[/:]'. */
103 after_slash_or_colon = 1;
104 limit = repo + strlen(repo);
105 start = repo;
106 end = limit;
107 for (p = repo; p < limit; p++) {
108 const char *prefix = is_bundle ? ".bundle" : ".git";
109 if (!prefixcmp(p, prefix)) {
110 if (!after_slash_or_colon)
111 end = p;
112 p += strlen(prefix) - 1;
113 } else if (!prefixcmp(p, ".bundle")) {
114 if (!after_slash_or_colon)
115 end = p;
116 p += 7;
117 } else if (*p == '/' || *p == ':') {
118 if (end == limit)
119 end = p;
120 after_slash_or_colon = 1;
121 } else if (after_slash_or_colon) {
122 start = p;
123 end = limit;
124 after_slash_or_colon = 0;
128 return xstrndup(start, end - start);
131 static int is_directory(const char *path)
133 struct stat buf;
135 return !stat(path, &buf) && S_ISDIR(buf.st_mode);
138 static void setup_reference(const char *repo)
140 const char *ref_git;
141 char *ref_git_copy;
143 struct remote *remote;
144 struct transport *transport;
145 const struct ref *extra;
147 ref_git = make_absolute_path(option_reference);
149 if (is_directory(mkpath("%s/.git/objects", ref_git)))
150 ref_git = mkpath("%s/.git", ref_git);
151 else if (!is_directory(mkpath("%s/objects", ref_git)))
152 die("reference repository '%s' is not a local directory.",
153 option_reference);
155 ref_git_copy = xstrdup(ref_git);
157 add_to_alternates_file(ref_git_copy);
159 remote = remote_get(ref_git_copy);
160 transport = transport_get(remote, ref_git_copy);
161 for (extra = transport_get_remote_refs(transport); extra;
162 extra = extra->next)
163 add_extra_ref(extra->name, extra->old_sha1, 0);
165 transport_disconnect(transport);
167 free(ref_git_copy);
170 static void copy_or_link_directory(char *src, char *dest)
172 struct dirent *de;
173 struct stat buf;
174 int src_len, dest_len;
175 DIR *dir;
177 dir = opendir(src);
178 if (!dir)
179 die("failed to open %s\n", src);
181 if (mkdir(dest, 0777)) {
182 if (errno != EEXIST)
183 die("failed to create directory %s\n", dest);
184 else if (stat(dest, &buf))
185 die("failed to stat %s\n", dest);
186 else if (!S_ISDIR(buf.st_mode))
187 die("%s exists and is not a directory\n", dest);
190 src_len = strlen(src);
191 src[src_len] = '/';
192 dest_len = strlen(dest);
193 dest[dest_len] = '/';
195 while ((de = readdir(dir)) != NULL) {
196 strcpy(src + src_len + 1, de->d_name);
197 strcpy(dest + dest_len + 1, de->d_name);
198 if (stat(src, &buf)) {
199 warning ("failed to stat %s\n", src);
200 continue;
202 if (S_ISDIR(buf.st_mode)) {
203 if (de->d_name[0] != '.')
204 copy_or_link_directory(src, dest);
205 continue;
208 if (unlink(dest) && errno != ENOENT)
209 die("failed to unlink %s\n", dest);
210 if (!option_no_hardlinks) {
211 if (!link(src, dest))
212 continue;
213 if (option_local)
214 die("failed to create link %s\n", dest);
215 option_no_hardlinks = 1;
217 if (copy_file(dest, src, 0666))
218 die("failed to copy file to %s\n", dest);
220 closedir(dir);
223 static const struct ref *clone_local(const char *src_repo,
224 const char *dest_repo)
226 const struct ref *ret;
227 char src[PATH_MAX];
228 char dest[PATH_MAX];
229 struct remote *remote;
230 struct transport *transport;
232 if (option_shared)
233 add_to_alternates_file(src_repo);
234 else {
235 snprintf(src, PATH_MAX, "%s/objects", src_repo);
236 snprintf(dest, PATH_MAX, "%s/objects", dest_repo);
237 copy_or_link_directory(src, dest);
240 remote = remote_get(src_repo);
241 transport = transport_get(remote, src_repo);
242 ret = transport_get_remote_refs(transport);
243 transport_disconnect(transport);
244 return ret;
247 static const char *junk_work_tree;
248 static const char *junk_git_dir;
249 pid_t junk_pid;
251 static void remove_junk(void)
253 struct strbuf sb;
254 if (getpid() != junk_pid)
255 return;
256 strbuf_init(&sb, 0);
257 if (junk_git_dir) {
258 strbuf_addstr(&sb, junk_git_dir);
259 remove_dir_recursively(&sb, 0);
260 strbuf_reset(&sb);
262 if (junk_work_tree) {
263 strbuf_addstr(&sb, junk_work_tree);
264 remove_dir_recursively(&sb, 0);
265 strbuf_reset(&sb);
269 static void remove_junk_on_signal(int signo)
271 remove_junk();
272 signal(SIGINT, SIG_DFL);
273 raise(signo);
276 static const struct ref *locate_head(const struct ref *refs,
277 const struct ref *mapped_refs,
278 const struct ref **remote_head_p)
280 const struct ref *remote_head = NULL;
281 const struct ref *remote_master = NULL;
282 const struct ref *r;
283 for (r = refs; r; r = r->next)
284 if (!strcmp(r->name, "HEAD"))
285 remote_head = r;
287 for (r = mapped_refs; r; r = r->next)
288 if (!strcmp(r->name, "refs/heads/master"))
289 remote_master = r;
291 if (remote_head_p)
292 *remote_head_p = remote_head;
294 /* If there's no HEAD value at all, never mind. */
295 if (!remote_head)
296 return NULL;
298 /* If refs/heads/master could be right, it is. */
299 if (remote_master && !hashcmp(remote_master->old_sha1,
300 remote_head->old_sha1))
301 return remote_master;
303 /* Look for another ref that points there */
304 for (r = mapped_refs; r; r = r->next)
305 if (r != remote_head &&
306 !hashcmp(r->old_sha1, remote_head->old_sha1))
307 return r;
309 /* Nothing is the same */
310 return NULL;
313 static struct ref *write_remote_refs(const struct ref *refs,
314 struct refspec *refspec, const char *reflog)
316 struct ref *local_refs = NULL;
317 struct ref **tail = &local_refs;
318 struct ref *r;
320 get_fetch_map(refs, refspec, &tail, 0);
321 get_fetch_map(refs, tag_refspec, &tail, 0);
323 for (r = local_refs; r; r = r->next)
324 update_ref(reflog,
325 r->peer_ref->name, r->old_sha1, NULL, 0, DIE_ON_ERR);
326 return local_refs;
329 int cmd_clone(int argc, const char **argv, const char *prefix)
331 int use_local_hardlinks = 1;
332 int use_separate_remote = 1;
333 int is_bundle = 0;
334 struct stat buf;
335 const char *repo_name, *repo, *work_tree, *git_dir;
336 char *path, *dir;
337 const struct ref *refs, *head_points_at, *remote_head, *mapped_refs;
338 char branch_top[256], key[256], value[256];
339 struct strbuf reflog_msg;
340 struct transport *transport = NULL;
342 struct refspec refspec;
344 junk_pid = getpid();
346 argc = parse_options(argc, argv, builtin_clone_options,
347 builtin_clone_usage, 0);
349 if (argc == 0)
350 die("You must specify a repository to clone.");
352 if (option_no_hardlinks)
353 use_local_hardlinks = 0;
355 if (option_bare) {
356 if (option_origin)
357 die("--bare and --origin %s options are incompatible.",
358 option_origin);
359 option_no_checkout = 1;
360 use_separate_remote = 0;
363 if (!option_origin)
364 option_origin = "origin";
366 repo_name = argv[0];
368 path = get_repo_path(repo_name, &is_bundle);
369 if (path)
370 repo = path;
371 else if (!strchr(repo_name, ':'))
372 repo = xstrdup(make_absolute_path(repo_name));
373 else
374 repo = repo_name;
376 if (argc == 2)
377 dir = xstrdup(argv[1]);
378 else
379 dir = guess_dir_name(repo_name, is_bundle);
381 if (!stat(dir, &buf))
382 die("destination directory '%s' already exists.", dir);
384 strbuf_init(&reflog_msg, 0);
385 strbuf_addf(&reflog_msg, "clone: from %s", repo);
387 if (option_bare)
388 work_tree = NULL;
389 else {
390 work_tree = getenv("GIT_WORK_TREE");
391 if (work_tree && !stat(work_tree, &buf))
392 die("working tree '%s' already exists.", work_tree);
395 if (option_bare || work_tree)
396 git_dir = xstrdup(dir);
397 else {
398 work_tree = dir;
399 git_dir = xstrdup(mkpath("%s/.git", dir));
402 if (!option_bare) {
403 junk_work_tree = work_tree;
404 if (safe_create_leading_directories_const(work_tree) < 0)
405 die("could not create leading directories of '%s'",
406 work_tree);
407 if (mkdir(work_tree, 0755))
408 die("could not create work tree dir '%s'.", work_tree);
409 set_git_work_tree(work_tree);
411 junk_git_dir = git_dir;
412 atexit(remove_junk);
413 signal(SIGINT, remove_junk_on_signal);
415 setenv(CONFIG_ENVIRONMENT, xstrdup(mkpath("%s/config", git_dir)), 1);
417 if (safe_create_leading_directories_const(git_dir) < 0)
418 die("could not create leading directories of '%s'", git_dir);
419 set_git_dir(make_absolute_path(git_dir));
421 init_db(option_template, option_quiet ? INIT_DB_QUIET : 0);
424 * At this point, the config exists, so we do not need the
425 * environment variable. We actually need to unset it, too, to
426 * re-enable parsing of the global configs.
428 unsetenv(CONFIG_ENVIRONMENT);
430 if (option_reference)
431 setup_reference(git_dir);
433 git_config(git_default_config, NULL);
435 if (option_bare) {
436 strcpy(branch_top, "refs/heads/");
438 git_config_set("core.bare", "true");
439 } else {
440 snprintf(branch_top, sizeof(branch_top),
441 "refs/remotes/%s/", option_origin);
443 /* Configure the remote */
444 snprintf(key, sizeof(key), "remote.%s.url", option_origin);
445 git_config_set(key, repo);
447 snprintf(key, sizeof(key), "remote.%s.fetch", option_origin);
448 snprintf(value, sizeof(value),
449 "+refs/heads/*:%s*", branch_top);
450 git_config_set_multivar(key, value, "^$", 0);
453 refspec.force = 0;
454 refspec.pattern = 1;
455 refspec.src = "refs/heads/";
456 refspec.dst = branch_top;
458 if (path && !is_bundle)
459 refs = clone_local(path, git_dir);
460 else {
461 struct remote *remote = remote_get(argv[0]);
462 transport = transport_get(remote, remote->url[0]);
464 if (!transport->get_refs_list || !transport->fetch)
465 die("Don't know how to clone %s", transport->url);
467 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
469 if (option_depth)
470 transport_set_option(transport, TRANS_OPT_DEPTH,
471 option_depth);
473 if (option_quiet)
474 transport->verbose = -1;
476 if (option_upload_pack)
477 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
478 option_upload_pack);
480 refs = transport_get_remote_refs(transport);
481 transport_fetch_refs(transport, refs);
484 clear_extra_refs();
486 mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
488 head_points_at = locate_head(refs, mapped_refs, &remote_head);
490 if (head_points_at) {
491 /* Local default branch link */
492 create_symref("HEAD", head_points_at->name, NULL);
494 if (!option_bare) {
495 struct strbuf head_ref;
496 const char *head = head_points_at->name;
498 if (!prefixcmp(head, "refs/heads/"))
499 head += 11;
501 /* Set up the initial local branch */
503 /* Local branch initial value */
504 update_ref(reflog_msg.buf, "HEAD",
505 head_points_at->old_sha1,
506 NULL, 0, DIE_ON_ERR);
508 strbuf_init(&head_ref, 0);
509 strbuf_addstr(&head_ref, branch_top);
510 strbuf_addstr(&head_ref, "HEAD");
512 /* Remote branch link */
513 create_symref(head_ref.buf,
514 head_points_at->peer_ref->name,
515 reflog_msg.buf);
517 snprintf(key, sizeof(key), "branch.%s.remote", head);
518 git_config_set(key, option_origin);
519 snprintf(key, sizeof(key), "branch.%s.merge", head);
520 git_config_set(key, head_points_at->name);
522 } else if (remote_head) {
523 /* Source had detached HEAD pointing somewhere. */
524 if (!option_bare)
525 update_ref(reflog_msg.buf, "HEAD",
526 remote_head->old_sha1,
527 NULL, REF_NODEREF, DIE_ON_ERR);
528 } else {
529 /* Nothing to checkout out */
530 if (!option_no_checkout)
531 warning("remote HEAD refers to nonexistent ref, "
532 "unable to checkout.\n");
533 option_no_checkout = 1;
536 if (transport)
537 transport_unlock_pack(transport);
539 if (!option_no_checkout) {
540 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
541 struct unpack_trees_options opts;
542 struct tree *tree;
543 struct tree_desc t;
544 int fd;
546 /* We need to be in the new work tree for the checkout */
547 setup_work_tree();
549 fd = hold_locked_index(lock_file, 1);
551 memset(&opts, 0, sizeof opts);
552 opts.update = 1;
553 opts.merge = 1;
554 opts.fn = oneway_merge;
555 opts.verbose_update = !option_quiet;
556 opts.src_index = &the_index;
557 opts.dst_index = &the_index;
559 tree = parse_tree_indirect(remote_head->old_sha1);
560 parse_tree(tree);
561 init_tree_desc(&t, tree->buffer, tree->size);
562 unpack_trees(1, &t, &opts);
564 if (write_cache(fd, active_cache, active_nr) ||
565 commit_locked_index(lock_file))
566 die("unable to write new index file");
569 strbuf_release(&reflog_msg);
570 junk_pid = 0;
571 return 0;