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 * - respect DB_ENVIRONMENT for .git/objects.
27 * Implementation notes:
28 * - dropping use-separate-remote and no-separate-remote compatibility
31 static const char * const builtin_clone_usage
[] = {
32 "git clone [options] [--] <repo> [<dir>]",
36 static int option_quiet
, option_no_checkout
, option_bare
, option_mirror
;
37 static int option_local
, option_no_hardlinks
, option_shared
;
38 static char *option_template
, *option_reference
, *option_depth
;
39 static char *option_origin
= NULL
;
40 static char *option_upload_pack
= "git-upload-pack";
42 static struct option builtin_clone_options
[] = {
43 OPT__QUIET(&option_quiet
),
44 OPT_BOOLEAN('n', "no-checkout", &option_no_checkout
,
45 "don't create a checkout"),
46 OPT_BOOLEAN(0, "bare", &option_bare
, "create a bare repository"),
47 OPT_BOOLEAN(0, "naked", &option_bare
, "create a bare repository"),
48 OPT_BOOLEAN(0, "mirror", &option_mirror
,
49 "create a mirror repository (implies bare)"),
50 OPT_BOOLEAN('l', "local", &option_local
,
51 "to clone from a local repository"),
52 OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks
,
53 "don't use local hardlinks, always copy"),
54 OPT_BOOLEAN('s', "shared", &option_shared
,
55 "setup as shared repository"),
56 OPT_STRING(0, "template", &option_template
, "path",
57 "path the template repository"),
58 OPT_STRING(0, "reference", &option_reference
, "repo",
59 "reference repository"),
60 OPT_STRING('o', "origin", &option_origin
, "branch",
61 "use <branch> instead or 'origin' to track upstream"),
62 OPT_STRING('u', "upload-pack", &option_upload_pack
, "path",
63 "path to git-upload-pack on the remote"),
64 OPT_STRING(0, "depth", &option_depth
, "depth",
65 "create a shallow clone of that depth"),
70 static char *get_repo_path(const char *repo
, int *is_bundle
)
72 static char *suffix
[] = { "/.git", ".git", "" };
73 static char *bundle_suffix
[] = { ".bundle", "" };
77 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
79 path
= mkpath("%s%s", repo
, suffix
[i
]);
80 if (!stat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
82 return xstrdup(make_nonrelative_path(path
));
86 for (i
= 0; i
< ARRAY_SIZE(bundle_suffix
); i
++) {
88 path
= mkpath("%s%s", repo
, bundle_suffix
[i
]);
89 if (!stat(path
, &st
) && S_ISREG(st
.st_mode
)) {
91 return xstrdup(make_nonrelative_path(path
));
98 static char *guess_dir_name(const char *repo
, int is_bundle
)
100 const char *end
= repo
+ strlen(repo
), *start
;
103 * Strip trailing slashes and /.git
105 while (repo
< end
&& is_dir_sep(end
[-1]))
107 if (end
- repo
> 5 && is_dir_sep(end
[-5]) &&
108 !strncmp(end
- 4, ".git", 4)) {
110 while (repo
< end
&& is_dir_sep(end
[-1]))
115 * Find last component, but be prepared that repo could have
116 * the form "remote.example.com:foo.git", i.e. no slash
117 * in the directory part.
120 while (repo
< start
&& !is_dir_sep(start
[-1]) && start
[-1] != ':')
124 * Strip .{bundle,git}.
127 if (end
- start
> 7 && !strncmp(end
- 7, ".bundle", 7))
130 if (end
- start
> 4 && !strncmp(end
- 4, ".git", 4))
134 return xstrndup(start
, end
- start
);
137 static int is_directory(const char *path
)
141 return !stat(path
, &buf
) && S_ISDIR(buf
.st_mode
);
144 static void setup_reference(const char *repo
)
149 struct remote
*remote
;
150 struct transport
*transport
;
151 const struct ref
*extra
;
153 ref_git
= make_absolute_path(option_reference
);
155 if (is_directory(mkpath("%s/.git/objects", ref_git
)))
156 ref_git
= mkpath("%s/.git", ref_git
);
157 else if (!is_directory(mkpath("%s/objects", ref_git
)))
158 die("reference repository '%s' is not a local directory.",
161 ref_git_copy
= xstrdup(ref_git
);
163 add_to_alternates_file(ref_git_copy
);
165 remote
= remote_get(ref_git_copy
);
166 transport
= transport_get(remote
, ref_git_copy
);
167 for (extra
= transport_get_remote_refs(transport
); extra
;
169 add_extra_ref(extra
->name
, extra
->old_sha1
, 0);
171 transport_disconnect(transport
);
176 static void copy_or_link_directory(char *src
, char *dest
)
180 int src_len
, dest_len
;
185 die("failed to open %s\n", src
);
187 if (mkdir(dest
, 0777)) {
189 die("failed to create directory %s\n", dest
);
190 else if (stat(dest
, &buf
))
191 die("failed to stat %s\n", dest
);
192 else if (!S_ISDIR(buf
.st_mode
))
193 die("%s exists and is not a directory\n", dest
);
196 src_len
= strlen(src
);
198 dest_len
= strlen(dest
);
199 dest
[dest_len
] = '/';
201 while ((de
= readdir(dir
)) != NULL
) {
202 strcpy(src
+ src_len
+ 1, de
->d_name
);
203 strcpy(dest
+ dest_len
+ 1, de
->d_name
);
204 if (stat(src
, &buf
)) {
205 warning ("failed to stat %s\n", src
);
208 if (S_ISDIR(buf
.st_mode
)) {
209 if (de
->d_name
[0] != '.')
210 copy_or_link_directory(src
, dest
);
214 if (unlink(dest
) && errno
!= ENOENT
)
215 die("failed to unlink %s\n", dest
);
216 if (!option_no_hardlinks
) {
217 if (!link(src
, dest
))
220 die("failed to create link %s\n", dest
);
221 option_no_hardlinks
= 1;
223 if (copy_file(dest
, src
, 0666))
224 die("failed to copy file to %s\n", dest
);
229 static const struct ref
*clone_local(const char *src_repo
,
230 const char *dest_repo
)
232 const struct ref
*ret
;
235 struct remote
*remote
;
236 struct transport
*transport
;
239 add_to_alternates_file(src_repo
);
241 snprintf(src
, PATH_MAX
, "%s/objects", src_repo
);
242 snprintf(dest
, PATH_MAX
, "%s/objects", dest_repo
);
243 copy_or_link_directory(src
, dest
);
246 remote
= remote_get(src_repo
);
247 transport
= transport_get(remote
, src_repo
);
248 ret
= transport_get_remote_refs(transport
);
249 transport_disconnect(transport
);
253 static const char *junk_work_tree
;
254 static const char *junk_git_dir
;
257 static void remove_junk(void)
260 if (getpid() != junk_pid
)
264 strbuf_addstr(&sb
, junk_git_dir
);
265 remove_dir_recursively(&sb
, 0);
268 if (junk_work_tree
) {
269 strbuf_addstr(&sb
, junk_work_tree
);
270 remove_dir_recursively(&sb
, 0);
275 static void remove_junk_on_signal(int signo
)
278 signal(SIGINT
, SIG_DFL
);
282 static const struct ref
*locate_head(const struct ref
*refs
,
283 const struct ref
*mapped_refs
,
284 const struct ref
**remote_head_p
)
286 const struct ref
*remote_head
= NULL
;
287 const struct ref
*remote_master
= NULL
;
289 for (r
= refs
; r
; r
= r
->next
)
290 if (!strcmp(r
->name
, "HEAD"))
293 for (r
= mapped_refs
; r
; r
= r
->next
)
294 if (!strcmp(r
->name
, "refs/heads/master"))
298 *remote_head_p
= remote_head
;
300 /* If there's no HEAD value at all, never mind. */
304 /* If refs/heads/master could be right, it is. */
305 if (remote_master
&& !hashcmp(remote_master
->old_sha1
,
306 remote_head
->old_sha1
))
307 return remote_master
;
309 /* Look for another ref that points there */
310 for (r
= mapped_refs
; r
; r
= r
->next
)
311 if (r
!= remote_head
&&
312 !hashcmp(r
->old_sha1
, remote_head
->old_sha1
))
315 /* Nothing is the same */
319 static struct ref
*write_remote_refs(const struct ref
*refs
,
320 struct refspec
*refspec
, const char *reflog
)
322 struct ref
*local_refs
= NULL
;
323 struct ref
**tail
= &local_refs
;
326 get_fetch_map(refs
, refspec
, &tail
, 0);
327 get_fetch_map(refs
, tag_refspec
, &tail
, 0);
329 for (r
= local_refs
; r
; r
= r
->next
)
330 add_extra_ref(r
->peer_ref
->name
, r
->old_sha1
, 0);
332 pack_refs(PACK_REFS_ALL
);
338 int cmd_clone(int argc
, const char **argv
, const char *prefix
)
340 int use_local_hardlinks
= 1;
341 int use_separate_remote
= 1;
344 const char *repo_name
, *repo
, *work_tree
, *git_dir
;
346 const struct ref
*refs
, *head_points_at
, *remote_head
, *mapped_refs
;
347 char branch_top
[256], key
[256], value
[256];
348 struct strbuf reflog_msg
;
349 struct transport
*transport
= NULL
;
350 char *src_ref_prefix
= "refs/heads/";
352 struct refspec refspec
;
356 argc
= parse_options(argc
, argv
, builtin_clone_options
,
357 builtin_clone_usage
, 0);
360 die("You must specify a repository to clone.");
362 if (option_no_hardlinks
)
363 use_local_hardlinks
= 0;
370 die("--bare and --origin %s options are incompatible.",
372 option_no_checkout
= 1;
373 use_separate_remote
= 0;
377 option_origin
= "origin";
381 path
= get_repo_path(repo_name
, &is_bundle
);
384 else if (!strchr(repo_name
, ':'))
385 repo
= xstrdup(make_absolute_path(repo_name
));
390 dir
= xstrdup(argv
[1]);
392 dir
= guess_dir_name(repo_name
, is_bundle
);
394 if (!stat(dir
, &buf
))
395 die("destination directory '%s' already exists.", dir
);
397 strbuf_init(&reflog_msg
, 0);
398 strbuf_addf(&reflog_msg
, "clone: from %s", repo
);
403 work_tree
= getenv("GIT_WORK_TREE");
404 if (work_tree
&& !stat(work_tree
, &buf
))
405 die("working tree '%s' already exists.", work_tree
);
408 if (option_bare
|| work_tree
)
409 git_dir
= xstrdup(dir
);
412 git_dir
= xstrdup(mkpath("%s/.git", dir
));
416 junk_work_tree
= work_tree
;
417 if (safe_create_leading_directories_const(work_tree
) < 0)
418 die("could not create leading directories of '%s'",
420 if (mkdir(work_tree
, 0755))
421 die("could not create work tree dir '%s'.", work_tree
);
422 set_git_work_tree(work_tree
);
424 junk_git_dir
= git_dir
;
426 signal(SIGINT
, remove_junk_on_signal
);
428 setenv(CONFIG_ENVIRONMENT
, xstrdup(mkpath("%s/config", git_dir
)), 1);
430 if (safe_create_leading_directories_const(git_dir
) < 0)
431 die("could not create leading directories of '%s'", git_dir
);
432 set_git_dir(make_absolute_path(git_dir
));
434 init_db(option_template
, option_quiet
? INIT_DB_QUIET
: 0);
437 * At this point, the config exists, so we do not need the
438 * environment variable. We actually need to unset it, too, to
439 * re-enable parsing of the global configs.
441 unsetenv(CONFIG_ENVIRONMENT
);
443 if (option_reference
)
444 setup_reference(git_dir
);
446 git_config(git_default_config
, NULL
);
450 src_ref_prefix
= "refs/";
451 strcpy(branch_top
, src_ref_prefix
);
453 git_config_set("core.bare", "true");
455 snprintf(branch_top
, sizeof(branch_top
),
456 "refs/remotes/%s/", option_origin
);
459 if (option_mirror
|| !option_bare
) {
460 /* Configure the remote */
462 snprintf(key
, sizeof(key
),
463 "remote.%s.mirror", option_origin
);
464 git_config_set(key
, "true");
467 snprintf(key
, sizeof(key
), "remote.%s.url", option_origin
);
468 git_config_set(key
, repo
);
470 snprintf(key
, sizeof(key
), "remote.%s.fetch", option_origin
);
471 snprintf(value
, sizeof(value
),
472 "+%s*:%s*", src_ref_prefix
, branch_top
);
473 git_config_set_multivar(key
, value
, "^$", 0);
478 refspec
.src
= src_ref_prefix
;
479 refspec
.dst
= branch_top
;
481 if (path
&& !is_bundle
)
482 refs
= clone_local(path
, git_dir
);
484 struct remote
*remote
= remote_get(argv
[0]);
485 transport
= transport_get(remote
, remote
->url
[0]);
487 if (!transport
->get_refs_list
|| !transport
->fetch
)
488 die("Don't know how to clone %s", transport
->url
);
490 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
493 transport_set_option(transport
, TRANS_OPT_DEPTH
,
497 transport
->verbose
= -1;
499 if (option_upload_pack
)
500 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
,
503 refs
= transport_get_remote_refs(transport
);
504 transport_fetch_refs(transport
, refs
);
509 mapped_refs
= write_remote_refs(refs
, &refspec
, reflog_msg
.buf
);
511 head_points_at
= locate_head(refs
, mapped_refs
, &remote_head
);
513 if (head_points_at
) {
514 /* Local default branch link */
515 create_symref("HEAD", head_points_at
->name
, NULL
);
518 struct strbuf head_ref
;
519 const char *head
= head_points_at
->name
;
521 if (!prefixcmp(head
, "refs/heads/"))
524 /* Set up the initial local branch */
526 /* Local branch initial value */
527 update_ref(reflog_msg
.buf
, "HEAD",
528 head_points_at
->old_sha1
,
529 NULL
, 0, DIE_ON_ERR
);
531 strbuf_init(&head_ref
, 0);
532 strbuf_addstr(&head_ref
, branch_top
);
533 strbuf_addstr(&head_ref
, "HEAD");
535 /* Remote branch link */
536 create_symref(head_ref
.buf
,
537 head_points_at
->peer_ref
->name
,
540 snprintf(key
, sizeof(key
), "branch.%s.remote", head
);
541 git_config_set(key
, option_origin
);
542 snprintf(key
, sizeof(key
), "branch.%s.merge", head
);
543 git_config_set(key
, head_points_at
->name
);
545 } else if (remote_head
) {
546 /* Source had detached HEAD pointing somewhere. */
548 update_ref(reflog_msg
.buf
, "HEAD",
549 remote_head
->old_sha1
,
550 NULL
, REF_NODEREF
, DIE_ON_ERR
);
552 /* Nothing to checkout out */
553 if (!option_no_checkout
)
554 warning("remote HEAD refers to nonexistent ref, "
555 "unable to checkout.\n");
556 option_no_checkout
= 1;
560 transport_unlock_pack(transport
);
562 if (!option_no_checkout
) {
563 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
564 struct unpack_trees_options opts
;
569 /* We need to be in the new work tree for the checkout */
572 fd
= hold_locked_index(lock_file
, 1);
574 memset(&opts
, 0, sizeof opts
);
577 opts
.fn
= oneway_merge
;
578 opts
.verbose_update
= !option_quiet
;
579 opts
.src_index
= &the_index
;
580 opts
.dst_index
= &the_index
;
582 tree
= parse_tree_indirect(remote_head
->old_sha1
);
584 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
585 unpack_trees(1, &t
, &opts
);
587 if (write_cache(fd
, active_cache
, active_nr
) ||
588 commit_locked_index(lock_file
))
589 die("unable to write new index file");
592 strbuf_release(&reflog_msg
);