3 test_description
='compare full workdir to sparse workdir'
10 test_expect_success
'setup' '
11 git init initial-repo &&
13 GIT_TEST_SPARSE_INDEX=0 &&
16 echo "after deep" >e &&
17 echo "after folder1" >g &&
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 &&
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 &&
47 cp -r deep/deeper1/0 folder1 &&
48 cp -r deep/deeper1/0 folder2 &&
49 echo >>folder1/0/0/0 &&
52 git commit -m "initial commit" &&
53 git checkout -b base &&
54 for dir in folder1 folder2 deep
56 git checkout -b update-$dir base &&
57 echo "updated $dir" >$dir/a &&
58 git commit -a -m "update $dir" || return 1
61 git checkout -b rename-base base &&
62 cat >folder1/larger-content <<-\EOF &&
69 cp folder1/larger-content folder2/ &&
70 cp folder1/larger-content deep/deeper1/ &&
72 git commit -m "add interesting rename content" &&
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 &&
79 echo stuff >>deep/deeper1/a &&
81 git commit -m "rename folder1/... to folder2/..." &&
83 git checkout -b rename-out-to-in rename-base &&
84 mv folder1/a deep/deeper1/b &&
85 echo more stuff >>deep/deeper1/a &&
88 echo >>folder2/0/1/1 &&
89 mv folder1/larger-content deep/deeper1/edited-content &&
90 echo >>deep/deeper1/edited-content &&
92 git commit -m "rename folder1/... to deep/deeper1/..." &&
94 git checkout -b rename-in-to-out rename-base &&
95 mv deep/deeper1/a folder1/b &&
99 mv deep/deeper1/larger-content folder1/edited-content &&
100 echo >>folder1/edited-content &&
102 git commit -m "rename deep/deeper1/... to folder1/..." &&
104 git checkout -b df-conflict-1 base &&
106 echo content >folder1 &&
108 git commit -m "dir to file" &&
110 git checkout -b df-conflict-2 base &&
112 echo content >folder2 &&
114 git commit -m "dir to file" &&
116 git checkout -b fd-conflict base &&
121 git commit -m "file to dir" &&
123 for side in left right
125 git checkout -b merge-$side base &&
126 echo $side >>deep/deeper2/a &&
127 echo $side >>folder1/a &&
128 echo $side >>folder2/a &&
130 git commit -m "$side" || return 1
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" &&
139 git checkout -f base &&
145 rm -rf full-checkout sparse-checkout sparse-index
&&
147 # create repos in initial state
148 cp -r initial-repo full-checkout
&&
149 git
-C full-checkout
reset --hard &&
151 cp -r initial-repo sparse-checkout
&&
152 git
-C sparse-checkout
reset --hard &&
154 cp -r initial-repo sparse-index
&&
155 git
-C sparse-index
reset --hard &&
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
167 cd sparse-checkout
&&
168 GIT_PROGRESS_DELAY
=100000 "$@" >..
/sparse-checkout-out
2>..
/sparse-checkout-err
172 GIT_PROGRESS_DELAY
=100000 "$@" >..
/sparse-index-out
2>..
/sparse-index-err
179 GIT_PROGRESS_DELAY
=100000 "$@" >..
/full-checkout-out
2>..
/full-checkout-err
186 test_cmp full-checkout-out sparse-checkout-out
&&
187 test_cmp full-checkout-out sparse-index-out
&&
188 test_cmp full-checkout-err sparse-checkout-err
&&
189 test_cmp full-checkout-err sparse-index-err
192 test_sparse_match
() {
193 run_on_sparse
"$@" &&
194 test_cmp sparse-checkout-out sparse-index-out
&&
195 test_cmp sparse-checkout-err sparse-index-err
198 test_sparse_unstaged
() {
200 for repo
in sparse-checkout sparse-index
202 # Skip "unmerged" paths
203 git
-C $repo diff --staged --diff-filter=u
-- "$file" >diff &&
204 test_must_be_empty
diff ||
return 1
208 # Usage: test_sprase_checkout_set "<c1> ... <cN>" "<s1> ... <sM>"
209 # Verifies that "git sparse-checkout set <c1> ... <cN>" succeeds and
210 # leaves the sparse index in a state where <s1> ... <sM> are sparse
211 # directories (and <c1> ... <cN> are not).
212 test_sparse_checkout_set
() {
215 git
-C sparse-index sparse-checkout
set --skip-checks $CONE_DIRS &&
216 git
-C sparse-index ls-files
--sparse --stage >cache
&&
218 # Check that the directories outside of the sparse-checkout cone
219 # have sparse directory entries.
220 for dir
in $SPARSE_DIRS
222 TREE
=$
(git
-C sparse-index rev-parse HEAD
:$dir) &&
223 grep "040000 $TREE 0 $dir/" cache \
227 # Check that the directories in the sparse-checkout cone
228 # are not sparse directory entries.
229 for dir
in $CONE_DIRS
231 # Allow TREE to not exist because
232 # $dir does not exist at HEAD.
233 TREE
=$
(git
-C sparse-index rev-parse HEAD
:$dir) ||
234 ! grep "040000 $TREE 0 $dir/" cache \
239 test_expect_success
'sparse-index contents' '
242 # Remove deep, add three other directories.
243 test_sparse_checkout_set \
244 "folder1 folder2 x" \
247 # Remove folder1, add deep
248 test_sparse_checkout_set \
252 # Replace deep with deep/deeper2 (dropping deep/deeper1)
254 test_sparse_checkout_set \
255 "deep/deeper2 folder1 folder2 x" \
256 "before deep/deeper1" &&
258 # Replace deep/deeper2 with deep/deeper1
259 # Replace folder1 with folder1/0/0
260 # Replace folder2 with non-existent folder2/2/3
261 # Add non-existent "bogus"
262 test_sparse_checkout_set \
263 "bogus deep/deeper1 folder1/0/0 folder2/2/3 x" \
264 "before deep/deeper2 folder2/0" &&
266 # Drop down to only files at root
267 test_sparse_checkout_set \
269 "before deep folder1 folder2 x" &&
271 # Disabling the sparse-index replaces tree entries with full ones
272 git -C sparse-index sparse-checkout init --no-sparse-index &&
273 test_sparse_match git ls-files --stage --sparse
276 test_expect_success
'expanded in-memory index matches full index' '
278 test_sparse_match git ls-files --stage
281 test_expect_success
'root directory cannot be sparse' '
284 # Remove all in-cone files and directories from the index, collapse index
285 # with `git sparse-checkout reapply`
286 git -C sparse-index rm -r . &&
287 git -C sparse-index sparse-checkout reapply &&
289 # Verify sparse directories still present, root directory is not sparse
290 cat >expect <<-EOF &&
296 git -C sparse-index ls-files --sparse >actual &&
297 test_cmp expect actual
300 test_expect_success
'status with options' '
302 test_sparse_match ls &&
303 test_all_match git status --porcelain=v2 &&
304 test_all_match git status --porcelain=v2 -z -u &&
305 test_all_match git status --porcelain=v2 -uno &&
306 run_on_all touch README.md &&
307 test_all_match git status --porcelain=v2 &&
308 test_all_match git status --porcelain=v2 -z -u &&
309 test_all_match git status --porcelain=v2 -uno &&
310 test_all_match git add README.md &&
311 test_all_match git status --porcelain=v2 &&
312 test_all_match git status --porcelain=v2 -z -u &&
313 test_all_match git status --porcelain=v2 -uno
316 test_expect_success
'status with diff in unexpanded sparse directory' '
318 test_all_match git checkout rename-base &&
319 test_all_match git reset --soft rename-out-to-out &&
320 test_all_match git status --porcelain=v2
323 test_expect_success
'status reports sparse-checkout' '
325 git -C sparse-checkout status >full &&
326 git -C sparse-index status >sparse &&
327 test_i18ngrep "You are in a sparse checkout with " full &&
328 test_i18ngrep "You are in a sparse checkout." sparse
331 test_expect_success
'add, commit, checkout' '
334 write_script edit-contents <<-\EOF &&
337 run_on_all ../edit-contents README.md &&
339 test_all_match git add README.md &&
340 test_all_match git status --porcelain=v2 &&
341 test_all_match git commit -m "Add README.md" &&
343 test_all_match git checkout HEAD~1 &&
344 test_all_match git checkout - &&
346 run_on_all ../edit-contents README.md &&
348 test_all_match git add -A &&
349 test_all_match git status --porcelain=v2 &&
350 test_all_match git commit -m "Extend README.md" &&
352 test_all_match git checkout HEAD~1 &&
353 test_all_match git checkout - &&
355 run_on_all ../edit-contents deep/newfile &&
357 test_all_match git status --porcelain=v2 -uno &&
358 test_all_match git status --porcelain=v2 &&
359 test_all_match git add . &&
360 test_all_match git status --porcelain=v2 &&
361 test_all_match git commit -m "add deep/newfile" &&
363 test_all_match git checkout HEAD~1 &&
364 test_all_match git checkout -
367 test_expect_success
'deep changes during checkout' '
370 test_sparse_match git sparse-checkout set deep/deeper1/deepest &&
371 test_all_match git checkout deepest &&
372 test_all_match git checkout base
375 test_expect_success
'checkout with modified sparse directory' '
378 test_all_match git checkout rename-in-to-out -- . &&
379 test_sparse_match git sparse-checkout reapply &&
380 test_all_match git checkout base
383 test_expect_success
'checkout orphan then non-orphan' '
386 test_all_match git checkout --orphan test-orphan &&
387 test_all_match git status --porcelain=v2 &&
388 test_all_match git checkout base &&
389 test_all_match git status --porcelain=v2
392 test_expect_success
'add outside sparse cone' '
395 run_on_sparse mkdir folder1 &&
396 run_on_sparse ../edit-contents folder1/a &&
397 run_on_sparse ../edit-contents folder1/newfile &&
398 test_sparse_match test_must_fail git add folder1/a &&
399 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
400 test_sparse_unstaged folder1/a &&
401 test_sparse_match test_must_fail git add folder1/newfile &&
402 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
403 test_sparse_unstaged folder1/newfile
406 test_expect_success
'commit including unstaged changes' '
409 write_script edit-file <<-\EOF &&
413 run_on_all ../edit-file 1 a &&
414 run_on_all ../edit-file 1 deep/a &&
416 test_all_match git commit -m "-a" -a &&
417 test_all_match git status --porcelain=v2 &&
419 run_on_all ../edit-file 2 a &&
420 run_on_all ../edit-file 2 deep/a &&
422 test_all_match git commit -m "--include" --include deep/a &&
423 test_all_match git status --porcelain=v2 &&
424 test_all_match git commit -m "--include" --include a &&
425 test_all_match git status --porcelain=v2 &&
427 run_on_all ../edit-file 3 a &&
428 run_on_all ../edit-file 3 deep/a &&
430 test_all_match git commit -m "--amend" -a --amend &&
431 test_all_match git status --porcelain=v2
434 test_expect_success
'status/add: outside sparse cone' '
437 # folder1 is at HEAD, but outside the sparse cone
438 run_on_sparse mkdir folder1 &&
439 cp initial-repo/folder1/a sparse-checkout/folder1/a &&
440 cp initial-repo/folder1/a sparse-index/folder1/a &&
442 test_sparse_match git status &&
444 write_script edit-contents <<-\EOF &&
447 run_on_all ../edit-contents folder1/a &&
448 run_on_all ../edit-contents folder1/new &&
450 test_sparse_match git status --porcelain=v2 &&
452 # Adding the path outside of the sparse-checkout cone should fail.
453 test_sparse_match test_must_fail git add folder1/a &&
454 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
455 test_sparse_unstaged folder1/a &&
456 test_all_match git add --refresh folder1/a &&
457 test_must_be_empty sparse-checkout-err &&
458 test_sparse_unstaged folder1/a &&
459 test_sparse_match test_must_fail git add folder1/new &&
460 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
461 test_sparse_unstaged folder1/new &&
462 test_sparse_match git add --sparse folder1/a &&
463 test_sparse_match git add --sparse folder1/new &&
465 test_all_match git add --sparse . &&
466 test_all_match git status --porcelain=v2 &&
467 test_all_match git commit -m folder1/new &&
468 test_all_match git rev-parse HEAD^{tree} &&
470 run_on_all ../edit-contents folder1/newer &&
471 test_all_match git add --sparse folder1/ &&
472 test_all_match git status --porcelain=v2 &&
473 test_all_match git commit -m folder1/newer &&
474 test_all_match git rev-parse HEAD^{tree}
477 test_expect_success
'checkout and reset --hard' '
480 test_all_match git checkout update-folder1 &&
481 test_all_match git status --porcelain=v2 &&
483 test_all_match git checkout update-deep &&
484 test_all_match git status --porcelain=v2 &&
486 test_all_match git checkout -b reset-test &&
487 test_all_match git reset --hard deepest &&
488 test_all_match git reset --hard update-folder1 &&
489 test_all_match git reset --hard update-folder2
492 test_expect_success
'diff --cached' '
495 write_script edit-contents <<-\EOF &&
496 echo text >>README.md
498 run_on_all ../edit-contents &&
500 test_all_match git diff &&
501 test_all_match git diff --cached &&
502 test_all_match git add README.md &&
503 test_all_match git diff &&
504 test_all_match git diff --cached
507 # NEEDSWORK: sparse-checkout behaves differently from full-checkout when
508 # running this test with 'df-conflict-2' after 'df-conflict-1'.
509 test_expect_success
'diff with renames and conflicts' '
512 for branch in rename-out-to-out \
518 test_all_match git checkout rename-base &&
519 test_all_match git checkout $branch -- . &&
520 test_all_match git status --porcelain=v2 &&
521 test_all_match git diff --cached --no-renames &&
522 test_all_match git diff --cached --find-renames || return 1
526 test_expect_success
'diff with directory/file conflicts' '
529 for branch in rename-out-to-out \
536 git -C full-checkout reset --hard &&
537 test_sparse_match git reset --hard &&
538 test_all_match git checkout $branch &&
539 test_all_match git checkout rename-base -- . &&
540 test_all_match git status --porcelain=v2 &&
541 test_all_match git diff --cached --no-renames &&
542 test_all_match git diff --cached --find-renames || return 1
546 test_expect_success
'log with pathspec outside sparse definition' '
549 test_all_match git log -- a &&
550 test_all_match git log -- folder1/a &&
551 test_all_match git log -- folder2/a &&
552 test_all_match git log -- deep/a &&
553 test_all_match git log -- deep/deeper1/a &&
554 test_all_match git log -- deep/deeper1/deepest/a &&
556 test_all_match git checkout update-folder1 &&
557 test_all_match git log -- folder1/a
560 test_expect_success
'blame with pathspec inside sparse definition' '
566 deep/deeper1/deepest/a
568 test_all_match git blame $file || return 1
572 # Without a revision specified, blame will error if passed any file that
573 # is not present in the working directory (even if the file is tracked).
574 # Here we just verify that this is also true with sparse checkouts.
575 test_expect_success
'blame with pathspec outside sparse definition' '
577 test_sparse_match git sparse-checkout set &&
582 deep/deeper1/deepest/a
584 test_sparse_match test_must_fail git blame $file &&
585 cat >expect <<-EOF &&
586 fatal: Cannot lstat '"'"'$file'"'"': No such file or directory
588 # We compare sparse-checkout-err and sparse-index-err in
589 # `test_sparse_match`. Given we know they are the same, we
590 # only check the content of sparse-index-err here.
591 test_cmp expect sparse-index-err || return 1
595 test_expect_success
'checkout and reset (mixed)' '
598 test_all_match git checkout -b reset-test update-deep &&
599 test_all_match git reset deepest &&
601 # Because skip-worktree is preserved, resetting to update-folder1
602 # will show worktree changes for folder1/a in full-checkout, but not
603 # in sparse-checkout or sparse-index.
604 git -C full-checkout reset update-folder1 >full-checkout-out &&
605 test_sparse_match git reset update-folder1 &&
606 grep "M folder1/a" full-checkout-out &&
607 ! grep "M folder1/a" sparse-checkout-out &&
608 run_on_sparse test_path_is_missing folder1
611 test_expect_success
'checkout and reset (merge)' '
614 write_script edit-contents <<-\EOF &&
618 test_all_match git checkout -b reset-test update-deep &&
619 run_on_all ../edit-contents a &&
620 test_all_match git reset --merge deepest &&
621 test_all_match git status --porcelain=v2 &&
623 test_all_match git reset --hard update-deep &&
624 run_on_all ../edit-contents deep/a &&
625 test_all_match test_must_fail git reset --merge deepest
628 test_expect_success
'checkout and reset (keep)' '
631 write_script edit-contents <<-\EOF &&
635 test_all_match git checkout -b reset-test update-deep &&
636 run_on_all ../edit-contents a &&
637 test_all_match git reset --keep deepest &&
638 test_all_match git status --porcelain=v2 &&
640 test_all_match git reset --hard update-deep &&
641 run_on_all ../edit-contents deep/a &&
642 test_all_match test_must_fail git reset --keep deepest
645 test_expect_success
'reset with pathspecs inside sparse definition' '
648 write_script edit-contents <<-\EOF &&
652 test_all_match git checkout -b reset-test update-deep &&
653 run_on_all ../edit-contents deep/a &&
655 test_all_match git reset base -- deep/a &&
656 test_all_match git status --porcelain=v2 &&
658 test_all_match git reset base -- nonexistent-file &&
659 test_all_match git status --porcelain=v2 &&
661 test_all_match git reset deepest -- deep &&
662 test_all_match git status --porcelain=v2
665 # Although the working tree differs between full and sparse checkouts after
666 # reset, the state of the index is the same.
667 test_expect_success
'reset with pathspecs outside sparse definition' '
669 test_all_match git checkout -b reset-test base &&
671 test_sparse_match git reset update-folder1 -- folder1 &&
672 git -C full-checkout reset update-folder1 -- folder1 &&
673 test_all_match git ls-files -s -- folder1 &&
675 test_sparse_match git reset update-folder2 -- folder2/a &&
676 git -C full-checkout reset update-folder2 -- folder2/a &&
677 test_all_match git ls-files -s -- folder2/a
680 test_expect_success
'reset with wildcard pathspec' '
683 test_all_match git reset update-deep -- deep\* &&
684 test_all_match git ls-files -s -- deep &&
686 test_all_match git reset deepest -- deep\*\*\* &&
687 test_all_match git ls-files -s -- deep &&
689 # The following `git reset`s result in updating the index on files with
690 # `skip-worktree` enabled. To avoid failing due to discrepencies in reported
691 # "modified" files, `test_sparse_match` reset is performed separately from
692 # "full-checkout" reset, then the index contents of all repos are verified.
694 test_sparse_match git reset update-folder1 -- \*/a &&
695 git -C full-checkout reset update-folder1 -- \*/a &&
696 test_all_match git ls-files -s -- deep/a folder1/a &&
698 test_sparse_match git reset update-folder2 -- folder\* &&
699 git -C full-checkout reset update-folder2 -- folder\* &&
700 test_all_match git ls-files -s -- folder10 folder1 folder2 &&
702 test_sparse_match git reset base -- folder1/\* &&
703 git -C full-checkout reset base -- folder1/\* &&
704 test_all_match git ls-files -s -- folder1
707 test_expect_success
'reset hard with removed sparse dir' '
710 run_on_all git rm -r --sparse folder1 &&
711 test_all_match git status --porcelain=v2 &&
713 test_all_match git reset --hard &&
714 test_all_match git status --porcelain=v2 &&
716 cat >expect <<-\EOF &&
720 git -C sparse-index ls-files --sparse folder1 >out &&
724 test_expect_success
'update-index modify outside sparse definition' '
727 write_script edit-contents <<-\EOF &&
731 # Create & modify folder1/a
732 # Note that this setup is a manual way of reaching the erroneous
733 # condition in which a `skip-worktree` enabled, outside-of-cone file
734 # exists on disk. It is used here to ensure `update-index` is stable
735 # and behaves predictably if such a condition occurs.
736 run_on_sparse mkdir -p folder1 &&
737 run_on_sparse cp ../initial-repo/folder1/a folder1/a &&
738 run_on_all ../edit-contents folder1/a &&
740 # If file has skip-worktree enabled, but the file is present, it is
741 # treated the same as if skip-worktree is disabled
742 test_all_match git status --porcelain=v2 &&
743 test_all_match git update-index folder1/a &&
744 test_all_match git status --porcelain=v2 &&
746 # When skip-worktree is disabled (even on files outside sparse cone), file
747 # is updated in the index
748 test_sparse_match git update-index --no-skip-worktree folder1/a &&
749 test_all_match git status --porcelain=v2 &&
750 test_all_match git update-index folder1/a &&
751 test_all_match git status --porcelain=v2
754 test_expect_success
'update-index --add outside sparse definition' '
757 write_script edit-contents <<-\EOF &&
761 # Create folder1, add new file
762 run_on_sparse mkdir -p folder1 &&
763 run_on_all ../edit-contents folder1/b &&
765 # The *untracked* out-of-cone file is added to the index because it does
766 # not have a `skip-worktree` bit to signal that it should be ignored
767 # (unlike in `git add`, which will fail due to the file being outside
768 # the sparse checkout definition).
769 test_all_match git update-index --add folder1/b &&
770 test_all_match git status --porcelain=v2
773 # NEEDSWORK: `--remove`, unlike the rest of `update-index`, does not ignore
774 # `skip-worktree` entries by default and will remove them from the index.
775 # The `--ignore-skip-worktree-entries` flag must be used in conjunction with
776 # `--remove` to ignore the `skip-worktree` entries and prevent their removal
778 test_expect_success
'update-index --remove outside sparse definition' '
781 # When --ignore-skip-worktree-entries is _not_ specified:
782 # out-of-cone, not-on-disk files are removed from the index
783 test_sparse_match git update-index --remove folder1/a &&
784 cat >expect <<-EOF &&
787 test_sparse_match git diff --cached --name-status &&
788 test_cmp expect sparse-checkout-out &&
791 test_all_match git reset --hard &&
793 # When --ignore-skip-worktree-entries is specified, out-of-cone
794 # (skip-worktree) files are ignored
795 test_sparse_match git update-index --remove --ignore-skip-worktree-entries folder1/a &&
796 test_sparse_match git diff --cached --name-status &&
797 test_must_be_empty sparse-checkout-out &&
800 test_all_match git reset --hard &&
802 # --force-remove supercedes --ignore-skip-worktree-entries, removing
803 # a skip-worktree file from the index (and disk) when both are specified
805 test_sparse_match git update-index --force-remove --ignore-skip-worktree-entries folder1/a &&
806 cat >expect <<-EOF &&
809 test_sparse_match git diff --cached --name-status &&
810 test_cmp expect sparse-checkout-out
813 test_expect_success
'update-index with directories' '
816 # update-index will exit silently when provided with a directory name
817 # containing a trailing slash
818 test_all_match git update-index deep/ folder1/ &&
819 grep "Ignoring path deep/" sparse-checkout-err &&
820 grep "Ignoring path folder1/" sparse-checkout-err &&
822 # When update-index is given a directory name WITHOUT a trailing slash, it will
823 # behave in different ways depending on the status of the directory on disk:
824 # * if it exists, the command exits with an error ("add individual files instead")
825 # * if it does NOT exist (e.g., in a sparse-checkout), it is assumed to be a
826 # file and either triggers an error ("does not exist and --remove not passed")
827 # or is ignored completely (when using --remove)
828 test_all_match test_must_fail git update-index deep &&
829 run_on_all test_must_fail git update-index folder1 &&
830 test_must_fail git -C full-checkout update-index --remove folder1 &&
831 test_sparse_match git update-index --remove folder1 &&
832 test_all_match git status --porcelain=v2
835 test_expect_success
'update-index --again file outside sparse definition' '
838 test_all_match git checkout -b test-reupdate &&
840 # Update HEAD without modifying the index to introduce a difference in
842 test_sparse_match git reset --soft update-folder1 &&
844 # Because folder1/a differs in the index vs HEAD,
845 # `git update-index --no-skip-worktree --again` will effectively perform
846 # `git update-index --no-skip-worktree folder1/a` and remove the skip-worktree
847 # flag from folder1/a
848 test_sparse_match git update-index --no-skip-worktree --again &&
849 test_sparse_match git status --porcelain=v2 &&
851 cat >expect <<-EOF &&
854 test_sparse_match git diff --name-status &&
855 test_cmp expect sparse-checkout-out
858 test_expect_success
'update-index --cacheinfo' '
861 deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
862 folder2_oid=$(git -C full-checkout rev-parse update-folder2:folder2) &&
863 folder1_a_oid=$(git -C full-checkout rev-parse update-folder1:folder1/a) &&
865 test_all_match git update-index --cacheinfo 100644 $deep_a_oid deep/a &&
866 test_all_match git status --porcelain=v2 &&
868 # Cannot add sparse directory, even in sparse index case
869 test_all_match test_must_fail git update-index --add --cacheinfo 040000 $folder2_oid folder2/ &&
871 # Sparse match only: the new outside-of-cone entry is added *without* skip-worktree,
872 # so `git status` reports it as "deleted" in the worktree
873 test_sparse_match git update-index --add --cacheinfo 100644 $folder1_a_oid folder1/a &&
874 test_sparse_match git status --porcelain=v2 &&
875 cat >expect <<-EOF &&
878 test_sparse_match git status --short -- folder1/a &&
879 test_cmp expect sparse-checkout-out &&
881 # To return folder1/a to "normal" for a sparse checkout (ignored &
882 # outside-of-cone), add the skip-worktree flag.
883 test_sparse_match git update-index --skip-worktree folder1/a &&
884 cat >expect <<-EOF &&
887 test_sparse_match git ls-files -t -- folder1/a &&
888 test_cmp expect sparse-checkout-out
891 for MERGE_TREES
in "base HEAD update-folder2" \
892 "update-folder1 update-folder2" \
895 test_expect_success
"'read-tree -mu $MERGE_TREES' with files outside sparse definition" '
898 # Although the index matches, without --no-sparse-checkout, outside-of-
899 # definition files will not exist on disk for sparse checkouts
900 test_all_match git read-tree -mu $MERGE_TREES &&
901 test_all_match git status --porcelain=v2 &&
902 test_path_is_missing sparse-checkout/folder2 &&
903 test_path_is_missing sparse-index/folder2 &&
905 test_all_match git read-tree --reset -u HEAD &&
906 test_all_match git status --porcelain=v2 &&
908 test_all_match git read-tree -mu --no-sparse-checkout $MERGE_TREES &&
909 test_all_match git status --porcelain=v2 &&
910 test_cmp sparse-checkout/folder2/a sparse-index/folder2/a &&
911 test_cmp sparse-checkout/folder2/a full-checkout/folder2/a
916 test_expect_success
'read-tree --merge with edit/edit conflicts in sparse directories' '
919 # Merge of multiple changes to same directory (but not same files) should
921 test_all_match git read-tree -mu base rename-base update-folder1 &&
922 test_all_match git status --porcelain=v2 &&
924 test_all_match git reset --hard &&
926 test_all_match git read-tree -mu rename-base update-folder2 &&
927 test_all_match git status --porcelain=v2 &&
929 test_all_match git reset --hard &&
931 test_all_match test_must_fail git read-tree -mu base update-folder1 rename-out-to-in &&
932 test_all_match test_must_fail git read-tree -mu rename-out-to-in update-folder1
935 test_expect_success
'read-tree --prefix' '
938 # If files differing between the index and target <commit-ish> exist
939 # inside the prefix, `read-tree --prefix` should fail
940 test_all_match test_must_fail git read-tree --prefix=deep/ deepest &&
941 test_all_match test_must_fail git read-tree --prefix=folder1/ update-folder1 &&
943 # If no differing index entries exist matching the prefix,
944 # `read-tree --prefix` updates the index successfully
945 test_all_match git rm -rf deep/deeper1/deepest/ &&
946 test_all_match git read-tree --prefix=deep/deeper1/deepest -u deepest &&
947 test_all_match git status --porcelain=v2 &&
949 run_on_all git rm -rf --sparse folder1/ &&
950 test_all_match git read-tree --prefix=folder1/ -u update-folder1 &&
951 test_all_match git status --porcelain=v2 &&
953 test_all_match git rm -rf --sparse folder2/0 &&
954 test_all_match git read-tree --prefix=folder2/0/ -u rename-out-to-out &&
955 test_all_match git status --porcelain=v2
958 test_expect_success
'read-tree --merge with directory-file conflicts' '
961 test_all_match git checkout -b test-branch rename-base &&
963 # Although the index matches, without --no-sparse-checkout, outside-of-
964 # definition files will not exist on disk for sparse checkouts
965 test_sparse_match git read-tree -mu rename-out-to-out &&
966 test_sparse_match git status --porcelain=v2 &&
967 test_path_is_missing sparse-checkout/folder2 &&
968 test_path_is_missing sparse-index/folder2 &&
970 test_sparse_match git read-tree --reset -u HEAD &&
971 test_sparse_match git status --porcelain=v2 &&
973 test_sparse_match git read-tree -mu --no-sparse-checkout rename-out-to-out &&
974 test_sparse_match git status --porcelain=v2 &&
975 test_cmp sparse-checkout/folder2/0/1 sparse-index/folder2/0/1
978 test_expect_success
'merge, cherry-pick, and rebase' '
981 for OPERATION in "merge -m merge" cherry-pick "rebase --apply" "rebase --merge"
983 test_all_match git checkout -B temp update-deep &&
984 test_all_match git $OPERATION update-folder1 &&
985 test_all_match git rev-parse HEAD^{tree} &&
986 test_all_match git $OPERATION update-folder2 &&
987 test_all_match git rev-parse HEAD^{tree} || return 1
991 test_expect_success
'merge with conflict outside cone' '
994 test_all_match git checkout -b merge-tip merge-left &&
995 test_all_match git status --porcelain=v2 &&
996 test_all_match test_must_fail git merge -m merge merge-right &&
997 test_all_match git status --porcelain=v2 &&
999 # Resolve the conflict in different ways:
1000 # 1. Revert to the base
1001 test_all_match git checkout base -- deep/deeper2/a &&
1002 test_all_match git status --porcelain=v2 &&
1004 # 2. Add the file with conflict markers
1005 test_sparse_match test_must_fail git add folder1/a &&
1006 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1007 test_sparse_unstaged folder1/a &&
1008 test_all_match git add --sparse folder1/a &&
1009 test_all_match git status --porcelain=v2 &&
1011 # 3. Rename the file to another sparse filename and
1012 # accept conflict markers as resolved content.
1013 run_on_all mv folder2/a folder2/z &&
1014 test_sparse_match test_must_fail git add folder2 &&
1015 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1016 test_sparse_unstaged folder2/z &&
1017 test_all_match git add --sparse folder2 &&
1018 test_all_match git status --porcelain=v2 &&
1020 test_all_match git merge --continue &&
1021 test_all_match git status --porcelain=v2 &&
1022 test_all_match git rev-parse HEAD^{tree}
1025 test_expect_success
'cherry-pick/rebase with conflict outside cone' '
1028 for OPERATION in cherry-pick rebase
1030 test_all_match git checkout -B tip &&
1031 test_all_match git reset --hard merge-left &&
1032 test_all_match git status --porcelain=v2 &&
1033 test_all_match test_must_fail git $OPERATION merge-right &&
1034 test_all_match git status --porcelain=v2 &&
1036 # Resolve the conflict in different ways:
1037 # 1. Revert to the base
1038 test_all_match git checkout base -- deep/deeper2/a &&
1039 test_all_match git status --porcelain=v2 &&
1041 # 2. Add the file with conflict markers
1042 # NEEDSWORK: Even though the merge conflict removed the
1043 # SKIP_WORKTREE bit from the index entry for folder1/a, we should
1044 # warn that this is a problematic add.
1045 test_sparse_match test_must_fail git add folder1/a &&
1046 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1047 test_sparse_unstaged folder1/a &&
1048 test_all_match git add --sparse folder1/a &&
1049 test_all_match git status --porcelain=v2 &&
1051 # 3. Rename the file to another sparse filename and
1052 # accept conflict markers as resolved content.
1053 # NEEDSWORK: This mode now fails, because folder2/z is
1054 # outside of the sparse-checkout cone and does not match an
1055 # existing index entry with the SKIP_WORKTREE bit cleared.
1056 run_on_all mv folder2/a folder2/z &&
1057 test_sparse_match test_must_fail git add folder2 &&
1058 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1059 test_sparse_unstaged folder2/z &&
1060 test_all_match git add --sparse folder2 &&
1061 test_all_match git status --porcelain=v2 &&
1063 test_all_match git $OPERATION --continue &&
1064 test_all_match git status --porcelain=v2 &&
1065 test_all_match git rev-parse HEAD^{tree} || return 1
1069 test_expect_success
'merge with outside renames' '
1072 for type in out-to-out out-to-in in-to-out
1074 test_all_match git reset --hard &&
1075 test_all_match git checkout -f -b merge-$type update-deep &&
1076 test_all_match git merge -m "$type" rename-$type &&
1077 test_all_match git rev-parse HEAD^{tree} || return 1
1081 # Sparse-index fails to convert the index in the
1082 # final 'git cherry-pick' command.
1083 test_expect_success
'cherry-pick with conflicts' '
1086 write_script edit-conflict <<-\EOF &&
1090 test_all_match git checkout -b to-cherry-pick &&
1091 run_on_all ../edit-conflict ABC &&
1092 test_all_match git add conflict &&
1093 test_all_match git commit -m "conflict to pick" &&
1095 test_all_match git checkout -B base HEAD~1 &&
1096 run_on_all ../edit-conflict DEF &&
1097 test_all_match git add conflict &&
1098 test_all_match git commit -m "conflict in base" &&
1100 test_all_match test_must_fail git cherry-pick to-cherry-pick
1103 test_expect_success
'stash' '
1106 write_script edit-contents <<-\EOF &&
1110 # Stash a sparse directory (folder1)
1111 test_all_match git checkout -b test-branch rename-base &&
1112 test_all_match git reset --soft rename-out-to-out &&
1113 test_all_match git stash &&
1114 test_all_match git status --porcelain=v2 &&
1116 # Apply the sparse directory stash without reinstating the index
1117 test_all_match git stash apply -q &&
1118 test_all_match git status --porcelain=v2 &&
1120 # Reset to state where stash can be applied
1121 test_sparse_match git sparse-checkout reapply &&
1122 test_all_match git reset --hard rename-out-to-out &&
1124 # Apply the sparse directory stash *with* reinstating the index
1125 test_all_match git stash apply --index -q &&
1126 test_all_match git status --porcelain=v2 &&
1128 # Reset to state where we will get a conflict applying the stash
1129 test_sparse_match git sparse-checkout reapply &&
1130 test_all_match git reset --hard update-folder1 &&
1132 # Apply the sparse directory stash with conflicts
1133 test_all_match test_must_fail git stash apply --index -q &&
1134 test_all_match test_must_fail git stash apply -q &&
1135 test_all_match git status --porcelain=v2 &&
1137 # Reset to base branch
1138 test_sparse_match git sparse-checkout reapply &&
1139 test_all_match git reset --hard base &&
1141 # Stash & unstash an untracked file outside of the sparse checkout
1143 run_on_sparse mkdir -p folder1 &&
1144 run_on_all ../edit-contents folder1/new &&
1145 test_all_match git stash -u &&
1146 test_all_match git status --porcelain=v2 &&
1148 test_all_match git stash pop -q &&
1149 test_all_match git status --porcelain=v2
1152 test_expect_success
'checkout-index inside sparse definition' '
1155 run_on_all rm -f deep/a &&
1156 test_all_match git checkout-index -- deep/a &&
1157 test_all_match git status --porcelain=v2 &&
1159 echo test >>new-a &&
1160 run_on_all cp ../new-a a &&
1161 test_all_match test_must_fail git checkout-index -- a &&
1162 test_all_match git checkout-index -f -- a &&
1163 test_all_match git status --porcelain=v2
1166 test_expect_success
'checkout-index outside sparse definition' '
1169 # Without --ignore-skip-worktree-bits, outside-of-cone files will trigger
1171 test_sparse_match test_must_fail git checkout-index -- folder1/a &&
1172 test_i18ngrep "folder1/a has skip-worktree enabled" sparse-checkout-err &&
1173 test_path_is_missing folder1/a &&
1175 # With --ignore-skip-worktree-bits, outside-of-cone files are checked out
1176 test_sparse_match git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
1177 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
1178 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a &&
1180 run_on_sparse rm -rf folder1 &&
1182 run_on_sparse mkdir -p folder1 &&
1183 run_on_all cp ../new-a folder1/a &&
1185 test_all_match test_must_fail git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
1186 test_all_match git checkout-index -f --ignore-skip-worktree-bits -- folder1/a &&
1187 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
1188 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a
1191 test_expect_success
'checkout-index with folders' '
1194 # Inside checkout definition
1195 test_all_match test_must_fail git checkout-index -f -- deep/ &&
1197 # Outside checkout definition
1198 # Note: although all tests fail (as expected), the messaging differs. For
1199 # non-sparse index checkouts, the error is that the "file" does not appear
1200 # in the index; for sparse checkouts, the error is explicitly that the
1201 # entry is a sparse directory.
1202 run_on_all test_must_fail git checkout-index -f -- folder1/ &&
1203 test_cmp full-checkout-err sparse-checkout-err &&
1204 ! test_cmp full-checkout-err sparse-index-err &&
1205 grep "is a sparse directory" sparse-index-err
1208 test_expect_success
'checkout-index --all' '
1211 test_all_match git checkout-index --all &&
1212 test_sparse_match test_path_is_missing folder1 &&
1214 # --ignore-skip-worktree-bits will cause `skip-worktree` files to be
1215 # checked out, causing the outside-of-cone `folder1` to exist on-disk
1216 test_all_match git checkout-index --ignore-skip-worktree-bits --all &&
1217 test_all_match test_path_exists folder1
1220 test_expect_success
'clean' '
1223 echo bogus >>.gitignore &&
1224 run_on_all cp ../.gitignore . &&
1225 test_all_match git add .gitignore &&
1226 test_all_match git commit -m "ignore bogus files" &&
1228 run_on_sparse mkdir folder1 &&
1229 run_on_all mkdir -p deep/untracked-deep &&
1230 run_on_all touch folder1/bogus &&
1231 run_on_all touch folder1/untracked &&
1232 run_on_all touch deep/untracked-deep/bogus &&
1233 run_on_all touch deep/untracked-deep/untracked &&
1235 test_all_match git status --porcelain=v2 &&
1236 test_all_match git clean -f &&
1237 test_all_match git status --porcelain=v2 &&
1238 test_sparse_match ls &&
1239 test_sparse_match ls folder1 &&
1240 run_on_all test_path_exists folder1/bogus &&
1241 run_on_all test_path_is_missing folder1/untracked &&
1242 run_on_all test_path_exists deep/untracked-deep/bogus &&
1243 run_on_all test_path_exists deep/untracked-deep/untracked &&
1245 test_all_match git clean -fd &&
1246 test_all_match git status --porcelain=v2 &&
1247 test_sparse_match ls &&
1248 test_sparse_match ls folder1 &&
1249 run_on_all test_path_exists folder1/bogus &&
1250 run_on_all test_path_exists deep/untracked-deep/bogus &&
1251 run_on_all test_path_is_missing deep/untracked-deep/untracked &&
1253 test_all_match git clean -xf &&
1254 test_all_match git status --porcelain=v2 &&
1255 test_sparse_match ls &&
1256 test_sparse_match ls folder1 &&
1257 run_on_all test_path_is_missing folder1/bogus &&
1258 run_on_all test_path_exists deep/untracked-deep/bogus &&
1260 test_all_match git clean -xdf &&
1261 test_all_match git status --porcelain=v2 &&
1262 test_sparse_match ls &&
1263 test_sparse_match ls folder1 &&
1264 run_on_all test_path_is_missing deep/untracked-deep/bogus &&
1266 test_sparse_match test_path_is_dir folder1
1269 for builtin in show rev-parse
1271 test_expect_success
"$builtin (cached blobs/trees)" "
1274 test_all_match git $builtin :a &&
1275 test_all_match git $builtin :deep/a &&
1276 test_sparse_match git $builtin :folder1/a &&
1278 # The error message differs depending on whether
1279 # the directory exists in the worktree.
1280 test_all_match test_must_fail git $builtin :deep/ &&
1281 test_must_fail git -C full-checkout $builtin :folder1/ &&
1282 test_sparse_match test_must_fail git $builtin :folder1/ &&
1284 # Change the sparse cone for an extra case:
1285 run_on_sparse git sparse-checkout set deep/deeper1 &&
1287 # deep/deeper2 is a sparse directory in the sparse index.
1288 test_sparse_match test_must_fail git $builtin :deep/deeper2/ &&
1290 # deep/deeper2/deepest is not in the sparse index, but
1291 # will trigger an index expansion.
1292 test_sparse_match test_must_fail git $builtin :deep/deeper2/deepest/
1296 test_expect_success
'submodule handling' '
1299 test_sparse_match git sparse-checkout add modules &&
1300 test_all_match mkdir modules &&
1301 test_all_match touch modules/a &&
1302 test_all_match git add modules &&
1303 test_all_match git commit -m "add modules directory" &&
1305 run_on_all git submodule add "$(pwd)/initial-repo" modules/sub &&
1306 test_all_match git commit -m "add submodule" &&
1308 # having a submodule prevents "modules" from collapse
1309 test_sparse_match git sparse-checkout set deep/deeper1 &&
1310 git -C sparse-index ls-files --sparse --stage >cache &&
1311 grep "100644 .* modules/a" cache &&
1312 grep "160000 $(git -C initial-repo rev-parse HEAD) 0 modules/sub" cache
1315 # When working with a sparse index, some commands will need to expand the
1316 # index to operate properly. If those commands also write the index back
1317 # to disk, they need to convert the index to sparse before writing.
1318 # This test verifies that both of these events are logged in trace2 logs.
1319 test_expect_success
'sparse-index is expanded and converted back' '
1322 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1323 git -C sparse-index reset -- folder1/a &&
1324 test_region index convert_to_sparse trace2.txt &&
1325 test_region index ensure_full_index trace2.txt &&
1327 # ls-files expands on read, but does not write.
1329 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1330 git -C sparse-index ls-files &&
1331 test_region index ensure_full_index trace2.txt
1334 test_expect_success
'index.sparse disabled inline uses full index' '
1337 # When index.sparse is disabled inline with `git status`, the
1338 # index is expanded at the beginning of the execution then never
1339 # converted back to sparse. It is then written to disk as a full index.
1341 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1342 git -C sparse-index -c index.sparse=false status &&
1343 ! test_region index convert_to_sparse trace2.txt &&
1344 test_region index ensure_full_index trace2.txt &&
1346 # Since index.sparse is set to true at a repo level, the index
1347 # is converted from full to sparse when read, then never expanded
1348 # over the course of `git status`. It is written to disk as a sparse
1351 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1352 git -C sparse-index status &&
1353 test_region index convert_to_sparse trace2.txt &&
1354 ! test_region index ensure_full_index trace2.txt &&
1356 # Now that the index has been written to disk as sparse, it is not
1357 # converted to sparse (or expanded to full) when read by `git status`.
1359 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1360 git -C sparse-index status &&
1361 ! test_region index convert_to_sparse trace2.txt &&
1362 ! test_region index ensure_full_index trace2.txt
1365 ensure_not_expanded
() {
1367 if test -z "$WITHOUT_UNTRACKED_TXT"
1369 echo >>sparse-index
/untracked.txt
1375 test_must_fail env \
1376 GIT_TRACE2_EVENT
="$(pwd)/trace2.txt" \
1377 git
-C sparse-index
"$@" \
1379 2>sparse-index-error ||
return 1
1381 GIT_TRACE2_EVENT
="$(pwd)/trace2.txt" \
1382 git
-C sparse-index
"$@" \
1384 2>sparse-index-error ||
return 1
1386 test_region
! index ensure_full_index trace2.txt
1389 test_expect_success
'sparse-index is not expanded' '
1392 ensure_not_expanded status &&
1393 ensure_not_expanded ls-files --sparse &&
1394 ensure_not_expanded commit --allow-empty -m empty &&
1395 echo >>sparse-index/a &&
1396 ensure_not_expanded commit -a -m a &&
1397 echo >>sparse-index/a &&
1398 ensure_not_expanded commit --include a -m a &&
1399 echo >>sparse-index/deep/deeper1/a &&
1400 ensure_not_expanded commit --include deep/deeper1/a -m deeper &&
1401 ensure_not_expanded checkout rename-out-to-out &&
1402 ensure_not_expanded checkout - &&
1403 ensure_not_expanded switch rename-out-to-out &&
1404 ensure_not_expanded switch - &&
1405 ensure_not_expanded reset --hard &&
1406 ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
1407 ensure_not_expanded reset --hard &&
1408 ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
1410 echo >>sparse-index/README.md &&
1411 ensure_not_expanded add -A &&
1412 echo >>sparse-index/extra.txt &&
1413 ensure_not_expanded add extra.txt &&
1414 echo >>sparse-index/untracked.txt &&
1415 ensure_not_expanded add . &&
1417 ensure_not_expanded checkout-index -f a &&
1418 ensure_not_expanded checkout-index -f --all &&
1419 for ref in update-deep update-folder1 update-folder2 update-deep
1421 echo >>sparse-index/README.md &&
1422 ensure_not_expanded reset --hard $ref || return 1
1425 ensure_not_expanded reset --mixed base &&
1426 ensure_not_expanded reset --hard update-deep &&
1427 ensure_not_expanded reset --keep base &&
1428 ensure_not_expanded reset --merge update-deep &&
1429 ensure_not_expanded reset --hard &&
1431 ensure_not_expanded reset base -- deep/a &&
1432 ensure_not_expanded reset base -- nonexistent-file &&
1433 ensure_not_expanded reset deepest -- deep &&
1435 # Although folder1 is outside the sparse definition, it exists as a
1436 # directory entry in the index, so the pathspec will not force the
1437 # index to be expanded.
1438 ensure_not_expanded reset deepest -- folder1 &&
1439 ensure_not_expanded reset deepest -- folder1/ &&
1441 # Wildcard identifies only in-cone files, no index expansion
1442 ensure_not_expanded reset deepest -- deep/\* &&
1444 # Wildcard identifies only full sparse directories, no index expansion
1445 ensure_not_expanded reset deepest -- folder\* &&
1447 ensure_not_expanded clean -fd &&
1449 ensure_not_expanded checkout -f update-deep &&
1450 test_config -C sparse-index pull.twohead ort &&
1452 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1453 for OPERATION in "merge -m merge" cherry-pick rebase
1455 ensure_not_expanded merge -m merge update-folder1 &&
1456 ensure_not_expanded merge -m merge update-folder2 || return 1
1461 test_expect_success
'sparse-index is not expanded: merge conflict in cone' '
1464 for side in right left
1466 git -C sparse-index checkout -b expand-$side base &&
1467 echo $side >sparse-index/deep/a &&
1468 git -C sparse-index commit -a -m "$side" || return 1
1472 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1473 git -C sparse-index config pull.twohead ort &&
1474 ensure_not_expanded ! merge -m merged expand-right
1478 test_expect_success
'sparse-index is not expanded: stash' '
1481 echo >>sparse-index/a &&
1482 ensure_not_expanded stash &&
1483 ensure_not_expanded stash list &&
1484 ensure_not_expanded stash show stash@{0} &&
1485 ensure_not_expanded stash apply stash@{0} &&
1486 ensure_not_expanded stash drop stash@{0} &&
1488 echo >>sparse-index/deep/new &&
1489 ensure_not_expanded stash -u &&
1491 WITHOUT_UNTRACKED_TXT=1 &&
1492 ensure_not_expanded stash pop
1495 ensure_not_expanded stash create &&
1496 oid=$(git -C sparse-index stash create) &&
1497 ensure_not_expanded stash store -m "test" $oid &&
1498 ensure_not_expanded reset --hard &&
1499 ensure_not_expanded stash pop
1502 test_expect_success
'sparse index is not expanded: diff' '
1505 write_script edit-contents <<-\EOF &&
1509 # Add file within cone
1510 test_sparse_match git sparse-checkout set deep &&
1511 run_on_all ../edit-contents deep/testfile &&
1512 test_all_match git add deep/testfile &&
1513 run_on_all ../edit-contents deep/testfile &&
1515 test_all_match git diff &&
1516 test_all_match git diff --cached &&
1517 ensure_not_expanded diff &&
1518 ensure_not_expanded diff --cached &&
1520 # Add file outside cone
1521 test_all_match git reset --hard &&
1522 run_on_all mkdir newdirectory &&
1523 run_on_all ../edit-contents newdirectory/testfile &&
1524 test_sparse_match git sparse-checkout set newdirectory &&
1525 test_all_match git add newdirectory/testfile &&
1526 run_on_all ../edit-contents newdirectory/testfile &&
1527 test_sparse_match git sparse-checkout set &&
1529 test_all_match git diff &&
1530 test_all_match git diff --cached &&
1531 ensure_not_expanded diff &&
1532 ensure_not_expanded diff --cached &&
1534 # Merge conflict outside cone
1535 # The sparse checkout will report a warning that is not in the
1536 # full checkout, so we use `run_on_all` instead of
1538 run_on_all git reset --hard &&
1539 test_all_match git checkout merge-left &&
1540 test_all_match test_must_fail git merge merge-right &&
1542 test_all_match git diff &&
1543 test_all_match git diff --cached &&
1544 ensure_not_expanded diff &&
1545 ensure_not_expanded diff --cached
1548 test_expect_success
'sparse index is not expanded: show and rev-parse' '
1551 ensure_not_expanded show :a &&
1552 ensure_not_expanded show :deep/a &&
1553 ensure_not_expanded rev-parse :a &&
1554 ensure_not_expanded rev-parse :deep/a
1557 test_expect_success
'sparse index is not expanded: update-index' '
1560 deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
1561 ensure_not_expanded update-index --cacheinfo 100644 $deep_a_oid deep/a &&
1563 echo "test" >sparse-index/README.md &&
1564 echo "test2" >sparse-index/a &&
1565 rm -f sparse-index/deep/a &&
1567 ensure_not_expanded update-index --add README.md &&
1568 ensure_not_expanded update-index a &&
1569 ensure_not_expanded update-index --remove deep/a &&
1571 ensure_not_expanded reset --soft update-deep &&
1572 ensure_not_expanded update-index --add --remove --again
1575 test_expect_success
'sparse index is not expanded: blame' '
1581 deep/deeper1/deepest/a
1583 ensure_not_expanded blame $file || return 1
1587 test_expect_success
'sparse index is not expanded: fetch/pull' '
1590 git -C sparse-index remote add full "file://$(pwd)/full-checkout" &&
1591 ensure_not_expanded fetch full &&
1592 git -C full-checkout commit --allow-empty -m "for pull merge" &&
1593 git -C sparse-index commit --allow-empty -m "for pull merge" &&
1594 ensure_not_expanded pull full base
1597 test_expect_success
'sparse index is not expanded: read-tree' '
1600 ensure_not_expanded checkout -b test-branch update-folder1 &&
1601 for MERGE_TREES in "base HEAD update-folder2" \
1602 "base HEAD rename-base" \
1603 "base update-folder2" \
1604 "base rename-base" \
1607 ensure_not_expanded read-tree -mu $MERGE_TREES &&
1608 ensure_not_expanded reset --hard || return 1
1611 rm -rf sparse-index/deep/deeper2 &&
1612 ensure_not_expanded add . &&
1613 ensure_not_expanded commit -m "test" &&
1615 ensure_not_expanded read-tree --prefix=deep/deeper2 -u deepest
1618 test_expect_success
'ls-files' '
1621 # Use a smaller sparse-checkout for reduced output
1622 test_sparse_match git sparse-checkout set &&
1624 # Behavior agrees by default. Sparse index is expanded.
1625 test_all_match git ls-files &&
1627 # With --sparse, the sparse index data changes behavior.
1628 git -C sparse-index ls-files --sparse >actual &&
1630 cat >expect <<-\EOF &&
1645 test_cmp expect actual &&
1647 # With --sparse and no sparse index, nothing changes.
1648 git -C sparse-checkout ls-files >dense &&
1649 git -C sparse-checkout ls-files --sparse >sparse &&
1650 test_cmp dense sparse &&
1652 # Set up a strange condition of having a file edit
1653 # outside of the sparse-checkout cone. We want to verify
1654 # that all modes handle this the same, and detect the
1656 write_script edit-content <<-\EOF &&
1658 echo content >>folder1/a
1660 run_on_all ../edit-content &&
1662 test_all_match git ls-files --modified &&
1664 git -C sparse-index ls-files --sparse --modified >sparse-index-out &&
1665 cat >expect <<-\EOF &&
1668 test_cmp expect sparse-index-out &&
1670 # Add folder1 to the sparse-checkout cone and
1671 # check that ls-files shows the expanded files.
1672 test_sparse_match git sparse-checkout add folder1 &&
1673 test_all_match git ls-files --modified &&
1675 test_all_match git ls-files &&
1676 git -C sparse-index ls-files --sparse >actual &&
1678 cat >expect <<-\EOF &&
1695 test_cmp expect actual &&
1697 # Double-check index expansion is avoided
1698 ensure_not_expanded ls-files --sparse
1701 test_expect_success
'sparse index is not expanded: sparse-checkout' '
1704 ensure_not_expanded sparse-checkout set deep/deeper2 &&
1705 ensure_not_expanded sparse-checkout set deep/deeper1 &&
1706 ensure_not_expanded sparse-checkout set deep &&
1707 ensure_not_expanded sparse-checkout add folder1 &&
1708 ensure_not_expanded sparse-checkout set deep/deeper1 &&
1709 ensure_not_expanded sparse-checkout set folder2 &&
1711 # Demonstrate that the checks that "folder1/a" is a file
1712 # do not cause a sparse-index expansion (since it is in the
1713 # sparse-checkout cone).
1714 echo >>sparse-index/folder2/a &&
1715 git -C sparse-index add folder2/a &&
1717 ensure_not_expanded sparse-checkout add folder1 &&
1719 # Skip checks here, since deep/deeper1 is inside a sparse directory
1720 # that must be expanded to check whether `deep/deeper1` is a file
1722 ensure_not_expanded sparse-checkout set --skip-checks deep/deeper1 &&
1723 ensure_not_expanded sparse-checkout set
1726 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
1727 # in this scenario, but it shouldn't.
1728 test_expect_success
'reset mixed and checkout orphan' '
1731 test_all_match git checkout rename-out-to-in &&
1733 # Sparse checkouts do not agree with full checkouts about
1734 # how to report a directory/file conflict during a reset.
1735 # This command would fail with test_all_match because the
1736 # full checkout reports "T folder1/0/1" while a sparse
1737 # checkout reports "D folder1/0/1". This matches because
1738 # the sparse checkouts skip "adding" the other side of
1740 test_sparse_match git reset --mixed HEAD~1 &&
1741 test_sparse_match git ls-files --stage &&
1742 test_sparse_match git status --porcelain=v2 &&
1744 # At this point, sparse-checkouts behave differently
1745 # from the full-checkout.
1746 test_sparse_match git checkout --orphan new-branch &&
1747 test_sparse_match git ls-files --stage &&
1748 test_sparse_match git status --porcelain=v2
1751 test_expect_success
'add everything with deep new file' '
1754 run_on_sparse git sparse-checkout set deep/deeper1/deepest &&
1756 run_on_all touch deep/deeper1/x &&
1757 test_all_match git add . &&
1758 test_all_match git status --porcelain=v2
1761 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1762 # directory/file conflicts, even without sparse-checkout. Use this
1763 # test only as a documentation of the incorrect behavior, not a
1764 # measure of how it _should_ behave.
1765 test_expect_success
'checkout behaves oddly with df-conflict-1' '
1768 test_sparse_match git sparse-checkout disable &&
1770 write_script edit-content <<-\EOF &&
1771 echo content >>folder1/larger-content
1775 run_on_all ../edit-content &&
1776 test_all_match git status --porcelain=v2 &&
1778 git -C sparse-checkout sparse-checkout init --cone &&
1779 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1781 test_all_match git status --porcelain=v2 &&
1783 # This checkout command should fail, because we have a staged
1784 # change to folder1/larger-content, but the destination changes
1785 # folder1 to a file.
1786 git -C full-checkout checkout df-conflict-1 \
1787 1>full-checkout-out \
1788 2>full-checkout-err &&
1789 git -C sparse-checkout checkout df-conflict-1 \
1790 1>sparse-checkout-out \
1791 2>sparse-checkout-err &&
1792 git -C sparse-index checkout df-conflict-1 \
1793 1>sparse-index-out \
1794 2>sparse-index-err &&
1796 # Instead, the checkout deletes the folder1 file and adds the
1797 # folder1/larger-content file, leaving all other paths that were
1798 # in folder1/ as deleted (without any warning).
1799 cat >expect <<-EOF &&
1801 A folder1/larger-content
1803 test_cmp expect full-checkout-out &&
1804 test_cmp expect sparse-checkout-out &&
1806 # The sparse-index reports no output
1807 test_must_be_empty sparse-index-out &&
1809 # stderr: Switched to branch df-conflict-1
1810 test_cmp full-checkout-err sparse-checkout-err &&
1811 test_cmp full-checkout-err sparse-checkout-err
1814 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1815 # directory/file conflicts, even without sparse-checkout. Use this
1816 # test only as a documentation of the incorrect behavior, not a
1817 # measure of how it _should_ behave.
1818 test_expect_success
'checkout behaves oddly with df-conflict-2' '
1821 test_sparse_match git sparse-checkout disable &&
1823 write_script edit-content <<-\EOF &&
1824 echo content >>folder2/larger-content
1828 run_on_all ../edit-content &&
1829 test_all_match git status --porcelain=v2 &&
1831 git -C sparse-checkout sparse-checkout init --cone &&
1832 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1834 test_all_match git status --porcelain=v2 &&
1836 # This checkout command should fail, because we have a staged
1837 # change to folder1/larger-content, but the destination changes
1838 # folder1 to a file.
1839 git -C full-checkout checkout df-conflict-2 \
1840 1>full-checkout-out \
1841 2>full-checkout-err &&
1842 git -C sparse-checkout checkout df-conflict-2 \
1843 1>sparse-checkout-out \
1844 2>sparse-checkout-err &&
1845 git -C sparse-index checkout df-conflict-2 \
1846 1>sparse-index-out \
1847 2>sparse-index-err &&
1849 # The full checkout deviates from the df-conflict-1 case here!
1850 # It drops the change to folder1/larger-content and leaves the
1851 # folder1 path as-is on disk. The sparse-index behaves the same.
1852 test_must_be_empty full-checkout-out &&
1853 test_must_be_empty sparse-index-out &&
1855 # In the sparse-checkout case, the checkout deletes the folder1
1856 # file and adds the folder1/larger-content file, leaving all other
1857 # paths that were in folder1/ as deleted (without any warning).
1858 cat >expect <<-EOF &&
1860 A folder2/larger-content
1862 test_cmp expect sparse-checkout-out &&
1864 # Switched to branch df-conflict-1
1865 test_cmp full-checkout-err sparse-checkout-err &&
1866 test_cmp full-checkout-err sparse-index-err
1869 test_expect_success
'mv directory from out-of-cone to in-cone' '
1872 # <source> as a sparse directory (or SKIP_WORKTREE_DIR without enabling
1874 test_all_match git mv --sparse folder1 deep &&
1875 test_all_match git status --porcelain=v2 &&
1876 test_sparse_match git ls-files -t &&
1877 git -C sparse-checkout ls-files -t >actual &&
1878 grep -e "H deep/folder1/0/0/0" actual &&
1879 grep -e "H deep/folder1/0/1" actual &&
1880 grep -e "H deep/folder1/a" actual &&
1882 test_all_match git reset --hard &&
1884 # <source> as a directory deeper than sparse index boundary (where
1885 # sparse index will expand).
1886 test_sparse_match git mv --sparse folder1/0 deep &&
1887 test_sparse_match git status --porcelain=v2 &&
1888 test_sparse_match git ls-files -t &&
1889 git -C sparse-checkout ls-files -t >actual &&
1890 grep -e "H deep/0/0/0" actual &&
1891 grep -e "H deep/0/1" actual
1894 test_expect_success
'rm pathspec inside sparse definition' '
1897 test_all_match git rm deep/a &&
1898 test_all_match git status --porcelain=v2 &&
1901 run_on_all git reset --hard &&
1902 test_all_match git rm deep/* &&
1903 test_all_match git status --porcelain=v2 &&
1906 run_on_all git reset --hard &&
1907 test_all_match git rm -r deep &&
1908 test_all_match git status --porcelain=v2
1911 test_expect_success
'rm pathspec outside sparse definition' '
1914 for file in folder1/a folder1/0/1
1916 test_sparse_match test_must_fail git rm $file &&
1917 test_sparse_match test_must_fail git rm --cached $file &&
1918 test_sparse_match git rm --sparse $file &&
1919 test_sparse_match git status --porcelain=v2 || return 1
1922 cat >folder1-full <<-EOF &&
1923 rm ${SQ}folder1/0/0/0${SQ}
1924 rm ${SQ}folder1/0/1${SQ}
1925 rm ${SQ}folder1/a${SQ}
1928 cat >folder1-sparse <<-EOF &&
1929 rm ${SQ}folder1/${SQ}
1933 run_on_sparse git reset --hard &&
1934 run_on_sparse git sparse-checkout reapply &&
1935 test_sparse_match test_must_fail git rm folder1/* &&
1936 run_on_sparse git rm --sparse folder1/* &&
1937 test_cmp folder1-full sparse-checkout-out &&
1938 test_cmp folder1-sparse sparse-index-out &&
1939 test_sparse_match git status --porcelain=v2 &&
1942 run_on_sparse git reset --hard &&
1943 run_on_sparse git sparse-checkout reapply &&
1944 test_sparse_match test_must_fail git rm --sparse folder1 &&
1945 run_on_sparse git rm --sparse -r folder1 &&
1946 test_cmp folder1-full sparse-checkout-out &&
1947 test_cmp folder1-sparse sparse-index-out &&
1948 test_sparse_match git status --porcelain=v2
1951 test_expect_success
'rm pathspec expands index when necessary' '
1954 # in-cone pathspec (do not expand)
1955 ensure_not_expanded rm "deep/deep*" &&
1956 test_must_be_empty sparse-index-err &&
1958 # out-of-cone pathspec (expand)
1959 ! ensure_not_expanded rm --sparse "folder1/a*" &&
1960 test_must_be_empty sparse-index-err &&
1962 # pathspec that should expand index
1963 ! ensure_not_expanded rm "*/a" &&
1964 test_must_be_empty sparse-index-err &&
1966 ! ensure_not_expanded rm "**a" &&
1967 test_must_be_empty sparse-index-err
1970 test_expect_success
'sparse index is not expanded: rm' '
1973 ensure_not_expanded rm deep/a &&
1975 # test in-cone wildcard
1976 git -C sparse-index reset --hard &&
1977 ensure_not_expanded rm deep/* &&
1980 git -C sparse-index reset --hard &&
1981 ensure_not_expanded rm -r deep