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