Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Builtin "git clone" |
3 | | * |
4 | | * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>, |
5 | | * 2008 Daniel Barkalow <barkalow@iabervon.org> |
6 | | * Based on git-commit.sh by Junio C Hamano and Linus Torvalds |
7 | | * |
8 | | * Clone a repository into a different directory that does not yet exist. |
9 | | */ |
10 | | |
11 | | #include "builtin.h" |
12 | | #include "abspath.h" |
13 | | #include "advice.h" |
14 | | #include "config.h" |
15 | | #include "copy.h" |
16 | | #include "environment.h" |
17 | | #include "gettext.h" |
18 | | #include "hex.h" |
19 | | #include "lockfile.h" |
20 | | #include "parse-options.h" |
21 | | #include "refs.h" |
22 | | #include "refspec.h" |
23 | | #include "object-file.h" |
24 | | #include "object-store-ll.h" |
25 | | #include "tree.h" |
26 | | #include "tree-walk.h" |
27 | | #include "unpack-trees.h" |
28 | | #include "transport.h" |
29 | | #include "strbuf.h" |
30 | | #include "dir.h" |
31 | | #include "dir-iterator.h" |
32 | | #include "iterator.h" |
33 | | #include "sigchain.h" |
34 | | #include "branch.h" |
35 | | #include "remote.h" |
36 | | #include "run-command.h" |
37 | | #include "setup.h" |
38 | | #include "connected.h" |
39 | | #include "packfile.h" |
40 | | #include "path.h" |
41 | | #include "pkt-line.h" |
42 | | #include "list-objects-filter-options.h" |
43 | | #include "hook.h" |
44 | | #include "bundle.h" |
45 | | #include "bundle-uri.h" |
46 | | |
47 | | /* |
48 | | * Overall FIXMEs: |
49 | | * - respect DB_ENVIRONMENT for .git/objects. |
50 | | * |
51 | | * Implementation notes: |
52 | | * - dropping use-separate-remote and no-separate-remote compatibility |
53 | | * |
54 | | */ |
55 | | static const char * const builtin_clone_usage[] = { |
56 | | N_("git clone [<options>] [--] <repo> [<dir>]"), |
57 | | NULL |
58 | | }; |
59 | | |
60 | | static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1; |
61 | | static int option_local = -1, option_no_hardlinks, option_shared; |
62 | | static int option_no_tags; |
63 | | static int option_shallow_submodules; |
64 | | static int option_reject_shallow = -1; /* unspecified */ |
65 | | static int config_reject_shallow = -1; /* unspecified */ |
66 | | static int deepen; |
67 | | static char *option_template, *option_depth, *option_since; |
68 | | static char *option_origin = NULL; |
69 | | static char *remote_name = NULL; |
70 | | static char *option_branch = NULL; |
71 | | static struct string_list option_not = STRING_LIST_INIT_NODUP; |
72 | | static const char *real_git_dir; |
73 | | static const char *ref_format; |
74 | | static const char *option_upload_pack = "git-upload-pack"; |
75 | | static int option_verbosity; |
76 | | static int option_progress = -1; |
77 | | static int option_sparse_checkout; |
78 | | static enum transport_family family; |
79 | | static struct string_list option_config = STRING_LIST_INIT_NODUP; |
80 | | static struct string_list option_required_reference = STRING_LIST_INIT_NODUP; |
81 | | static struct string_list option_optional_reference = STRING_LIST_INIT_NODUP; |
82 | | static int option_dissociate; |
83 | | static int max_jobs = -1; |
84 | | static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP; |
85 | | static struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT; |
86 | | static int option_filter_submodules = -1; /* unspecified */ |
87 | | static int config_filter_submodules = -1; /* unspecified */ |
88 | | static struct string_list server_options = STRING_LIST_INIT_NODUP; |
89 | | static int option_remote_submodules; |
90 | | static const char *bundle_uri; |
91 | | |
92 | | static int recurse_submodules_cb(const struct option *opt, |
93 | | const char *arg, int unset) |
94 | 0 | { |
95 | 0 | if (unset) |
96 | 0 | string_list_clear((struct string_list *)opt->value, 0); |
97 | 0 | else if (arg) |
98 | 0 | string_list_append((struct string_list *)opt->value, arg); |
99 | 0 | else |
100 | 0 | string_list_append((struct string_list *)opt->value, |
101 | 0 | (const char *)opt->defval); |
102 | |
|
103 | 0 | return 0; |
104 | 0 | } |
105 | | |
106 | | static struct option builtin_clone_options[] = { |
107 | | OPT__VERBOSITY(&option_verbosity), |
108 | | OPT_BOOL(0, "progress", &option_progress, |
109 | | N_("force progress reporting")), |
110 | | OPT_BOOL(0, "reject-shallow", &option_reject_shallow, |
111 | | N_("don't clone shallow repository")), |
112 | | OPT_BOOL('n', "no-checkout", &option_no_checkout, |
113 | | N_("don't create a checkout")), |
114 | | OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")), |
115 | | OPT_HIDDEN_BOOL(0, "naked", &option_bare, |
116 | | N_("create a bare repository")), |
117 | | OPT_BOOL(0, "mirror", &option_mirror, |
118 | | N_("create a mirror repository (implies --bare)")), |
119 | | OPT_BOOL('l', "local", &option_local, |
120 | | N_("to clone from a local repository")), |
121 | | OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks, |
122 | | N_("don't use local hardlinks, always copy")), |
123 | | OPT_BOOL('s', "shared", &option_shared, |
124 | | N_("setup as shared repository")), |
125 | | { OPTION_CALLBACK, 0, "recurse-submodules", &option_recurse_submodules, |
126 | | N_("pathspec"), N_("initialize submodules in the clone"), |
127 | | PARSE_OPT_OPTARG, recurse_submodules_cb, (intptr_t)"." }, |
128 | | OPT_ALIAS(0, "recursive", "recurse-submodules"), |
129 | | OPT_INTEGER('j', "jobs", &max_jobs, |
130 | | N_("number of submodules cloned in parallel")), |
131 | | OPT_STRING(0, "template", &option_template, N_("template-directory"), |
132 | | N_("directory from which templates will be used")), |
133 | | OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"), |
134 | | N_("reference repository")), |
135 | | OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference, |
136 | | N_("repo"), N_("reference repository")), |
137 | | OPT_BOOL(0, "dissociate", &option_dissociate, |
138 | | N_("use --reference only while cloning")), |
139 | | OPT_STRING('o', "origin", &option_origin, N_("name"), |
140 | | N_("use <name> instead of 'origin' to track upstream")), |
141 | | OPT_STRING('b', "branch", &option_branch, N_("branch"), |
142 | | N_("checkout <branch> instead of the remote's HEAD")), |
143 | | OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"), |
144 | | N_("path to git-upload-pack on the remote")), |
145 | | OPT_STRING(0, "depth", &option_depth, N_("depth"), |
146 | | N_("create a shallow clone of that depth")), |
147 | | OPT_STRING(0, "shallow-since", &option_since, N_("time"), |
148 | | N_("create a shallow clone since a specific time")), |
149 | | OPT_STRING_LIST(0, "shallow-exclude", &option_not, N_("revision"), |
150 | | N_("deepen history of shallow clone, excluding rev")), |
151 | | OPT_BOOL(0, "single-branch", &option_single_branch, |
152 | | N_("clone only one branch, HEAD or --branch")), |
153 | | OPT_BOOL(0, "no-tags", &option_no_tags, |
154 | | N_("don't clone any tags, and make later fetches not to follow them")), |
155 | | OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules, |
156 | | N_("any cloned submodules will be shallow")), |
157 | | OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"), |
158 | | N_("separate git dir from working tree")), |
159 | | OPT_STRING(0, "ref-format", &ref_format, N_("format"), |
160 | | N_("specify the reference format to use")), |
161 | | OPT_STRING_LIST('c', "config", &option_config, N_("key=value"), |
162 | | N_("set config inside the new repository")), |
163 | | OPT_STRING_LIST(0, "server-option", &server_options, |
164 | | N_("server-specific"), N_("option to transmit")), |
165 | | OPT_IPVERSION(&family), |
166 | | OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options), |
167 | | OPT_BOOL(0, "also-filter-submodules", &option_filter_submodules, |
168 | | N_("apply partial clone filters to submodules")), |
169 | | OPT_BOOL(0, "remote-submodules", &option_remote_submodules, |
170 | | N_("any cloned submodules will use their remote-tracking branch")), |
171 | | OPT_BOOL(0, "sparse", &option_sparse_checkout, |
172 | | N_("initialize sparse-checkout file to include only files at root")), |
173 | | OPT_STRING(0, "bundle-uri", &bundle_uri, |
174 | | N_("uri"), N_("a URI for downloading bundles before fetching from origin remote")), |
175 | | OPT_END() |
176 | | }; |
177 | | |
178 | | static const char *get_repo_path_1(struct strbuf *path, int *is_bundle) |
179 | 0 | { |
180 | 0 | static const char *suffix[] = { "/.git", "", ".git/.git", ".git" }; |
181 | 0 | static const char *bundle_suffix[] = { ".bundle", "" }; |
182 | 0 | size_t baselen = path->len; |
183 | 0 | struct stat st; |
184 | 0 | int i; |
185 | |
|
186 | 0 | for (i = 0; i < ARRAY_SIZE(suffix); i++) { |
187 | 0 | strbuf_setlen(path, baselen); |
188 | 0 | strbuf_addstr(path, suffix[i]); |
189 | 0 | if (stat(path->buf, &st)) |
190 | 0 | continue; |
191 | 0 | if (S_ISDIR(st.st_mode) && is_git_directory(path->buf)) { |
192 | 0 | *is_bundle = 0; |
193 | 0 | return path->buf; |
194 | 0 | } else if (S_ISREG(st.st_mode) && st.st_size > 8) { |
195 | | /* Is it a "gitfile"? */ |
196 | 0 | char signature[8]; |
197 | 0 | const char *dst; |
198 | 0 | int len, fd = open(path->buf, O_RDONLY); |
199 | 0 | if (fd < 0) |
200 | 0 | continue; |
201 | 0 | len = read_in_full(fd, signature, 8); |
202 | 0 | close(fd); |
203 | 0 | if (len != 8 || strncmp(signature, "gitdir: ", 8)) |
204 | 0 | continue; |
205 | 0 | dst = read_gitfile(path->buf); |
206 | 0 | if (dst) { |
207 | 0 | *is_bundle = 0; |
208 | 0 | return dst; |
209 | 0 | } |
210 | 0 | } |
211 | 0 | } |
212 | | |
213 | 0 | for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) { |
214 | 0 | strbuf_setlen(path, baselen); |
215 | 0 | strbuf_addstr(path, bundle_suffix[i]); |
216 | 0 | if (!stat(path->buf, &st) && S_ISREG(st.st_mode)) { |
217 | 0 | *is_bundle = 1; |
218 | 0 | return path->buf; |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | 0 | return NULL; |
223 | 0 | } |
224 | | |
225 | | static char *get_repo_path(const char *repo, int *is_bundle) |
226 | 0 | { |
227 | 0 | struct strbuf path = STRBUF_INIT; |
228 | 0 | const char *raw; |
229 | 0 | char *canon; |
230 | |
|
231 | 0 | strbuf_addstr(&path, repo); |
232 | 0 | raw = get_repo_path_1(&path, is_bundle); |
233 | 0 | canon = raw ? absolute_pathdup(raw) : NULL; |
234 | 0 | strbuf_release(&path); |
235 | 0 | return canon; |
236 | 0 | } |
237 | | |
238 | | static int add_one_reference(struct string_list_item *item, void *cb_data) |
239 | 0 | { |
240 | 0 | struct strbuf err = STRBUF_INIT; |
241 | 0 | int *required = cb_data; |
242 | 0 | char *ref_git = compute_alternate_path(item->string, &err); |
243 | |
|
244 | 0 | if (!ref_git) { |
245 | 0 | if (*required) |
246 | 0 | die("%s", err.buf); |
247 | 0 | else |
248 | 0 | fprintf(stderr, |
249 | 0 | _("info: Could not add alternate for '%s': %s\n"), |
250 | 0 | item->string, err.buf); |
251 | 0 | } else { |
252 | 0 | struct strbuf sb = STRBUF_INIT; |
253 | 0 | strbuf_addf(&sb, "%s/objects", ref_git); |
254 | 0 | add_to_alternates_file(sb.buf); |
255 | 0 | strbuf_release(&sb); |
256 | 0 | } |
257 | | |
258 | 0 | strbuf_release(&err); |
259 | 0 | free(ref_git); |
260 | 0 | return 0; |
261 | 0 | } |
262 | | |
263 | | static void setup_reference(void) |
264 | 0 | { |
265 | 0 | int required = 1; |
266 | 0 | for_each_string_list(&option_required_reference, |
267 | 0 | add_one_reference, &required); |
268 | 0 | required = 0; |
269 | 0 | for_each_string_list(&option_optional_reference, |
270 | 0 | add_one_reference, &required); |
271 | 0 | } |
272 | | |
273 | | static void copy_alternates(struct strbuf *src, const char *src_repo) |
274 | 0 | { |
275 | | /* |
276 | | * Read from the source objects/info/alternates file |
277 | | * and copy the entries to corresponding file in the |
278 | | * destination repository with add_to_alternates_file(). |
279 | | * Both src and dst have "$path/objects/info/alternates". |
280 | | * |
281 | | * Instead of copying bit-for-bit from the original, |
282 | | * we need to append to existing one so that the already |
283 | | * created entry via "clone -s" is not lost, and also |
284 | | * to turn entries with paths relative to the original |
285 | | * absolute, so that they can be used in the new repository. |
286 | | */ |
287 | 0 | FILE *in = xfopen(src->buf, "r"); |
288 | 0 | struct strbuf line = STRBUF_INIT; |
289 | |
|
290 | 0 | while (strbuf_getline(&line, in) != EOF) { |
291 | 0 | char *abs_path; |
292 | 0 | if (!line.len || line.buf[0] == '#') |
293 | 0 | continue; |
294 | 0 | if (is_absolute_path(line.buf)) { |
295 | 0 | add_to_alternates_file(line.buf); |
296 | 0 | continue; |
297 | 0 | } |
298 | 0 | abs_path = mkpathdup("%s/objects/%s", src_repo, line.buf); |
299 | 0 | if (!normalize_path_copy(abs_path, abs_path)) |
300 | 0 | add_to_alternates_file(abs_path); |
301 | 0 | else |
302 | 0 | warning("skipping invalid relative alternate: %s/%s", |
303 | 0 | src_repo, line.buf); |
304 | 0 | free(abs_path); |
305 | 0 | } |
306 | 0 | strbuf_release(&line); |
307 | 0 | fclose(in); |
308 | 0 | } |
309 | | |
310 | | static void mkdir_if_missing(const char *pathname, mode_t mode) |
311 | 0 | { |
312 | 0 | struct stat st; |
313 | |
|
314 | 0 | if (!mkdir(pathname, mode)) |
315 | 0 | return; |
316 | | |
317 | 0 | if (errno != EEXIST) |
318 | 0 | die_errno(_("failed to create directory '%s'"), pathname); |
319 | 0 | else if (stat(pathname, &st)) |
320 | 0 | die_errno(_("failed to stat '%s'"), pathname); |
321 | 0 | else if (!S_ISDIR(st.st_mode)) |
322 | 0 | die(_("%s exists and is not a directory"), pathname); |
323 | 0 | } |
324 | | |
325 | | static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest, |
326 | | const char *src_repo) |
327 | 0 | { |
328 | 0 | int src_len, dest_len; |
329 | 0 | struct dir_iterator *iter; |
330 | 0 | int iter_status; |
331 | | |
332 | | /* |
333 | | * Refuse copying directories by default which aren't owned by us. The |
334 | | * code that performs either the copying or hardlinking is not prepared |
335 | | * to handle various edge cases where an adversary may for example |
336 | | * racily swap out files for symlinks. This can cause us to |
337 | | * inadvertently use the wrong source file. |
338 | | * |
339 | | * Furthermore, even if we were prepared to handle such races safely, |
340 | | * creating hardlinks across user boundaries is an inherently unsafe |
341 | | * operation as the hardlinked files can be rewritten at will by the |
342 | | * potentially-untrusted user. We thus refuse to do so by default. |
343 | | */ |
344 | 0 | die_upon_dubious_ownership(NULL, NULL, src_repo); |
345 | |
|
346 | 0 | mkdir_if_missing(dest->buf, 0777); |
347 | |
|
348 | 0 | iter = dir_iterator_begin(src->buf, DIR_ITERATOR_PEDANTIC); |
349 | |
|
350 | 0 | if (!iter) { |
351 | 0 | if (errno == ENOTDIR) { |
352 | 0 | int saved_errno = errno; |
353 | 0 | struct stat st; |
354 | |
|
355 | 0 | if (!lstat(src->buf, &st) && S_ISLNK(st.st_mode)) |
356 | 0 | die(_("'%s' is a symlink, refusing to clone with --local"), |
357 | 0 | src->buf); |
358 | 0 | errno = saved_errno; |
359 | 0 | } |
360 | 0 | die_errno(_("failed to start iterator over '%s'"), src->buf); |
361 | 0 | } |
362 | | |
363 | 0 | strbuf_addch(src, '/'); |
364 | 0 | src_len = src->len; |
365 | 0 | strbuf_addch(dest, '/'); |
366 | 0 | dest_len = dest->len; |
367 | |
|
368 | 0 | while ((iter_status = dir_iterator_advance(iter)) == ITER_OK) { |
369 | 0 | strbuf_setlen(src, src_len); |
370 | 0 | strbuf_addstr(src, iter->relative_path); |
371 | 0 | strbuf_setlen(dest, dest_len); |
372 | 0 | strbuf_addstr(dest, iter->relative_path); |
373 | |
|
374 | 0 | if (S_ISLNK(iter->st.st_mode)) |
375 | 0 | die(_("symlink '%s' exists, refusing to clone with --local"), |
376 | 0 | iter->relative_path); |
377 | | |
378 | 0 | if (S_ISDIR(iter->st.st_mode)) { |
379 | 0 | mkdir_if_missing(dest->buf, 0777); |
380 | 0 | continue; |
381 | 0 | } |
382 | | |
383 | | /* Files that cannot be copied bit-for-bit... */ |
384 | 0 | if (!fspathcmp(iter->relative_path, "info/alternates")) { |
385 | 0 | copy_alternates(src, src_repo); |
386 | 0 | continue; |
387 | 0 | } |
388 | | |
389 | 0 | if (unlink(dest->buf) && errno != ENOENT) |
390 | 0 | die_errno(_("failed to unlink '%s'"), dest->buf); |
391 | 0 | if (!option_no_hardlinks) { |
392 | 0 | if (!link(src->buf, dest->buf)) { |
393 | 0 | struct stat st; |
394 | | |
395 | | /* |
396 | | * Sanity-check whether the created hardlink |
397 | | * actually links to the expected file now. This |
398 | | * catches time-of-check-time-of-use bugs in |
399 | | * case the source file was meanwhile swapped. |
400 | | */ |
401 | 0 | if (lstat(dest->buf, &st)) |
402 | 0 | die(_("hardlink cannot be checked at '%s'"), dest->buf); |
403 | 0 | if (st.st_mode != iter->st.st_mode || |
404 | 0 | st.st_ino != iter->st.st_ino || |
405 | 0 | st.st_dev != iter->st.st_dev || |
406 | 0 | st.st_size != iter->st.st_size || |
407 | 0 | st.st_uid != iter->st.st_uid || |
408 | 0 | st.st_gid != iter->st.st_gid) |
409 | 0 | die(_("hardlink different from source at '%s'"), dest->buf); |
410 | | |
411 | 0 | continue; |
412 | 0 | } |
413 | 0 | if (option_local > 0) |
414 | 0 | die_errno(_("failed to create link '%s'"), dest->buf); |
415 | 0 | option_no_hardlinks = 1; |
416 | 0 | } |
417 | 0 | if (copy_file_with_time(dest->buf, src->buf, 0666)) |
418 | 0 | die_errno(_("failed to copy file to '%s'"), dest->buf); |
419 | 0 | } |
420 | | |
421 | 0 | if (iter_status != ITER_DONE) { |
422 | 0 | strbuf_setlen(src, src_len); |
423 | 0 | die(_("failed to iterate over '%s'"), src->buf); |
424 | 0 | } |
425 | 0 | } |
426 | | |
427 | | static void clone_local(const char *src_repo, const char *dest_repo) |
428 | 0 | { |
429 | 0 | if (option_shared) { |
430 | 0 | struct strbuf alt = STRBUF_INIT; |
431 | 0 | get_common_dir(&alt, src_repo); |
432 | 0 | strbuf_addstr(&alt, "/objects"); |
433 | 0 | add_to_alternates_file(alt.buf); |
434 | 0 | strbuf_release(&alt); |
435 | 0 | } else { |
436 | 0 | struct strbuf src = STRBUF_INIT; |
437 | 0 | struct strbuf dest = STRBUF_INIT; |
438 | 0 | get_common_dir(&src, src_repo); |
439 | 0 | get_common_dir(&dest, dest_repo); |
440 | 0 | strbuf_addstr(&src, "/objects"); |
441 | 0 | strbuf_addstr(&dest, "/objects"); |
442 | 0 | copy_or_link_directory(&src, &dest, src_repo); |
443 | 0 | strbuf_release(&src); |
444 | 0 | strbuf_release(&dest); |
445 | 0 | } |
446 | |
|
447 | 0 | if (0 <= option_verbosity) |
448 | 0 | fprintf(stderr, _("done.\n")); |
449 | 0 | } |
450 | | |
451 | | static const char *junk_work_tree; |
452 | | static int junk_work_tree_flags; |
453 | | static const char *junk_git_dir; |
454 | | static int junk_git_dir_flags; |
455 | | static enum { |
456 | | JUNK_LEAVE_NONE, |
457 | | JUNK_LEAVE_REPO, |
458 | | JUNK_LEAVE_ALL |
459 | | } junk_mode = JUNK_LEAVE_NONE; |
460 | | |
461 | | static const char junk_leave_repo_msg[] = |
462 | | N_("Clone succeeded, but checkout failed.\n" |
463 | | "You can inspect what was checked out with 'git status'\n" |
464 | | "and retry with 'git restore --source=HEAD :/'\n"); |
465 | | |
466 | | static void remove_junk(void) |
467 | 0 | { |
468 | 0 | struct strbuf sb = STRBUF_INIT; |
469 | |
|
470 | 0 | switch (junk_mode) { |
471 | 0 | case JUNK_LEAVE_REPO: |
472 | 0 | warning("%s", _(junk_leave_repo_msg)); |
473 | | /* fall-through */ |
474 | 0 | case JUNK_LEAVE_ALL: |
475 | 0 | return; |
476 | 0 | default: |
477 | | /* proceed to removal */ |
478 | 0 | break; |
479 | 0 | } |
480 | | |
481 | 0 | if (junk_git_dir) { |
482 | 0 | strbuf_addstr(&sb, junk_git_dir); |
483 | 0 | remove_dir_recursively(&sb, junk_git_dir_flags); |
484 | 0 | strbuf_reset(&sb); |
485 | 0 | } |
486 | 0 | if (junk_work_tree) { |
487 | 0 | strbuf_addstr(&sb, junk_work_tree); |
488 | 0 | remove_dir_recursively(&sb, junk_work_tree_flags); |
489 | 0 | } |
490 | 0 | strbuf_release(&sb); |
491 | 0 | } |
492 | | |
493 | | static void remove_junk_on_signal(int signo) |
494 | 0 | { |
495 | 0 | remove_junk(); |
496 | 0 | sigchain_pop(signo); |
497 | 0 | raise(signo); |
498 | 0 | } |
499 | | |
500 | | static struct ref *find_remote_branch(const struct ref *refs, const char *branch) |
501 | 0 | { |
502 | 0 | struct ref *ref; |
503 | 0 | struct strbuf head = STRBUF_INIT; |
504 | 0 | strbuf_addstr(&head, "refs/heads/"); |
505 | 0 | strbuf_addstr(&head, branch); |
506 | 0 | ref = find_ref_by_name(refs, head.buf); |
507 | 0 | strbuf_release(&head); |
508 | |
|
509 | 0 | if (ref) |
510 | 0 | return ref; |
511 | | |
512 | 0 | strbuf_addstr(&head, "refs/tags/"); |
513 | 0 | strbuf_addstr(&head, branch); |
514 | 0 | ref = find_ref_by_name(refs, head.buf); |
515 | 0 | strbuf_release(&head); |
516 | |
|
517 | 0 | return ref; |
518 | 0 | } |
519 | | |
520 | | static struct ref *wanted_peer_refs(const struct ref *refs, |
521 | | struct refspec *refspec) |
522 | 0 | { |
523 | 0 | struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD")); |
524 | 0 | struct ref *local_refs = head; |
525 | 0 | struct ref **tail = head ? &head->next : &local_refs; |
526 | 0 | struct refspec_item tag_refspec; |
527 | |
|
528 | 0 | refspec_item_init(&tag_refspec, TAG_REFSPEC, 0); |
529 | |
|
530 | 0 | if (option_single_branch) { |
531 | 0 | struct ref *remote_head = NULL; |
532 | |
|
533 | 0 | if (!option_branch) |
534 | 0 | remote_head = guess_remote_head(head, refs, 0); |
535 | 0 | else { |
536 | 0 | free_one_ref(head); |
537 | 0 | local_refs = head = NULL; |
538 | 0 | tail = &local_refs; |
539 | 0 | remote_head = copy_ref(find_remote_branch(refs, option_branch)); |
540 | 0 | } |
541 | |
|
542 | 0 | if (!remote_head && option_branch) |
543 | 0 | warning(_("Could not find remote branch %s to clone."), |
544 | 0 | option_branch); |
545 | 0 | else { |
546 | 0 | int i; |
547 | 0 | for (i = 0; i < refspec->nr; i++) |
548 | 0 | get_fetch_map(remote_head, &refspec->items[i], |
549 | 0 | &tail, 0); |
550 | | |
551 | | /* if --branch=tag, pull the requested tag explicitly */ |
552 | 0 | get_fetch_map(remote_head, &tag_refspec, &tail, 0); |
553 | 0 | } |
554 | 0 | free_refs(remote_head); |
555 | 0 | } else { |
556 | 0 | int i; |
557 | 0 | for (i = 0; i < refspec->nr; i++) |
558 | 0 | get_fetch_map(refs, &refspec->items[i], &tail, 0); |
559 | 0 | } |
560 | |
|
561 | 0 | if (!option_mirror && !option_single_branch && !option_no_tags) |
562 | 0 | get_fetch_map(refs, &tag_refspec, &tail, 0); |
563 | |
|
564 | 0 | refspec_item_clear(&tag_refspec); |
565 | 0 | return local_refs; |
566 | 0 | } |
567 | | |
568 | | static void write_remote_refs(const struct ref *local_refs) |
569 | 0 | { |
570 | 0 | const struct ref *r; |
571 | |
|
572 | 0 | struct ref_transaction *t; |
573 | 0 | struct strbuf err = STRBUF_INIT; |
574 | |
|
575 | 0 | t = ref_store_transaction_begin(get_main_ref_store(the_repository), |
576 | 0 | &err); |
577 | 0 | if (!t) |
578 | 0 | die("%s", err.buf); |
579 | | |
580 | 0 | for (r = local_refs; r; r = r->next) { |
581 | 0 | if (!r->peer_ref) |
582 | 0 | continue; |
583 | 0 | if (ref_transaction_create(t, r->peer_ref->name, &r->old_oid, |
584 | 0 | NULL, 0, NULL, &err)) |
585 | 0 | die("%s", err.buf); |
586 | 0 | } |
587 | | |
588 | 0 | if (initial_ref_transaction_commit(t, &err)) |
589 | 0 | die("%s", err.buf); |
590 | | |
591 | 0 | strbuf_release(&err); |
592 | 0 | ref_transaction_free(t); |
593 | 0 | } |
594 | | |
595 | | static void write_followtags(const struct ref *refs, const char *msg) |
596 | 0 | { |
597 | 0 | const struct ref *ref; |
598 | 0 | for (ref = refs; ref; ref = ref->next) { |
599 | 0 | if (!starts_with(ref->name, "refs/tags/")) |
600 | 0 | continue; |
601 | 0 | if (ends_with(ref->name, "^{}")) |
602 | 0 | continue; |
603 | 0 | if (!repo_has_object_file_with_flags(the_repository, &ref->old_oid, |
604 | 0 | OBJECT_INFO_QUICK | |
605 | 0 | OBJECT_INFO_SKIP_FETCH_OBJECT)) |
606 | 0 | continue; |
607 | 0 | refs_update_ref(get_main_ref_store(the_repository), msg, |
608 | 0 | ref->name, &ref->old_oid, NULL, 0, |
609 | 0 | UPDATE_REFS_DIE_ON_ERR); |
610 | 0 | } |
611 | 0 | } |
612 | | |
613 | | static const struct object_id *iterate_ref_map(void *cb_data) |
614 | 0 | { |
615 | 0 | struct ref **rm = cb_data; |
616 | 0 | 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 | 0 | while (ref && !ref->peer_ref) |
623 | 0 | ref = ref->next; |
624 | 0 | if (!ref) |
625 | 0 | return NULL; |
626 | | |
627 | 0 | *rm = ref->next; |
628 | 0 | return &ref->old_oid; |
629 | 0 | } |
630 | | |
631 | | static void update_remote_refs(const struct ref *refs, |
632 | | const struct ref *mapped_refs, |
633 | | const struct ref *remote_head_points_at, |
634 | | const char *branch_top, |
635 | | const char *msg, |
636 | | struct transport *transport, |
637 | | int check_connectivity) |
638 | 0 | { |
639 | 0 | const struct ref *rm = mapped_refs; |
640 | |
|
641 | 0 | if (check_connectivity) { |
642 | 0 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
643 | |
|
644 | 0 | opt.transport = transport; |
645 | 0 | opt.progress = transport->progress; |
646 | |
|
647 | 0 | if (check_connected(iterate_ref_map, &rm, &opt)) |
648 | 0 | die(_("remote did not send all necessary objects")); |
649 | 0 | } |
650 | | |
651 | 0 | if (refs) { |
652 | 0 | write_remote_refs(mapped_refs); |
653 | 0 | if (option_single_branch && !option_no_tags) |
654 | 0 | write_followtags(refs, msg); |
655 | 0 | } |
656 | |
|
657 | 0 | if (remote_head_points_at && !option_bare) { |
658 | 0 | struct strbuf head_ref = STRBUF_INIT; |
659 | 0 | strbuf_addstr(&head_ref, branch_top); |
660 | 0 | strbuf_addstr(&head_ref, "HEAD"); |
661 | 0 | if (refs_update_symref(get_main_ref_store(the_repository), head_ref.buf, |
662 | 0 | remote_head_points_at->peer_ref->name, |
663 | 0 | msg) < 0) |
664 | 0 | die(_("unable to update %s"), head_ref.buf); |
665 | 0 | strbuf_release(&head_ref); |
666 | 0 | } |
667 | 0 | } |
668 | | |
669 | | static void update_head(const struct ref *our, const struct ref *remote, |
670 | | const char *unborn, const char *msg) |
671 | 0 | { |
672 | 0 | const char *head; |
673 | 0 | if (our && skip_prefix(our->name, "refs/heads/", &head)) { |
674 | | /* Local default branch link */ |
675 | 0 | if (refs_update_symref(get_main_ref_store(the_repository), "HEAD", our->name, NULL) < 0) |
676 | 0 | die(_("unable to update HEAD")); |
677 | 0 | if (!option_bare) { |
678 | 0 | refs_update_ref(get_main_ref_store(the_repository), |
679 | 0 | msg, "HEAD", &our->old_oid, NULL, 0, |
680 | 0 | UPDATE_REFS_DIE_ON_ERR); |
681 | 0 | install_branch_config(0, head, remote_name, our->name); |
682 | 0 | } |
683 | 0 | } else if (our) { |
684 | 0 | struct commit *c = lookup_commit_reference(the_repository, |
685 | 0 | &our->old_oid); |
686 | | /* --branch specifies a non-branch (i.e. tags), detach HEAD */ |
687 | 0 | refs_update_ref(get_main_ref_store(the_repository), msg, |
688 | 0 | "HEAD", &c->object.oid, NULL, REF_NO_DEREF, |
689 | 0 | UPDATE_REFS_DIE_ON_ERR); |
690 | 0 | } else if (remote) { |
691 | | /* |
692 | | * We know remote HEAD points to a non-branch, or |
693 | | * HEAD points to a branch but we don't know which one. |
694 | | * Detach HEAD in all these cases. |
695 | | */ |
696 | 0 | refs_update_ref(get_main_ref_store(the_repository), msg, |
697 | 0 | "HEAD", &remote->old_oid, NULL, REF_NO_DEREF, |
698 | 0 | UPDATE_REFS_DIE_ON_ERR); |
699 | 0 | } else if (unborn && skip_prefix(unborn, "refs/heads/", &head)) { |
700 | | /* |
701 | | * Unborn head from remote; same as "our" case above except |
702 | | * that we have no ref to update. |
703 | | */ |
704 | 0 | if (refs_update_symref(get_main_ref_store(the_repository), "HEAD", unborn, NULL) < 0) |
705 | 0 | die(_("unable to update HEAD")); |
706 | 0 | if (!option_bare) |
707 | 0 | install_branch_config(0, head, remote_name, unborn); |
708 | 0 | } |
709 | 0 | } |
710 | | |
711 | | static int git_sparse_checkout_init(const char *repo) |
712 | 0 | { |
713 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
714 | 0 | int result = 0; |
715 | 0 | strvec_pushl(&cmd.args, "-C", repo, "sparse-checkout", "set", NULL); |
716 | | |
717 | | /* |
718 | | * We must apply the setting in the current process |
719 | | * for the later checkout to use the sparse-checkout file. |
720 | | */ |
721 | 0 | core_apply_sparse_checkout = 1; |
722 | |
|
723 | 0 | cmd.git_cmd = 1; |
724 | 0 | if (run_command(&cmd)) { |
725 | 0 | error(_("failed to initialize sparse-checkout")); |
726 | 0 | result = 1; |
727 | 0 | } |
728 | |
|
729 | 0 | return result; |
730 | 0 | } |
731 | | |
732 | | static int checkout(int submodule_progress, int filter_submodules, |
733 | | enum ref_storage_format ref_storage_format) |
734 | 0 | { |
735 | 0 | struct object_id oid; |
736 | 0 | char *head; |
737 | 0 | struct lock_file lock_file = LOCK_INIT; |
738 | 0 | struct unpack_trees_options opts; |
739 | 0 | struct tree *tree; |
740 | 0 | struct tree_desc t; |
741 | 0 | int err = 0; |
742 | |
|
743 | 0 | if (option_no_checkout) |
744 | 0 | return 0; |
745 | | |
746 | 0 | head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD", |
747 | 0 | RESOLVE_REF_READING, &oid, NULL); |
748 | 0 | if (!head) { |
749 | 0 | warning(_("remote HEAD refers to nonexistent ref, " |
750 | 0 | "unable to checkout")); |
751 | 0 | return 0; |
752 | 0 | } |
753 | 0 | if (!strcmp(head, "HEAD")) { |
754 | 0 | if (advice_enabled(ADVICE_DETACHED_HEAD)) |
755 | 0 | detach_advice(oid_to_hex(&oid)); |
756 | 0 | FREE_AND_NULL(head); |
757 | 0 | } else { |
758 | 0 | if (!starts_with(head, "refs/heads/")) |
759 | 0 | die(_("HEAD not found below refs/heads!")); |
760 | 0 | } |
761 | | |
762 | | /* We need to be in the new work tree for the checkout */ |
763 | 0 | setup_work_tree(); |
764 | |
|
765 | 0 | repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); |
766 | |
|
767 | 0 | memset(&opts, 0, sizeof opts); |
768 | 0 | opts.update = 1; |
769 | 0 | opts.merge = 1; |
770 | 0 | opts.clone = 1; |
771 | 0 | opts.preserve_ignored = 0; |
772 | 0 | opts.fn = oneway_merge; |
773 | 0 | opts.verbose_update = (option_verbosity >= 0); |
774 | 0 | opts.src_index = the_repository->index; |
775 | 0 | opts.dst_index = the_repository->index; |
776 | 0 | init_checkout_metadata(&opts.meta, head, &oid, NULL); |
777 | |
|
778 | 0 | tree = parse_tree_indirect(&oid); |
779 | 0 | if (!tree) |
780 | 0 | die(_("unable to parse commit %s"), oid_to_hex(&oid)); |
781 | 0 | if (parse_tree(tree) < 0) |
782 | 0 | exit(128); |
783 | 0 | init_tree_desc(&t, &tree->object.oid, tree->buffer, tree->size); |
784 | 0 | if (unpack_trees(1, &t, &opts) < 0) |
785 | 0 | die(_("unable to checkout working tree")); |
786 | | |
787 | 0 | free(head); |
788 | |
|
789 | 0 | if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK)) |
790 | 0 | die(_("unable to write new index file")); |
791 | | |
792 | 0 | err |= run_hooks_l(the_repository, "post-checkout", oid_to_hex(null_oid()), |
793 | 0 | oid_to_hex(&oid), "1", NULL); |
794 | |
|
795 | 0 | if (!err && (option_recurse_submodules.nr > 0)) { |
796 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
797 | 0 | strvec_pushl(&cmd.args, "submodule", "update", "--require-init", |
798 | 0 | "--recursive", NULL); |
799 | |
|
800 | 0 | if (option_shallow_submodules == 1) |
801 | 0 | strvec_push(&cmd.args, "--depth=1"); |
802 | |
|
803 | 0 | if (max_jobs != -1) |
804 | 0 | strvec_pushf(&cmd.args, "--jobs=%d", max_jobs); |
805 | |
|
806 | 0 | if (submodule_progress) |
807 | 0 | strvec_push(&cmd.args, "--progress"); |
808 | |
|
809 | 0 | if (option_verbosity < 0) |
810 | 0 | strvec_push(&cmd.args, "--quiet"); |
811 | |
|
812 | 0 | if (option_remote_submodules) { |
813 | 0 | strvec_push(&cmd.args, "--remote"); |
814 | 0 | strvec_push(&cmd.args, "--no-fetch"); |
815 | 0 | } |
816 | |
|
817 | 0 | if (ref_storage_format != REF_STORAGE_FORMAT_UNKNOWN) |
818 | 0 | strvec_pushf(&cmd.args, "--ref-format=%s", |
819 | 0 | ref_storage_format_to_name(ref_storage_format)); |
820 | |
|
821 | 0 | if (filter_submodules && filter_options.choice) |
822 | 0 | strvec_pushf(&cmd.args, "--filter=%s", |
823 | 0 | expand_list_objects_filter_spec(&filter_options)); |
824 | |
|
825 | 0 | if (option_single_branch >= 0) |
826 | 0 | strvec_push(&cmd.args, option_single_branch ? |
827 | 0 | "--single-branch" : |
828 | 0 | "--no-single-branch"); |
829 | |
|
830 | 0 | cmd.git_cmd = 1; |
831 | 0 | err = run_command(&cmd); |
832 | 0 | } |
833 | |
|
834 | 0 | return err; |
835 | 0 | } |
836 | | |
837 | | static int git_clone_config(const char *k, const char *v, |
838 | | const struct config_context *ctx, void *cb) |
839 | 0 | { |
840 | 0 | if (!strcmp(k, "clone.defaultremotename")) { |
841 | 0 | if (!v) |
842 | 0 | return config_error_nonbool(k); |
843 | 0 | free(remote_name); |
844 | 0 | remote_name = xstrdup(v); |
845 | 0 | } |
846 | 0 | if (!strcmp(k, "clone.rejectshallow")) |
847 | 0 | config_reject_shallow = git_config_bool(k, v); |
848 | 0 | if (!strcmp(k, "clone.filtersubmodules")) |
849 | 0 | config_filter_submodules = git_config_bool(k, v); |
850 | |
|
851 | 0 | return git_default_config(k, v, ctx, cb); |
852 | 0 | } |
853 | | |
854 | | static int write_one_config(const char *key, const char *value, |
855 | | const struct config_context *ctx, |
856 | | void *data) |
857 | 0 | { |
858 | | /* |
859 | | * give git_clone_config a chance to write config values back to the |
860 | | * environment, since git_config_set_multivar_gently only deals with |
861 | | * config-file writes |
862 | | */ |
863 | 0 | int apply_failed = git_clone_config(key, value, ctx, data); |
864 | 0 | if (apply_failed) |
865 | 0 | return apply_failed; |
866 | | |
867 | 0 | return git_config_set_multivar_gently(key, |
868 | 0 | value ? value : "true", |
869 | 0 | CONFIG_REGEX_NONE, 0); |
870 | 0 | } |
871 | | |
872 | | static void write_config(struct string_list *config) |
873 | 0 | { |
874 | 0 | int i; |
875 | |
|
876 | 0 | for (i = 0; i < config->nr; i++) { |
877 | 0 | if (git_config_parse_parameter(config->items[i].string, |
878 | 0 | write_one_config, NULL) < 0) |
879 | 0 | die(_("unable to write parameters to config file")); |
880 | 0 | } |
881 | 0 | } |
882 | | |
883 | | static void write_refspec_config(const char *src_ref_prefix, |
884 | | const struct ref *our_head_points_at, |
885 | | const struct ref *remote_head_points_at, |
886 | | struct strbuf *branch_top) |
887 | 0 | { |
888 | 0 | struct strbuf key = STRBUF_INIT; |
889 | 0 | struct strbuf value = STRBUF_INIT; |
890 | |
|
891 | 0 | if (option_mirror || !option_bare) { |
892 | 0 | if (option_single_branch && !option_mirror) { |
893 | 0 | if (option_branch) { |
894 | 0 | if (starts_with(our_head_points_at->name, "refs/tags/")) |
895 | 0 | strbuf_addf(&value, "+%s:%s", our_head_points_at->name, |
896 | 0 | our_head_points_at->name); |
897 | 0 | else |
898 | 0 | strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name, |
899 | 0 | branch_top->buf, option_branch); |
900 | 0 | } else if (remote_head_points_at) { |
901 | 0 | const char *head = remote_head_points_at->name; |
902 | 0 | if (!skip_prefix(head, "refs/heads/", &head)) |
903 | 0 | BUG("remote HEAD points at non-head?"); |
904 | | |
905 | 0 | strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name, |
906 | 0 | branch_top->buf, head); |
907 | 0 | } |
908 | | /* |
909 | | * otherwise, the next "git fetch" will |
910 | | * simply fetch from HEAD without updating |
911 | | * any remote-tracking branch, which is what |
912 | | * we want. |
913 | | */ |
914 | 0 | } else { |
915 | 0 | strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf); |
916 | 0 | } |
917 | | /* Configure the remote */ |
918 | 0 | if (value.len) { |
919 | 0 | strbuf_addf(&key, "remote.%s.fetch", remote_name); |
920 | 0 | git_config_set_multivar(key.buf, value.buf, "^$", 0); |
921 | 0 | strbuf_reset(&key); |
922 | |
|
923 | 0 | if (option_mirror) { |
924 | 0 | strbuf_addf(&key, "remote.%s.mirror", remote_name); |
925 | 0 | git_config_set(key.buf, "true"); |
926 | 0 | strbuf_reset(&key); |
927 | 0 | } |
928 | 0 | } |
929 | 0 | } |
930 | | |
931 | 0 | strbuf_release(&key); |
932 | 0 | strbuf_release(&value); |
933 | 0 | } |
934 | | |
935 | | static void dissociate_from_references(void) |
936 | 0 | { |
937 | 0 | char *alternates = git_pathdup("objects/info/alternates"); |
938 | |
|
939 | 0 | if (!access(alternates, F_OK)) { |
940 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
941 | |
|
942 | 0 | cmd.git_cmd = 1; |
943 | 0 | cmd.no_stdin = 1; |
944 | 0 | strvec_pushl(&cmd.args, "repack", "-a", "-d", NULL); |
945 | 0 | if (run_command(&cmd)) |
946 | 0 | die(_("cannot repack to clean up")); |
947 | 0 | if (unlink(alternates) && errno != ENOENT) |
948 | 0 | die_errno(_("cannot unlink temporary alternates file")); |
949 | 0 | } |
950 | 0 | free(alternates); |
951 | 0 | } |
952 | | |
953 | | static int path_exists(const char *path) |
954 | 0 | { |
955 | 0 | struct stat sb; |
956 | 0 | return !stat(path, &sb); |
957 | 0 | } |
958 | | |
959 | | int cmd_clone(int argc, const char **argv, const char *prefix) |
960 | 0 | { |
961 | 0 | int is_bundle = 0, is_local; |
962 | 0 | int reject_shallow = 0; |
963 | 0 | const char *repo_name, *repo, *work_tree, *git_dir; |
964 | 0 | char *repo_to_free = NULL; |
965 | 0 | char *path = NULL, *dir, *display_repo = NULL; |
966 | 0 | int dest_exists, real_dest_exists = 0; |
967 | 0 | const struct ref *refs, *remote_head; |
968 | 0 | struct ref *remote_head_points_at = NULL; |
969 | 0 | const struct ref *our_head_points_at; |
970 | 0 | char *unborn_head = NULL; |
971 | 0 | struct ref *mapped_refs = NULL; |
972 | 0 | const struct ref *ref; |
973 | 0 | struct strbuf key = STRBUF_INIT; |
974 | 0 | struct strbuf buf = STRBUF_INIT; |
975 | 0 | struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT; |
976 | 0 | struct transport *transport = NULL; |
977 | 0 | const char *src_ref_prefix = "refs/heads/"; |
978 | 0 | struct remote *remote; |
979 | 0 | int err = 0, complete_refs_before_fetch = 1; |
980 | 0 | int submodule_progress; |
981 | 0 | int filter_submodules = 0; |
982 | 0 | int hash_algo; |
983 | 0 | enum ref_storage_format ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN; |
984 | 0 | const int do_not_override_repo_unix_permissions = -1; |
985 | |
|
986 | 0 | struct transport_ls_refs_options transport_ls_refs_options = |
987 | 0 | TRANSPORT_LS_REFS_OPTIONS_INIT; |
988 | |
|
989 | 0 | packet_trace_identity("clone"); |
990 | |
|
991 | 0 | git_config(git_clone_config, NULL); |
992 | |
|
993 | 0 | argc = parse_options(argc, argv, prefix, builtin_clone_options, |
994 | 0 | builtin_clone_usage, 0); |
995 | |
|
996 | 0 | if (argc > 2) |
997 | 0 | usage_msg_opt(_("Too many arguments."), |
998 | 0 | builtin_clone_usage, builtin_clone_options); |
999 | | |
1000 | 0 | if (argc == 0) |
1001 | 0 | usage_msg_opt(_("You must specify a repository to clone."), |
1002 | 0 | builtin_clone_usage, builtin_clone_options); |
1003 | | |
1004 | 0 | if (option_depth || option_since || option_not.nr) |
1005 | 0 | deepen = 1; |
1006 | 0 | if (option_single_branch == -1) |
1007 | 0 | option_single_branch = deepen ? 1 : 0; |
1008 | |
|
1009 | 0 | if (ref_format) { |
1010 | 0 | ref_storage_format = ref_storage_format_by_name(ref_format); |
1011 | 0 | if (ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN) |
1012 | 0 | die(_("unknown ref storage format '%s'"), ref_format); |
1013 | 0 | } |
1014 | | |
1015 | 0 | if (option_mirror) |
1016 | 0 | option_bare = 1; |
1017 | |
|
1018 | 0 | if (option_bare) { |
1019 | 0 | if (real_git_dir) |
1020 | 0 | die(_("options '%s' and '%s' cannot be used together"), "--bare", "--separate-git-dir"); |
1021 | 0 | option_no_checkout = 1; |
1022 | 0 | } |
1023 | | |
1024 | 0 | if (bundle_uri && deepen) |
1025 | 0 | die(_("options '%s' and '%s' cannot be used together"), |
1026 | 0 | "--bundle-uri", |
1027 | 0 | "--depth/--shallow-since/--shallow-exclude"); |
1028 | | |
1029 | 0 | repo_name = argv[0]; |
1030 | |
|
1031 | 0 | path = get_repo_path(repo_name, &is_bundle); |
1032 | 0 | if (path) { |
1033 | 0 | FREE_AND_NULL(path); |
1034 | 0 | repo = repo_to_free = absolute_pathdup(repo_name); |
1035 | 0 | } else if (strchr(repo_name, ':')) { |
1036 | 0 | repo = repo_name; |
1037 | 0 | display_repo = transport_anonymize_url(repo); |
1038 | 0 | } else |
1039 | 0 | die(_("repository '%s' does not exist"), repo_name); |
1040 | | |
1041 | | /* no need to be strict, transport_set_option() will validate it again */ |
1042 | 0 | if (option_depth && atoi(option_depth) < 1) |
1043 | 0 | die(_("depth %s is not a positive number"), option_depth); |
1044 | | |
1045 | 0 | if (argc == 2) |
1046 | 0 | dir = xstrdup(argv[1]); |
1047 | 0 | else |
1048 | 0 | dir = git_url_basename(repo_name, is_bundle, option_bare); |
1049 | 0 | strip_dir_trailing_slashes(dir); |
1050 | |
|
1051 | 0 | dest_exists = path_exists(dir); |
1052 | 0 | if (dest_exists && !is_empty_dir(dir)) |
1053 | 0 | die(_("destination path '%s' already exists and is not " |
1054 | 0 | "an empty directory."), dir); |
1055 | | |
1056 | 0 | if (real_git_dir) { |
1057 | 0 | real_dest_exists = path_exists(real_git_dir); |
1058 | 0 | if (real_dest_exists && !is_empty_dir(real_git_dir)) |
1059 | 0 | die(_("repository path '%s' already exists and is not " |
1060 | 0 | "an empty directory."), real_git_dir); |
1061 | 0 | } |
1062 | | |
1063 | | |
1064 | 0 | strbuf_addf(&reflog_msg, "clone: from %s", |
1065 | 0 | display_repo ? display_repo : repo); |
1066 | 0 | free(display_repo); |
1067 | |
|
1068 | 0 | if (option_bare) |
1069 | 0 | work_tree = NULL; |
1070 | 0 | else { |
1071 | 0 | work_tree = getenv("GIT_WORK_TREE"); |
1072 | 0 | if (work_tree && path_exists(work_tree)) |
1073 | 0 | die(_("working tree '%s' already exists."), work_tree); |
1074 | 0 | } |
1075 | | |
1076 | 0 | if (option_bare || work_tree) |
1077 | 0 | git_dir = xstrdup(dir); |
1078 | 0 | else { |
1079 | 0 | work_tree = dir; |
1080 | 0 | git_dir = mkpathdup("%s/.git", dir); |
1081 | 0 | } |
1082 | |
|
1083 | 0 | atexit(remove_junk); |
1084 | 0 | sigchain_push_common(remove_junk_on_signal); |
1085 | |
|
1086 | 0 | if (!option_bare) { |
1087 | 0 | if (safe_create_leading_directories_const(work_tree) < 0) |
1088 | 0 | die_errno(_("could not create leading directories of '%s'"), |
1089 | 0 | work_tree); |
1090 | 0 | if (dest_exists) |
1091 | 0 | junk_work_tree_flags |= REMOVE_DIR_KEEP_TOPLEVEL; |
1092 | 0 | else if (mkdir(work_tree, 0777)) |
1093 | 0 | die_errno(_("could not create work tree dir '%s'"), |
1094 | 0 | work_tree); |
1095 | 0 | junk_work_tree = work_tree; |
1096 | 0 | set_git_work_tree(work_tree); |
1097 | 0 | } |
1098 | | |
1099 | 0 | if (real_git_dir) { |
1100 | 0 | if (real_dest_exists) |
1101 | 0 | junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL; |
1102 | 0 | junk_git_dir = real_git_dir; |
1103 | 0 | } else { |
1104 | 0 | if (dest_exists) |
1105 | 0 | junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL; |
1106 | 0 | junk_git_dir = git_dir; |
1107 | 0 | } |
1108 | 0 | if (safe_create_leading_directories_const(git_dir) < 0) |
1109 | 0 | die(_("could not create leading directories of '%s'"), git_dir); |
1110 | | |
1111 | 0 | if (0 <= option_verbosity) { |
1112 | 0 | if (option_bare) |
1113 | 0 | fprintf(stderr, _("Cloning into bare repository '%s'...\n"), dir); |
1114 | 0 | else |
1115 | 0 | fprintf(stderr, _("Cloning into '%s'...\n"), dir); |
1116 | 0 | } |
1117 | |
|
1118 | 0 | if (option_recurse_submodules.nr > 0) { |
1119 | 0 | struct string_list_item *item; |
1120 | 0 | struct strbuf sb = STRBUF_INIT; |
1121 | 0 | int val; |
1122 | | |
1123 | | /* remove duplicates */ |
1124 | 0 | string_list_sort(&option_recurse_submodules); |
1125 | 0 | string_list_remove_duplicates(&option_recurse_submodules, 0); |
1126 | | |
1127 | | /* |
1128 | | * NEEDSWORK: In a multi-working-tree world, this needs to be |
1129 | | * set in the per-worktree config. |
1130 | | */ |
1131 | 0 | for_each_string_list_item(item, &option_recurse_submodules) { |
1132 | 0 | strbuf_addf(&sb, "submodule.active=%s", |
1133 | 0 | item->string); |
1134 | 0 | string_list_append(&option_config, |
1135 | 0 | strbuf_detach(&sb, NULL)); |
1136 | 0 | } |
1137 | |
|
1138 | 0 | if (!git_config_get_bool("submodule.stickyRecursiveClone", &val) && |
1139 | 0 | val) |
1140 | 0 | string_list_append(&option_config, "submodule.recurse=true"); |
1141 | |
|
1142 | 0 | if (option_required_reference.nr && |
1143 | 0 | option_optional_reference.nr) |
1144 | 0 | die(_("clone --recursive is not compatible with " |
1145 | 0 | "both --reference and --reference-if-able")); |
1146 | 0 | else if (option_required_reference.nr) { |
1147 | 0 | string_list_append(&option_config, |
1148 | 0 | "submodule.alternateLocation=superproject"); |
1149 | 0 | string_list_append(&option_config, |
1150 | 0 | "submodule.alternateErrorStrategy=die"); |
1151 | 0 | } else if (option_optional_reference.nr) { |
1152 | 0 | string_list_append(&option_config, |
1153 | 0 | "submodule.alternateLocation=superproject"); |
1154 | 0 | string_list_append(&option_config, |
1155 | 0 | "submodule.alternateErrorStrategy=info"); |
1156 | 0 | } |
1157 | 0 | } |
1158 | | |
1159 | | /* |
1160 | | * Initialize the repository, but skip initializing the reference |
1161 | | * database. We do not yet know about the object format of the |
1162 | | * repository, and reference backends may persist that information into |
1163 | | * their on-disk data structures. |
1164 | | */ |
1165 | 0 | init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, |
1166 | 0 | ref_storage_format, NULL, |
1167 | 0 | do_not_override_repo_unix_permissions, INIT_DB_QUIET | INIT_DB_SKIP_REFDB); |
1168 | |
|
1169 | 0 | if (real_git_dir) { |
1170 | 0 | free((char *)git_dir); |
1171 | 0 | git_dir = real_git_dir; |
1172 | 0 | } |
1173 | | |
1174 | | /* |
1175 | | * We have a chicken-and-egg situation between initializing the refdb |
1176 | | * and spawning transport helpers: |
1177 | | * |
1178 | | * - Initializing the refdb requires us to know about the object |
1179 | | * format. We thus have to spawn the transport helper to learn |
1180 | | * about it. |
1181 | | * |
1182 | | * - The transport helper may want to access the Git repository. But |
1183 | | * because the refdb has not been initialized, we don't have "HEAD" |
1184 | | * or "refs/". Thus, the helper cannot find the Git repository. |
1185 | | * |
1186 | | * Ideally, we would have structured the helper protocol such that it's |
1187 | | * mandatory for the helper to first announce its capabilities without |
1188 | | * yet assuming a fully initialized repository. Like that, we could |
1189 | | * have added a "lazy-refdb-init" capability that announces whether the |
1190 | | * helper is ready to handle not-yet-initialized refdbs. If any helper |
1191 | | * didn't support them, we would have fully initialized the refdb with |
1192 | | * the SHA1 object format, but later on bailed out if we found out that |
1193 | | * the remote repository used a different object format. |
1194 | | * |
1195 | | * But we didn't, and thus we use the following workaround to partially |
1196 | | * initialize the repository's refdb such that it can be discovered by |
1197 | | * Git commands. To do so, we: |
1198 | | * |
1199 | | * - Create an invalid HEAD ref pointing at "refs/heads/.invalid". |
1200 | | * |
1201 | | * - Create the "refs/" directory. |
1202 | | * |
1203 | | * - Set up the ref storage format and repository version as |
1204 | | * required. |
1205 | | * |
1206 | | * This is sufficient for Git commands to discover the Git directory. |
1207 | | */ |
1208 | 0 | initialize_repository_version(GIT_HASH_UNKNOWN, |
1209 | 0 | the_repository->ref_storage_format, 1); |
1210 | |
|
1211 | 0 | strbuf_addf(&buf, "%s/HEAD", git_dir); |
1212 | 0 | write_file(buf.buf, "ref: refs/heads/.invalid"); |
1213 | |
|
1214 | 0 | strbuf_reset(&buf); |
1215 | 0 | strbuf_addf(&buf, "%s/refs", git_dir); |
1216 | 0 | safe_create_dir(buf.buf, 1); |
1217 | | |
1218 | | /* |
1219 | | * additional config can be injected with -c, make sure it's included |
1220 | | * after init_db, which clears the entire config environment. |
1221 | | */ |
1222 | 0 | write_config(&option_config); |
1223 | | |
1224 | | /* |
1225 | | * re-read config after init_db and write_config to pick up any config |
1226 | | * injected by --template and --config, respectively. |
1227 | | */ |
1228 | 0 | git_config(git_clone_config, NULL); |
1229 | | |
1230 | | /* |
1231 | | * If option_reject_shallow is specified from CLI option, |
1232 | | * ignore config_reject_shallow from git_clone_config. |
1233 | | */ |
1234 | 0 | if (config_reject_shallow != -1) |
1235 | 0 | reject_shallow = config_reject_shallow; |
1236 | 0 | if (option_reject_shallow != -1) |
1237 | 0 | reject_shallow = option_reject_shallow; |
1238 | | |
1239 | | /* |
1240 | | * If option_filter_submodules is specified from CLI option, |
1241 | | * ignore config_filter_submodules from git_clone_config. |
1242 | | */ |
1243 | 0 | if (config_filter_submodules != -1) |
1244 | 0 | filter_submodules = config_filter_submodules; |
1245 | 0 | if (option_filter_submodules != -1) |
1246 | 0 | filter_submodules = option_filter_submodules; |
1247 | | |
1248 | | /* |
1249 | | * Exit if the user seems to be doing something silly with submodule |
1250 | | * filter flags (but not with filter configs, as those should be |
1251 | | * set-and-forget). |
1252 | | */ |
1253 | 0 | if (option_filter_submodules > 0 && !filter_options.choice) |
1254 | 0 | die(_("the option '%s' requires '%s'"), |
1255 | 0 | "--also-filter-submodules", "--filter"); |
1256 | 0 | if (option_filter_submodules > 0 && !option_recurse_submodules.nr) |
1257 | 0 | die(_("the option '%s' requires '%s'"), |
1258 | 0 | "--also-filter-submodules", "--recurse-submodules"); |
1259 | | |
1260 | | /* |
1261 | | * apply the remote name provided by --origin only after this second |
1262 | | * call to git_config, to ensure it overrides all config-based values. |
1263 | | */ |
1264 | 0 | if (option_origin) { |
1265 | 0 | free(remote_name); |
1266 | 0 | remote_name = xstrdup(option_origin); |
1267 | 0 | } |
1268 | |
|
1269 | 0 | if (!remote_name) |
1270 | 0 | remote_name = xstrdup("origin"); |
1271 | |
|
1272 | 0 | if (!valid_remote_name(remote_name)) |
1273 | 0 | die(_("'%s' is not a valid remote name"), remote_name); |
1274 | | |
1275 | 0 | if (option_bare) { |
1276 | 0 | if (option_mirror) |
1277 | 0 | src_ref_prefix = "refs/"; |
1278 | 0 | strbuf_addstr(&branch_top, src_ref_prefix); |
1279 | |
|
1280 | 0 | git_config_set("core.bare", "true"); |
1281 | 0 | } else { |
1282 | 0 | strbuf_addf(&branch_top, "refs/remotes/%s/", remote_name); |
1283 | 0 | } |
1284 | |
|
1285 | 0 | strbuf_addf(&key, "remote.%s.url", remote_name); |
1286 | 0 | git_config_set(key.buf, repo); |
1287 | 0 | strbuf_reset(&key); |
1288 | |
|
1289 | 0 | if (option_no_tags) { |
1290 | 0 | strbuf_addf(&key, "remote.%s.tagOpt", remote_name); |
1291 | 0 | git_config_set(key.buf, "--no-tags"); |
1292 | 0 | strbuf_reset(&key); |
1293 | 0 | } |
1294 | |
|
1295 | 0 | if (option_required_reference.nr || option_optional_reference.nr) |
1296 | 0 | setup_reference(); |
1297 | |
|
1298 | 0 | remote = remote_get_early(remote_name); |
1299 | |
|
1300 | 0 | refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix, |
1301 | 0 | branch_top.buf); |
1302 | |
|
1303 | 0 | path = get_repo_path(remote->url.v[0], &is_bundle); |
1304 | 0 | is_local = option_local != 0 && path && !is_bundle; |
1305 | 0 | if (is_local) { |
1306 | 0 | if (option_depth) |
1307 | 0 | warning(_("--depth is ignored in local clones; use file:// instead.")); |
1308 | 0 | if (option_since) |
1309 | 0 | warning(_("--shallow-since is ignored in local clones; use file:// instead.")); |
1310 | 0 | if (option_not.nr) |
1311 | 0 | warning(_("--shallow-exclude is ignored in local clones; use file:// instead.")); |
1312 | 0 | if (filter_options.choice) |
1313 | 0 | warning(_("--filter is ignored in local clones; use file:// instead.")); |
1314 | 0 | if (!access(mkpath("%s/shallow", path), F_OK)) { |
1315 | 0 | if (reject_shallow) |
1316 | 0 | die(_("source repository is shallow, reject to clone.")); |
1317 | 0 | if (option_local > 0) |
1318 | 0 | warning(_("source repository is shallow, ignoring --local")); |
1319 | 0 | is_local = 0; |
1320 | 0 | } |
1321 | 0 | } |
1322 | 0 | if (option_local > 0 && !is_local) |
1323 | 0 | warning(_("--local is ignored")); |
1324 | |
|
1325 | 0 | transport = transport_get(remote, path ? path : remote->url.v[0]); |
1326 | 0 | transport_set_verbosity(transport, option_verbosity, option_progress); |
1327 | 0 | transport->family = family; |
1328 | 0 | transport->cloning = 1; |
1329 | |
|
1330 | 0 | if (is_bundle) { |
1331 | 0 | struct bundle_header header = BUNDLE_HEADER_INIT; |
1332 | 0 | int fd = read_bundle_header(path, &header); |
1333 | 0 | int has_filter = header.filter.choice != LOFC_DISABLED; |
1334 | |
|
1335 | 0 | if (fd > 0) |
1336 | 0 | close(fd); |
1337 | 0 | bundle_header_release(&header); |
1338 | 0 | if (has_filter) |
1339 | 0 | die(_("cannot clone from filtered bundle")); |
1340 | 0 | } |
1341 | | |
1342 | 0 | transport_set_option(transport, TRANS_OPT_KEEP, "yes"); |
1343 | |
|
1344 | 0 | if (reject_shallow) |
1345 | 0 | transport_set_option(transport, TRANS_OPT_REJECT_SHALLOW, "1"); |
1346 | 0 | if (option_depth) |
1347 | 0 | transport_set_option(transport, TRANS_OPT_DEPTH, |
1348 | 0 | option_depth); |
1349 | 0 | if (option_since) |
1350 | 0 | transport_set_option(transport, TRANS_OPT_DEEPEN_SINCE, |
1351 | 0 | option_since); |
1352 | 0 | if (option_not.nr) |
1353 | 0 | transport_set_option(transport, TRANS_OPT_DEEPEN_NOT, |
1354 | 0 | (const char *)&option_not); |
1355 | 0 | if (option_single_branch) |
1356 | 0 | transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1"); |
1357 | |
|
1358 | 0 | if (option_upload_pack) |
1359 | 0 | transport_set_option(transport, TRANS_OPT_UPLOADPACK, |
1360 | 0 | option_upload_pack); |
1361 | |
|
1362 | 0 | if (server_options.nr) |
1363 | 0 | transport->server_options = &server_options; |
1364 | |
|
1365 | 0 | if (filter_options.choice) { |
1366 | 0 | const char *spec = |
1367 | 0 | expand_list_objects_filter_spec(&filter_options); |
1368 | 0 | transport_set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, |
1369 | 0 | spec); |
1370 | 0 | transport_set_option(transport, TRANS_OPT_FROM_PROMISOR, "1"); |
1371 | 0 | } |
1372 | |
|
1373 | 0 | if (transport->smart_options && !deepen && !filter_options.choice) |
1374 | 0 | transport->smart_options->check_self_contained_and_connected = 1; |
1375 | |
|
1376 | 0 | strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD"); |
1377 | 0 | refspec_ref_prefixes(&remote->fetch, |
1378 | 0 | &transport_ls_refs_options.ref_prefixes); |
1379 | 0 | if (option_branch) |
1380 | 0 | expand_ref_prefix(&transport_ls_refs_options.ref_prefixes, |
1381 | 0 | option_branch); |
1382 | 0 | if (!option_no_tags) |
1383 | 0 | strvec_push(&transport_ls_refs_options.ref_prefixes, |
1384 | 0 | "refs/tags/"); |
1385 | |
|
1386 | 0 | refs = transport_get_remote_refs(transport, &transport_ls_refs_options); |
1387 | | |
1388 | | /* |
1389 | | * Now that we know what algorithm the remote side is using, let's set |
1390 | | * ours to the same thing. |
1391 | | */ |
1392 | 0 | hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport)); |
1393 | 0 | initialize_repository_version(hash_algo, the_repository->ref_storage_format, 1); |
1394 | 0 | repo_set_hash_algo(the_repository, hash_algo); |
1395 | 0 | create_reference_database(the_repository->ref_storage_format, NULL, 1); |
1396 | | |
1397 | | /* |
1398 | | * Before fetching from the remote, download and install bundle |
1399 | | * data from the --bundle-uri option. |
1400 | | */ |
1401 | 0 | if (bundle_uri) { |
1402 | 0 | int has_heuristic = 0; |
1403 | | |
1404 | | /* At this point, we need the_repository to match the cloned repo. */ |
1405 | 0 | if (repo_init(the_repository, git_dir, work_tree)) |
1406 | 0 | warning(_("failed to initialize the repo, skipping bundle URI")); |
1407 | 0 | else if (fetch_bundle_uri(the_repository, bundle_uri, &has_heuristic)) |
1408 | 0 | warning(_("failed to fetch objects from bundle URI '%s'"), |
1409 | 0 | bundle_uri); |
1410 | 0 | else if (has_heuristic) |
1411 | 0 | git_config_set_gently("fetch.bundleuri", bundle_uri); |
1412 | 0 | } else { |
1413 | | /* |
1414 | | * Populate transport->got_remote_bundle_uri and |
1415 | | * transport->bundle_uri. We might get nothing. |
1416 | | */ |
1417 | 0 | transport_get_remote_bundle_uri(transport); |
1418 | |
|
1419 | 0 | if (transport->bundles && |
1420 | 0 | hashmap_get_size(&transport->bundles->bundles)) { |
1421 | | /* At this point, we need the_repository to match the cloned repo. */ |
1422 | 0 | if (repo_init(the_repository, git_dir, work_tree)) |
1423 | 0 | warning(_("failed to initialize the repo, skipping bundle URI")); |
1424 | 0 | else if (fetch_bundle_list(the_repository, |
1425 | 0 | transport->bundles)) |
1426 | 0 | warning(_("failed to fetch advertised bundles")); |
1427 | 0 | } else { |
1428 | 0 | clear_bundle_list(transport->bundles); |
1429 | 0 | FREE_AND_NULL(transport->bundles); |
1430 | 0 | } |
1431 | 0 | } |
1432 | |
|
1433 | 0 | if (refs) |
1434 | 0 | mapped_refs = wanted_peer_refs(refs, &remote->fetch); |
1435 | |
|
1436 | 0 | if (mapped_refs) { |
1437 | | /* |
1438 | | * transport_get_remote_refs() may return refs with null sha-1 |
1439 | | * in mapped_refs (see struct transport->get_refs_list |
1440 | | * comment). In that case we need fetch it early because |
1441 | | * remote_head code below relies on it. |
1442 | | * |
1443 | | * for normal clones, transport_get_remote_refs() should |
1444 | | * return reliable ref set, we can delay cloning until after |
1445 | | * remote HEAD check. |
1446 | | */ |
1447 | 0 | for (ref = refs; ref; ref = ref->next) |
1448 | 0 | if (is_null_oid(&ref->old_oid)) { |
1449 | 0 | complete_refs_before_fetch = 0; |
1450 | 0 | break; |
1451 | 0 | } |
1452 | |
|
1453 | 0 | if (!is_local && !complete_refs_before_fetch) { |
1454 | 0 | if (transport_fetch_refs(transport, mapped_refs)) |
1455 | 0 | die(_("remote transport reported error")); |
1456 | 0 | } |
1457 | 0 | } |
1458 | | |
1459 | 0 | remote_head = find_ref_by_name(refs, "HEAD"); |
1460 | 0 | remote_head_points_at = guess_remote_head(remote_head, mapped_refs, 0); |
1461 | |
|
1462 | 0 | if (option_branch) { |
1463 | 0 | our_head_points_at = find_remote_branch(mapped_refs, option_branch); |
1464 | 0 | if (!our_head_points_at) |
1465 | 0 | die(_("Remote branch %s not found in upstream %s"), |
1466 | 0 | option_branch, remote_name); |
1467 | 0 | } else if (remote_head_points_at) { |
1468 | 0 | our_head_points_at = remote_head_points_at; |
1469 | 0 | } else if (remote_head) { |
1470 | 0 | our_head_points_at = NULL; |
1471 | 0 | } else { |
1472 | 0 | char *to_free = NULL; |
1473 | 0 | const char *branch; |
1474 | |
|
1475 | 0 | if (!mapped_refs) { |
1476 | 0 | warning(_("You appear to have cloned an empty repository.")); |
1477 | 0 | option_no_checkout = 1; |
1478 | 0 | } |
1479 | |
|
1480 | 0 | if (transport_ls_refs_options.unborn_head_target && |
1481 | 0 | skip_prefix(transport_ls_refs_options.unborn_head_target, |
1482 | 0 | "refs/heads/", &branch)) { |
1483 | 0 | unborn_head = xstrdup(transport_ls_refs_options.unborn_head_target); |
1484 | 0 | } else { |
1485 | 0 | branch = to_free = repo_default_branch_name(the_repository, 0); |
1486 | 0 | unborn_head = xstrfmt("refs/heads/%s", branch); |
1487 | 0 | } |
1488 | | |
1489 | | /* |
1490 | | * We may have selected a local default branch name "foo", |
1491 | | * and even though the remote's HEAD does not point there, |
1492 | | * it may still have a "foo" branch. If so, set it up so |
1493 | | * that we can follow the usual checkout code later. |
1494 | | * |
1495 | | * Note that for an empty repo we'll already have set |
1496 | | * option_no_checkout above, which would work against us here. |
1497 | | * But for an empty repo, find_remote_branch() can never find |
1498 | | * a match. |
1499 | | */ |
1500 | 0 | our_head_points_at = find_remote_branch(mapped_refs, branch); |
1501 | |
|
1502 | 0 | free(to_free); |
1503 | 0 | } |
1504 | | |
1505 | 0 | write_refspec_config(src_ref_prefix, our_head_points_at, |
1506 | 0 | remote_head_points_at, &branch_top); |
1507 | |
|
1508 | 0 | if (filter_options.choice) |
1509 | 0 | partial_clone_register(remote_name, &filter_options); |
1510 | |
|
1511 | 0 | if (is_local) |
1512 | 0 | clone_local(path, git_dir); |
1513 | 0 | else if (mapped_refs && complete_refs_before_fetch) { |
1514 | 0 | if (transport_fetch_refs(transport, mapped_refs)) |
1515 | 0 | die(_("remote transport reported error")); |
1516 | 0 | } |
1517 | | |
1518 | 0 | update_remote_refs(refs, mapped_refs, remote_head_points_at, |
1519 | 0 | branch_top.buf, reflog_msg.buf, transport, |
1520 | 0 | !is_local); |
1521 | |
|
1522 | 0 | update_head(our_head_points_at, remote_head, unborn_head, reflog_msg.buf); |
1523 | | |
1524 | | /* |
1525 | | * We want to show progress for recursive submodule clones iff |
1526 | | * we did so for the main clone. But only the transport knows |
1527 | | * the final decision for this flag, so we need to rescue the value |
1528 | | * before we free the transport. |
1529 | | */ |
1530 | 0 | submodule_progress = transport->progress; |
1531 | |
|
1532 | 0 | transport_unlock_pack(transport, 0); |
1533 | 0 | transport_disconnect(transport); |
1534 | |
|
1535 | 0 | if (option_dissociate) { |
1536 | 0 | close_object_store(the_repository->objects); |
1537 | 0 | dissociate_from_references(); |
1538 | 0 | } |
1539 | |
|
1540 | 0 | if (option_sparse_checkout && git_sparse_checkout_init(dir)) |
1541 | 0 | return 1; |
1542 | | |
1543 | 0 | junk_mode = JUNK_LEAVE_REPO; |
1544 | 0 | err = checkout(submodule_progress, filter_submodules, |
1545 | 0 | ref_storage_format); |
1546 | |
|
1547 | 0 | free(remote_name); |
1548 | 0 | strbuf_release(&reflog_msg); |
1549 | 0 | strbuf_release(&branch_top); |
1550 | 0 | strbuf_release(&buf); |
1551 | 0 | strbuf_release(&key); |
1552 | 0 | free_refs(mapped_refs); |
1553 | 0 | free_refs(remote_head_points_at); |
1554 | 0 | free(unborn_head); |
1555 | 0 | free(dir); |
1556 | 0 | free(path); |
1557 | 0 | free(repo_to_free); |
1558 | 0 | UNLEAK(repo); |
1559 | 0 | junk_mode = JUNK_LEAVE_ALL; |
1560 | |
|
1561 | 0 | transport_ls_refs_options_release(&transport_ls_refs_options); |
1562 | 0 | return err; |
1563 | 0 | } |