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_config
;
50 static struct string_list option_reference
;
52 static int opt_parse_reference(const struct option
*opt
, const char *arg
, int unset
)
54 struct string_list
*option_reference
= opt
->value
;
57 string_list_append(option_reference
, arg
);
61 static struct option builtin_clone_options
[] = {
62 OPT__VERBOSITY(&option_verbosity
),
63 OPT_BOOLEAN(0, "progress", &option_progress
,
64 "force progress reporting"),
65 OPT_BOOLEAN('n', "no-checkout", &option_no_checkout
,
66 "don't create a checkout"),
67 OPT_BOOLEAN(0, "bare", &option_bare
, "create a bare repository"),
68 { OPTION_BOOLEAN
, 0, "naked", &option_bare
, NULL
,
69 "create a bare repository",
70 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
},
71 OPT_BOOLEAN(0, "mirror", &option_mirror
,
72 "create a mirror repository (implies bare)"),
73 OPT_BOOLEAN('l', "local", &option_local
,
74 "to clone from a local repository"),
75 OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks
,
76 "don't use local hardlinks, always copy"),
77 OPT_BOOLEAN('s', "shared", &option_shared
,
78 "setup as shared repository"),
79 OPT_BOOLEAN(0, "recursive", &option_recursive
,
80 "initialize submodules in the clone"),
81 OPT_BOOLEAN(0, "recurse-submodules", &option_recursive
,
82 "initialize submodules in the clone"),
83 OPT_STRING(0, "template", &option_template
, "template-directory",
84 "directory from which templates will be used"),
85 OPT_CALLBACK(0 , "reference", &option_reference
, "repo",
86 "reference repository", &opt_parse_reference
),
87 OPT_STRING('o', "origin", &option_origin
, "branch",
88 "use <branch> instead of 'origin' to track upstream"),
89 OPT_STRING('b', "branch", &option_branch
, "branch",
90 "checkout <branch> instead of the remote's HEAD"),
91 OPT_STRING('u', "upload-pack", &option_upload_pack
, "path",
92 "path to git-upload-pack on the remote"),
93 OPT_STRING(0, "depth", &option_depth
, "depth",
94 "create a shallow clone of that depth"),
95 OPT_STRING(0, "separate-git-dir", &real_git_dir
, "gitdir",
96 "separate git dir from working tree"),
97 OPT_STRING_LIST('c', "config", &option_config
, "key=value",
98 "set config inside the new repository"),
102 static const char *argv_submodule
[] = {
103 "submodule", "update", "--init", "--recursive", NULL
106 static char *get_repo_path(const char *repo
, int *is_bundle
)
108 static char *suffix
[] = { "/.git", ".git", "" };
109 static char *bundle_suffix
[] = { ".bundle", "" };
113 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
115 path
= mkpath("%s%s", repo
, suffix
[i
]);
116 if (is_directory(path
)) {
118 return xstrdup(absolute_path(path
));
122 for (i
= 0; i
< ARRAY_SIZE(bundle_suffix
); i
++) {
124 path
= mkpath("%s%s", repo
, bundle_suffix
[i
]);
125 if (!stat(path
, &st
) && S_ISREG(st
.st_mode
)) {
127 return xstrdup(absolute_path(path
));
134 static char *guess_dir_name(const char *repo
, int is_bundle
, int is_bare
)
136 const char *end
= repo
+ strlen(repo
), *start
;
140 * Strip trailing spaces, slashes and /.git
142 while (repo
< end
&& (is_dir_sep(end
[-1]) || isspace(end
[-1])))
144 if (end
- repo
> 5 && is_dir_sep(end
[-5]) &&
145 !strncmp(end
- 4, ".git", 4)) {
147 while (repo
< end
&& is_dir_sep(end
[-1]))
152 * Find last component, but be prepared that repo could have
153 * the form "remote.example.com:foo.git", i.e. no slash
154 * in the directory part.
157 while (repo
< start
&& !is_dir_sep(start
[-1]) && start
[-1] != ':')
161 * Strip .{bundle,git}.
164 if (end
- start
> 7 && !strncmp(end
- 7, ".bundle", 7))
167 if (end
- start
> 4 && !strncmp(end
- 4, ".git", 4))
172 struct strbuf result
= STRBUF_INIT
;
173 strbuf_addf(&result
, "%.*s.git", (int)(end
- start
), start
);
174 dir
= strbuf_detach(&result
, NULL
);
176 dir
= xstrndup(start
, end
- start
);
178 * Replace sequences of 'control' characters and whitespace
179 * with one ascii space, remove leading and trailing spaces.
183 int prev_space
= 1 /* strip leading whitespace */;
184 for (end
= dir
; *end
; ++end
) {
186 if ((unsigned char)ch
< '\x20')
197 if (out
> dir
&& prev_space
)
203 static void strip_trailing_slashes(char *dir
)
205 char *end
= dir
+ strlen(dir
);
207 while (dir
< end
- 1 && is_dir_sep(end
[-1]))
212 static int add_one_reference(struct string_list_item
*item
, void *cb_data
)
215 struct strbuf alternate
= STRBUF_INIT
;
216 struct remote
*remote
;
217 struct transport
*transport
;
218 const struct ref
*extra
;
220 /* Beware: real_path() and mkpath() return static buffer */
221 ref_git
= xstrdup(real_path(item
->string
));
222 if (is_directory(mkpath("%s/.git/objects", ref_git
))) {
223 char *ref_git_git
= xstrdup(mkpath("%s/.git", ref_git
));
225 ref_git
= ref_git_git
;
226 } else if (!is_directory(mkpath("%s/objects", ref_git
)))
227 die(_("reference repository '%s' is not a local directory."),
230 strbuf_addf(&alternate
, "%s/objects", ref_git
);
231 add_to_alternates_file(alternate
.buf
);
232 strbuf_release(&alternate
);
234 remote
= remote_get(ref_git
);
235 transport
= transport_get(remote
, ref_git
);
236 for (extra
= transport_get_remote_refs(transport
); extra
;
238 add_extra_ref(extra
->name
, extra
->old_sha1
, 0);
240 transport_disconnect(transport
);
245 static void setup_reference(void)
247 for_each_string_list(&option_reference
, add_one_reference
, NULL
);
250 static void copy_alternates(struct strbuf
*src
, struct strbuf
*dst
,
251 const char *src_repo
)
254 * Read from the source objects/info/alternates file
255 * and copy the entries to corresponding file in the
256 * destination repository with add_to_alternates_file().
257 * Both src and dst have "$path/objects/info/alternates".
259 * Instead of copying bit-for-bit from the original,
260 * we need to append to existing one so that the already
261 * created entry via "clone -s" is not lost, and also
262 * to turn entries with paths relative to the original
263 * absolute, so that they can be used in the new repository.
265 FILE *in
= fopen(src
->buf
, "r");
266 struct strbuf line
= STRBUF_INIT
;
268 while (strbuf_getline(&line
, in
, '\n') != EOF
) {
269 char *abs_path
, abs_buf
[PATH_MAX
];
270 if (!line
.len
|| line
.buf
[0] == '#')
272 if (is_absolute_path(line
.buf
)) {
273 add_to_alternates_file(line
.buf
);
276 abs_path
= mkpath("%s/objects/%s", src_repo
, line
.buf
);
277 normalize_path_copy(abs_buf
, abs_path
);
278 add_to_alternates_file(abs_buf
);
280 strbuf_release(&line
);
284 static void copy_or_link_directory(struct strbuf
*src
, struct strbuf
*dest
,
285 const char *src_repo
, int src_baselen
)
289 int src_len
, dest_len
;
292 dir
= opendir(src
->buf
);
294 die_errno(_("failed to open '%s'"), src
->buf
);
296 if (mkdir(dest
->buf
, 0777)) {
298 die_errno(_("failed to create directory '%s'"), dest
->buf
);
299 else if (stat(dest
->buf
, &buf
))
300 die_errno(_("failed to stat '%s'"), dest
->buf
);
301 else if (!S_ISDIR(buf
.st_mode
))
302 die(_("%s exists and is not a directory"), dest
->buf
);
305 strbuf_addch(src
, '/');
307 strbuf_addch(dest
, '/');
308 dest_len
= dest
->len
;
310 while ((de
= readdir(dir
)) != NULL
) {
311 strbuf_setlen(src
, src_len
);
312 strbuf_addstr(src
, de
->d_name
);
313 strbuf_setlen(dest
, dest_len
);
314 strbuf_addstr(dest
, de
->d_name
);
315 if (stat(src
->buf
, &buf
)) {
316 warning (_("failed to stat %s\n"), src
->buf
);
319 if (S_ISDIR(buf
.st_mode
)) {
320 if (de
->d_name
[0] != '.')
321 copy_or_link_directory(src
, dest
,
322 src_repo
, src_baselen
);
326 /* Files that cannot be copied bit-for-bit... */
327 if (!strcmp(src
->buf
+ src_baselen
, "/info/alternates")) {
328 copy_alternates(src
, dest
, src_repo
);
332 if (unlink(dest
->buf
) && errno
!= ENOENT
)
333 die_errno(_("failed to unlink '%s'"), dest
->buf
);
334 if (!option_no_hardlinks
) {
335 if (!link(src
->buf
, dest
->buf
))
338 die_errno(_("failed to create link '%s'"), dest
->buf
);
339 option_no_hardlinks
= 1;
341 if (copy_file_with_time(dest
->buf
, src
->buf
, 0666))
342 die_errno(_("failed to copy file to '%s'"), dest
->buf
);
347 static const struct ref
*clone_local(const char *src_repo
,
348 const char *dest_repo
)
350 const struct ref
*ret
;
351 struct remote
*remote
;
352 struct transport
*transport
;
355 struct strbuf alt
= STRBUF_INIT
;
356 strbuf_addf(&alt
, "%s/objects", src_repo
);
357 add_to_alternates_file(alt
.buf
);
358 strbuf_release(&alt
);
360 struct strbuf src
= STRBUF_INIT
;
361 struct strbuf dest
= STRBUF_INIT
;
362 strbuf_addf(&src
, "%s/objects", src_repo
);
363 strbuf_addf(&dest
, "%s/objects", dest_repo
);
364 copy_or_link_directory(&src
, &dest
, src_repo
, src
.len
);
365 strbuf_release(&src
);
366 strbuf_release(&dest
);
369 remote
= remote_get(src_repo
);
370 transport
= transport_get(remote
, src_repo
);
371 ret
= transport_get_remote_refs(transport
);
372 transport_disconnect(transport
);
373 if (0 <= option_verbosity
)
374 printf(_("done.\n"));
378 static const char *junk_work_tree
;
379 static const char *junk_git_dir
;
380 static pid_t junk_pid
;
382 static void remove_junk(void)
384 struct strbuf sb
= STRBUF_INIT
;
385 if (getpid() != junk_pid
)
388 strbuf_addstr(&sb
, junk_git_dir
);
389 remove_dir_recursively(&sb
, 0);
392 if (junk_work_tree
) {
393 strbuf_addstr(&sb
, junk_work_tree
);
394 remove_dir_recursively(&sb
, 0);
399 static void remove_junk_on_signal(int signo
)
406 static struct ref
*wanted_peer_refs(const struct ref
*refs
,
407 struct refspec
*refspec
)
409 struct ref
*head
= copy_ref(find_ref_by_name(refs
, "HEAD"));
410 struct ref
*local_refs
= head
;
411 struct ref
**tail
= head
? &head
->next
: &local_refs
;
413 get_fetch_map(refs
, refspec
, &tail
, 0);
415 get_fetch_map(refs
, tag_refspec
, &tail
, 0);
420 static void write_remote_refs(const struct ref
*local_refs
)
424 for (r
= local_refs
; r
; r
= r
->next
) {
427 add_extra_ref(r
->peer_ref
->name
, r
->old_sha1
, 0);
430 pack_refs(PACK_REFS_ALL
);
434 static int write_one_config(const char *key
, const char *value
, void *data
)
436 return git_config_set_multivar(key
, value
? value
: "true", "^$", 0);
439 static void write_config(struct string_list
*config
)
443 for (i
= 0; i
< config
->nr
; i
++) {
444 if (git_config_parse_parameter(config
->items
[i
].string
,
445 write_one_config
, NULL
) < 0)
446 die("unable to write parameters to config file");
450 int cmd_clone(int argc
, const char **argv
, const char *prefix
)
452 int is_bundle
= 0, is_local
;
454 const char *repo_name
, *repo
, *work_tree
, *git_dir
;
457 const struct ref
*refs
, *remote_head
;
458 const struct ref
*remote_head_points_at
;
459 const struct ref
*our_head_points_at
;
460 struct ref
*mapped_refs
;
461 struct strbuf key
= STRBUF_INIT
, value
= STRBUF_INIT
;
462 struct strbuf branch_top
= STRBUF_INIT
, reflog_msg
= STRBUF_INIT
;
463 struct transport
*transport
= NULL
;
464 char *src_ref_prefix
= "refs/heads/";
467 struct refspec
*refspec
;
468 const char *fetch_pattern
;
472 packet_trace_identity("clone");
473 argc
= parse_options(argc
, argv
, prefix
, builtin_clone_options
,
474 builtin_clone_usage
, 0);
477 usage_msg_opt(_("Too many arguments."),
478 builtin_clone_usage
, builtin_clone_options
);
481 usage_msg_opt(_("You must specify a repository to clone."),
482 builtin_clone_usage
, builtin_clone_options
);
489 die(_("--bare and --origin %s options are incompatible."),
491 option_no_checkout
= 1;
495 option_origin
= "origin";
499 path
= get_repo_path(repo_name
, &is_bundle
);
501 repo
= xstrdup(absolute_path(repo_name
));
502 else if (!strchr(repo_name
, ':'))
503 die(_("repository '%s' does not exist"), repo_name
);
506 is_local
= path
&& !is_bundle
;
507 if (is_local
&& option_depth
)
508 warning(_("--depth is ignored in local clones; use file:// instead."));
511 dir
= xstrdup(argv
[1]);
513 dir
= guess_dir_name(repo_name
, is_bundle
, option_bare
);
514 strip_trailing_slashes(dir
);
516 dest_exists
= !stat(dir
, &buf
);
517 if (dest_exists
&& !is_empty_dir(dir
))
518 die(_("destination path '%s' already exists and is not "
519 "an empty directory."), dir
);
521 strbuf_addf(&reflog_msg
, "clone: from %s", repo
);
526 work_tree
= getenv("GIT_WORK_TREE");
527 if (work_tree
&& !stat(work_tree
, &buf
))
528 die(_("working tree '%s' already exists."), work_tree
);
531 if (option_bare
|| work_tree
)
532 git_dir
= xstrdup(dir
);
535 git_dir
= xstrdup(mkpath("%s/.git", dir
));
539 junk_work_tree
= work_tree
;
540 if (safe_create_leading_directories_const(work_tree
) < 0)
541 die_errno(_("could not create leading directories of '%s'"),
543 if (!dest_exists
&& mkdir(work_tree
, 0755))
544 die_errno(_("could not create work tree dir '%s'."),
546 set_git_work_tree(work_tree
);
548 junk_git_dir
= git_dir
;
550 sigchain_push_common(remove_junk_on_signal
);
552 setenv(CONFIG_ENVIRONMENT
, mkpath("%s/config", git_dir
), 1);
554 if (safe_create_leading_directories_const(git_dir
) < 0)
555 die(_("could not create leading directories of '%s'"), git_dir
);
557 set_git_dir_init(git_dir
, real_git_dir
, 0);
559 git_dir
= real_git_dir
;
561 if (0 <= option_verbosity
) {
563 printf(_("Cloning into bare repository %s...\n"), dir
);
565 printf(_("Cloning into %s...\n"), dir
);
567 init_db(option_template
, INIT_DB_QUIET
);
568 write_config(&option_config
);
571 * At this point, the config exists, so we do not need the
572 * environment variable. We actually need to unset it, too, to
573 * re-enable parsing of the global configs.
575 unsetenv(CONFIG_ENVIRONMENT
);
577 git_config(git_default_config
, NULL
);
581 src_ref_prefix
= "refs/";
582 strbuf_addstr(&branch_top
, src_ref_prefix
);
584 git_config_set("core.bare", "true");
586 strbuf_addf(&branch_top
, "refs/remotes/%s/", option_origin
);
589 strbuf_addf(&value
, "+%s*:%s*", src_ref_prefix
, branch_top
.buf
);
591 if (option_mirror
|| !option_bare
) {
592 /* Configure the remote */
593 strbuf_addf(&key
, "remote.%s.fetch", option_origin
);
594 git_config_set_multivar(key
.buf
, value
.buf
, "^$", 0);
598 strbuf_addf(&key
, "remote.%s.mirror", option_origin
);
599 git_config_set(key
.buf
, "true");
604 strbuf_addf(&key
, "remote.%s.url", option_origin
);
605 git_config_set(key
.buf
, repo
);
608 if (option_reference
.nr
)
611 fetch_pattern
= value
.buf
;
612 refspec
= parse_fetch_refspec(1, &fetch_pattern
);
614 strbuf_reset(&value
);
617 refs
= clone_local(path
, git_dir
);
618 mapped_refs
= wanted_peer_refs(refs
, refspec
);
620 struct remote
*remote
= remote_get(option_origin
);
621 transport
= transport_get(remote
, remote
->url
[0]);
623 if (!transport
->get_refs_list
|| !transport
->fetch
)
624 die(_("Don't know how to clone %s"), transport
->url
);
626 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
629 transport_set_option(transport
, TRANS_OPT_DEPTH
,
632 transport_set_verbosity(transport
, option_verbosity
, option_progress
);
634 if (option_upload_pack
)
635 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
,
638 refs
= transport_get_remote_refs(transport
);
640 mapped_refs
= wanted_peer_refs(refs
, refspec
);
641 transport_fetch_refs(transport
, mapped_refs
);
648 write_remote_refs(mapped_refs
);
650 remote_head
= find_ref_by_name(refs
, "HEAD");
651 remote_head_points_at
=
652 guess_remote_head(remote_head
, mapped_refs
, 0);
655 struct strbuf head
= STRBUF_INIT
;
656 strbuf_addstr(&head
, src_ref_prefix
);
657 strbuf_addstr(&head
, option_branch
);
659 find_ref_by_name(mapped_refs
, head
.buf
);
660 strbuf_release(&head
);
662 if (!our_head_points_at
) {
663 warning(_("Remote branch %s not found in "
664 "upstream %s, using HEAD instead"),
665 option_branch
, option_origin
);
666 our_head_points_at
= remote_head_points_at
;
670 our_head_points_at
= remote_head_points_at
;
673 warning(_("You appear to have cloned an empty repository."));
674 our_head_points_at
= NULL
;
675 remote_head_points_at
= NULL
;
677 option_no_checkout
= 1;
679 install_branch_config(0, "master", option_origin
,
680 "refs/heads/master");
683 if (remote_head_points_at
&& !option_bare
) {
684 struct strbuf head_ref
= STRBUF_INIT
;
685 strbuf_addstr(&head_ref
, branch_top
.buf
);
686 strbuf_addstr(&head_ref
, "HEAD");
687 create_symref(head_ref
.buf
,
688 remote_head_points_at
->peer_ref
->name
,
692 if (our_head_points_at
) {
693 /* Local default branch link */
694 create_symref("HEAD", our_head_points_at
->name
, NULL
);
696 const char *head
= skip_prefix(our_head_points_at
->name
,
698 update_ref(reflog_msg
.buf
, "HEAD",
699 our_head_points_at
->old_sha1
,
700 NULL
, 0, DIE_ON_ERR
);
701 install_branch_config(0, head
, option_origin
,
702 our_head_points_at
->name
);
704 } else if (remote_head
) {
705 /* Source had detached HEAD pointing somewhere. */
707 update_ref(reflog_msg
.buf
, "HEAD",
708 remote_head
->old_sha1
,
709 NULL
, REF_NODEREF
, DIE_ON_ERR
);
710 our_head_points_at
= remote_head
;
713 /* Nothing to checkout out */
714 if (!option_no_checkout
)
715 warning(_("remote HEAD refers to nonexistent ref, "
716 "unable to checkout.\n"));
717 option_no_checkout
= 1;
721 transport_unlock_pack(transport
);
722 transport_disconnect(transport
);
725 if (!option_no_checkout
) {
726 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
727 struct unpack_trees_options opts
;
732 /* We need to be in the new work tree for the checkout */
735 fd
= hold_locked_index(lock_file
, 1);
737 memset(&opts
, 0, sizeof opts
);
740 opts
.fn
= oneway_merge
;
741 opts
.verbose_update
= (option_verbosity
> 0);
742 opts
.src_index
= &the_index
;
743 opts
.dst_index
= &the_index
;
745 tree
= parse_tree_indirect(our_head_points_at
->old_sha1
);
747 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
748 unpack_trees(1, &t
, &opts
);
750 if (write_cache(fd
, active_cache
, active_nr
) ||
751 commit_locked_index(lock_file
))
752 die(_("unable to write new index file"));
754 err
|= run_hook(NULL
, "post-checkout", sha1_to_hex(null_sha1
),
755 sha1_to_hex(our_head_points_at
->old_sha1
), "1",
758 if (!err
&& option_recursive
)
759 err
= run_command_v_opt(argv_submodule
, RUN_GIT_CMD
);
762 strbuf_release(&reflog_msg
);
763 strbuf_release(&branch_top
);
764 strbuf_release(&key
);
765 strbuf_release(&value
);