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_reference
, *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_config
;
51 static struct option builtin_clone_options
[] = {
52 OPT__VERBOSITY(&option_verbosity
),
53 OPT_BOOLEAN(0, "progress", &option_progress
,
54 "force progress reporting"),
55 OPT_BOOLEAN('n', "no-checkout", &option_no_checkout
,
56 "don't create a checkout"),
57 OPT_BOOLEAN(0, "bare", &option_bare
, "create a bare repository"),
58 { OPTION_BOOLEAN
, 0, "naked", &option_bare
, NULL
,
59 "create a bare repository",
60 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
},
61 OPT_BOOLEAN(0, "mirror", &option_mirror
,
62 "create a mirror repository (implies bare)"),
63 OPT_BOOLEAN('l', "local", &option_local
,
64 "to clone from a local repository"),
65 OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks
,
66 "don't use local hardlinks, always copy"),
67 OPT_BOOLEAN('s', "shared", &option_shared
,
68 "setup as shared repository"),
69 OPT_BOOLEAN(0, "recursive", &option_recursive
,
70 "initialize submodules in the clone"),
71 OPT_BOOLEAN(0, "recurse-submodules", &option_recursive
,
72 "initialize submodules in the clone"),
73 OPT_STRING(0, "template", &option_template
, "template-directory",
74 "directory from which templates will be used"),
75 OPT_STRING(0, "reference", &option_reference
, "repo",
76 "reference repository"),
77 OPT_STRING('o', "origin", &option_origin
, "branch",
78 "use <branch> instead of 'origin' to track upstream"),
79 OPT_STRING('b', "branch", &option_branch
, "branch",
80 "checkout <branch> instead of the remote's HEAD"),
81 OPT_STRING('u', "upload-pack", &option_upload_pack
, "path",
82 "path to git-upload-pack on the remote"),
83 OPT_STRING(0, "depth", &option_depth
, "depth",
84 "create a shallow clone of that depth"),
85 OPT_STRING(0, "separate-git-dir", &real_git_dir
, "gitdir",
86 "separate git dir from working tree"),
87 OPT_STRING_LIST('c', "config", &option_config
, "key=value",
88 "set config inside the new repository"),
92 static const char *argv_submodule
[] = {
93 "submodule", "update", "--init", "--recursive", NULL
96 static char *get_repo_path(const char *repo
, int *is_bundle
)
98 static char *suffix
[] = { "/.git", ".git", "" };
99 static char *bundle_suffix
[] = { ".bundle", "" };
103 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
105 path
= mkpath("%s%s", repo
, suffix
[i
]);
106 if (is_directory(path
)) {
108 return xstrdup(absolute_path(path
));
112 for (i
= 0; i
< ARRAY_SIZE(bundle_suffix
); i
++) {
114 path
= mkpath("%s%s", repo
, bundle_suffix
[i
]);
115 if (!stat(path
, &st
) && S_ISREG(st
.st_mode
)) {
117 return xstrdup(absolute_path(path
));
124 static char *guess_dir_name(const char *repo
, int is_bundle
, int is_bare
)
126 const char *end
= repo
+ strlen(repo
), *start
;
130 * Strip trailing spaces, slashes and /.git
132 while (repo
< end
&& (is_dir_sep(end
[-1]) || isspace(end
[-1])))
134 if (end
- repo
> 5 && is_dir_sep(end
[-5]) &&
135 !strncmp(end
- 4, ".git", 4)) {
137 while (repo
< end
&& is_dir_sep(end
[-1]))
142 * Find last component, but be prepared that repo could have
143 * the form "remote.example.com:foo.git", i.e. no slash
144 * in the directory part.
147 while (repo
< start
&& !is_dir_sep(start
[-1]) && start
[-1] != ':')
151 * Strip .{bundle,git}.
154 if (end
- start
> 7 && !strncmp(end
- 7, ".bundle", 7))
157 if (end
- start
> 4 && !strncmp(end
- 4, ".git", 4))
162 struct strbuf result
= STRBUF_INIT
;
163 strbuf_addf(&result
, "%.*s.git", (int)(end
- start
), start
);
164 dir
= strbuf_detach(&result
, NULL
);
166 dir
= xstrndup(start
, end
- start
);
168 * Replace sequences of 'control' characters and whitespace
169 * with one ascii space, remove leading and trailing spaces.
173 int prev_space
= 1 /* strip leading whitespace */;
174 for (end
= dir
; *end
; ++end
) {
176 if ((unsigned char)ch
< '\x20')
187 if (out
> dir
&& prev_space
)
193 static void strip_trailing_slashes(char *dir
)
195 char *end
= dir
+ strlen(dir
);
197 while (dir
< end
- 1 && is_dir_sep(end
[-1]))
202 static void setup_reference(const char *repo
)
207 struct remote
*remote
;
208 struct transport
*transport
;
209 const struct ref
*extra
;
211 ref_git
= real_path(option_reference
);
213 if (is_directory(mkpath("%s/.git/objects", ref_git
)))
214 ref_git
= mkpath("%s/.git", ref_git
);
215 else if (!is_directory(mkpath("%s/objects", ref_git
)))
216 die(_("reference repository '%s' is not a local directory."),
219 ref_git_copy
= xstrdup(ref_git
);
221 add_to_alternates_file(ref_git_copy
);
223 remote
= remote_get(ref_git_copy
);
224 transport
= transport_get(remote
, ref_git_copy
);
225 for (extra
= transport_get_remote_refs(transport
); extra
;
227 add_extra_ref(extra
->name
, extra
->old_sha1
, 0);
229 transport_disconnect(transport
);
234 static void copy_or_link_directory(struct strbuf
*src
, struct strbuf
*dest
)
238 int src_len
, dest_len
;
241 dir
= opendir(src
->buf
);
243 die_errno(_("failed to open '%s'"), src
->buf
);
245 if (mkdir(dest
->buf
, 0777)) {
247 die_errno(_("failed to create directory '%s'"), dest
->buf
);
248 else if (stat(dest
->buf
, &buf
))
249 die_errno(_("failed to stat '%s'"), dest
->buf
);
250 else if (!S_ISDIR(buf
.st_mode
))
251 die(_("%s exists and is not a directory"), dest
->buf
);
254 strbuf_addch(src
, '/');
256 strbuf_addch(dest
, '/');
257 dest_len
= dest
->len
;
259 while ((de
= readdir(dir
)) != NULL
) {
260 strbuf_setlen(src
, src_len
);
261 strbuf_addstr(src
, de
->d_name
);
262 strbuf_setlen(dest
, dest_len
);
263 strbuf_addstr(dest
, de
->d_name
);
264 if (stat(src
->buf
, &buf
)) {
265 warning (_("failed to stat %s\n"), src
->buf
);
268 if (S_ISDIR(buf
.st_mode
)) {
269 if (de
->d_name
[0] != '.')
270 copy_or_link_directory(src
, dest
);
274 if (unlink(dest
->buf
) && errno
!= ENOENT
)
275 die_errno(_("failed to unlink '%s'"), dest
->buf
);
276 if (!option_no_hardlinks
) {
277 if (!link(src
->buf
, dest
->buf
))
280 die_errno(_("failed to create link '%s'"), dest
->buf
);
281 option_no_hardlinks
= 1;
283 if (copy_file_with_time(dest
->buf
, src
->buf
, 0666))
284 die_errno(_("failed to copy file to '%s'"), dest
->buf
);
289 static const struct ref
*clone_local(const char *src_repo
,
290 const char *dest_repo
)
292 const struct ref
*ret
;
293 struct strbuf src
= STRBUF_INIT
;
294 struct strbuf dest
= STRBUF_INIT
;
295 struct remote
*remote
;
296 struct transport
*transport
;
299 add_to_alternates_file(src_repo
);
301 strbuf_addf(&src
, "%s/objects", src_repo
);
302 strbuf_addf(&dest
, "%s/objects", dest_repo
);
303 copy_or_link_directory(&src
, &dest
);
304 strbuf_release(&src
);
305 strbuf_release(&dest
);
308 remote
= remote_get(src_repo
);
309 transport
= transport_get(remote
, src_repo
);
310 ret
= transport_get_remote_refs(transport
);
311 transport_disconnect(transport
);
312 if (0 <= option_verbosity
)
313 printf(_("done.\n"));
317 static const char *junk_work_tree
;
318 static const char *junk_git_dir
;
319 static pid_t junk_pid
;
321 static void remove_junk(void)
323 struct strbuf sb
= STRBUF_INIT
;
324 if (getpid() != junk_pid
)
327 strbuf_addstr(&sb
, junk_git_dir
);
328 remove_dir_recursively(&sb
, 0);
331 if (junk_work_tree
) {
332 strbuf_addstr(&sb
, junk_work_tree
);
333 remove_dir_recursively(&sb
, 0);
338 static void remove_junk_on_signal(int signo
)
345 static struct ref
*wanted_peer_refs(const struct ref
*refs
,
346 struct refspec
*refspec
)
348 struct ref
*head
= copy_ref(find_ref_by_name(refs
, "HEAD"));
349 struct ref
*local_refs
= head
;
350 struct ref
**tail
= head
? &head
->next
: &local_refs
;
352 get_fetch_map(refs
, refspec
, &tail
, 0);
354 get_fetch_map(refs
, tag_refspec
, &tail
, 0);
359 static void write_remote_refs(const struct ref
*local_refs
)
363 for (r
= local_refs
; r
; r
= r
->next
) {
366 add_extra_ref(r
->peer_ref
->name
, r
->old_sha1
, 0);
369 pack_refs(PACK_REFS_ALL
);
373 static int write_one_config(const char *key
, const char *value
, void *data
)
375 return git_config_set_multivar(key
, value
? value
: "true", "^$", 0);
378 static void write_config(struct string_list
*config
)
382 for (i
= 0; i
< config
->nr
; i
++) {
383 if (git_config_parse_parameter(config
->items
[i
].string
,
384 write_one_config
, NULL
) < 0)
385 die("unable to write parameters to config file");
389 int cmd_clone(int argc
, const char **argv
, const char *prefix
)
391 int is_bundle
= 0, is_local
;
393 const char *repo_name
, *repo
, *work_tree
, *git_dir
;
396 const struct ref
*refs
, *remote_head
;
397 const struct ref
*remote_head_points_at
;
398 const struct ref
*our_head_points_at
;
399 struct ref
*mapped_refs
;
400 struct strbuf key
= STRBUF_INIT
, value
= STRBUF_INIT
;
401 struct strbuf branch_top
= STRBUF_INIT
, reflog_msg
= STRBUF_INIT
;
402 struct transport
*transport
= NULL
;
403 char *src_ref_prefix
= "refs/heads/";
406 struct refspec
*refspec
;
407 const char *fetch_pattern
;
411 packet_trace_identity("clone");
412 argc
= parse_options(argc
, argv
, prefix
, builtin_clone_options
,
413 builtin_clone_usage
, 0);
416 usage_msg_opt(_("Too many arguments."),
417 builtin_clone_usage
, builtin_clone_options
);
420 usage_msg_opt(_("You must specify a repository to clone."),
421 builtin_clone_usage
, builtin_clone_options
);
428 die(_("--bare and --origin %s options are incompatible."),
430 option_no_checkout
= 1;
434 option_origin
= "origin";
438 path
= get_repo_path(repo_name
, &is_bundle
);
440 repo
= xstrdup(absolute_path(repo_name
));
441 else if (!strchr(repo_name
, ':'))
442 die(_("repository '%s' does not exist"), repo_name
);
445 is_local
= path
&& !is_bundle
;
446 if (is_local
&& option_depth
)
447 warning(_("--depth is ignored in local clones; use file:// instead."));
450 dir
= xstrdup(argv
[1]);
452 dir
= guess_dir_name(repo_name
, is_bundle
, option_bare
);
453 strip_trailing_slashes(dir
);
455 dest_exists
= !stat(dir
, &buf
);
456 if (dest_exists
&& !is_empty_dir(dir
))
457 die(_("destination path '%s' already exists and is not "
458 "an empty directory."), dir
);
460 strbuf_addf(&reflog_msg
, "clone: from %s", repo
);
465 work_tree
= getenv("GIT_WORK_TREE");
466 if (work_tree
&& !stat(work_tree
, &buf
))
467 die(_("working tree '%s' already exists."), work_tree
);
470 if (option_bare
|| work_tree
)
471 git_dir
= xstrdup(dir
);
474 git_dir
= xstrdup(mkpath("%s/.git", dir
));
478 junk_work_tree
= work_tree
;
479 if (safe_create_leading_directories_const(work_tree
) < 0)
480 die_errno(_("could not create leading directories of '%s'"),
482 if (!dest_exists
&& mkdir(work_tree
, 0755))
483 die_errno(_("could not create work tree dir '%s'."),
485 set_git_work_tree(work_tree
);
487 junk_git_dir
= git_dir
;
489 sigchain_push_common(remove_junk_on_signal
);
491 setenv(CONFIG_ENVIRONMENT
, mkpath("%s/config", git_dir
), 1);
493 if (safe_create_leading_directories_const(git_dir
) < 0)
494 die(_("could not create leading directories of '%s'"), git_dir
);
496 set_git_dir_init(git_dir
, real_git_dir
, 0);
498 git_dir
= real_git_dir
;
500 if (0 <= option_verbosity
) {
502 printf(_("Cloning into bare repository %s...\n"), dir
);
504 printf(_("Cloning into %s...\n"), dir
);
506 init_db(option_template
, INIT_DB_QUIET
);
507 write_config(&option_config
);
510 * At this point, the config exists, so we do not need the
511 * environment variable. We actually need to unset it, too, to
512 * re-enable parsing of the global configs.
514 unsetenv(CONFIG_ENVIRONMENT
);
516 git_config(git_default_config
, NULL
);
520 src_ref_prefix
= "refs/";
521 strbuf_addstr(&branch_top
, src_ref_prefix
);
523 git_config_set("core.bare", "true");
525 strbuf_addf(&branch_top
, "refs/remotes/%s/", option_origin
);
528 strbuf_addf(&value
, "+%s*:%s*", src_ref_prefix
, branch_top
.buf
);
530 if (option_mirror
|| !option_bare
) {
531 /* Configure the remote */
532 strbuf_addf(&key
, "remote.%s.fetch", option_origin
);
533 git_config_set_multivar(key
.buf
, value
.buf
, "^$", 0);
537 strbuf_addf(&key
, "remote.%s.mirror", option_origin
);
538 git_config_set(key
.buf
, "true");
543 strbuf_addf(&key
, "remote.%s.url", option_origin
);
544 git_config_set(key
.buf
, repo
);
547 if (option_reference
)
548 setup_reference(git_dir
);
550 fetch_pattern
= value
.buf
;
551 refspec
= parse_fetch_refspec(1, &fetch_pattern
);
553 strbuf_reset(&value
);
556 refs
= clone_local(path
, git_dir
);
557 mapped_refs
= wanted_peer_refs(refs
, refspec
);
559 struct remote
*remote
= remote_get(option_origin
);
560 transport
= transport_get(remote
, remote
->url
[0]);
562 if (!transport
->get_refs_list
|| !transport
->fetch
)
563 die(_("Don't know how to clone %s"), transport
->url
);
565 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
568 transport_set_option(transport
, TRANS_OPT_DEPTH
,
571 transport_set_verbosity(transport
, option_verbosity
, option_progress
);
573 if (option_upload_pack
)
574 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
,
577 refs
= transport_get_remote_refs(transport
);
579 mapped_refs
= wanted_peer_refs(refs
, refspec
);
580 transport_fetch_refs(transport
, mapped_refs
);
587 write_remote_refs(mapped_refs
);
589 remote_head
= find_ref_by_name(refs
, "HEAD");
590 remote_head_points_at
=
591 guess_remote_head(remote_head
, mapped_refs
, 0);
594 struct strbuf head
= STRBUF_INIT
;
595 strbuf_addstr(&head
, src_ref_prefix
);
596 strbuf_addstr(&head
, option_branch
);
598 find_ref_by_name(mapped_refs
, head
.buf
);
599 strbuf_release(&head
);
601 if (!our_head_points_at
) {
602 warning(_("Remote branch %s not found in "
603 "upstream %s, using HEAD instead"),
604 option_branch
, option_origin
);
605 our_head_points_at
= remote_head_points_at
;
609 our_head_points_at
= remote_head_points_at
;
612 warning(_("You appear to have cloned an empty repository."));
613 our_head_points_at
= NULL
;
614 remote_head_points_at
= NULL
;
616 option_no_checkout
= 1;
618 install_branch_config(0, "master", option_origin
,
619 "refs/heads/master");
622 if (remote_head_points_at
&& !option_bare
) {
623 struct strbuf head_ref
= STRBUF_INIT
;
624 strbuf_addstr(&head_ref
, branch_top
.buf
);
625 strbuf_addstr(&head_ref
, "HEAD");
626 create_symref(head_ref
.buf
,
627 remote_head_points_at
->peer_ref
->name
,
631 if (our_head_points_at
) {
632 /* Local default branch link */
633 create_symref("HEAD", our_head_points_at
->name
, NULL
);
635 const char *head
= skip_prefix(our_head_points_at
->name
,
637 update_ref(reflog_msg
.buf
, "HEAD",
638 our_head_points_at
->old_sha1
,
639 NULL
, 0, DIE_ON_ERR
);
640 install_branch_config(0, head
, option_origin
,
641 our_head_points_at
->name
);
643 } else if (remote_head
) {
644 /* Source had detached HEAD pointing somewhere. */
646 update_ref(reflog_msg
.buf
, "HEAD",
647 remote_head
->old_sha1
,
648 NULL
, REF_NODEREF
, DIE_ON_ERR
);
649 our_head_points_at
= remote_head
;
652 /* Nothing to checkout out */
653 if (!option_no_checkout
)
654 warning(_("remote HEAD refers to nonexistent ref, "
655 "unable to checkout.\n"));
656 option_no_checkout
= 1;
660 transport_unlock_pack(transport
);
661 transport_disconnect(transport
);
664 if (!option_no_checkout
) {
665 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
666 struct unpack_trees_options opts
;
671 /* We need to be in the new work tree for the checkout */
674 fd
= hold_locked_index(lock_file
, 1);
676 memset(&opts
, 0, sizeof opts
);
679 opts
.fn
= oneway_merge
;
680 opts
.verbose_update
= (option_verbosity
> 0);
681 opts
.src_index
= &the_index
;
682 opts
.dst_index
= &the_index
;
684 tree
= parse_tree_indirect(our_head_points_at
->old_sha1
);
686 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
687 unpack_trees(1, &t
, &opts
);
689 if (write_cache(fd
, active_cache
, active_nr
) ||
690 commit_locked_index(lock_file
))
691 die(_("unable to write new index file"));
693 err
|= run_hook(NULL
, "post-checkout", sha1_to_hex(null_sha1
),
694 sha1_to_hex(our_head_points_at
->old_sha1
), "1",
697 if (!err
&& option_recursive
)
698 err
= run_command_v_opt(argv_submodule
, RUN_GIT_CMD
);
701 strbuf_release(&reflog_msg
);
702 strbuf_release(&branch_top
);
703 strbuf_release(&key
);
704 strbuf_release(&value
);