Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/t1092-sparse-checkout-compatibility.sh # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / t1092-sparse-checkout-compatibility.sh` 2747 lines ยท 87204 bytes Raw Blame History 1 #!/bin/sh 2 3 test_description='compare full workdir to sparse workdir' 4 5 GIT_TEST_SPLIT_INDEX=0 6 GIT_TEST_SPARSE_INDEX= 7 8 . ./test-lib.sh 9 10 test_expect_success 'setup' ' 11 git init initial-repo && 12 ( 13 GIT_TEST_SPARSE_INDEX=0 && 14 cd initial-repo && 15 echo a >a && 16 echo "after deep" >e && 17 echo "after folder1" >g && 18 echo "after x" >z && 19 mkdir folder1 folder2 deep before x && 20 echo "before deep" >before/a && 21 echo "before deep again" >before/b && 22 mkdir deep/deeper1 deep/deeper2 deep/before deep/later && 23 mkdir deep/deeper1/deepest && 24 mkdir deep/deeper1/deepest2 && 25 mkdir deep/deeper1/deepest3 && 26 echo "after deeper1" >deep/e && 27 echo "after deepest" >deep/deeper1/e && 28 cp a folder1 && 29 cp a folder2 && 30 cp a x && 31 cp a deep && 32 cp a deep/before && 33 cp a deep/deeper1 && 34 cp a deep/deeper2 && 35 cp a deep/later && 36 cp a deep/deeper1/deepest && 37 cp a deep/deeper1/deepest2 && 38 cp a deep/deeper1/deepest3 && 39 cp -r deep/deeper1/ deep/deeper2 && 40 mkdir deep/deeper1/0 && 41 mkdir deep/deeper1/0/0 && 42 touch deep/deeper1/0/1 && 43 touch deep/deeper1/0/0/0 && 44 >folder1- && 45 >folder1.x && 46 >folder10 && 47 cp -r deep/deeper1/0 folder1 && 48 cp -r deep/deeper1/0 folder2 && 49 echo >>folder1/0/0/0 && 50 echo >>folder2/0/1 && 51 git add . && 52 git commit -m "initial commit" && 53 git checkout -b base && 54 for dir in folder1 folder2 deep 55 do 56 git checkout -b update-$dir base && 57 echo "updated $dir" >$dir/a && 58 git commit -a -m "update $dir" || return 1 59 done && 60 61 git checkout -b rename-base base && 62 cat >folder1/larger-content <<-\EOF && 63 matching 64 lines 65 help 66 inexact 67 renames 68 EOF 69 cp folder1/larger-content folder2/ && 70 cp folder1/larger-content deep/deeper1/ && 71 git add . && 72 git commit -m "add interesting rename content" && 73 74 git checkout -b rename-out-to-out rename-base && 75 mv folder1/a folder2/b && 76 mv folder1/larger-content folder2/edited-content && 77 echo >>folder2/edited-content && 78 echo >>folder2/0/1 && 79 echo stuff >>deep/deeper1/a && 80 git add . && 81 git commit -m "rename folder1/... to folder2/..." && 82 83 git checkout -b rename-out-to-in rename-base && 84 mv folder1/a deep/deeper1/b && 85 echo more stuff >>deep/deeper1/a && 86 rm folder2/0/1 && 87 mkdir folder2/0/1 && 88 echo >>folder2/0/1/1 && 89 mv folder1/larger-content deep/deeper1/edited-content && 90 echo >>deep/deeper1/edited-content && 91 git add . && 92 git commit -m "rename folder1/... to deep/deeper1/..." && 93 94 git checkout -b rename-in-to-out rename-base && 95 mv deep/deeper1/a folder1/b && 96 echo >>folder2/0/1 && 97 rm -rf folder1/0/0 && 98 echo >>folder1/0/0 && 99 mv deep/deeper1/larger-content folder1/edited-content && 100 echo >>folder1/edited-content && 101 git add . && 102 git commit -m "rename deep/deeper1/... to folder1/..." && 103 104 git checkout -b df-conflict-1 base && 105 rm -rf folder1 && 106 echo content >folder1 && 107 git add . && 108 git commit -m "dir to file" && 109 110 git checkout -b df-conflict-2 base && 111 rm -rf folder2 && 112 echo content >folder2 && 113 git add . && 114 git commit -m "dir to file" && 115 116 git checkout -b fd-conflict base && 117 rm a && 118 mkdir a && 119 echo content >a/a && 120 git add . && 121 git commit -m "file to dir" && 122 123 for side in left right 124 do 125 git checkout -b merge-$side base && 126 echo $side >>deep/deeper2/a && 127 echo $side >>folder1/a && 128 echo $side >>folder2/a && 129 git add . && 130 git commit -m "$side" || return 1 131 done && 132 133 git checkout -b deepest base && 134 echo "updated deepest" >deep/deeper1/deepest/a && 135 echo "updated deepest2" >deep/deeper1/deepest2/a && 136 echo "updated deepest3" >deep/deeper1/deepest3/a && 137 git commit -a -m "update deepest" && 138 139 git checkout -f base && 140 git reset --hard 141 ) 142 ' 143 144 init_repos () { 145 rm -rf full-checkout sparse-checkout sparse-index && 146 147 # create repos in initial state 148 cp -r initial-repo full-checkout && 149 git -C full-checkout reset --hard && 150 151 cp -r initial-repo sparse-checkout && 152 git -C sparse-checkout reset --hard && 153 154 cp -r initial-repo sparse-index && 155 git -C sparse-index reset --hard && 156 157 # initialize sparse-checkout definitions 158 git -C sparse-checkout sparse-checkout init --cone && 159 git -C sparse-checkout sparse-checkout set deep && 160 git -C sparse-index sparse-checkout init --cone --sparse-index && 161 test_cmp_config -C sparse-index true index.sparse && 162 git -C sparse-index sparse-checkout set deep && 163 164 # Disable this message to keep stderr the same. 165 git -C sparse-index config advice.sparseIndexExpanded false 166 } 167 168 init_repos_as_submodules () { 169 git reset --hard && 170 init_repos && 171 git submodule add ./full-checkout && 172 git submodule add ./sparse-checkout && 173 git submodule add ./sparse-index && 174 175 git submodule status >actual && 176 grep full-checkout actual && 177 grep sparse-checkout actual && 178 grep sparse-index actual 179 } 180 181 run_on_sparse () { 182 cat >run-on-sparse-input && 183 184 ( 185 cd sparse-checkout && 186 GIT_PROGRESS_DELAY=100000 "$@" >../sparse-checkout-out 2>../sparse-checkout-err 187 ) ../sparse-index-out 2>../sparse-index-err 191 ) run-on-all-input && 196 197 ( 198 cd full-checkout && 199 GIT_PROGRESS_DELAY=100000 "$@" >../full-checkout-out 2>../full-checkout-err 200 ) diff && 224 test_must_be_empty diff || return 1 225 done 226 } 227 228 # Usage: test_sparse_checkout_set " ... " " ... " 229 # Verifies that "git sparse-checkout set ... " succeeds and 230 # leaves the sparse index in a state where ... are sparse 231 # directories (and ... are not). 232 test_sparse_checkout_set () { 233 CONE_DIRS=$1 && 234 SPARSE_DIRS=$2 && 235 git -C sparse-index sparse-checkout set --skip-checks $CONE_DIRS && 236 git -C sparse-index ls-files --sparse --stage >cache && 237 238 # Check that the directories outside of the sparse-checkout cone 239 # have sparse directory entries. 240 for dir in $SPARSE_DIRS 241 do 242 TREE=$(git -C sparse-index rev-parse HEAD:$dir) && 243 grep "040000 $TREE 0 $dir/" cache \ 244 || return 1 245 done && 246 247 # Check that the directories in the sparse-checkout cone 248 # are not sparse directory entries. 249 for dir in $CONE_DIRS 250 do 251 # Allow TREE to not exist because 252 # $dir does not exist at HEAD. 253 TREE=$(git -C sparse-index rev-parse HEAD:$dir) || 254 ! grep "040000 $TREE 0 $dir/" cache \ 255 || return 1 256 done 257 } 258 259 test_expect_success 'sparse-index contents' ' 260 init_repos && 261 262 # Remove deep, add three other directories. 263 test_sparse_checkout_set \ 264 "folder1 folder2 x" \ 265 "before deep" && 266 267 # Remove folder1, add deep 268 test_sparse_checkout_set \ 269 "deep folder2 x" \ 270 "before folder1" && 271 272 # Replace deep with deep/deeper2 (dropping deep/deeper1) 273 # Add folder1 274 test_sparse_checkout_set \ 275 "deep/deeper2 folder1 folder2 x" \ 276 "before deep/deeper1" && 277 278 # Replace deep/deeper2 with deep/deeper1 279 # Replace folder1 with folder1/0/0 280 # Replace folder2 with non-existent folder2/2/3 281 # Add non-existent "bogus" 282 test_sparse_checkout_set \ 283 "bogus deep/deeper1 folder1/0/0 folder2/2/3 x" \ 284 "before deep/deeper2 folder2/0" && 285 286 # Drop down to only files at root 287 test_sparse_checkout_set \ 288 "" \ 289 "before deep folder1 folder2 x" && 290 291 # Disabling the sparse-index replaces tree entries with full ones 292 git -C sparse-index sparse-checkout init --no-sparse-index && 293 test_sparse_match git ls-files --stage --sparse 294 ' 295 296 test_expect_success 'expanded in-memory index matches full index' ' 297 init_repos && 298 test_sparse_match git ls-files --stage 299 ' 300 301 test_expect_success 'root directory cannot be sparse' ' 302 init_repos && 303 304 # Remove all in-cone files and directories from the index, collapse index 305 # with `git sparse-checkout reapply` 306 git -C sparse-index rm -r . && 307 git -C sparse-index sparse-checkout reapply && 308 309 # Verify sparse directories still present, root directory is not sparse 310 cat >expect <<-EOF && 311 before/ 312 folder1/ 313 folder2/ 314 x/ 315 EOF 316 git -C sparse-index ls-files --sparse >actual && 317 test_cmp expect actual 318 ' 319 320 test_expect_success 'status with options' ' 321 init_repos && 322 test_sparse_match ls && 323 test_all_match git status --porcelain=v2 && 324 test_all_match git status --porcelain=v2 -z -u && 325 test_all_match git status --porcelain=v2 -uno && 326 run_on_all touch README.md && 327 test_all_match git status --porcelain=v2 && 328 test_all_match git status --porcelain=v2 -z -u && 329 test_all_match git status --porcelain=v2 -uno && 330 test_all_match git add README.md && 331 test_all_match git status --porcelain=v2 && 332 test_all_match git status --porcelain=v2 -z -u && 333 test_all_match git status --porcelain=v2 -uno 334 ' 335 336 test_expect_success 'status with diff in unexpanded sparse directory' ' 337 init_repos && 338 test_all_match git checkout rename-base && 339 test_all_match git reset --soft rename-out-to-out && 340 test_all_match git status --porcelain=v2 341 ' 342 343 test_expect_success 'status reports sparse-checkout' ' 344 init_repos && 345 git -C sparse-checkout status >full && 346 git -C sparse-index status >sparse && 347 test_grep "You are in a sparse checkout with " full && 348 test_grep "You are in a sparse checkout." sparse 349 ' 350 351 test_expect_success 'add, commit, checkout' ' 352 init_repos && 353 354 write_script edit-contents <<-\EOF && 355 echo text >>$1 356 EOF 357 run_on_all ../edit-contents README.md && 358 359 test_all_match git add README.md && 360 test_all_match git status --porcelain=v2 && 361 test_all_match git commit -m "Add README.md" && 362 363 test_all_match git checkout HEAD~1 && 364 test_all_match git checkout - && 365 366 run_on_all ../edit-contents README.md && 367 368 test_all_match git add -A && 369 test_all_match git status --porcelain=v2 && 370 test_all_match git commit -m "Extend README.md" && 371 372 test_all_match git checkout HEAD~1 && 373 test_all_match git checkout - && 374 375 run_on_all ../edit-contents deep/newfile && 376 377 test_all_match git status --porcelain=v2 -uno && 378 test_all_match git status --porcelain=v2 && 379 test_all_match git add . && 380 test_all_match git status --porcelain=v2 && 381 test_all_match git commit -m "add deep/newfile" && 382 383 test_all_match git checkout HEAD~1 && 384 test_all_match git checkout - 385 ' 386 387 test_expect_success 'intent-to-add entries outside sparse-checkout' ' 388 init_repos && 389 390 write_script edit-contents <<-\EOF && 391 echo text >>$1 392 EOF 393 394 test_sparse_match git sparse-checkout set deep folder1 && 395 run_on_sparse mkdir -p folder1 && 396 run_on_all ../edit-contents folder1/newita && 397 test_sparse_match git add -N folder1/newita && 398 399 test_sparse_match git sparse-checkout set deep && 400 test_sparse_match git status --porcelain=v2 && 401 test_sparse_match git ls-files --stage 402 ' 403 404 test_expect_success 'intent-to-add with --sparse outside sparse-checkout' ' 405 init_repos && 406 407 write_script edit-contents <<-\EOF && 408 echo text >>$1 409 EOF 410 411 run_on_all mkdir -p folder1 && 412 run_on_all ../edit-contents folder1/newita && 413 test_all_match git add --sparse --intent-to-add folder1/newita && 414 415 test_all_match git status --porcelain=v2 && 416 test_all_match git ls-files --stage && 417 test_all_match git diff --cached --stat && 418 419 # Ensure sparse index stores correct sparse directories and 420 # intent-to-add path. 421 git -C sparse-index ls-files --format="%(path)" --sparse >out && 422 423 # These paths should be present in index as-is. 424 test_grep "^before/\$" out && 425 test_grep "^folder1/newita\$" out && 426 test_grep "^folder2/\$" out && 427 test_grep "^x/\$" out && 428 429 # folder/0/ could theoretically be collapsed to a sparse 430 # directory entry, but the current implementation avoids the 431 # reduction because of folder1/newita 432 test_grep "^folder1/0/0/0\$" out 433 ' 434 435 test_expect_success 'git add, checkout, and reset with -p' ' 436 init_repos && 437 438 write_script edit-contents <<-\EOF && 439 echo text >>$1 440 EOF 441 442 # Does not expand when edits are within sparse checkout. 443 run_on_all ../edit-contents deep/a && 444 run_on_all ../edit-contents deep/deeper1/a && 445 446 test_write_lines y n >in && 447 run_on_all git add -p in && 452 run_on_all git add -i in && 459 run_on_all git add -p in && 463 run_on_all git add -i in && 469 test_sparse_match git checkout HEAD~1 --patch $2 517 EOF 518 519 run_on_all ../edit-file 1 a && 520 run_on_all ../edit-file 1 deep/a && 521 522 test_all_match git commit -m "-a" -a && 523 test_all_match git status --porcelain=v2 && 524 525 run_on_all ../edit-file 2 a && 526 run_on_all ../edit-file 2 deep/a && 527 528 test_all_match git commit -m "--include" --include deep/a && 529 test_all_match git status --porcelain=v2 && 530 test_all_match git commit -m "--include" --include a && 531 test_all_match git status --porcelain=v2 && 532 533 run_on_all ../edit-file 3 a && 534 run_on_all ../edit-file 3 deep/a && 535 536 test_all_match git commit -m "--amend" -a --amend && 537 test_all_match git status --porcelain=v2 538 ' 539 540 test_expect_success 'status/add: outside sparse cone' ' 541 init_repos && 542 543 # folder1 is at HEAD, but outside the sparse cone 544 run_on_sparse mkdir folder1 && 545 cp initial-repo/folder1/a sparse-checkout/folder1/a && 546 cp initial-repo/folder1/a sparse-index/folder1/a && 547 548 test_sparse_match git status && 549 550 write_script edit-contents <<-\EOF && 551 echo text >>$1 552 EOF 553 run_on_all ../edit-contents folder1/a && 554 run_on_all ../edit-contents folder1/new && 555 556 test_sparse_match git status --porcelain=v2 && 557 558 # Adding the path outside of the sparse-checkout cone should fail. 559 test_sparse_match test_must_fail git add folder1/a && 560 test_grep "Disable or modify the sparsity rules" sparse-checkout-err && 561 test_sparse_unstaged folder1/a && 562 test_all_match git add --refresh folder1/a && 563 test_must_be_empty sparse-checkout-err && 564 test_sparse_unstaged folder1/a && 565 test_sparse_match test_must_fail git add folder1/new && 566 test_grep "Disable or modify the sparsity rules" sparse-checkout-err && 567 test_sparse_unstaged folder1/new && 568 test_sparse_match git add --sparse folder1/a && 569 test_sparse_match git add --sparse folder1/new && 570 571 test_all_match git add --sparse . && 572 test_all_match git status --porcelain=v2 && 573 test_all_match git commit -m folder1/new && 574 test_all_match git rev-parse HEAD^{tree} && 575 576 run_on_all ../edit-contents folder1/newer && 577 test_all_match git add --sparse folder1/ && 578 test_all_match git status --porcelain=v2 && 579 test_all_match git commit -m folder1/newer && 580 test_all_match git rev-parse HEAD^{tree} 581 ' 582 583 test_expect_success 'checkout and reset --hard' ' 584 init_repos && 585 586 test_all_match git checkout update-folder1 && 587 test_all_match git status --porcelain=v2 && 588 589 test_all_match git checkout update-deep && 590 test_all_match git status --porcelain=v2 && 591 592 test_all_match git checkout -b reset-test && 593 test_all_match git reset --hard deepest && 594 test_all_match git reset --hard update-folder1 && 595 test_all_match git reset --hard update-folder2 596 ' 597 598 test_expect_success 'diff --cached' ' 599 init_repos && 600 601 write_script edit-contents <<-\EOF && 602 echo text >>README.md 603 EOF 604 run_on_all ../edit-contents && 605 606 test_all_match git diff && 607 test_all_match git diff --cached && 608 test_all_match git add README.md && 609 test_all_match git diff && 610 test_all_match git diff --cached 611 ' 612 613 # NEEDSWORK: sparse-checkout behaves differently from full-checkout when 614 # running this test with 'df-conflict-2' after 'df-conflict-1'. 615 test_expect_success 'diff with renames and conflicts' ' 616 init_repos && 617 618 for branch in rename-out-to-out \ 619 rename-out-to-in \ 620 rename-in-to-out \ 621 df-conflict-1 \ 622 fd-conflict 623 do 624 test_all_match git checkout rename-base && 625 test_all_match git checkout $branch -- . && 626 test_all_match git status --porcelain=v2 && 627 test_all_match git diff --cached --no-renames && 628 test_all_match git diff --cached --find-renames || return 1 629 done 630 ' 631 632 test_expect_success 'diff with directory/file conflicts' ' 633 init_repos && 634 635 for branch in rename-out-to-out \ 636 rename-out-to-in \ 637 rename-in-to-out \ 638 df-conflict-1 \ 639 df-conflict-2 \ 640 fd-conflict 641 do 642 git -C full-checkout reset --hard && 643 test_sparse_match git reset --hard && 644 test_all_match git checkout $branch && 645 test_all_match git checkout rename-base -- . && 646 test_all_match git status --porcelain=v2 && 647 test_all_match git diff --cached --no-renames && 648 test_all_match git diff --cached --find-renames || return 1 649 done 650 ' 651 652 test_expect_success 'log with pathspec outside sparse definition' ' 653 init_repos && 654 655 test_all_match git log -- a && 656 test_all_match git log -- folder1/a && 657 test_all_match git log -- folder2/a && 658 test_all_match git log -- deep/a && 659 test_all_match git log -- deep/deeper1/a && 660 test_all_match git log -- deep/deeper1/deepest/a && 661 662 test_all_match git checkout update-folder1 && 663 test_all_match git log -- folder1/a 664 ' 665 666 test_expect_success 'blame with pathspec inside sparse definition' ' 667 init_repos && 668 669 for file in a \ 670 deep/a \ 671 deep/deeper1/a \ 672 deep/deeper1/deepest/a 673 do 674 test_all_match git blame $file || return 1 675 done 676 ' 677 678 # Without a revision specified, blame will error if passed any file that 679 # is not present in the working directory (even if the file is tracked). 680 # Here we just verify that this is also true with sparse checkouts. 681 test_expect_success 'blame with pathspec outside sparse definition' ' 682 init_repos && 683 test_sparse_match git sparse-checkout set && 684 685 for file in \ 686 deep/a \ 687 deep/deeper1/a \ 688 deep/deeper1/deepest/a 689 do 690 test_sparse_match test_must_fail git blame $file && 691 cat >expect <<-EOF && 692 fatal: Cannot lstat '"'"'$file'"'"': No such file or directory 693 EOF 694 # We compare sparse-checkout-err and sparse-index-err in 695 # `test_sparse_match`. Given we know they are the same, we 696 # only check the content of sparse-index-err here. 697 test_cmp expect sparse-index-err || return 1 698 done 699 ' 700 701 test_expect_success 'checkout and reset (mixed)' ' 702 init_repos && 703 704 test_all_match git checkout -b reset-test update-deep && 705 test_all_match git reset deepest && 706 707 # Because skip-worktree is preserved, resetting to update-folder1 708 # will show worktree changes for folder1/a in full-checkout, but not 709 # in sparse-checkout or sparse-index. 710 git -C full-checkout reset update-folder1 >full-checkout-out && 711 test_sparse_match git reset update-folder1 && 712 test_grep "M folder1/a" full-checkout-out && 713 test_grep ! "M folder1/a" sparse-checkout-out && 714 run_on_sparse test_path_is_missing folder1 715 ' 716 717 test_expect_success 'checkout and reset (merge)' ' 718 init_repos && 719 720 write_script edit-contents <<-\EOF && 721 echo text >>$1 722 EOF 723 724 test_all_match git checkout -b reset-test update-deep && 725 run_on_all ../edit-contents a && 726 test_all_match git reset --merge deepest && 727 test_all_match git status --porcelain=v2 && 728 729 test_all_match git reset --hard update-deep && 730 run_on_all ../edit-contents deep/a && 731 test_all_match test_must_fail git reset --merge deepest 732 ' 733 734 test_expect_success 'checkout and reset (keep)' ' 735 init_repos && 736 737 write_script edit-contents <<-\EOF && 738 echo text >>$1 739 EOF 740 741 test_all_match git checkout -b reset-test update-deep && 742 run_on_all ../edit-contents a && 743 test_all_match git reset --keep deepest && 744 test_all_match git status --porcelain=v2 && 745 746 test_all_match git reset --hard update-deep && 747 run_on_all ../edit-contents deep/a && 748 test_all_match test_must_fail git reset --keep deepest 749 ' 750 751 test_expect_success 'reset with pathspecs inside sparse definition' ' 752 init_repos && 753 754 write_script edit-contents <<-\EOF && 755 echo text >>$1 756 EOF 757 758 test_all_match git checkout -b reset-test update-deep && 759 run_on_all ../edit-contents deep/a && 760 761 test_all_match git reset base -- deep/a && 762 test_all_match git status --porcelain=v2 && 763 764 test_all_match git reset base -- nonexistent-file && 765 test_all_match git status --porcelain=v2 && 766 767 test_all_match git reset deepest -- deep && 768 test_all_match git status --porcelain=v2 769 ' 770 771 # Although the working tree differs between full and sparse checkouts after 772 # reset, the state of the index is the same. 773 test_expect_success 'reset with pathspecs outside sparse definition' ' 774 init_repos && 775 test_all_match git checkout -b reset-test base && 776 777 test_sparse_match git reset update-folder1 -- folder1 && 778 git -C full-checkout reset update-folder1 -- folder1 && 779 test_all_match git ls-files -s -- folder1 && 780 781 test_sparse_match git reset update-folder2 -- folder2/a && 782 git -C full-checkout reset update-folder2 -- folder2/a && 783 test_all_match git ls-files -s -- folder2/a 784 ' 785 786 test_expect_success 'reset with wildcard pathspec' ' 787 init_repos && 788 789 test_all_match git reset update-deep -- deep\* && 790 test_all_match git ls-files -s -- deep && 791 792 test_all_match git reset deepest -- deep\*\*\* && 793 test_all_match git ls-files -s -- deep && 794 795 # The following `git reset`s result in updating the index on files with 796 # `skip-worktree` enabled. To avoid failing due to discrepancies in reported 797 # "modified" files, `test_sparse_match` reset is performed separately from 798 # "full-checkout" reset, then the index contents of all repos are verified. 799 800 test_sparse_match git reset update-folder1 -- \*/a && 801 git -C full-checkout reset update-folder1 -- \*/a && 802 test_all_match git ls-files -s -- deep/a folder1/a && 803 804 test_sparse_match git reset update-folder2 -- folder\* && 805 git -C full-checkout reset update-folder2 -- folder\* && 806 test_all_match git ls-files -s -- folder10 folder1 folder2 && 807 808 test_sparse_match git reset base -- folder1/\* && 809 git -C full-checkout reset base -- folder1/\* && 810 test_all_match git ls-files -s -- folder1 811 ' 812 813 test_expect_success 'reset hard with removed sparse dir' ' 814 init_repos && 815 816 run_on_all git rm -r --sparse folder1 && 817 test_all_match git status --porcelain=v2 && 818 819 test_all_match git reset --hard && 820 test_all_match git status --porcelain=v2 && 821 822 cat >expect <<-\EOF && 823 folder1/ 824 EOF 825 826 git -C sparse-index ls-files --sparse folder1 >out && 827 test_cmp expect out 828 ' 829 830 test_expect_success 'update-index modify outside sparse definition' ' 831 init_repos && 832 833 write_script edit-contents <<-\EOF && 834 echo text >>$1 835 EOF 836 837 # Create & modify folder1/a 838 # Note that this setup is a manual way of reaching the erroneous 839 # condition in which a `skip-worktree` enabled, outside-of-cone file 840 # exists on disk. It is used here to ensure `update-index` is stable 841 # and behaves predictably if such a condition occurs. 842 run_on_sparse mkdir -p folder1 && 843 run_on_sparse cp ../initial-repo/folder1/a folder1/a && 844 run_on_all ../edit-contents folder1/a && 845 846 # If file has skip-worktree enabled, but the file is present, it is 847 # treated the same as if skip-worktree is disabled 848 test_all_match git status --porcelain=v2 && 849 test_all_match git update-index folder1/a && 850 test_all_match git status --porcelain=v2 && 851 852 # When skip-worktree is disabled (even on files outside sparse cone), file 853 # is updated in the index 854 test_sparse_match git update-index --no-skip-worktree folder1/a && 855 test_all_match git status --porcelain=v2 && 856 test_all_match git update-index folder1/a && 857 test_all_match git status --porcelain=v2 858 ' 859 860 test_expect_success 'update-index --add outside sparse definition' ' 861 init_repos && 862 863 write_script edit-contents <<-\EOF && 864 echo text >>$1 865 EOF 866 867 # Create folder1, add new file 868 run_on_sparse mkdir -p folder1 && 869 run_on_all ../edit-contents folder1/b && 870 871 # The *untracked* out-of-cone file is added to the index because it does 872 # not have a `skip-worktree` bit to signal that it should be ignored 873 # (unlike in `git add`, which will fail due to the file being outside 874 # the sparse checkout definition). 875 test_all_match git update-index --add folder1/b && 876 test_all_match git status --porcelain=v2 877 ' 878 879 # NEEDSWORK: `--remove`, unlike the rest of `update-index`, does not ignore 880 # `skip-worktree` entries by default and will remove them from the index. 881 # The `--ignore-skip-worktree-entries` flag must be used in conjunction with 882 # `--remove` to ignore the `skip-worktree` entries and prevent their removal 883 # from the index. 884 test_expect_success 'update-index --remove outside sparse definition' ' 885 init_repos && 886 887 # When --ignore-skip-worktree-entries is _not_ specified: 888 # out-of-cone, not-on-disk files are removed from the index 889 test_sparse_match git update-index --remove folder1/a && 890 cat >expect <<-EOF && 891 D folder1/a 892 EOF 893 test_sparse_match git diff --cached --name-status && 894 test_cmp expect sparse-checkout-out && 895 896 test_sparse_match git diff-index --cached HEAD && 897 898 # Reset the state 899 test_all_match git reset --hard && 900 901 # When --ignore-skip-worktree-entries is specified, out-of-cone 902 # (skip-worktree) files are ignored 903 test_sparse_match git update-index --remove --ignore-skip-worktree-entries folder1/a && 904 test_sparse_match git diff --cached --name-status && 905 test_must_be_empty sparse-checkout-out && 906 907 test_sparse_match git diff-index --cached HEAD && 908 909 # Reset the state 910 test_all_match git reset --hard && 911 912 # --force-remove supersedes --ignore-skip-worktree-entries, removing 913 # a skip-worktree file from the index (and disk) when both are specified 914 # with --remove 915 test_sparse_match git update-index --force-remove --ignore-skip-worktree-entries folder1/a && 916 cat >expect <<-EOF && 917 D folder1/a 918 EOF 919 test_sparse_match git diff --cached --name-status && 920 test_cmp expect sparse-checkout-out && 921 922 test_sparse_match git diff-index --cached HEAD 923 ' 924 925 test_expect_success 'update-index with directories' ' 926 init_repos && 927 928 # update-index will exit silently when provided with a directory name 929 # containing a trailing slash 930 test_all_match git update-index deep/ folder1/ && 931 test_grep "Ignoring path deep/" sparse-checkout-err && 932 test_grep "Ignoring path folder1/" sparse-checkout-err && 933 934 # When update-index is given a directory name WITHOUT a trailing slash, it will 935 # behave in different ways depending on the status of the directory on disk: 936 # * if it exists, the command exits with an error ("add individual files instead") 937 # * if it does NOT exist (e.g., in a sparse-checkout), it is assumed to be a 938 # file and either triggers an error ("does not exist and --remove not passed") 939 # or is ignored completely (when using --remove) 940 test_all_match test_must_fail git update-index deep && 941 run_on_all test_must_fail git update-index folder1 && 942 test_must_fail git -C full-checkout update-index --remove folder1 && 943 test_sparse_match git update-index --remove folder1 && 944 test_all_match git status --porcelain=v2 945 ' 946 947 test_expect_success 'update-index --again file outside sparse definition' ' 948 init_repos && 949 950 test_all_match git checkout -b test-reupdate && 951 952 # Update HEAD without modifying the index to introduce a difference in 953 # folder1/a 954 test_sparse_match git reset --soft update-folder1 && 955 956 # Because folder1/a differs in the index vs HEAD, 957 # `git update-index --no-skip-worktree --again` will effectively perform 958 # `git update-index --no-skip-worktree folder1/a` and remove the skip-worktree 959 # flag from folder1/a 960 test_sparse_match git update-index --no-skip-worktree --again && 961 test_sparse_match git status --porcelain=v2 && 962 963 cat >expect <<-EOF && 964 D folder1/a 965 EOF 966 test_sparse_match git diff --name-status && 967 test_cmp expect sparse-checkout-out 968 ' 969 970 test_expect_success 'update-index --cacheinfo' ' 971 init_repos && 972 973 deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) && 974 folder2_oid=$(git -C full-checkout rev-parse update-folder2:folder2) && 975 folder1_a_oid=$(git -C full-checkout rev-parse update-folder1:folder1/a) && 976 977 test_all_match git update-index --cacheinfo 100644 $deep_a_oid deep/a && 978 test_all_match git status --porcelain=v2 && 979 980 # Cannot add sparse directory, even in sparse index case 981 test_all_match test_must_fail git update-index --add --cacheinfo 040000 $folder2_oid folder2/ && 982 983 # Sparse match only: the new outside-of-cone entry is added *without* skip-worktree, 984 # so `git status` reports it as "deleted" in the worktree 985 test_sparse_match git update-index --add --cacheinfo 100644 $folder1_a_oid folder1/a && 986 test_sparse_match git status --porcelain=v2 && 987 cat >expect <<-EOF && 988 MD folder1/a 989 EOF 990 test_sparse_match git status --short -- folder1/a && 991 test_cmp expect sparse-checkout-out && 992 993 # To return folder1/a to "normal" for a sparse checkout (ignored & 994 # outside-of-cone), add the skip-worktree flag. 995 test_sparse_match git update-index --skip-worktree folder1/a && 996 cat >expect <<-EOF && 997 S folder1/a 998 EOF 999 test_sparse_match git ls-files -t -- folder1/a && 1000 test_cmp expect sparse-checkout-out 1001 ' 1002 1003 for MERGE_TREES in "base HEAD update-folder2" \ 1004 "update-folder1 update-folder2" \ 1005 "update-folder2" 1006 do 1007 test_expect_success "'read-tree -mu $MERGE_TREES' with files outside sparse definition" ' 1008 init_repos && 1009 1010 # Although the index matches, without --no-sparse-checkout, outside-of- 1011 # definition files will not exist on disk for sparse checkouts 1012 test_all_match git read-tree -mu $MERGE_TREES && 1013 test_all_match git status --porcelain=v2 && 1014 test_path_is_missing sparse-checkout/folder2 && 1015 test_path_is_missing sparse-index/folder2 && 1016 1017 test_all_match git read-tree --reset -u HEAD && 1018 test_all_match git status --porcelain=v2 && 1019 1020 test_all_match git read-tree -mu --no-sparse-checkout $MERGE_TREES && 1021 test_all_match git status --porcelain=v2 && 1022 test_cmp sparse-checkout/folder2/a sparse-index/folder2/a && 1023 test_cmp sparse-checkout/folder2/a full-checkout/folder2/a 1024 1025 ' 1026 done 1027 1028 test_expect_success 'read-tree --merge with edit/edit conflicts in sparse directories' ' 1029 init_repos && 1030 1031 # Merge of multiple changes to same directory (but not same files) should 1032 # succeed 1033 test_all_match git read-tree -mu base rename-base update-folder1 && 1034 test_all_match git status --porcelain=v2 && 1035 1036 test_all_match git reset --hard && 1037 1038 test_all_match git read-tree -mu rename-base update-folder2 && 1039 test_all_match git status --porcelain=v2 && 1040 1041 test_all_match git reset --hard && 1042 1043 test_all_match test_must_fail git read-tree -mu base update-folder1 rename-out-to-in && 1044 test_all_match test_must_fail git read-tree -mu rename-out-to-in update-folder1 1045 ' 1046 1047 test_expect_success 'read-tree --prefix' ' 1048 init_repos && 1049 1050 # If files differing between the index and target exist 1051 # inside the prefix, `read-tree --prefix` should fail 1052 test_all_match test_must_fail git read-tree --prefix=deep/ deepest && 1053 test_all_match test_must_fail git read-tree --prefix=folder1/ update-folder1 && 1054 1055 # If no differing index entries exist matching the prefix, 1056 # `read-tree --prefix` updates the index successfully 1057 test_all_match git rm -rf deep/deeper1/deepest/ && 1058 test_all_match git read-tree --prefix=deep/deeper1/deepest -u deepest && 1059 test_all_match git status --porcelain=v2 && 1060 1061 run_on_all git rm -rf --sparse folder1/ && 1062 test_all_match git read-tree --prefix=folder1/ -u update-folder1 && 1063 test_all_match git status --porcelain=v2 && 1064 1065 test_all_match git rm -rf --sparse folder2/0 && 1066 test_all_match git read-tree --prefix=folder2/0/ -u rename-out-to-out && 1067 test_all_match git status --porcelain=v2 1068 ' 1069 1070 test_expect_success 'read-tree --merge with directory-file conflicts' ' 1071 init_repos && 1072 1073 test_all_match git checkout -b test-branch rename-base && 1074 1075 # Although the index matches, without --no-sparse-checkout, outside-of- 1076 # definition files will not exist on disk for sparse checkouts 1077 test_sparse_match git read-tree -mu rename-out-to-out && 1078 test_sparse_match git status --porcelain=v2 && 1079 test_path_is_missing sparse-checkout/folder2 && 1080 test_path_is_missing sparse-index/folder2 && 1081 1082 test_sparse_match git read-tree --reset -u HEAD && 1083 test_sparse_match git status --porcelain=v2 && 1084 1085 test_sparse_match git read-tree -mu --no-sparse-checkout rename-out-to-out && 1086 test_sparse_match git status --porcelain=v2 && 1087 test_cmp sparse-checkout/folder2/0/1 sparse-index/folder2/0/1 1088 ' 1089 1090 test_expect_success 'merge, cherry-pick, and rebase' ' 1091 init_repos && 1092 1093 for OPERATION in "merge -m merge" cherry-pick "rebase --apply" "rebase --merge" 1094 do 1095 test_all_match git checkout -B temp update-deep && 1096 test_all_match git $OPERATION update-folder1 && 1097 test_all_match git rev-parse HEAD^{tree} && 1098 test_all_match git $OPERATION update-folder2 && 1099 test_all_match git rev-parse HEAD^{tree} || return 1 1100 done 1101 ' 1102 1103 test_expect_success 'merge with conflict outside cone' ' 1104 init_repos && 1105 1106 test_all_match git checkout -b merge-tip merge-left && 1107 test_all_match git status --porcelain=v2 && 1108 test_all_match test_must_fail git merge -m merge merge-right && 1109 test_all_match git status --porcelain=v2 && 1110 1111 # Resolve the conflict in different ways: 1112 # 1. Revert to the base 1113 test_all_match git checkout base -- deep/deeper2/a && 1114 test_all_match git status --porcelain=v2 && 1115 1116 # 2. Add the file with conflict markers 1117 test_sparse_match test_must_fail git add folder1/a && 1118 test_grep "Disable or modify the sparsity rules" sparse-checkout-err && 1119 test_sparse_unstaged folder1/a && 1120 test_all_match git add --sparse folder1/a && 1121 test_all_match git status --porcelain=v2 && 1122 1123 # 3. Rename the file to another sparse filename and 1124 # accept conflict markers as resolved content. 1125 run_on_all mv folder2/a folder2/z && 1126 test_sparse_match test_must_fail git add folder2 && 1127 test_grep "Disable or modify the sparsity rules" sparse-checkout-err && 1128 test_sparse_unstaged folder2/z && 1129 test_all_match git add --sparse folder2 && 1130 test_all_match git status --porcelain=v2 && 1131 1132 test_all_match git merge --continue && 1133 test_all_match git status --porcelain=v2 && 1134 test_all_match git rev-parse HEAD^{tree} 1135 ' 1136 1137 test_expect_success 'cherry-pick/rebase with conflict outside cone' ' 1138 init_repos && 1139 1140 for OPERATION in cherry-pick rebase 1141 do 1142 test_all_match git checkout -B tip && 1143 test_all_match git reset --hard merge-left && 1144 test_all_match git status --porcelain=v2 && 1145 test_all_match test_must_fail git $OPERATION merge-right && 1146 test_all_match git status --porcelain=v2 && 1147 1148 # Resolve the conflict in different ways: 1149 # 1. Revert to the base 1150 test_all_match git checkout base -- deep/deeper2/a && 1151 test_all_match git status --porcelain=v2 && 1152 1153 # 2. Add the file with conflict markers 1154 # NEEDSWORK: Even though the merge conflict removed the 1155 # SKIP_WORKTREE bit from the index entry for folder1/a, we should 1156 # warn that this is a problematic add. 1157 test_sparse_match test_must_fail git add folder1/a && 1158 test_grep "Disable or modify the sparsity rules" sparse-checkout-err && 1159 test_sparse_unstaged folder1/a && 1160 test_all_match git add --sparse folder1/a && 1161 test_all_match git status --porcelain=v2 && 1162 1163 # 3. Rename the file to another sparse filename and 1164 # accept conflict markers as resolved content. 1165 # NEEDSWORK: This mode now fails, because folder2/z is 1166 # outside of the sparse-checkout cone and does not match an 1167 # existing index entry with the SKIP_WORKTREE bit cleared. 1168 run_on_all mv folder2/a folder2/z && 1169 test_sparse_match test_must_fail git add folder2 && 1170 test_grep "Disable or modify the sparsity rules" sparse-checkout-err && 1171 test_sparse_unstaged folder2/z && 1172 test_all_match git add --sparse folder2 && 1173 test_all_match git status --porcelain=v2 && 1174 1175 test_all_match git $OPERATION --continue && 1176 test_all_match git status --porcelain=v2 && 1177 test_all_match git rev-parse HEAD^{tree} || return 1 1178 done 1179 ' 1180 1181 test_expect_success 'merge with outside renames' ' 1182 init_repos && 1183 1184 for type in out-to-out out-to-in in-to-out 1185 do 1186 test_all_match git reset --hard && 1187 test_all_match git checkout -f -b merge-$type update-deep && 1188 test_all_match git merge -m "$type" rename-$type && 1189 test_all_match git rev-parse HEAD^{tree} || return 1 1190 done 1191 ' 1192 1193 # Sparse-index fails to convert the index in the 1194 # final 'git cherry-pick' command. 1195 test_expect_success 'cherry-pick with conflicts' ' 1196 init_repos && 1197 1198 write_script edit-conflict <<-\EOF && 1199 echo $1 >conflict 1200 EOF 1201 1202 test_all_match git checkout -b to-cherry-pick && 1203 run_on_all ../edit-conflict ABC && 1204 test_all_match git add conflict && 1205 test_all_match git commit -m "conflict to pick" && 1206 1207 test_all_match git checkout -B base HEAD~1 && 1208 run_on_all ../edit-conflict DEF && 1209 test_all_match git add conflict && 1210 test_all_match git commit -m "conflict in base" && 1211 1212 test_all_match test_must_fail git cherry-pick to-cherry-pick 1213 ' 1214 1215 test_expect_success 'stash' ' 1216 init_repos && 1217 1218 write_script edit-contents <<-\EOF && 1219 echo text >>$1 1220 EOF 1221 1222 # Stash a sparse directory (folder1) 1223 test_all_match git checkout -b test-branch rename-base && 1224 test_all_match git reset --soft rename-out-to-out && 1225 test_all_match git stash && 1226 test_all_match git status --porcelain=v2 && 1227 1228 # Apply the sparse directory stash without reinstating the index 1229 test_all_match git stash apply -q && 1230 test_all_match git status --porcelain=v2 && 1231 1232 # Reset to state where stash can be applied 1233 test_sparse_match git sparse-checkout reapply && 1234 test_all_match git reset --hard rename-out-to-out && 1235 1236 # Apply the sparse directory stash *with* reinstating the index 1237 test_all_match git stash apply --index -q && 1238 test_all_match git status --porcelain=v2 && 1239 1240 # Reset to state where we will get a conflict applying the stash 1241 test_sparse_match git sparse-checkout reapply && 1242 test_all_match git reset --hard update-folder1 && 1243 1244 # Apply the sparse directory stash with conflicts 1245 test_all_match test_must_fail git stash apply --index -q && 1246 test_all_match test_must_fail git stash apply -q && 1247 test_all_match git status --porcelain=v2 && 1248 1249 # Reset to base branch 1250 test_sparse_match git sparse-checkout reapply && 1251 test_all_match git reset --hard base && 1252 1253 # Stash & unstash an untracked file outside of the sparse checkout 1254 # definition. 1255 run_on_sparse mkdir -p folder1 && 1256 run_on_all ../edit-contents folder1/new && 1257 test_all_match git stash -u && 1258 test_all_match git status --porcelain=v2 && 1259 1260 test_all_match git stash pop -q && 1261 test_all_match git status --porcelain=v2 1262 ' 1263 1264 test_expect_success 'checkout-index inside sparse definition' ' 1265 init_repos && 1266 1267 run_on_all rm -f deep/a && 1268 test_all_match git checkout-index -- deep/a && 1269 test_all_match git status --porcelain=v2 && 1270 1271 echo test >>new-a && 1272 run_on_all cp ../new-a a && 1273 test_all_match test_must_fail git checkout-index -- a && 1274 test_all_match git checkout-index -f -- a && 1275 test_all_match git status --porcelain=v2 1276 ' 1277 1278 test_expect_success 'checkout-index outside sparse definition' ' 1279 init_repos && 1280 1281 # Without --ignore-skip-worktree-bits, outside-of-cone files will trigger 1282 # an error 1283 test_sparse_match test_must_fail git checkout-index -- folder1/a && 1284 test_grep "folder1/a has skip-worktree enabled" sparse-checkout-err && 1285 test_path_is_missing folder1/a && 1286 1287 # With --ignore-skip-worktree-bits, outside-of-cone files are checked out 1288 test_sparse_match git checkout-index --ignore-skip-worktree-bits -- folder1/a && 1289 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a && 1290 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a && 1291 1292 run_on_sparse rm -rf folder1 && 1293 echo test >new-a && 1294 run_on_sparse mkdir -p folder1 && 1295 run_on_all cp ../new-a folder1/a && 1296 1297 test_all_match test_must_fail git checkout-index --ignore-skip-worktree-bits -- folder1/a && 1298 test_all_match git checkout-index -f --ignore-skip-worktree-bits -- folder1/a && 1299 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a && 1300 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a 1301 ' 1302 1303 test_expect_success 'checkout-index with folders' ' 1304 init_repos && 1305 1306 # Inside checkout definition 1307 test_all_match test_must_fail git checkout-index -f -- deep/ && 1308 1309 # Outside checkout definition 1310 # Note: although all tests fail (as expected), the messaging differs. For 1311 # non-sparse index checkouts, the error is that the "file" does not appear 1312 # in the index; for sparse checkouts, the error is explicitly that the 1313 # entry is a sparse directory. 1314 run_on_all test_must_fail git checkout-index -f -- folder1/ && 1315 test_cmp full-checkout-err sparse-checkout-err && 1316 ! test_cmp full-checkout-err sparse-index-err && 1317 test_grep "is a sparse directory" sparse-index-err 1318 ' 1319 1320 test_expect_success 'checkout-index --all' ' 1321 init_repos && 1322 1323 test_all_match git checkout-index --all && 1324 test_sparse_match test_path_is_missing folder1 && 1325 1326 # --ignore-skip-worktree-bits will cause `skip-worktree` files to be 1327 # checked out, causing the outside-of-cone `folder1` to exist on-disk 1328 test_all_match git checkout-index --ignore-skip-worktree-bits --all && 1329 test_all_match test_path_exists folder1 1330 ' 1331 1332 test_expect_success 'clean' ' 1333 init_repos && 1334 1335 echo bogus >>.gitignore && 1336 run_on_all cp ../.gitignore . && 1337 test_all_match git add .gitignore && 1338 test_all_match git commit -m "ignore bogus files" && 1339 1340 run_on_sparse mkdir folder1 && 1341 run_on_all mkdir -p deep/untracked-deep && 1342 run_on_all touch folder1/bogus && 1343 run_on_all touch folder1/untracked && 1344 run_on_all touch deep/untracked-deep/bogus && 1345 run_on_all touch deep/untracked-deep/untracked && 1346 1347 test_all_match git status --porcelain=v2 && 1348 test_all_match git clean -f && 1349 test_all_match git status --porcelain=v2 && 1350 test_sparse_match ls && 1351 test_sparse_match ls folder1 && 1352 run_on_all test_path_exists folder1/bogus && 1353 run_on_all test_path_is_missing folder1/untracked && 1354 run_on_all test_path_exists deep/untracked-deep/bogus && 1355 run_on_all test_path_exists deep/untracked-deep/untracked && 1356 1357 test_all_match git clean -fd && 1358 test_all_match git status --porcelain=v2 && 1359 test_sparse_match ls && 1360 test_sparse_match ls folder1 && 1361 run_on_all test_path_exists folder1/bogus && 1362 run_on_all test_path_exists deep/untracked-deep/bogus && 1363 run_on_all test_path_is_missing deep/untracked-deep/untracked && 1364 1365 test_all_match git clean -xf && 1366 test_all_match git status --porcelain=v2 && 1367 test_sparse_match ls && 1368 test_sparse_match ls folder1 && 1369 run_on_all test_path_is_missing folder1/bogus && 1370 run_on_all test_path_exists deep/untracked-deep/bogus && 1371 1372 test_all_match git clean -xdf && 1373 test_all_match git status --porcelain=v2 && 1374 test_sparse_match ls && 1375 test_sparse_match ls folder1 && 1376 run_on_all test_path_is_missing deep/untracked-deep/bogus && 1377 1378 test_sparse_match test_path_is_dir folder1 1379 ' 1380 1381 for builtin in show rev-parse 1382 do 1383 test_expect_success "$builtin (cached blobs/trees)" " 1384 init_repos && 1385 1386 test_all_match git $builtin :a && 1387 test_all_match git $builtin :deep/a && 1388 test_sparse_match git $builtin :folder1/a && 1389 1390 # The error message differs depending on whether 1391 # the directory exists in the worktree. 1392 test_all_match test_must_fail git $builtin :deep/ && 1393 test_must_fail git -C full-checkout $builtin :folder1/ && 1394 test_sparse_match test_must_fail git $builtin :folder1/ && 1395 1396 # Change the sparse cone for an extra case: 1397 run_on_sparse git sparse-checkout set deep/deeper1 && 1398 1399 # deep/deeper2 is a sparse directory in the sparse index. 1400 test_sparse_match test_must_fail git $builtin :deep/deeper2/ && 1401 1402 # deep/deeper2/deepest is not in the sparse index, but 1403 # will trigger an index expansion. 1404 test_sparse_match test_must_fail git $builtin :deep/deeper2/deepest/ 1405 " 1406 done 1407 1408 test_expect_success 'relative path to a sparse directory' ' 1409 init_repos && 1410 1411 # A "::" argument whose path is relative is resolved 1412 # into a heap-allocated buffer, and a sparse directory found at that 1413 # path is reported through it. Cover that combination, so that the 1414 # reporting does not read the buffer after it has been released. 1415 test_sparse_match test_must_fail git show :0:./folder1/ && 1416 test_sparse_match test_must_fail git rev-parse :0:./folder1/ 1417 ' 1418 1419 test_expect_success 'submodule handling' ' 1420 init_repos && 1421 1422 test_sparse_match git sparse-checkout add modules && 1423 test_all_match mkdir modules && 1424 test_all_match touch modules/a && 1425 test_all_match git add modules && 1426 test_all_match git commit -m "add modules directory" && 1427 1428 test_config_global protocol.file.allow always && 1429 1430 run_on_all git submodule add "$(pwd)/initial-repo" modules/sub && 1431 test_all_match git commit -m "add submodule" && 1432 1433 # having a submodule prevents "modules" from collapse 1434 test_sparse_match git sparse-checkout set deep/deeper1 && 1435 git -C sparse-index ls-files --sparse --stage >cache && 1436 test_grep "100644 .* modules/a" cache && 1437 test_grep "160000 $(git -C initial-repo rev-parse HEAD) 0 modules/sub" cache 1438 ' 1439 1440 test_expect_success 'git apply functionality' ' 1441 init_repos && 1442 1443 test_all_match git checkout base && 1444 1445 git -C full-checkout diff base..merge-right -- deep >patch-in-sparse && 1446 git -C full-checkout diff base..merge-right -- folder2 >patch-outside && 1447 1448 # Apply a patch to a file inside the sparse definition 1449 test_all_match git apply --index --stat ../patch-in-sparse && 1450 test_all_match git status --porcelain=v2 && 1451 1452 # Apply a patch to a file outside the sparse definition 1453 test_sparse_match test_must_fail git apply ../patch-outside && 1454 test_grep "No such file or directory" sparse-checkout-err && 1455 1456 # But it works with --index and --cached 1457 test_all_match git apply --index --stat ../patch-outside && 1458 test_all_match git status --porcelain=v2 && 1459 test_all_match git reset --hard && 1460 test_all_match git apply --cached --stat ../patch-outside && 1461 test_all_match git status --porcelain=v2 1462 ' 1463 1464 # When working with a sparse index, some commands will need to expand the 1465 # index to operate properly. If those commands also write the index back 1466 # to disk, they need to convert the index to sparse before writing. 1467 # This test verifies that both of these events are logged in trace2 logs. 1468 test_expect_success 'sparse-index is expanded and converted back' ' 1469 init_repos && 1470 1471 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \ 1472 git -C sparse-index reset -- folder1/a && 1473 test_region index convert_to_sparse trace2.txt && 1474 test_region index ensure_full_index trace2.txt && 1475 1476 # ls-files expands on read, but does not write. 1477 rm trace2.txt && 1478 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ 1479 git -C sparse-index ls-files && 1480 test_region index ensure_full_index trace2.txt 1481 ' 1482 1483 test_expect_success 'index.sparse disabled inline uses full index' ' 1484 init_repos && 1485 1486 # When index.sparse is disabled inline with `git status`, the 1487 # index is expanded at the beginning of the execution then never 1488 # converted back to sparse. It is then written to disk as a full index. 1489 rm -f trace2.txt && 1490 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ 1491 git -C sparse-index -c index.sparse=false status && 1492 ! test_region index convert_to_sparse trace2.txt && 1493 test_region index ensure_full_index trace2.txt && 1494 1495 # Since index.sparse is set to true at a repo level, the index 1496 # is converted from full to sparse when read, then never expanded 1497 # over the course of `git status`. It is written to disk as a sparse 1498 # index. 1499 rm -f trace2.txt && 1500 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ 1501 git -C sparse-index status && 1502 test_region index convert_to_sparse trace2.txt && 1503 ! test_region index ensure_full_index trace2.txt && 1504 1505 # Now that the index has been written to disk as sparse, it is not 1506 # converted to sparse (or expanded to full) when read by `git status`. 1507 rm -f trace2.txt && 1508 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ 1509 git -C sparse-index status && 1510 ! test_region index convert_to_sparse trace2.txt && 1511 ! test_region index ensure_full_index trace2.txt 1512 ' 1513 1514 run_sparse_index_trace2 () { 1515 rm -f trace2.txt && 1516 if test -z "$WITHOUT_UNTRACKED_TXT" 1517 then 1518 echo >>sparse-index/untracked.txt 1519 fi && 1520 1521 if test "$1" = "!" 1522 then 1523 shift && 1524 test_must_fail env \ 1525 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \ 1526 git -C sparse-index "$@" \ 1527 >sparse-index-out \ 1528 2>sparse-index-error || return 1 1529 else 1530 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \ 1531 git -C sparse-index "$@" \ 1532 >sparse-index-out \ 1533 2>sparse-index-error || return 1 1534 fi 1535 } 1536 1537 ensure_expanded () { 1538 run_sparse_index_trace2 "$@" && 1539 test_region index ensure_full_index trace2.txt 1540 } 1541 1542 ensure_not_expanded () { 1543 run_sparse_index_trace2 "$@" && 1544 test_region ! index ensure_full_index trace2.txt 1545 } 1546 1547 test_expect_success 'sparse-index is not expanded' ' 1548 init_repos && 1549 1550 ensure_not_expanded status && 1551 ensure_not_expanded ls-files --sparse && 1552 ensure_not_expanded commit --allow-empty -m empty && 1553 echo >>sparse-index/a && 1554 ensure_not_expanded commit -a -m a && 1555 echo >>sparse-index/a && 1556 ensure_not_expanded commit --include a -m a && 1557 echo >>sparse-index/deep/deeper1/a && 1558 ensure_not_expanded commit --include deep/deeper1/a -m deeper && 1559 ensure_not_expanded checkout rename-out-to-out && 1560 ensure_not_expanded checkout - && 1561 ensure_not_expanded switch rename-out-to-out && 1562 ensure_not_expanded switch - && 1563 ensure_not_expanded reset --hard && 1564 ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 && 1565 ensure_not_expanded reset --hard && 1566 ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 && 1567 1568 ensure_not_expanded ls-files deep/deeper1 && 1569 1570 echo >>sparse-index/README.md && 1571 ensure_not_expanded add -A && 1572 echo >>sparse-index/extra.txt && 1573 ensure_not_expanded add extra.txt && 1574 echo >>sparse-index/untracked.txt && 1575 ensure_not_expanded add . && 1576 1577 ensure_not_expanded checkout-index -f a && 1578 ensure_not_expanded checkout-index -f --all && 1579 for ref in update-deep update-folder1 update-folder2 update-deep 1580 do 1581 echo >>sparse-index/README.md && 1582 ensure_not_expanded reset --hard $ref || return 1 1583 done && 1584 1585 ensure_not_expanded reset --mixed base && 1586 ensure_not_expanded reset --hard update-deep && 1587 ensure_not_expanded reset --keep base && 1588 ensure_not_expanded reset --merge update-deep && 1589 ensure_not_expanded reset --hard && 1590 1591 ensure_not_expanded reset base -- deep/a && 1592 ensure_not_expanded reset base -- nonexistent-file && 1593 ensure_not_expanded reset deepest -- deep && 1594 1595 # Although folder1 is outside the sparse definition, it exists as a 1596 # directory entry in the index, so the pathspec will not force the 1597 # index to be expanded. 1598 ensure_not_expanded reset deepest -- folder1 && 1599 ensure_not_expanded reset deepest -- folder1/ && 1600 1601 # Wildcard identifies only in-cone files, no index expansion 1602 ensure_not_expanded reset deepest -- deep/\* && 1603 1604 # Wildcard identifies only full sparse directories, no index expansion 1605 ensure_not_expanded reset deepest -- folder\* && 1606 1607 ensure_not_expanded clean -fd && 1608 1609 ensure_not_expanded checkout -f update-deep && 1610 test_config -C sparse-index pull.twohead ort && 1611 ( 1612 for OPERATION in "merge -m merge" cherry-pick rebase 1613 do 1614 ensure_not_expanded merge -m merge update-folder1 && 1615 ensure_not_expanded merge -m merge update-folder2 || return 1 1616 done 1617 ) 1618 ' 1619 1620 test_expect_success 'sparse-index is not expanded: merge conflict in cone' ' 1621 init_repos && 1622 1623 for side in right left 1624 do 1625 git -C sparse-index checkout -b expand-$side base && 1626 echo $side >sparse-index/deep/a && 1627 git -C sparse-index commit -a -m "$side" || return 1 1628 done && 1629 1630 ( 1631 git -C sparse-index config pull.twohead ort && 1632 ensure_not_expanded ! merge -m merged expand-right 1633 ) 1634 ' 1635 1636 test_expect_success 'sparse-index is not expanded: stash' ' 1637 init_repos && 1638 1639 echo >>sparse-index/a && 1640 ensure_not_expanded stash && 1641 ensure_not_expanded stash list && 1642 ensure_not_expanded stash show stash@{0} && 1643 ensure_not_expanded stash apply stash@{0} && 1644 ensure_not_expanded stash drop stash@{0} && 1645 1646 echo >>sparse-index/deep/new && 1647 ensure_not_expanded stash -u && 1648 ( 1649 WITHOUT_UNTRACKED_TXT=1 && 1650 ensure_not_expanded stash pop 1651 ) && 1652 1653 ensure_not_expanded stash create && 1654 oid=$(git -C sparse-index stash create) && 1655 ensure_not_expanded stash store -m "test" $oid && 1656 ensure_not_expanded reset --hard && 1657 ensure_not_expanded stash pop 1658 ' 1659 1660 test_expect_success 'sparse-index is not expanded: stash in-cone pathspec' ' 1661 init_repos && 1662 1663 echo unrelated >>sparse-index/deep/e && 1664 echo literal >>sparse-index/deep/a && 1665 ensure_not_expanded stash push -- deep/a && 1666 test_grep ! literal sparse-index/deep/a && 1667 test_grep unrelated sparse-index/deep/e && 1668 ensure_not_expanded stash pop && 1669 test_grep literal sparse-index/deep/a && 1670 1671 echo prefixed >>sparse-index/deep/a && 1672 ensure_not_expanded -C deep stash push -- a && 1673 test_grep ! prefixed sparse-index/deep/a && 1674 test_grep unrelated sparse-index/deep/e && 1675 ensure_not_expanded stash pop && 1676 test_grep prefixed sparse-index/deep/a && 1677 1678 echo wildcard >>sparse-index/deep/a && 1679 ensure_not_expanded stash push -- "deep/a*" && 1680 test_grep ! wildcard sparse-index/deep/a && 1681 test_grep unrelated sparse-index/deep/e && 1682 ensure_not_expanded stash pop && 1683 test_grep wildcard sparse-index/deep/a && 1684 1685 echo pathspec-file >>sparse-index/deep/a && 1686 echo deep/a >pathspec-file && 1687 ensure_not_expanded stash push --pathspec-from-file=../pathspec-file && 1688 test_grep ! pathspec-file sparse-index/deep/a && 1689 test_grep unrelated sparse-index/deep/e && 1690 ensure_not_expanded stash pop && 1691 test_grep pathspec-file sparse-index/deep/a && 1692 1693 echo multiple-a >>sparse-index/deep/a && 1694 echo multiple-e >>sparse-index/deep/e && 1695 ensure_not_expanded stash push -- deep/a deep/e && 1696 test_grep ! multiple-a sparse-index/deep/a && 1697 test_grep ! multiple-e sparse-index/deep/e && 1698 ensure_not_expanded stash pop && 1699 test_grep multiple-a sparse-index/deep/a && 1700 test_grep multiple-e sparse-index/deep/e && 1701 1702 echo staged >>sparse-index/deep/a && 1703 git -C sparse-index add deep/a && 1704 ensure_not_expanded stash push --staged -- deep/a && 1705 test_grep ! staged sparse-index/deep/a && 1706 test_grep unrelated sparse-index/deep/e && 1707 ensure_not_expanded stash pop --index && 1708 test_grep staged sparse-index/deep/a && 1709 test_must_fail git -C sparse-index diff --cached --quiet -- deep/a && 1710 1711 ensure_not_expanded ! stash push -- deep/does-not-exist && 1712 test_grep "did not match any file" sparse-index-error 1713 ' 1714 1715 test_expect_success 'describe tested on all' ' 1716 init_repos && 1717 1718 # Add tag to be read by describe 1719 1720 run_on_all git tag -a v1.0 -m "Version 1" && 1721 test_all_match git describe --dirty && 1722 run_on_all rm g && 1723 test_all_match git describe --dirty 1724 ' 1725 1726 test_expect_success 'ls-files filtering and expansion' ' 1727 init_repos && 1728 1729 # This filtering will hit a sparse directory midway 1730 # through the iteration. 1731 test_all_match git ls-files deep && 1732 1733 # This pathspec will filter the index to only a sparse 1734 # directory. 1735 test_all_match git ls-files folder1 1736 ' 1737 1738 test_expect_success 'sparse-index is not expanded: describe' ' 1739 init_repos && 1740 1741 # Add tag to be read by describe 1742 1743 git -C sparse-index tag -a v1.0 -m "Version 1" && 1744 1745 ensure_not_expanded describe --dirty && 1746 echo "test" >>sparse-index/g && 1747 ensure_not_expanded describe --dirty && 1748 ensure_not_expanded describe 1749 ' 1750 1751 test_expect_success 'sparse index is not expanded: diff and diff-index' ' 1752 init_repos && 1753 1754 write_script edit-contents <<-\EOF && 1755 echo text >>$1 1756 EOF 1757 1758 # Add file within cone 1759 test_sparse_match git sparse-checkout set deep && 1760 run_on_all ../edit-contents deep/testfile && 1761 test_all_match git add deep/testfile && 1762 run_on_all ../edit-contents deep/testfile && 1763 1764 test_all_match git diff && 1765 test_all_match git diff --cached && 1766 ensure_not_expanded diff && 1767 ensure_not_expanded diff --cached && 1768 ensure_not_expanded diff-index --cached HEAD && 1769 1770 # Add file outside cone 1771 test_all_match git reset --hard && 1772 run_on_all mkdir newdirectory && 1773 run_on_all ../edit-contents newdirectory/testfile && 1774 test_sparse_match git sparse-checkout set newdirectory && 1775 test_all_match git add newdirectory/testfile && 1776 run_on_all ../edit-contents newdirectory/testfile && 1777 test_sparse_match git sparse-checkout set && 1778 1779 test_all_match git diff && 1780 test_all_match git diff --cached && 1781 ensure_not_expanded diff && 1782 ensure_not_expanded diff --cached && 1783 ensure_not_expanded diff-index --cached HEAD && 1784 1785 # Merge conflict outside cone 1786 # The sparse checkout will report a warning that is not in the 1787 # full checkout, so we use `run_on_all` instead of 1788 # `test_all_match` 1789 run_on_all git reset --hard && 1790 test_all_match git checkout merge-left && 1791 test_all_match test_must_fail git merge merge-right && 1792 1793 test_all_match git diff && 1794 test_all_match git diff --cached && 1795 ensure_not_expanded diff && 1796 ensure_not_expanded diff --cached && 1797 ensure_not_expanded diff-index --cached HEAD 1798 ' 1799 1800 test_expect_success 'sparse index is not expanded: show and rev-parse' ' 1801 init_repos && 1802 1803 ensure_not_expanded show :a && 1804 ensure_not_expanded show :deep/a && 1805 ensure_not_expanded rev-parse :a && 1806 ensure_not_expanded rev-parse :deep/a 1807 ' 1808 1809 test_expect_success 'sparse index is not expanded: update-index' ' 1810 init_repos && 1811 1812 deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) && 1813 ensure_not_expanded update-index --cacheinfo 100644 $deep_a_oid deep/a && 1814 1815 echo "test" >sparse-index/README.md && 1816 echo "test2" >sparse-index/a && 1817 rm -f sparse-index/deep/a && 1818 1819 ensure_not_expanded update-index --add README.md && 1820 ensure_not_expanded update-index a && 1821 ensure_not_expanded update-index --remove deep/a && 1822 1823 ensure_not_expanded reset --soft update-deep && 1824 ensure_not_expanded update-index --add --remove --again 1825 ' 1826 1827 test_expect_success 'sparse index is not expanded: blame' ' 1828 init_repos && 1829 1830 for file in a \ 1831 deep/a \ 1832 deep/deeper1/a \ 1833 deep/deeper1/deepest/a 1834 do 1835 ensure_not_expanded blame $file || return 1 1836 done 1837 ' 1838 1839 test_expect_success 'sparse index is not expanded: fetch/pull' ' 1840 init_repos && 1841 1842 git -C sparse-index remote add full "file://$(pwd)/full-checkout" && 1843 ensure_not_expanded fetch full && 1844 git -C full-checkout commit --allow-empty -m "for pull merge" && 1845 git -C sparse-index commit --allow-empty -m "for pull merge" && 1846 ensure_not_expanded pull full base 1847 ' 1848 1849 test_expect_success 'sparse index is not expanded: read-tree' ' 1850 init_repos && 1851 1852 ensure_not_expanded checkout -b test-branch update-folder1 && 1853 for MERGE_TREES in "base HEAD update-folder2" \ 1854 "base HEAD rename-base" \ 1855 "base update-folder2" \ 1856 "base rename-base" \ 1857 "update-folder2" 1858 do 1859 ensure_not_expanded read-tree -mu $MERGE_TREES && 1860 ensure_not_expanded reset --hard || return 1 1861 done && 1862 1863 rm -rf sparse-index/deep/deeper2 && 1864 ensure_not_expanded add . && 1865 ensure_not_expanded commit -m "test" && 1866 1867 ensure_not_expanded read-tree --prefix=deep/deeper2 -u deepest 1868 ' 1869 1870 test_expect_success 'ls-files' ' 1871 init_repos && 1872 1873 # Use a smaller sparse-checkout for reduced output 1874 test_sparse_match git sparse-checkout set && 1875 1876 # Behavior agrees by default. Sparse index is expanded. 1877 test_all_match git ls-files && 1878 1879 # With --sparse, the sparse index data changes behavior. 1880 git -C sparse-index ls-files --sparse >actual && 1881 1882 cat >expect <<-\EOF && 1883 a 1884 before/ 1885 deep/ 1886 e 1887 folder1- 1888 folder1.x 1889 folder1/ 1890 folder10 1891 folder2/ 1892 g 1893 x/ 1894 z 1895 EOF 1896 1897 test_cmp expect actual && 1898 1899 # With --sparse and no sparse index, nothing changes. 1900 git -C sparse-checkout ls-files >dense && 1901 git -C sparse-checkout ls-files --sparse >sparse && 1902 test_cmp dense sparse && 1903 1904 # Set up a strange condition of having a file edit 1905 # outside of the sparse-checkout cone. We want to verify 1906 # that all modes handle this the same, and detect the 1907 # modification. 1908 write_script edit-content <<-\EOF && 1909 mkdir -p folder1 && 1910 echo content >>folder1/a 1911 EOF 1912 run_on_all ../edit-content && 1913 1914 test_all_match git ls-files --modified && 1915 1916 git -C sparse-index ls-files --sparse --modified >sparse-index-out && 1917 cat >expect <<-\EOF && 1918 folder1/a 1919 EOF 1920 test_cmp expect sparse-index-out && 1921 1922 # Add folder1 to the sparse-checkout cone and 1923 # check that ls-files shows the expanded files. 1924 test_sparse_match git sparse-checkout add folder1 && 1925 test_all_match git ls-files --modified && 1926 1927 test_all_match git ls-files && 1928 git -C sparse-index ls-files --sparse >actual && 1929 1930 cat >expect <<-\EOF && 1931 a 1932 before/ 1933 deep/ 1934 e 1935 folder1- 1936 folder1.x 1937 folder1/0/0/0 1938 folder1/0/1 1939 folder1/a 1940 folder10 1941 folder2/ 1942 g 1943 x/ 1944 z 1945 EOF 1946 1947 test_cmp expect actual && 1948 1949 # Double-check index expansion is avoided 1950 ensure_not_expanded ls-files --sparse 1951 ' 1952 1953 test_expect_success 'sparse index is not expanded: sparse-checkout' ' 1954 init_repos && 1955 1956 ensure_not_expanded sparse-checkout set deep/deeper2 && 1957 ensure_not_expanded sparse-checkout set deep/deeper1 && 1958 ensure_not_expanded sparse-checkout set deep && 1959 ensure_not_expanded sparse-checkout add folder1 && 1960 ensure_not_expanded sparse-checkout set deep/deeper1 && 1961 ensure_not_expanded sparse-checkout set folder2 && 1962 1963 # Demonstrate that the checks that "folder1/a" is a file 1964 # do not cause a sparse-index expansion (since it is in the 1965 # sparse-checkout cone). 1966 echo >>sparse-index/folder2/a && 1967 git -C sparse-index add folder2/a && 1968 1969 ensure_not_expanded sparse-checkout add folder1 && 1970 1971 # Skip checks here, since deep/deeper1 is inside a sparse directory 1972 # that must be expanded to check whether `deep/deeper1` is a file 1973 # or not. 1974 ensure_not_expanded sparse-checkout set --skip-checks deep/deeper1 && 1975 ensure_not_expanded sparse-checkout set 1976 ' 1977 1978 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout 1979 # in this scenario, but it shouldn't. 1980 test_expect_success 'reset mixed and checkout orphan' ' 1981 init_repos && 1982 1983 test_all_match git checkout rename-out-to-in && 1984 1985 # Sparse checkouts do not agree with full checkouts about 1986 # how to report a directory/file conflict during a reset. 1987 # This command would fail with test_all_match because the 1988 # full checkout reports "T folder1/0/1" while a sparse 1989 # checkout reports "D folder1/0/1". This matches because 1990 # the sparse checkouts skip "adding" the other side of 1991 # the conflict. 1992 test_sparse_match git reset --mixed HEAD~1 && 1993 test_sparse_match git ls-files --stage && 1994 test_sparse_match git status --porcelain=v2 && 1995 1996 # At this point, sparse-checkouts behave differently 1997 # from the full-checkout. 1998 test_sparse_match git checkout --orphan new-branch && 1999 test_sparse_match git ls-files --stage && 2000 test_sparse_match git status --porcelain=v2 2001 ' 2002 2003 test_expect_success 'add everything with deep new file' ' 2004 init_repos && 2005 2006 run_on_sparse git sparse-checkout set deep/deeper1/deepest && 2007 2008 run_on_all touch deep/deeper1/x && 2009 test_all_match git add . && 2010 test_all_match git status --porcelain=v2 2011 ' 2012 2013 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of 2014 # directory/file conflicts, even without sparse-checkout. Use this 2015 # test only as a documentation of the incorrect behavior, not a 2016 # measure of how it _should_ behave. 2017 test_expect_success 'checkout behaves oddly with df-conflict-1' ' 2018 init_repos && 2019 2020 test_sparse_match git sparse-checkout disable && 2021 2022 write_script edit-content <<-\EOF && 2023 echo content >>folder1/larger-content 2024 git add folder1 2025 EOF 2026 2027 run_on_all ../edit-content && 2028 test_all_match git status --porcelain=v2 && 2029 2030 git -C sparse-checkout sparse-checkout init --cone && 2031 git -C sparse-index sparse-checkout init --cone --sparse-index && 2032 2033 test_all_match git status --porcelain=v2 && 2034 2035 # This checkout command should fail, because we have a staged 2036 # change to folder1/larger-content, but the destination changes 2037 # folder1 to a file. 2038 git -C full-checkout checkout df-conflict-1 \ 2039 1>full-checkout-out \ 2040 2>full-checkout-err && 2041 git -C sparse-checkout checkout df-conflict-1 \ 2042 1>sparse-checkout-out \ 2043 2>sparse-checkout-err && 2044 git -C sparse-index checkout df-conflict-1 \ 2045 1>sparse-index-out \ 2046 2>sparse-index-err && 2047 2048 # Instead, the checkout deletes the folder1 file and adds the 2049 # folder1/larger-content file, leaving all other paths that were 2050 # in folder1/ as deleted (without any warning). 2051 cat >expect <<-EOF && 2052 D folder1 2053 A folder1/larger-content 2054 EOF 2055 test_cmp expect full-checkout-out && 2056 test_cmp expect sparse-checkout-out && 2057 2058 # The sparse-index reports no output 2059 test_must_be_empty sparse-index-out && 2060 2061 # stderr: Switched to branch df-conflict-1 2062 test_cmp full-checkout-err sparse-checkout-err && 2063 test_cmp full-checkout-err sparse-checkout-err 2064 ' 2065 2066 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of 2067 # directory/file conflicts, even without sparse-checkout. Use this 2068 # test only as a documentation of the incorrect behavior, not a 2069 # measure of how it _should_ behave. 2070 test_expect_success 'checkout behaves oddly with df-conflict-2' ' 2071 init_repos && 2072 2073 test_sparse_match git sparse-checkout disable && 2074 2075 write_script edit-content <<-\EOF && 2076 echo content >>folder2/larger-content 2077 git add folder2 2078 EOF 2079 2080 run_on_all ../edit-content && 2081 test_all_match git status --porcelain=v2 && 2082 2083 git -C sparse-checkout sparse-checkout init --cone && 2084 git -C sparse-index sparse-checkout init --cone --sparse-index && 2085 2086 test_all_match git status --porcelain=v2 && 2087 2088 # This checkout command should fail, because we have a staged 2089 # change to folder1/larger-content, but the destination changes 2090 # folder1 to a file. 2091 git -C full-checkout checkout df-conflict-2 \ 2092 1>full-checkout-out \ 2093 2>full-checkout-err && 2094 git -C sparse-checkout checkout df-conflict-2 \ 2095 1>sparse-checkout-out \ 2096 2>sparse-checkout-err && 2097 git -C sparse-index checkout df-conflict-2 \ 2098 1>sparse-index-out \ 2099 2>sparse-index-err && 2100 2101 # The full checkout deviates from the df-conflict-1 case here! 2102 # It drops the change to folder1/larger-content and leaves the 2103 # folder1 path as-is on disk. The sparse-index behaves the same. 2104 test_must_be_empty full-checkout-out && 2105 test_must_be_empty sparse-index-out && 2106 2107 # In the sparse-checkout case, the checkout deletes the folder1 2108 # file and adds the folder1/larger-content file, leaving all other 2109 # paths that were in folder1/ as deleted (without any warning). 2110 cat >expect <<-EOF && 2111 D folder2 2112 A folder2/larger-content 2113 EOF 2114 test_cmp expect sparse-checkout-out && 2115 2116 # Switched to branch df-conflict-1 2117 test_cmp full-checkout-err sparse-checkout-err && 2118 test_cmp full-checkout-err sparse-index-err 2119 ' 2120 2121 test_expect_success 'mv directory from out-of-cone to in-cone' ' 2122 init_repos && 2123 2124 # as a sparse directory (or SKIP_WORKTREE_DIR without enabling 2125 # sparse index). 2126 test_all_match git mv --sparse folder1 deep && 2127 test_all_match git status --porcelain=v2 && 2128 test_sparse_match git ls-files -t && 2129 git -C sparse-checkout ls-files -t >actual && 2130 test_grep -e "H deep/folder1/0/0/0" actual && 2131 test_grep -e "H deep/folder1/0/1" actual && 2132 test_grep -e "H deep/folder1/a" actual && 2133 2134 test_all_match git reset --hard && 2135 2136 # as a directory deeper than sparse index boundary (where 2137 # sparse index will expand). 2138 test_sparse_match git mv --sparse folder1/0 deep && 2139 test_sparse_match git status --porcelain=v2 && 2140 test_sparse_match git ls-files -t && 2141 git -C sparse-checkout ls-files -t >actual && 2142 test_grep -e "H deep/0/0/0" actual && 2143 test_grep -e "H deep/0/1" actual 2144 ' 2145 2146 test_expect_success 'rm pathspec inside sparse definition' ' 2147 init_repos && 2148 2149 test_all_match git rm deep/a && 2150 test_all_match git status --porcelain=v2 && 2151 2152 # test wildcard 2153 run_on_all git reset --hard && 2154 test_all_match git rm deep/* && 2155 test_all_match git status --porcelain=v2 && 2156 2157 # test recursive rm 2158 run_on_all git reset --hard && 2159 test_all_match git rm -r deep && 2160 test_all_match git status --porcelain=v2 2161 ' 2162 2163 test_expect_success 'rm pathspec outside sparse definition' ' 2164 init_repos && 2165 2166 for file in folder1/a folder1/0/1 2167 do 2168 test_sparse_match test_must_fail git rm $file && 2169 test_sparse_match test_must_fail git rm --cached $file && 2170 test_sparse_match git rm --sparse $file && 2171 test_sparse_match git status --porcelain=v2 || return 1 2172 done && 2173 2174 cat >folder1-full <<-EOF && 2175 rm ${SQ}folder1/0/0/0${SQ} 2176 rm ${SQ}folder1/0/1${SQ} 2177 rm ${SQ}folder1/a${SQ} 2178 EOF 2179 2180 cat >folder1-sparse <<-EOF && 2181 rm ${SQ}folder1/${SQ} 2182 EOF 2183 2184 # test wildcard 2185 run_on_sparse git reset --hard && 2186 run_on_sparse git sparse-checkout reapply && 2187 test_sparse_match test_must_fail git rm folder1/* && 2188 run_on_sparse git rm --sparse folder1/* && 2189 test_cmp folder1-full sparse-checkout-out && 2190 test_cmp folder1-sparse sparse-index-out && 2191 test_sparse_match git status --porcelain=v2 && 2192 2193 # test recursive rm 2194 run_on_sparse git reset --hard && 2195 run_on_sparse git sparse-checkout reapply && 2196 test_sparse_match test_must_fail git rm --sparse folder1 && 2197 run_on_sparse git rm --sparse -r folder1 && 2198 test_cmp folder1-full sparse-checkout-out && 2199 test_cmp folder1-sparse sparse-index-out && 2200 test_sparse_match git status --porcelain=v2 2201 ' 2202 2203 test_expect_success 'rm pathspec expands index when necessary' ' 2204 init_repos && 2205 2206 # in-cone pathspec (do not expand) 2207 ensure_not_expanded rm "deep/deep*" && 2208 test_must_be_empty sparse-index-err && 2209 2210 # out-of-cone pathspec (expand) 2211 ! ensure_not_expanded rm --sparse "folder1/a*" && 2212 test_must_be_empty sparse-index-err && 2213 2214 # pathspec that should expand index 2215 ! ensure_not_expanded rm "*/a" && 2216 test_must_be_empty sparse-index-err && 2217 2218 ! ensure_not_expanded rm "**a" && 2219 test_must_be_empty sparse-index-err 2220 ' 2221 2222 test_expect_success 'sparse index is not expanded: rm' ' 2223 init_repos && 2224 2225 ensure_not_expanded rm deep/a && 2226 2227 # test in-cone wildcard 2228 git -C sparse-index reset --hard && 2229 ensure_not_expanded rm deep/* && 2230 2231 # test recursive rm 2232 git -C sparse-index reset --hard && 2233 ensure_not_expanded rm -r deep 2234 ' 2235 2236 test_expect_success 'sparse index is not expanded: prefixed wildcard pathspec' ' 2237 init_repos && 2238 2239 ensure_not_expanded -C deep rm --dry-run -- "a*" && 2240 ensure_not_expanded -C deep reset base -- "a*" 2241 ' 2242 2243 test_expect_success 'grep with and --cached' ' 2244 init_repos && 2245 2246 test_all_match git grep --cached a && 2247 test_all_match git grep --cached a -- "folder1/*" 2248 ' 2249 2250 test_expect_success 'grep is not expanded' ' 2251 init_repos && 2252 2253 ensure_not_expanded grep a && 2254 ensure_not_expanded grep a -- deep/* && 2255 2256 # All files within the folder1/* pathspec are sparse, 2257 # so this command does not find any matches 2258 ensure_not_expanded ! grep a -- folder1/* && 2259 2260 # test out-of-cone pathspec with or without wildcard 2261 ensure_not_expanded grep --cached a -- "folder1/a" && 2262 ensure_not_expanded grep --cached a -- "folder1/*" && 2263 2264 # test in-cone pathspec with or without wildcard 2265 ensure_not_expanded grep --cached a -- "deep/a" && 2266 ensure_not_expanded grep --cached a -- "deep/*" 2267 ' 2268 2269 # NEEDSWORK: when running `grep` in the superproject with --recurse-submodules, 2270 # Git expands the index of the submodules unexpectedly. Even though `grep` 2271 # builtin is marked as "command_requires_full_index = 0", this config is only 2272 # useful for the superproject. Namely, the submodules have their own configs, 2273 # which are _not_ populated by the one-time sparse-index feature switch. 2274 test_expect_failure 'grep within submodules is not expanded' ' 2275 init_repos_as_submodules && 2276 2277 # do not use ensure_not_expanded() here, because `grep` should be 2278 # run in the superproject, not in "./sparse-index" 2279 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \ 2280 git grep --cached --recurse-submodules a -- "*/folder1/*" && 2281 test_region ! index ensure_full_index trace2.txt 2282 ' 2283 2284 # NEEDSWORK: this test is not actually testing the code. The design purpose 2285 # of this test is to verify the grep result when the submodules are using a 2286 # sparse-index. Namely, we want "folder1/" as a tree (a sparse directory); but 2287 # because of the index expansion, we are now grepping the "folder1/a" blob. 2288 # Because of the problem stated above 'grep within submodules is not expanded', 2289 # we don't have the ideal test environment yet. 2290 test_expect_success 'grep sparse directory within submodules' ' 2291 init_repos_as_submodules && 2292 2293 cat >expect <<-\EOF && 2294 full-checkout/folder1/a:a 2295 sparse-checkout/folder1/a:a 2296 sparse-index/folder1/a:a 2297 EOF 2298 git grep --cached --recurse-submodules a -- "*/folder1/*" >actual && 2299 test_cmp actual expect 2300 ' 2301 2302 test_expect_success 'write-tree' ' 2303 init_repos && 2304 2305 test_all_match git write-tree && 2306 2307 write_script edit-contents <<-\EOF && 2308 echo text >>"$1" 2309 EOF 2310 2311 # make a change inside the sparse cone 2312 run_on_all ../edit-contents deep/a && 2313 test_all_match git update-index deep/a && 2314 test_all_match git write-tree && 2315 test_all_match git status --porcelain=v2 && 2316 2317 # make a change outside the sparse cone 2318 run_on_all mkdir -p folder1 && 2319 run_on_all cp a folder1/a && 2320 run_on_all ../edit-contents folder1/a && 2321 test_all_match git update-index folder1/a && 2322 test_all_match git write-tree && 2323 test_all_match git status --porcelain=v2 && 2324 2325 # check that SKIP_WORKTREE files are not materialized 2326 test_path_is_missing sparse-checkout/folder2/a && 2327 test_path_is_missing sparse-index/folder2/a 2328 ' 2329 2330 test_expect_success 'sparse-index is not expanded: write-tree' ' 2331 init_repos && 2332 2333 ensure_not_expanded write-tree && 2334 2335 echo "test1" >>sparse-index/a && 2336 git -C sparse-index update-index a && 2337 ensure_not_expanded write-tree 2338 ' 2339 2340 test_expect_success 'diff-files with pathspec inside sparse definition' ' 2341 init_repos && 2342 2343 write_script edit-contents <<-\EOF && 2344 echo text >>"$1" 2345 EOF 2346 2347 run_on_all ../edit-contents deep/a && 2348 2349 test_all_match git diff-files && 2350 2351 test_all_match git diff-files -- deep/a && 2352 2353 # test wildcard 2354 test_all_match git diff-files -- "deep/*" 2355 ' 2356 2357 test_expect_success 'diff-files with pathspec outside sparse definition' ' 2358 init_repos && 2359 2360 test_sparse_match git diff-files -- folder2/a && 2361 2362 write_script edit-contents <<-\EOF && 2363 echo text >>"$1" 2364 EOF 2365 2366 # The directory "folder1" is outside the cone of interest 2367 # and will not exist in the sparse checkout repositories. 2368 # Create it as needed, add file "folder1/a" there with 2369 # contents that is different from the staged version. 2370 run_on_all mkdir -p folder1 && 2371 run_on_all cp a folder1/a && 2372 2373 run_on_all ../edit-contents folder1/a && 2374 test_all_match git diff-files && 2375 test_all_match git diff-files -- folder1/a && 2376 test_all_match git diff-files -- "folder*/a" 2377 ' 2378 2379 test_expect_success 'sparse index is not expanded: diff-files' ' 2380 init_repos && 2381 2382 write_script edit-contents <<-\EOF && 2383 echo text >>"$1" 2384 EOF 2385 2386 run_on_all ../edit-contents deep/a && 2387 2388 ensure_not_expanded diff-files && 2389 ensure_not_expanded diff-files -- deep/a && 2390 ensure_not_expanded diff-files -- "deep/*" 2391 ' 2392 2393 test_expect_success 'diff-tree' ' 2394 init_repos && 2395 2396 # Test change inside sparse cone 2397 tree1=$(git -C sparse-index rev-parse HEAD^{tree}) && 2398 tree2=$(git -C sparse-index rev-parse update-deep^{tree}) && 2399 test_all_match git diff-tree $tree1 $tree2 && 2400 test_all_match git diff-tree $tree1 $tree2 -- deep/a && 2401 test_all_match git diff-tree HEAD update-deep && 2402 test_all_match git diff-tree HEAD update-deep -- deep/a && 2403 2404 # Test change outside sparse cone 2405 tree3=$(git -C sparse-index rev-parse update-folder1^{tree}) && 2406 test_all_match git diff-tree $tree1 $tree3 && 2407 test_all_match git diff-tree $tree1 $tree3 -- folder1/a && 2408 test_all_match git diff-tree HEAD update-folder1 && 2409 test_all_match git diff-tree HEAD update-folder1 -- folder1/a && 2410 2411 # Check that SKIP_WORKTREE files are not materialized 2412 test_path_is_missing sparse-checkout/folder1/a && 2413 test_path_is_missing sparse-index/folder1/a && 2414 test_path_is_missing sparse-checkout/folder2/a && 2415 test_path_is_missing sparse-index/folder2/a 2416 ' 2417 2418 test_expect_success 'sparse-index is not expanded: diff-tree' ' 2419 init_repos && 2420 2421 tree1=$(git -C sparse-index rev-parse HEAD^{tree}) && 2422 tree2=$(git -C sparse-index rev-parse update-deep^{tree}) && 2423 tree3=$(git -C sparse-index rev-parse update-folder1^{tree}) && 2424 2425 ensure_not_expanded diff-tree $tree1 $tree2 && 2426 ensure_not_expanded diff-tree $tree1 $tree2 -- deep/a && 2427 ensure_not_expanded diff-tree HEAD update-deep && 2428 ensure_not_expanded diff-tree HEAD update-deep -- deep/a && 2429 ensure_not_expanded diff-tree $tree1 $tree3 && 2430 ensure_not_expanded diff-tree $tree1 $tree3 -- folder1/a && 2431 ensure_not_expanded diff-tree HEAD update-folder1 && 2432 ensure_not_expanded diff-tree HEAD update-folder1 -- folder1/a 2433 ' 2434 2435 test_expect_success 'worktree' ' 2436 init_repos && 2437 2438 write_script edit-contents <<-\EOF && 2439 echo text >>"$1" 2440 EOF 2441 2442 for repo in full-checkout sparse-checkout sparse-index 2443 do 2444 worktree=${repo}-wt && 2445 git -C $repo worktree add ../$worktree && 2446 2447 # Compare worktree content with "ls" 2448 (cd $repo && ls) >worktree_contents && 2449 (cd $worktree && ls) >new_worktree_contents && 2450 test_cmp worktree_contents new_worktree_contents && 2451 2452 # Compare index content with "ls-files --sparse" 2453 git -C $repo ls-files --sparse >index_contents && 2454 git -C $worktree ls-files --sparse >new_index_contents && 2455 test_cmp index_contents new_index_contents && 2456 2457 git -C $repo worktree remove ../$worktree || return 1 2458 done && 2459 2460 test_all_match git worktree add .worktrees/hotfix && 2461 run_on_all ../edit-contents .worktrees/hotfix/deep/a && 2462 test_all_match test_must_fail git worktree remove .worktrees/hotfix 2463 ' 2464 2465 test_expect_success 'worktree is not expanded' ' 2466 init_repos && 2467 2468 ensure_not_expanded worktree add .worktrees/hotfix && 2469 ensure_not_expanded worktree remove .worktrees/hotfix 2470 ' 2471 2472 test_expect_success 'check-attr with pathspec inside sparse definition' ' 2473 init_repos && 2474 2475 echo "a -crlf myAttr" >>.gitattributes && 2476 run_on_all cp ../.gitattributes ./deep && 2477 2478 test_all_match git check-attr -a -- deep/a && 2479 2480 test_all_match git add deep/.gitattributes && 2481 test_all_match git check-attr -a --cached -- deep/a 2482 ' 2483 2484 test_expect_success 'check-attr with pathspec outside sparse definition' ' 2485 init_repos && 2486 2487 echo "a -crlf myAttr" >>.gitattributes && 2488 run_on_sparse mkdir folder1 && 2489 run_on_all cp ../.gitattributes ./folder1 && 2490 run_on_all cp a folder1/a && 2491 2492 test_all_match git check-attr -a -- folder1/a && 2493 2494 git -C full-checkout add folder1/.gitattributes && 2495 test_sparse_match git add --sparse folder1/.gitattributes && 2496 test_all_match git commit -m "add .gitattributes" && 2497 test_sparse_match git sparse-checkout reapply && 2498 test_all_match git check-attr -a --cached -- folder1/a 2499 ' 2500 2501 # NEEDSWORK: The 'diff --check' test is left as 'test_expect_failure' due 2502 # to an underlying issue in oneway_diff() within diff-lib.c. 2503 # 'do_oneway_diff()' is not called as expected for paths that could match 2504 # inside of a sparse directory. Specifically, the 'ce_path_match()' function 2505 # fails to recognize files inside a sparse directory (e.g., when 'folder1/' 2506 # is a sparse directory, 'folder1/a' cannot be recognized). The goal is to 2507 # proceed with 'do_oneway_diff()' if the pathspec could match inside of a 2508 # sparse directory. 2509 test_expect_failure 'diff --check with pathspec outside sparse definition' ' 2510 init_repos && 2511 2512 write_script edit-contents <<-\EOF && 2513 echo "a " >"$1" 2514 EOF 2515 2516 test_all_match git config core.whitespace -trailing-space,-space-before-tab && 2517 2518 echo "a whitespace=trailing-space,space-before-tab" >>.gitattributes && 2519 run_on_all mkdir -p folder1 && 2520 run_on_all cp ../.gitattributes ./folder1 && 2521 test_all_match git add --sparse folder1/.gitattributes && 2522 run_on_all ../edit-contents folder1/a && 2523 test_all_match git add --sparse folder1/a && 2524 2525 test_sparse_match git sparse-checkout reapply && 2526 test_all_match test_must_fail git diff --check --cached -- folder1/a 2527 ' 2528 2529 test_expect_success 'sparse-index is not expanded: check-attr' ' 2530 init_repos && 2531 2532 echo "a -crlf myAttr" >>.gitattributes && 2533 mkdir ./sparse-index/folder1 && 2534 cp ./sparse-index/a ./sparse-index/folder1/a && 2535 cp .gitattributes ./sparse-index/deep && 2536 cp .gitattributes ./sparse-index/folder1 && 2537 2538 git -C sparse-index add deep/.gitattributes && 2539 git -C sparse-index add --sparse folder1/.gitattributes && 2540 ensure_not_expanded check-attr -a --cached -- deep/a && 2541 ensure_not_expanded check-attr -a --cached -- folder1/a 2542 ' 2543 2544 test_expect_success 'sparse-index is not expanded: git apply' ' 2545 init_repos && 2546 2547 git -C sparse-index checkout base && 2548 git -C full-checkout diff base..merge-right -- deep >patch-in-sparse && 2549 git -C full-checkout diff base..merge-right -- folder2 >patch-outside && 2550 2551 # Apply a patch to a file inside the sparse definition 2552 ensure_not_expanded apply --index --stat ../patch-in-sparse && 2553 2554 # Apply a patch to a file outside the sparse definition 2555 # Fails when caring about the worktree. 2556 ensure_not_expanded ! apply ../patch-outside && 2557 2558 # Expands when using --index. 2559 ensure_expanded apply --index ../patch-outside && 2560 2561 # Does not when index is partially expanded. 2562 git -C sparse-index reset --hard && 2563 ensure_not_expanded apply --cached ../patch-outside && 2564 2565 # Try again with a reset and collapsed index. 2566 git -C sparse-index reset --hard && 2567 git -C sparse-index sparse-checkout reapply && 2568 2569 # Expands when index is collapsed. 2570 ensure_expanded apply --cached ../patch-outside 2571 ' 2572 2573 test_expect_success 'sparse-index is not expanded: git add -p' ' 2574 init_repos && 2575 2576 # Does not expand when edits are within sparse checkout. 2577 echo "new content" >sparse-index/deep/a && 2578 echo "new content" >sparse-index/deep/deeper1/a && 2579 test_write_lines y n >in && 2580 ensure_not_expanded add -p sparse-index/folder1/a && 2587 test_write_lines y n y >in && 2588 ensure_expanded add -p sparse-index/folder1/a && 2597 test_write_lines u 2 3 "" q >in && 2598 ensure_expanded add -i sparse-index/deep/a && 2606 echo "new content" >sparse-index/deep/deeper1/a && 2607 git -C sparse-index commit -a -m "inside-changes" && 2608 2609 test_write_lines y y >in && 2610 ensure_not_expanded checkout HEAD~1 --patch sparse-index/deep/a && 2613 echo "new content" >sparse-index/deep/deeper1/a && 2614 git -C sparse-index add . && 2615 ensure_not_expanded reset --patch sparse-index/folder1/a && 2620 git -C sparse-index add --sparse folder1 && 2621 git -C sparse-index sparse-checkout reapply && 2622 ensure_expanded reset --patch sparse-index/folder1/a && 2627 git -C sparse-index add --sparse folder1 && 2628 git -C sparse-index commit -m "folder1 change" && 2629 git -C sparse-index sparse-checkout reapply && 2630 ensure_expanded checkout HEAD~1 --patch err && 2641 test_grep "The sparse index is expanding to a full index" err && 2642 2643 git -C sparse-index sparse-checkout disable 2>err && 2644 test_line_count = 0 err 2645 ' 2646 2647 test_expect_success 'cat-file -p' ' 2648 init_repos && 2649 echo "new content" >>full-checkout/deep/a && 2650 echo "new content" >>sparse-checkout/deep/a && 2651 echo "new content" >>sparse-index/deep/a && 2652 run_on_all git add deep/a && 2653 2654 test_all_match git cat-file -p :deep/a && 2655 ensure_not_expanded cat-file -p :deep/a && 2656 test_all_match git cat-file -p :folder1/a && 2657 ensure_expanded cat-file -p :folder1/a 2658 ' 2659 2660 test_expect_success 'cat-file --batch' ' 2661 init_repos && 2662 echo "new content" >>full-checkout/deep/a && 2663 echo "new content" >>sparse-checkout/deep/a && 2664 echo "new content" >>sparse-index/deep/a && 2665 run_on_all git add deep/a && 2666 2667 echo ":deep/a" >in && 2668 test_all_match git cat-file --batch in && 2672 test_all_match git cat-file --batch in <<-\EOF && 2676 :deep/a 2677 :folder1/a 2678 EOF 2679 test_all_match git cat-file --batch