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