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