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