]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-clone.c
git-instaweb: fix lighttpd configuration on cygwin
[thirdparty/git.git] / builtin-clone.c
CommitLineData
8434c2f1
DB
1/*
2 * Builtin "git clone"
3 *
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
7 *
8 * Clone a repository into a different directory that does not yet exist.
9 */
10
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"
3e8aded2 21#include "pack-refs.h"
4a16d072 22#include "sigchain.h"
8434c2f1
DB
23
24/*
25 * Overall FIXMEs:
26 * - respect DB_ENVIRONMENT for .git/objects.
27 *
28 * Implementation notes:
29 * - dropping use-separate-remote and no-separate-remote compatibility
30 *
31 */
32static const char * const builtin_clone_usage[] = {
1b1dd23f 33 "git clone [options] [--] <repo> [<dir>]",
8434c2f1
DB
34 NULL
35};
36
bc699afc 37static int option_quiet, option_no_checkout, option_bare, option_mirror;
8434c2f1
DB
38static int option_local, option_no_hardlinks, option_shared;
39static char *option_template, *option_reference, *option_depth;
40static char *option_origin = NULL;
41static char *option_upload_pack = "git-upload-pack";
21188b1e 42static int option_verbose;
8434c2f1
DB
43
44static struct option builtin_clone_options[] = {
45 OPT__QUIET(&option_quiet),
21188b1e 46 OPT__VERBOSE(&option_verbose),
8434c2f1
DB
47 OPT_BOOLEAN('n', "no-checkout", &option_no_checkout,
48 "don't create a checkout"),
49 OPT_BOOLEAN(0, "bare", &option_bare, "create a bare repository"),
50 OPT_BOOLEAN(0, "naked", &option_bare, "create a bare repository"),
bc699afc
JS
51 OPT_BOOLEAN(0, "mirror", &option_mirror,
52 "create a mirror repository (implies bare)"),
8434c2f1
DB
53 OPT_BOOLEAN('l', "local", &option_local,
54 "to clone from a local repository"),
55 OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks,
56 "don't use local hardlinks, always copy"),
57 OPT_BOOLEAN('s', "shared", &option_shared,
58 "setup as shared repository"),
59 OPT_STRING(0, "template", &option_template, "path",
60 "path the template repository"),
61 OPT_STRING(0, "reference", &option_reference, "repo",
62 "reference repository"),
63 OPT_STRING('o', "origin", &option_origin, "branch",
02ed2458 64 "use <branch> instead of 'origin' to track upstream"),
8434c2f1
DB
65 OPT_STRING('u', "upload-pack", &option_upload_pack, "path",
66 "path to git-upload-pack on the remote"),
67 OPT_STRING(0, "depth", &option_depth, "depth",
68 "create a shallow clone of that depth"),
69
70 OPT_END()
71};
72
73static char *get_repo_path(const char *repo, int *is_bundle)
74{
75 static char *suffix[] = { "/.git", ".git", "" };
76 static char *bundle_suffix[] = { ".bundle", "" };
77 struct stat st;
78 int i;
79
80 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
81 const char *path;
82 path = mkpath("%s%s", repo, suffix[i]);
90b4a71c 83 if (is_directory(path)) {
8434c2f1 84 *is_bundle = 0;
1b9a9467 85 return xstrdup(make_nonrelative_path(path));
8434c2f1
DB
86 }
87 }
88
89 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
90 const char *path;
91 path = mkpath("%s%s", repo, bundle_suffix[i]);
92 if (!stat(path, &st) && S_ISREG(st.st_mode)) {
93 *is_bundle = 1;
1b9a9467 94 return xstrdup(make_nonrelative_path(path));
8434c2f1
DB
95 }
96 }
97
98 return NULL;
99}
100
6612f877 101static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
8434c2f1 102{
b8c5db35
JS
103 const char *end = repo + strlen(repo), *start;
104
105 /*
106 * Strip trailing slashes and /.git
107 */
108 while (repo < end && is_dir_sep(end[-1]))
109 end--;
110 if (end - repo > 5 && is_dir_sep(end[-5]) &&
111 !strncmp(end - 4, ".git", 4)) {
112 end -= 5;
113 while (repo < end && is_dir_sep(end[-1]))
114 end--;
115 }
116
117 /*
118 * Find last component, but be prepared that repo could have
119 * the form "remote.example.com:foo.git", i.e. no slash
120 * in the directory part.
121 */
122 start = end;
123 while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
124 start--;
125
126 /*
127 * Strip .{bundle,git}.
128 */
129 if (is_bundle) {
130 if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
131 end -= 7;
132 } else {
133 if (end - start > 4 && !strncmp(end - 4, ".git", 4))
134 end -= 4;
8434c2f1
DB
135 }
136
6612f877 137 if (is_bare) {
32716a2c
MV
138 struct strbuf result = STRBUF_INIT;
139 strbuf_addf(&result, "%.*s.git", (int)(end - start), start);
140 return strbuf_detach(&result, 0);
6612f877
JS
141 }
142
8434c2f1
DB
143 return xstrndup(start, end - start);
144}
145
44a68fd5
CB
146static void strip_trailing_slashes(char *dir)
147{
148 char *end = dir + strlen(dir);
149
150 while (dir < end - 1 && is_dir_sep(end[-1]))
151 end--;
152 *end = '\0';
153}
154
8434c2f1
DB
155static void setup_reference(const char *repo)
156{
157 const char *ref_git;
158 char *ref_git_copy;
159
160 struct remote *remote;
161 struct transport *transport;
162 const struct ref *extra;
163
164 ref_git = make_absolute_path(option_reference);
165
166 if (is_directory(mkpath("%s/.git/objects", ref_git)))
167 ref_git = mkpath("%s/.git", ref_git);
168 else if (!is_directory(mkpath("%s/objects", ref_git)))
169 die("reference repository '%s' is not a local directory.",
170 option_reference);
171
172 ref_git_copy = xstrdup(ref_git);
173
174 add_to_alternates_file(ref_git_copy);
175
176 remote = remote_get(ref_git_copy);
177 transport = transport_get(remote, ref_git_copy);
178 for (extra = transport_get_remote_refs(transport); extra;
179 extra = extra->next)
180 add_extra_ref(extra->name, extra->old_sha1, 0);
181
182 transport_disconnect(transport);
183
184 free(ref_git_copy);
185}
186
b9e125e0 187static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
8434c2f1
DB
188{
189 struct dirent *de;
190 struct stat buf;
191 int src_len, dest_len;
192 DIR *dir;
193
b9e125e0 194 dir = opendir(src->buf);
8434c2f1 195 if (!dir)
d7530708 196 die("failed to open %s", src->buf);
8434c2f1 197
b9e125e0 198 if (mkdir(dest->buf, 0777)) {
8434c2f1 199 if (errno != EEXIST)
d7530708 200 die("failed to create directory %s", dest->buf);
b9e125e0 201 else if (stat(dest->buf, &buf))
d7530708 202 die("failed to stat %s", dest->buf);
8434c2f1 203 else if (!S_ISDIR(buf.st_mode))
d7530708 204 die("%s exists and is not a directory", dest->buf);
8434c2f1
DB
205 }
206
b9e125e0
MV
207 strbuf_addch(src, '/');
208 src_len = src->len;
209 strbuf_addch(dest, '/');
210 dest_len = dest->len;
8434c2f1
DB
211
212 while ((de = readdir(dir)) != NULL) {
b9e125e0
MV
213 strbuf_setlen(src, src_len);
214 strbuf_addstr(src, de->d_name);
215 strbuf_setlen(dest, dest_len);
216 strbuf_addstr(dest, de->d_name);
217 if (stat(src->buf, &buf)) {
218 warning ("failed to stat %s\n", src->buf);
8434c2f1
DB
219 continue;
220 }
221 if (S_ISDIR(buf.st_mode)) {
222 if (de->d_name[0] != '.')
223 copy_or_link_directory(src, dest);
224 continue;
225 }
226
b9e125e0 227 if (unlink(dest->buf) && errno != ENOENT)
d7530708 228 die("failed to unlink %s", dest->buf);
fdabc242 229 if (!option_no_hardlinks) {
b9e125e0 230 if (!link(src->buf, dest->buf))
fdabc242
DB
231 continue;
232 if (option_local)
d7530708 233 die("failed to create link %s", dest->buf);
fdabc242 234 option_no_hardlinks = 1;
8434c2f1 235 }
b9e125e0 236 if (copy_file(dest->buf, src->buf, 0666))
d7530708 237 die("failed to copy file to %s", dest->buf);
8434c2f1 238 }
689ef4d4 239 closedir(dir);
8434c2f1
DB
240}
241
242static const struct ref *clone_local(const char *src_repo,
243 const char *dest_repo)
244{
245 const struct ref *ret;
b9e125e0
MV
246 struct strbuf src = STRBUF_INIT;
247 struct strbuf dest = STRBUF_INIT;
8434c2f1
DB
248 struct remote *remote;
249 struct transport *transport;
250
251 if (option_shared)
252 add_to_alternates_file(src_repo);
253 else {
b9e125e0
MV
254 strbuf_addf(&src, "%s/objects", src_repo);
255 strbuf_addf(&dest, "%s/objects", dest_repo);
256 copy_or_link_directory(&src, &dest);
257 strbuf_release(&src);
258 strbuf_release(&dest);
8434c2f1
DB
259 }
260
261 remote = remote_get(src_repo);
262 transport = transport_get(remote, src_repo);
263 ret = transport_get_remote_refs(transport);
264 transport_disconnect(transport);
265 return ret;
266}
267
268static const char *junk_work_tree;
269static const char *junk_git_dir;
270pid_t junk_pid;
271
272static void remove_junk(void)
273{
f285a2d7 274 struct strbuf sb = STRBUF_INIT;
8434c2f1
DB
275 if (getpid() != junk_pid)
276 return;
8434c2f1
DB
277 if (junk_git_dir) {
278 strbuf_addstr(&sb, junk_git_dir);
279 remove_dir_recursively(&sb, 0);
280 strbuf_reset(&sb);
281 }
282 if (junk_work_tree) {
283 strbuf_addstr(&sb, junk_work_tree);
284 remove_dir_recursively(&sb, 0);
285 strbuf_reset(&sb);
286 }
287}
288
289static void remove_junk_on_signal(int signo)
290{
291 remove_junk();
4a16d072 292 sigchain_pop(signo);
8434c2f1
DB
293 raise(signo);
294}
295
296static const struct ref *locate_head(const struct ref *refs,
297 const struct ref *mapped_refs,
298 const struct ref **remote_head_p)
299{
300 const struct ref *remote_head = NULL;
301 const struct ref *remote_master = NULL;
302 const struct ref *r;
303 for (r = refs; r; r = r->next)
304 if (!strcmp(r->name, "HEAD"))
305 remote_head = r;
306
307 for (r = mapped_refs; r; r = r->next)
308 if (!strcmp(r->name, "refs/heads/master"))
309 remote_master = r;
310
311 if (remote_head_p)
312 *remote_head_p = remote_head;
313
314 /* If there's no HEAD value at all, never mind. */
315 if (!remote_head)
316 return NULL;
317
318 /* If refs/heads/master could be right, it is. */
319 if (remote_master && !hashcmp(remote_master->old_sha1,
320 remote_head->old_sha1))
321 return remote_master;
322
323 /* Look for another ref that points there */
324 for (r = mapped_refs; r; r = r->next)
325 if (r != remote_head &&
326 !hashcmp(r->old_sha1, remote_head->old_sha1))
327 return r;
328
329 /* Nothing is the same */
330 return NULL;
331}
332
333static struct ref *write_remote_refs(const struct ref *refs,
334 struct refspec *refspec, const char *reflog)
335{
336 struct ref *local_refs = NULL;
337 struct ref **tail = &local_refs;
338 struct ref *r;
339
340 get_fetch_map(refs, refspec, &tail, 0);
468386a9
JS
341 if (!option_mirror)
342 get_fetch_map(refs, tag_refspec, &tail, 0);
8434c2f1
DB
343
344 for (r = local_refs; r; r = r->next)
3e8aded2
JH
345 add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
346
347 pack_refs(PACK_REFS_ALL);
348 clear_extra_refs();
349
8434c2f1
DB
350 return local_refs;
351}
352
5cd12b85
JH
353static void install_branch_config(const char *local,
354 const char *origin,
355 const char *remote)
356{
357 struct strbuf key = STRBUF_INIT;
358 strbuf_addf(&key, "branch.%s.remote", local);
359 git_config_set(key.buf, origin);
360 strbuf_reset(&key);
361 strbuf_addf(&key, "branch.%s.merge", local);
362 git_config_set(key.buf, remote);
363 strbuf_release(&key);
364}
365
8434c2f1
DB
366int cmd_clone(int argc, const char **argv, const char *prefix)
367{
8434c2f1
DB
368 int is_bundle = 0;
369 struct stat buf;
370 const char *repo_name, *repo, *work_tree, *git_dir;
371 char *path, *dir;
55892d23 372 int dest_exists;
8434c2f1 373 const struct ref *refs, *head_points_at, *remote_head, *mapped_refs;
b5ff37ac
MV
374 struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
375 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
1db4a75c 376 struct transport *transport = NULL;
bc699afc 377 char *src_ref_prefix = "refs/heads/";
8434c2f1
DB
378
379 struct refspec refspec;
380
381 junk_pid = getpid();
382
383 argc = parse_options(argc, argv, builtin_clone_options,
384 builtin_clone_usage, 0);
385
386 if (argc == 0)
387 die("You must specify a repository to clone.");
388
bc699afc
JS
389 if (option_mirror)
390 option_bare = 1;
391
8434c2f1
DB
392 if (option_bare) {
393 if (option_origin)
394 die("--bare and --origin %s options are incompatible.",
395 option_origin);
396 option_no_checkout = 1;
8434c2f1
DB
397 }
398
399 if (!option_origin)
400 option_origin = "origin";
401
402 repo_name = argv[0];
403
404 path = get_repo_path(repo_name, &is_bundle);
405 if (path)
86521aca 406 repo = xstrdup(make_nonrelative_path(repo_name));
8434c2f1
DB
407 else if (!strchr(repo_name, ':'))
408 repo = xstrdup(make_absolute_path(repo_name));
409 else
410 repo = repo_name;
411
412 if (argc == 2)
413 dir = xstrdup(argv[1]);
414 else
6612f877 415 dir = guess_dir_name(repo_name, is_bundle, option_bare);
44a68fd5 416 strip_trailing_slashes(dir);
8434c2f1 417
55892d23
AP
418 dest_exists = !stat(dir, &buf);
419 if (dest_exists && !is_empty_dir(dir))
420 die("destination path '%s' already exists and is not "
421 "an empty directory.", dir);
8434c2f1 422
8434c2f1
DB
423 strbuf_addf(&reflog_msg, "clone: from %s", repo);
424
425 if (option_bare)
426 work_tree = NULL;
427 else {
428 work_tree = getenv("GIT_WORK_TREE");
429 if (work_tree && !stat(work_tree, &buf))
430 die("working tree '%s' already exists.", work_tree);
431 }
432
433 if (option_bare || work_tree)
434 git_dir = xstrdup(dir);
435 else {
436 work_tree = dir;
437 git_dir = xstrdup(mkpath("%s/.git", dir));
438 }
439
440 if (!option_bare) {
441 junk_work_tree = work_tree;
8e21d63b 442 if (safe_create_leading_directories_const(work_tree) < 0)
44a68fd5
CB
443 die("could not create leading directories of '%s': %s",
444 work_tree, strerror(errno));
55892d23 445 if (!dest_exists && mkdir(work_tree, 0755))
44a68fd5
CB
446 die("could not create work tree dir '%s': %s.",
447 work_tree, strerror(errno));
8434c2f1
DB
448 set_git_work_tree(work_tree);
449 }
450 junk_git_dir = git_dir;
451 atexit(remove_junk);
57b235a4 452 sigchain_push_common(remove_junk_on_signal);
8434c2f1
DB
453
454 setenv(CONFIG_ENVIRONMENT, xstrdup(mkpath("%s/config", git_dir)), 1);
455
8e21d63b
JK
456 if (safe_create_leading_directories_const(git_dir) < 0)
457 die("could not create leading directories of '%s'", git_dir);
8434c2f1
DB
458 set_git_dir(make_absolute_path(git_dir));
459
8434c2f1
DB
460 init_db(option_template, option_quiet ? INIT_DB_QUIET : 0);
461
5b8063b5
JS
462 /*
463 * At this point, the config exists, so we do not need the
464 * environment variable. We actually need to unset it, too, to
465 * re-enable parsing of the global configs.
466 */
467 unsetenv(CONFIG_ENVIRONMENT);
468
8434c2f1
DB
469 if (option_reference)
470 setup_reference(git_dir);
471
9bd81e42 472 git_config(git_default_config, NULL);
8434c2f1
DB
473
474 if (option_bare) {
bc699afc
JS
475 if (option_mirror)
476 src_ref_prefix = "refs/";
b5ff37ac 477 strbuf_addstr(&branch_top, src_ref_prefix);
8434c2f1
DB
478
479 git_config_set("core.bare", "true");
480 } else {
b5ff37ac 481 strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
bc699afc 482 }
8434c2f1 483
bc699afc 484 if (option_mirror || !option_bare) {
8434c2f1 485 /* Configure the remote */
bc699afc 486 if (option_mirror) {
b5ff37ac
MV
487 strbuf_addf(&key, "remote.%s.mirror", option_origin);
488 git_config_set(key.buf, "true");
489 strbuf_reset(&key);
bc699afc
JS
490 }
491
b5ff37ac
MV
492 strbuf_addf(&key, "remote.%s.url", option_origin);
493 git_config_set(key.buf, repo);
494 strbuf_reset(&key);
8434c2f1 495
b5ff37ac
MV
496 strbuf_addf(&key, "remote.%s.fetch", option_origin);
497 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
498 git_config_set_multivar(key.buf, value.buf, "^$", 0);
499 strbuf_reset(&key);
500 strbuf_reset(&value);
8434c2f1
DB
501 }
502
503 refspec.force = 0;
504 refspec.pattern = 1;
bc699afc 505 refspec.src = src_ref_prefix;
b5ff37ac 506 refspec.dst = branch_top.buf;
8434c2f1
DB
507
508 if (path && !is_bundle)
509 refs = clone_local(path, git_dir);
510 else {
511 struct remote *remote = remote_get(argv[0]);
1db4a75c 512 transport = transport_get(remote, remote->url[0]);
8434c2f1 513
37b78c25
JK
514 if (!transport->get_refs_list || !transport->fetch)
515 die("Don't know how to clone %s", transport->url);
516
8434c2f1
DB
517 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
518
519 if (option_depth)
520 transport_set_option(transport, TRANS_OPT_DEPTH,
521 option_depth);
522
523 if (option_quiet)
524 transport->verbose = -1;
21188b1e
MV
525 else if (option_verbose)
526 transport->progress = 1;
8434c2f1 527
837c8767
SH
528 if (option_upload_pack)
529 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
530 option_upload_pack);
531
8434c2f1 532 refs = transport_get_remote_refs(transport);
86ac7518
SR
533 if(refs)
534 transport_fetch_refs(transport, refs);
8434c2f1
DB
535 }
536
86ac7518
SR
537 if (refs) {
538 clear_extra_refs();
8434c2f1 539
86ac7518 540 mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
8434c2f1 541
86ac7518
SR
542 head_points_at = locate_head(refs, mapped_refs, &remote_head);
543 }
544 else {
545 warning("You appear to have cloned an empty repository.");
546 head_points_at = NULL;
547 remote_head = NULL;
548 option_no_checkout = 1;
5cd12b85
JH
549 if (!option_bare)
550 install_branch_config("master", option_origin,
551 "refs/heads/master");
86ac7518 552 }
8434c2f1
DB
553
554 if (head_points_at) {
555 /* Local default branch link */
556 create_symref("HEAD", head_points_at->name, NULL);
557
558 if (!option_bare) {
f285a2d7 559 struct strbuf head_ref = STRBUF_INIT;
8434c2f1
DB
560 const char *head = head_points_at->name;
561
562 if (!prefixcmp(head, "refs/heads/"))
563 head += 11;
564
565 /* Set up the initial local branch */
566
567 /* Local branch initial value */
568 update_ref(reflog_msg.buf, "HEAD",
569 head_points_at->old_sha1,
570 NULL, 0, DIE_ON_ERR);
571
b5ff37ac 572 strbuf_addstr(&head_ref, branch_top.buf);
8434c2f1
DB
573 strbuf_addstr(&head_ref, "HEAD");
574
575 /* Remote branch link */
576 create_symref(head_ref.buf,
577 head_points_at->peer_ref->name,
578 reflog_msg.buf);
579
5cd12b85
JH
580 install_branch_config(head, option_origin,
581 head_points_at->name);
8434c2f1
DB
582 }
583 } else if (remote_head) {
584 /* Source had detached HEAD pointing somewhere. */
585 if (!option_bare)
586 update_ref(reflog_msg.buf, "HEAD",
587 remote_head->old_sha1,
588 NULL, REF_NODEREF, DIE_ON_ERR);
589 } else {
590 /* Nothing to checkout out */
591 if (!option_no_checkout)
592 warning("remote HEAD refers to nonexistent ref, "
593 "unable to checkout.\n");
594 option_no_checkout = 1;
595 }
596
1db4a75c
SP
597 if (transport)
598 transport_unlock_pack(transport);
599
8434c2f1
DB
600 if (!option_no_checkout) {
601 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
602 struct unpack_trees_options opts;
603 struct tree *tree;
604 struct tree_desc t;
605 int fd;
606
607 /* We need to be in the new work tree for the checkout */
608 setup_work_tree();
609
610 fd = hold_locked_index(lock_file, 1);
611
612 memset(&opts, 0, sizeof opts);
613 opts.update = 1;
a73bc127
JS
614 opts.merge = 1;
615 opts.fn = oneway_merge;
8434c2f1 616 opts.verbose_update = !option_quiet;
a73bc127 617 opts.src_index = &the_index;
8434c2f1
DB
618 opts.dst_index = &the_index;
619
620 tree = parse_tree_indirect(remote_head->old_sha1);
621 parse_tree(tree);
622 init_tree_desc(&t, tree->buffer, tree->size);
623 unpack_trees(1, &t, &opts);
624
625 if (write_cache(fd, active_cache, active_nr) ||
626 commit_locked_index(lock_file))
627 die("unable to write new index file");
628 }
629
630 strbuf_release(&reflog_msg);
b5ff37ac
MV
631 strbuf_release(&branch_top);
632 strbuf_release(&key);
633 strbuf_release(&value);
8434c2f1
DB
634 junk_pid = 0;
635 return 0;
636}