]>
Commit | Line | Data |
---|---|---|
8434c2f1 DB |
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 | ||
c2e86add | 11 | #include "builtin.h" |
697cc8ef | 12 | #include "lockfile.h" |
8434c2f1 DB |
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" | |
4a16d072 | 22 | #include "sigchain.h" |
a9f2c136 | 23 | #include "branch.h" |
8ef51733 | 24 | #include "remote.h" |
dfa7a6c5 | 25 | #include "run-command.h" |
0433ad12 | 26 | #include "connected.h" |
8434c2f1 DB |
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[] = { | |
9c9b4f2f | 37 | N_("git clone [<options>] [--] <repo> [<dir>]"), |
8434c2f1 DB |
38 | NULL |
39 | }; | |
40 | ||
3e6e0edd | 41 | static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1; |
bb62e0a9 | 42 | static int option_local = -1, option_no_hardlinks, option_shared; |
18a74a09 | 43 | static int option_shallow_submodules; |
994c2aaf NTND |
44 | static int deepen; |
45 | static char *option_template, *option_depth, *option_since; | |
8434c2f1 | 46 | static char *option_origin = NULL; |
7a4ee28f | 47 | static char *option_branch = NULL; |
859e5df9 | 48 | static struct string_list option_not = STRING_LIST_INIT_NODUP; |
b57fb80a | 49 | static const char *real_git_dir; |
8434c2f1 | 50 | static char *option_upload_pack = "git-upload-pack"; |
5bd631b3 | 51 | static int option_verbosity; |
01fdc21f | 52 | static int option_progress = -1; |
c915f11e | 53 | static enum transport_family family; |
2721ce21 | 54 | static struct string_list option_config = STRING_LIST_INIT_NODUP; |
5e40800d | 55 | static struct string_list option_required_reference = STRING_LIST_INIT_NODUP; |
f7415b4d | 56 | static struct string_list option_optional_reference = STRING_LIST_INIT_NODUP; |
fb1d6dab | 57 | static int option_dissociate; |
72290d6a | 58 | static int max_jobs = -1; |
bb62e0a9 BW |
59 | static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP; |
60 | ||
61 | static int recurse_submodules_cb(const struct option *opt, | |
62 | const char *arg, int unset) | |
63 | { | |
64 | if (unset) | |
65 | string_list_clear((struct string_list *)opt->value, 0); | |
66 | else if (arg) | |
67 | string_list_append((struct string_list *)opt->value, arg); | |
68 | else | |
69 | string_list_append((struct string_list *)opt->value, | |
70 | (const char *)opt->defval); | |
71 | ||
72 | return 0; | |
73 | } | |
dbc92b07 | 74 | |
8434c2f1 | 75 | static struct option builtin_clone_options[] = { |
5bd631b3 | 76 | OPT__VERBOSITY(&option_verbosity), |
01fdc21f | 77 | OPT_BOOL(0, "progress", &option_progress, |
32b77add | 78 | N_("force progress reporting")), |
d5d09d47 SB |
79 | OPT_BOOL('n', "no-checkout", &option_no_checkout, |
80 | N_("don't create a checkout")), | |
4741edd5 SB |
81 | OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")), |
82 | OPT_HIDDEN_BOOL(0, "naked", &option_bare, | |
83 | N_("create a bare repository")), | |
d5d09d47 SB |
84 | OPT_BOOL(0, "mirror", &option_mirror, |
85 | N_("create a mirror repository (implies bare)")), | |
189260b1 | 86 | OPT_BOOL('l', "local", &option_local, |
32b77add | 87 | N_("to clone from a local repository")), |
d5d09d47 | 88 | OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks, |
32b77add | 89 | N_("don't use local hardlinks, always copy")), |
d5d09d47 | 90 | OPT_BOOL('s', "shared", &option_shared, |
32b77add | 91 | N_("setup as shared repository")), |
bb62e0a9 BW |
92 | { OPTION_CALLBACK, 0, "recursive", &option_recurse_submodules, |
93 | N_("pathspec"), N_("initialize submodules in the clone"), | |
94 | PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, recurse_submodules_cb, | |
95 | (intptr_t)"." }, | |
96 | { OPTION_CALLBACK, 0, "recurse-submodules", &option_recurse_submodules, | |
97 | N_("pathspec"), N_("initialize submodules in the clone"), | |
98 | PARSE_OPT_OPTARG, recurse_submodules_cb, (intptr_t)"." }, | |
72290d6a SB |
99 | OPT_INTEGER('j', "jobs", &max_jobs, |
100 | N_("number of submodules cloned in parallel")), | |
32b77add NTND |
101 | OPT_STRING(0, "template", &option_template, N_("template-directory"), |
102 | N_("directory from which templates will be used")), | |
5e40800d | 103 | OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"), |
8ade009c | 104 | N_("reference repository")), |
f7415b4d SB |
105 | OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference, |
106 | N_("repo"), N_("reference repository")), | |
14f8b9b4 JK |
107 | OPT_BOOL(0, "dissociate", &option_dissociate, |
108 | N_("use --reference only while cloning")), | |
32b77add NTND |
109 | OPT_STRING('o', "origin", &option_origin, N_("name"), |
110 | N_("use <name> instead of 'origin' to track upstream")), | |
111 | OPT_STRING('b', "branch", &option_branch, N_("branch"), | |
112 | N_("checkout <branch> instead of the remote's HEAD")), | |
113 | OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"), | |
114 | N_("path to git-upload-pack on the remote")), | |
115 | OPT_STRING(0, "depth", &option_depth, N_("depth"), | |
116 | N_("create a shallow clone of that depth")), | |
994c2aaf NTND |
117 | OPT_STRING(0, "shallow-since", &option_since, N_("time"), |
118 | N_("create a shallow clone since a specific time")), | |
859e5df9 | 119 | OPT_STRING_LIST(0, "shallow-exclude", &option_not, N_("revision"), |
6d873865 | 120 | N_("deepen history of shallow clone, excluding rev")), |
3e6e0edd | 121 | OPT_BOOL(0, "single-branch", &option_single_branch, |
32b77add | 122 | N_("clone only one branch, HEAD or --branch")), |
d22eb044 SB |
123 | OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules, |
124 | N_("any cloned submodules will be shallow")), | |
32b77add NTND |
125 | OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"), |
126 | N_("separate git dir from working tree")), | |
127 | OPT_STRING_LIST('c', "config", &option_config, N_("key=value"), | |
128 | N_("set config inside the new repository")), | |
c915f11e EW |
129 | OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"), |
130 | TRANSPORT_FAMILY_IPV4), | |
131 | OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"), | |
132 | TRANSPORT_FAMILY_IPV6), | |
8434c2f1 DB |
133 | OPT_END() |
134 | }; | |
135 | ||
0ea68e42 | 136 | static const char *get_repo_path_1(struct strbuf *path, int *is_bundle) |
8434c2f1 | 137 | { |
b3256eb8 | 138 | static char *suffix[] = { "/.git", "", ".git/.git", ".git" }; |
8434c2f1 | 139 | static char *bundle_suffix[] = { ".bundle", "" }; |
0ea68e42 | 140 | size_t baselen = path->len; |
8434c2f1 DB |
141 | struct stat st; |
142 | int i; | |
143 | ||
144 | for (i = 0; i < ARRAY_SIZE(suffix); i++) { | |
0ea68e42 JK |
145 | strbuf_setlen(path, baselen); |
146 | strbuf_addstr(path, suffix[i]); | |
147 | if (stat(path->buf, &st)) | |
9b0ebc72 | 148 | continue; |
0ea68e42 | 149 | if (S_ISDIR(st.st_mode) && is_git_directory(path->buf)) { |
8434c2f1 | 150 | *is_bundle = 0; |
0ea68e42 | 151 | return path->buf; |
9b0ebc72 NTND |
152 | } else if (S_ISREG(st.st_mode) && st.st_size > 8) { |
153 | /* Is it a "gitfile"? */ | |
154 | char signature[8]; | |
0ea68e42 JK |
155 | const char *dst; |
156 | int len, fd = open(path->buf, O_RDONLY); | |
9b0ebc72 NTND |
157 | if (fd < 0) |
158 | continue; | |
159 | len = read_in_full(fd, signature, 8); | |
160 | close(fd); | |
161 | if (len != 8 || strncmp(signature, "gitdir: ", 8)) | |
162 | continue; | |
0ea68e42 JK |
163 | dst = read_gitfile(path->buf); |
164 | if (dst) { | |
9b0ebc72 | 165 | *is_bundle = 0; |
0ea68e42 | 166 | return dst; |
9b0ebc72 | 167 | } |
8434c2f1 DB |
168 | } |
169 | } | |
170 | ||
171 | for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) { | |
0ea68e42 JK |
172 | strbuf_setlen(path, baselen); |
173 | strbuf_addstr(path, bundle_suffix[i]); | |
174 | if (!stat(path->buf, &st) && S_ISREG(st.st_mode)) { | |
8434c2f1 | 175 | *is_bundle = 1; |
0ea68e42 | 176 | return path->buf; |
8434c2f1 DB |
177 | } |
178 | } | |
179 | ||
180 | return NULL; | |
181 | } | |
182 | ||
0ea68e42 JK |
183 | static char *get_repo_path(const char *repo, int *is_bundle) |
184 | { | |
185 | struct strbuf path = STRBUF_INIT; | |
186 | const char *raw; | |
187 | char *canon; | |
188 | ||
189 | strbuf_addstr(&path, repo); | |
190 | raw = get_repo_path_1(&path, is_bundle); | |
0aaad415 | 191 | canon = raw ? absolute_pathdup(raw) : NULL; |
0ea68e42 JK |
192 | strbuf_release(&path); |
193 | return canon; | |
194 | } | |
195 | ||
6612f877 | 196 | static char *guess_dir_name(const char *repo, int is_bundle, int is_bare) |
8434c2f1 | 197 | { |
e8959867 | 198 | const char *end = repo + strlen(repo), *start, *ptr; |
7e837c64 | 199 | size_t len; |
8a94bc7b | 200 | char *dir; |
b8c5db35 | 201 | |
e8959867 PS |
202 | /* |
203 | * Skip scheme. | |
204 | */ | |
205 | start = strstr(repo, "://"); | |
206 | if (start == NULL) | |
207 | start = repo; | |
208 | else | |
209 | start += 3; | |
210 | ||
211 | /* | |
212 | * Skip authentication data. The stripping does happen | |
213 | * greedily, such that we strip up to the last '@' inside | |
214 | * the host part. | |
215 | */ | |
216 | for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) { | |
217 | if (*ptr == '@') | |
218 | start = ptr + 1; | |
219 | } | |
220 | ||
b8c5db35 | 221 | /* |
8a94bc7b | 222 | * Strip trailing spaces, slashes and /.git |
b8c5db35 | 223 | */ |
e8959867 | 224 | while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1]))) |
b8c5db35 | 225 | end--; |
e8959867 | 226 | if (end - start > 5 && is_dir_sep(end[-5]) && |
b8c5db35 JS |
227 | !strncmp(end - 4, ".git", 4)) { |
228 | end -= 5; | |
e8959867 | 229 | while (start < end && is_dir_sep(end[-1])) |
b8c5db35 JS |
230 | end--; |
231 | } | |
232 | ||
233 | /* | |
92722efe PS |
234 | * Strip trailing port number if we've got only a |
235 | * hostname (that is, there is no dir separator but a | |
236 | * colon). This check is required such that we do not | |
237 | * strip URI's like '/foo/bar:2222.git', which should | |
238 | * result in a dir '2222' being guessed due to backwards | |
239 | * compatibility. | |
240 | */ | |
241 | if (memchr(start, '/', end - start) == NULL | |
242 | && memchr(start, ':', end - start) != NULL) { | |
243 | ptr = end; | |
244 | while (start < ptr && isdigit(ptr[-1]) && ptr[-1] != ':') | |
245 | ptr--; | |
246 | if (start < ptr && ptr[-1] == ':') | |
247 | end = ptr - 1; | |
248 | } | |
249 | ||
b8c5db35 | 250 | /* |
e8959867 PS |
251 | * Find last component. To remain backwards compatible we |
252 | * also regard colons as path separators, such that | |
253 | * cloning a repository 'foo:bar.git' would result in a | |
254 | * directory 'bar' being guessed. | |
b8c5db35 | 255 | */ |
e8959867 PS |
256 | ptr = end; |
257 | while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':') | |
258 | ptr--; | |
259 | start = ptr; | |
b8c5db35 JS |
260 | |
261 | /* | |
262 | * Strip .{bundle,git}. | |
263 | */ | |
db2e2204 JK |
264 | len = end - start; |
265 | strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git"); | |
8434c2f1 | 266 | |
adef9561 | 267 | if (!len || (len == 1 && *start == '/')) |
39ad4f39 NTND |
268 | die(_("No directory name could be guessed.\n" |
269 | "Please specify a directory on the command line")); | |
adef9561 | 270 | |
7e837c64 SS |
271 | if (is_bare) |
272 | dir = xstrfmt("%.*s.git", (int)len, start); | |
273 | else | |
274 | dir = xstrndup(start, len); | |
8a94bc7b AR |
275 | /* |
276 | * Replace sequences of 'control' characters and whitespace | |
277 | * with one ascii space, remove leading and trailing spaces. | |
278 | */ | |
279 | if (*dir) { | |
280 | char *out = dir; | |
281 | int prev_space = 1 /* strip leading whitespace */; | |
282 | for (end = dir; *end; ++end) { | |
283 | char ch = *end; | |
284 | if ((unsigned char)ch < '\x20') | |
285 | ch = '\x20'; | |
286 | if (isspace(ch)) { | |
287 | if (prev_space) | |
288 | continue; | |
289 | prev_space = 1; | |
290 | } else | |
291 | prev_space = 0; | |
292 | *out++ = ch; | |
293 | } | |
294 | *out = '\0'; | |
295 | if (out > dir && prev_space) | |
296 | out[-1] = '\0'; | |
6612f877 | 297 | } |
8a94bc7b | 298 | return dir; |
8434c2f1 DB |
299 | } |
300 | ||
44a68fd5 CB |
301 | static void strip_trailing_slashes(char *dir) |
302 | { | |
303 | char *end = dir + strlen(dir); | |
304 | ||
305 | while (dir < end - 1 && is_dir_sep(end[-1])) | |
306 | end--; | |
307 | *end = '\0'; | |
308 | } | |
309 | ||
dbc92b07 | 310 | static int add_one_reference(struct string_list_item *item, void *cb_data) |
8434c2f1 | 311 | { |
9eeea7d2 | 312 | struct strbuf err = STRBUF_INIT; |
f7415b4d | 313 | int *required = cb_data; |
9eeea7d2 | 314 | char *ref_git = compute_alternate_path(item->string, &err); |
8434c2f1 | 315 | |
f7415b4d SB |
316 | if (!ref_git) { |
317 | if (*required) | |
318 | die("%s", err.buf); | |
319 | else | |
320 | fprintf(stderr, | |
321 | _("info: Could not add alternate for '%s': %s\n"), | |
322 | item->string, err.buf); | |
323 | } else { | |
324 | struct strbuf sb = STRBUF_INIT; | |
325 | strbuf_addf(&sb, "%s/objects", ref_git); | |
326 | add_to_alternates_file(sb.buf); | |
327 | strbuf_release(&sb); | |
328 | } | |
606e435a | 329 | |
9eeea7d2 | 330 | strbuf_release(&err); |
f7415b4d | 331 | free(ref_git); |
dbc92b07 JH |
332 | return 0; |
333 | } | |
8434c2f1 | 334 | |
dbc92b07 JH |
335 | static void setup_reference(void) |
336 | { | |
f7415b4d SB |
337 | int required = 1; |
338 | for_each_string_list(&option_required_reference, | |
339 | add_one_reference, &required); | |
340 | required = 0; | |
341 | for_each_string_list(&option_optional_reference, | |
342 | add_one_reference, &required); | |
8434c2f1 DB |
343 | } |
344 | ||
e6baf4a1 JH |
345 | static void copy_alternates(struct strbuf *src, struct strbuf *dst, |
346 | const char *src_repo) | |
347 | { | |
348 | /* | |
349 | * Read from the source objects/info/alternates file | |
350 | * and copy the entries to corresponding file in the | |
351 | * destination repository with add_to_alternates_file(). | |
352 | * Both src and dst have "$path/objects/info/alternates". | |
353 | * | |
354 | * Instead of copying bit-for-bit from the original, | |
355 | * we need to append to existing one so that the already | |
356 | * created entry via "clone -s" is not lost, and also | |
357 | * to turn entries with paths relative to the original | |
358 | * absolute, so that they can be used in the new repository. | |
359 | */ | |
360 | FILE *in = fopen(src->buf, "r"); | |
361 | struct strbuf line = STRBUF_INIT; | |
362 | ||
3f163962 | 363 | while (strbuf_getline(&line, in) != EOF) { |
dcf69262 | 364 | char *abs_path; |
e6baf4a1 JH |
365 | if (!line.len || line.buf[0] == '#') |
366 | continue; | |
367 | if (is_absolute_path(line.buf)) { | |
368 | add_to_alternates_file(line.buf); | |
369 | continue; | |
370 | } | |
dcf69262 | 371 | abs_path = mkpathdup("%s/objects/%s", src_repo, line.buf); |
22d3b8de JK |
372 | if (!normalize_path_copy(abs_path, abs_path)) |
373 | add_to_alternates_file(abs_path); | |
374 | else | |
375 | warning("skipping invalid relative alternate: %s/%s", | |
376 | src_repo, line.buf); | |
dcf69262 | 377 | free(abs_path); |
e6baf4a1 JH |
378 | } |
379 | strbuf_release(&line); | |
380 | fclose(in); | |
381 | } | |
382 | ||
383 | static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest, | |
384 | const char *src_repo, int src_baselen) | |
8434c2f1 DB |
385 | { |
386 | struct dirent *de; | |
387 | struct stat buf; | |
388 | int src_len, dest_len; | |
389 | DIR *dir; | |
390 | ||
b9e125e0 | 391 | dir = opendir(src->buf); |
8434c2f1 | 392 | if (!dir) |
e84d7b74 | 393 | die_errno(_("failed to open '%s'"), src->buf); |
8434c2f1 | 394 | |
b9e125e0 | 395 | if (mkdir(dest->buf, 0777)) { |
8434c2f1 | 396 | if (errno != EEXIST) |
e84d7b74 | 397 | die_errno(_("failed to create directory '%s'"), dest->buf); |
b9e125e0 | 398 | else if (stat(dest->buf, &buf)) |
e84d7b74 | 399 | die_errno(_("failed to stat '%s'"), dest->buf); |
8434c2f1 | 400 | else if (!S_ISDIR(buf.st_mode)) |
e84d7b74 | 401 | die(_("%s exists and is not a directory"), dest->buf); |
8434c2f1 DB |
402 | } |
403 | ||
b9e125e0 MV |
404 | strbuf_addch(src, '/'); |
405 | src_len = src->len; | |
406 | strbuf_addch(dest, '/'); | |
407 | dest_len = dest->len; | |
8434c2f1 DB |
408 | |
409 | while ((de = readdir(dir)) != NULL) { | |
b9e125e0 MV |
410 | strbuf_setlen(src, src_len); |
411 | strbuf_addstr(src, de->d_name); | |
412 | strbuf_setlen(dest, dest_len); | |
413 | strbuf_addstr(dest, de->d_name); | |
414 | if (stat(src->buf, &buf)) { | |
e84d7b74 | 415 | warning (_("failed to stat %s\n"), src->buf); |
8434c2f1 DB |
416 | continue; |
417 | } | |
418 | if (S_ISDIR(buf.st_mode)) { | |
419 | if (de->d_name[0] != '.') | |
e6baf4a1 JH |
420 | copy_or_link_directory(src, dest, |
421 | src_repo, src_baselen); | |
422 | continue; | |
423 | } | |
424 | ||
425 | /* Files that cannot be copied bit-for-bit... */ | |
426 | if (!strcmp(src->buf + src_baselen, "/info/alternates")) { | |
427 | copy_alternates(src, dest, src_repo); | |
8434c2f1 DB |
428 | continue; |
429 | } | |
430 | ||
b9e125e0 | 431 | if (unlink(dest->buf) && errno != ENOENT) |
e84d7b74 | 432 | die_errno(_("failed to unlink '%s'"), dest->buf); |
fdabc242 | 433 | if (!option_no_hardlinks) { |
b9e125e0 | 434 | if (!link(src->buf, dest->buf)) |
fdabc242 | 435 | continue; |
189260b1 | 436 | if (option_local > 0) |
e84d7b74 | 437 | die_errno(_("failed to create link '%s'"), dest->buf); |
fdabc242 | 438 | option_no_hardlinks = 1; |
8434c2f1 | 439 | } |
f7835a25 | 440 | if (copy_file_with_time(dest->buf, src->buf, 0666)) |
e84d7b74 | 441 | die_errno(_("failed to copy file to '%s'"), dest->buf); |
8434c2f1 | 442 | } |
689ef4d4 | 443 | closedir(dir); |
8434c2f1 DB |
444 | } |
445 | ||
6f48d39f | 446 | static void clone_local(const char *src_repo, const char *dest_repo) |
8434c2f1 | 447 | { |
e6baf4a1 JH |
448 | if (option_shared) { |
449 | struct strbuf alt = STRBUF_INIT; | |
450 | strbuf_addf(&alt, "%s/objects", src_repo); | |
451 | add_to_alternates_file(alt.buf); | |
452 | strbuf_release(&alt); | |
453 | } else { | |
454 | struct strbuf src = STRBUF_INIT; | |
455 | struct strbuf dest = STRBUF_INIT; | |
744e4697 NTND |
456 | get_common_dir(&src, src_repo); |
457 | get_common_dir(&dest, dest_repo); | |
458 | strbuf_addstr(&src, "/objects"); | |
459 | strbuf_addstr(&dest, "/objects"); | |
e6baf4a1 | 460 | copy_or_link_directory(&src, &dest, src_repo, src.len); |
b9e125e0 MV |
461 | strbuf_release(&src); |
462 | strbuf_release(&dest); | |
8434c2f1 DB |
463 | } |
464 | ||
28ba96ab | 465 | if (0 <= option_verbosity) |
68b939b2 | 466 | fprintf(stderr, _("done.\n")); |
8434c2f1 DB |
467 | } |
468 | ||
469 | static const char *junk_work_tree; | |
470 | static const char *junk_git_dir; | |
85064630 | 471 | static enum { |
d3b34622 JK |
472 | JUNK_LEAVE_NONE, |
473 | JUNK_LEAVE_REPO, | |
474 | JUNK_LEAVE_ALL | |
475 | } junk_mode = JUNK_LEAVE_NONE; | |
476 | ||
477 | static const char junk_leave_repo_msg[] = | |
478 | N_("Clone succeeded, but checkout failed.\n" | |
479 | "You can inspect what was checked out with 'git status'\n" | |
480 | "and retry the checkout with 'git checkout -f HEAD'\n"); | |
8434c2f1 DB |
481 | |
482 | static void remove_junk(void) | |
483 | { | |
f285a2d7 | 484 | struct strbuf sb = STRBUF_INIT; |
d3b34622 JK |
485 | |
486 | switch (junk_mode) { | |
487 | case JUNK_LEAVE_REPO: | |
488 | warning("%s", _(junk_leave_repo_msg)); | |
489 | /* fall-through */ | |
490 | case JUNK_LEAVE_ALL: | |
491 | return; | |
492 | default: | |
493 | /* proceed to removal */ | |
494 | break; | |
495 | } | |
496 | ||
8434c2f1 DB |
497 | if (junk_git_dir) { |
498 | strbuf_addstr(&sb, junk_git_dir); | |
499 | remove_dir_recursively(&sb, 0); | |
500 | strbuf_reset(&sb); | |
501 | } | |
502 | if (junk_work_tree) { | |
503 | strbuf_addstr(&sb, junk_work_tree); | |
504 | remove_dir_recursively(&sb, 0); | |
505 | strbuf_reset(&sb); | |
506 | } | |
507 | } | |
508 | ||
509 | static void remove_junk_on_signal(int signo) | |
510 | { | |
511 | remove_junk(); | |
4a16d072 | 512 | sigchain_pop(signo); |
8434c2f1 DB |
513 | raise(signo); |
514 | } | |
515 | ||
9e585046 NTND |
516 | static struct ref *find_remote_branch(const struct ref *refs, const char *branch) |
517 | { | |
518 | struct ref *ref; | |
519 | struct strbuf head = STRBUF_INIT; | |
520 | strbuf_addstr(&head, "refs/heads/"); | |
521 | strbuf_addstr(&head, branch); | |
522 | ref = find_ref_by_name(refs, head.buf); | |
523 | strbuf_release(&head); | |
5a7d5b68 NTND |
524 | |
525 | if (ref) | |
526 | return ref; | |
527 | ||
528 | strbuf_addstr(&head, "refs/tags/"); | |
529 | strbuf_addstr(&head, branch); | |
530 | ref = find_ref_by_name(refs, head.buf); | |
531 | strbuf_release(&head); | |
532 | ||
9e585046 NTND |
533 | return ref; |
534 | } | |
535 | ||
5bdc32d3 NP |
536 | static struct ref *wanted_peer_refs(const struct ref *refs, |
537 | struct refspec *refspec) | |
8434c2f1 | 538 | { |
c1921c18 JK |
539 | struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD")); |
540 | struct ref *local_refs = head; | |
541 | struct ref **tail = head ? &head->next : &local_refs; | |
8434c2f1 | 542 | |
3e6e0edd NTND |
543 | if (option_single_branch) { |
544 | struct ref *remote_head = NULL; | |
545 | ||
546 | if (!option_branch) | |
547 | remote_head = guess_remote_head(head, refs, 0); | |
0ec4b165 NTND |
548 | else { |
549 | local_refs = NULL; | |
550 | tail = &local_refs; | |
551 | remote_head = copy_ref(find_remote_branch(refs, option_branch)); | |
552 | } | |
3e6e0edd NTND |
553 | |
554 | if (!remote_head && option_branch) | |
555 | warning(_("Could not find remote branch %s to clone."), | |
556 | option_branch); | |
5a7d5b68 | 557 | else { |
3e6e0edd | 558 | get_fetch_map(remote_head, refspec, &tail, 0); |
5a7d5b68 NTND |
559 | |
560 | /* if --branch=tag, pull the requested tag explicitly */ | |
561 | get_fetch_map(remote_head, tag_refspec, &tail, 0); | |
562 | } | |
3e6e0edd NTND |
563 | } else |
564 | get_fetch_map(refs, refspec, &tail, 0); | |
565 | ||
566 | if (!option_mirror && !option_single_branch) | |
468386a9 | 567 | get_fetch_map(refs, tag_refspec, &tail, 0); |
8434c2f1 | 568 | |
5bdc32d3 NP |
569 | return local_refs; |
570 | } | |
571 | ||
572 | static void write_remote_refs(const struct ref *local_refs) | |
573 | { | |
574 | const struct ref *r; | |
575 | ||
58f233ce MH |
576 | struct ref_transaction *t; |
577 | struct strbuf err = STRBUF_INIT; | |
578 | ||
579 | t = ref_transaction_begin(&err); | |
580 | if (!t) | |
581 | die("%s", err.buf); | |
9f69d297 | 582 | |
c1921c18 JK |
583 | for (r = local_refs; r; r = r->next) { |
584 | if (!r->peer_ref) | |
585 | continue; | |
f4e54d02 | 586 | if (ref_transaction_create(t, r->peer_ref->name, r->old_oid.hash, |
58f233ce MH |
587 | 0, NULL, &err)) |
588 | die("%s", err.buf); | |
c1921c18 | 589 | } |
3e8aded2 | 590 | |
58f233ce MH |
591 | if (initial_ref_transaction_commit(t, &err)) |
592 | die("%s", err.buf); | |
593 | ||
594 | strbuf_release(&err); | |
595 | ref_transaction_free(t); | |
8434c2f1 DB |
596 | } |
597 | ||
3e6e0edd NTND |
598 | static void write_followtags(const struct ref *refs, const char *msg) |
599 | { | |
600 | const struct ref *ref; | |
601 | for (ref = refs; ref; ref = ref->next) { | |
59556548 | 602 | if (!starts_with(ref->name, "refs/tags/")) |
3e6e0edd | 603 | continue; |
59556548 | 604 | if (ends_with(ref->name, "^{}")) |
3e6e0edd | 605 | continue; |
f4e54d02 | 606 | if (!has_object_file(&ref->old_oid)) |
3e6e0edd | 607 | continue; |
f4e54d02 | 608 | update_ref(msg, ref->name, ref->old_oid.hash, |
f4124112 | 609 | NULL, 0, UPDATE_REFS_DIE_ON_ERR); |
3e6e0edd NTND |
610 | } |
611 | } | |
612 | ||
0433ad12 JK |
613 | static int iterate_ref_map(void *cb_data, unsigned char sha1[20]) |
614 | { | |
615 | struct ref **rm = cb_data; | |
616 | struct ref *ref = *rm; | |
617 | ||
618 | /* | |
619 | * Skip anything missing a peer_ref, which we are not | |
620 | * actually going to write a ref for. | |
621 | */ | |
622 | while (ref && !ref->peer_ref) | |
623 | ref = ref->next; | |
624 | /* Returning -1 notes "end of list" to the caller. */ | |
625 | if (!ref) | |
626 | return -1; | |
627 | ||
f4e54d02 | 628 | hashcpy(sha1, ref->old_oid.hash); |
0433ad12 JK |
629 | *rm = ref->next; |
630 | return 0; | |
631 | } | |
632 | ||
960b7d1c NTND |
633 | static void update_remote_refs(const struct ref *refs, |
634 | const struct ref *mapped_refs, | |
635 | const struct ref *remote_head_points_at, | |
636 | const char *branch_top, | |
c6807a40 | 637 | const char *msg, |
45ed4afa | 638 | struct transport *transport, |
125a05fd | 639 | int check_connectivity) |
960b7d1c | 640 | { |
0433ad12 JK |
641 | const struct ref *rm = mapped_refs; |
642 | ||
125a05fd | 643 | if (check_connectivity) { |
7043c707 JK |
644 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
645 | ||
646 | opt.transport = transport; | |
38e590ea | 647 | opt.progress = transport->progress; |
7043c707 | 648 | |
7043c707 | 649 | if (check_connected(iterate_ref_map, &rm, &opt)) |
125a05fd JK |
650 | die(_("remote did not send all necessary objects")); |
651 | } | |
0433ad12 | 652 | |
960b7d1c | 653 | if (refs) { |
960b7d1c NTND |
654 | write_remote_refs(mapped_refs); |
655 | if (option_single_branch) | |
656 | write_followtags(refs, msg); | |
657 | } | |
658 | ||
659 | if (remote_head_points_at && !option_bare) { | |
660 | struct strbuf head_ref = STRBUF_INIT; | |
661 | strbuf_addstr(&head_ref, branch_top); | |
662 | strbuf_addstr(&head_ref, "HEAD"); | |
4be49d75 JK |
663 | if (create_symref(head_ref.buf, |
664 | remote_head_points_at->peer_ref->name, | |
665 | msg) < 0) | |
39ad4f39 | 666 | die(_("unable to update %s"), head_ref.buf); |
4be49d75 | 667 | strbuf_release(&head_ref); |
960b7d1c NTND |
668 | } |
669 | } | |
670 | ||
f034d354 NTND |
671 | static void update_head(const struct ref *our, const struct ref *remote, |
672 | const char *msg) | |
673 | { | |
cf4fff57 JK |
674 | const char *head; |
675 | if (our && skip_prefix(our->name, "refs/heads/", &head)) { | |
f034d354 | 676 | /* Local default branch link */ |
4be49d75 | 677 | if (create_symref("HEAD", our->name, NULL) < 0) |
39ad4f39 | 678 | die(_("unable to update HEAD")); |
f034d354 | 679 | if (!option_bare) { |
f4e54d02 | 680 | update_ref(msg, "HEAD", our->old_oid.hash, NULL, 0, |
f4124112 | 681 | UPDATE_REFS_DIE_ON_ERR); |
f034d354 NTND |
682 | install_branch_config(0, head, option_origin, our->name); |
683 | } | |
5a7d5b68 | 684 | } else if (our) { |
f4e54d02 | 685 | struct commit *c = lookup_commit_reference(our->old_oid.hash); |
5a7d5b68 | 686 | /* --branch specifies a non-branch (i.e. tags), detach HEAD */ |
ed1c9977 | 687 | update_ref(msg, "HEAD", c->object.oid.hash, |
f4124112 | 688 | NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR); |
f034d354 NTND |
689 | } else if (remote) { |
690 | /* | |
691 | * We know remote HEAD points to a non-branch, or | |
920b691f | 692 | * HEAD points to a branch but we don't know which one. |
f034d354 NTND |
693 | * Detach HEAD in all these cases. |
694 | */ | |
f4e54d02 | 695 | update_ref(msg, "HEAD", remote->old_oid.hash, |
f4124112 | 696 | NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR); |
f034d354 NTND |
697 | } |
698 | } | |
699 | ||
72c5f883 | 700 | static int checkout(int submodule_progress) |
c39852c1 | 701 | { |
ddc2cc64 | 702 | struct object_id oid; |
c39852c1 NTND |
703 | char *head; |
704 | struct lock_file *lock_file; | |
705 | struct unpack_trees_options opts; | |
706 | struct tree *tree; | |
707 | struct tree_desc t; | |
03b86647 | 708 | int err = 0; |
c39852c1 NTND |
709 | |
710 | if (option_no_checkout) | |
711 | return 0; | |
712 | ||
ddc2cc64 | 713 | head = resolve_refdup("HEAD", RESOLVE_REF_READING, oid.hash, NULL); |
c39852c1 NTND |
714 | if (!head) { |
715 | warning(_("remote HEAD refers to nonexistent ref, " | |
716 | "unable to checkout.\n")); | |
717 | return 0; | |
718 | } | |
2857093b NTND |
719 | if (!strcmp(head, "HEAD")) { |
720 | if (advice_detached_head) | |
ddc2cc64 | 721 | detach_advice(oid_to_hex(&oid)); |
2857093b | 722 | } else { |
59556548 | 723 | if (!starts_with(head, "refs/heads/")) |
c39852c1 NTND |
724 | die(_("HEAD not found below refs/heads!")); |
725 | } | |
726 | free(head); | |
727 | ||
728 | /* We need to be in the new work tree for the checkout */ | |
729 | setup_work_tree(); | |
730 | ||
731 | lock_file = xcalloc(1, sizeof(struct lock_file)); | |
b3e83cc7 | 732 | hold_locked_index(lock_file, LOCK_DIE_ON_ERROR); |
c39852c1 NTND |
733 | |
734 | memset(&opts, 0, sizeof opts); | |
735 | opts.update = 1; | |
736 | opts.merge = 1; | |
737 | opts.fn = oneway_merge; | |
8f63da13 | 738 | opts.verbose_update = (option_verbosity >= 0); |
c39852c1 NTND |
739 | opts.src_index = &the_index; |
740 | opts.dst_index = &the_index; | |
741 | ||
ddc2cc64 | 742 | tree = parse_tree_indirect(oid.hash); |
c39852c1 NTND |
743 | parse_tree(tree); |
744 | init_tree_desc(&t, tree->buffer, tree->size); | |
0aac7bb2 JK |
745 | if (unpack_trees(1, &t, &opts) < 0) |
746 | die(_("unable to checkout working tree")); | |
c39852c1 | 747 | |
03b86647 | 748 | if (write_locked_index(&the_index, lock_file, COMMIT_LOCK)) |
c39852c1 NTND |
749 | die(_("unable to write new index file")); |
750 | ||
15048f8a | 751 | err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1), |
ddc2cc64 | 752 | oid_to_hex(&oid), "1", NULL); |
c39852c1 | 753 | |
bb62e0a9 | 754 | if (!err && (option_recurse_submodules.nr > 0)) { |
72290d6a SB |
755 | struct argv_array args = ARGV_ARRAY_INIT; |
756 | argv_array_pushl(&args, "submodule", "update", "--init", "--recursive", NULL); | |
757 | ||
18a74a09 | 758 | if (option_shallow_submodules == 1) |
d22eb044 SB |
759 | argv_array_push(&args, "--depth=1"); |
760 | ||
72290d6a SB |
761 | if (max_jobs != -1) |
762 | argv_array_pushf(&args, "--jobs=%d", max_jobs); | |
763 | ||
72c5f883 JK |
764 | if (submodule_progress) |
765 | argv_array_push(&args, "--progress"); | |
766 | ||
72290d6a SB |
767 | err = run_command_v_opt(args.argv, RUN_GIT_CMD); |
768 | argv_array_clear(&args); | |
769 | } | |
c39852c1 NTND |
770 | |
771 | return err; | |
772 | } | |
773 | ||
84054f79 JK |
774 | static int write_one_config(const char *key, const char *value, void *data) |
775 | { | |
db4eca1f JN |
776 | return git_config_set_multivar_gently(key, |
777 | value ? value : "true", | |
778 | CONFIG_REGEX_NONE, 0); | |
84054f79 JK |
779 | } |
780 | ||
781 | static void write_config(struct string_list *config) | |
782 | { | |
783 | int i; | |
784 | ||
785 | for (i = 0; i < config->nr; i++) { | |
786 | if (git_config_parse_parameter(config->items[i].string, | |
787 | write_one_config, NULL) < 0) | |
39ad4f39 | 788 | die(_("unable to write parameters to config file")); |
84054f79 JK |
789 | } |
790 | } | |
791 | ||
24d36f14 DA |
792 | static void write_refspec_config(const char *src_ref_prefix, |
793 | const struct ref *our_head_points_at, | |
794 | const struct ref *remote_head_points_at, | |
795 | struct strbuf *branch_top) | |
31b808a0 RT |
796 | { |
797 | struct strbuf key = STRBUF_INIT; | |
798 | struct strbuf value = STRBUF_INIT; | |
799 | ||
800 | if (option_mirror || !option_bare) { | |
801 | if (option_single_branch && !option_mirror) { | |
802 | if (option_branch) { | |
60a5f5fc | 803 | if (starts_with(our_head_points_at->name, "refs/tags/")) |
31b808a0 RT |
804 | strbuf_addf(&value, "+%s:%s", our_head_points_at->name, |
805 | our_head_points_at->name); | |
806 | else | |
807 | strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name, | |
808 | branch_top->buf, option_branch); | |
809 | } else if (remote_head_points_at) { | |
cf4fff57 JK |
810 | const char *head = remote_head_points_at->name; |
811 | if (!skip_prefix(head, "refs/heads/", &head)) | |
812 | die("BUG: remote HEAD points at non-head?"); | |
813 | ||
31b808a0 | 814 | strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name, |
cf4fff57 | 815 | branch_top->buf, head); |
31b808a0 RT |
816 | } |
817 | /* | |
818 | * otherwise, the next "git fetch" will | |
819 | * simply fetch from HEAD without updating | |
d6ac1d21 | 820 | * any remote-tracking branch, which is what |
31b808a0 RT |
821 | * we want. |
822 | */ | |
823 | } else { | |
824 | strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf); | |
825 | } | |
826 | /* Configure the remote */ | |
827 | if (value.len) { | |
828 | strbuf_addf(&key, "remote.%s.fetch", option_origin); | |
829 | git_config_set_multivar(key.buf, value.buf, "^$", 0); | |
830 | strbuf_reset(&key); | |
831 | ||
832 | if (option_mirror) { | |
833 | strbuf_addf(&key, "remote.%s.mirror", option_origin); | |
834 | git_config_set(key.buf, "true"); | |
835 | strbuf_reset(&key); | |
836 | } | |
837 | } | |
838 | } | |
839 | ||
840 | strbuf_release(&key); | |
841 | strbuf_release(&value); | |
842 | } | |
843 | ||
fb1d6dab JH |
844 | static void dissociate_from_references(void) |
845 | { | |
846 | static const char* argv[] = { "repack", "-a", "-d", NULL }; | |
0181681e | 847 | char *alternates = git_pathdup("objects/info/alternates"); |
fb1d6dab | 848 | |
0181681e AR |
849 | if (!access(alternates, F_OK)) { |
850 | if (run_command_v_opt(argv, RUN_GIT_CMD|RUN_COMMAND_NO_STDIN)) | |
851 | die(_("cannot repack to clean up")); | |
852 | if (unlink(alternates) && errno != ENOENT) | |
853 | die_errno(_("cannot unlink temporary alternates file")); | |
854 | } | |
855 | free(alternates); | |
fb1d6dab JH |
856 | } |
857 | ||
8434c2f1 DB |
858 | int cmd_clone(int argc, const char **argv, const char *prefix) |
859 | { | |
24c61c44 | 860 | int is_bundle = 0, is_local; |
8434c2f1 DB |
861 | struct stat buf; |
862 | const char *repo_name, *repo, *work_tree, *git_dir; | |
863 | char *path, *dir; | |
55892d23 | 864 | int dest_exists; |
37148311 | 865 | const struct ref *refs, *remote_head; |
7a4ee28f JK |
866 | const struct ref *remote_head_points_at; |
867 | const struct ref *our_head_points_at; | |
37148311 | 868 | struct ref *mapped_refs; |
90498161 | 869 | const struct ref *ref; |
b5ff37ac MV |
870 | struct strbuf key = STRBUF_INIT, value = STRBUF_INIT; |
871 | struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT; | |
1db4a75c | 872 | struct transport *transport = NULL; |
9e585046 | 873 | const char *src_ref_prefix = "refs/heads/"; |
6f48d39f | 874 | struct remote *remote; |
90498161 | 875 | int err = 0, complete_refs_before_fetch = 1; |
72c5f883 | 876 | int submodule_progress; |
8434c2f1 | 877 | |
689f0396 DB |
878 | struct refspec *refspec; |
879 | const char *fetch_pattern; | |
8434c2f1 | 880 | |
bbc30f99 | 881 | packet_trace_identity("clone"); |
37782920 | 882 | argc = parse_options(argc, argv, prefix, builtin_clone_options, |
8434c2f1 DB |
883 | builtin_clone_usage, 0); |
884 | ||
d52dc4b1 | 885 | if (argc > 2) |
e84d7b74 | 886 | usage_msg_opt(_("Too many arguments."), |
d52dc4b1 JN |
887 | builtin_clone_usage, builtin_clone_options); |
888 | ||
8434c2f1 | 889 | if (argc == 0) |
e84d7b74 | 890 | usage_msg_opt(_("You must specify a repository to clone."), |
d52dc4b1 | 891 | builtin_clone_usage, builtin_clone_options); |
8434c2f1 | 892 | |
859e5df9 | 893 | if (option_depth || option_since || option_not.nr) |
994c2aaf | 894 | deepen = 1; |
3e6e0edd | 895 | if (option_single_branch == -1) |
994c2aaf | 896 | option_single_branch = deepen ? 1 : 0; |
3e6e0edd | 897 | |
bc699afc JS |
898 | if (option_mirror) |
899 | option_bare = 1; | |
900 | ||
8434c2f1 DB |
901 | if (option_bare) { |
902 | if (option_origin) | |
e84d7b74 | 903 | die(_("--bare and --origin %s options are incompatible."), |
8434c2f1 | 904 | option_origin); |
95b63f1e NTND |
905 | if (real_git_dir) |
906 | die(_("--bare and --separate-git-dir are incompatible.")); | |
8434c2f1 | 907 | option_no_checkout = 1; |
8434c2f1 DB |
908 | } |
909 | ||
910 | if (!option_origin) | |
911 | option_origin = "origin"; | |
912 | ||
913 | repo_name = argv[0]; | |
914 | ||
915 | path = get_repo_path(repo_name, &is_bundle); | |
916 | if (path) | |
0aaad415 | 917 | repo = absolute_pathdup(repo_name); |
8434c2f1 | 918 | else if (!strchr(repo_name, ':')) |
d3e1ddfd | 919 | die(_("repository '%s' does not exist"), repo_name); |
8434c2f1 DB |
920 | else |
921 | repo = repo_name; | |
922 | ||
5594bcad NTND |
923 | /* no need to be strict, transport_set_option() will validate it again */ |
924 | if (option_depth && atoi(option_depth) < 1) | |
925 | die(_("depth %s is not a positive number"), option_depth); | |
926 | ||
8434c2f1 DB |
927 | if (argc == 2) |
928 | dir = xstrdup(argv[1]); | |
929 | else | |
6612f877 | 930 | dir = guess_dir_name(repo_name, is_bundle, option_bare); |
44a68fd5 | 931 | strip_trailing_slashes(dir); |
8434c2f1 | 932 | |
55892d23 AP |
933 | dest_exists = !stat(dir, &buf); |
934 | if (dest_exists && !is_empty_dir(dir)) | |
e84d7b74 ÆAB |
935 | die(_("destination path '%s' already exists and is not " |
936 | "an empty directory."), dir); | |
8434c2f1 | 937 | |
8434c2f1 DB |
938 | strbuf_addf(&reflog_msg, "clone: from %s", repo); |
939 | ||
940 | if (option_bare) | |
941 | work_tree = NULL; | |
942 | else { | |
943 | work_tree = getenv("GIT_WORK_TREE"); | |
944 | if (work_tree && !stat(work_tree, &buf)) | |
e84d7b74 | 945 | die(_("working tree '%s' already exists."), work_tree); |
8434c2f1 DB |
946 | } |
947 | ||
948 | if (option_bare || work_tree) | |
949 | git_dir = xstrdup(dir); | |
950 | else { | |
951 | work_tree = dir; | |
4e2d094d | 952 | git_dir = mkpathdup("%s/.git", dir); |
8434c2f1 DB |
953 | } |
954 | ||
ee0e3872 JK |
955 | atexit(remove_junk); |
956 | sigchain_push_common(remove_junk_on_signal); | |
957 | ||
8434c2f1 | 958 | if (!option_bare) { |
8e21d63b | 959 | if (safe_create_leading_directories_const(work_tree) < 0) |
e84d7b74 | 960 | die_errno(_("could not create leading directories of '%s'"), |
d824cbba | 961 | work_tree); |
45d4fdc2 | 962 | if (!dest_exists && mkdir(work_tree, 0777)) |
16eff6c0 | 963 | die_errno(_("could not create work tree dir '%s'"), |
d824cbba | 964 | work_tree); |
ee0e3872 | 965 | junk_work_tree = work_tree; |
8434c2f1 DB |
966 | set_git_work_tree(work_tree); |
967 | } | |
8434c2f1 | 968 | |
33158701 | 969 | junk_git_dir = real_git_dir ? real_git_dir : git_dir; |
8e21d63b | 970 | if (safe_create_leading_directories_const(git_dir) < 0) |
e84d7b74 | 971 | die(_("could not create leading directories of '%s'"), git_dir); |
b57fb80a | 972 | |
3781fcce ÆAB |
973 | if (0 <= option_verbosity) { |
974 | if (option_bare) | |
68b939b2 | 975 | fprintf(stderr, _("Cloning into bare repository '%s'...\n"), dir); |
3781fcce | 976 | else |
68b939b2 | 977 | fprintf(stderr, _("Cloning into '%s'...\n"), dir); |
3781fcce | 978 | } |
31224cbd | 979 | |
bb62e0a9 BW |
980 | if (option_recurse_submodules.nr > 0) { |
981 | struct string_list_item *item; | |
982 | struct strbuf sb = STRBUF_INIT; | |
983 | ||
984 | /* remove duplicates */ | |
985 | string_list_sort(&option_recurse_submodules); | |
986 | string_list_remove_duplicates(&option_recurse_submodules, 0); | |
987 | ||
988 | /* | |
989 | * NEEDSWORK: In a multi-working-tree world, this needs to be | |
990 | * set in the per-worktree config. | |
991 | */ | |
992 | for_each_string_list_item(item, &option_recurse_submodules) { | |
993 | strbuf_addf(&sb, "submodule.active=%s", | |
994 | item->string); | |
995 | string_list_append(&option_config, | |
996 | strbuf_detach(&sb, NULL)); | |
997 | } | |
998 | ||
31224cbd SB |
999 | if (option_required_reference.nr && |
1000 | option_optional_reference.nr) | |
1001 | die(_("clone --recursive is not compatible with " | |
1002 | "both --reference and --reference-if-able")); | |
1003 | else if (option_required_reference.nr) { | |
1004 | string_list_append(&option_config, | |
1005 | "submodule.alternateLocation=superproject"); | |
1006 | string_list_append(&option_config, | |
1007 | "submodule.alternateErrorStrategy=die"); | |
1008 | } else if (option_optional_reference.nr) { | |
1009 | string_list_append(&option_config, | |
1010 | "submodule.alternateLocation=superproject"); | |
1011 | string_list_append(&option_config, | |
1012 | "submodule.alternateErrorStrategy=info"); | |
1013 | } | |
1014 | } | |
1015 | ||
33158701 NTND |
1016 | init_db(git_dir, real_git_dir, option_template, INIT_DB_QUIET); |
1017 | ||
1018 | if (real_git_dir) | |
1019 | git_dir = real_git_dir; | |
1020 | ||
84054f79 | 1021 | write_config(&option_config); |
8434c2f1 | 1022 | |
9bd81e42 | 1023 | git_config(git_default_config, NULL); |
8434c2f1 DB |
1024 | |
1025 | if (option_bare) { | |
bc699afc JS |
1026 | if (option_mirror) |
1027 | src_ref_prefix = "refs/"; | |
b5ff37ac | 1028 | strbuf_addstr(&branch_top, src_ref_prefix); |
8434c2f1 DB |
1029 | |
1030 | git_config_set("core.bare", "true"); | |
1031 | } else { | |
b5ff37ac | 1032 | strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin); |
bc699afc | 1033 | } |
8434c2f1 | 1034 | |
689f0396 | 1035 | strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf); |
df61c889 SR |
1036 | strbuf_addf(&key, "remote.%s.url", option_origin); |
1037 | git_config_set(key.buf, repo); | |
1038 | strbuf_reset(&key); | |
1039 | ||
f7415b4d | 1040 | if (option_required_reference.nr || option_optional_reference.nr) |
dbc92b07 | 1041 | setup_reference(); |
766ac6a6 | 1042 | |
689f0396 DB |
1043 | fetch_pattern = value.buf; |
1044 | refspec = parse_fetch_refspec(1, &fetch_pattern); | |
1045 | ||
1046 | strbuf_reset(&value); | |
8434c2f1 | 1047 | |
6f48d39f NTND |
1048 | remote = remote_get(option_origin); |
1049 | transport = transport_get(remote, remote->url[0]); | |
822f0c4f | 1050 | transport_set_verbosity(transport, option_verbosity, option_progress); |
c915f11e | 1051 | transport->family = family; |
822f0c4f | 1052 | |
f38aa83f MB |
1053 | path = get_repo_path(remote->url[0], &is_bundle); |
1054 | is_local = option_local != 0 && path && !is_bundle; | |
1055 | if (is_local) { | |
1056 | if (option_depth) | |
1057 | warning(_("--depth is ignored in local clones; use file:// instead.")); | |
994c2aaf NTND |
1058 | if (option_since) |
1059 | warning(_("--shallow-since is ignored in local clones; use file:// instead.")); | |
859e5df9 NTND |
1060 | if (option_not.nr) |
1061 | warning(_("--shallow-exclude is ignored in local clones; use file:// instead.")); | |
f38aa83f MB |
1062 | if (!access(mkpath("%s/shallow", path), F_OK)) { |
1063 | if (option_local > 0) | |
1064 | warning(_("source repository is shallow, ignoring --local")); | |
1065 | is_local = 0; | |
1066 | } | |
1067 | } | |
1068 | if (option_local > 0 && !is_local) | |
1069 | warning(_("--local is ignored")); | |
beea4152 | 1070 | transport->cloning = 1; |
8434c2f1 | 1071 | |
643f918d JK |
1072 | if (!transport->get_refs_list || (!is_local && !transport->fetch)) |
1073 | die(_("Don't know how to clone %s"), transport->url); | |
37b78c25 | 1074 | |
643f918d | 1075 | transport_set_option(transport, TRANS_OPT_KEEP, "yes"); |
8434c2f1 | 1076 | |
643f918d JK |
1077 | if (option_depth) |
1078 | transport_set_option(transport, TRANS_OPT_DEPTH, | |
1079 | option_depth); | |
994c2aaf NTND |
1080 | if (option_since) |
1081 | transport_set_option(transport, TRANS_OPT_DEEPEN_SINCE, | |
1082 | option_since); | |
859e5df9 NTND |
1083 | if (option_not.nr) |
1084 | transport_set_option(transport, TRANS_OPT_DEEPEN_NOT, | |
1085 | (const char *)&option_not); | |
643f918d JK |
1086 | if (option_single_branch) |
1087 | transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1"); | |
8434c2f1 | 1088 | |
643f918d JK |
1089 | if (option_upload_pack) |
1090 | transport_set_option(transport, TRANS_OPT_UPLOADPACK, | |
1091 | option_upload_pack); | |
c6807a40 | 1092 | |
994c2aaf | 1093 | if (transport->smart_options && !deepen) |
643f918d | 1094 | transport->smart_options->check_self_contained_and_connected = 1; |
8434c2f1 | 1095 | |
6f48d39f | 1096 | refs = transport_get_remote_refs(transport); |
8434c2f1 | 1097 | |
5b05795c MH |
1098 | if (refs) { |
1099 | mapped_refs = wanted_peer_refs(refs, refspec); | |
1100 | /* | |
1101 | * transport_get_remote_refs() may return refs with null sha-1 | |
1102 | * in mapped_refs (see struct transport->get_refs_list | |
1103 | * comment). In that case we need fetch it early because | |
1104 | * remote_head code below relies on it. | |
1105 | * | |
1106 | * for normal clones, transport_get_remote_refs() should | |
1107 | * return reliable ref set, we can delay cloning until after | |
1108 | * remote HEAD check. | |
1109 | */ | |
1110 | for (ref = refs; ref; ref = ref->next) | |
f4e54d02 | 1111 | if (is_null_oid(&ref->old_oid)) { |
5b05795c MH |
1112 | complete_refs_before_fetch = 0; |
1113 | break; | |
1114 | } | |
90498161 | 1115 | |
5b05795c MH |
1116 | if (!is_local && !complete_refs_before_fetch) |
1117 | transport_fetch_refs(transport, mapped_refs); | |
8434c2f1 | 1118 | |
6cb4e6cc | 1119 | remote_head = find_ref_by_name(refs, "HEAD"); |
7a4ee28f JK |
1120 | remote_head_points_at = |
1121 | guess_remote_head(remote_head, mapped_refs, 0); | |
1122 | ||
1123 | if (option_branch) { | |
7a4ee28f | 1124 | our_head_points_at = |
9e585046 | 1125 | find_remote_branch(mapped_refs, option_branch); |
7a4ee28f | 1126 | |
920b691f NTND |
1127 | if (!our_head_points_at) |
1128 | die(_("Remote branch %s not found in upstream %s"), | |
1129 | option_branch, option_origin); | |
7a4ee28f JK |
1130 | } |
1131 | else | |
1132 | our_head_points_at = remote_head_points_at; | |
86ac7518 SR |
1133 | } |
1134 | else { | |
a3552aba RT |
1135 | if (option_branch) |
1136 | die(_("Remote branch %s not found in upstream %s"), | |
1137 | option_branch, option_origin); | |
1138 | ||
e84d7b74 | 1139 | warning(_("You appear to have cloned an empty repository.")); |
5b05795c | 1140 | mapped_refs = NULL; |
7a4ee28f JK |
1141 | our_head_points_at = NULL; |
1142 | remote_head_points_at = NULL; | |
86ac7518 SR |
1143 | remote_head = NULL; |
1144 | option_no_checkout = 1; | |
5cd12b85 | 1145 | if (!option_bare) |
a9f2c136 | 1146 | install_branch_config(0, "master", option_origin, |
5cd12b85 | 1147 | "refs/heads/master"); |
86ac7518 | 1148 | } |
8434c2f1 | 1149 | |
31b808a0 RT |
1150 | write_refspec_config(src_ref_prefix, our_head_points_at, |
1151 | remote_head_points_at, &branch_top); | |
1152 | ||
6f48d39f NTND |
1153 | if (is_local) |
1154 | clone_local(path, git_dir); | |
90498161 | 1155 | else if (refs && complete_refs_before_fetch) |
6f48d39f | 1156 | transport_fetch_refs(transport, mapped_refs); |
8434c2f1 | 1157 | |
960b7d1c | 1158 | update_remote_refs(refs, mapped_refs, remote_head_points_at, |
45ed4afa | 1159 | branch_top.buf, reflog_msg.buf, transport, !is_local); |
8434c2f1 | 1160 | |
f034d354 | 1161 | update_head(our_head_points_at, remote_head, reflog_msg.buf); |
1db4a75c | 1162 | |
72c5f883 JK |
1163 | /* |
1164 | * We want to show progress for recursive submodule clones iff | |
1165 | * we did so for the main clone. But only the transport knows | |
1166 | * the final decision for this flag, so we need to rescue the value | |
1167 | * before we free the transport. | |
1168 | */ | |
1169 | submodule_progress = transport->progress; | |
1170 | ||
6f48d39f NTND |
1171 | transport_unlock_pack(transport); |
1172 | transport_disconnect(transport); | |
1db4a75c | 1173 | |
786b150c JS |
1174 | if (option_dissociate) { |
1175 | close_all_packs(); | |
fb1d6dab | 1176 | dissociate_from_references(); |
786b150c | 1177 | } |
fb1d6dab | 1178 | |
d3b34622 | 1179 | junk_mode = JUNK_LEAVE_REPO; |
72c5f883 | 1180 | err = checkout(submodule_progress); |
8434c2f1 DB |
1181 | |
1182 | strbuf_release(&reflog_msg); | |
b5ff37ac MV |
1183 | strbuf_release(&branch_top); |
1184 | strbuf_release(&key); | |
1185 | strbuf_release(&value); | |
d3b34622 | 1186 | junk_mode = JUNK_LEAVE_ALL; |
50b67732 SB |
1187 | |
1188 | free(refspec); | |
dfa7a6c5 | 1189 | return err; |
8434c2f1 | 1190 | } |