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.
12 #include "parse-options.h"
13 #include "fetch-pack.h"
16 #include "tree-walk.h"
17 #include "unpack-trees.h"
18 #include "transport.h"
21 #include "pack-refs.h"
26 * - respect DB_ENVIRONMENT for .git/objects.
28 * Implementation notes:
29 * - dropping use-separate-remote and no-separate-remote compatibility
32 static const char * const builtin_clone_usage
[] = {
33 "git clone [options] [--] <repo> [<dir>]",
37 static int option_quiet
, option_no_checkout
, option_bare
, option_mirror
;
38 static int option_local
, option_no_hardlinks
, option_shared
;
39 static char *option_template
, *option_reference
, *option_depth
;
40 static char *option_origin
= NULL
;
41 static char *option_upload_pack
= "git-upload-pack";
42 static int option_verbose
;
44 static struct option builtin_clone_options
[] = {
45 OPT__QUIET(&option_quiet
),
46 OPT__VERBOSE(&option_verbose
),
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"),
51 OPT_BOOLEAN(0, "mirror", &option_mirror
,
52 "create a mirror repository (implies bare)"),
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",
64 "use <branch> instead of 'origin' to track upstream"),
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"),
73 static char *get_repo_path(const char *repo
, int *is_bundle
)
75 static char *suffix
[] = { "/.git", ".git", "" };
76 static char *bundle_suffix
[] = { ".bundle", "" };
80 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
82 path
= mkpath("%s%s", repo
, suffix
[i
]);
83 if (is_directory(path
)) {
85 return xstrdup(make_nonrelative_path(path
));
89 for (i
= 0; i
< ARRAY_SIZE(bundle_suffix
); i
++) {
91 path
= mkpath("%s%s", repo
, bundle_suffix
[i
]);
92 if (!stat(path
, &st
) && S_ISREG(st
.st_mode
)) {
94 return xstrdup(make_nonrelative_path(path
));
101 static char *guess_dir_name(const char *repo
, int is_bundle
, int is_bare
)
103 const char *end
= repo
+ strlen(repo
), *start
;
106 * Strip trailing slashes and /.git
108 while (repo
< end
&& is_dir_sep(end
[-1]))
110 if (end
- repo
> 5 && is_dir_sep(end
[-5]) &&
111 !strncmp(end
- 4, ".git", 4)) {
113 while (repo
< end
&& is_dir_sep(end
[-1]))
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.
123 while (repo
< start
&& !is_dir_sep(start
[-1]) && start
[-1] != ':')
127 * Strip .{bundle,git}.
130 if (end
- start
> 7 && !strncmp(end
- 7, ".bundle", 7))
133 if (end
- start
> 4 && !strncmp(end
- 4, ".git", 4))
138 struct strbuf result
= STRBUF_INIT
;
139 strbuf_addf(&result
, "%.*s.git", (int)(end
- start
), start
);
140 return strbuf_detach(&result
, 0);
143 return xstrndup(start
, end
- start
);
146 static void strip_trailing_slashes(char *dir
)
148 char *end
= dir
+ strlen(dir
);
150 while (dir
< end
- 1 && is_dir_sep(end
[-1]))
155 static void setup_reference(const char *repo
)
160 struct remote
*remote
;
161 struct transport
*transport
;
162 const struct ref
*extra
;
164 ref_git
= make_absolute_path(option_reference
);
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.",
172 ref_git_copy
= xstrdup(ref_git
);
174 add_to_alternates_file(ref_git_copy
);
176 remote
= remote_get(ref_git_copy
);
177 transport
= transport_get(remote
, ref_git_copy
);
178 for (extra
= transport_get_remote_refs(transport
); extra
;
180 add_extra_ref(extra
->name
, extra
->old_sha1
, 0);
182 transport_disconnect(transport
);
187 static void copy_or_link_directory(struct strbuf
*src
, struct strbuf
*dest
)
191 int src_len
, dest_len
;
194 dir
= opendir(src
->buf
);
196 die("failed to open %s", src
->buf
);
198 if (mkdir(dest
->buf
, 0777)) {
200 die("failed to create directory %s", dest
->buf
);
201 else if (stat(dest
->buf
, &buf
))
202 die("failed to stat %s", dest
->buf
);
203 else if (!S_ISDIR(buf
.st_mode
))
204 die("%s exists and is not a directory", dest
->buf
);
207 strbuf_addch(src
, '/');
209 strbuf_addch(dest
, '/');
210 dest_len
= dest
->len
;
212 while ((de
= readdir(dir
)) != NULL
) {
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
);
221 if (S_ISDIR(buf
.st_mode
)) {
222 if (de
->d_name
[0] != '.')
223 copy_or_link_directory(src
, dest
);
227 if (unlink(dest
->buf
) && errno
!= ENOENT
)
228 die("failed to unlink %s", dest
->buf
);
229 if (!option_no_hardlinks
) {
230 if (!link(src
->buf
, dest
->buf
))
233 die("failed to create link %s", dest
->buf
);
234 option_no_hardlinks
= 1;
236 if (copy_file(dest
->buf
, src
->buf
, 0666))
237 die("failed to copy file to %s", dest
->buf
);
242 static const struct ref
*clone_local(const char *src_repo
,
243 const char *dest_repo
)
245 const struct ref
*ret
;
246 struct strbuf src
= STRBUF_INIT
;
247 struct strbuf dest
= STRBUF_INIT
;
248 struct remote
*remote
;
249 struct transport
*transport
;
252 add_to_alternates_file(src_repo
);
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
);
261 remote
= remote_get(src_repo
);
262 transport
= transport_get(remote
, src_repo
);
263 ret
= transport_get_remote_refs(transport
);
264 transport_disconnect(transport
);
268 static const char *junk_work_tree
;
269 static const char *junk_git_dir
;
272 static void remove_junk(void)
274 struct strbuf sb
= STRBUF_INIT
;
275 if (getpid() != junk_pid
)
278 strbuf_addstr(&sb
, junk_git_dir
);
279 remove_dir_recursively(&sb
, 0);
282 if (junk_work_tree
) {
283 strbuf_addstr(&sb
, junk_work_tree
);
284 remove_dir_recursively(&sb
, 0);
289 static void remove_junk_on_signal(int signo
)
296 static const struct ref
*locate_head(const struct ref
*refs
,
297 const struct ref
*mapped_refs
,
298 const struct ref
**remote_head_p
)
300 const struct ref
*remote_head
= NULL
;
301 const struct ref
*remote_master
= NULL
;
303 for (r
= refs
; r
; r
= r
->next
)
304 if (!strcmp(r
->name
, "HEAD"))
307 for (r
= mapped_refs
; r
; r
= r
->next
)
308 if (!strcmp(r
->name
, "refs/heads/master"))
312 *remote_head_p
= remote_head
;
314 /* If there's no HEAD value at all, never mind. */
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
;
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
))
329 /* Nothing is the same */
333 static struct ref
*write_remote_refs(const struct ref
*refs
,
334 struct refspec
*refspec
, const char *reflog
)
336 struct ref
*local_refs
= NULL
;
337 struct ref
**tail
= &local_refs
;
340 get_fetch_map(refs
, refspec
, &tail
, 0);
342 get_fetch_map(refs
, tag_refspec
, &tail
, 0);
344 for (r
= local_refs
; r
; r
= r
->next
)
345 add_extra_ref(r
->peer_ref
->name
, r
->old_sha1
, 0);
347 pack_refs(PACK_REFS_ALL
);
353 static void install_branch_config(const char *local
,
357 struct strbuf key
= STRBUF_INIT
;
358 strbuf_addf(&key
, "branch.%s.remote", local
);
359 git_config_set(key
.buf
, origin
);
361 strbuf_addf(&key
, "branch.%s.merge", local
);
362 git_config_set(key
.buf
, remote
);
363 strbuf_release(&key
);
366 int cmd_clone(int argc
, const char **argv
, const char *prefix
)
368 int use_local_hardlinks
= 1;
369 int use_separate_remote
= 1;
372 const char *repo_name
, *repo
, *work_tree
, *git_dir
;
375 const struct ref
*refs
, *head_points_at
, *remote_head
, *mapped_refs
;
376 struct strbuf key
= STRBUF_INIT
, value
= STRBUF_INIT
;
377 struct strbuf branch_top
= STRBUF_INIT
, reflog_msg
= STRBUF_INIT
;
378 struct transport
*transport
= NULL
;
379 char *src_ref_prefix
= "refs/heads/";
381 struct refspec refspec
;
385 argc
= parse_options(argc
, argv
, builtin_clone_options
,
386 builtin_clone_usage
, 0);
389 die("You must specify a repository to clone.");
391 if (option_no_hardlinks
)
392 use_local_hardlinks
= 0;
399 die("--bare and --origin %s options are incompatible.",
401 option_no_checkout
= 1;
402 use_separate_remote
= 0;
406 option_origin
= "origin";
410 path
= get_repo_path(repo_name
, &is_bundle
);
412 repo
= xstrdup(make_nonrelative_path(repo_name
));
413 else if (!strchr(repo_name
, ':'))
414 repo
= xstrdup(make_absolute_path(repo_name
));
419 dir
= xstrdup(argv
[1]);
421 dir
= guess_dir_name(repo_name
, is_bundle
, option_bare
);
422 strip_trailing_slashes(dir
);
424 dest_exists
= !stat(dir
, &buf
);
425 if (dest_exists
&& !is_empty_dir(dir
))
426 die("destination path '%s' already exists and is not "
427 "an empty directory.", dir
);
429 strbuf_addf(&reflog_msg
, "clone: from %s", repo
);
434 work_tree
= getenv("GIT_WORK_TREE");
435 if (work_tree
&& !stat(work_tree
, &buf
))
436 die("working tree '%s' already exists.", work_tree
);
439 if (option_bare
|| work_tree
)
440 git_dir
= xstrdup(dir
);
443 git_dir
= xstrdup(mkpath("%s/.git", dir
));
447 junk_work_tree
= work_tree
;
448 if (safe_create_leading_directories_const(work_tree
) < 0)
449 die("could not create leading directories of '%s': %s",
450 work_tree
, strerror(errno
));
451 if (!dest_exists
&& mkdir(work_tree
, 0755))
452 die("could not create work tree dir '%s': %s.",
453 work_tree
, strerror(errno
));
454 set_git_work_tree(work_tree
);
456 junk_git_dir
= git_dir
;
458 sigchain_push_common(remove_junk_on_signal
);
460 setenv(CONFIG_ENVIRONMENT
, xstrdup(mkpath("%s/config", git_dir
)), 1);
462 if (safe_create_leading_directories_const(git_dir
) < 0)
463 die("could not create leading directories of '%s'", git_dir
);
464 set_git_dir(make_absolute_path(git_dir
));
466 init_db(option_template
, option_quiet
? INIT_DB_QUIET
: 0);
469 * At this point, the config exists, so we do not need the
470 * environment variable. We actually need to unset it, too, to
471 * re-enable parsing of the global configs.
473 unsetenv(CONFIG_ENVIRONMENT
);
475 if (option_reference
)
476 setup_reference(git_dir
);
478 git_config(git_default_config
, NULL
);
482 src_ref_prefix
= "refs/";
483 strbuf_addstr(&branch_top
, src_ref_prefix
);
485 git_config_set("core.bare", "true");
487 strbuf_addf(&branch_top
, "refs/remotes/%s/", option_origin
);
490 if (option_mirror
|| !option_bare
) {
491 /* Configure the remote */
493 strbuf_addf(&key
, "remote.%s.mirror", option_origin
);
494 git_config_set(key
.buf
, "true");
498 strbuf_addf(&key
, "remote.%s.url", option_origin
);
499 git_config_set(key
.buf
, repo
);
502 strbuf_addf(&key
, "remote.%s.fetch", option_origin
);
503 strbuf_addf(&value
, "+%s*:%s*", src_ref_prefix
, branch_top
.buf
);
504 git_config_set_multivar(key
.buf
, value
.buf
, "^$", 0);
506 strbuf_reset(&value
);
511 refspec
.src
= src_ref_prefix
;
512 refspec
.dst
= branch_top
.buf
;
514 if (path
&& !is_bundle
)
515 refs
= clone_local(path
, git_dir
);
517 struct remote
*remote
= remote_get(argv
[0]);
518 transport
= transport_get(remote
, remote
->url
[0]);
520 if (!transport
->get_refs_list
|| !transport
->fetch
)
521 die("Don't know how to clone %s", transport
->url
);
523 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
526 transport_set_option(transport
, TRANS_OPT_DEPTH
,
530 transport
->verbose
= -1;
531 else if (option_verbose
)
532 transport
->progress
= 1;
534 if (option_upload_pack
)
535 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
,
538 refs
= transport_get_remote_refs(transport
);
540 transport_fetch_refs(transport
, refs
);
546 mapped_refs
= write_remote_refs(refs
, &refspec
, reflog_msg
.buf
);
548 head_points_at
= locate_head(refs
, mapped_refs
, &remote_head
);
551 warning("You appear to have cloned an empty repository.");
552 head_points_at
= NULL
;
554 option_no_checkout
= 1;
556 install_branch_config("master", option_origin
,
557 "refs/heads/master");
560 if (head_points_at
) {
561 /* Local default branch link */
562 create_symref("HEAD", head_points_at
->name
, NULL
);
565 struct strbuf head_ref
= STRBUF_INIT
;
566 const char *head
= head_points_at
->name
;
568 if (!prefixcmp(head
, "refs/heads/"))
571 /* Set up the initial local branch */
573 /* Local branch initial value */
574 update_ref(reflog_msg
.buf
, "HEAD",
575 head_points_at
->old_sha1
,
576 NULL
, 0, DIE_ON_ERR
);
578 strbuf_addstr(&head_ref
, branch_top
.buf
);
579 strbuf_addstr(&head_ref
, "HEAD");
581 /* Remote branch link */
582 create_symref(head_ref
.buf
,
583 head_points_at
->peer_ref
->name
,
586 install_branch_config(head
, option_origin
,
587 head_points_at
->name
);
589 } else if (remote_head
) {
590 /* Source had detached HEAD pointing somewhere. */
592 update_ref(reflog_msg
.buf
, "HEAD",
593 remote_head
->old_sha1
,
594 NULL
, REF_NODEREF
, DIE_ON_ERR
);
596 /* Nothing to checkout out */
597 if (!option_no_checkout
)
598 warning("remote HEAD refers to nonexistent ref, "
599 "unable to checkout.\n");
600 option_no_checkout
= 1;
604 transport_unlock_pack(transport
);
606 if (!option_no_checkout
) {
607 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
608 struct unpack_trees_options opts
;
613 /* We need to be in the new work tree for the checkout */
616 fd
= hold_locked_index(lock_file
, 1);
618 memset(&opts
, 0, sizeof opts
);
621 opts
.fn
= oneway_merge
;
622 opts
.verbose_update
= !option_quiet
;
623 opts
.src_index
= &the_index
;
624 opts
.dst_index
= &the_index
;
626 tree
= parse_tree_indirect(remote_head
->old_sha1
);
628 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
629 unpack_trees(1, &t
, &opts
);
631 if (write_cache(fd
, active_cache
, active_nr
) ||
632 commit_locked_index(lock_file
))
633 die("unable to write new index file");
636 strbuf_release(&reflog_msg
);
637 strbuf_release(&branch_top
);
638 strbuf_release(&key
);
639 strbuf_release(&value
);