Merge branch 'rs/archive-add-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 #define USE_THE_INDEX_COMPATIBILITY_MACROS
12 #include "builtin.h"
13 #include "config.h"
14 #include "lockfile.h"
15 #include "parse-options.h"
16 #include "fetch-pack.h"
17 #include "refs.h"
18 #include "refspec.h"
19 #include "object-store.h"
20 #include "tree.h"
21 #include "tree-walk.h"
22 #include "unpack-trees.h"
23 #include "transport.h"
24 #include "strbuf.h"
25 #include "dir.h"
26 #include "dir-iterator.h"
27 #include "iterator.h"
28 #include "sigchain.h"
29 #include "branch.h"
30 #include "remote.h"
31 #include "run-command.h"
32 #include "connected.h"
33 #include "packfile.h"
34 #include "list-objects-filter-options.h"
35
36 /*
37  * Overall FIXMEs:
38  *  - respect DB_ENVIRONMENT for .git/objects.
39  *
40  * Implementation notes:
41  *  - dropping use-separate-remote and no-separate-remote compatibility
42  *
43  */
44 static const char * const builtin_clone_usage[] = {
45         N_("git clone [<options>] [--] <repo> [<dir>]"),
46         NULL
47 };
48
49 static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
50 static int option_local = -1, option_no_hardlinks, option_shared;
51 static int option_no_tags;
52 static int option_shallow_submodules;
53 static int deepen;
54 static char *option_template, *option_depth, *option_since;
55 static char *option_origin = NULL;
56 static char *option_branch = NULL;
57 static struct string_list option_not = STRING_LIST_INIT_NODUP;
58 static const char *real_git_dir;
59 static char *option_upload_pack = "git-upload-pack";
60 static int option_verbosity;
61 static int option_progress = -1;
62 static int option_sparse_checkout;
63 static enum transport_family family;
64 static struct string_list option_config = STRING_LIST_INIT_NODUP;
65 static struct string_list option_required_reference = STRING_LIST_INIT_NODUP;
66 static struct string_list option_optional_reference = STRING_LIST_INIT_NODUP;
67 static int option_dissociate;
68 static int max_jobs = -1;
69 static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP;
70 static struct list_objects_filter_options filter_options;
71 static struct string_list server_options = STRING_LIST_INIT_NODUP;
72 static int option_remote_submodules;
73
74 static int recurse_submodules_cb(const struct option *opt,
75                                  const char *arg, int unset)
76 {
77         if (unset)
78                 string_list_clear((struct string_list *)opt->value, 0);
79         else if (arg)
80                 string_list_append((struct string_list *)opt->value, arg);
81         else
82                 string_list_append((struct string_list *)opt->value,
83                                    (const char *)opt->defval);
84
85         return 0;
86 }
87
88 static struct option builtin_clone_options[] = {
89         OPT__VERBOSITY(&option_verbosity),
90         OPT_BOOL(0, "progress", &option_progress,
91                  N_("force progress reporting")),
92         OPT_BOOL('n', "no-checkout", &option_no_checkout,
93                  N_("don't create a checkout")),
94         OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
95         OPT_HIDDEN_BOOL(0, "naked", &option_bare,
96                         N_("create a bare repository")),
97         OPT_BOOL(0, "mirror", &option_mirror,
98                  N_("create a mirror repository (implies bare)")),
99         OPT_BOOL('l', "local", &option_local,
100                 N_("to clone from a local repository")),
101         OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
102                     N_("don't use local hardlinks, always copy")),
103         OPT_BOOL('s', "shared", &option_shared,
104                     N_("setup as shared repository")),
105         { OPTION_CALLBACK, 0, "recurse-submodules", &option_recurse_submodules,
106           N_("pathspec"), N_("initialize submodules in the clone"),
107           PARSE_OPT_OPTARG, recurse_submodules_cb, (intptr_t)"." },
108         OPT_ALIAS(0, "recursive", "recurse-submodules"),
109         OPT_INTEGER('j', "jobs", &max_jobs,
110                     N_("number of submodules cloned in parallel")),
111         OPT_STRING(0, "template", &option_template, N_("template-directory"),
112                    N_("directory from which templates will be used")),
113         OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"),
114                         N_("reference repository")),
115         OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference,
116                         N_("repo"), N_("reference repository")),
117         OPT_BOOL(0, "dissociate", &option_dissociate,
118                  N_("use --reference only while cloning")),
119         OPT_STRING('o', "origin", &option_origin, N_("name"),
120                    N_("use <name> instead of 'origin' to track upstream")),
121         OPT_STRING('b', "branch", &option_branch, N_("branch"),
122                    N_("checkout <branch> instead of the remote's HEAD")),
123         OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
124                    N_("path to git-upload-pack on the remote")),
125         OPT_STRING(0, "depth", &option_depth, N_("depth"),
126                     N_("create a shallow clone of that depth")),
127         OPT_STRING(0, "shallow-since", &option_since, N_("time"),
128                     N_("create a shallow clone since a specific time")),
129         OPT_STRING_LIST(0, "shallow-exclude", &option_not, N_("revision"),
130                         N_("deepen history of shallow clone, excluding rev")),
131         OPT_BOOL(0, "single-branch", &option_single_branch,
132                     N_("clone only one branch, HEAD or --branch")),
133         OPT_BOOL(0, "no-tags", &option_no_tags,
134                  N_("don't clone any tags, and make later fetches not to follow them")),
135         OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules,
136                     N_("any cloned submodules will be shallow")),
137         OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
138                    N_("separate git dir from working tree")),
139         OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
140                         N_("set config inside the new repository")),
141         OPT_STRING_LIST(0, "server-option", &server_options,
142                         N_("server-specific"), N_("option to transmit")),
143         OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
144                         TRANSPORT_FAMILY_IPV4),
145         OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
146                         TRANSPORT_FAMILY_IPV6),
147         OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
148         OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
149                     N_("any cloned submodules will use their remote-tracking branch")),
150         OPT_BOOL(0, "sparse", &option_sparse_checkout,
151                     N_("initialize sparse-checkout file to include only files at root")),
152         OPT_END()
153 };
154
155 static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
156 {
157         static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
158         static char *bundle_suffix[] = { ".bundle", "" };
159         size_t baselen = path->len;
160         struct stat st;
161         int i;
162
163         for (i = 0; i < ARRAY_SIZE(suffix); i++) {
164                 strbuf_setlen(path, baselen);
165                 strbuf_addstr(path, suffix[i]);
166                 if (stat(path->buf, &st))
167                         continue;
168                 if (S_ISDIR(st.st_mode) && is_git_directory(path->buf)) {
169                         *is_bundle = 0;
170                         return path->buf;
171                 } else if (S_ISREG(st.st_mode) && st.st_size > 8) {
172                         /* Is it a "gitfile"? */
173                         char signature[8];
174                         const char *dst;
175                         int len, fd = open(path->buf, O_RDONLY);
176                         if (fd < 0)
177                                 continue;
178                         len = read_in_full(fd, signature, 8);
179                         close(fd);
180                         if (len != 8 || strncmp(signature, "gitdir: ", 8))
181                                 continue;
182                         dst = read_gitfile(path->buf);
183                         if (dst) {
184                                 *is_bundle = 0;
185                                 return dst;
186                         }
187                 }
188         }
189
190         for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
191                 strbuf_setlen(path, baselen);
192                 strbuf_addstr(path, bundle_suffix[i]);
193                 if (!stat(path->buf, &st) && S_ISREG(st.st_mode)) {
194                         *is_bundle = 1;
195                         return path->buf;
196                 }
197         }
198
199         return NULL;
200 }
201
202 static char *get_repo_path(const char *repo, int *is_bundle)
203 {
204         struct strbuf path = STRBUF_INIT;
205         const char *raw;
206         char *canon;
207
208         strbuf_addstr(&path, repo);
209         raw = get_repo_path_1(&path, is_bundle);
210         canon = raw ? absolute_pathdup(raw) : NULL;
211         strbuf_release(&path);
212         return canon;
213 }
214
215 static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
216 {
217         const char *end = repo + strlen(repo), *start, *ptr;
218         size_t len;
219         char *dir;
220
221         /*
222          * Skip scheme.
223          */
224         start = strstr(repo, "://");
225         if (start == NULL)
226                 start = repo;
227         else
228                 start += 3;
229
230         /*
231          * Skip authentication data. The stripping does happen
232          * greedily, such that we strip up to the last '@' inside
233          * the host part.
234          */
235         for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) {
236                 if (*ptr == '@')
237                         start = ptr + 1;
238         }
239
240         /*
241          * Strip trailing spaces, slashes and /.git
242          */
243         while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
244                 end--;
245         if (end - start > 5 && is_dir_sep(end[-5]) &&
246             !strncmp(end - 4, ".git", 4)) {
247                 end -= 5;
248                 while (start < end && is_dir_sep(end[-1]))
249                         end--;
250         }
251
252         /*
253          * Strip trailing port number if we've got only a
254          * hostname (that is, there is no dir separator but a
255          * colon). This check is required such that we do not
256          * strip URI's like '/foo/bar:2222.git', which should
257          * result in a dir '2222' being guessed due to backwards
258          * compatibility.
259          */
260         if (memchr(start, '/', end - start) == NULL
261             && memchr(start, ':', end - start) != NULL) {
262                 ptr = end;
263                 while (start < ptr && isdigit(ptr[-1]) && ptr[-1] != ':')
264                         ptr--;
265                 if (start < ptr && ptr[-1] == ':')
266                         end = ptr - 1;
267         }
268
269         /*
270          * Find last component. To remain backwards compatible we
271          * also regard colons as path separators, such that
272          * cloning a repository 'foo:bar.git' would result in a
273          * directory 'bar' being guessed.
274          */
275         ptr = end;
276         while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':')
277                 ptr--;
278         start = ptr;
279
280         /*
281          * Strip .{bundle,git}.
282          */
283         len = end - start;
284         strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git");
285
286         if (!len || (len == 1 && *start == '/'))
287                 die(_("No directory name could be guessed.\n"
288                       "Please specify a directory on the command line"));
289
290         if (is_bare)
291                 dir = xstrfmt("%.*s.git", (int)len, start);
292         else
293                 dir = xstrndup(start, len);
294         /*
295          * Replace sequences of 'control' characters and whitespace
296          * with one ascii space, remove leading and trailing spaces.
297          */
298         if (*dir) {
299                 char *out = dir;
300                 int prev_space = 1 /* strip leading whitespace */;
301                 for (end = dir; *end; ++end) {
302                         char ch = *end;
303                         if ((unsigned char)ch < '\x20')
304                                 ch = '\x20';
305                         if (isspace(ch)) {
306                                 if (prev_space)
307                                         continue;
308                                 prev_space = 1;
309                         } else
310                                 prev_space = 0;
311                         *out++ = ch;
312                 }
313                 *out = '\0';
314                 if (out > dir && prev_space)
315                         out[-1] = '\0';
316         }
317         return dir;
318 }
319
320 static void strip_trailing_slashes(char *dir)
321 {
322         char *end = dir + strlen(dir);
323
324         while (dir < end - 1 && is_dir_sep(end[-1]))
325                 end--;
326         *end = '\0';
327 }
328
329 static int add_one_reference(struct string_list_item *item, void *cb_data)
330 {
331         struct strbuf err = STRBUF_INIT;
332         int *required = cb_data;
333         char *ref_git = compute_alternate_path(item->string, &err);
334
335         if (!ref_git) {
336                 if (*required)
337                         die("%s", err.buf);
338                 else
339                         fprintf(stderr,
340                                 _("info: Could not add alternate for '%s': %s\n"),
341                                 item->string, err.buf);
342         } else {
343                 struct strbuf sb = STRBUF_INIT;
344                 strbuf_addf(&sb, "%s/objects", ref_git);
345                 add_to_alternates_file(sb.buf);
346                 strbuf_release(&sb);
347         }
348
349         strbuf_release(&err);
350         free(ref_git);
351         return 0;
352 }
353
354 static void setup_reference(void)
355 {
356         int required = 1;
357         for_each_string_list(&option_required_reference,
358                              add_one_reference, &required);
359         required = 0;
360         for_each_string_list(&option_optional_reference,
361                              add_one_reference, &required);
362 }
363
364 static void copy_alternates(struct strbuf *src, const char *src_repo)
365 {
366         /*
367          * Read from the source objects/info/alternates file
368          * and copy the entries to corresponding file in the
369          * destination repository with add_to_alternates_file().
370          * Both src and dst have "$path/objects/info/alternates".
371          *
372          * Instead of copying bit-for-bit from the original,
373          * we need to append to existing one so that the already
374          * created entry via "clone -s" is not lost, and also
375          * to turn entries with paths relative to the original
376          * absolute, so that they can be used in the new repository.
377          */
378         FILE *in = xfopen(src->buf, "r");
379         struct strbuf line = STRBUF_INIT;
380
381         while (strbuf_getline(&line, in) != EOF) {
382                 char *abs_path;
383                 if (!line.len || line.buf[0] == '#')
384                         continue;
385                 if (is_absolute_path(line.buf)) {
386                         add_to_alternates_file(line.buf);
387                         continue;
388                 }
389                 abs_path = mkpathdup("%s/objects/%s", src_repo, line.buf);
390                 if (!normalize_path_copy(abs_path, abs_path))
391                         add_to_alternates_file(abs_path);
392                 else
393                         warning("skipping invalid relative alternate: %s/%s",
394                                 src_repo, line.buf);
395                 free(abs_path);
396         }
397         strbuf_release(&line);
398         fclose(in);
399 }
400
401 static void mkdir_if_missing(const char *pathname, mode_t mode)
402 {
403         struct stat st;
404
405         if (!mkdir(pathname, mode))
406                 return;
407
408         if (errno != EEXIST)
409                 die_errno(_("failed to create directory '%s'"), pathname);
410         else if (stat(pathname, &st))
411                 die_errno(_("failed to stat '%s'"), pathname);
412         else if (!S_ISDIR(st.st_mode))
413                 die(_("%s exists and is not a directory"), pathname);
414 }
415
416 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
417                                    const char *src_repo)
418 {
419         int src_len, dest_len;
420         struct dir_iterator *iter;
421         int iter_status;
422         unsigned int flags;
423         struct strbuf realpath = STRBUF_INIT;
424
425         mkdir_if_missing(dest->buf, 0777);
426
427         flags = DIR_ITERATOR_PEDANTIC | DIR_ITERATOR_FOLLOW_SYMLINKS;
428         iter = dir_iterator_begin(src->buf, flags);
429
430         if (!iter)
431                 die_errno(_("failed to start iterator over '%s'"), src->buf);
432
433         strbuf_addch(src, '/');
434         src_len = src->len;
435         strbuf_addch(dest, '/');
436         dest_len = dest->len;
437
438         while ((iter_status = dir_iterator_advance(iter)) == ITER_OK) {
439                 strbuf_setlen(src, src_len);
440                 strbuf_addstr(src, iter->relative_path);
441                 strbuf_setlen(dest, dest_len);
442                 strbuf_addstr(dest, iter->relative_path);
443
444                 if (S_ISDIR(iter->st.st_mode)) {
445                         mkdir_if_missing(dest->buf, 0777);
446                         continue;
447                 }
448
449                 /* Files that cannot be copied bit-for-bit... */
450                 if (!fspathcmp(iter->relative_path, "info/alternates")) {
451                         copy_alternates(src, src_repo);
452                         continue;
453                 }
454
455                 if (unlink(dest->buf) && errno != ENOENT)
456                         die_errno(_("failed to unlink '%s'"), dest->buf);
457                 if (!option_no_hardlinks) {
458                         strbuf_realpath(&realpath, src->buf, 1);
459                         if (!link(realpath.buf, dest->buf))
460                                 continue;
461                         if (option_local > 0)
462                                 die_errno(_("failed to create link '%s'"), dest->buf);
463                         option_no_hardlinks = 1;
464                 }
465                 if (copy_file_with_time(dest->buf, src->buf, 0666))
466                         die_errno(_("failed to copy file to '%s'"), dest->buf);
467         }
468
469         if (iter_status != ITER_DONE) {
470                 strbuf_setlen(src, src_len);
471                 die(_("failed to iterate over '%s'"), src->buf);
472         }
473
474         strbuf_release(&realpath);
475 }
476
477 static void clone_local(const char *src_repo, const char *dest_repo)
478 {
479         if (option_shared) {
480                 struct strbuf alt = STRBUF_INIT;
481                 get_common_dir(&alt, src_repo);
482                 strbuf_addstr(&alt, "/objects");
483                 add_to_alternates_file(alt.buf);
484                 strbuf_release(&alt);
485         } else {
486                 struct strbuf src = STRBUF_INIT;
487                 struct strbuf dest = STRBUF_INIT;
488                 get_common_dir(&src, src_repo);
489                 get_common_dir(&dest, dest_repo);
490                 strbuf_addstr(&src, "/objects");
491                 strbuf_addstr(&dest, "/objects");
492                 copy_or_link_directory(&src, &dest, src_repo);
493                 strbuf_release(&src);
494                 strbuf_release(&dest);
495         }
496
497         if (0 <= option_verbosity)
498                 fprintf(stderr, _("done.\n"));
499 }
500
501 static const char *junk_work_tree;
502 static int junk_work_tree_flags;
503 static const char *junk_git_dir;
504 static int junk_git_dir_flags;
505 static enum {
506         JUNK_LEAVE_NONE,
507         JUNK_LEAVE_REPO,
508         JUNK_LEAVE_ALL
509 } junk_mode = JUNK_LEAVE_NONE;
510
511 static const char junk_leave_repo_msg[] =
512 N_("Clone succeeded, but checkout failed.\n"
513    "You can inspect what was checked out with 'git status'\n"
514    "and retry with 'git restore --source=HEAD :/'\n");
515
516 static void remove_junk(void)
517 {
518         struct strbuf sb = STRBUF_INIT;
519
520         switch (junk_mode) {
521         case JUNK_LEAVE_REPO:
522                 warning("%s", _(junk_leave_repo_msg));
523                 /* fall-through */
524         case JUNK_LEAVE_ALL:
525                 return;
526         default:
527                 /* proceed to removal */
528                 break;
529         }
530
531         if (junk_git_dir) {
532                 strbuf_addstr(&sb, junk_git_dir);
533                 remove_dir_recursively(&sb, junk_git_dir_flags);
534                 strbuf_reset(&sb);
535         }
536         if (junk_work_tree) {
537                 strbuf_addstr(&sb, junk_work_tree);
538                 remove_dir_recursively(&sb, junk_work_tree_flags);
539         }
540         strbuf_release(&sb);
541 }
542
543 static void remove_junk_on_signal(int signo)
544 {
545         remove_junk();
546         sigchain_pop(signo);
547         raise(signo);
548 }
549
550 static struct ref *find_remote_branch(const struct ref *refs, const char *branch)
551 {
552         struct ref *ref;
553         struct strbuf head = STRBUF_INIT;
554         strbuf_addstr(&head, "refs/heads/");
555         strbuf_addstr(&head, branch);
556         ref = find_ref_by_name(refs, head.buf);
557         strbuf_release(&head);
558
559         if (ref)
560                 return ref;
561
562         strbuf_addstr(&head, "refs/tags/");
563         strbuf_addstr(&head, branch);
564         ref = find_ref_by_name(refs, head.buf);
565         strbuf_release(&head);
566
567         return ref;
568 }
569
570 static struct ref *wanted_peer_refs(const struct ref *refs,
571                 struct refspec *refspec)
572 {
573         struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD"));
574         struct ref *local_refs = head;
575         struct ref **tail = head ? &head->next : &local_refs;
576
577         if (option_single_branch) {
578                 struct ref *remote_head = NULL;
579
580                 if (!option_branch)
581                         remote_head = guess_remote_head(head, refs, 0);
582                 else {
583                         local_refs = NULL;
584                         tail = &local_refs;
585                         remote_head = copy_ref(find_remote_branch(refs, option_branch));
586                 }
587
588                 if (!remote_head && option_branch)
589                         warning(_("Could not find remote branch %s to clone."),
590                                 option_branch);
591                 else {
592                         int i;
593                         for (i = 0; i < refspec->nr; i++)
594                                 get_fetch_map(remote_head, &refspec->items[i],
595                                               &tail, 0);
596
597                         /* if --branch=tag, pull the requested tag explicitly */
598                         get_fetch_map(remote_head, tag_refspec, &tail, 0);
599                 }
600         } else {
601                 int i;
602                 for (i = 0; i < refspec->nr; i++)
603                         get_fetch_map(refs, &refspec->items[i], &tail, 0);
604         }
605
606         if (!option_mirror && !option_single_branch && !option_no_tags)
607                 get_fetch_map(refs, tag_refspec, &tail, 0);
608
609         return local_refs;
610 }
611
612 static void write_remote_refs(const struct ref *local_refs)
613 {
614         const struct ref *r;
615
616         struct ref_transaction *t;
617         struct strbuf err = STRBUF_INIT;
618
619         t = ref_transaction_begin(&err);
620         if (!t)
621                 die("%s", err.buf);
622
623         for (r = local_refs; r; r = r->next) {
624                 if (!r->peer_ref)
625                         continue;
626                 if (ref_transaction_create(t, r->peer_ref->name, &r->old_oid,
627                                            0, NULL, &err))
628                         die("%s", err.buf);
629         }
630
631         if (initial_ref_transaction_commit(t, &err))
632                 die("%s", err.buf);
633
634         strbuf_release(&err);
635         ref_transaction_free(t);
636 }
637
638 static void write_followtags(const struct ref *refs, const char *msg)
639 {
640         const struct ref *ref;
641         for (ref = refs; ref; ref = ref->next) {
642                 if (!starts_with(ref->name, "refs/tags/"))
643                         continue;
644                 if (ends_with(ref->name, "^{}"))
645                         continue;
646                 if (!has_object_file_with_flags(&ref->old_oid,
647                                                 OBJECT_INFO_QUICK |
648                                                 OBJECT_INFO_SKIP_FETCH_OBJECT))
649                         continue;
650                 update_ref(msg, ref->name, &ref->old_oid, NULL, 0,
651                            UPDATE_REFS_DIE_ON_ERR);
652         }
653 }
654
655 static int iterate_ref_map(void *cb_data, struct object_id *oid)
656 {
657         struct ref **rm = cb_data;
658         struct ref *ref = *rm;
659
660         /*
661          * Skip anything missing a peer_ref, which we are not
662          * actually going to write a ref for.
663          */
664         while (ref && !ref->peer_ref)
665                 ref = ref->next;
666         /* Returning -1 notes "end of list" to the caller. */
667         if (!ref)
668                 return -1;
669
670         oidcpy(oid, &ref->old_oid);
671         *rm = ref->next;
672         return 0;
673 }
674
675 static void update_remote_refs(const struct ref *refs,
676                                const struct ref *mapped_refs,
677                                const struct ref *remote_head_points_at,
678                                const char *branch_top,
679                                const char *msg,
680                                struct transport *transport,
681                                int check_connectivity)
682 {
683         const struct ref *rm = mapped_refs;
684
685         if (check_connectivity) {
686                 struct check_connected_options opt = CHECK_CONNECTED_INIT;
687
688                 opt.transport = transport;
689                 opt.progress = transport->progress;
690
691                 if (check_connected(iterate_ref_map, &rm, &opt))
692                         die(_("remote did not send all necessary objects"));
693         }
694
695         if (refs) {
696                 write_remote_refs(mapped_refs);
697                 if (option_single_branch && !option_no_tags)
698                         write_followtags(refs, msg);
699         }
700
701         if (remote_head_points_at && !option_bare) {
702                 struct strbuf head_ref = STRBUF_INIT;
703                 strbuf_addstr(&head_ref, branch_top);
704                 strbuf_addstr(&head_ref, "HEAD");
705                 if (create_symref(head_ref.buf,
706                                   remote_head_points_at->peer_ref->name,
707                                   msg) < 0)
708                         die(_("unable to update %s"), head_ref.buf);
709                 strbuf_release(&head_ref);
710         }
711 }
712
713 static void update_head(const struct ref *our, const struct ref *remote,
714                         const char *msg)
715 {
716         const char *head;
717         if (our && skip_prefix(our->name, "refs/heads/", &head)) {
718                 /* Local default branch link */
719                 if (create_symref("HEAD", our->name, NULL) < 0)
720                         die(_("unable to update HEAD"));
721                 if (!option_bare) {
722                         update_ref(msg, "HEAD", &our->old_oid, NULL, 0,
723                                    UPDATE_REFS_DIE_ON_ERR);
724                         install_branch_config(0, head, option_origin, our->name);
725                 }
726         } else if (our) {
727                 struct commit *c = lookup_commit_reference(the_repository,
728                                                            &our->old_oid);
729                 /* --branch specifies a non-branch (i.e. tags), detach HEAD */
730                 update_ref(msg, "HEAD", &c->object.oid, NULL, REF_NO_DEREF,
731                            UPDATE_REFS_DIE_ON_ERR);
732         } else if (remote) {
733                 /*
734                  * We know remote HEAD points to a non-branch, or
735                  * HEAD points to a branch but we don't know which one.
736                  * Detach HEAD in all these cases.
737                  */
738                 update_ref(msg, "HEAD", &remote->old_oid, NULL, REF_NO_DEREF,
739                            UPDATE_REFS_DIE_ON_ERR);
740         }
741 }
742
743 static int git_sparse_checkout_init(const char *repo)
744 {
745         struct strvec argv = STRVEC_INIT;
746         int result = 0;
747         strvec_pushl(&argv, "-C", repo, "sparse-checkout", "init", NULL);
748
749         /*
750          * We must apply the setting in the current process
751          * for the later checkout to use the sparse-checkout file.
752          */
753         core_apply_sparse_checkout = 1;
754
755         if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
756                 error(_("failed to initialize sparse-checkout"));
757                 result = 1;
758         }
759
760         strvec_clear(&argv);
761         return result;
762 }
763
764 static int checkout(int submodule_progress)
765 {
766         struct object_id oid;
767         char *head;
768         struct lock_file lock_file = LOCK_INIT;
769         struct unpack_trees_options opts;
770         struct tree *tree;
771         struct tree_desc t;
772         int err = 0;
773
774         if (option_no_checkout)
775                 return 0;
776
777         head = resolve_refdup("HEAD", RESOLVE_REF_READING, &oid, NULL);
778         if (!head) {
779                 warning(_("remote HEAD refers to nonexistent ref, "
780                           "unable to checkout.\n"));
781                 return 0;
782         }
783         if (!strcmp(head, "HEAD")) {
784                 if (advice_detached_head)
785                         detach_advice(oid_to_hex(&oid));
786                 FREE_AND_NULL(head);
787         } else {
788                 if (!starts_with(head, "refs/heads/"))
789                         die(_("HEAD not found below refs/heads!"));
790         }
791
792         /* We need to be in the new work tree for the checkout */
793         setup_work_tree();
794
795         hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
796
797         memset(&opts, 0, sizeof opts);
798         opts.update = 1;
799         opts.merge = 1;
800         opts.clone = 1;
801         opts.fn = oneway_merge;
802         opts.verbose_update = (option_verbosity >= 0);
803         opts.src_index = &the_index;
804         opts.dst_index = &the_index;
805         init_checkout_metadata(&opts.meta, head, &oid, NULL);
806
807         tree = parse_tree_indirect(&oid);
808         parse_tree(tree);
809         init_tree_desc(&t, tree->buffer, tree->size);
810         if (unpack_trees(1, &t, &opts) < 0)
811                 die(_("unable to checkout working tree"));
812
813         free(head);
814
815         if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
816                 die(_("unable to write new index file"));
817
818         err |= run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid),
819                            oid_to_hex(&oid), "1", NULL);
820
821         if (!err && (option_recurse_submodules.nr > 0)) {
822                 struct strvec args = STRVEC_INIT;
823                 strvec_pushl(&args, "submodule", "update", "--require-init", "--recursive", NULL);
824
825                 if (option_shallow_submodules == 1)
826                         strvec_push(&args, "--depth=1");
827
828                 if (max_jobs != -1)
829                         strvec_pushf(&args, "--jobs=%d", max_jobs);
830
831                 if (submodule_progress)
832                         strvec_push(&args, "--progress");
833
834                 if (option_verbosity < 0)
835                         strvec_push(&args, "--quiet");
836
837                 if (option_remote_submodules) {
838                         strvec_push(&args, "--remote");
839                         strvec_push(&args, "--no-fetch");
840                 }
841
842                 if (option_single_branch >= 0)
843                         strvec_push(&args, option_single_branch ?
844                                                "--single-branch" :
845                                                "--no-single-branch");
846
847                 err = run_command_v_opt(args.v, RUN_GIT_CMD);
848                 strvec_clear(&args);
849         }
850
851         return err;
852 }
853
854 static int write_one_config(const char *key, const char *value, void *data)
855 {
856         return git_config_set_multivar_gently(key,
857                                               value ? value : "true",
858                                               CONFIG_REGEX_NONE, 0);
859 }
860
861 static void write_config(struct string_list *config)
862 {
863         int i;
864
865         for (i = 0; i < config->nr; i++) {
866                 if (git_config_parse_parameter(config->items[i].string,
867                                                write_one_config, NULL) < 0)
868                         die(_("unable to write parameters to config file"));
869         }
870 }
871
872 static void write_refspec_config(const char *src_ref_prefix,
873                 const struct ref *our_head_points_at,
874                 const struct ref *remote_head_points_at,
875                 struct strbuf *branch_top)
876 {
877         struct strbuf key = STRBUF_INIT;
878         struct strbuf value = STRBUF_INIT;
879
880         if (option_mirror || !option_bare) {
881                 if (option_single_branch && !option_mirror) {
882                         if (option_branch) {
883                                 if (starts_with(our_head_points_at->name, "refs/tags/"))
884                                         strbuf_addf(&value, "+%s:%s", our_head_points_at->name,
885                                                 our_head_points_at->name);
886                                 else
887                                         strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
888                                                 branch_top->buf, option_branch);
889                         } else if (remote_head_points_at) {
890                                 const char *head = remote_head_points_at->name;
891                                 if (!skip_prefix(head, "refs/heads/", &head))
892                                         BUG("remote HEAD points at non-head?");
893
894                                 strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name,
895                                                 branch_top->buf, head);
896                         }
897                         /*
898                          * otherwise, the next "git fetch" will
899                          * simply fetch from HEAD without updating
900                          * any remote-tracking branch, which is what
901                          * we want.
902                          */
903                 } else {
904                         strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf);
905                 }
906                 /* Configure the remote */
907                 if (value.len) {
908                         strbuf_addf(&key, "remote.%s.fetch", option_origin);
909                         git_config_set_multivar(key.buf, value.buf, "^$", 0);
910                         strbuf_reset(&key);
911
912                         if (option_mirror) {
913                                 strbuf_addf(&key, "remote.%s.mirror", option_origin);
914                                 git_config_set(key.buf, "true");
915                                 strbuf_reset(&key);
916                         }
917                 }
918         }
919
920         strbuf_release(&key);
921         strbuf_release(&value);
922 }
923
924 static void dissociate_from_references(void)
925 {
926         static const char* argv[] = { "repack", "-a", "-d", NULL };
927         char *alternates = git_pathdup("objects/info/alternates");
928
929         if (!access(alternates, F_OK)) {
930                 if (run_command_v_opt(argv, RUN_GIT_CMD|RUN_COMMAND_NO_STDIN))
931                         die(_("cannot repack to clean up"));
932                 if (unlink(alternates) && errno != ENOENT)
933                         die_errno(_("cannot unlink temporary alternates file"));
934         }
935         free(alternates);
936 }
937
938 static int path_exists(const char *path)
939 {
940         struct stat sb;
941         return !stat(path, &sb);
942 }
943
944 int cmd_clone(int argc, const char **argv, const char *prefix)
945 {
946         int is_bundle = 0, is_local;
947         const char *repo_name, *repo, *work_tree, *git_dir;
948         char *path, *dir, *display_repo = NULL;
949         int dest_exists, real_dest_exists = 0;
950         const struct ref *refs, *remote_head;
951         const struct ref *remote_head_points_at;
952         const struct ref *our_head_points_at;
953         struct ref *mapped_refs;
954         const struct ref *ref;
955         struct strbuf key = STRBUF_INIT;
956         struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
957         struct transport *transport = NULL;
958         const char *src_ref_prefix = "refs/heads/";
959         struct remote *remote;
960         int err = 0, complete_refs_before_fetch = 1;
961         int submodule_progress;
962
963         struct strvec ref_prefixes = STRVEC_INIT;
964
965         packet_trace_identity("clone");
966         argc = parse_options(argc, argv, prefix, builtin_clone_options,
967                              builtin_clone_usage, 0);
968
969         if (argc > 2)
970                 usage_msg_opt(_("Too many arguments."),
971                         builtin_clone_usage, builtin_clone_options);
972
973         if (argc == 0)
974                 usage_msg_opt(_("You must specify a repository to clone."),
975                         builtin_clone_usage, builtin_clone_options);
976
977         if (option_depth || option_since || option_not.nr)
978                 deepen = 1;
979         if (option_single_branch == -1)
980                 option_single_branch = deepen ? 1 : 0;
981
982         if (option_mirror)
983                 option_bare = 1;
984
985         if (option_bare) {
986                 if (option_origin)
987                         die(_("--bare and --origin %s options are incompatible."),
988                             option_origin);
989                 if (real_git_dir)
990                         die(_("--bare and --separate-git-dir are incompatible."));
991                 option_no_checkout = 1;
992         }
993
994         if (!option_origin)
995                 option_origin = "origin";
996
997         repo_name = argv[0];
998
999         path = get_repo_path(repo_name, &is_bundle);
1000         if (path)
1001                 repo = absolute_pathdup(repo_name);
1002         else if (strchr(repo_name, ':')) {
1003                 repo = repo_name;
1004                 display_repo = transport_anonymize_url(repo);
1005         } else
1006                 die(_("repository '%s' does not exist"), repo_name);
1007
1008         /* no need to be strict, transport_set_option() will validate it again */
1009         if (option_depth && atoi(option_depth) < 1)
1010                 die(_("depth %s is not a positive number"), option_depth);
1011
1012         if (argc == 2)
1013                 dir = xstrdup(argv[1]);
1014         else
1015                 dir = guess_dir_name(repo_name, is_bundle, option_bare);
1016         strip_trailing_slashes(dir);
1017
1018         dest_exists = path_exists(dir);
1019         if (dest_exists && !is_empty_dir(dir))
1020                 die(_("destination path '%s' already exists and is not "
1021                         "an empty directory."), dir);
1022
1023         if (real_git_dir) {
1024                 real_dest_exists = path_exists(real_git_dir);
1025                 if (real_dest_exists && !is_empty_dir(real_git_dir))
1026                         die(_("repository path '%s' already exists and is not "
1027                                 "an empty directory."), real_git_dir);
1028         }
1029
1030
1031         strbuf_addf(&reflog_msg, "clone: from %s",
1032                     display_repo ? display_repo : repo);
1033         free(display_repo);
1034
1035         if (option_bare)
1036                 work_tree = NULL;
1037         else {
1038                 work_tree = getenv("GIT_WORK_TREE");
1039                 if (work_tree && path_exists(work_tree))
1040                         die(_("working tree '%s' already exists."), work_tree);
1041         }
1042
1043         if (option_bare || work_tree)
1044                 git_dir = xstrdup(dir);
1045         else {
1046                 work_tree = dir;
1047                 git_dir = mkpathdup("%s/.git", dir);
1048         }
1049
1050         atexit(remove_junk);
1051         sigchain_push_common(remove_junk_on_signal);
1052
1053         if (!option_bare) {
1054                 if (safe_create_leading_directories_const(work_tree) < 0)
1055                         die_errno(_("could not create leading directories of '%s'"),
1056                                   work_tree);
1057                 if (dest_exists)
1058                         junk_work_tree_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
1059                 else if (mkdir(work_tree, 0777))
1060                         die_errno(_("could not create work tree dir '%s'"),
1061                                   work_tree);
1062                 junk_work_tree = work_tree;
1063                 set_git_work_tree(work_tree);
1064         }
1065
1066         if (real_git_dir) {
1067                 if (real_dest_exists)
1068                         junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
1069                 junk_git_dir = real_git_dir;
1070         } else {
1071                 if (dest_exists)
1072                         junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
1073                 junk_git_dir = git_dir;
1074         }
1075         if (safe_create_leading_directories_const(git_dir) < 0)
1076                 die(_("could not create leading directories of '%s'"), git_dir);
1077
1078         if (0 <= option_verbosity) {
1079                 if (option_bare)
1080                         fprintf(stderr, _("Cloning into bare repository '%s'...\n"), dir);
1081                 else
1082                         fprintf(stderr, _("Cloning into '%s'...\n"), dir);
1083         }
1084
1085         if (option_recurse_submodules.nr > 0) {
1086                 struct string_list_item *item;
1087                 struct strbuf sb = STRBUF_INIT;
1088
1089                 /* remove duplicates */
1090                 string_list_sort(&option_recurse_submodules);
1091                 string_list_remove_duplicates(&option_recurse_submodules, 0);
1092
1093                 /*
1094                  * NEEDSWORK: In a multi-working-tree world, this needs to be
1095                  * set in the per-worktree config.
1096                  */
1097                 for_each_string_list_item(item, &option_recurse_submodules) {
1098                         strbuf_addf(&sb, "submodule.active=%s",
1099                                     item->string);
1100                         string_list_append(&option_config,
1101                                            strbuf_detach(&sb, NULL));
1102                 }
1103
1104                 if (option_required_reference.nr &&
1105                     option_optional_reference.nr)
1106                         die(_("clone --recursive is not compatible with "
1107                               "both --reference and --reference-if-able"));
1108                 else if (option_required_reference.nr) {
1109                         string_list_append(&option_config,
1110                                 "submodule.alternateLocation=superproject");
1111                         string_list_append(&option_config,
1112                                 "submodule.alternateErrorStrategy=die");
1113                 } else if (option_optional_reference.nr) {
1114                         string_list_append(&option_config,
1115                                 "submodule.alternateLocation=superproject");
1116                         string_list_append(&option_config,
1117                                 "submodule.alternateErrorStrategy=info");
1118                 }
1119         }
1120
1121         init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, NULL,
1122                 INIT_DB_QUIET);
1123
1124         if (real_git_dir)
1125                 git_dir = real_git_dir;
1126
1127         write_config(&option_config);
1128
1129         git_config(git_default_config, NULL);
1130
1131         if (option_bare) {
1132                 if (option_mirror)
1133                         src_ref_prefix = "refs/";
1134                 strbuf_addstr(&branch_top, src_ref_prefix);
1135
1136                 git_config_set("core.bare", "true");
1137         } else {
1138                 strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
1139         }
1140
1141         strbuf_addf(&key, "remote.%s.url", option_origin);
1142         git_config_set(key.buf, repo);
1143         strbuf_reset(&key);
1144
1145         if (option_no_tags) {
1146                 strbuf_addf(&key, "remote.%s.tagOpt", option_origin);
1147                 git_config_set(key.buf, "--no-tags");
1148                 strbuf_reset(&key);
1149         }
1150
1151         if (option_required_reference.nr || option_optional_reference.nr)
1152                 setup_reference();
1153
1154         if (option_sparse_checkout && git_sparse_checkout_init(dir))
1155                 return 1;
1156
1157         remote = remote_get(option_origin);
1158
1159         refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix,
1160                         branch_top.buf);
1161
1162         transport = transport_get(remote, remote->url[0]);
1163         transport_set_verbosity(transport, option_verbosity, option_progress);
1164         transport->family = family;
1165
1166         path = get_repo_path(remote->url[0], &is_bundle);
1167         is_local = option_local != 0 && path && !is_bundle;
1168         if (is_local) {
1169                 if (option_depth)
1170                         warning(_("--depth is ignored in local clones; use file:// instead."));
1171                 if (option_since)
1172                         warning(_("--shallow-since is ignored in local clones; use file:// instead."));
1173                 if (option_not.nr)
1174                         warning(_("--shallow-exclude is ignored in local clones; use file:// instead."));
1175                 if (filter_options.choice)
1176                         warning(_("--filter is ignored in local clones; use file:// instead."));
1177                 if (!access(mkpath("%s/shallow", path), F_OK)) {
1178                         if (option_local > 0)
1179                                 warning(_("source repository is shallow, ignoring --local"));
1180                         is_local = 0;
1181                 }
1182         }
1183         if (option_local > 0 && !is_local)
1184                 warning(_("--local is ignored"));
1185         transport->cloning = 1;
1186
1187         transport_set_option(transport, TRANS_OPT_KEEP, "yes");
1188
1189         if (option_depth)
1190                 transport_set_option(transport, TRANS_OPT_DEPTH,
1191                                      option_depth);
1192         if (option_since)
1193                 transport_set_option(transport, TRANS_OPT_DEEPEN_SINCE,
1194                                      option_since);
1195         if (option_not.nr)
1196                 transport_set_option(transport, TRANS_OPT_DEEPEN_NOT,
1197                                      (const char *)&option_not);
1198         if (option_single_branch)
1199                 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
1200
1201         if (option_upload_pack)
1202                 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
1203                                      option_upload_pack);
1204
1205         if (server_options.nr)
1206                 transport->server_options = &server_options;
1207
1208         if (filter_options.choice) {
1209                 const char *spec =
1210                         expand_list_objects_filter_spec(&filter_options);
1211                 transport_set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER,
1212                                      spec);
1213                 transport_set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1214         }
1215
1216         if (transport->smart_options && !deepen && !filter_options.choice)
1217                 transport->smart_options->check_self_contained_and_connected = 1;
1218
1219
1220         strvec_push(&ref_prefixes, "HEAD");
1221         refspec_ref_prefixes(&remote->fetch, &ref_prefixes);
1222         if (option_branch)
1223                 expand_ref_prefix(&ref_prefixes, option_branch);
1224         if (!option_no_tags)
1225                 strvec_push(&ref_prefixes, "refs/tags/");
1226
1227         refs = transport_get_remote_refs(transport, &ref_prefixes);
1228
1229         if (refs) {
1230                 int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
1231
1232                 /*
1233                  * Now that we know what algorithm the remote side is using,
1234                  * let's set ours to the same thing.
1235                  */
1236                 initialize_repository_version(hash_algo, 1);
1237                 repo_set_hash_algo(the_repository, hash_algo);
1238
1239                 mapped_refs = wanted_peer_refs(refs, &remote->fetch);
1240                 /*
1241                  * transport_get_remote_refs() may return refs with null sha-1
1242                  * in mapped_refs (see struct transport->get_refs_list
1243                  * comment). In that case we need fetch it early because
1244                  * remote_head code below relies on it.
1245                  *
1246                  * for normal clones, transport_get_remote_refs() should
1247                  * return reliable ref set, we can delay cloning until after
1248                  * remote HEAD check.
1249                  */
1250                 for (ref = refs; ref; ref = ref->next)
1251                         if (is_null_oid(&ref->old_oid)) {
1252                                 complete_refs_before_fetch = 0;
1253                                 break;
1254                         }
1255
1256                 if (!is_local && !complete_refs_before_fetch)
1257                         transport_fetch_refs(transport, mapped_refs);
1258
1259                 remote_head = find_ref_by_name(refs, "HEAD");
1260                 remote_head_points_at =
1261                         guess_remote_head(remote_head, mapped_refs, 0);
1262
1263                 if (option_branch) {
1264                         our_head_points_at =
1265                                 find_remote_branch(mapped_refs, option_branch);
1266
1267                         if (!our_head_points_at)
1268                                 die(_("Remote branch %s not found in upstream %s"),
1269                                     option_branch, option_origin);
1270                 }
1271                 else
1272                         our_head_points_at = remote_head_points_at;
1273         }
1274         else {
1275                 if (option_branch)
1276                         die(_("Remote branch %s not found in upstream %s"),
1277                                         option_branch, option_origin);
1278
1279                 warning(_("You appear to have cloned an empty repository."));
1280                 mapped_refs = NULL;
1281                 our_head_points_at = NULL;
1282                 remote_head_points_at = NULL;
1283                 remote_head = NULL;
1284                 option_no_checkout = 1;
1285                 if (!option_bare) {
1286                         const char *branch = git_default_branch_name();
1287                         char *ref = xstrfmt("refs/heads/%s", branch);
1288
1289                         install_branch_config(0, branch, option_origin, ref);
1290                         free(ref);
1291                 }
1292         }
1293
1294         write_refspec_config(src_ref_prefix, our_head_points_at,
1295                         remote_head_points_at, &branch_top);
1296
1297         if (filter_options.choice)
1298                 partial_clone_register(option_origin, &filter_options);
1299
1300         if (is_local)
1301                 clone_local(path, git_dir);
1302         else if (refs && complete_refs_before_fetch)
1303                 transport_fetch_refs(transport, mapped_refs);
1304
1305         update_remote_refs(refs, mapped_refs, remote_head_points_at,
1306                            branch_top.buf, reflog_msg.buf, transport,
1307                            !is_local);
1308
1309         update_head(our_head_points_at, remote_head, reflog_msg.buf);
1310
1311         /*
1312          * We want to show progress for recursive submodule clones iff
1313          * we did so for the main clone. But only the transport knows
1314          * the final decision for this flag, so we need to rescue the value
1315          * before we free the transport.
1316          */
1317         submodule_progress = transport->progress;
1318
1319         transport_unlock_pack(transport);
1320         transport_disconnect(transport);
1321
1322         if (option_dissociate) {
1323                 close_object_store(the_repository->objects);
1324                 dissociate_from_references();
1325         }
1326
1327         junk_mode = JUNK_LEAVE_REPO;
1328         err = checkout(submodule_progress);
1329
1330         strbuf_release(&reflog_msg);
1331         strbuf_release(&branch_top);
1332         strbuf_release(&key);
1333         junk_mode = JUNK_LEAVE_ALL;
1334
1335         strvec_clear(&ref_prefixes);
1336         return err;
1337 }