Merge branch 'jc/finalize-temp-file'
[git] / builtin / clone.c
1 /*
2  * Builtin "git clone"
3  *
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
7  *
8  * Clone a repository into a different directory that does not yet exist.
9  */
10
11 #include "builtin.h"
12 #include "lockfile.h"
13 #include "parse-options.h"
14 #include "fetch-pack.h"
15 #include "refs.h"
16 #include "tree.h"
17 #include "tree-walk.h"
18 #include "unpack-trees.h"
19 #include "transport.h"
20 #include "strbuf.h"
21 #include "dir.h"
22 #include "sigchain.h"
23 #include "branch.h"
24 #include "remote.h"
25 #include "run-command.h"
26 #include "connected.h"
27
28 /*
29  * Overall FIXMEs:
30  *  - respect DB_ENVIRONMENT for .git/objects.
31  *
32  * Implementation notes:
33  *  - dropping use-separate-remote and no-separate-remote compatibility
34  *
35  */
36 static const char * const builtin_clone_usage[] = {
37         N_("git clone [<options>] [--] <repo> [<dir>]"),
38         NULL
39 };
40
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;
53
54 static struct option builtin_clone_options[] = {
55         OPT__VERBOSITY(&option_verbosity),
56         OPT_BOOL(0, "progress", &option_progress,
57                  N_("force progress reporting")),
58         OPT_BOOL('n', "no-checkout", &option_no_checkout,
59                  N_("don't create a checkout")),
60         OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
61         OPT_HIDDEN_BOOL(0, "naked", &option_bare,
62                         N_("create a bare repository")),
63         OPT_BOOL(0, "mirror", &option_mirror,
64                  N_("create a mirror repository (implies bare)")),
65         OPT_BOOL('l', "local", &option_local,
66                 N_("to clone from a local repository")),
67         OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
68                     N_("don't use local hardlinks, always copy")),
69         OPT_BOOL('s', "shared", &option_shared,
70                     N_("setup as shared repository")),
71         OPT_BOOL(0, "recursive", &option_recursive,
72                     N_("initialize submodules in the clone")),
73         OPT_BOOL(0, "recurse-submodules", &option_recursive,
74                     N_("initialize submodules in the clone")),
75         OPT_STRING(0, "template", &option_template, N_("template-directory"),
76                    N_("directory from which templates will be used")),
77         OPT_STRING_LIST(0, "reference", &option_reference, N_("repo"),
78                         N_("reference repository")),
79         OPT_BOOL(0, "dissociate", &option_dissociate,
80                  N_("use --reference only while cloning")),
81         OPT_STRING('o', "origin", &option_origin, N_("name"),
82                    N_("use <name> instead of 'origin' to track upstream")),
83         OPT_STRING('b', "branch", &option_branch, N_("branch"),
84                    N_("checkout <branch> instead of the remote's HEAD")),
85         OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
86                    N_("path to git-upload-pack on the remote")),
87         OPT_STRING(0, "depth", &option_depth, N_("depth"),
88                     N_("create a shallow clone of that depth")),
89         OPT_BOOL(0, "single-branch", &option_single_branch,
90                     N_("clone only one branch, HEAD or --branch")),
91         OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
92                    N_("separate git dir from working tree")),
93         OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
94                         N_("set config inside the new repository")),
95         OPT_END()
96 };
97
98 static const char *argv_submodule[] = {
99         "submodule", "update", "--init", "--recursive", NULL
100 };
101
102 static char *get_repo_path(const char *repo, int *is_bundle)
103 {
104         static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
105         static char *bundle_suffix[] = { ".bundle", "" };
106         struct stat st;
107         int i;
108
109         for (i = 0; i < ARRAY_SIZE(suffix); i++) {
110                 const char *path;
111                 path = mkpath("%s%s", repo, suffix[i]);
112                 if (stat(path, &st))
113                         continue;
114                 if (S_ISDIR(st.st_mode) && is_git_directory(path)) {
115                         *is_bundle = 0;
116                         return xstrdup(absolute_path(path));
117                 } else if (S_ISREG(st.st_mode) && st.st_size > 8) {
118                         /* Is it a "gitfile"? */
119                         char signature[8];
120                         int len, fd = open(path, O_RDONLY);
121                         if (fd < 0)
122                                 continue;
123                         len = read_in_full(fd, signature, 8);
124                         close(fd);
125                         if (len != 8 || strncmp(signature, "gitdir: ", 8))
126                                 continue;
127                         path = read_gitfile(path);
128                         if (path) {
129                                 *is_bundle = 0;
130                                 return xstrdup(absolute_path(path));
131                         }
132                 }
133         }
134
135         for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
136                 const char *path;
137                 path = mkpath("%s%s", repo, bundle_suffix[i]);
138                 if (!stat(path, &st) && S_ISREG(st.st_mode)) {
139                         *is_bundle = 1;
140                         return xstrdup(absolute_path(path));
141                 }
142         }
143
144         return NULL;
145 }
146
147 static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
148 {
149         const char *end = repo + strlen(repo), *start, *ptr;
150         size_t len;
151         char *dir;
152
153         /*
154          * Skip scheme.
155          */
156         start = strstr(repo, "://");
157         if (start == NULL)
158                 start = repo;
159         else
160                 start += 3;
161
162         /*
163          * Skip authentication data. The stripping does happen
164          * greedily, such that we strip up to the last '@' inside
165          * the host part.
166          */
167         for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) {
168                 if (*ptr == '@')
169                         start = ptr + 1;
170         }
171
172         /*
173          * Strip trailing spaces, slashes and /.git
174          */
175         while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
176                 end--;
177         if (end - start > 5 && is_dir_sep(end[-5]) &&
178             !strncmp(end - 4, ".git", 4)) {
179                 end -= 5;
180                 while (start < end && is_dir_sep(end[-1]))
181                         end--;
182         }
183
184         /*
185          * Strip trailing port number if we've got only a
186          * hostname (that is, there is no dir separator but a
187          * colon). This check is required such that we do not
188          * strip URI's like '/foo/bar:2222.git', which should
189          * result in a dir '2222' being guessed due to backwards
190          * compatibility.
191          */
192         if (memchr(start, '/', end - start) == NULL
193             && memchr(start, ':', end - start) != NULL) {
194                 ptr = end;
195                 while (start < ptr && isdigit(ptr[-1]) && ptr[-1] != ':')
196                         ptr--;
197                 if (start < ptr && ptr[-1] == ':')
198                         end = ptr - 1;
199         }
200
201         /*
202          * Find last component. To remain backwards compatible we
203          * also regard colons as path separators, such that
204          * cloning a repository 'foo:bar.git' would result in a
205          * directory 'bar' being guessed.
206          */
207         ptr = end;
208         while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':')
209                 ptr--;
210         start = ptr;
211
212         /*
213          * Strip .{bundle,git}.
214          */
215         len = end - start;
216         strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git");
217
218         if (!len || (len == 1 && *start == '/'))
219             die("No directory name could be guessed.\n"
220                 "Please specify a directory on the command line");
221
222         if (is_bare)
223                 dir = xstrfmt("%.*s.git", (int)len, start);
224         else
225                 dir = xstrndup(start, len);
226         /*
227          * Replace sequences of 'control' characters and whitespace
228          * with one ascii space, remove leading and trailing spaces.
229          */
230         if (*dir) {
231                 char *out = dir;
232                 int prev_space = 1 /* strip leading whitespace */;
233                 for (end = dir; *end; ++end) {
234                         char ch = *end;
235                         if ((unsigned char)ch < '\x20')
236                                 ch = '\x20';
237                         if (isspace(ch)) {
238                                 if (prev_space)
239                                         continue;
240                                 prev_space = 1;
241                         } else
242                                 prev_space = 0;
243                         *out++ = ch;
244                 }
245                 *out = '\0';
246                 if (out > dir && prev_space)
247                         out[-1] = '\0';
248         }
249         return dir;
250 }
251
252 static void strip_trailing_slashes(char *dir)
253 {
254         char *end = dir + strlen(dir);
255
256         while (dir < end - 1 && is_dir_sep(end[-1]))
257                 end--;
258         *end = '\0';
259 }
260
261 static int add_one_reference(struct string_list_item *item, void *cb_data)
262 {
263         char *ref_git;
264         const char *repo;
265         struct strbuf alternate = STRBUF_INIT;
266
267         /* Beware: read_gitfile(), real_path() and mkpath() return static buffer */
268         ref_git = xstrdup(real_path(item->string));
269
270         repo = read_gitfile(ref_git);
271         if (!repo)
272                 repo = read_gitfile(mkpath("%s/.git", ref_git));
273         if (repo) {
274                 free(ref_git);
275                 ref_git = xstrdup(repo);
276         }
277
278         if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
279                 char *ref_git_git = mkpathdup("%s/.git", ref_git);
280                 free(ref_git);
281                 ref_git = ref_git_git;
282         } else if (!is_directory(mkpath("%s/objects", ref_git)))
283                 die(_("reference repository '%s' is not a local repository."),
284                     item->string);
285
286         if (!access(mkpath("%s/shallow", ref_git), F_OK))
287                 die(_("reference repository '%s' is shallow"), item->string);
288
289         if (!access(mkpath("%s/info/grafts", ref_git), F_OK))
290                 die(_("reference repository '%s' is grafted"), item->string);
291
292         strbuf_addf(&alternate, "%s/objects", ref_git);
293         add_to_alternates_file(alternate.buf);
294         strbuf_release(&alternate);
295         free(ref_git);
296         return 0;
297 }
298
299 static void setup_reference(void)
300 {
301         for_each_string_list(&option_reference, add_one_reference, NULL);
302 }
303
304 static void copy_alternates(struct strbuf *src, struct strbuf *dst,
305                             const char *src_repo)
306 {
307         /*
308          * Read from the source objects/info/alternates file
309          * and copy the entries to corresponding file in the
310          * destination repository with add_to_alternates_file().
311          * Both src and dst have "$path/objects/info/alternates".
312          *
313          * Instead of copying bit-for-bit from the original,
314          * we need to append to existing one so that the already
315          * created entry via "clone -s" is not lost, and also
316          * to turn entries with paths relative to the original
317          * absolute, so that they can be used in the new repository.
318          */
319         FILE *in = fopen(src->buf, "r");
320         struct strbuf line = STRBUF_INIT;
321
322         while (strbuf_getline(&line, in, '\n') != EOF) {
323                 char *abs_path;
324                 if (!line.len || line.buf[0] == '#')
325                         continue;
326                 if (is_absolute_path(line.buf)) {
327                         add_to_alternates_file(line.buf);
328                         continue;
329                 }
330                 abs_path = mkpathdup("%s/objects/%s", src_repo, line.buf);
331                 normalize_path_copy(abs_path, abs_path);
332                 add_to_alternates_file(abs_path);
333                 free(abs_path);
334         }
335         strbuf_release(&line);
336         fclose(in);
337 }
338
339 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
340                                    const char *src_repo, int src_baselen)
341 {
342         struct dirent *de;
343         struct stat buf;
344         int src_len, dest_len;
345         DIR *dir;
346
347         dir = opendir(src->buf);
348         if (!dir)
349                 die_errno(_("failed to open '%s'"), src->buf);
350
351         if (mkdir(dest->buf, 0777)) {
352                 if (errno != EEXIST)
353                         die_errno(_("failed to create directory '%s'"), dest->buf);
354                 else if (stat(dest->buf, &buf))
355                         die_errno(_("failed to stat '%s'"), dest->buf);
356                 else if (!S_ISDIR(buf.st_mode))
357                         die(_("%s exists and is not a directory"), dest->buf);
358         }
359
360         strbuf_addch(src, '/');
361         src_len = src->len;
362         strbuf_addch(dest, '/');
363         dest_len = dest->len;
364
365         while ((de = readdir(dir)) != NULL) {
366                 strbuf_setlen(src, src_len);
367                 strbuf_addstr(src, de->d_name);
368                 strbuf_setlen(dest, dest_len);
369                 strbuf_addstr(dest, de->d_name);
370                 if (stat(src->buf, &buf)) {
371                         warning (_("failed to stat %s\n"), src->buf);
372                         continue;
373                 }
374                 if (S_ISDIR(buf.st_mode)) {
375                         if (de->d_name[0] != '.')
376                                 copy_or_link_directory(src, dest,
377                                                        src_repo, src_baselen);
378                         continue;
379                 }
380
381                 /* Files that cannot be copied bit-for-bit... */
382                 if (!strcmp(src->buf + src_baselen, "/info/alternates")) {
383                         copy_alternates(src, dest, src_repo);
384                         continue;
385                 }
386
387                 if (unlink(dest->buf) && errno != ENOENT)
388                         die_errno(_("failed to unlink '%s'"), dest->buf);
389                 if (!option_no_hardlinks) {
390                         if (!link(src->buf, dest->buf))
391                                 continue;
392                         if (option_local > 0)
393                                 die_errno(_("failed to create link '%s'"), dest->buf);
394                         option_no_hardlinks = 1;
395                 }
396                 if (copy_file_with_time(dest->buf, src->buf, 0666))
397                         die_errno(_("failed to copy file to '%s'"), dest->buf);
398         }
399         closedir(dir);
400 }
401
402 static void clone_local(const char *src_repo, const char *dest_repo)
403 {
404         if (option_shared) {
405                 struct strbuf alt = STRBUF_INIT;
406                 strbuf_addf(&alt, "%s/objects", src_repo);
407                 add_to_alternates_file(alt.buf);
408                 strbuf_release(&alt);
409         } else {
410                 struct strbuf src = STRBUF_INIT;
411                 struct strbuf dest = STRBUF_INIT;
412                 strbuf_addf(&src, "%s/objects", src_repo);
413                 strbuf_addf(&dest, "%s/objects", dest_repo);
414                 copy_or_link_directory(&src, &dest, src_repo, src.len);
415                 strbuf_release(&src);
416                 strbuf_release(&dest);
417         }
418
419         if (0 <= option_verbosity)
420                 fprintf(stderr, _("done.\n"));
421 }
422
423 static const char *junk_work_tree;
424 static const char *junk_git_dir;
425 static enum {
426         JUNK_LEAVE_NONE,
427         JUNK_LEAVE_REPO,
428         JUNK_LEAVE_ALL
429 } junk_mode = JUNK_LEAVE_NONE;
430
431 static const char junk_leave_repo_msg[] =
432 N_("Clone succeeded, but checkout failed.\n"
433    "You can inspect what was checked out with 'git status'\n"
434    "and retry the checkout with 'git checkout -f HEAD'\n");
435
436 static void remove_junk(void)
437 {
438         struct strbuf sb = STRBUF_INIT;
439
440         switch (junk_mode) {
441         case JUNK_LEAVE_REPO:
442                 warning("%s", _(junk_leave_repo_msg));
443                 /* fall-through */
444         case JUNK_LEAVE_ALL:
445                 return;
446         default:
447                 /* proceed to removal */
448                 break;
449         }
450
451         if (junk_git_dir) {
452                 strbuf_addstr(&sb, junk_git_dir);
453                 remove_dir_recursively(&sb, 0);
454                 strbuf_reset(&sb);
455         }
456         if (junk_work_tree) {
457                 strbuf_addstr(&sb, junk_work_tree);
458                 remove_dir_recursively(&sb, 0);
459                 strbuf_reset(&sb);
460         }
461 }
462
463 static void remove_junk_on_signal(int signo)
464 {
465         remove_junk();
466         sigchain_pop(signo);
467         raise(signo);
468 }
469
470 static struct ref *find_remote_branch(const struct ref *refs, const char *branch)
471 {
472         struct ref *ref;
473         struct strbuf head = STRBUF_INIT;
474         strbuf_addstr(&head, "refs/heads/");
475         strbuf_addstr(&head, branch);
476         ref = find_ref_by_name(refs, head.buf);
477         strbuf_release(&head);
478
479         if (ref)
480                 return ref;
481
482         strbuf_addstr(&head, "refs/tags/");
483         strbuf_addstr(&head, branch);
484         ref = find_ref_by_name(refs, head.buf);
485         strbuf_release(&head);
486
487         return ref;
488 }
489
490 static struct ref *wanted_peer_refs(const struct ref *refs,
491                 struct refspec *refspec)
492 {
493         struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD"));
494         struct ref *local_refs = head;
495         struct ref **tail = head ? &head->next : &local_refs;
496
497         if (option_single_branch) {
498                 struct ref *remote_head = NULL;
499
500                 if (!option_branch)
501                         remote_head = guess_remote_head(head, refs, 0);
502                 else {
503                         local_refs = NULL;
504                         tail = &local_refs;
505                         remote_head = copy_ref(find_remote_branch(refs, option_branch));
506                 }
507
508                 if (!remote_head && option_branch)
509                         warning(_("Could not find remote branch %s to clone."),
510                                 option_branch);
511                 else {
512                         get_fetch_map(remote_head, refspec, &tail, 0);
513
514                         /* if --branch=tag, pull the requested tag explicitly */
515                         get_fetch_map(remote_head, tag_refspec, &tail, 0);
516                 }
517         } else
518                 get_fetch_map(refs, refspec, &tail, 0);
519
520         if (!option_mirror && !option_single_branch)
521                 get_fetch_map(refs, tag_refspec, &tail, 0);
522
523         return local_refs;
524 }
525
526 static void write_remote_refs(const struct ref *local_refs)
527 {
528         const struct ref *r;
529
530         struct ref_transaction *t;
531         struct strbuf err = STRBUF_INIT;
532
533         t = ref_transaction_begin(&err);
534         if (!t)
535                 die("%s", err.buf);
536
537         for (r = local_refs; r; r = r->next) {
538                 if (!r->peer_ref)
539                         continue;
540                 if (ref_transaction_create(t, r->peer_ref->name, r->old_sha1,
541                                            0, NULL, &err))
542                         die("%s", err.buf);
543         }
544
545         if (initial_ref_transaction_commit(t, &err))
546                 die("%s", err.buf);
547
548         strbuf_release(&err);
549         ref_transaction_free(t);
550 }
551
552 static void write_followtags(const struct ref *refs, const char *msg)
553 {
554         const struct ref *ref;
555         for (ref = refs; ref; ref = ref->next) {
556                 if (!starts_with(ref->name, "refs/tags/"))
557                         continue;
558                 if (ends_with(ref->name, "^{}"))
559                         continue;
560                 if (!has_sha1_file(ref->old_sha1))
561                         continue;
562                 update_ref(msg, ref->name, ref->old_sha1,
563                            NULL, 0, UPDATE_REFS_DIE_ON_ERR);
564         }
565 }
566
567 static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
568 {
569         struct ref **rm = cb_data;
570         struct ref *ref = *rm;
571
572         /*
573          * Skip anything missing a peer_ref, which we are not
574          * actually going to write a ref for.
575          */
576         while (ref && !ref->peer_ref)
577                 ref = ref->next;
578         /* Returning -1 notes "end of list" to the caller. */
579         if (!ref)
580                 return -1;
581
582         hashcpy(sha1, ref->old_sha1);
583         *rm = ref->next;
584         return 0;
585 }
586
587 static void update_remote_refs(const struct ref *refs,
588                                const struct ref *mapped_refs,
589                                const struct ref *remote_head_points_at,
590                                const char *branch_top,
591                                const char *msg,
592                                struct transport *transport,
593                                int check_connectivity)
594 {
595         const struct ref *rm = mapped_refs;
596
597         if (check_connectivity) {
598                 if (transport->progress)
599                         fprintf(stderr, _("Checking connectivity... "));
600                 if (check_everything_connected_with_transport(iterate_ref_map,
601                                                               0, &rm, transport))
602                         die(_("remote did not send all necessary objects"));
603                 if (transport->progress)
604                         fprintf(stderr, _("done.\n"));
605         }
606
607         if (refs) {
608                 write_remote_refs(mapped_refs);
609                 if (option_single_branch)
610                         write_followtags(refs, msg);
611         }
612
613         if (remote_head_points_at && !option_bare) {
614                 struct strbuf head_ref = STRBUF_INIT;
615                 strbuf_addstr(&head_ref, branch_top);
616                 strbuf_addstr(&head_ref, "HEAD");
617                 create_symref(head_ref.buf,
618                               remote_head_points_at->peer_ref->name,
619                               msg);
620         }
621 }
622
623 static void update_head(const struct ref *our, const struct ref *remote,
624                         const char *msg)
625 {
626         const char *head;
627         if (our && skip_prefix(our->name, "refs/heads/", &head)) {
628                 /* Local default branch link */
629                 create_symref("HEAD", our->name, NULL);
630                 if (!option_bare) {
631                         update_ref(msg, "HEAD", our->old_sha1, NULL, 0,
632                                    UPDATE_REFS_DIE_ON_ERR);
633                         install_branch_config(0, head, option_origin, our->name);
634                 }
635         } else if (our) {
636                 struct commit *c = lookup_commit_reference(our->old_sha1);
637                 /* --branch specifies a non-branch (i.e. tags), detach HEAD */
638                 update_ref(msg, "HEAD", c->object.sha1,
639                            NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
640         } else if (remote) {
641                 /*
642                  * We know remote HEAD points to a non-branch, or
643                  * HEAD points to a branch but we don't know which one.
644                  * Detach HEAD in all these cases.
645                  */
646                 update_ref(msg, "HEAD", remote->old_sha1,
647                            NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
648         }
649 }
650
651 static int checkout(void)
652 {
653         unsigned char sha1[20];
654         char *head;
655         struct lock_file *lock_file;
656         struct unpack_trees_options opts;
657         struct tree *tree;
658         struct tree_desc t;
659         int err = 0;
660
661         if (option_no_checkout)
662                 return 0;
663
664         head = resolve_refdup("HEAD", RESOLVE_REF_READING, sha1, NULL);
665         if (!head) {
666                 warning(_("remote HEAD refers to nonexistent ref, "
667                           "unable to checkout.\n"));
668                 return 0;
669         }
670         if (!strcmp(head, "HEAD")) {
671                 if (advice_detached_head)
672                         detach_advice(sha1_to_hex(sha1));
673         } else {
674                 if (!starts_with(head, "refs/heads/"))
675                         die(_("HEAD not found below refs/heads!"));
676         }
677         free(head);
678
679         /* We need to be in the new work tree for the checkout */
680         setup_work_tree();
681
682         lock_file = xcalloc(1, sizeof(struct lock_file));
683         hold_locked_index(lock_file, 1);
684
685         memset(&opts, 0, sizeof opts);
686         opts.update = 1;
687         opts.merge = 1;
688         opts.fn = oneway_merge;
689         opts.verbose_update = (option_verbosity >= 0);
690         opts.src_index = &the_index;
691         opts.dst_index = &the_index;
692
693         tree = parse_tree_indirect(sha1);
694         parse_tree(tree);
695         init_tree_desc(&t, tree->buffer, tree->size);
696         if (unpack_trees(1, &t, &opts) < 0)
697                 die(_("unable to checkout working tree"));
698
699         if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
700                 die(_("unable to write new index file"));
701
702         err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
703                            sha1_to_hex(sha1), "1", NULL);
704
705         if (!err && option_recursive)
706                 err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
707
708         return err;
709 }
710
711 static int write_one_config(const char *key, const char *value, void *data)
712 {
713         return git_config_set_multivar(key, value ? value : "true", "^$", 0);
714 }
715
716 static void write_config(struct string_list *config)
717 {
718         int i;
719
720         for (i = 0; i < config->nr; i++) {
721                 if (git_config_parse_parameter(config->items[i].string,
722                                                write_one_config, NULL) < 0)
723                         die("unable to write parameters to config file");
724         }
725 }
726
727 static void write_refspec_config(const char *src_ref_prefix,
728                 const struct ref *our_head_points_at,
729                 const struct ref *remote_head_points_at,
730                 struct strbuf *branch_top)
731 {
732         struct strbuf key = STRBUF_INIT;
733         struct strbuf value = STRBUF_INIT;
734
735         if (option_mirror || !option_bare) {
736                 if (option_single_branch && !option_mirror) {
737                         if (option_branch) {
738                                 if (starts_with(our_head_points_at->name, "refs/tags/"))
739                                         strbuf_addf(&value, "+%s:%s", our_head_points_at->name,
740                                                 our_head_points_at->name);
741                                 else
742                                         strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
743                                                 branch_top->buf, option_branch);
744                         } else if (remote_head_points_at) {
745                                 const char *head = remote_head_points_at->name;
746                                 if (!skip_prefix(head, "refs/heads/", &head))
747                                         die("BUG: remote HEAD points at non-head?");
748
749                                 strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name,
750                                                 branch_top->buf, head);
751                         }
752                         /*
753                          * otherwise, the next "git fetch" will
754                          * simply fetch from HEAD without updating
755                          * any remote-tracking branch, which is what
756                          * we want.
757                          */
758                 } else {
759                         strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf);
760                 }
761                 /* Configure the remote */
762                 if (value.len) {
763                         strbuf_addf(&key, "remote.%s.fetch", option_origin);
764                         git_config_set_multivar(key.buf, value.buf, "^$", 0);
765                         strbuf_reset(&key);
766
767                         if (option_mirror) {
768                                 strbuf_addf(&key, "remote.%s.mirror", option_origin);
769                                 git_config_set(key.buf, "true");
770                                 strbuf_reset(&key);
771                         }
772                 }
773         }
774
775         strbuf_release(&key);
776         strbuf_release(&value);
777 }
778
779 static void dissociate_from_references(void)
780 {
781         static const char* argv[] = { "repack", "-a", "-d", NULL };
782
783         if (run_command_v_opt(argv, RUN_GIT_CMD|RUN_COMMAND_NO_STDIN))
784                 die(_("cannot repack to clean up"));
785         if (unlink(git_path("objects/info/alternates")) && errno != ENOENT)
786                 die_errno(_("cannot unlink temporary alternates file"));
787 }
788
789 int cmd_clone(int argc, const char **argv, const char *prefix)
790 {
791         int is_bundle = 0, is_local;
792         struct stat buf;
793         const char *repo_name, *repo, *work_tree, *git_dir;
794         char *path, *dir;
795         int dest_exists;
796         const struct ref *refs, *remote_head;
797         const struct ref *remote_head_points_at;
798         const struct ref *our_head_points_at;
799         struct ref *mapped_refs;
800         const struct ref *ref;
801         struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
802         struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
803         struct transport *transport = NULL;
804         const char *src_ref_prefix = "refs/heads/";
805         struct remote *remote;
806         int err = 0, complete_refs_before_fetch = 1;
807
808         struct refspec *refspec;
809         const char *fetch_pattern;
810
811         packet_trace_identity("clone");
812         argc = parse_options(argc, argv, prefix, builtin_clone_options,
813                              builtin_clone_usage, 0);
814
815         if (argc > 2)
816                 usage_msg_opt(_("Too many arguments."),
817                         builtin_clone_usage, builtin_clone_options);
818
819         if (argc == 0)
820                 usage_msg_opt(_("You must specify a repository to clone."),
821                         builtin_clone_usage, builtin_clone_options);
822
823         if (option_single_branch == -1)
824                 option_single_branch = option_depth ? 1 : 0;
825
826         if (option_mirror)
827                 option_bare = 1;
828
829         if (option_bare) {
830                 if (option_origin)
831                         die(_("--bare and --origin %s options are incompatible."),
832                             option_origin);
833                 if (real_git_dir)
834                         die(_("--bare and --separate-git-dir are incompatible."));
835                 option_no_checkout = 1;
836         }
837
838         if (!option_origin)
839                 option_origin = "origin";
840
841         repo_name = argv[0];
842
843         path = get_repo_path(repo_name, &is_bundle);
844         if (path)
845                 repo = xstrdup(absolute_path(repo_name));
846         else if (!strchr(repo_name, ':'))
847                 die(_("repository '%s' does not exist"), repo_name);
848         else
849                 repo = repo_name;
850
851         /* no need to be strict, transport_set_option() will validate it again */
852         if (option_depth && atoi(option_depth) < 1)
853                 die(_("depth %s is not a positive number"), option_depth);
854
855         if (argc == 2)
856                 dir = xstrdup(argv[1]);
857         else
858                 dir = guess_dir_name(repo_name, is_bundle, option_bare);
859         strip_trailing_slashes(dir);
860
861         dest_exists = !stat(dir, &buf);
862         if (dest_exists && !is_empty_dir(dir))
863                 die(_("destination path '%s' already exists and is not "
864                         "an empty directory."), dir);
865
866         strbuf_addf(&reflog_msg, "clone: from %s", repo);
867
868         if (option_bare)
869                 work_tree = NULL;
870         else {
871                 work_tree = getenv("GIT_WORK_TREE");
872                 if (work_tree && !stat(work_tree, &buf))
873                         die(_("working tree '%s' already exists."), work_tree);
874         }
875
876         if (option_bare || work_tree)
877                 git_dir = xstrdup(dir);
878         else {
879                 work_tree = dir;
880                 git_dir = mkpathdup("%s/.git", dir);
881         }
882
883         atexit(remove_junk);
884         sigchain_push_common(remove_junk_on_signal);
885
886         if (!option_bare) {
887                 if (safe_create_leading_directories_const(work_tree) < 0)
888                         die_errno(_("could not create leading directories of '%s'"),
889                                   work_tree);
890                 if (!dest_exists && mkdir(work_tree, 0777))
891                         die_errno(_("could not create work tree dir '%s'"),
892                                   work_tree);
893                 junk_work_tree = work_tree;
894                 set_git_work_tree(work_tree);
895         }
896
897         junk_git_dir = git_dir;
898         if (safe_create_leading_directories_const(git_dir) < 0)
899                 die(_("could not create leading directories of '%s'"), git_dir);
900
901         set_git_dir_init(git_dir, real_git_dir, 0);
902         if (real_git_dir) {
903                 git_dir = real_git_dir;
904                 junk_git_dir = real_git_dir;
905         }
906
907         if (0 <= option_verbosity) {
908                 if (option_bare)
909                         fprintf(stderr, _("Cloning into bare repository '%s'...\n"), dir);
910                 else
911                         fprintf(stderr, _("Cloning into '%s'...\n"), dir);
912         }
913         init_db(option_template, INIT_DB_QUIET);
914         write_config(&option_config);
915
916         git_config(git_default_config, NULL);
917
918         if (option_bare) {
919                 if (option_mirror)
920                         src_ref_prefix = "refs/";
921                 strbuf_addstr(&branch_top, src_ref_prefix);
922
923                 git_config_set("core.bare", "true");
924         } else {
925                 strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
926         }
927
928         strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
929         strbuf_addf(&key, "remote.%s.url", option_origin);
930         git_config_set(key.buf, repo);
931         strbuf_reset(&key);
932
933         if (option_reference.nr)
934                 setup_reference();
935         else if (option_dissociate) {
936                 warning(_("--dissociate given, but there is no --reference"));
937                 option_dissociate = 0;
938         }
939
940         fetch_pattern = value.buf;
941         refspec = parse_fetch_refspec(1, &fetch_pattern);
942
943         strbuf_reset(&value);
944
945         remote = remote_get(option_origin);
946         transport = transport_get(remote, remote->url[0]);
947         transport_set_verbosity(transport, option_verbosity, option_progress);
948
949         path = get_repo_path(remote->url[0], &is_bundle);
950         is_local = option_local != 0 && path && !is_bundle;
951         if (is_local) {
952                 if (option_depth)
953                         warning(_("--depth is ignored in local clones; use file:// instead."));
954                 if (!access(mkpath("%s/shallow", path), F_OK)) {
955                         if (option_local > 0)
956                                 warning(_("source repository is shallow, ignoring --local"));
957                         is_local = 0;
958                 }
959         }
960         if (option_local > 0 && !is_local)
961                 warning(_("--local is ignored"));
962         transport->cloning = 1;
963
964         if (!transport->get_refs_list || (!is_local && !transport->fetch))
965                 die(_("Don't know how to clone %s"), transport->url);
966
967         transport_set_option(transport, TRANS_OPT_KEEP, "yes");
968
969         if (option_depth)
970                 transport_set_option(transport, TRANS_OPT_DEPTH,
971                                      option_depth);
972         if (option_single_branch)
973                 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
974
975         if (option_upload_pack)
976                 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
977                                      option_upload_pack);
978
979         if (transport->smart_options && !option_depth)
980                 transport->smart_options->check_self_contained_and_connected = 1;
981
982         refs = transport_get_remote_refs(transport);
983
984         if (refs) {
985                 mapped_refs = wanted_peer_refs(refs, refspec);
986                 /*
987                  * transport_get_remote_refs() may return refs with null sha-1
988                  * in mapped_refs (see struct transport->get_refs_list
989                  * comment). In that case we need fetch it early because
990                  * remote_head code below relies on it.
991                  *
992                  * for normal clones, transport_get_remote_refs() should
993                  * return reliable ref set, we can delay cloning until after
994                  * remote HEAD check.
995                  */
996                 for (ref = refs; ref; ref = ref->next)
997                         if (is_null_sha1(ref->old_sha1)) {
998                                 complete_refs_before_fetch = 0;
999                                 break;
1000                         }
1001
1002                 if (!is_local && !complete_refs_before_fetch)
1003                         transport_fetch_refs(transport, mapped_refs);
1004
1005                 remote_head = find_ref_by_name(refs, "HEAD");
1006                 remote_head_points_at =
1007                         guess_remote_head(remote_head, mapped_refs, 0);
1008
1009                 if (option_branch) {
1010                         our_head_points_at =
1011                                 find_remote_branch(mapped_refs, option_branch);
1012
1013                         if (!our_head_points_at)
1014                                 die(_("Remote branch %s not found in upstream %s"),
1015                                     option_branch, option_origin);
1016                 }
1017                 else
1018                         our_head_points_at = remote_head_points_at;
1019         }
1020         else {
1021                 if (option_branch)
1022                         die(_("Remote branch %s not found in upstream %s"),
1023                                         option_branch, option_origin);
1024
1025                 warning(_("You appear to have cloned an empty repository."));
1026                 mapped_refs = NULL;
1027                 our_head_points_at = NULL;
1028                 remote_head_points_at = NULL;
1029                 remote_head = NULL;
1030                 option_no_checkout = 1;
1031                 if (!option_bare)
1032                         install_branch_config(0, "master", option_origin,
1033                                               "refs/heads/master");
1034         }
1035
1036         write_refspec_config(src_ref_prefix, our_head_points_at,
1037                         remote_head_points_at, &branch_top);
1038
1039         if (is_local)
1040                 clone_local(path, git_dir);
1041         else if (refs && complete_refs_before_fetch)
1042                 transport_fetch_refs(transport, mapped_refs);
1043
1044         update_remote_refs(refs, mapped_refs, remote_head_points_at,
1045                            branch_top.buf, reflog_msg.buf, transport, !is_local);
1046
1047         update_head(our_head_points_at, remote_head, reflog_msg.buf);
1048
1049         transport_unlock_pack(transport);
1050         transport_disconnect(transport);
1051
1052         if (option_dissociate)
1053                 dissociate_from_references();
1054
1055         junk_mode = JUNK_LEAVE_REPO;
1056         err = checkout();
1057
1058         strbuf_release(&reflog_msg);
1059         strbuf_release(&branch_top);
1060         strbuf_release(&key);
1061         strbuf_release(&value);
1062         junk_mode = JUNK_LEAVE_ALL;
1063
1064         free(refspec);
1065         return err;
1066 }