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
, int is_bare
)
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))
135 char *result
= xmalloc(end
- start
+ 5);
136 sprintf(result
, "%.*s.git", (int)(end
- start
), start
);
140 return xstrndup(start
, end
- start
);
143 static int is_directory(const char *path
)
147 return !stat(path
, &buf
) && S_ISDIR(buf
.st_mode
);
150 static void strip_trailing_slashes(char *dir
)
152 char *end
= dir
+ strlen(dir
);
154 while (dir
< end
- 1 && is_dir_sep(end
[-1]))
159 static void setup_reference(const char *repo
)
164 struct remote
*remote
;
165 struct transport
*transport
;
166 const struct ref
*extra
;
168 ref_git
= make_absolute_path(option_reference
);
170 if (is_directory(mkpath("%s/.git/objects", ref_git
)))
171 ref_git
= mkpath("%s/.git", ref_git
);
172 else if (!is_directory(mkpath("%s/objects", ref_git
)))
173 die("reference repository '%s' is not a local directory.",
176 ref_git_copy
= xstrdup(ref_git
);
178 add_to_alternates_file(ref_git_copy
);
180 remote
= remote_get(ref_git_copy
);
181 transport
= transport_get(remote
, ref_git_copy
);
182 for (extra
= transport_get_remote_refs(transport
); extra
;
184 add_extra_ref(extra
->name
, extra
->old_sha1
, 0);
186 transport_disconnect(transport
);
191 static void copy_or_link_directory(char *src
, char *dest
)
195 int src_len
, dest_len
;
200 die("failed to open %s\n", src
);
202 if (mkdir(dest
, 0777)) {
204 die("failed to create directory %s\n", dest
);
205 else if (stat(dest
, &buf
))
206 die("failed to stat %s\n", dest
);
207 else if (!S_ISDIR(buf
.st_mode
))
208 die("%s exists and is not a directory\n", dest
);
211 src_len
= strlen(src
);
213 dest_len
= strlen(dest
);
214 dest
[dest_len
] = '/';
216 while ((de
= readdir(dir
)) != NULL
) {
217 strcpy(src
+ src_len
+ 1, de
->d_name
);
218 strcpy(dest
+ dest_len
+ 1, de
->d_name
);
219 if (stat(src
, &buf
)) {
220 warning ("failed to stat %s\n", src
);
223 if (S_ISDIR(buf
.st_mode
)) {
224 if (de
->d_name
[0] != '.')
225 copy_or_link_directory(src
, dest
);
229 if (unlink(dest
) && errno
!= ENOENT
)
230 die("failed to unlink %s\n", dest
);
231 if (!option_no_hardlinks
) {
232 if (!link(src
, dest
))
235 die("failed to create link %s\n", dest
);
236 option_no_hardlinks
= 1;
238 if (copy_file(dest
, src
, 0666))
239 die("failed to copy file to %s\n", dest
);
244 static const struct ref
*clone_local(const char *src_repo
,
245 const char *dest_repo
)
247 const struct ref
*ret
;
250 struct remote
*remote
;
251 struct transport
*transport
;
254 add_to_alternates_file(src_repo
);
256 snprintf(src
, PATH_MAX
, "%s/objects", src_repo
);
257 snprintf(dest
, PATH_MAX
, "%s/objects", dest_repo
);
258 copy_or_link_directory(src
, dest
);
261 remote
= remote_get(src_repo
);
262 transport
= transport_get(remote
, src_repo
);
263 ret
= transport_get_remote_refs(transport
);
264 transport_disconnect(transport
);
268 static const char *junk_work_tree
;
269 static const char *junk_git_dir
;
272 static void remove_junk(void)
275 if (getpid() != junk_pid
)
279 strbuf_addstr(&sb
, junk_git_dir
);
280 remove_dir_recursively(&sb
, 0);
283 if (junk_work_tree
) {
284 strbuf_addstr(&sb
, junk_work_tree
);
285 remove_dir_recursively(&sb
, 0);
290 static void remove_junk_on_signal(int signo
)
293 signal(SIGINT
, SIG_DFL
);
297 static const struct ref
*locate_head(const struct ref
*refs
,
298 const struct ref
*mapped_refs
,
299 const struct ref
**remote_head_p
)
301 const struct ref
*remote_head
= NULL
;
302 const struct ref
*remote_master
= NULL
;
304 for (r
= refs
; r
; r
= r
->next
)
305 if (!strcmp(r
->name
, "HEAD"))
308 for (r
= mapped_refs
; r
; r
= r
->next
)
309 if (!strcmp(r
->name
, "refs/heads/master"))
313 *remote_head_p
= remote_head
;
315 /* If there's no HEAD value at all, never mind. */
319 /* If refs/heads/master could be right, it is. */
320 if (remote_master
&& !hashcmp(remote_master
->old_sha1
,
321 remote_head
->old_sha1
))
322 return remote_master
;
324 /* Look for another ref that points there */
325 for (r
= mapped_refs
; r
; r
= r
->next
)
326 if (r
!= remote_head
&&
327 !hashcmp(r
->old_sha1
, remote_head
->old_sha1
))
330 /* Nothing is the same */
334 static struct ref
*write_remote_refs(const struct ref
*refs
,
335 struct refspec
*refspec
, const char *reflog
)
337 struct ref
*local_refs
= NULL
;
338 struct ref
**tail
= &local_refs
;
341 get_fetch_map(refs
, refspec
, &tail
, 0);
343 get_fetch_map(refs
, tag_refspec
, &tail
, 0);
345 for (r
= local_refs
; r
; r
= r
->next
)
346 add_extra_ref(r
->peer_ref
->name
, r
->old_sha1
, 0);
348 pack_refs(PACK_REFS_ALL
);
354 int cmd_clone(int argc
, const char **argv
, const char *prefix
)
356 int use_local_hardlinks
= 1;
357 int use_separate_remote
= 1;
360 const char *repo_name
, *repo
, *work_tree
, *git_dir
;
362 const struct ref
*refs
, *head_points_at
, *remote_head
, *mapped_refs
;
363 char branch_top
[256], key
[256], value
[256];
364 struct strbuf reflog_msg
;
365 struct transport
*transport
= NULL
;
366 char *src_ref_prefix
= "refs/heads/";
368 struct refspec refspec
;
372 argc
= parse_options(argc
, argv
, builtin_clone_options
,
373 builtin_clone_usage
, 0);
376 die("You must specify a repository to clone.");
378 if (option_no_hardlinks
)
379 use_local_hardlinks
= 0;
386 die("--bare and --origin %s options are incompatible.",
388 option_no_checkout
= 1;
389 use_separate_remote
= 0;
393 option_origin
= "origin";
397 path
= get_repo_path(repo_name
, &is_bundle
);
399 repo
= xstrdup(make_nonrelative_path(repo_name
));
400 else if (!strchr(repo_name
, ':'))
401 repo
= xstrdup(make_absolute_path(repo_name
));
406 dir
= xstrdup(argv
[1]);
408 dir
= guess_dir_name(repo_name
, is_bundle
, option_bare
);
409 strip_trailing_slashes(dir
);
411 if (!stat(dir
, &buf
))
412 die("destination directory '%s' already exists.", dir
);
414 strbuf_init(&reflog_msg
, 0);
415 strbuf_addf(&reflog_msg
, "clone: from %s", repo
);
420 work_tree
= getenv("GIT_WORK_TREE");
421 if (work_tree
&& !stat(work_tree
, &buf
))
422 die("working tree '%s' already exists.", work_tree
);
425 if (option_bare
|| work_tree
)
426 git_dir
= xstrdup(dir
);
429 git_dir
= xstrdup(mkpath("%s/.git", dir
));
433 junk_work_tree
= work_tree
;
434 if (safe_create_leading_directories_const(work_tree
) < 0)
435 die("could not create leading directories of '%s': %s",
436 work_tree
, strerror(errno
));
437 if (mkdir(work_tree
, 0755))
438 die("could not create work tree dir '%s': %s.",
439 work_tree
, strerror(errno
));
440 set_git_work_tree(work_tree
);
442 junk_git_dir
= git_dir
;
444 signal(SIGINT
, remove_junk_on_signal
);
446 setenv(CONFIG_ENVIRONMENT
, xstrdup(mkpath("%s/config", git_dir
)), 1);
448 if (safe_create_leading_directories_const(git_dir
) < 0)
449 die("could not create leading directories of '%s'", git_dir
);
450 set_git_dir(make_absolute_path(git_dir
));
452 init_db(option_template
, option_quiet
? INIT_DB_QUIET
: 0);
455 * At this point, the config exists, so we do not need the
456 * environment variable. We actually need to unset it, too, to
457 * re-enable parsing of the global configs.
459 unsetenv(CONFIG_ENVIRONMENT
);
461 if (option_reference
)
462 setup_reference(git_dir
);
464 git_config(git_default_config
, NULL
);
468 src_ref_prefix
= "refs/";
469 strcpy(branch_top
, src_ref_prefix
);
471 git_config_set("core.bare", "true");
473 snprintf(branch_top
, sizeof(branch_top
),
474 "refs/remotes/%s/", option_origin
);
477 if (option_mirror
|| !option_bare
) {
478 /* Configure the remote */
480 snprintf(key
, sizeof(key
),
481 "remote.%s.mirror", option_origin
);
482 git_config_set(key
, "true");
485 snprintf(key
, sizeof(key
), "remote.%s.url", option_origin
);
486 git_config_set(key
, repo
);
488 snprintf(key
, sizeof(key
), "remote.%s.fetch", option_origin
);
489 snprintf(value
, sizeof(value
),
490 "+%s*:%s*", src_ref_prefix
, branch_top
);
491 git_config_set_multivar(key
, value
, "^$", 0);
496 refspec
.src
= src_ref_prefix
;
497 refspec
.dst
= branch_top
;
499 if (path
&& !is_bundle
)
500 refs
= clone_local(path
, git_dir
);
502 struct remote
*remote
= remote_get(argv
[0]);
503 transport
= transport_get(remote
, remote
->url
[0]);
505 if (!transport
->get_refs_list
|| !transport
->fetch
)
506 die("Don't know how to clone %s", transport
->url
);
508 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
511 transport_set_option(transport
, TRANS_OPT_DEPTH
,
515 transport
->verbose
= -1;
517 if (option_upload_pack
)
518 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
,
521 refs
= transport_get_remote_refs(transport
);
522 transport_fetch_refs(transport
, refs
);
527 mapped_refs
= write_remote_refs(refs
, &refspec
, reflog_msg
.buf
);
529 head_points_at
= locate_head(refs
, mapped_refs
, &remote_head
);
531 if (head_points_at
) {
532 /* Local default branch link */
533 create_symref("HEAD", head_points_at
->name
, NULL
);
536 struct strbuf head_ref
;
537 const char *head
= head_points_at
->name
;
539 if (!prefixcmp(head
, "refs/heads/"))
542 /* Set up the initial local branch */
544 /* Local branch initial value */
545 update_ref(reflog_msg
.buf
, "HEAD",
546 head_points_at
->old_sha1
,
547 NULL
, 0, DIE_ON_ERR
);
549 strbuf_init(&head_ref
, 0);
550 strbuf_addstr(&head_ref
, branch_top
);
551 strbuf_addstr(&head_ref
, "HEAD");
553 /* Remote branch link */
554 create_symref(head_ref
.buf
,
555 head_points_at
->peer_ref
->name
,
558 snprintf(key
, sizeof(key
), "branch.%s.remote", head
);
559 git_config_set(key
, option_origin
);
560 snprintf(key
, sizeof(key
), "branch.%s.merge", head
);
561 git_config_set(key
, head_points_at
->name
);
563 } else if (remote_head
) {
564 /* Source had detached HEAD pointing somewhere. */
566 update_ref(reflog_msg
.buf
, "HEAD",
567 remote_head
->old_sha1
,
568 NULL
, REF_NODEREF
, DIE_ON_ERR
);
570 /* Nothing to checkout out */
571 if (!option_no_checkout
)
572 warning("remote HEAD refers to nonexistent ref, "
573 "unable to checkout.\n");
574 option_no_checkout
= 1;
578 transport_unlock_pack(transport
);
580 if (!option_no_checkout
) {
581 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
582 struct unpack_trees_options opts
;
587 /* We need to be in the new work tree for the checkout */
590 fd
= hold_locked_index(lock_file
, 1);
592 memset(&opts
, 0, sizeof opts
);
595 opts
.fn
= oneway_merge
;
596 opts
.verbose_update
= !option_quiet
;
597 opts
.src_index
= &the_index
;
598 opts
.dst_index
= &the_index
;
600 tree
= parse_tree_indirect(remote_head
->old_sha1
);
602 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
603 unpack_trees(1, &t
, &opts
);
605 if (write_cache(fd
, active_cache
, active_nr
) ||
606 commit_locked_index(lock_file
))
607 die("unable to write new index file");
610 strbuf_release(&reflog_msg
);