blame: use xdi_diff_hunks(), get rid of struct patch
[git/kirr.git] / builtin-clone.c
blob1ddc14b9c70b3318ab5e1cad779c8af029e56ce3
1 /*
2 * Builtin "git clone"
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.
9 */
11 #include "cache.h"
12 #include "parse-options.h"
13 #include "fetch-pack.h"
14 #include "refs.h"
15 #include "tree.h"
16 #include "tree-walk.h"
17 #include "unpack-trees.h"
18 #include "transport.h"
19 #include "strbuf.h"
20 #include "dir.h"
21 #include "pack-refs.h"
24 * Overall FIXMEs:
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>]",
33 NULL
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 of '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"),
67 OPT_END()
70 static char *get_repo_path(const char *repo, int *is_bundle)
72 static char *suffix[] = { "/.git", ".git", "" };
73 static char *bundle_suffix[] = { ".bundle", "" };
74 struct stat st;
75 int i;
77 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
78 const char *path;
79 path = mkpath("%s%s", repo, suffix[i]);
80 if (is_directory(path)) {
81 *is_bundle = 0;
82 return xstrdup(make_nonrelative_path(path));
86 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
87 const char *path;
88 path = mkpath("%s%s", repo, bundle_suffix[i]);
89 if (!stat(path, &st) && S_ISREG(st.st_mode)) {
90 *is_bundle = 1;
91 return xstrdup(make_nonrelative_path(path));
95 return NULL;
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]))
106 end--;
107 if (end - repo > 5 && is_dir_sep(end[-5]) &&
108 !strncmp(end - 4, ".git", 4)) {
109 end -= 5;
110 while (repo < end && is_dir_sep(end[-1]))
111 end--;
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.
119 start = end;
120 while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
121 start--;
124 * Strip .{bundle,git}.
126 if (is_bundle) {
127 if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
128 end -= 7;
129 } else {
130 if (end - start > 4 && !strncmp(end - 4, ".git", 4))
131 end -= 4;
134 if (is_bare) {
135 char *result = xmalloc(end - start + 5);
136 sprintf(result, "%.*s.git", (int)(end - start), start);
137 return result;
140 return xstrndup(start, end - start);
143 static void strip_trailing_slashes(char *dir)
145 char *end = dir + strlen(dir);
147 while (dir < end - 1 && is_dir_sep(end[-1]))
148 end--;
149 *end = '\0';
152 static void setup_reference(const char *repo)
154 const char *ref_git;
155 char *ref_git_copy;
157 struct remote *remote;
158 struct transport *transport;
159 const struct ref *extra;
161 ref_git = make_absolute_path(option_reference);
163 if (is_directory(mkpath("%s/.git/objects", ref_git)))
164 ref_git = mkpath("%s/.git", ref_git);
165 else if (!is_directory(mkpath("%s/objects", ref_git)))
166 die("reference repository '%s' is not a local directory.",
167 option_reference);
169 ref_git_copy = xstrdup(ref_git);
171 add_to_alternates_file(ref_git_copy);
173 remote = remote_get(ref_git_copy);
174 transport = transport_get(remote, ref_git_copy);
175 for (extra = transport_get_remote_refs(transport); extra;
176 extra = extra->next)
177 add_extra_ref(extra->name, extra->old_sha1, 0);
179 transport_disconnect(transport);
181 free(ref_git_copy);
184 static void copy_or_link_directory(char *src, char *dest)
186 struct dirent *de;
187 struct stat buf;
188 int src_len, dest_len;
189 DIR *dir;
191 dir = opendir(src);
192 if (!dir)
193 die("failed to open %s\n", src);
195 if (mkdir(dest, 0777)) {
196 if (errno != EEXIST)
197 die("failed to create directory %s\n", dest);
198 else if (stat(dest, &buf))
199 die("failed to stat %s\n", dest);
200 else if (!S_ISDIR(buf.st_mode))
201 die("%s exists and is not a directory\n", dest);
204 src_len = strlen(src);
205 src[src_len] = '/';
206 dest_len = strlen(dest);
207 dest[dest_len] = '/';
209 while ((de = readdir(dir)) != NULL) {
210 strcpy(src + src_len + 1, de->d_name);
211 strcpy(dest + dest_len + 1, de->d_name);
212 if (stat(src, &buf)) {
213 warning ("failed to stat %s\n", src);
214 continue;
216 if (S_ISDIR(buf.st_mode)) {
217 if (de->d_name[0] != '.')
218 copy_or_link_directory(src, dest);
219 continue;
222 if (unlink(dest) && errno != ENOENT)
223 die("failed to unlink %s\n", dest);
224 if (!option_no_hardlinks) {
225 if (!link(src, dest))
226 continue;
227 if (option_local)
228 die("failed to create link %s\n", dest);
229 option_no_hardlinks = 1;
231 if (copy_file(dest, src, 0666))
232 die("failed to copy file to %s\n", dest);
234 closedir(dir);
237 static const struct ref *clone_local(const char *src_repo,
238 const char *dest_repo)
240 const struct ref *ret;
241 char src[PATH_MAX];
242 char dest[PATH_MAX];
243 struct remote *remote;
244 struct transport *transport;
246 if (option_shared)
247 add_to_alternates_file(src_repo);
248 else {
249 snprintf(src, PATH_MAX, "%s/objects", src_repo);
250 snprintf(dest, PATH_MAX, "%s/objects", dest_repo);
251 copy_or_link_directory(src, dest);
254 remote = remote_get(src_repo);
255 transport = transport_get(remote, src_repo);
256 ret = transport_get_remote_refs(transport);
257 transport_disconnect(transport);
258 return ret;
261 static const char *junk_work_tree;
262 static const char *junk_git_dir;
263 pid_t junk_pid;
265 static void remove_junk(void)
267 struct strbuf sb = STRBUF_INIT;
268 if (getpid() != junk_pid)
269 return;
270 if (junk_git_dir) {
271 strbuf_addstr(&sb, junk_git_dir);
272 remove_dir_recursively(&sb, 0);
273 strbuf_reset(&sb);
275 if (junk_work_tree) {
276 strbuf_addstr(&sb, junk_work_tree);
277 remove_dir_recursively(&sb, 0);
278 strbuf_reset(&sb);
282 static void remove_junk_on_signal(int signo)
284 remove_junk();
285 signal(SIGINT, SIG_DFL);
286 raise(signo);
289 static const struct ref *locate_head(const struct ref *refs,
290 const struct ref *mapped_refs,
291 const struct ref **remote_head_p)
293 const struct ref *remote_head = NULL;
294 const struct ref *remote_master = NULL;
295 const struct ref *r;
296 for (r = refs; r; r = r->next)
297 if (!strcmp(r->name, "HEAD"))
298 remote_head = r;
300 for (r = mapped_refs; r; r = r->next)
301 if (!strcmp(r->name, "refs/heads/master"))
302 remote_master = r;
304 if (remote_head_p)
305 *remote_head_p = remote_head;
307 /* If there's no HEAD value at all, never mind. */
308 if (!remote_head)
309 return NULL;
311 /* If refs/heads/master could be right, it is. */
312 if (remote_master && !hashcmp(remote_master->old_sha1,
313 remote_head->old_sha1))
314 return remote_master;
316 /* Look for another ref that points there */
317 for (r = mapped_refs; r; r = r->next)
318 if (r != remote_head &&
319 !hashcmp(r->old_sha1, remote_head->old_sha1))
320 return r;
322 /* Nothing is the same */
323 return NULL;
326 static struct ref *write_remote_refs(const struct ref *refs,
327 struct refspec *refspec, const char *reflog)
329 struct ref *local_refs = NULL;
330 struct ref **tail = &local_refs;
331 struct ref *r;
333 get_fetch_map(refs, refspec, &tail, 0);
334 if (!option_mirror)
335 get_fetch_map(refs, tag_refspec, &tail, 0);
337 for (r = local_refs; r; r = r->next)
338 add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
340 pack_refs(PACK_REFS_ALL);
341 clear_extra_refs();
343 return local_refs;
346 int cmd_clone(int argc, const char **argv, const char *prefix)
348 int use_local_hardlinks = 1;
349 int use_separate_remote = 1;
350 int is_bundle = 0;
351 struct stat buf;
352 const char *repo_name, *repo, *work_tree, *git_dir;
353 char *path, *dir;
354 const struct ref *refs, *head_points_at, *remote_head, *mapped_refs;
355 char branch_top[256], key[256], value[256];
356 struct strbuf reflog_msg = STRBUF_INIT;
357 struct transport *transport = NULL;
358 char *src_ref_prefix = "refs/heads/";
360 struct refspec refspec;
362 junk_pid = getpid();
364 argc = parse_options(argc, argv, builtin_clone_options,
365 builtin_clone_usage, 0);
367 if (argc == 0)
368 die("You must specify a repository to clone.");
370 if (option_no_hardlinks)
371 use_local_hardlinks = 0;
373 if (option_mirror)
374 option_bare = 1;
376 if (option_bare) {
377 if (option_origin)
378 die("--bare and --origin %s options are incompatible.",
379 option_origin);
380 option_no_checkout = 1;
381 use_separate_remote = 0;
384 if (!option_origin)
385 option_origin = "origin";
387 repo_name = argv[0];
389 path = get_repo_path(repo_name, &is_bundle);
390 if (path)
391 repo = xstrdup(make_nonrelative_path(repo_name));
392 else if (!strchr(repo_name, ':'))
393 repo = xstrdup(make_absolute_path(repo_name));
394 else
395 repo = repo_name;
397 if (argc == 2)
398 dir = xstrdup(argv[1]);
399 else
400 dir = guess_dir_name(repo_name, is_bundle, option_bare);
401 strip_trailing_slashes(dir);
403 if (!stat(dir, &buf))
404 die("destination directory '%s' already exists.", dir);
406 strbuf_addf(&reflog_msg, "clone: from %s", repo);
408 if (option_bare)
409 work_tree = NULL;
410 else {
411 work_tree = getenv("GIT_WORK_TREE");
412 if (work_tree && !stat(work_tree, &buf))
413 die("working tree '%s' already exists.", work_tree);
416 if (option_bare || work_tree)
417 git_dir = xstrdup(dir);
418 else {
419 work_tree = dir;
420 git_dir = xstrdup(mkpath("%s/.git", dir));
423 if (!option_bare) {
424 junk_work_tree = work_tree;
425 if (safe_create_leading_directories_const(work_tree) < 0)
426 die("could not create leading directories of '%s': %s",
427 work_tree, strerror(errno));
428 if (mkdir(work_tree, 0755))
429 die("could not create work tree dir '%s': %s.",
430 work_tree, strerror(errno));
431 set_git_work_tree(work_tree);
433 junk_git_dir = git_dir;
434 atexit(remove_junk);
435 signal(SIGINT, remove_junk_on_signal);
437 setenv(CONFIG_ENVIRONMENT, xstrdup(mkpath("%s/config", git_dir)), 1);
439 if (safe_create_leading_directories_const(git_dir) < 0)
440 die("could not create leading directories of '%s'", git_dir);
441 set_git_dir(make_absolute_path(git_dir));
443 init_db(option_template, option_quiet ? INIT_DB_QUIET : 0);
446 * At this point, the config exists, so we do not need the
447 * environment variable. We actually need to unset it, too, to
448 * re-enable parsing of the global configs.
450 unsetenv(CONFIG_ENVIRONMENT);
452 if (option_reference)
453 setup_reference(git_dir);
455 git_config(git_default_config, NULL);
457 if (option_bare) {
458 if (option_mirror)
459 src_ref_prefix = "refs/";
460 strcpy(branch_top, src_ref_prefix);
462 git_config_set("core.bare", "true");
463 } else {
464 snprintf(branch_top, sizeof(branch_top),
465 "refs/remotes/%s/", option_origin);
468 if (option_mirror || !option_bare) {
469 /* Configure the remote */
470 if (option_mirror) {
471 snprintf(key, sizeof(key),
472 "remote.%s.mirror", option_origin);
473 git_config_set(key, "true");
476 snprintf(key, sizeof(key), "remote.%s.url", option_origin);
477 git_config_set(key, repo);
479 snprintf(key, sizeof(key), "remote.%s.fetch", option_origin);
480 snprintf(value, sizeof(value),
481 "+%s*:%s*", src_ref_prefix, branch_top);
482 git_config_set_multivar(key, value, "^$", 0);
485 refspec.force = 0;
486 refspec.pattern = 1;
487 refspec.src = src_ref_prefix;
488 refspec.dst = branch_top;
490 if (path && !is_bundle)
491 refs = clone_local(path, git_dir);
492 else {
493 struct remote *remote = remote_get(argv[0]);
494 transport = transport_get(remote, remote->url[0]);
496 if (!transport->get_refs_list || !transport->fetch)
497 die("Don't know how to clone %s", transport->url);
499 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
501 if (option_depth)
502 transport_set_option(transport, TRANS_OPT_DEPTH,
503 option_depth);
505 if (option_quiet)
506 transport->verbose = -1;
508 if (option_upload_pack)
509 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
510 option_upload_pack);
512 refs = transport_get_remote_refs(transport);
513 transport_fetch_refs(transport, refs);
516 clear_extra_refs();
518 mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
520 head_points_at = locate_head(refs, mapped_refs, &remote_head);
522 if (head_points_at) {
523 /* Local default branch link */
524 create_symref("HEAD", head_points_at->name, NULL);
526 if (!option_bare) {
527 struct strbuf head_ref = STRBUF_INIT;
528 const char *head = head_points_at->name;
530 if (!prefixcmp(head, "refs/heads/"))
531 head += 11;
533 /* Set up the initial local branch */
535 /* Local branch initial value */
536 update_ref(reflog_msg.buf, "HEAD",
537 head_points_at->old_sha1,
538 NULL, 0, DIE_ON_ERR);
540 strbuf_addstr(&head_ref, branch_top);
541 strbuf_addstr(&head_ref, "HEAD");
543 /* Remote branch link */
544 create_symref(head_ref.buf,
545 head_points_at->peer_ref->name,
546 reflog_msg.buf);
548 snprintf(key, sizeof(key), "branch.%s.remote", head);
549 git_config_set(key, option_origin);
550 snprintf(key, sizeof(key), "branch.%s.merge", head);
551 git_config_set(key, head_points_at->name);
553 } else if (remote_head) {
554 /* Source had detached HEAD pointing somewhere. */
555 if (!option_bare)
556 update_ref(reflog_msg.buf, "HEAD",
557 remote_head->old_sha1,
558 NULL, REF_NODEREF, DIE_ON_ERR);
559 } else {
560 /* Nothing to checkout out */
561 if (!option_no_checkout)
562 warning("remote HEAD refers to nonexistent ref, "
563 "unable to checkout.\n");
564 option_no_checkout = 1;
567 if (transport)
568 transport_unlock_pack(transport);
570 if (!option_no_checkout) {
571 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
572 struct unpack_trees_options opts;
573 struct tree *tree;
574 struct tree_desc t;
575 int fd;
577 /* We need to be in the new work tree for the checkout */
578 setup_work_tree();
580 fd = hold_locked_index(lock_file, 1);
582 memset(&opts, 0, sizeof opts);
583 opts.update = 1;
584 opts.merge = 1;
585 opts.fn = oneway_merge;
586 opts.verbose_update = !option_quiet;
587 opts.src_index = &the_index;
588 opts.dst_index = &the_index;
590 tree = parse_tree_indirect(remote_head->old_sha1);
591 parse_tree(tree);
592 init_tree_desc(&t, tree->buffer, tree->size);
593 unpack_trees(1, &t, &opts);
595 if (write_cache(fd, active_cache, active_nr) ||
596 commit_locked_index(lock_file))
597 die("unable to write new index file");
600 strbuf_release(&reflog_msg);
601 junk_pid = 0;
602 return 0;