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
*local_refs
= NULL
;
349 struct ref
**tail
= &local_refs
;
351 get_fetch_map(refs
, refspec
, &tail
, 0);
353 get_fetch_map(refs
, tag_refspec
, &tail
, 0);
358 static void write_remote_refs(const struct ref
*local_refs
)
362 for (r
= local_refs
; r
; r
= r
->next
)
363 add_extra_ref(r
->peer_ref
->name
, r
->old_sha1
, 0);
365 pack_refs(PACK_REFS_ALL
);
369 static int write_one_config(const char *key
, const char *value
, void *data
)
371 return git_config_set_multivar(key
, value
? value
: "true", "^$", 0);
374 static void write_config(struct string_list
*config
)
378 for (i
= 0; i
< config
->nr
; i
++) {
379 if (git_config_parse_parameter(config
->items
[i
].string
,
380 write_one_config
, NULL
) < 0)
381 die("unable to write parameters to config file");
385 int cmd_clone(int argc
, const char **argv
, const char *prefix
)
387 int is_bundle
= 0, is_local
;
389 const char *repo_name
, *repo
, *work_tree
, *git_dir
;
392 const struct ref
*refs
, *remote_head
;
393 const struct ref
*remote_head_points_at
;
394 const struct ref
*our_head_points_at
;
395 struct ref
*mapped_refs
;
396 struct strbuf key
= STRBUF_INIT
, value
= STRBUF_INIT
;
397 struct strbuf branch_top
= STRBUF_INIT
, reflog_msg
= STRBUF_INIT
;
398 struct transport
*transport
= NULL
;
399 char *src_ref_prefix
= "refs/heads/";
402 struct refspec
*refspec
;
403 const char *fetch_pattern
;
407 packet_trace_identity("clone");
408 argc
= parse_options(argc
, argv
, prefix
, builtin_clone_options
,
409 builtin_clone_usage
, 0);
412 usage_msg_opt(_("Too many arguments."),
413 builtin_clone_usage
, builtin_clone_options
);
416 usage_msg_opt(_("You must specify a repository to clone."),
417 builtin_clone_usage
, builtin_clone_options
);
424 die(_("--bare and --origin %s options are incompatible."),
426 option_no_checkout
= 1;
430 option_origin
= "origin";
434 path
= get_repo_path(repo_name
, &is_bundle
);
436 repo
= xstrdup(absolute_path(repo_name
));
437 else if (!strchr(repo_name
, ':'))
438 die(_("repository '%s' does not exist"), repo_name
);
441 is_local
= path
&& !is_bundle
;
442 if (is_local
&& option_depth
)
443 warning(_("--depth is ignored in local clones; use file:// instead."));
446 dir
= xstrdup(argv
[1]);
448 dir
= guess_dir_name(repo_name
, is_bundle
, option_bare
);
449 strip_trailing_slashes(dir
);
451 dest_exists
= !stat(dir
, &buf
);
452 if (dest_exists
&& !is_empty_dir(dir
))
453 die(_("destination path '%s' already exists and is not "
454 "an empty directory."), dir
);
456 strbuf_addf(&reflog_msg
, "clone: from %s", repo
);
461 work_tree
= getenv("GIT_WORK_TREE");
462 if (work_tree
&& !stat(work_tree
, &buf
))
463 die(_("working tree '%s' already exists."), work_tree
);
466 if (option_bare
|| work_tree
)
467 git_dir
= xstrdup(dir
);
470 git_dir
= xstrdup(mkpath("%s/.git", dir
));
474 junk_work_tree
= work_tree
;
475 if (safe_create_leading_directories_const(work_tree
) < 0)
476 die_errno(_("could not create leading directories of '%s'"),
478 if (!dest_exists
&& mkdir(work_tree
, 0755))
479 die_errno(_("could not create work tree dir '%s'."),
481 set_git_work_tree(work_tree
);
483 junk_git_dir
= git_dir
;
485 sigchain_push_common(remove_junk_on_signal
);
487 setenv(CONFIG_ENVIRONMENT
, mkpath("%s/config", git_dir
), 1);
489 if (safe_create_leading_directories_const(git_dir
) < 0)
490 die(_("could not create leading directories of '%s'"), git_dir
);
492 set_git_dir_init(git_dir
, real_git_dir
, 0);
494 git_dir
= real_git_dir
;
496 if (0 <= option_verbosity
) {
498 printf(_("Cloning into bare repository %s...\n"), dir
);
500 printf(_("Cloning into %s...\n"), dir
);
502 init_db(option_template
, INIT_DB_QUIET
);
503 write_config(&option_config
);
506 * At this point, the config exists, so we do not need the
507 * environment variable. We actually need to unset it, too, to
508 * re-enable parsing of the global configs.
510 unsetenv(CONFIG_ENVIRONMENT
);
512 git_config(git_default_config
, NULL
);
516 src_ref_prefix
= "refs/";
517 strbuf_addstr(&branch_top
, src_ref_prefix
);
519 git_config_set("core.bare", "true");
521 strbuf_addf(&branch_top
, "refs/remotes/%s/", option_origin
);
524 strbuf_addf(&value
, "+%s*:%s*", src_ref_prefix
, branch_top
.buf
);
526 if (option_mirror
|| !option_bare
) {
527 /* Configure the remote */
528 strbuf_addf(&key
, "remote.%s.fetch", option_origin
);
529 git_config_set_multivar(key
.buf
, value
.buf
, "^$", 0);
533 strbuf_addf(&key
, "remote.%s.mirror", option_origin
);
534 git_config_set(key
.buf
, "true");
539 strbuf_addf(&key
, "remote.%s.url", option_origin
);
540 git_config_set(key
.buf
, repo
);
543 if (option_reference
)
544 setup_reference(git_dir
);
546 fetch_pattern
= value
.buf
;
547 refspec
= parse_fetch_refspec(1, &fetch_pattern
);
549 strbuf_reset(&value
);
552 refs
= clone_local(path
, git_dir
);
553 mapped_refs
= wanted_peer_refs(refs
, refspec
);
555 struct remote
*remote
= remote_get(option_origin
);
556 transport
= transport_get(remote
, remote
->url
[0]);
558 if (!transport
->get_refs_list
|| !transport
->fetch
)
559 die(_("Don't know how to clone %s"), transport
->url
);
561 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
564 transport_set_option(transport
, TRANS_OPT_DEPTH
,
567 transport_set_verbosity(transport
, option_verbosity
, option_progress
);
569 if (option_upload_pack
)
570 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
,
573 refs
= transport_get_remote_refs(transport
);
575 mapped_refs
= wanted_peer_refs(refs
, refspec
);
576 transport_fetch_refs(transport
, mapped_refs
);
583 write_remote_refs(mapped_refs
);
585 remote_head
= find_ref_by_name(refs
, "HEAD");
586 remote_head_points_at
=
587 guess_remote_head(remote_head
, mapped_refs
, 0);
590 struct strbuf head
= STRBUF_INIT
;
591 strbuf_addstr(&head
, src_ref_prefix
);
592 strbuf_addstr(&head
, option_branch
);
594 find_ref_by_name(mapped_refs
, head
.buf
);
595 strbuf_release(&head
);
597 if (!our_head_points_at
) {
598 warning(_("Remote branch %s not found in "
599 "upstream %s, using HEAD instead"),
600 option_branch
, option_origin
);
601 our_head_points_at
= remote_head_points_at
;
605 our_head_points_at
= remote_head_points_at
;
608 warning(_("You appear to have cloned an empty repository."));
609 our_head_points_at
= NULL
;
610 remote_head_points_at
= NULL
;
612 option_no_checkout
= 1;
614 install_branch_config(0, "master", option_origin
,
615 "refs/heads/master");
618 if (remote_head_points_at
&& !option_bare
) {
619 struct strbuf head_ref
= STRBUF_INIT
;
620 strbuf_addstr(&head_ref
, branch_top
.buf
);
621 strbuf_addstr(&head_ref
, "HEAD");
622 create_symref(head_ref
.buf
,
623 remote_head_points_at
->peer_ref
->name
,
627 if (our_head_points_at
) {
628 /* Local default branch link */
629 create_symref("HEAD", our_head_points_at
->name
, NULL
);
631 const char *head
= skip_prefix(our_head_points_at
->name
,
633 update_ref(reflog_msg
.buf
, "HEAD",
634 our_head_points_at
->old_sha1
,
635 NULL
, 0, DIE_ON_ERR
);
636 install_branch_config(0, head
, option_origin
,
637 our_head_points_at
->name
);
639 } else if (remote_head
) {
640 /* Source had detached HEAD pointing somewhere. */
642 update_ref(reflog_msg
.buf
, "HEAD",
643 remote_head
->old_sha1
,
644 NULL
, REF_NODEREF
, DIE_ON_ERR
);
645 our_head_points_at
= remote_head
;
648 /* Nothing to checkout out */
649 if (!option_no_checkout
)
650 warning(_("remote HEAD refers to nonexistent ref, "
651 "unable to checkout.\n"));
652 option_no_checkout
= 1;
656 transport_unlock_pack(transport
);
657 transport_disconnect(transport
);
660 if (!option_no_checkout
) {
661 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
662 struct unpack_trees_options opts
;
667 /* We need to be in the new work tree for the checkout */
670 fd
= hold_locked_index(lock_file
, 1);
672 memset(&opts
, 0, sizeof opts
);
675 opts
.fn
= oneway_merge
;
676 opts
.verbose_update
= (option_verbosity
> 0);
677 opts
.src_index
= &the_index
;
678 opts
.dst_index
= &the_index
;
680 tree
= parse_tree_indirect(our_head_points_at
->old_sha1
);
682 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
683 unpack_trees(1, &t
, &opts
);
685 if (write_cache(fd
, active_cache
, active_nr
) ||
686 commit_locked_index(lock_file
))
687 die(_("unable to write new index file"));
689 err
|= run_hook(NULL
, "post-checkout", sha1_to_hex(null_sha1
),
690 sha1_to_hex(our_head_points_at
->old_sha1
), "1",
693 if (!err
&& option_recursive
)
694 err
= run_command_v_opt(argv_submodule
, RUN_GIT_CMD
);
697 strbuf_release(&reflog_msg
);
698 strbuf_release(&branch_top
);
699 strbuf_release(&key
);
700 strbuf_release(&value
);