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
165 init_repos_as_submodules
() {
168 git submodule add .
/full-checkout
&&
169 git submodule add .
/sparse-checkout
&&
170 git submodule add .
/sparse-index
&&
172 git submodule status
>actual
&&
173 grep full-checkout actual
&&
174 grep sparse-checkout actual
&&
175 grep sparse-index actual
180 cd sparse-checkout
&&
181 GIT_PROGRESS_DELAY
=100000 "$@" >..
/sparse-checkout-out
2>..
/sparse-checkout-err
185 GIT_PROGRESS_DELAY
=100000 "$@" >..
/sparse-index-out
2>..
/sparse-index-err
192 GIT_PROGRESS_DELAY
=100000 "$@" >..
/full-checkout-out
2>..
/full-checkout-err
199 test_cmp full-checkout-out sparse-checkout-out
&&
200 test_cmp full-checkout-out sparse-index-out
&&
201 test_cmp full-checkout-err sparse-checkout-err
&&
202 test_cmp full-checkout-err sparse-index-err
205 test_sparse_match
() {
206 run_on_sparse
"$@" &&
207 test_cmp sparse-checkout-out sparse-index-out
&&
208 test_cmp sparse-checkout-err sparse-index-err
211 test_sparse_unstaged
() {
213 for repo
in sparse-checkout sparse-index
215 # Skip "unmerged" paths
216 git
-C $repo diff --staged --diff-filter=u
-- "$file" >diff &&
217 test_must_be_empty
diff ||
return 1
221 # Usage: test_sprase_checkout_set "<c1> ... <cN>" "<s1> ... <sM>"
222 # Verifies that "git sparse-checkout set <c1> ... <cN>" succeeds and
223 # leaves the sparse index in a state where <s1> ... <sM> are sparse
224 # directories (and <c1> ... <cN> are not).
225 test_sparse_checkout_set
() {
228 git
-C sparse-index sparse-checkout
set --skip-checks $CONE_DIRS &&
229 git
-C sparse-index ls-files
--sparse --stage >cache
&&
231 # Check that the directories outside of the sparse-checkout cone
232 # have sparse directory entries.
233 for dir
in $SPARSE_DIRS
235 TREE
=$
(git
-C sparse-index rev-parse HEAD
:$dir) &&
236 grep "040000 $TREE 0 $dir/" cache \
240 # Check that the directories in the sparse-checkout cone
241 # are not sparse directory entries.
242 for dir
in $CONE_DIRS
244 # Allow TREE to not exist because
245 # $dir does not exist at HEAD.
246 TREE
=$
(git
-C sparse-index rev-parse HEAD
:$dir) ||
247 ! grep "040000 $TREE 0 $dir/" cache \
252 test_expect_success
'sparse-index contents' '
255 # Remove deep, add three other directories.
256 test_sparse_checkout_set \
257 "folder1 folder2 x" \
260 # Remove folder1, add deep
261 test_sparse_checkout_set \
265 # Replace deep with deep/deeper2 (dropping deep/deeper1)
267 test_sparse_checkout_set \
268 "deep/deeper2 folder1 folder2 x" \
269 "before deep/deeper1" &&
271 # Replace deep/deeper2 with deep/deeper1
272 # Replace folder1 with folder1/0/0
273 # Replace folder2 with non-existent folder2/2/3
274 # Add non-existent "bogus"
275 test_sparse_checkout_set \
276 "bogus deep/deeper1 folder1/0/0 folder2/2/3 x" \
277 "before deep/deeper2 folder2/0" &&
279 # Drop down to only files at root
280 test_sparse_checkout_set \
282 "before deep folder1 folder2 x" &&
284 # Disabling the sparse-index replaces tree entries with full ones
285 git -C sparse-index sparse-checkout init --no-sparse-index &&
286 test_sparse_match git ls-files --stage --sparse
289 test_expect_success
'expanded in-memory index matches full index' '
291 test_sparse_match git ls-files --stage
294 test_expect_success
'root directory cannot be sparse' '
297 # Remove all in-cone files and directories from the index, collapse index
298 # with `git sparse-checkout reapply`
299 git -C sparse-index rm -r . &&
300 git -C sparse-index sparse-checkout reapply &&
302 # Verify sparse directories still present, root directory is not sparse
303 cat >expect <<-EOF &&
309 git -C sparse-index ls-files --sparse >actual &&
310 test_cmp expect actual
313 test_expect_success
'status with options' '
315 test_sparse_match ls &&
316 test_all_match git status --porcelain=v2 &&
317 test_all_match git status --porcelain=v2 -z -u &&
318 test_all_match git status --porcelain=v2 -uno &&
319 run_on_all touch README.md &&
320 test_all_match git status --porcelain=v2 &&
321 test_all_match git status --porcelain=v2 -z -u &&
322 test_all_match git status --porcelain=v2 -uno &&
323 test_all_match git add README.md &&
324 test_all_match git status --porcelain=v2 &&
325 test_all_match git status --porcelain=v2 -z -u &&
326 test_all_match git status --porcelain=v2 -uno
329 test_expect_success
'status with diff in unexpanded sparse directory' '
331 test_all_match git checkout rename-base &&
332 test_all_match git reset --soft rename-out-to-out &&
333 test_all_match git status --porcelain=v2
336 test_expect_success
'status reports sparse-checkout' '
338 git -C sparse-checkout status >full &&
339 git -C sparse-index status >sparse &&
340 test_i18ngrep "You are in a sparse checkout with " full &&
341 test_i18ngrep "You are in a sparse checkout." sparse
344 test_expect_success
'add, commit, checkout' '
347 write_script edit-contents <<-\EOF &&
350 run_on_all ../edit-contents README.md &&
352 test_all_match git add README.md &&
353 test_all_match git status --porcelain=v2 &&
354 test_all_match git commit -m "Add README.md" &&
356 test_all_match git checkout HEAD~1 &&
357 test_all_match git checkout - &&
359 run_on_all ../edit-contents README.md &&
361 test_all_match git add -A &&
362 test_all_match git status --porcelain=v2 &&
363 test_all_match git commit -m "Extend README.md" &&
365 test_all_match git checkout HEAD~1 &&
366 test_all_match git checkout - &&
368 run_on_all ../edit-contents deep/newfile &&
370 test_all_match git status --porcelain=v2 -uno &&
371 test_all_match git status --porcelain=v2 &&
372 test_all_match git add . &&
373 test_all_match git status --porcelain=v2 &&
374 test_all_match git commit -m "add deep/newfile" &&
376 test_all_match git checkout HEAD~1 &&
377 test_all_match git checkout -
380 test_expect_success
'deep changes during checkout' '
383 test_sparse_match git sparse-checkout set deep/deeper1/deepest &&
384 test_all_match git checkout deepest &&
385 test_all_match git checkout base
388 test_expect_success
'checkout with modified sparse directory' '
391 test_all_match git checkout rename-in-to-out -- . &&
392 test_sparse_match git sparse-checkout reapply &&
393 test_all_match git checkout base
396 test_expect_success
'checkout orphan then non-orphan' '
399 test_all_match git checkout --orphan test-orphan &&
400 test_all_match git status --porcelain=v2 &&
401 test_all_match git checkout base &&
402 test_all_match git status --porcelain=v2
405 test_expect_success
'add outside sparse cone' '
408 run_on_sparse mkdir folder1 &&
409 run_on_sparse ../edit-contents folder1/a &&
410 run_on_sparse ../edit-contents folder1/newfile &&
411 test_sparse_match test_must_fail git add folder1/a &&
412 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
413 test_sparse_unstaged folder1/a &&
414 test_sparse_match test_must_fail git add folder1/newfile &&
415 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
416 test_sparse_unstaged folder1/newfile
419 test_expect_success
'commit including unstaged changes' '
422 write_script edit-file <<-\EOF &&
426 run_on_all ../edit-file 1 a &&
427 run_on_all ../edit-file 1 deep/a &&
429 test_all_match git commit -m "-a" -a &&
430 test_all_match git status --porcelain=v2 &&
432 run_on_all ../edit-file 2 a &&
433 run_on_all ../edit-file 2 deep/a &&
435 test_all_match git commit -m "--include" --include deep/a &&
436 test_all_match git status --porcelain=v2 &&
437 test_all_match git commit -m "--include" --include a &&
438 test_all_match git status --porcelain=v2 &&
440 run_on_all ../edit-file 3 a &&
441 run_on_all ../edit-file 3 deep/a &&
443 test_all_match git commit -m "--amend" -a --amend &&
444 test_all_match git status --porcelain=v2
447 test_expect_success
'status/add: outside sparse cone' '
450 # folder1 is at HEAD, but outside the sparse cone
451 run_on_sparse mkdir folder1 &&
452 cp initial-repo/folder1/a sparse-checkout/folder1/a &&
453 cp initial-repo/folder1/a sparse-index/folder1/a &&
455 test_sparse_match git status &&
457 write_script edit-contents <<-\EOF &&
460 run_on_all ../edit-contents folder1/a &&
461 run_on_all ../edit-contents folder1/new &&
463 test_sparse_match git status --porcelain=v2 &&
465 # Adding the path outside of the sparse-checkout cone should fail.
466 test_sparse_match test_must_fail git add folder1/a &&
467 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
468 test_sparse_unstaged folder1/a &&
469 test_all_match git add --refresh folder1/a &&
470 test_must_be_empty sparse-checkout-err &&
471 test_sparse_unstaged folder1/a &&
472 test_sparse_match test_must_fail git add folder1/new &&
473 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
474 test_sparse_unstaged folder1/new &&
475 test_sparse_match git add --sparse folder1/a &&
476 test_sparse_match git add --sparse folder1/new &&
478 test_all_match git add --sparse . &&
479 test_all_match git status --porcelain=v2 &&
480 test_all_match git commit -m folder1/new &&
481 test_all_match git rev-parse HEAD^{tree} &&
483 run_on_all ../edit-contents folder1/newer &&
484 test_all_match git add --sparse folder1/ &&
485 test_all_match git status --porcelain=v2 &&
486 test_all_match git commit -m folder1/newer &&
487 test_all_match git rev-parse HEAD^{tree}
490 test_expect_success
'checkout and reset --hard' '
493 test_all_match git checkout update-folder1 &&
494 test_all_match git status --porcelain=v2 &&
496 test_all_match git checkout update-deep &&
497 test_all_match git status --porcelain=v2 &&
499 test_all_match git checkout -b reset-test &&
500 test_all_match git reset --hard deepest &&
501 test_all_match git reset --hard update-folder1 &&
502 test_all_match git reset --hard update-folder2
505 test_expect_success
'diff --cached' '
508 write_script edit-contents <<-\EOF &&
509 echo text >>README.md
511 run_on_all ../edit-contents &&
513 test_all_match git diff &&
514 test_all_match git diff --cached &&
515 test_all_match git add README.md &&
516 test_all_match git diff &&
517 test_all_match git diff --cached
520 # NEEDSWORK: sparse-checkout behaves differently from full-checkout when
521 # running this test with 'df-conflict-2' after 'df-conflict-1'.
522 test_expect_success
'diff with renames and conflicts' '
525 for branch in rename-out-to-out \
531 test_all_match git checkout rename-base &&
532 test_all_match git checkout $branch -- . &&
533 test_all_match git status --porcelain=v2 &&
534 test_all_match git diff --cached --no-renames &&
535 test_all_match git diff --cached --find-renames || return 1
539 test_expect_success
'diff with directory/file conflicts' '
542 for branch in rename-out-to-out \
549 git -C full-checkout reset --hard &&
550 test_sparse_match git reset --hard &&
551 test_all_match git checkout $branch &&
552 test_all_match git checkout rename-base -- . &&
553 test_all_match git status --porcelain=v2 &&
554 test_all_match git diff --cached --no-renames &&
555 test_all_match git diff --cached --find-renames || return 1
559 test_expect_success
'log with pathspec outside sparse definition' '
562 test_all_match git log -- a &&
563 test_all_match git log -- folder1/a &&
564 test_all_match git log -- folder2/a &&
565 test_all_match git log -- deep/a &&
566 test_all_match git log -- deep/deeper1/a &&
567 test_all_match git log -- deep/deeper1/deepest/a &&
569 test_all_match git checkout update-folder1 &&
570 test_all_match git log -- folder1/a
573 test_expect_success
'blame with pathspec inside sparse definition' '
579 deep/deeper1/deepest/a
581 test_all_match git blame $file || return 1
585 # Without a revision specified, blame will error if passed any file that
586 # is not present in the working directory (even if the file is tracked).
587 # Here we just verify that this is also true with sparse checkouts.
588 test_expect_success
'blame with pathspec outside sparse definition' '
590 test_sparse_match git sparse-checkout set &&
595 deep/deeper1/deepest/a
597 test_sparse_match test_must_fail git blame $file &&
598 cat >expect <<-EOF &&
599 fatal: Cannot lstat '"'"'$file'"'"': No such file or directory
601 # We compare sparse-checkout-err and sparse-index-err in
602 # `test_sparse_match`. Given we know they are the same, we
603 # only check the content of sparse-index-err here.
604 test_cmp expect sparse-index-err || return 1
608 test_expect_success
'checkout and reset (mixed)' '
611 test_all_match git checkout -b reset-test update-deep &&
612 test_all_match git reset deepest &&
614 # Because skip-worktree is preserved, resetting to update-folder1
615 # will show worktree changes for folder1/a in full-checkout, but not
616 # in sparse-checkout or sparse-index.
617 git -C full-checkout reset update-folder1 >full-checkout-out &&
618 test_sparse_match git reset update-folder1 &&
619 grep "M folder1/a" full-checkout-out &&
620 ! grep "M folder1/a" sparse-checkout-out &&
621 run_on_sparse test_path_is_missing folder1
624 test_expect_success
'checkout and reset (merge)' '
627 write_script edit-contents <<-\EOF &&
631 test_all_match git checkout -b reset-test update-deep &&
632 run_on_all ../edit-contents a &&
633 test_all_match git reset --merge deepest &&
634 test_all_match git status --porcelain=v2 &&
636 test_all_match git reset --hard update-deep &&
637 run_on_all ../edit-contents deep/a &&
638 test_all_match test_must_fail git reset --merge deepest
641 test_expect_success
'checkout and reset (keep)' '
644 write_script edit-contents <<-\EOF &&
648 test_all_match git checkout -b reset-test update-deep &&
649 run_on_all ../edit-contents a &&
650 test_all_match git reset --keep deepest &&
651 test_all_match git status --porcelain=v2 &&
653 test_all_match git reset --hard update-deep &&
654 run_on_all ../edit-contents deep/a &&
655 test_all_match test_must_fail git reset --keep deepest
658 test_expect_success
'reset with pathspecs inside sparse definition' '
661 write_script edit-contents <<-\EOF &&
665 test_all_match git checkout -b reset-test update-deep &&
666 run_on_all ../edit-contents deep/a &&
668 test_all_match git reset base -- deep/a &&
669 test_all_match git status --porcelain=v2 &&
671 test_all_match git reset base -- nonexistent-file &&
672 test_all_match git status --porcelain=v2 &&
674 test_all_match git reset deepest -- deep &&
675 test_all_match git status --porcelain=v2
678 # Although the working tree differs between full and sparse checkouts after
679 # reset, the state of the index is the same.
680 test_expect_success
'reset with pathspecs outside sparse definition' '
682 test_all_match git checkout -b reset-test base &&
684 test_sparse_match git reset update-folder1 -- folder1 &&
685 git -C full-checkout reset update-folder1 -- folder1 &&
686 test_all_match git ls-files -s -- folder1 &&
688 test_sparse_match git reset update-folder2 -- folder2/a &&
689 git -C full-checkout reset update-folder2 -- folder2/a &&
690 test_all_match git ls-files -s -- folder2/a
693 test_expect_success
'reset with wildcard pathspec' '
696 test_all_match git reset update-deep -- deep\* &&
697 test_all_match git ls-files -s -- deep &&
699 test_all_match git reset deepest -- deep\*\*\* &&
700 test_all_match git ls-files -s -- deep &&
702 # The following `git reset`s result in updating the index on files with
703 # `skip-worktree` enabled. To avoid failing due to discrepencies in reported
704 # "modified" files, `test_sparse_match` reset is performed separately from
705 # "full-checkout" reset, then the index contents of all repos are verified.
707 test_sparse_match git reset update-folder1 -- \*/a &&
708 git -C full-checkout reset update-folder1 -- \*/a &&
709 test_all_match git ls-files -s -- deep/a folder1/a &&
711 test_sparse_match git reset update-folder2 -- folder\* &&
712 git -C full-checkout reset update-folder2 -- folder\* &&
713 test_all_match git ls-files -s -- folder10 folder1 folder2 &&
715 test_sparse_match git reset base -- folder1/\* &&
716 git -C full-checkout reset base -- folder1/\* &&
717 test_all_match git ls-files -s -- folder1
720 test_expect_success
'reset hard with removed sparse dir' '
723 run_on_all git rm -r --sparse folder1 &&
724 test_all_match git status --porcelain=v2 &&
726 test_all_match git reset --hard &&
727 test_all_match git status --porcelain=v2 &&
729 cat >expect <<-\EOF &&
733 git -C sparse-index ls-files --sparse folder1 >out &&
737 test_expect_success
'update-index modify outside sparse definition' '
740 write_script edit-contents <<-\EOF &&
744 # Create & modify folder1/a
745 # Note that this setup is a manual way of reaching the erroneous
746 # condition in which a `skip-worktree` enabled, outside-of-cone file
747 # exists on disk. It is used here to ensure `update-index` is stable
748 # and behaves predictably if such a condition occurs.
749 run_on_sparse mkdir -p folder1 &&
750 run_on_sparse cp ../initial-repo/folder1/a folder1/a &&
751 run_on_all ../edit-contents folder1/a &&
753 # If file has skip-worktree enabled, but the file is present, it is
754 # treated the same as if skip-worktree is disabled
755 test_all_match git status --porcelain=v2 &&
756 test_all_match git update-index folder1/a &&
757 test_all_match git status --porcelain=v2 &&
759 # When skip-worktree is disabled (even on files outside sparse cone), file
760 # is updated in the index
761 test_sparse_match git update-index --no-skip-worktree folder1/a &&
762 test_all_match git status --porcelain=v2 &&
763 test_all_match git update-index folder1/a &&
764 test_all_match git status --porcelain=v2
767 test_expect_success
'update-index --add outside sparse definition' '
770 write_script edit-contents <<-\EOF &&
774 # Create folder1, add new file
775 run_on_sparse mkdir -p folder1 &&
776 run_on_all ../edit-contents folder1/b &&
778 # The *untracked* out-of-cone file is added to the index because it does
779 # not have a `skip-worktree` bit to signal that it should be ignored
780 # (unlike in `git add`, which will fail due to the file being outside
781 # the sparse checkout definition).
782 test_all_match git update-index --add folder1/b &&
783 test_all_match git status --porcelain=v2
786 # NEEDSWORK: `--remove`, unlike the rest of `update-index`, does not ignore
787 # `skip-worktree` entries by default and will remove them from the index.
788 # The `--ignore-skip-worktree-entries` flag must be used in conjunction with
789 # `--remove` to ignore the `skip-worktree` entries and prevent their removal
791 test_expect_success
'update-index --remove outside sparse definition' '
794 # When --ignore-skip-worktree-entries is _not_ specified:
795 # out-of-cone, not-on-disk files are removed from the index
796 test_sparse_match git update-index --remove folder1/a &&
797 cat >expect <<-EOF &&
800 test_sparse_match git diff --cached --name-status &&
801 test_cmp expect sparse-checkout-out &&
804 test_all_match git reset --hard &&
806 # When --ignore-skip-worktree-entries is specified, out-of-cone
807 # (skip-worktree) files are ignored
808 test_sparse_match git update-index --remove --ignore-skip-worktree-entries folder1/a &&
809 test_sparse_match git diff --cached --name-status &&
810 test_must_be_empty sparse-checkout-out &&
813 test_all_match git reset --hard &&
815 # --force-remove supercedes --ignore-skip-worktree-entries, removing
816 # a skip-worktree file from the index (and disk) when both are specified
818 test_sparse_match git update-index --force-remove --ignore-skip-worktree-entries folder1/a &&
819 cat >expect <<-EOF &&
822 test_sparse_match git diff --cached --name-status &&
823 test_cmp expect sparse-checkout-out
826 test_expect_success
'update-index with directories' '
829 # update-index will exit silently when provided with a directory name
830 # containing a trailing slash
831 test_all_match git update-index deep/ folder1/ &&
832 grep "Ignoring path deep/" sparse-checkout-err &&
833 grep "Ignoring path folder1/" sparse-checkout-err &&
835 # When update-index is given a directory name WITHOUT a trailing slash, it will
836 # behave in different ways depending on the status of the directory on disk:
837 # * if it exists, the command exits with an error ("add individual files instead")
838 # * if it does NOT exist (e.g., in a sparse-checkout), it is assumed to be a
839 # file and either triggers an error ("does not exist and --remove not passed")
840 # or is ignored completely (when using --remove)
841 test_all_match test_must_fail git update-index deep &&
842 run_on_all test_must_fail git update-index folder1 &&
843 test_must_fail git -C full-checkout update-index --remove folder1 &&
844 test_sparse_match git update-index --remove folder1 &&
845 test_all_match git status --porcelain=v2
848 test_expect_success
'update-index --again file outside sparse definition' '
851 test_all_match git checkout -b test-reupdate &&
853 # Update HEAD without modifying the index to introduce a difference in
855 test_sparse_match git reset --soft update-folder1 &&
857 # Because folder1/a differs in the index vs HEAD,
858 # `git update-index --no-skip-worktree --again` will effectively perform
859 # `git update-index --no-skip-worktree folder1/a` and remove the skip-worktree
860 # flag from folder1/a
861 test_sparse_match git update-index --no-skip-worktree --again &&
862 test_sparse_match git status --porcelain=v2 &&
864 cat >expect <<-EOF &&
867 test_sparse_match git diff --name-status &&
868 test_cmp expect sparse-checkout-out
871 test_expect_success
'update-index --cacheinfo' '
874 deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
875 folder2_oid=$(git -C full-checkout rev-parse update-folder2:folder2) &&
876 folder1_a_oid=$(git -C full-checkout rev-parse update-folder1:folder1/a) &&
878 test_all_match git update-index --cacheinfo 100644 $deep_a_oid deep/a &&
879 test_all_match git status --porcelain=v2 &&
881 # Cannot add sparse directory, even in sparse index case
882 test_all_match test_must_fail git update-index --add --cacheinfo 040000 $folder2_oid folder2/ &&
884 # Sparse match only: the new outside-of-cone entry is added *without* skip-worktree,
885 # so `git status` reports it as "deleted" in the worktree
886 test_sparse_match git update-index --add --cacheinfo 100644 $folder1_a_oid folder1/a &&
887 test_sparse_match git status --porcelain=v2 &&
888 cat >expect <<-EOF &&
891 test_sparse_match git status --short -- folder1/a &&
892 test_cmp expect sparse-checkout-out &&
894 # To return folder1/a to "normal" for a sparse checkout (ignored &
895 # outside-of-cone), add the skip-worktree flag.
896 test_sparse_match git update-index --skip-worktree folder1/a &&
897 cat >expect <<-EOF &&
900 test_sparse_match git ls-files -t -- folder1/a &&
901 test_cmp expect sparse-checkout-out
904 for MERGE_TREES
in "base HEAD update-folder2" \
905 "update-folder1 update-folder2" \
908 test_expect_success
"'read-tree -mu $MERGE_TREES' with files outside sparse definition" '
911 # Although the index matches, without --no-sparse-checkout, outside-of-
912 # definition files will not exist on disk for sparse checkouts
913 test_all_match git read-tree -mu $MERGE_TREES &&
914 test_all_match git status --porcelain=v2 &&
915 test_path_is_missing sparse-checkout/folder2 &&
916 test_path_is_missing sparse-index/folder2 &&
918 test_all_match git read-tree --reset -u HEAD &&
919 test_all_match git status --porcelain=v2 &&
921 test_all_match git read-tree -mu --no-sparse-checkout $MERGE_TREES &&
922 test_all_match git status --porcelain=v2 &&
923 test_cmp sparse-checkout/folder2/a sparse-index/folder2/a &&
924 test_cmp sparse-checkout/folder2/a full-checkout/folder2/a
929 test_expect_success
'read-tree --merge with edit/edit conflicts in sparse directories' '
932 # Merge of multiple changes to same directory (but not same files) should
934 test_all_match git read-tree -mu base rename-base update-folder1 &&
935 test_all_match git status --porcelain=v2 &&
937 test_all_match git reset --hard &&
939 test_all_match git read-tree -mu rename-base update-folder2 &&
940 test_all_match git status --porcelain=v2 &&
942 test_all_match git reset --hard &&
944 test_all_match test_must_fail git read-tree -mu base update-folder1 rename-out-to-in &&
945 test_all_match test_must_fail git read-tree -mu rename-out-to-in update-folder1
948 test_expect_success
'read-tree --prefix' '
951 # If files differing between the index and target <commit-ish> exist
952 # inside the prefix, `read-tree --prefix` should fail
953 test_all_match test_must_fail git read-tree --prefix=deep/ deepest &&
954 test_all_match test_must_fail git read-tree --prefix=folder1/ update-folder1 &&
956 # If no differing index entries exist matching the prefix,
957 # `read-tree --prefix` updates the index successfully
958 test_all_match git rm -rf deep/deeper1/deepest/ &&
959 test_all_match git read-tree --prefix=deep/deeper1/deepest -u deepest &&
960 test_all_match git status --porcelain=v2 &&
962 run_on_all git rm -rf --sparse folder1/ &&
963 test_all_match git read-tree --prefix=folder1/ -u update-folder1 &&
964 test_all_match git status --porcelain=v2 &&
966 test_all_match git rm -rf --sparse folder2/0 &&
967 test_all_match git read-tree --prefix=folder2/0/ -u rename-out-to-out &&
968 test_all_match git status --porcelain=v2
971 test_expect_success
'read-tree --merge with directory-file conflicts' '
974 test_all_match git checkout -b test-branch rename-base &&
976 # Although the index matches, without --no-sparse-checkout, outside-of-
977 # definition files will not exist on disk for sparse checkouts
978 test_sparse_match git read-tree -mu rename-out-to-out &&
979 test_sparse_match git status --porcelain=v2 &&
980 test_path_is_missing sparse-checkout/folder2 &&
981 test_path_is_missing sparse-index/folder2 &&
983 test_sparse_match git read-tree --reset -u HEAD &&
984 test_sparse_match git status --porcelain=v2 &&
986 test_sparse_match git read-tree -mu --no-sparse-checkout rename-out-to-out &&
987 test_sparse_match git status --porcelain=v2 &&
988 test_cmp sparse-checkout/folder2/0/1 sparse-index/folder2/0/1
991 test_expect_success
'merge, cherry-pick, and rebase' '
994 for OPERATION in "merge -m merge" cherry-pick "rebase --apply" "rebase --merge"
996 test_all_match git checkout -B temp update-deep &&
997 test_all_match git $OPERATION update-folder1 &&
998 test_all_match git rev-parse HEAD^{tree} &&
999 test_all_match git $OPERATION update-folder2 &&
1000 test_all_match git rev-parse HEAD^{tree} || return 1
1004 test_expect_success
'merge with conflict outside cone' '
1007 test_all_match git checkout -b merge-tip merge-left &&
1008 test_all_match git status --porcelain=v2 &&
1009 test_all_match test_must_fail git merge -m merge merge-right &&
1010 test_all_match git status --porcelain=v2 &&
1012 # Resolve the conflict in different ways:
1013 # 1. Revert to the base
1014 test_all_match git checkout base -- deep/deeper2/a &&
1015 test_all_match git status --porcelain=v2 &&
1017 # 2. Add the file with conflict markers
1018 test_sparse_match test_must_fail git add folder1/a &&
1019 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1020 test_sparse_unstaged folder1/a &&
1021 test_all_match git add --sparse folder1/a &&
1022 test_all_match git status --porcelain=v2 &&
1024 # 3. Rename the file to another sparse filename and
1025 # accept conflict markers as resolved content.
1026 run_on_all mv folder2/a folder2/z &&
1027 test_sparse_match test_must_fail git add folder2 &&
1028 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1029 test_sparse_unstaged folder2/z &&
1030 test_all_match git add --sparse folder2 &&
1031 test_all_match git status --porcelain=v2 &&
1033 test_all_match git merge --continue &&
1034 test_all_match git status --porcelain=v2 &&
1035 test_all_match git rev-parse HEAD^{tree}
1038 test_expect_success
'cherry-pick/rebase with conflict outside cone' '
1041 for OPERATION in cherry-pick rebase
1043 test_all_match git checkout -B tip &&
1044 test_all_match git reset --hard merge-left &&
1045 test_all_match git status --porcelain=v2 &&
1046 test_all_match test_must_fail git $OPERATION merge-right &&
1047 test_all_match git status --porcelain=v2 &&
1049 # Resolve the conflict in different ways:
1050 # 1. Revert to the base
1051 test_all_match git checkout base -- deep/deeper2/a &&
1052 test_all_match git status --porcelain=v2 &&
1054 # 2. Add the file with conflict markers
1055 # NEEDSWORK: Even though the merge conflict removed the
1056 # SKIP_WORKTREE bit from the index entry for folder1/a, we should
1057 # warn that this is a problematic add.
1058 test_sparse_match test_must_fail git add folder1/a &&
1059 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1060 test_sparse_unstaged folder1/a &&
1061 test_all_match git add --sparse folder1/a &&
1062 test_all_match git status --porcelain=v2 &&
1064 # 3. Rename the file to another sparse filename and
1065 # accept conflict markers as resolved content.
1066 # NEEDSWORK: This mode now fails, because folder2/z is
1067 # outside of the sparse-checkout cone and does not match an
1068 # existing index entry with the SKIP_WORKTREE bit cleared.
1069 run_on_all mv folder2/a folder2/z &&
1070 test_sparse_match test_must_fail git add folder2 &&
1071 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1072 test_sparse_unstaged folder2/z &&
1073 test_all_match git add --sparse folder2 &&
1074 test_all_match git status --porcelain=v2 &&
1076 test_all_match git $OPERATION --continue &&
1077 test_all_match git status --porcelain=v2 &&
1078 test_all_match git rev-parse HEAD^{tree} || return 1
1082 test_expect_success
'merge with outside renames' '
1085 for type in out-to-out out-to-in in-to-out
1087 test_all_match git reset --hard &&
1088 test_all_match git checkout -f -b merge-$type update-deep &&
1089 test_all_match git merge -m "$type" rename-$type &&
1090 test_all_match git rev-parse HEAD^{tree} || return 1
1094 # Sparse-index fails to convert the index in the
1095 # final 'git cherry-pick' command.
1096 test_expect_success
'cherry-pick with conflicts' '
1099 write_script edit-conflict <<-\EOF &&
1103 test_all_match git checkout -b to-cherry-pick &&
1104 run_on_all ../edit-conflict ABC &&
1105 test_all_match git add conflict &&
1106 test_all_match git commit -m "conflict to pick" &&
1108 test_all_match git checkout -B base HEAD~1 &&
1109 run_on_all ../edit-conflict DEF &&
1110 test_all_match git add conflict &&
1111 test_all_match git commit -m "conflict in base" &&
1113 test_all_match test_must_fail git cherry-pick to-cherry-pick
1116 test_expect_success
'stash' '
1119 write_script edit-contents <<-\EOF &&
1123 # Stash a sparse directory (folder1)
1124 test_all_match git checkout -b test-branch rename-base &&
1125 test_all_match git reset --soft rename-out-to-out &&
1126 test_all_match git stash &&
1127 test_all_match git status --porcelain=v2 &&
1129 # Apply the sparse directory stash without reinstating the index
1130 test_all_match git stash apply -q &&
1131 test_all_match git status --porcelain=v2 &&
1133 # Reset to state where stash can be applied
1134 test_sparse_match git sparse-checkout reapply &&
1135 test_all_match git reset --hard rename-out-to-out &&
1137 # Apply the sparse directory stash *with* reinstating the index
1138 test_all_match git stash apply --index -q &&
1139 test_all_match git status --porcelain=v2 &&
1141 # Reset to state where we will get a conflict applying the stash
1142 test_sparse_match git sparse-checkout reapply &&
1143 test_all_match git reset --hard update-folder1 &&
1145 # Apply the sparse directory stash with conflicts
1146 test_all_match test_must_fail git stash apply --index -q &&
1147 test_all_match test_must_fail git stash apply -q &&
1148 test_all_match git status --porcelain=v2 &&
1150 # Reset to base branch
1151 test_sparse_match git sparse-checkout reapply &&
1152 test_all_match git reset --hard base &&
1154 # Stash & unstash an untracked file outside of the sparse checkout
1156 run_on_sparse mkdir -p folder1 &&
1157 run_on_all ../edit-contents folder1/new &&
1158 test_all_match git stash -u &&
1159 test_all_match git status --porcelain=v2 &&
1161 test_all_match git stash pop -q &&
1162 test_all_match git status --porcelain=v2
1165 test_expect_success
'checkout-index inside sparse definition' '
1168 run_on_all rm -f deep/a &&
1169 test_all_match git checkout-index -- deep/a &&
1170 test_all_match git status --porcelain=v2 &&
1172 echo test >>new-a &&
1173 run_on_all cp ../new-a a &&
1174 test_all_match test_must_fail git checkout-index -- a &&
1175 test_all_match git checkout-index -f -- a &&
1176 test_all_match git status --porcelain=v2
1179 test_expect_success
'checkout-index outside sparse definition' '
1182 # Without --ignore-skip-worktree-bits, outside-of-cone files will trigger
1184 test_sparse_match test_must_fail git checkout-index -- folder1/a &&
1185 test_i18ngrep "folder1/a has skip-worktree enabled" sparse-checkout-err &&
1186 test_path_is_missing folder1/a &&
1188 # With --ignore-skip-worktree-bits, outside-of-cone files are checked out
1189 test_sparse_match git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
1190 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
1191 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a &&
1193 run_on_sparse rm -rf folder1 &&
1195 run_on_sparse mkdir -p folder1 &&
1196 run_on_all cp ../new-a folder1/a &&
1198 test_all_match test_must_fail git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
1199 test_all_match git checkout-index -f --ignore-skip-worktree-bits -- folder1/a &&
1200 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
1201 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a
1204 test_expect_success
'checkout-index with folders' '
1207 # Inside checkout definition
1208 test_all_match test_must_fail git checkout-index -f -- deep/ &&
1210 # Outside checkout definition
1211 # Note: although all tests fail (as expected), the messaging differs. For
1212 # non-sparse index checkouts, the error is that the "file" does not appear
1213 # in the index; for sparse checkouts, the error is explicitly that the
1214 # entry is a sparse directory.
1215 run_on_all test_must_fail git checkout-index -f -- folder1/ &&
1216 test_cmp full-checkout-err sparse-checkout-err &&
1217 ! test_cmp full-checkout-err sparse-index-err &&
1218 grep "is a sparse directory" sparse-index-err
1221 test_expect_success
'checkout-index --all' '
1224 test_all_match git checkout-index --all &&
1225 test_sparse_match test_path_is_missing folder1 &&
1227 # --ignore-skip-worktree-bits will cause `skip-worktree` files to be
1228 # checked out, causing the outside-of-cone `folder1` to exist on-disk
1229 test_all_match git checkout-index --ignore-skip-worktree-bits --all &&
1230 test_all_match test_path_exists folder1
1233 test_expect_success
'clean' '
1236 echo bogus >>.gitignore &&
1237 run_on_all cp ../.gitignore . &&
1238 test_all_match git add .gitignore &&
1239 test_all_match git commit -m "ignore bogus files" &&
1241 run_on_sparse mkdir folder1 &&
1242 run_on_all mkdir -p deep/untracked-deep &&
1243 run_on_all touch folder1/bogus &&
1244 run_on_all touch folder1/untracked &&
1245 run_on_all touch deep/untracked-deep/bogus &&
1246 run_on_all touch deep/untracked-deep/untracked &&
1248 test_all_match git status --porcelain=v2 &&
1249 test_all_match git clean -f &&
1250 test_all_match git status --porcelain=v2 &&
1251 test_sparse_match ls &&
1252 test_sparse_match ls folder1 &&
1253 run_on_all test_path_exists folder1/bogus &&
1254 run_on_all test_path_is_missing folder1/untracked &&
1255 run_on_all test_path_exists deep/untracked-deep/bogus &&
1256 run_on_all test_path_exists deep/untracked-deep/untracked &&
1258 test_all_match git clean -fd &&
1259 test_all_match git status --porcelain=v2 &&
1260 test_sparse_match ls &&
1261 test_sparse_match ls folder1 &&
1262 run_on_all test_path_exists folder1/bogus &&
1263 run_on_all test_path_exists deep/untracked-deep/bogus &&
1264 run_on_all test_path_is_missing deep/untracked-deep/untracked &&
1266 test_all_match git clean -xf &&
1267 test_all_match git status --porcelain=v2 &&
1268 test_sparse_match ls &&
1269 test_sparse_match ls folder1 &&
1270 run_on_all test_path_is_missing folder1/bogus &&
1271 run_on_all test_path_exists deep/untracked-deep/bogus &&
1273 test_all_match git clean -xdf &&
1274 test_all_match git status --porcelain=v2 &&
1275 test_sparse_match ls &&
1276 test_sparse_match ls folder1 &&
1277 run_on_all test_path_is_missing deep/untracked-deep/bogus &&
1279 test_sparse_match test_path_is_dir folder1
1282 for builtin in show rev-parse
1284 test_expect_success
"$builtin (cached blobs/trees)" "
1287 test_all_match git $builtin :a &&
1288 test_all_match git $builtin :deep/a &&
1289 test_sparse_match git $builtin :folder1/a &&
1291 # The error message differs depending on whether
1292 # the directory exists in the worktree.
1293 test_all_match test_must_fail git $builtin :deep/ &&
1294 test_must_fail git -C full-checkout $builtin :folder1/ &&
1295 test_sparse_match test_must_fail git $builtin :folder1/ &&
1297 # Change the sparse cone for an extra case:
1298 run_on_sparse git sparse-checkout set deep/deeper1 &&
1300 # deep/deeper2 is a sparse directory in the sparse index.
1301 test_sparse_match test_must_fail git $builtin :deep/deeper2/ &&
1303 # deep/deeper2/deepest is not in the sparse index, but
1304 # will trigger an index expansion.
1305 test_sparse_match test_must_fail git $builtin :deep/deeper2/deepest/
1309 test_expect_success
'submodule handling' '
1312 test_sparse_match git sparse-checkout add modules &&
1313 test_all_match mkdir modules &&
1314 test_all_match touch modules/a &&
1315 test_all_match git add modules &&
1316 test_all_match git commit -m "add modules directory" &&
1318 test_config_global protocol.file.allow always &&
1320 run_on_all git submodule add "$(pwd)/initial-repo" modules/sub &&
1321 test_all_match git commit -m "add submodule" &&
1323 # having a submodule prevents "modules" from collapse
1324 test_sparse_match git sparse-checkout set deep/deeper1 &&
1325 git -C sparse-index ls-files --sparse --stage >cache &&
1326 grep "100644 .* modules/a" cache &&
1327 grep "160000 $(git -C initial-repo rev-parse HEAD) 0 modules/sub" cache
1330 # When working with a sparse index, some commands will need to expand the
1331 # index to operate properly. If those commands also write the index back
1332 # to disk, they need to convert the index to sparse before writing.
1333 # This test verifies that both of these events are logged in trace2 logs.
1334 test_expect_success
'sparse-index is expanded and converted back' '
1337 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1338 git -C sparse-index reset -- folder1/a &&
1339 test_region index convert_to_sparse trace2.txt &&
1340 test_region index ensure_full_index trace2.txt &&
1342 # ls-files expands on read, but does not write.
1344 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1345 git -C sparse-index ls-files &&
1346 test_region index ensure_full_index trace2.txt
1349 test_expect_success
'index.sparse disabled inline uses full index' '
1352 # When index.sparse is disabled inline with `git status`, the
1353 # index is expanded at the beginning of the execution then never
1354 # converted back to sparse. It is then written to disk as a full index.
1356 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1357 git -C sparse-index -c index.sparse=false status &&
1358 ! test_region index convert_to_sparse trace2.txt &&
1359 test_region index ensure_full_index trace2.txt &&
1361 # Since index.sparse is set to true at a repo level, the index
1362 # is converted from full to sparse when read, then never expanded
1363 # over the course of `git status`. It is written to disk as a sparse
1366 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1367 git -C sparse-index status &&
1368 test_region index convert_to_sparse trace2.txt &&
1369 ! test_region index ensure_full_index trace2.txt &&
1371 # Now that the index has been written to disk as sparse, it is not
1372 # converted to sparse (or expanded to full) when read by `git status`.
1374 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1375 git -C sparse-index status &&
1376 ! test_region index convert_to_sparse trace2.txt &&
1377 ! test_region index ensure_full_index trace2.txt
1380 run_sparse_index_trace2
() {
1382 if test -z "$WITHOUT_UNTRACKED_TXT"
1384 echo >>sparse-index
/untracked.txt
1390 test_must_fail env \
1391 GIT_TRACE2_EVENT
="$(pwd)/trace2.txt" \
1392 git
-C sparse-index
"$@" \
1394 2>sparse-index-error ||
return 1
1396 GIT_TRACE2_EVENT
="$(pwd)/trace2.txt" \
1397 git
-C sparse-index
"$@" \
1399 2>sparse-index-error ||
return 1
1403 ensure_expanded
() {
1404 run_sparse_index_trace2
"$@" &&
1405 test_region index ensure_full_index trace2.txt
1408 ensure_not_expanded
() {
1409 run_sparse_index_trace2
"$@" &&
1410 test_region
! index ensure_full_index trace2.txt
1413 test_expect_success
'sparse-index is not expanded' '
1416 ensure_not_expanded status &&
1417 ensure_not_expanded ls-files --sparse &&
1418 ensure_not_expanded commit --allow-empty -m empty &&
1419 echo >>sparse-index/a &&
1420 ensure_not_expanded commit -a -m a &&
1421 echo >>sparse-index/a &&
1422 ensure_not_expanded commit --include a -m a &&
1423 echo >>sparse-index/deep/deeper1/a &&
1424 ensure_not_expanded commit --include deep/deeper1/a -m deeper &&
1425 ensure_not_expanded checkout rename-out-to-out &&
1426 ensure_not_expanded checkout - &&
1427 ensure_not_expanded switch rename-out-to-out &&
1428 ensure_not_expanded switch - &&
1429 ensure_not_expanded reset --hard &&
1430 ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
1431 ensure_not_expanded reset --hard &&
1432 ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
1434 echo >>sparse-index/README.md &&
1435 ensure_not_expanded add -A &&
1436 echo >>sparse-index/extra.txt &&
1437 ensure_not_expanded add extra.txt &&
1438 echo >>sparse-index/untracked.txt &&
1439 ensure_not_expanded add . &&
1441 ensure_not_expanded checkout-index -f a &&
1442 ensure_not_expanded checkout-index -f --all &&
1443 for ref in update-deep update-folder1 update-folder2 update-deep
1445 echo >>sparse-index/README.md &&
1446 ensure_not_expanded reset --hard $ref || return 1
1449 ensure_not_expanded reset --mixed base &&
1450 ensure_not_expanded reset --hard update-deep &&
1451 ensure_not_expanded reset --keep base &&
1452 ensure_not_expanded reset --merge update-deep &&
1453 ensure_not_expanded reset --hard &&
1455 ensure_not_expanded reset base -- deep/a &&
1456 ensure_not_expanded reset base -- nonexistent-file &&
1457 ensure_not_expanded reset deepest -- deep &&
1459 # Although folder1 is outside the sparse definition, it exists as a
1460 # directory entry in the index, so the pathspec will not force the
1461 # index to be expanded.
1462 ensure_not_expanded reset deepest -- folder1 &&
1463 ensure_not_expanded reset deepest -- folder1/ &&
1465 # Wildcard identifies only in-cone files, no index expansion
1466 ensure_not_expanded reset deepest -- deep/\* &&
1468 # Wildcard identifies only full sparse directories, no index expansion
1469 ensure_not_expanded reset deepest -- folder\* &&
1471 ensure_not_expanded clean -fd &&
1473 ensure_not_expanded checkout -f update-deep &&
1474 test_config -C sparse-index pull.twohead ort &&
1476 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1477 for OPERATION in "merge -m merge" cherry-pick rebase
1479 ensure_not_expanded merge -m merge update-folder1 &&
1480 ensure_not_expanded merge -m merge update-folder2 || return 1
1485 test_expect_success
'sparse-index is not expanded: merge conflict in cone' '
1488 for side in right left
1490 git -C sparse-index checkout -b expand-$side base &&
1491 echo $side >sparse-index/deep/a &&
1492 git -C sparse-index commit -a -m "$side" || return 1
1496 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1497 git -C sparse-index config pull.twohead ort &&
1498 ensure_not_expanded ! merge -m merged expand-right
1502 test_expect_success
'sparse-index is not expanded: stash' '
1505 echo >>sparse-index/a &&
1506 ensure_not_expanded stash &&
1507 ensure_not_expanded stash list &&
1508 ensure_not_expanded stash show stash@{0} &&
1509 ensure_not_expanded stash apply stash@{0} &&
1510 ensure_not_expanded stash drop stash@{0} &&
1512 echo >>sparse-index/deep/new &&
1513 ensure_not_expanded stash -u &&
1515 WITHOUT_UNTRACKED_TXT=1 &&
1516 ensure_not_expanded stash pop
1519 ensure_not_expanded stash create &&
1520 oid=$(git -C sparse-index stash create) &&
1521 ensure_not_expanded stash store -m "test" $oid &&
1522 ensure_not_expanded reset --hard &&
1523 ensure_not_expanded stash pop
1526 test_expect_success
'describe tested on all' '
1529 # Add tag to be read by describe
1531 run_on_all git tag -a v1.0 -m "Version 1" &&
1532 test_all_match git describe --dirty &&
1534 test_all_match git describe --dirty
1538 test_expect_success
'sparse-index is not expanded: describe' '
1541 # Add tag to be read by describe
1543 git -C sparse-index tag -a v1.0 -m "Version 1" &&
1545 ensure_not_expanded describe --dirty &&
1546 echo "test" >>sparse-index/g &&
1547 ensure_not_expanded describe --dirty &&
1548 ensure_not_expanded describe
1551 test_expect_success
'sparse index is not expanded: diff' '
1554 write_script edit-contents <<-\EOF &&
1558 # Add file within cone
1559 test_sparse_match git sparse-checkout set deep &&
1560 run_on_all ../edit-contents deep/testfile &&
1561 test_all_match git add deep/testfile &&
1562 run_on_all ../edit-contents deep/testfile &&
1564 test_all_match git diff &&
1565 test_all_match git diff --cached &&
1566 ensure_not_expanded diff &&
1567 ensure_not_expanded diff --cached &&
1569 # Add file outside cone
1570 test_all_match git reset --hard &&
1571 run_on_all mkdir newdirectory &&
1572 run_on_all ../edit-contents newdirectory/testfile &&
1573 test_sparse_match git sparse-checkout set newdirectory &&
1574 test_all_match git add newdirectory/testfile &&
1575 run_on_all ../edit-contents newdirectory/testfile &&
1576 test_sparse_match git sparse-checkout set &&
1578 test_all_match git diff &&
1579 test_all_match git diff --cached &&
1580 ensure_not_expanded diff &&
1581 ensure_not_expanded diff --cached &&
1583 # Merge conflict outside cone
1584 # The sparse checkout will report a warning that is not in the
1585 # full checkout, so we use `run_on_all` instead of
1587 run_on_all git reset --hard &&
1588 test_all_match git checkout merge-left &&
1589 test_all_match test_must_fail git merge merge-right &&
1591 test_all_match git diff &&
1592 test_all_match git diff --cached &&
1593 ensure_not_expanded diff &&
1594 ensure_not_expanded diff --cached
1597 test_expect_success
'sparse index is not expanded: show and rev-parse' '
1600 ensure_not_expanded show :a &&
1601 ensure_not_expanded show :deep/a &&
1602 ensure_not_expanded rev-parse :a &&
1603 ensure_not_expanded rev-parse :deep/a
1606 test_expect_success
'sparse index is not expanded: update-index' '
1609 deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
1610 ensure_not_expanded update-index --cacheinfo 100644 $deep_a_oid deep/a &&
1612 echo "test" >sparse-index/README.md &&
1613 echo "test2" >sparse-index/a &&
1614 rm -f sparse-index/deep/a &&
1616 ensure_not_expanded update-index --add README.md &&
1617 ensure_not_expanded update-index a &&
1618 ensure_not_expanded update-index --remove deep/a &&
1620 ensure_not_expanded reset --soft update-deep &&
1621 ensure_not_expanded update-index --add --remove --again
1624 test_expect_success
'sparse index is not expanded: blame' '
1630 deep/deeper1/deepest/a
1632 ensure_not_expanded blame $file || return 1
1636 test_expect_success
'sparse index is not expanded: fetch/pull' '
1639 git -C sparse-index remote add full "file://$(pwd)/full-checkout" &&
1640 ensure_not_expanded fetch full &&
1641 git -C full-checkout commit --allow-empty -m "for pull merge" &&
1642 git -C sparse-index commit --allow-empty -m "for pull merge" &&
1643 ensure_not_expanded pull full base
1646 test_expect_success
'sparse index is not expanded: read-tree' '
1649 ensure_not_expanded checkout -b test-branch update-folder1 &&
1650 for MERGE_TREES in "base HEAD update-folder2" \
1651 "base HEAD rename-base" \
1652 "base update-folder2" \
1653 "base rename-base" \
1656 ensure_not_expanded read-tree -mu $MERGE_TREES &&
1657 ensure_not_expanded reset --hard || return 1
1660 rm -rf sparse-index/deep/deeper2 &&
1661 ensure_not_expanded add . &&
1662 ensure_not_expanded commit -m "test" &&
1664 ensure_not_expanded read-tree --prefix=deep/deeper2 -u deepest
1667 test_expect_success
'ls-files' '
1670 # Use a smaller sparse-checkout for reduced output
1671 test_sparse_match git sparse-checkout set &&
1673 # Behavior agrees by default. Sparse index is expanded.
1674 test_all_match git ls-files &&
1676 # With --sparse, the sparse index data changes behavior.
1677 git -C sparse-index ls-files --sparse >actual &&
1679 cat >expect <<-\EOF &&
1694 test_cmp expect actual &&
1696 # With --sparse and no sparse index, nothing changes.
1697 git -C sparse-checkout ls-files >dense &&
1698 git -C sparse-checkout ls-files --sparse >sparse &&
1699 test_cmp dense sparse &&
1701 # Set up a strange condition of having a file edit
1702 # outside of the sparse-checkout cone. We want to verify
1703 # that all modes handle this the same, and detect the
1705 write_script edit-content <<-\EOF &&
1707 echo content >>folder1/a
1709 run_on_all ../edit-content &&
1711 test_all_match git ls-files --modified &&
1713 git -C sparse-index ls-files --sparse --modified >sparse-index-out &&
1714 cat >expect <<-\EOF &&
1717 test_cmp expect sparse-index-out &&
1719 # Add folder1 to the sparse-checkout cone and
1720 # check that ls-files shows the expanded files.
1721 test_sparse_match git sparse-checkout add folder1 &&
1722 test_all_match git ls-files --modified &&
1724 test_all_match git ls-files &&
1725 git -C sparse-index ls-files --sparse >actual &&
1727 cat >expect <<-\EOF &&
1744 test_cmp expect actual &&
1746 # Double-check index expansion is avoided
1747 ensure_not_expanded ls-files --sparse
1750 test_expect_success
'sparse index is not expanded: sparse-checkout' '
1753 ensure_not_expanded sparse-checkout set deep/deeper2 &&
1754 ensure_not_expanded sparse-checkout set deep/deeper1 &&
1755 ensure_not_expanded sparse-checkout set deep &&
1756 ensure_not_expanded sparse-checkout add folder1 &&
1757 ensure_not_expanded sparse-checkout set deep/deeper1 &&
1758 ensure_not_expanded sparse-checkout set folder2 &&
1760 # Demonstrate that the checks that "folder1/a" is a file
1761 # do not cause a sparse-index expansion (since it is in the
1762 # sparse-checkout cone).
1763 echo >>sparse-index/folder2/a &&
1764 git -C sparse-index add folder2/a &&
1766 ensure_not_expanded sparse-checkout add folder1 &&
1768 # Skip checks here, since deep/deeper1 is inside a sparse directory
1769 # that must be expanded to check whether `deep/deeper1` is a file
1771 ensure_not_expanded sparse-checkout set --skip-checks deep/deeper1 &&
1772 ensure_not_expanded sparse-checkout set
1775 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
1776 # in this scenario, but it shouldn't.
1777 test_expect_success
'reset mixed and checkout orphan' '
1780 test_all_match git checkout rename-out-to-in &&
1782 # Sparse checkouts do not agree with full checkouts about
1783 # how to report a directory/file conflict during a reset.
1784 # This command would fail with test_all_match because the
1785 # full checkout reports "T folder1/0/1" while a sparse
1786 # checkout reports "D folder1/0/1". This matches because
1787 # the sparse checkouts skip "adding" the other side of
1789 test_sparse_match git reset --mixed HEAD~1 &&
1790 test_sparse_match git ls-files --stage &&
1791 test_sparse_match git status --porcelain=v2 &&
1793 # At this point, sparse-checkouts behave differently
1794 # from the full-checkout.
1795 test_sparse_match git checkout --orphan new-branch &&
1796 test_sparse_match git ls-files --stage &&
1797 test_sparse_match git status --porcelain=v2
1800 test_expect_success
'add everything with deep new file' '
1803 run_on_sparse git sparse-checkout set deep/deeper1/deepest &&
1805 run_on_all touch deep/deeper1/x &&
1806 test_all_match git add . &&
1807 test_all_match git status --porcelain=v2
1810 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1811 # directory/file conflicts, even without sparse-checkout. Use this
1812 # test only as a documentation of the incorrect behavior, not a
1813 # measure of how it _should_ behave.
1814 test_expect_success
'checkout behaves oddly with df-conflict-1' '
1817 test_sparse_match git sparse-checkout disable &&
1819 write_script edit-content <<-\EOF &&
1820 echo content >>folder1/larger-content
1824 run_on_all ../edit-content &&
1825 test_all_match git status --porcelain=v2 &&
1827 git -C sparse-checkout sparse-checkout init --cone &&
1828 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1830 test_all_match git status --porcelain=v2 &&
1832 # This checkout command should fail, because we have a staged
1833 # change to folder1/larger-content, but the destination changes
1834 # folder1 to a file.
1835 git -C full-checkout checkout df-conflict-1 \
1836 1>full-checkout-out \
1837 2>full-checkout-err &&
1838 git -C sparse-checkout checkout df-conflict-1 \
1839 1>sparse-checkout-out \
1840 2>sparse-checkout-err &&
1841 git -C sparse-index checkout df-conflict-1 \
1842 1>sparse-index-out \
1843 2>sparse-index-err &&
1845 # Instead, the checkout deletes the folder1 file and adds the
1846 # folder1/larger-content file, leaving all other paths that were
1847 # in folder1/ as deleted (without any warning).
1848 cat >expect <<-EOF &&
1850 A folder1/larger-content
1852 test_cmp expect full-checkout-out &&
1853 test_cmp expect sparse-checkout-out &&
1855 # The sparse-index reports no output
1856 test_must_be_empty sparse-index-out &&
1858 # stderr: Switched to branch df-conflict-1
1859 test_cmp full-checkout-err sparse-checkout-err &&
1860 test_cmp full-checkout-err sparse-checkout-err
1863 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1864 # directory/file conflicts, even without sparse-checkout. Use this
1865 # test only as a documentation of the incorrect behavior, not a
1866 # measure of how it _should_ behave.
1867 test_expect_success
'checkout behaves oddly with df-conflict-2' '
1870 test_sparse_match git sparse-checkout disable &&
1872 write_script edit-content <<-\EOF &&
1873 echo content >>folder2/larger-content
1877 run_on_all ../edit-content &&
1878 test_all_match git status --porcelain=v2 &&
1880 git -C sparse-checkout sparse-checkout init --cone &&
1881 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1883 test_all_match git status --porcelain=v2 &&
1885 # This checkout command should fail, because we have a staged
1886 # change to folder1/larger-content, but the destination changes
1887 # folder1 to a file.
1888 git -C full-checkout checkout df-conflict-2 \
1889 1>full-checkout-out \
1890 2>full-checkout-err &&
1891 git -C sparse-checkout checkout df-conflict-2 \
1892 1>sparse-checkout-out \
1893 2>sparse-checkout-err &&
1894 git -C sparse-index checkout df-conflict-2 \
1895 1>sparse-index-out \
1896 2>sparse-index-err &&
1898 # The full checkout deviates from the df-conflict-1 case here!
1899 # It drops the change to folder1/larger-content and leaves the
1900 # folder1 path as-is on disk. The sparse-index behaves the same.
1901 test_must_be_empty full-checkout-out &&
1902 test_must_be_empty sparse-index-out &&
1904 # In the sparse-checkout case, the checkout deletes the folder1
1905 # file and adds the folder1/larger-content file, leaving all other
1906 # paths that were in folder1/ as deleted (without any warning).
1907 cat >expect <<-EOF &&
1909 A folder2/larger-content
1911 test_cmp expect sparse-checkout-out &&
1913 # Switched to branch df-conflict-1
1914 test_cmp full-checkout-err sparse-checkout-err &&
1915 test_cmp full-checkout-err sparse-index-err
1918 test_expect_success
'mv directory from out-of-cone to in-cone' '
1921 # <source> as a sparse directory (or SKIP_WORKTREE_DIR without enabling
1923 test_all_match git mv --sparse folder1 deep &&
1924 test_all_match git status --porcelain=v2 &&
1925 test_sparse_match git ls-files -t &&
1926 git -C sparse-checkout ls-files -t >actual &&
1927 grep -e "H deep/folder1/0/0/0" actual &&
1928 grep -e "H deep/folder1/0/1" actual &&
1929 grep -e "H deep/folder1/a" actual &&
1931 test_all_match git reset --hard &&
1933 # <source> as a directory deeper than sparse index boundary (where
1934 # sparse index will expand).
1935 test_sparse_match git mv --sparse folder1/0 deep &&
1936 test_sparse_match git status --porcelain=v2 &&
1937 test_sparse_match git ls-files -t &&
1938 git -C sparse-checkout ls-files -t >actual &&
1939 grep -e "H deep/0/0/0" actual &&
1940 grep -e "H deep/0/1" actual
1943 test_expect_success
'rm pathspec inside sparse definition' '
1946 test_all_match git rm deep/a &&
1947 test_all_match git status --porcelain=v2 &&
1950 run_on_all git reset --hard &&
1951 test_all_match git rm deep/* &&
1952 test_all_match git status --porcelain=v2 &&
1955 run_on_all git reset --hard &&
1956 test_all_match git rm -r deep &&
1957 test_all_match git status --porcelain=v2
1960 test_expect_success
'rm pathspec outside sparse definition' '
1963 for file in folder1/a folder1/0/1
1965 test_sparse_match test_must_fail git rm $file &&
1966 test_sparse_match test_must_fail git rm --cached $file &&
1967 test_sparse_match git rm --sparse $file &&
1968 test_sparse_match git status --porcelain=v2 || return 1
1971 cat >folder1-full <<-EOF &&
1972 rm ${SQ}folder1/0/0/0${SQ}
1973 rm ${SQ}folder1/0/1${SQ}
1974 rm ${SQ}folder1/a${SQ}
1977 cat >folder1-sparse <<-EOF &&
1978 rm ${SQ}folder1/${SQ}
1982 run_on_sparse git reset --hard &&
1983 run_on_sparse git sparse-checkout reapply &&
1984 test_sparse_match test_must_fail git rm folder1/* &&
1985 run_on_sparse git rm --sparse folder1/* &&
1986 test_cmp folder1-full sparse-checkout-out &&
1987 test_cmp folder1-sparse sparse-index-out &&
1988 test_sparse_match git status --porcelain=v2 &&
1991 run_on_sparse git reset --hard &&
1992 run_on_sparse git sparse-checkout reapply &&
1993 test_sparse_match test_must_fail git rm --sparse folder1 &&
1994 run_on_sparse git rm --sparse -r folder1 &&
1995 test_cmp folder1-full sparse-checkout-out &&
1996 test_cmp folder1-sparse sparse-index-out &&
1997 test_sparse_match git status --porcelain=v2
2000 test_expect_success
'rm pathspec expands index when necessary' '
2003 # in-cone pathspec (do not expand)
2004 ensure_not_expanded rm "deep/deep*" &&
2005 test_must_be_empty sparse-index-err &&
2007 # out-of-cone pathspec (expand)
2008 ! ensure_not_expanded rm --sparse "folder1/a*" &&
2009 test_must_be_empty sparse-index-err &&
2011 # pathspec that should expand index
2012 ! ensure_not_expanded rm "*/a" &&
2013 test_must_be_empty sparse-index-err &&
2015 ! ensure_not_expanded rm "**a" &&
2016 test_must_be_empty sparse-index-err
2019 test_expect_success
'sparse index is not expanded: rm' '
2022 ensure_not_expanded rm deep/a &&
2024 # test in-cone wildcard
2025 git -C sparse-index reset --hard &&
2026 ensure_not_expanded rm deep/* &&
2029 git -C sparse-index reset --hard &&
2030 ensure_not_expanded rm -r deep
2033 test_expect_success
'grep with and --cached' '
2036 test_all_match git grep --cached a &&
2037 test_all_match git grep --cached a -- "folder1/*"
2040 test_expect_success
'grep is not expanded' '
2043 ensure_not_expanded grep a &&
2044 ensure_not_expanded grep a -- deep/* &&
2046 # All files within the folder1/* pathspec are sparse,
2047 # so this command does not find any matches
2048 ensure_not_expanded ! grep a -- folder1/* &&
2050 # test out-of-cone pathspec with or without wildcard
2051 ensure_not_expanded grep --cached a -- "folder1/a" &&
2052 ensure_not_expanded grep --cached a -- "folder1/*" &&
2054 # test in-cone pathspec with or without wildcard
2055 ensure_not_expanded grep --cached a -- "deep/a" &&
2056 ensure_not_expanded grep --cached a -- "deep/*"
2059 # NEEDSWORK: when running `grep` in the superproject with --recurse-submodules,
2060 # Git expands the index of the submodules unexpectedly. Even though `grep`
2061 # builtin is marked as "command_requires_full_index = 0", this config is only
2062 # useful for the superproject. Namely, the submodules have their own configs,
2063 # which are _not_ populated by the one-time sparse-index feature switch.
2064 test_expect_failure
'grep within submodules is not expanded' '
2065 init_repos_as_submodules &&
2067 # do not use ensure_not_expanded() here, becasue `grep` should be
2068 # run in the superproject, not in "./sparse-index"
2069 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
2070 git grep --cached --recurse-submodules a -- "*/folder1/*" &&
2071 test_region ! index ensure_full_index trace2.txt
2074 # NEEDSWORK: this test is not actually testing the code. The design purpose
2075 # of this test is to verify the grep result when the submodules are using a
2076 # sparse-index. Namely, we want "folder1/" as a tree (a sparse directory); but
2077 # because of the index expansion, we are now grepping the "folder1/a" blob.
2078 # Because of the problem stated above 'grep within submodules is not expanded',
2079 # we don't have the ideal test environment yet.
2080 test_expect_success
'grep sparse directory within submodules' '
2081 init_repos_as_submodules &&
2083 cat >expect <<-\EOF &&
2084 full-checkout/folder1/a:a
2085 sparse-checkout/folder1/a:a
2086 sparse-index/folder1/a:a
2088 git grep --cached --recurse-submodules a -- "*/folder1/*" >actual &&
2089 test_cmp actual expect
2092 test_expect_success
'write-tree' '
2095 test_all_match git write-tree &&
2097 write_script edit-contents <<-\EOF &&
2101 # make a change inside the sparse cone
2102 run_on_all ../edit-contents deep/a &&
2103 test_all_match git update-index deep/a &&
2104 test_all_match git write-tree &&
2105 test_all_match git status --porcelain=v2 &&
2107 # make a change outside the sparse cone
2108 run_on_all mkdir -p folder1 &&
2109 run_on_all cp a folder1/a &&
2110 run_on_all ../edit-contents folder1/a &&
2111 test_all_match git update-index folder1/a &&
2112 test_all_match git write-tree &&
2113 test_all_match git status --porcelain=v2 &&
2115 # check that SKIP_WORKTREE files are not materialized
2116 test_path_is_missing sparse-checkout/folder2/a &&
2117 test_path_is_missing sparse-index/folder2/a
2120 test_expect_success
'sparse-index is not expanded: write-tree' '
2123 ensure_not_expanded write-tree &&
2125 echo "test1" >>sparse-index/a &&
2126 git -C sparse-index update-index a &&
2127 ensure_not_expanded write-tree
2130 test_expect_success
'diff-files with pathspec inside sparse definition' '
2133 write_script edit-contents <<-\EOF &&
2137 run_on_all ../edit-contents deep/a &&
2139 test_all_match git diff-files &&
2141 test_all_match git diff-files -- deep/a &&
2144 test_all_match git diff-files -- "deep/*"
2147 test_expect_success
'diff-files with pathspec outside sparse definition' '
2150 test_sparse_match git diff-files -- folder2/a &&
2152 write_script edit-contents <<-\EOF &&
2156 # The directory "folder1" is outside the cone of interest
2157 # and will not exist in the sparse checkout repositories.
2158 # Create it as needed, add file "folder1/a" there with
2159 # contents that is different from the staged version.
2160 run_on_all mkdir -p folder1 &&
2161 run_on_all cp a folder1/a &&
2163 run_on_all ../edit-contents folder1/a &&
2164 test_all_match git diff-files &&
2165 test_all_match git diff-files -- folder1/a &&
2166 test_all_match git diff-files -- "folder*/a"
2169 test_expect_success
'sparse index is not expanded: diff-files' '
2172 write_script edit-contents <<-\EOF &&
2176 run_on_all ../edit-contents deep/a &&
2178 ensure_not_expanded diff-files &&
2179 ensure_not_expanded diff-files -- deep/a &&
2180 ensure_not_expanded diff-files -- "deep/*"