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.
13 #include "parse-options.h"
14 #include "fetch-pack.h"
17 #include "tree-walk.h"
18 #include "unpack-trees.h"
19 #include "transport.h"
25 #include "run-command.h"
26 #include "connected.h"
30 * - respect DB_ENVIRONMENT for .git/objects.
32 * Implementation notes:
33 * - dropping use-separate-remote and no-separate-remote compatibility
36 static const char * const builtin_clone_usage
[] = {
37 N_("git clone [<options>] [--] <repo> [<dir>]"),
41 static int option_no_checkout
, option_bare
, option_mirror
, option_single_branch
= -1;
42 static int option_local
= -1, option_no_hardlinks
, option_shared
, option_recursive
;
43 static char *option_template
, *option_depth
;
44 static char *option_origin
= NULL
;
45 static char *option_branch
= NULL
;
46 static const char *real_git_dir
;
47 static char *option_upload_pack
= "git-upload-pack";
48 static int option_verbosity
;
49 static int option_progress
= -1;
50 static struct string_list option_config
;
51 static struct string_list option_reference
;
52 static int option_dissociate
;
54 static int opt_parse_reference(const struct option
*opt
, const char *arg
, int unset
)
56 struct string_list
*option_reference
= opt
->value
;
59 string_list_append(option_reference
, arg
);
63 static struct option builtin_clone_options
[] = {
64 OPT__VERBOSITY(&option_verbosity
),
65 OPT_BOOL(0, "progress", &option_progress
,
66 N_("force progress reporting")),
67 OPT_BOOL('n', "no-checkout", &option_no_checkout
,
68 N_("don't create a checkout")),
69 OPT_BOOL(0, "bare", &option_bare
, N_("create a bare repository")),
70 OPT_HIDDEN_BOOL(0, "naked", &option_bare
,
71 N_("create a bare repository")),
72 OPT_BOOL(0, "mirror", &option_mirror
,
73 N_("create a mirror repository (implies bare)")),
74 OPT_BOOL('l', "local", &option_local
,
75 N_("to clone from a local repository")),
76 OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks
,
77 N_("don't use local hardlinks, always copy")),
78 OPT_BOOL('s', "shared", &option_shared
,
79 N_("setup as shared repository")),
80 OPT_BOOL(0, "recursive", &option_recursive
,
81 N_("initialize submodules in the clone")),
82 OPT_BOOL(0, "recurse-submodules", &option_recursive
,
83 N_("initialize submodules in the clone")),
84 OPT_STRING(0, "template", &option_template
, N_("template-directory"),
85 N_("directory from which templates will be used")),
86 OPT_CALLBACK(0 , "reference", &option_reference
, N_("repo"),
87 N_("reference repository"), &opt_parse_reference
),
88 OPT_STRING('o', "origin", &option_origin
, N_("name"),
89 N_("use <name> instead of 'origin' to track upstream")),
90 OPT_STRING('b', "branch", &option_branch
, N_("branch"),
91 N_("checkout <branch> instead of the remote's HEAD")),
92 OPT_STRING('u', "upload-pack", &option_upload_pack
, N_("path"),
93 N_("path to git-upload-pack on the remote")),
94 OPT_STRING(0, "depth", &option_depth
, N_("depth"),
95 N_("create a shallow clone of that depth")),
96 OPT_BOOL(0, "single-branch", &option_single_branch
,
97 N_("clone only one branch, HEAD or --branch")),
98 OPT_BOOL(0, "dissociate", &option_dissociate
,
99 N_("use --reference only while cloning")),
100 OPT_STRING(0, "separate-git-dir", &real_git_dir
, N_("gitdir"),
101 N_("separate git dir from working tree")),
102 OPT_STRING_LIST('c', "config", &option_config
, N_("key=value"),
103 N_("set config inside the new repository")),
107 static const char *argv_submodule
[] = {
108 "submodule", "update", "--init", "--recursive", NULL
111 static char *get_repo_path(const char *repo
, int *is_bundle
)
113 static char *suffix
[] = { "/.git", "", ".git/.git", ".git" };
114 static char *bundle_suffix
[] = { ".bundle", "" };
118 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
120 path
= mkpath("%s%s", repo
, suffix
[i
]);
123 if (S_ISDIR(st
.st_mode
) && is_git_directory(path
)) {
125 return xstrdup(absolute_path(path
));
126 } else if (S_ISREG(st
.st_mode
) && st
.st_size
> 8) {
127 /* Is it a "gitfile"? */
129 int len
, fd
= open(path
, O_RDONLY
);
132 len
= read_in_full(fd
, signature
, 8);
134 if (len
!= 8 || strncmp(signature
, "gitdir: ", 8))
136 path
= read_gitfile(path
);
139 return xstrdup(absolute_path(path
));
144 for (i
= 0; i
< ARRAY_SIZE(bundle_suffix
); i
++) {
146 path
= mkpath("%s%s", repo
, bundle_suffix
[i
]);
147 if (!stat(path
, &st
) && S_ISREG(st
.st_mode
)) {
149 return xstrdup(absolute_path(path
));
156 static char *guess_dir_name(const char *repo
, int is_bundle
, int is_bare
)
158 const char *end
= repo
+ strlen(repo
), *start
;
162 * Strip trailing spaces, slashes and /.git
164 while (repo
< end
&& (is_dir_sep(end
[-1]) || isspace(end
[-1])))
166 if (end
- repo
> 5 && is_dir_sep(end
[-5]) &&
167 !strncmp(end
- 4, ".git", 4)) {
169 while (repo
< end
&& is_dir_sep(end
[-1]))
174 * Find last component, but be prepared that repo could have
175 * the form "remote.example.com:foo.git", i.e. no slash
176 * in the directory part.
179 while (repo
< start
&& !is_dir_sep(start
[-1]) && start
[-1] != ':')
183 * Strip .{bundle,git}.
186 if (end
- start
> 7 && !strncmp(end
- 7, ".bundle", 7))
189 if (end
- start
> 4 && !strncmp(end
- 4, ".git", 4))
194 struct strbuf result
= STRBUF_INIT
;
195 strbuf_addf(&result
, "%.*s.git", (int)(end
- start
), start
);
196 dir
= strbuf_detach(&result
, NULL
);
198 dir
= xstrndup(start
, end
- start
);
200 * Replace sequences of 'control' characters and whitespace
201 * with one ascii space, remove leading and trailing spaces.
205 int prev_space
= 1 /* strip leading whitespace */;
206 for (end
= dir
; *end
; ++end
) {
208 if ((unsigned char)ch
< '\x20')
219 if (out
> dir
&& prev_space
)
225 static void strip_trailing_slashes(char *dir
)
227 char *end
= dir
+ strlen(dir
);
229 while (dir
< end
- 1 && is_dir_sep(end
[-1]))
234 static int add_one_reference(struct string_list_item
*item
, void *cb_data
)
238 struct strbuf alternate
= STRBUF_INIT
;
240 /* Beware: read_gitfile(), real_path() and mkpath() return static buffer */
241 ref_git
= xstrdup(real_path(item
->string
));
243 repo
= read_gitfile(ref_git
);
245 repo
= read_gitfile(mkpath("%s/.git", ref_git
));
248 ref_git
= xstrdup(repo
);
251 if (!repo
&& is_directory(mkpath("%s/.git/objects", ref_git
))) {
252 char *ref_git_git
= mkpathdup("%s/.git", ref_git
);
254 ref_git
= ref_git_git
;
255 } else if (!is_directory(mkpath("%s/objects", ref_git
)))
256 die(_("reference repository '%s' is not a local repository."),
259 if (!access(mkpath("%s/shallow", ref_git
), F_OK
))
260 die(_("reference repository '%s' is shallow"), item
->string
);
262 if (!access(mkpath("%s/info/grafts", ref_git
), F_OK
))
263 die(_("reference repository '%s' is grafted"), item
->string
);
265 strbuf_addf(&alternate
, "%s/objects", ref_git
);
266 add_to_alternates_file(alternate
.buf
);
267 strbuf_release(&alternate
);
272 static void setup_reference(void)
274 for_each_string_list(&option_reference
, add_one_reference
, NULL
);
277 static void copy_alternates(struct strbuf
*src
, struct strbuf
*dst
,
278 const char *src_repo
)
281 * Read from the source objects/info/alternates file
282 * and copy the entries to corresponding file in the
283 * destination repository with add_to_alternates_file().
284 * Both src and dst have "$path/objects/info/alternates".
286 * Instead of copying bit-for-bit from the original,
287 * we need to append to existing one so that the already
288 * created entry via "clone -s" is not lost, and also
289 * to turn entries with paths relative to the original
290 * absolute, so that they can be used in the new repository.
292 FILE *in
= fopen(src
->buf
, "r");
293 struct strbuf line
= STRBUF_INIT
;
295 while (strbuf_getline(&line
, in
, '\n') != EOF
) {
296 char *abs_path
, abs_buf
[PATH_MAX
];
297 if (!line
.len
|| line
.buf
[0] == '#')
299 if (is_absolute_path(line
.buf
)) {
300 add_to_alternates_file(line
.buf
);
303 abs_path
= mkpath("%s/objects/%s", src_repo
, line
.buf
);
304 normalize_path_copy(abs_buf
, abs_path
);
305 add_to_alternates_file(abs_buf
);
307 strbuf_release(&line
);
311 static void copy_or_link_directory(struct strbuf
*src
, struct strbuf
*dest
,
312 const char *src_repo
, int src_baselen
)
316 int src_len
, dest_len
;
319 dir
= opendir(src
->buf
);
321 die_errno(_("failed to open '%s'"), src
->buf
);
323 if (mkdir(dest
->buf
, 0777)) {
325 die_errno(_("failed to create directory '%s'"), dest
->buf
);
326 else if (stat(dest
->buf
, &buf
))
327 die_errno(_("failed to stat '%s'"), dest
->buf
);
328 else if (!S_ISDIR(buf
.st_mode
))
329 die(_("%s exists and is not a directory"), dest
->buf
);
332 strbuf_addch(src
, '/');
334 strbuf_addch(dest
, '/');
335 dest_len
= dest
->len
;
337 while ((de
= readdir(dir
)) != NULL
) {
338 strbuf_setlen(src
, src_len
);
339 strbuf_addstr(src
, de
->d_name
);
340 strbuf_setlen(dest
, dest_len
);
341 strbuf_addstr(dest
, de
->d_name
);
342 if (stat(src
->buf
, &buf
)) {
343 warning (_("failed to stat %s\n"), src
->buf
);
346 if (S_ISDIR(buf
.st_mode
)) {
347 if (de
->d_name
[0] != '.')
348 copy_or_link_directory(src
, dest
,
349 src_repo
, src_baselen
);
353 /* Files that cannot be copied bit-for-bit... */
354 if (!strcmp(src
->buf
+ src_baselen
, "/info/alternates")) {
355 copy_alternates(src
, dest
, src_repo
);
359 if (unlink(dest
->buf
) && errno
!= ENOENT
)
360 die_errno(_("failed to unlink '%s'"), dest
->buf
);
361 if (!option_no_hardlinks
) {
362 if (!link(src
->buf
, dest
->buf
))
364 if (option_local
> 0)
365 die_errno(_("failed to create link '%s'"), dest
->buf
);
366 option_no_hardlinks
= 1;
368 if (copy_file_with_time(dest
->buf
, src
->buf
, 0666))
369 die_errno(_("failed to copy file to '%s'"), dest
->buf
);
374 static void clone_local(const char *src_repo
, const char *dest_repo
)
377 struct strbuf alt
= STRBUF_INIT
;
378 strbuf_addf(&alt
, "%s/objects", src_repo
);
379 add_to_alternates_file(alt
.buf
);
380 strbuf_release(&alt
);
382 struct strbuf src
= STRBUF_INIT
;
383 struct strbuf dest
= STRBUF_INIT
;
384 strbuf_addf(&src
, "%s/objects", src_repo
);
385 strbuf_addf(&dest
, "%s/objects", dest_repo
);
386 copy_or_link_directory(&src
, &dest
, src_repo
, src
.len
);
387 strbuf_release(&src
);
388 strbuf_release(&dest
);
391 if (0 <= option_verbosity
)
392 fprintf(stderr
, _("done.\n"));
395 static const char *junk_work_tree
;
396 static const char *junk_git_dir
;
401 } junk_mode
= JUNK_LEAVE_NONE
;
403 static const char junk_leave_repo_msg
[] =
404 N_("Clone succeeded, but checkout failed.\n"
405 "You can inspect what was checked out with 'git status'\n"
406 "and retry the checkout with 'git checkout -f HEAD'\n");
408 static void remove_junk(void)
410 struct strbuf sb
= STRBUF_INIT
;
413 case JUNK_LEAVE_REPO
:
414 warning("%s", _(junk_leave_repo_msg
));
419 /* proceed to removal */
424 strbuf_addstr(&sb
, junk_git_dir
);
425 remove_dir_recursively(&sb
, 0);
428 if (junk_work_tree
) {
429 strbuf_addstr(&sb
, junk_work_tree
);
430 remove_dir_recursively(&sb
, 0);
435 static void remove_junk_on_signal(int signo
)
442 static struct ref
*find_remote_branch(const struct ref
*refs
, const char *branch
)
445 struct strbuf head
= STRBUF_INIT
;
446 strbuf_addstr(&head
, "refs/heads/");
447 strbuf_addstr(&head
, branch
);
448 ref
= find_ref_by_name(refs
, head
.buf
);
449 strbuf_release(&head
);
454 strbuf_addstr(&head
, "refs/tags/");
455 strbuf_addstr(&head
, branch
);
456 ref
= find_ref_by_name(refs
, head
.buf
);
457 strbuf_release(&head
);
462 static struct ref
*wanted_peer_refs(const struct ref
*refs
,
463 struct refspec
*refspec
)
465 struct ref
*head
= copy_ref(find_ref_by_name(refs
, "HEAD"));
466 struct ref
*local_refs
= head
;
467 struct ref
**tail
= head
? &head
->next
: &local_refs
;
469 if (option_single_branch
) {
470 struct ref
*remote_head
= NULL
;
473 remote_head
= guess_remote_head(head
, refs
, 0);
477 remote_head
= copy_ref(find_remote_branch(refs
, option_branch
));
480 if (!remote_head
&& option_branch
)
481 warning(_("Could not find remote branch %s to clone."),
484 get_fetch_map(remote_head
, refspec
, &tail
, 0);
486 /* if --branch=tag, pull the requested tag explicitly */
487 get_fetch_map(remote_head
, tag_refspec
, &tail
, 0);
490 get_fetch_map(refs
, refspec
, &tail
, 0);
492 if (!option_mirror
&& !option_single_branch
)
493 get_fetch_map(refs
, tag_refspec
, &tail
, 0);
498 static void write_remote_refs(const struct ref
*local_refs
)
502 lock_packed_refs(LOCK_DIE_ON_ERROR
);
504 for (r
= local_refs
; r
; r
= r
->next
) {
507 add_packed_ref(r
->peer_ref
->name
, r
->old_sha1
);
510 if (commit_packed_refs())
511 die_errno("unable to overwrite old ref-pack file");
514 static void write_followtags(const struct ref
*refs
, const char *msg
)
516 const struct ref
*ref
;
517 for (ref
= refs
; ref
; ref
= ref
->next
) {
518 if (!starts_with(ref
->name
, "refs/tags/"))
520 if (ends_with(ref
->name
, "^{}"))
522 if (!has_sha1_file(ref
->old_sha1
))
524 update_ref(msg
, ref
->name
, ref
->old_sha1
,
525 NULL
, 0, UPDATE_REFS_DIE_ON_ERR
);
529 static int iterate_ref_map(void *cb_data
, unsigned char sha1
[20])
531 struct ref
**rm
= cb_data
;
532 struct ref
*ref
= *rm
;
535 * Skip anything missing a peer_ref, which we are not
536 * actually going to write a ref for.
538 while (ref
&& !ref
->peer_ref
)
540 /* Returning -1 notes "end of list" to the caller. */
544 hashcpy(sha1
, ref
->old_sha1
);
549 static void update_remote_refs(const struct ref
*refs
,
550 const struct ref
*mapped_refs
,
551 const struct ref
*remote_head_points_at
,
552 const char *branch_top
,
554 struct transport
*transport
,
555 int check_connectivity
)
557 const struct ref
*rm
= mapped_refs
;
559 if (check_connectivity
) {
560 if (transport
->progress
)
561 fprintf(stderr
, _("Checking connectivity... "));
562 if (check_everything_connected_with_transport(iterate_ref_map
,
564 die(_("remote did not send all necessary objects"));
565 if (transport
->progress
)
566 fprintf(stderr
, _("done.\n"));
570 write_remote_refs(mapped_refs
);
571 if (option_single_branch
)
572 write_followtags(refs
, msg
);
575 if (remote_head_points_at
&& !option_bare
) {
576 struct strbuf head_ref
= STRBUF_INIT
;
577 strbuf_addstr(&head_ref
, branch_top
);
578 strbuf_addstr(&head_ref
, "HEAD");
579 create_symref(head_ref
.buf
,
580 remote_head_points_at
->peer_ref
->name
,
585 static void update_head(const struct ref
*our
, const struct ref
*remote
,
589 if (our
&& skip_prefix(our
->name
, "refs/heads/", &head
)) {
590 /* Local default branch link */
591 create_symref("HEAD", our
->name
, NULL
);
593 update_ref(msg
, "HEAD", our
->old_sha1
, NULL
, 0,
594 UPDATE_REFS_DIE_ON_ERR
);
595 install_branch_config(0, head
, option_origin
, our
->name
);
598 struct commit
*c
= lookup_commit_reference(our
->old_sha1
);
599 /* --branch specifies a non-branch (i.e. tags), detach HEAD */
600 update_ref(msg
, "HEAD", c
->object
.sha1
,
601 NULL
, REF_NODEREF
, UPDATE_REFS_DIE_ON_ERR
);
604 * We know remote HEAD points to a non-branch, or
605 * HEAD points to a branch but we don't know which one.
606 * Detach HEAD in all these cases.
608 update_ref(msg
, "HEAD", remote
->old_sha1
,
609 NULL
, REF_NODEREF
, UPDATE_REFS_DIE_ON_ERR
);
613 static int checkout(void)
615 unsigned char sha1
[20];
617 struct lock_file
*lock_file
;
618 struct unpack_trees_options opts
;
623 if (option_no_checkout
)
626 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, sha1
, NULL
);
628 warning(_("remote HEAD refers to nonexistent ref, "
629 "unable to checkout.\n"));
632 if (!strcmp(head
, "HEAD")) {
633 if (advice_detached_head
)
634 detach_advice(sha1_to_hex(sha1
));
636 if (!starts_with(head
, "refs/heads/"))
637 die(_("HEAD not found below refs/heads!"));
641 /* We need to be in the new work tree for the checkout */
644 lock_file
= xcalloc(1, sizeof(struct lock_file
));
645 hold_locked_index(lock_file
, 1);
647 memset(&opts
, 0, sizeof opts
);
650 opts
.fn
= oneway_merge
;
651 opts
.verbose_update
= (option_verbosity
>= 0);
652 opts
.src_index
= &the_index
;
653 opts
.dst_index
= &the_index
;
655 tree
= parse_tree_indirect(sha1
);
657 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
658 if (unpack_trees(1, &t
, &opts
) < 0)
659 die(_("unable to checkout working tree"));
661 if (write_locked_index(&the_index
, lock_file
, COMMIT_LOCK
))
662 die(_("unable to write new index file"));
664 err
|= run_hook_le(NULL
, "post-checkout", sha1_to_hex(null_sha1
),
665 sha1_to_hex(sha1
), "1", NULL
);
667 if (!err
&& option_recursive
)
668 err
= run_command_v_opt(argv_submodule
, RUN_GIT_CMD
);
673 static int write_one_config(const char *key
, const char *value
, void *data
)
675 return git_config_set_multivar(key
, value
? value
: "true", "^$", 0);
678 static void write_config(struct string_list
*config
)
682 for (i
= 0; i
< config
->nr
; i
++) {
683 if (git_config_parse_parameter(config
->items
[i
].string
,
684 write_one_config
, NULL
) < 0)
685 die("unable to write parameters to config file");
689 static void write_refspec_config(const char *src_ref_prefix
,
690 const struct ref
*our_head_points_at
,
691 const struct ref
*remote_head_points_at
,
692 struct strbuf
*branch_top
)
694 struct strbuf key
= STRBUF_INIT
;
695 struct strbuf value
= STRBUF_INIT
;
697 if (option_mirror
|| !option_bare
) {
698 if (option_single_branch
&& !option_mirror
) {
700 if (starts_with(our_head_points_at
->name
, "refs/tags/"))
701 strbuf_addf(&value
, "+%s:%s", our_head_points_at
->name
,
702 our_head_points_at
->name
);
704 strbuf_addf(&value
, "+%s:%s%s", our_head_points_at
->name
,
705 branch_top
->buf
, option_branch
);
706 } else if (remote_head_points_at
) {
707 const char *head
= remote_head_points_at
->name
;
708 if (!skip_prefix(head
, "refs/heads/", &head
))
709 die("BUG: remote HEAD points at non-head?");
711 strbuf_addf(&value
, "+%s:%s%s", remote_head_points_at
->name
,
712 branch_top
->buf
, head
);
715 * otherwise, the next "git fetch" will
716 * simply fetch from HEAD without updating
717 * any remote-tracking branch, which is what
721 strbuf_addf(&value
, "+%s*:%s*", src_ref_prefix
, branch_top
->buf
);
723 /* Configure the remote */
725 strbuf_addf(&key
, "remote.%s.fetch", option_origin
);
726 git_config_set_multivar(key
.buf
, value
.buf
, "^$", 0);
730 strbuf_addf(&key
, "remote.%s.mirror", option_origin
);
731 git_config_set(key
.buf
, "true");
737 strbuf_release(&key
);
738 strbuf_release(&value
);
741 static void dissociate_from_references(void)
743 static const char* argv
[] = { "repack", "-a", "-d", NULL
};
745 if (run_command_v_opt(argv
, RUN_GIT_CMD
|RUN_COMMAND_NO_STDIN
))
746 die(_("cannot repack to clean up"));
747 if (unlink(git_path("objects/info/alternates")) && errno
!= ENOENT
)
748 die_errno(_("cannot unlink temporary alternates file"));
751 int cmd_clone(int argc
, const char **argv
, const char *prefix
)
753 int is_bundle
= 0, is_local
;
755 const char *repo_name
, *repo
, *work_tree
, *git_dir
;
758 const struct ref
*refs
, *remote_head
;
759 const struct ref
*remote_head_points_at
;
760 const struct ref
*our_head_points_at
;
761 struct ref
*mapped_refs
;
762 const struct ref
*ref
;
763 struct strbuf key
= STRBUF_INIT
, value
= STRBUF_INIT
;
764 struct strbuf branch_top
= STRBUF_INIT
, reflog_msg
= STRBUF_INIT
;
765 struct transport
*transport
= NULL
;
766 const char *src_ref_prefix
= "refs/heads/";
767 struct remote
*remote
;
768 int err
= 0, complete_refs_before_fetch
= 1;
770 struct refspec
*refspec
;
771 const char *fetch_pattern
;
773 packet_trace_identity("clone");
774 argc
= parse_options(argc
, argv
, prefix
, builtin_clone_options
,
775 builtin_clone_usage
, 0);
778 usage_msg_opt(_("Too many arguments."),
779 builtin_clone_usage
, builtin_clone_options
);
782 usage_msg_opt(_("You must specify a repository to clone."),
783 builtin_clone_usage
, builtin_clone_options
);
785 if (option_single_branch
== -1)
786 option_single_branch
= option_depth
? 1 : 0;
793 die(_("--bare and --origin %s options are incompatible."),
796 die(_("--bare and --separate-git-dir are incompatible."));
797 option_no_checkout
= 1;
801 option_origin
= "origin";
805 path
= get_repo_path(repo_name
, &is_bundle
);
807 repo
= xstrdup(absolute_path(repo_name
));
808 else if (!strchr(repo_name
, ':'))
809 die(_("repository '%s' does not exist"), repo_name
);
813 /* no need to be strict, transport_set_option() will validate it again */
814 if (option_depth
&& atoi(option_depth
) < 1)
815 die(_("depth %s is not a positive number"), option_depth
);
818 dir
= xstrdup(argv
[1]);
820 dir
= guess_dir_name(repo_name
, is_bundle
, option_bare
);
821 strip_trailing_slashes(dir
);
823 dest_exists
= !stat(dir
, &buf
);
824 if (dest_exists
&& !is_empty_dir(dir
))
825 die(_("destination path '%s' already exists and is not "
826 "an empty directory."), dir
);
828 strbuf_addf(&reflog_msg
, "clone: from %s", repo
);
833 work_tree
= getenv("GIT_WORK_TREE");
834 if (work_tree
&& !stat(work_tree
, &buf
))
835 die(_("working tree '%s' already exists."), work_tree
);
838 if (option_bare
|| work_tree
)
839 git_dir
= xstrdup(dir
);
842 git_dir
= mkpathdup("%s/.git", dir
);
846 sigchain_push_common(remove_junk_on_signal
);
849 if (safe_create_leading_directories_const(work_tree
) < 0)
850 die_errno(_("could not create leading directories of '%s'"),
852 if (!dest_exists
&& mkdir(work_tree
, 0777))
853 die_errno(_("could not create work tree dir '%s'"),
855 junk_work_tree
= work_tree
;
856 set_git_work_tree(work_tree
);
859 junk_git_dir
= git_dir
;
860 if (safe_create_leading_directories_const(git_dir
) < 0)
861 die(_("could not create leading directories of '%s'"), git_dir
);
863 set_git_dir_init(git_dir
, real_git_dir
, 0);
865 git_dir
= real_git_dir
;
866 junk_git_dir
= real_git_dir
;
869 if (0 <= option_verbosity
) {
871 fprintf(stderr
, _("Cloning into bare repository '%s'...\n"), dir
);
873 fprintf(stderr
, _("Cloning into '%s'...\n"), dir
);
875 init_db(option_template
, INIT_DB_QUIET
);
876 write_config(&option_config
);
878 git_config(git_default_config
, NULL
);
882 src_ref_prefix
= "refs/";
883 strbuf_addstr(&branch_top
, src_ref_prefix
);
885 git_config_set("core.bare", "true");
887 strbuf_addf(&branch_top
, "refs/remotes/%s/", option_origin
);
890 strbuf_addf(&value
, "+%s*:%s*", src_ref_prefix
, branch_top
.buf
);
891 strbuf_addf(&key
, "remote.%s.url", option_origin
);
892 git_config_set(key
.buf
, repo
);
895 if (option_reference
.nr
)
897 else if (option_dissociate
) {
898 warning(_("--dissociate given, but there is no --reference"));
899 option_dissociate
= 0;
902 fetch_pattern
= value
.buf
;
903 refspec
= parse_fetch_refspec(1, &fetch_pattern
);
905 strbuf_reset(&value
);
907 remote
= remote_get(option_origin
);
908 transport
= transport_get(remote
, remote
->url
[0]);
909 path
= get_repo_path(remote
->url
[0], &is_bundle
);
910 is_local
= option_local
!= 0 && path
&& !is_bundle
;
913 warning(_("--depth is ignored in local clones; use file:// instead."));
914 if (!access(mkpath("%s/shallow", path
), F_OK
)) {
915 if (option_local
> 0)
916 warning(_("source repository is shallow, ignoring --local"));
920 if (option_local
> 0 && !is_local
)
921 warning(_("--local is ignored"));
922 transport
->cloning
= 1;
924 if (!transport
->get_refs_list
|| (!is_local
&& !transport
->fetch
))
925 die(_("Don't know how to clone %s"), transport
->url
);
927 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
930 transport_set_option(transport
, TRANS_OPT_DEPTH
,
932 if (option_single_branch
)
933 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
935 transport_set_verbosity(transport
, option_verbosity
, option_progress
);
937 if (option_upload_pack
)
938 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
,
941 if (transport
->smart_options
&& !option_depth
)
942 transport
->smart_options
->check_self_contained_and_connected
= 1;
944 refs
= transport_get_remote_refs(transport
);
947 mapped_refs
= wanted_peer_refs(refs
, refspec
);
949 * transport_get_remote_refs() may return refs with null sha-1
950 * in mapped_refs (see struct transport->get_refs_list
951 * comment). In that case we need fetch it early because
952 * remote_head code below relies on it.
954 * for normal clones, transport_get_remote_refs() should
955 * return reliable ref set, we can delay cloning until after
958 for (ref
= refs
; ref
; ref
= ref
->next
)
959 if (is_null_sha1(ref
->old_sha1
)) {
960 complete_refs_before_fetch
= 0;
964 if (!is_local
&& !complete_refs_before_fetch
)
965 transport_fetch_refs(transport
, mapped_refs
);
967 remote_head
= find_ref_by_name(refs
, "HEAD");
968 remote_head_points_at
=
969 guess_remote_head(remote_head
, mapped_refs
, 0);
973 find_remote_branch(mapped_refs
, option_branch
);
975 if (!our_head_points_at
)
976 die(_("Remote branch %s not found in upstream %s"),
977 option_branch
, option_origin
);
980 our_head_points_at
= remote_head_points_at
;
984 die(_("Remote branch %s not found in upstream %s"),
985 option_branch
, option_origin
);
987 warning(_("You appear to have cloned an empty repository."));
989 our_head_points_at
= NULL
;
990 remote_head_points_at
= NULL
;
992 option_no_checkout
= 1;
994 install_branch_config(0, "master", option_origin
,
995 "refs/heads/master");
998 write_refspec_config(src_ref_prefix
, our_head_points_at
,
999 remote_head_points_at
, &branch_top
);
1002 clone_local(path
, git_dir
);
1003 else if (refs
&& complete_refs_before_fetch
)
1004 transport_fetch_refs(transport
, mapped_refs
);
1006 update_remote_refs(refs
, mapped_refs
, remote_head_points_at
,
1007 branch_top
.buf
, reflog_msg
.buf
, transport
, !is_local
);
1009 update_head(our_head_points_at
, remote_head
, reflog_msg
.buf
);
1011 transport_unlock_pack(transport
);
1012 transport_disconnect(transport
);
1014 if (option_dissociate
)
1015 dissociate_from_references();
1017 junk_mode
= JUNK_LEAVE_REPO
;
1020 strbuf_release(&reflog_msg
);
1021 strbuf_release(&branch_top
);
1022 strbuf_release(&key
);
1023 strbuf_release(&value
);
1024 junk_mode
= JUNK_LEAVE_ALL
;