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"
25 #include "run-command.h"
29 * - respect DB_ENVIRONMENT for .git/objects.
31 * Implementation notes:
32 * - dropping use-separate-remote and no-separate-remote compatibility
35 static const char * const builtin_clone_usage
[] = {
36 "git clone [options] [--] <repo> [<dir>]",
40 static int option_no_checkout
, option_bare
, option_mirror
;
41 static int option_local
, option_no_hardlinks
, option_shared
, option_recursive
;
42 static char *option_template
, *option_depth
;
43 static char *option_origin
= NULL
;
44 static char *option_branch
= NULL
;
45 static const char *real_git_dir
;
46 static char *option_upload_pack
= "git-upload-pack";
47 static int option_verbosity
;
48 static int option_progress
;
49 static struct string_list option_reference
;
51 static int opt_parse_reference(const struct option
*opt
, const char *arg
, int unset
)
53 struct string_list
*option_reference
= opt
->value
;
56 string_list_append(option_reference
, arg
);
60 static struct option builtin_clone_options
[] = {
61 OPT__VERBOSITY(&option_verbosity
),
62 OPT_BOOLEAN(0, "progress", &option_progress
,
63 "force progress reporting"),
64 OPT_BOOLEAN('n', "no-checkout", &option_no_checkout
,
65 "don't create a checkout"),
66 OPT_BOOLEAN(0, "bare", &option_bare
, "create a bare repository"),
67 { OPTION_BOOLEAN
, 0, "naked", &option_bare
, NULL
,
68 "create a bare repository",
69 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
},
70 OPT_BOOLEAN(0, "mirror", &option_mirror
,
71 "create a mirror repository (implies bare)"),
72 OPT_BOOLEAN('l', "local", &option_local
,
73 "to clone from a local repository"),
74 OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks
,
75 "don't use local hardlinks, always copy"),
76 OPT_BOOLEAN('s', "shared", &option_shared
,
77 "setup as shared repository"),
78 OPT_BOOLEAN(0, "recursive", &option_recursive
,
79 "initialize submodules in the clone"),
80 OPT_BOOLEAN(0, "recurse-submodules", &option_recursive
,
81 "initialize submodules in the clone"),
82 OPT_STRING(0, "template", &option_template
, "template-directory",
83 "directory from which templates will be used"),
84 OPT_CALLBACK(0 , "reference", &option_reference
, "repo",
85 "reference repository", &opt_parse_reference
),
86 OPT_STRING('o', "origin", &option_origin
, "branch",
87 "use <branch> instead of 'origin' to track upstream"),
88 OPT_STRING('b', "branch", &option_branch
, "branch",
89 "checkout <branch> instead of the remote's HEAD"),
90 OPT_STRING('u', "upload-pack", &option_upload_pack
, "path",
91 "path to git-upload-pack on the remote"),
92 OPT_STRING(0, "depth", &option_depth
, "depth",
93 "create a shallow clone of that depth"),
94 OPT_STRING(0, "separate-git-dir", &real_git_dir
, "gitdir",
95 "separate git dir from working tree"),
100 static const char *argv_submodule
[] = {
101 "submodule", "update", "--init", "--recursive", NULL
104 static char *get_repo_path(const char *repo
, int *is_bundle
)
106 static char *suffix
[] = { "/.git", ".git", "" };
107 static char *bundle_suffix
[] = { ".bundle", "" };
111 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
113 path
= mkpath("%s%s", repo
, suffix
[i
]);
114 if (is_directory(path
)) {
116 return xstrdup(absolute_path(path
));
120 for (i
= 0; i
< ARRAY_SIZE(bundle_suffix
); i
++) {
122 path
= mkpath("%s%s", repo
, bundle_suffix
[i
]);
123 if (!stat(path
, &st
) && S_ISREG(st
.st_mode
)) {
125 return xstrdup(absolute_path(path
));
132 static char *guess_dir_name(const char *repo
, int is_bundle
, int is_bare
)
134 const char *end
= repo
+ strlen(repo
), *start
;
138 * Strip trailing spaces, slashes and /.git
140 while (repo
< end
&& (is_dir_sep(end
[-1]) || isspace(end
[-1])))
142 if (end
- repo
> 5 && is_dir_sep(end
[-5]) &&
143 !strncmp(end
- 4, ".git", 4)) {
145 while (repo
< end
&& is_dir_sep(end
[-1]))
150 * Find last component, but be prepared that repo could have
151 * the form "remote.example.com:foo.git", i.e. no slash
152 * in the directory part.
155 while (repo
< start
&& !is_dir_sep(start
[-1]) && start
[-1] != ':')
159 * Strip .{bundle,git}.
162 if (end
- start
> 7 && !strncmp(end
- 7, ".bundle", 7))
165 if (end
- start
> 4 && !strncmp(end
- 4, ".git", 4))
170 struct strbuf result
= STRBUF_INIT
;
171 strbuf_addf(&result
, "%.*s.git", (int)(end
- start
), start
);
172 dir
= strbuf_detach(&result
, NULL
);
174 dir
= xstrndup(start
, end
- start
);
176 * Replace sequences of 'control' characters and whitespace
177 * with one ascii space, remove leading and trailing spaces.
181 int prev_space
= 1 /* strip leading whitespace */;
182 for (end
= dir
; *end
; ++end
) {
184 if ((unsigned char)ch
< '\x20')
195 if (out
> dir
&& prev_space
)
201 static void strip_trailing_slashes(char *dir
)
203 char *end
= dir
+ strlen(dir
);
205 while (dir
< end
- 1 && is_dir_sep(end
[-1]))
210 static int add_one_reference(struct string_list_item
*item
, void *cb_data
)
215 struct remote
*remote
;
216 struct transport
*transport
;
217 const struct ref
*extra
;
219 ref_git
= real_path(item
->string
);
221 if (is_directory(mkpath("%s/.git/objects", ref_git
)))
222 ref_git
= mkpath("%s/.git", ref_git
);
223 else if (!is_directory(mkpath("%s/objects", ref_git
)))
224 die(_("reference repository '%s' is not a local directory."),
227 ref_git_copy
= xstrdup(ref_git
);
229 add_to_alternates_file(ref_git_copy
);
231 remote
= remote_get(ref_git_copy
);
232 transport
= transport_get(remote
, ref_git_copy
);
233 for (extra
= transport_get_remote_refs(transport
); extra
;
235 add_extra_ref(extra
->name
, extra
->old_sha1
, 0);
237 transport_disconnect(transport
);
243 static void setup_reference(void)
245 for_each_string_list(&option_reference
, add_one_reference
, NULL
);
248 static void copy_or_link_directory(struct strbuf
*src
, struct strbuf
*dest
)
252 int src_len
, dest_len
;
255 dir
= opendir(src
->buf
);
257 die_errno(_("failed to open '%s'"), src
->buf
);
259 if (mkdir(dest
->buf
, 0777)) {
261 die_errno(_("failed to create directory '%s'"), dest
->buf
);
262 else if (stat(dest
->buf
, &buf
))
263 die_errno(_("failed to stat '%s'"), dest
->buf
);
264 else if (!S_ISDIR(buf
.st_mode
))
265 die(_("%s exists and is not a directory"), dest
->buf
);
268 strbuf_addch(src
, '/');
270 strbuf_addch(dest
, '/');
271 dest_len
= dest
->len
;
273 while ((de
= readdir(dir
)) != NULL
) {
274 strbuf_setlen(src
, src_len
);
275 strbuf_addstr(src
, de
->d_name
);
276 strbuf_setlen(dest
, dest_len
);
277 strbuf_addstr(dest
, de
->d_name
);
278 if (stat(src
->buf
, &buf
)) {
279 warning (_("failed to stat %s\n"), src
->buf
);
282 if (S_ISDIR(buf
.st_mode
)) {
283 if (de
->d_name
[0] != '.')
284 copy_or_link_directory(src
, dest
);
288 if (unlink(dest
->buf
) && errno
!= ENOENT
)
289 die_errno(_("failed to unlink '%s'"), dest
->buf
);
290 if (!option_no_hardlinks
) {
291 if (!link(src
->buf
, dest
->buf
))
294 die_errno(_("failed to create link '%s'"), dest
->buf
);
295 option_no_hardlinks
= 1;
297 if (copy_file_with_time(dest
->buf
, src
->buf
, 0666))
298 die_errno(_("failed to copy file to '%s'"), dest
->buf
);
303 static const struct ref
*clone_local(const char *src_repo
,
304 const char *dest_repo
)
306 const struct ref
*ret
;
307 struct strbuf src
= STRBUF_INIT
;
308 struct strbuf dest
= STRBUF_INIT
;
309 struct remote
*remote
;
310 struct transport
*transport
;
313 add_to_alternates_file(src_repo
);
315 strbuf_addf(&src
, "%s/objects", src_repo
);
316 strbuf_addf(&dest
, "%s/objects", dest_repo
);
317 copy_or_link_directory(&src
, &dest
);
318 strbuf_release(&src
);
319 strbuf_release(&dest
);
322 remote
= remote_get(src_repo
);
323 transport
= transport_get(remote
, src_repo
);
324 ret
= transport_get_remote_refs(transport
);
325 transport_disconnect(transport
);
326 if (0 <= option_verbosity
)
327 printf(_("done.\n"));
331 static const char *junk_work_tree
;
332 static const char *junk_git_dir
;
333 static pid_t junk_pid
;
335 static void remove_junk(void)
337 struct strbuf sb
= STRBUF_INIT
;
338 if (getpid() != junk_pid
)
341 strbuf_addstr(&sb
, junk_git_dir
);
342 remove_dir_recursively(&sb
, 0);
345 if (junk_work_tree
) {
346 strbuf_addstr(&sb
, junk_work_tree
);
347 remove_dir_recursively(&sb
, 0);
352 static void remove_junk_on_signal(int signo
)
359 static struct ref
*wanted_peer_refs(const struct ref
*refs
,
360 struct refspec
*refspec
)
362 struct ref
*local_refs
= NULL
;
363 struct ref
**tail
= &local_refs
;
365 get_fetch_map(refs
, refspec
, &tail
, 0);
367 get_fetch_map(refs
, tag_refspec
, &tail
, 0);
372 static void write_remote_refs(const struct ref
*local_refs
)
376 for (r
= local_refs
; r
; r
= r
->next
)
377 add_extra_ref(r
->peer_ref
->name
, r
->old_sha1
, 0);
379 pack_refs(PACK_REFS_ALL
);
383 int cmd_clone(int argc
, const char **argv
, const char *prefix
)
385 int is_bundle
= 0, is_local
;
387 const char *repo_name
, *repo
, *work_tree
, *git_dir
;
390 const struct ref
*refs
, *remote_head
;
391 const struct ref
*remote_head_points_at
;
392 const struct ref
*our_head_points_at
;
393 struct ref
*mapped_refs
;
394 struct strbuf key
= STRBUF_INIT
, value
= STRBUF_INIT
;
395 struct strbuf branch_top
= STRBUF_INIT
, reflog_msg
= STRBUF_INIT
;
396 struct transport
*transport
= NULL
;
397 char *src_ref_prefix
= "refs/heads/";
400 struct refspec
*refspec
;
401 const char *fetch_pattern
;
405 packet_trace_identity("clone");
406 argc
= parse_options(argc
, argv
, prefix
, builtin_clone_options
,
407 builtin_clone_usage
, 0);
410 usage_msg_opt(_("Too many arguments."),
411 builtin_clone_usage
, builtin_clone_options
);
414 usage_msg_opt(_("You must specify a repository to clone."),
415 builtin_clone_usage
, builtin_clone_options
);
422 die(_("--bare and --origin %s options are incompatible."),
424 option_no_checkout
= 1;
428 option_origin
= "origin";
432 path
= get_repo_path(repo_name
, &is_bundle
);
434 repo
= xstrdup(absolute_path(repo_name
));
435 else if (!strchr(repo_name
, ':'))
436 die(_("repository '%s' does not exist"), repo_name
);
439 is_local
= path
&& !is_bundle
;
440 if (is_local
&& option_depth
)
441 warning(_("--depth is ignored in local clones; use file:// instead."));
444 dir
= xstrdup(argv
[1]);
446 dir
= guess_dir_name(repo_name
, is_bundle
, option_bare
);
447 strip_trailing_slashes(dir
);
449 dest_exists
= !stat(dir
, &buf
);
450 if (dest_exists
&& !is_empty_dir(dir
))
451 die(_("destination path '%s' already exists and is not "
452 "an empty directory."), dir
);
454 strbuf_addf(&reflog_msg
, "clone: from %s", repo
);
459 work_tree
= getenv("GIT_WORK_TREE");
460 if (work_tree
&& !stat(work_tree
, &buf
))
461 die(_("working tree '%s' already exists."), work_tree
);
464 if (option_bare
|| work_tree
)
465 git_dir
= xstrdup(dir
);
468 git_dir
= xstrdup(mkpath("%s/.git", dir
));
472 junk_work_tree
= work_tree
;
473 if (safe_create_leading_directories_const(work_tree
) < 0)
474 die_errno(_("could not create leading directories of '%s'"),
476 if (!dest_exists
&& mkdir(work_tree
, 0755))
477 die_errno(_("could not create work tree dir '%s'."),
479 set_git_work_tree(work_tree
);
481 junk_git_dir
= git_dir
;
483 sigchain_push_common(remove_junk_on_signal
);
485 setenv(CONFIG_ENVIRONMENT
, mkpath("%s/config", git_dir
), 1);
487 if (safe_create_leading_directories_const(git_dir
) < 0)
488 die(_("could not create leading directories of '%s'"), git_dir
);
490 set_git_dir_init(git_dir
, real_git_dir
, 0);
492 git_dir
= real_git_dir
;
494 if (0 <= option_verbosity
) {
496 printf(_("Cloning into bare repository %s...\n"), dir
);
498 printf(_("Cloning into %s...\n"), dir
);
500 init_db(option_template
, INIT_DB_QUIET
);
503 * At this point, the config exists, so we do not need the
504 * environment variable. We actually need to unset it, too, to
505 * re-enable parsing of the global configs.
507 unsetenv(CONFIG_ENVIRONMENT
);
509 git_config(git_default_config
, NULL
);
513 src_ref_prefix
= "refs/";
514 strbuf_addstr(&branch_top
, src_ref_prefix
);
516 git_config_set("core.bare", "true");
518 strbuf_addf(&branch_top
, "refs/remotes/%s/", option_origin
);
521 strbuf_addf(&value
, "+%s*:%s*", src_ref_prefix
, branch_top
.buf
);
523 if (option_mirror
|| !option_bare
) {
524 /* Configure the remote */
525 strbuf_addf(&key
, "remote.%s.fetch", option_origin
);
526 git_config_set_multivar(key
.buf
, value
.buf
, "^$", 0);
530 strbuf_addf(&key
, "remote.%s.mirror", option_origin
);
531 git_config_set(key
.buf
, "true");
536 strbuf_addf(&key
, "remote.%s.url", option_origin
);
537 git_config_set(key
.buf
, repo
);
540 if (option_reference
.nr
)
543 fetch_pattern
= value
.buf
;
544 refspec
= parse_fetch_refspec(1, &fetch_pattern
);
546 strbuf_reset(&value
);
549 refs
= clone_local(path
, git_dir
);
550 mapped_refs
= wanted_peer_refs(refs
, refspec
);
552 struct remote
*remote
= remote_get(option_origin
);
553 transport
= transport_get(remote
, remote
->url
[0]);
555 if (!transport
->get_refs_list
|| !transport
->fetch
)
556 die(_("Don't know how to clone %s"), transport
->url
);
558 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
561 transport_set_option(transport
, TRANS_OPT_DEPTH
,
564 transport_set_verbosity(transport
, option_verbosity
, option_progress
);
566 if (option_upload_pack
)
567 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
,
570 refs
= transport_get_remote_refs(transport
);
572 mapped_refs
= wanted_peer_refs(refs
, refspec
);
573 transport_fetch_refs(transport
, mapped_refs
);
580 write_remote_refs(mapped_refs
);
582 remote_head
= find_ref_by_name(refs
, "HEAD");
583 remote_head_points_at
=
584 guess_remote_head(remote_head
, mapped_refs
, 0);
587 struct strbuf head
= STRBUF_INIT
;
588 strbuf_addstr(&head
, src_ref_prefix
);
589 strbuf_addstr(&head
, option_branch
);
591 find_ref_by_name(mapped_refs
, head
.buf
);
592 strbuf_release(&head
);
594 if (!our_head_points_at
) {
595 warning(_("Remote branch %s not found in "
596 "upstream %s, using HEAD instead"),
597 option_branch
, option_origin
);
598 our_head_points_at
= remote_head_points_at
;
602 our_head_points_at
= remote_head_points_at
;
605 warning(_("You appear to have cloned an empty repository."));
606 our_head_points_at
= NULL
;
607 remote_head_points_at
= NULL
;
609 option_no_checkout
= 1;
611 install_branch_config(0, "master", option_origin
,
612 "refs/heads/master");
615 if (remote_head_points_at
&& !option_bare
) {
616 struct strbuf head_ref
= STRBUF_INIT
;
617 strbuf_addstr(&head_ref
, branch_top
.buf
);
618 strbuf_addstr(&head_ref
, "HEAD");
619 create_symref(head_ref
.buf
,
620 remote_head_points_at
->peer_ref
->name
,
624 if (our_head_points_at
) {
625 /* Local default branch link */
626 create_symref("HEAD", our_head_points_at
->name
, NULL
);
628 const char *head
= skip_prefix(our_head_points_at
->name
,
630 update_ref(reflog_msg
.buf
, "HEAD",
631 our_head_points_at
->old_sha1
,
632 NULL
, 0, DIE_ON_ERR
);
633 install_branch_config(0, head
, option_origin
,
634 our_head_points_at
->name
);
636 } else if (remote_head
) {
637 /* Source had detached HEAD pointing somewhere. */
639 update_ref(reflog_msg
.buf
, "HEAD",
640 remote_head
->old_sha1
,
641 NULL
, REF_NODEREF
, DIE_ON_ERR
);
642 our_head_points_at
= remote_head
;
645 /* Nothing to checkout out */
646 if (!option_no_checkout
)
647 warning(_("remote HEAD refers to nonexistent ref, "
648 "unable to checkout.\n"));
649 option_no_checkout
= 1;
653 transport_unlock_pack(transport
);
654 transport_disconnect(transport
);
657 if (!option_no_checkout
) {
658 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
659 struct unpack_trees_options opts
;
664 /* We need to be in the new work tree for the checkout */
667 fd
= hold_locked_index(lock_file
, 1);
669 memset(&opts
, 0, sizeof opts
);
672 opts
.fn
= oneway_merge
;
673 opts
.verbose_update
= (option_verbosity
> 0);
674 opts
.src_index
= &the_index
;
675 opts
.dst_index
= &the_index
;
677 tree
= parse_tree_indirect(our_head_points_at
->old_sha1
);
679 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
680 unpack_trees(1, &t
, &opts
);
682 if (write_cache(fd
, active_cache
, active_nr
) ||
683 commit_locked_index(lock_file
))
684 die(_("unable to write new index file"));
686 err
|= run_hook(NULL
, "post-checkout", sha1_to_hex(null_sha1
),
687 sha1_to_hex(our_head_points_at
->old_sha1
), "1",
690 if (!err
&& option_recursive
)
691 err
= run_command_v_opt(argv_submodule
, RUN_GIT_CMD
);
694 strbuf_release(&reflog_msg
);
695 strbuf_release(&branch_top
);
696 strbuf_release(&key
);
697 strbuf_release(&value
);