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