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 ensure_not_expanded
() {
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
1401 test_region
! index ensure_full_index trace2.txt
1404 test_expect_success
'sparse-index is not expanded' '
1407 ensure_not_expanded status &&
1408 ensure_not_expanded ls-files --sparse &&
1409 ensure_not_expanded commit --allow-empty -m empty &&
1410 echo >>sparse-index/a &&
1411 ensure_not_expanded commit -a -m a &&
1412 echo >>sparse-index/a &&
1413 ensure_not_expanded commit --include a -m a &&
1414 echo >>sparse-index/deep/deeper1/a &&
1415 ensure_not_expanded commit --include deep/deeper1/a -m deeper &&
1416 ensure_not_expanded checkout rename-out-to-out &&
1417 ensure_not_expanded checkout - &&
1418 ensure_not_expanded switch rename-out-to-out &&
1419 ensure_not_expanded switch - &&
1420 ensure_not_expanded reset --hard &&
1421 ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
1422 ensure_not_expanded reset --hard &&
1423 ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
1425 echo >>sparse-index/README.md &&
1426 ensure_not_expanded add -A &&
1427 echo >>sparse-index/extra.txt &&
1428 ensure_not_expanded add extra.txt &&
1429 echo >>sparse-index/untracked.txt &&
1430 ensure_not_expanded add . &&
1432 ensure_not_expanded checkout-index -f a &&
1433 ensure_not_expanded checkout-index -f --all &&
1434 for ref in update-deep update-folder1 update-folder2 update-deep
1436 echo >>sparse-index/README.md &&
1437 ensure_not_expanded reset --hard $ref || return 1
1440 ensure_not_expanded reset --mixed base &&
1441 ensure_not_expanded reset --hard update-deep &&
1442 ensure_not_expanded reset --keep base &&
1443 ensure_not_expanded reset --merge update-deep &&
1444 ensure_not_expanded reset --hard &&
1446 ensure_not_expanded reset base -- deep/a &&
1447 ensure_not_expanded reset base -- nonexistent-file &&
1448 ensure_not_expanded reset deepest -- deep &&
1450 # Although folder1 is outside the sparse definition, it exists as a
1451 # directory entry in the index, so the pathspec will not force the
1452 # index to be expanded.
1453 ensure_not_expanded reset deepest -- folder1 &&
1454 ensure_not_expanded reset deepest -- folder1/ &&
1456 # Wildcard identifies only in-cone files, no index expansion
1457 ensure_not_expanded reset deepest -- deep/\* &&
1459 # Wildcard identifies only full sparse directories, no index expansion
1460 ensure_not_expanded reset deepest -- folder\* &&
1462 ensure_not_expanded clean -fd &&
1464 ensure_not_expanded checkout -f update-deep &&
1465 test_config -C sparse-index pull.twohead ort &&
1467 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1468 for OPERATION in "merge -m merge" cherry-pick rebase
1470 ensure_not_expanded merge -m merge update-folder1 &&
1471 ensure_not_expanded merge -m merge update-folder2 || return 1
1476 test_expect_success
'sparse-index is not expanded: merge conflict in cone' '
1479 for side in right left
1481 git -C sparse-index checkout -b expand-$side base &&
1482 echo $side >sparse-index/deep/a &&
1483 git -C sparse-index commit -a -m "$side" || return 1
1487 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1488 git -C sparse-index config pull.twohead ort &&
1489 ensure_not_expanded ! merge -m merged expand-right
1493 test_expect_success
'sparse-index is not expanded: stash' '
1496 echo >>sparse-index/a &&
1497 ensure_not_expanded stash &&
1498 ensure_not_expanded stash list &&
1499 ensure_not_expanded stash show stash@{0} &&
1500 ensure_not_expanded stash apply stash@{0} &&
1501 ensure_not_expanded stash drop stash@{0} &&
1503 echo >>sparse-index/deep/new &&
1504 ensure_not_expanded stash -u &&
1506 WITHOUT_UNTRACKED_TXT=1 &&
1507 ensure_not_expanded stash pop
1510 ensure_not_expanded stash create &&
1511 oid=$(git -C sparse-index stash create) &&
1512 ensure_not_expanded stash store -m "test" $oid &&
1513 ensure_not_expanded reset --hard &&
1514 ensure_not_expanded stash pop
1517 test_expect_success
'sparse index is not expanded: diff' '
1520 write_script edit-contents <<-\EOF &&
1524 # Add file within cone
1525 test_sparse_match git sparse-checkout set deep &&
1526 run_on_all ../edit-contents deep/testfile &&
1527 test_all_match git add deep/testfile &&
1528 run_on_all ../edit-contents deep/testfile &&
1530 test_all_match git diff &&
1531 test_all_match git diff --cached &&
1532 ensure_not_expanded diff &&
1533 ensure_not_expanded diff --cached &&
1535 # Add file outside cone
1536 test_all_match git reset --hard &&
1537 run_on_all mkdir newdirectory &&
1538 run_on_all ../edit-contents newdirectory/testfile &&
1539 test_sparse_match git sparse-checkout set newdirectory &&
1540 test_all_match git add newdirectory/testfile &&
1541 run_on_all ../edit-contents newdirectory/testfile &&
1542 test_sparse_match git sparse-checkout set &&
1544 test_all_match git diff &&
1545 test_all_match git diff --cached &&
1546 ensure_not_expanded diff &&
1547 ensure_not_expanded diff --cached &&
1549 # Merge conflict outside cone
1550 # The sparse checkout will report a warning that is not in the
1551 # full checkout, so we use `run_on_all` instead of
1553 run_on_all git reset --hard &&
1554 test_all_match git checkout merge-left &&
1555 test_all_match test_must_fail git merge merge-right &&
1557 test_all_match git diff &&
1558 test_all_match git diff --cached &&
1559 ensure_not_expanded diff &&
1560 ensure_not_expanded diff --cached
1563 test_expect_success
'sparse index is not expanded: show and rev-parse' '
1566 ensure_not_expanded show :a &&
1567 ensure_not_expanded show :deep/a &&
1568 ensure_not_expanded rev-parse :a &&
1569 ensure_not_expanded rev-parse :deep/a
1572 test_expect_success
'sparse index is not expanded: update-index' '
1575 deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
1576 ensure_not_expanded update-index --cacheinfo 100644 $deep_a_oid deep/a &&
1578 echo "test" >sparse-index/README.md &&
1579 echo "test2" >sparse-index/a &&
1580 rm -f sparse-index/deep/a &&
1582 ensure_not_expanded update-index --add README.md &&
1583 ensure_not_expanded update-index a &&
1584 ensure_not_expanded update-index --remove deep/a &&
1586 ensure_not_expanded reset --soft update-deep &&
1587 ensure_not_expanded update-index --add --remove --again
1590 test_expect_success
'sparse index is not expanded: blame' '
1596 deep/deeper1/deepest/a
1598 ensure_not_expanded blame $file || return 1
1602 test_expect_success
'sparse index is not expanded: fetch/pull' '
1605 git -C sparse-index remote add full "file://$(pwd)/full-checkout" &&
1606 ensure_not_expanded fetch full &&
1607 git -C full-checkout commit --allow-empty -m "for pull merge" &&
1608 git -C sparse-index commit --allow-empty -m "for pull merge" &&
1609 ensure_not_expanded pull full base
1612 test_expect_success
'sparse index is not expanded: read-tree' '
1615 ensure_not_expanded checkout -b test-branch update-folder1 &&
1616 for MERGE_TREES in "base HEAD update-folder2" \
1617 "base HEAD rename-base" \
1618 "base update-folder2" \
1619 "base rename-base" \
1622 ensure_not_expanded read-tree -mu $MERGE_TREES &&
1623 ensure_not_expanded reset --hard || return 1
1626 rm -rf sparse-index/deep/deeper2 &&
1627 ensure_not_expanded add . &&
1628 ensure_not_expanded commit -m "test" &&
1630 ensure_not_expanded read-tree --prefix=deep/deeper2 -u deepest
1633 test_expect_success
'ls-files' '
1636 # Use a smaller sparse-checkout for reduced output
1637 test_sparse_match git sparse-checkout set &&
1639 # Behavior agrees by default. Sparse index is expanded.
1640 test_all_match git ls-files &&
1642 # With --sparse, the sparse index data changes behavior.
1643 git -C sparse-index ls-files --sparse >actual &&
1645 cat >expect <<-\EOF &&
1660 test_cmp expect actual &&
1662 # With --sparse and no sparse index, nothing changes.
1663 git -C sparse-checkout ls-files >dense &&
1664 git -C sparse-checkout ls-files --sparse >sparse &&
1665 test_cmp dense sparse &&
1667 # Set up a strange condition of having a file edit
1668 # outside of the sparse-checkout cone. We want to verify
1669 # that all modes handle this the same, and detect the
1671 write_script edit-content <<-\EOF &&
1673 echo content >>folder1/a
1675 run_on_all ../edit-content &&
1677 test_all_match git ls-files --modified &&
1679 git -C sparse-index ls-files --sparse --modified >sparse-index-out &&
1680 cat >expect <<-\EOF &&
1683 test_cmp expect sparse-index-out &&
1685 # Add folder1 to the sparse-checkout cone and
1686 # check that ls-files shows the expanded files.
1687 test_sparse_match git sparse-checkout add folder1 &&
1688 test_all_match git ls-files --modified &&
1690 test_all_match git ls-files &&
1691 git -C sparse-index ls-files --sparse >actual &&
1693 cat >expect <<-\EOF &&
1710 test_cmp expect actual &&
1712 # Double-check index expansion is avoided
1713 ensure_not_expanded ls-files --sparse
1716 test_expect_success
'sparse index is not expanded: sparse-checkout' '
1719 ensure_not_expanded sparse-checkout set deep/deeper2 &&
1720 ensure_not_expanded sparse-checkout set deep/deeper1 &&
1721 ensure_not_expanded sparse-checkout set deep &&
1722 ensure_not_expanded sparse-checkout add folder1 &&
1723 ensure_not_expanded sparse-checkout set deep/deeper1 &&
1724 ensure_not_expanded sparse-checkout set folder2 &&
1726 # Demonstrate that the checks that "folder1/a" is a file
1727 # do not cause a sparse-index expansion (since it is in the
1728 # sparse-checkout cone).
1729 echo >>sparse-index/folder2/a &&
1730 git -C sparse-index add folder2/a &&
1732 ensure_not_expanded sparse-checkout add folder1 &&
1734 # Skip checks here, since deep/deeper1 is inside a sparse directory
1735 # that must be expanded to check whether `deep/deeper1` is a file
1737 ensure_not_expanded sparse-checkout set --skip-checks deep/deeper1 &&
1738 ensure_not_expanded sparse-checkout set
1741 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
1742 # in this scenario, but it shouldn't.
1743 test_expect_success
'reset mixed and checkout orphan' '
1746 test_all_match git checkout rename-out-to-in &&
1748 # Sparse checkouts do not agree with full checkouts about
1749 # how to report a directory/file conflict during a reset.
1750 # This command would fail with test_all_match because the
1751 # full checkout reports "T folder1/0/1" while a sparse
1752 # checkout reports "D folder1/0/1". This matches because
1753 # the sparse checkouts skip "adding" the other side of
1755 test_sparse_match git reset --mixed HEAD~1 &&
1756 test_sparse_match git ls-files --stage &&
1757 test_sparse_match git status --porcelain=v2 &&
1759 # At this point, sparse-checkouts behave differently
1760 # from the full-checkout.
1761 test_sparse_match git checkout --orphan new-branch &&
1762 test_sparse_match git ls-files --stage &&
1763 test_sparse_match git status --porcelain=v2
1766 test_expect_success
'add everything with deep new file' '
1769 run_on_sparse git sparse-checkout set deep/deeper1/deepest &&
1771 run_on_all touch deep/deeper1/x &&
1772 test_all_match git add . &&
1773 test_all_match git status --porcelain=v2
1776 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1777 # directory/file conflicts, even without sparse-checkout. Use this
1778 # test only as a documentation of the incorrect behavior, not a
1779 # measure of how it _should_ behave.
1780 test_expect_success
'checkout behaves oddly with df-conflict-1' '
1783 test_sparse_match git sparse-checkout disable &&
1785 write_script edit-content <<-\EOF &&
1786 echo content >>folder1/larger-content
1790 run_on_all ../edit-content &&
1791 test_all_match git status --porcelain=v2 &&
1793 git -C sparse-checkout sparse-checkout init --cone &&
1794 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1796 test_all_match git status --porcelain=v2 &&
1798 # This checkout command should fail, because we have a staged
1799 # change to folder1/larger-content, but the destination changes
1800 # folder1 to a file.
1801 git -C full-checkout checkout df-conflict-1 \
1802 1>full-checkout-out \
1803 2>full-checkout-err &&
1804 git -C sparse-checkout checkout df-conflict-1 \
1805 1>sparse-checkout-out \
1806 2>sparse-checkout-err &&
1807 git -C sparse-index checkout df-conflict-1 \
1808 1>sparse-index-out \
1809 2>sparse-index-err &&
1811 # Instead, the checkout deletes the folder1 file and adds the
1812 # folder1/larger-content file, leaving all other paths that were
1813 # in folder1/ as deleted (without any warning).
1814 cat >expect <<-EOF &&
1816 A folder1/larger-content
1818 test_cmp expect full-checkout-out &&
1819 test_cmp expect sparse-checkout-out &&
1821 # The sparse-index reports no output
1822 test_must_be_empty sparse-index-out &&
1824 # stderr: Switched to branch df-conflict-1
1825 test_cmp full-checkout-err sparse-checkout-err &&
1826 test_cmp full-checkout-err sparse-checkout-err
1829 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1830 # directory/file conflicts, even without sparse-checkout. Use this
1831 # test only as a documentation of the incorrect behavior, not a
1832 # measure of how it _should_ behave.
1833 test_expect_success
'checkout behaves oddly with df-conflict-2' '
1836 test_sparse_match git sparse-checkout disable &&
1838 write_script edit-content <<-\EOF &&
1839 echo content >>folder2/larger-content
1843 run_on_all ../edit-content &&
1844 test_all_match git status --porcelain=v2 &&
1846 git -C sparse-checkout sparse-checkout init --cone &&
1847 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1849 test_all_match git status --porcelain=v2 &&
1851 # This checkout command should fail, because we have a staged
1852 # change to folder1/larger-content, but the destination changes
1853 # folder1 to a file.
1854 git -C full-checkout checkout df-conflict-2 \
1855 1>full-checkout-out \
1856 2>full-checkout-err &&
1857 git -C sparse-checkout checkout df-conflict-2 \
1858 1>sparse-checkout-out \
1859 2>sparse-checkout-err &&
1860 git -C sparse-index checkout df-conflict-2 \
1861 1>sparse-index-out \
1862 2>sparse-index-err &&
1864 # The full checkout deviates from the df-conflict-1 case here!
1865 # It drops the change to folder1/larger-content and leaves the
1866 # folder1 path as-is on disk. The sparse-index behaves the same.
1867 test_must_be_empty full-checkout-out &&
1868 test_must_be_empty sparse-index-out &&
1870 # In the sparse-checkout case, the checkout deletes the folder1
1871 # file and adds the folder1/larger-content file, leaving all other
1872 # paths that were in folder1/ as deleted (without any warning).
1873 cat >expect <<-EOF &&
1875 A folder2/larger-content
1877 test_cmp expect sparse-checkout-out &&
1879 # Switched to branch df-conflict-1
1880 test_cmp full-checkout-err sparse-checkout-err &&
1881 test_cmp full-checkout-err sparse-index-err
1884 test_expect_success
'mv directory from out-of-cone to in-cone' '
1887 # <source> as a sparse directory (or SKIP_WORKTREE_DIR without enabling
1889 test_all_match git mv --sparse folder1 deep &&
1890 test_all_match git status --porcelain=v2 &&
1891 test_sparse_match git ls-files -t &&
1892 git -C sparse-checkout ls-files -t >actual &&
1893 grep -e "H deep/folder1/0/0/0" actual &&
1894 grep -e "H deep/folder1/0/1" actual &&
1895 grep -e "H deep/folder1/a" actual &&
1897 test_all_match git reset --hard &&
1899 # <source> as a directory deeper than sparse index boundary (where
1900 # sparse index will expand).
1901 test_sparse_match git mv --sparse folder1/0 deep &&
1902 test_sparse_match git status --porcelain=v2 &&
1903 test_sparse_match git ls-files -t &&
1904 git -C sparse-checkout ls-files -t >actual &&
1905 grep -e "H deep/0/0/0" actual &&
1906 grep -e "H deep/0/1" actual
1909 test_expect_success
'rm pathspec inside sparse definition' '
1912 test_all_match git rm deep/a &&
1913 test_all_match git status --porcelain=v2 &&
1916 run_on_all git reset --hard &&
1917 test_all_match git rm deep/* &&
1918 test_all_match git status --porcelain=v2 &&
1921 run_on_all git reset --hard &&
1922 test_all_match git rm -r deep &&
1923 test_all_match git status --porcelain=v2
1926 test_expect_success
'rm pathspec outside sparse definition' '
1929 for file in folder1/a folder1/0/1
1931 test_sparse_match test_must_fail git rm $file &&
1932 test_sparse_match test_must_fail git rm --cached $file &&
1933 test_sparse_match git rm --sparse $file &&
1934 test_sparse_match git status --porcelain=v2 || return 1
1937 cat >folder1-full <<-EOF &&
1938 rm ${SQ}folder1/0/0/0${SQ}
1939 rm ${SQ}folder1/0/1${SQ}
1940 rm ${SQ}folder1/a${SQ}
1943 cat >folder1-sparse <<-EOF &&
1944 rm ${SQ}folder1/${SQ}
1948 run_on_sparse git reset --hard &&
1949 run_on_sparse git sparse-checkout reapply &&
1950 test_sparse_match test_must_fail git rm folder1/* &&
1951 run_on_sparse git rm --sparse folder1/* &&
1952 test_cmp folder1-full sparse-checkout-out &&
1953 test_cmp folder1-sparse sparse-index-out &&
1954 test_sparse_match git status --porcelain=v2 &&
1957 run_on_sparse git reset --hard &&
1958 run_on_sparse git sparse-checkout reapply &&
1959 test_sparse_match test_must_fail git rm --sparse folder1 &&
1960 run_on_sparse git rm --sparse -r folder1 &&
1961 test_cmp folder1-full sparse-checkout-out &&
1962 test_cmp folder1-sparse sparse-index-out &&
1963 test_sparse_match git status --porcelain=v2
1966 test_expect_success
'rm pathspec expands index when necessary' '
1969 # in-cone pathspec (do not expand)
1970 ensure_not_expanded rm "deep/deep*" &&
1971 test_must_be_empty sparse-index-err &&
1973 # out-of-cone pathspec (expand)
1974 ! ensure_not_expanded rm --sparse "folder1/a*" &&
1975 test_must_be_empty sparse-index-err &&
1977 # pathspec that should expand index
1978 ! ensure_not_expanded rm "*/a" &&
1979 test_must_be_empty sparse-index-err &&
1981 ! ensure_not_expanded rm "**a" &&
1982 test_must_be_empty sparse-index-err
1985 test_expect_success
'sparse index is not expanded: rm' '
1988 ensure_not_expanded rm deep/a &&
1990 # test in-cone wildcard
1991 git -C sparse-index reset --hard &&
1992 ensure_not_expanded rm deep/* &&
1995 git -C sparse-index reset --hard &&
1996 ensure_not_expanded rm -r deep
1999 test_expect_success
'grep with and --cached' '
2002 test_all_match git grep --cached a &&
2003 test_all_match git grep --cached a -- "folder1/*"
2006 test_expect_success
'grep is not expanded' '
2009 ensure_not_expanded grep a &&
2010 ensure_not_expanded grep a -- deep/* &&
2012 # All files within the folder1/* pathspec are sparse,
2013 # so this command does not find any matches
2014 ensure_not_expanded ! grep a -- folder1/* &&
2016 # test out-of-cone pathspec with or without wildcard
2017 ensure_not_expanded grep --cached a -- "folder1/a" &&
2018 ensure_not_expanded grep --cached a -- "folder1/*" &&
2020 # test in-cone pathspec with or without wildcard
2021 ensure_not_expanded grep --cached a -- "deep/a" &&
2022 ensure_not_expanded grep --cached a -- "deep/*"
2025 # NEEDSWORK: when running `grep` in the superproject with --recurse-submodules,
2026 # Git expands the index of the submodules unexpectedly. Even though `grep`
2027 # builtin is marked as "command_requires_full_index = 0", this config is only
2028 # useful for the superproject. Namely, the submodules have their own configs,
2029 # which are _not_ populated by the one-time sparse-index feature switch.
2030 test_expect_failure
'grep within submodules is not expanded' '
2031 init_repos_as_submodules &&
2033 # do not use ensure_not_expanded() here, becasue `grep` should be
2034 # run in the superproject, not in "./sparse-index"
2035 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
2036 git grep --cached --recurse-submodules a -- "*/folder1/*" &&
2037 test_region ! index ensure_full_index trace2.txt
2040 # NEEDSWORK: this test is not actually testing the code. The design purpose
2041 # of this test is to verify the grep result when the submodules are using a
2042 # sparse-index. Namely, we want "folder1/" as a tree (a sparse directory); but
2043 # because of the index expansion, we are now grepping the "folder1/a" blob.
2044 # Because of the problem stated above 'grep within submodules is not expanded',
2045 # we don't have the ideal test environment yet.
2046 test_expect_success
'grep sparse directory within submodules' '
2047 init_repos_as_submodules &&
2049 cat >expect <<-\EOF &&
2050 full-checkout/folder1/a:a
2051 sparse-checkout/folder1/a:a
2052 sparse-index/folder1/a:a
2054 git grep --cached --recurse-submodules a -- "*/folder1/*" >actual &&
2055 test_cmp actual expect