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