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 x &&
20 mkdir deep/deeper1 deep/deeper2 deep/before deep/later &&
21 mkdir deep/deeper1/deepest &&
22 mkdir deep/deeper1/deepest2 &&
23 mkdir deep/deeper1/deepest3 &&
24 echo "after deeper1" >deep/e &&
25 echo "after deepest" >deep/deeper1/e &&
34 cp a deep/deeper1/deepest &&
35 cp a deep/deeper1/deepest2 &&
36 cp a deep/deeper1/deepest3 &&
37 cp -r deep/deeper1/ deep/deeper2 &&
38 mkdir deep/deeper1/0 &&
39 mkdir deep/deeper1/0/0 &&
40 touch deep/deeper1/0/1 &&
41 touch deep/deeper1/0/0/0 &&
45 cp -r deep/deeper1/0 folder1 &&
46 cp -r deep/deeper1/0 folder2 &&
47 echo >>folder1/0/0/0 &&
50 git commit -m "initial commit" &&
51 git checkout -b base &&
52 for dir in folder1 folder2 deep
54 git checkout -b update-$dir base &&
55 echo "updated $dir" >$dir/a &&
56 git commit -a -m "update $dir" || return 1
59 git checkout -b rename-base base &&
60 cat >folder1/larger-content <<-\EOF &&
67 cp folder1/larger-content folder2/ &&
68 cp folder1/larger-content deep/deeper1/ &&
70 git commit -m "add interesting rename content" &&
72 git checkout -b rename-out-to-out rename-base &&
73 mv folder1/a folder2/b &&
74 mv folder1/larger-content folder2/edited-content &&
75 echo >>folder2/edited-content &&
77 echo stuff >>deep/deeper1/a &&
79 git commit -m "rename folder1/... to folder2/..." &&
81 git checkout -b rename-out-to-in rename-base &&
82 mv folder1/a deep/deeper1/b &&
83 echo more stuff >>deep/deeper1/a &&
86 echo >>folder2/0/1/1 &&
87 mv folder1/larger-content deep/deeper1/edited-content &&
88 echo >>deep/deeper1/edited-content &&
90 git commit -m "rename folder1/... to deep/deeper1/..." &&
92 git checkout -b rename-in-to-out rename-base &&
93 mv deep/deeper1/a folder1/b &&
97 mv deep/deeper1/larger-content folder1/edited-content &&
98 echo >>folder1/edited-content &&
100 git commit -m "rename deep/deeper1/... to folder1/..." &&
102 git checkout -b df-conflict-1 base &&
104 echo content >folder1 &&
106 git commit -m "dir to file" &&
108 git checkout -b df-conflict-2 base &&
110 echo content >folder2 &&
112 git commit -m "dir to file" &&
114 git checkout -b fd-conflict base &&
119 git commit -m "file to dir" &&
121 for side in left right
123 git checkout -b merge-$side base &&
124 echo $side >>deep/deeper2/a &&
125 echo $side >>folder1/a &&
126 echo $side >>folder2/a &&
128 git commit -m "$side" || return 1
131 git checkout -b deepest base &&
132 echo "updated deepest" >deep/deeper1/deepest/a &&
133 echo "updated deepest2" >deep/deeper1/deepest2/a &&
134 echo "updated deepest3" >deep/deeper1/deepest3/a &&
135 git commit -a -m "update deepest" &&
137 git checkout -f base &&
143 rm -rf full-checkout sparse-checkout sparse-index
&&
145 # create repos in initial state
146 cp -r initial-repo full-checkout
&&
147 git
-C full-checkout
reset --hard &&
149 cp -r initial-repo sparse-checkout
&&
150 git
-C sparse-checkout
reset --hard &&
152 cp -r initial-repo sparse-index
&&
153 git
-C sparse-index
reset --hard &&
155 # initialize sparse-checkout definitions
156 git
-C sparse-checkout sparse-checkout init
--cone &&
157 git
-C sparse-checkout sparse-checkout
set deep
&&
158 git
-C sparse-index sparse-checkout init
--cone --sparse-index &&
159 test_cmp_config
-C sparse-index true index.sparse
&&
160 git
-C sparse-index sparse-checkout
set deep
165 cd sparse-checkout
&&
166 GIT_PROGRESS_DELAY
=100000 "$@" >..
/sparse-checkout-out
2>..
/sparse-checkout-err
170 GIT_PROGRESS_DELAY
=100000 "$@" >..
/sparse-index-out
2>..
/sparse-index-err
177 GIT_PROGRESS_DELAY
=100000 "$@" >..
/full-checkout-out
2>..
/full-checkout-err
184 test_cmp full-checkout-out sparse-checkout-out
&&
185 test_cmp full-checkout-out sparse-index-out
&&
186 test_cmp full-checkout-err sparse-checkout-err
&&
187 test_cmp full-checkout-err sparse-index-err
190 test_sparse_match
() {
191 run_on_sparse
"$@" &&
192 test_cmp sparse-checkout-out sparse-index-out
&&
193 test_cmp sparse-checkout-err sparse-index-err
196 test_sparse_unstaged
() {
198 for repo
in sparse-checkout sparse-index
200 # Skip "unmerged" paths
201 git
-C $repo diff --staged --diff-filter=u
-- "$file" >diff &&
202 test_must_be_empty
diff ||
return 1
206 test_expect_success
'sparse-index contents' '
209 git -C sparse-index ls-files --sparse --stage >cache &&
210 for dir in folder1 folder2 x
212 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
213 grep "040000 $TREE 0 $dir/" cache \
217 git -C sparse-index sparse-checkout set folder1 &&
219 git -C sparse-index ls-files --sparse --stage >cache &&
220 for dir in deep folder2 x
222 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
223 grep "040000 $TREE 0 $dir/" cache \
227 git -C sparse-index sparse-checkout set deep/deeper1 &&
229 git -C sparse-index ls-files --sparse --stage >cache &&
230 for dir in deep/deeper2 folder1 folder2 x
232 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
233 grep "040000 $TREE 0 $dir/" cache \
237 # Disabling the sparse-index replaces tree entries with full ones
238 git -C sparse-index sparse-checkout init --no-sparse-index &&
239 test_sparse_match git ls-files --stage --sparse
242 test_expect_success
'expanded in-memory index matches full index' '
244 test_sparse_match git ls-files --stage
247 test_expect_success
'status with options' '
249 test_sparse_match ls &&
250 test_all_match git status --porcelain=v2 &&
251 test_all_match git status --porcelain=v2 -z -u &&
252 test_all_match git status --porcelain=v2 -uno &&
253 run_on_all touch README.md &&
254 test_all_match git status --porcelain=v2 &&
255 test_all_match git status --porcelain=v2 -z -u &&
256 test_all_match git status --porcelain=v2 -uno &&
257 test_all_match git add README.md &&
258 test_all_match git status --porcelain=v2 &&
259 test_all_match git status --porcelain=v2 -z -u &&
260 test_all_match git status --porcelain=v2 -uno
263 test_expect_success
'status reports sparse-checkout' '
265 git -C sparse-checkout status >full &&
266 git -C sparse-index status >sparse &&
267 test_i18ngrep "You are in a sparse checkout with " full &&
268 test_i18ngrep "You are in a sparse checkout." sparse
271 test_expect_success
'add, commit, checkout' '
274 write_script edit-contents <<-\EOF &&
277 run_on_all ../edit-contents README.md &&
279 test_all_match git add README.md &&
280 test_all_match git status --porcelain=v2 &&
281 test_all_match git commit -m "Add README.md" &&
283 test_all_match git checkout HEAD~1 &&
284 test_all_match git checkout - &&
286 run_on_all ../edit-contents README.md &&
288 test_all_match git add -A &&
289 test_all_match git status --porcelain=v2 &&
290 test_all_match git commit -m "Extend README.md" &&
292 test_all_match git checkout HEAD~1 &&
293 test_all_match git checkout - &&
295 run_on_all ../edit-contents deep/newfile &&
297 test_all_match git status --porcelain=v2 -uno &&
298 test_all_match git status --porcelain=v2 &&
299 test_all_match git add . &&
300 test_all_match git status --porcelain=v2 &&
301 test_all_match git commit -m "add deep/newfile" &&
303 test_all_match git checkout HEAD~1 &&
304 test_all_match git checkout -
307 test_expect_success
'deep changes during checkout' '
310 test_sparse_match git sparse-checkout set deep/deeper1/deepest &&
311 test_all_match git checkout deepest &&
312 test_all_match git checkout base
315 test_expect_success
'add outside sparse cone' '
318 run_on_sparse mkdir folder1 &&
319 run_on_sparse ../edit-contents folder1/a &&
320 run_on_sparse ../edit-contents folder1/newfile &&
321 test_sparse_match test_must_fail git add folder1/a &&
322 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
323 test_sparse_unstaged folder1/a &&
324 test_sparse_match test_must_fail git add folder1/newfile &&
325 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
326 test_sparse_unstaged folder1/newfile
329 test_expect_success
'commit including unstaged changes' '
332 write_script edit-file <<-\EOF &&
336 run_on_all ../edit-file 1 a &&
337 run_on_all ../edit-file 1 deep/a &&
339 test_all_match git commit -m "-a" -a &&
340 test_all_match git status --porcelain=v2 &&
342 run_on_all ../edit-file 2 a &&
343 run_on_all ../edit-file 2 deep/a &&
345 test_all_match git commit -m "--include" --include deep/a &&
346 test_all_match git status --porcelain=v2 &&
347 test_all_match git commit -m "--include" --include a &&
348 test_all_match git status --porcelain=v2 &&
350 run_on_all ../edit-file 3 a &&
351 run_on_all ../edit-file 3 deep/a &&
353 test_all_match git commit -m "--amend" -a --amend &&
354 test_all_match git status --porcelain=v2
357 test_expect_success
'status/add: outside sparse cone' '
360 # folder1 is at HEAD, but outside the sparse cone
361 run_on_sparse mkdir folder1 &&
362 cp initial-repo/folder1/a sparse-checkout/folder1/a &&
363 cp initial-repo/folder1/a sparse-index/folder1/a &&
365 test_sparse_match git status &&
367 write_script edit-contents <<-\EOF &&
370 run_on_sparse ../edit-contents folder1/a &&
371 run_on_all ../edit-contents folder1/new &&
373 test_sparse_match git status --porcelain=v2 &&
375 # Adding the path outside of the sparse-checkout cone should fail.
376 test_sparse_match test_must_fail git add folder1/a &&
377 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
378 test_sparse_unstaged folder1/a &&
379 test_sparse_match test_must_fail git add --refresh folder1/a &&
380 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
381 test_sparse_unstaged folder1/a &&
382 test_sparse_match test_must_fail git add folder1/new &&
383 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
384 test_sparse_unstaged folder1/new &&
385 test_sparse_match git add --sparse folder1/a &&
386 test_sparse_match git add --sparse folder1/new &&
388 test_all_match git add --sparse . &&
389 test_all_match git status --porcelain=v2 &&
390 test_all_match git commit -m folder1/new &&
391 test_all_match git rev-parse HEAD^{tree} &&
393 run_on_all ../edit-contents folder1/newer &&
394 test_all_match git add --sparse folder1/ &&
395 test_all_match git status --porcelain=v2 &&
396 test_all_match git commit -m folder1/newer &&
397 test_all_match git rev-parse HEAD^{tree}
400 test_expect_success
'checkout and reset --hard' '
403 test_all_match git checkout update-folder1 &&
404 test_all_match git status --porcelain=v2 &&
406 test_all_match git checkout update-deep &&
407 test_all_match git status --porcelain=v2 &&
409 test_all_match git checkout -b reset-test &&
410 test_all_match git reset --hard deepest &&
411 test_all_match git reset --hard update-folder1 &&
412 test_all_match git reset --hard update-folder2
415 test_expect_success
'diff --cached' '
418 write_script edit-contents <<-\EOF &&
419 echo text >>README.md
421 run_on_all ../edit-contents &&
423 test_all_match git diff &&
424 test_all_match git diff --cached &&
425 test_all_match git add README.md &&
426 test_all_match git diff &&
427 test_all_match git diff --cached
430 # NEEDSWORK: sparse-checkout behaves differently from full-checkout when
431 # running this test with 'df-conflict-2' after 'df-conflict-1'.
432 test_expect_success
'diff with renames and conflicts' '
435 for branch in rename-out-to-out \
441 test_all_match git checkout rename-base &&
442 test_all_match git checkout $branch -- . &&
443 test_all_match git status --porcelain=v2 &&
444 test_all_match git diff --cached --no-renames &&
445 test_all_match git diff --cached --find-renames || return 1
449 test_expect_success
'diff with directory/file conflicts' '
452 for branch in rename-out-to-out \
459 git -C full-checkout reset --hard &&
460 test_sparse_match git reset --hard &&
461 test_all_match git checkout $branch &&
462 test_all_match git checkout rename-base -- . &&
463 test_all_match git status --porcelain=v2 &&
464 test_all_match git diff --cached --no-renames &&
465 test_all_match git diff --cached --find-renames || return 1
469 test_expect_success
'log with pathspec outside sparse definition' '
472 test_all_match git log -- a &&
473 test_all_match git log -- folder1/a &&
474 test_all_match git log -- folder2/a &&
475 test_all_match git log -- deep/a &&
476 test_all_match git log -- deep/deeper1/a &&
477 test_all_match git log -- deep/deeper1/deepest/a &&
479 test_all_match git checkout update-folder1 &&
480 test_all_match git log -- folder1/a
483 test_expect_success
'blame with pathspec inside sparse definition' '
489 deep/deeper1/deepest/a
491 test_all_match git blame $file
495 # Without a revision specified, blame will error if passed any file that
496 # is not present in the working directory (even if the file is tracked).
497 # Here we just verify that this is also true with sparse checkouts.
498 test_expect_success
'blame with pathspec outside sparse definition' '
500 test_sparse_match git sparse-checkout set &&
505 deep/deeper1/deepest/a
507 test_sparse_match test_must_fail git blame $file &&
508 cat >expect <<-EOF &&
509 fatal: Cannot lstat '"'"'$file'"'"': No such file or directory
511 # We compare sparse-checkout-err and sparse-index-err in
512 # `test_sparse_match`. Given we know they are the same, we
513 # only check the content of sparse-index-err here.
514 test_cmp expect sparse-index-err
518 test_expect_success
'checkout and reset (mixed)' '
521 test_all_match git checkout -b reset-test update-deep &&
522 test_all_match git reset deepest &&
524 # Because skip-worktree is preserved, resetting to update-folder1
525 # will show worktree changes for folder1/a in full-checkout, but not
526 # in sparse-checkout or sparse-index.
527 git -C full-checkout reset update-folder1 >full-checkout-out &&
528 test_sparse_match git reset update-folder1 &&
529 grep "M folder1/a" full-checkout-out &&
530 ! grep "M folder1/a" sparse-checkout-out &&
531 run_on_sparse test_path_is_missing folder1
534 test_expect_success
'checkout and reset (merge)' '
537 write_script edit-contents <<-\EOF &&
541 test_all_match git checkout -b reset-test update-deep &&
542 run_on_all ../edit-contents a &&
543 test_all_match git reset --merge deepest &&
544 test_all_match git status --porcelain=v2 &&
546 test_all_match git reset --hard update-deep &&
547 run_on_all ../edit-contents deep/a &&
548 test_all_match test_must_fail git reset --merge deepest
551 test_expect_success
'checkout and reset (keep)' '
554 write_script edit-contents <<-\EOF &&
558 test_all_match git checkout -b reset-test update-deep &&
559 run_on_all ../edit-contents a &&
560 test_all_match git reset --keep deepest &&
561 test_all_match git status --porcelain=v2 &&
563 test_all_match git reset --hard update-deep &&
564 run_on_all ../edit-contents deep/a &&
565 test_all_match test_must_fail git reset --keep deepest
568 test_expect_success
'reset with pathspecs inside sparse definition' '
571 write_script edit-contents <<-\EOF &&
575 test_all_match git checkout -b reset-test update-deep &&
576 run_on_all ../edit-contents deep/a &&
578 test_all_match git reset base -- deep/a &&
579 test_all_match git status --porcelain=v2 &&
581 test_all_match git reset base -- nonexistent-file &&
582 test_all_match git status --porcelain=v2 &&
584 test_all_match git reset deepest -- deep &&
585 test_all_match git status --porcelain=v2
588 # Although the working tree differs between full and sparse checkouts after
589 # reset, the state of the index is the same.
590 test_expect_success
'reset with pathspecs outside sparse definition' '
592 test_all_match git checkout -b reset-test base &&
594 test_sparse_match git reset update-folder1 -- folder1 &&
595 git -C full-checkout reset update-folder1 -- folder1 &&
596 test_sparse_match git status --porcelain=v2 &&
597 test_all_match git rev-parse HEAD:folder1 &&
599 test_sparse_match git reset update-folder2 -- folder2/a &&
600 git -C full-checkout reset update-folder2 -- folder2/a &&
601 test_sparse_match git status --porcelain=v2 &&
602 test_all_match git rev-parse HEAD:folder2/a
605 test_expect_success
'reset with wildcard pathspec' '
608 test_all_match git reset update-deep -- deep\* &&
609 test_all_match git ls-files -s -- deep &&
611 test_all_match git reset deepest -- deep\*\*\* &&
612 test_all_match git ls-files -s -- deep &&
614 # The following `git reset`s result in updating the index on files with
615 # `skip-worktree` enabled. To avoid failing due to discrepencies in reported
616 # "modified" files, `test_sparse_match` reset is performed separately from
617 # "full-checkout" reset, then the index contents of all repos are verified.
619 test_sparse_match git reset update-folder1 -- \*/a &&
620 git -C full-checkout reset update-folder1 -- \*/a &&
621 test_all_match git ls-files -s -- deep/a folder1/a &&
623 test_sparse_match git reset update-folder2 -- folder\* &&
624 git -C full-checkout reset update-folder2 -- folder\* &&
625 test_all_match git ls-files -s -- folder10 folder1 folder2 &&
627 test_sparse_match git reset base -- folder1/\* &&
628 git -C full-checkout reset base -- folder1/\* &&
629 test_all_match git ls-files -s -- folder1
632 test_expect_success
'merge, cherry-pick, and rebase' '
635 for OPERATION in "merge -m merge" cherry-pick "rebase --apply" "rebase --merge"
637 test_all_match git checkout -B temp update-deep &&
638 test_all_match git $OPERATION update-folder1 &&
639 test_all_match git rev-parse HEAD^{tree} &&
640 test_all_match git $OPERATION update-folder2 &&
641 test_all_match git rev-parse HEAD^{tree} || return 1
645 test_expect_success
'merge with conflict outside cone' '
648 test_all_match git checkout -b merge-tip merge-left &&
649 test_all_match git status --porcelain=v2 &&
650 test_all_match test_must_fail git merge -m merge merge-right &&
651 test_all_match git status --porcelain=v2 &&
653 # Resolve the conflict in different ways:
654 # 1. Revert to the base
655 test_all_match git checkout base -- deep/deeper2/a &&
656 test_all_match git status --porcelain=v2 &&
658 # 2. Add the file with conflict markers
659 test_sparse_match test_must_fail git add folder1/a &&
660 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
661 test_sparse_unstaged folder1/a &&
662 test_all_match git add --sparse folder1/a &&
663 test_all_match git status --porcelain=v2 &&
665 # 3. Rename the file to another sparse filename and
666 # accept conflict markers as resolved content.
667 run_on_all mv folder2/a folder2/z &&
668 test_sparse_match test_must_fail git add folder2 &&
669 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
670 test_sparse_unstaged folder2/z &&
671 test_all_match git add --sparse folder2 &&
672 test_all_match git status --porcelain=v2 &&
674 test_all_match git merge --continue &&
675 test_all_match git status --porcelain=v2 &&
676 test_all_match git rev-parse HEAD^{tree}
679 test_expect_success
'cherry-pick/rebase with conflict outside cone' '
682 for OPERATION in cherry-pick rebase
684 test_all_match git checkout -B tip &&
685 test_all_match git reset --hard merge-left &&
686 test_all_match git status --porcelain=v2 &&
687 test_all_match test_must_fail git $OPERATION merge-right &&
688 test_all_match git status --porcelain=v2 &&
690 # Resolve the conflict in different ways:
691 # 1. Revert to the base
692 test_all_match git checkout base -- deep/deeper2/a &&
693 test_all_match git status --porcelain=v2 &&
695 # 2. Add the file with conflict markers
696 # NEEDSWORK: Even though the merge conflict removed the
697 # SKIP_WORKTREE bit from the index entry for folder1/a, we should
698 # warn that this is a problematic add.
699 test_sparse_match test_must_fail git add folder1/a &&
700 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
701 test_sparse_unstaged folder1/a &&
702 test_all_match git add --sparse folder1/a &&
703 test_all_match git status --porcelain=v2 &&
705 # 3. Rename the file to another sparse filename and
706 # accept conflict markers as resolved content.
707 # NEEDSWORK: This mode now fails, because folder2/z is
708 # outside of the sparse-checkout cone and does not match an
709 # existing index entry with the SKIP_WORKTREE bit cleared.
710 run_on_all mv folder2/a folder2/z &&
711 test_sparse_match test_must_fail git add folder2 &&
712 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
713 test_sparse_unstaged folder2/z &&
714 test_all_match git add --sparse folder2 &&
715 test_all_match git status --porcelain=v2 &&
717 test_all_match git $OPERATION --continue &&
718 test_all_match git status --porcelain=v2 &&
719 test_all_match git rev-parse HEAD^{tree} || return 1
723 test_expect_success
'merge with outside renames' '
726 for type in out-to-out out-to-in in-to-out
728 test_all_match git reset --hard &&
729 test_all_match git checkout -f -b merge-$type update-deep &&
730 test_all_match git merge -m "$type" rename-$type &&
731 test_all_match git rev-parse HEAD^{tree} || return 1
735 # Sparse-index fails to convert the index in the
736 # final 'git cherry-pick' command.
737 test_expect_success
'cherry-pick with conflicts' '
740 write_script edit-conflict <<-\EOF &&
744 test_all_match git checkout -b to-cherry-pick &&
745 run_on_all ../edit-conflict ABC &&
746 test_all_match git add conflict &&
747 test_all_match git commit -m "conflict to pick" &&
749 test_all_match git checkout -B base HEAD~1 &&
750 run_on_all ../edit-conflict DEF &&
751 test_all_match git add conflict &&
752 test_all_match git commit -m "conflict in base" &&
754 test_all_match test_must_fail git cherry-pick to-cherry-pick
757 test_expect_success
'clean' '
760 echo bogus >>.gitignore &&
761 run_on_all cp ../.gitignore . &&
762 test_all_match git add .gitignore &&
763 test_all_match git commit -m "ignore bogus files" &&
765 run_on_sparse mkdir folder1 &&
766 run_on_all touch folder1/bogus &&
768 test_all_match git status --porcelain=v2 &&
769 test_all_match git clean -f &&
770 test_all_match git status --porcelain=v2 &&
771 test_sparse_match ls &&
772 test_sparse_match ls folder1 &&
774 test_all_match git clean -xf &&
775 test_all_match git status --porcelain=v2 &&
776 test_sparse_match ls &&
777 test_sparse_match ls folder1 &&
779 test_all_match git clean -xdf &&
780 test_all_match git status --porcelain=v2 &&
781 test_sparse_match ls &&
782 test_sparse_match ls folder1 &&
784 test_sparse_match test_path_is_dir folder1
787 test_expect_success
'submodule handling' '
790 test_sparse_match git sparse-checkout add modules &&
791 test_all_match mkdir modules &&
792 test_all_match touch modules/a &&
793 test_all_match git add modules &&
794 test_all_match git commit -m "add modules directory" &&
796 run_on_all git submodule add "$(pwd)/initial-repo" modules/sub &&
797 test_all_match git commit -m "add submodule" &&
799 # having a submodule prevents "modules" from collapse
800 test_sparse_match git sparse-checkout set deep/deeper1 &&
801 git -C sparse-index ls-files --sparse --stage >cache &&
802 grep "100644 .* modules/a" cache &&
803 grep "160000 $(git -C initial-repo rev-parse HEAD) 0 modules/sub" cache
806 # When working with a sparse index, some commands will need to expand the
807 # index to operate properly. If those commands also write the index back
808 # to disk, they need to convert the index to sparse before writing.
809 # This test verifies that both of these events are logged in trace2 logs.
810 test_expect_success
'sparse-index is expanded and converted back' '
813 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
814 git -C sparse-index reset -- folder1/a &&
815 test_region index convert_to_sparse trace2.txt &&
816 test_region index ensure_full_index trace2.txt &&
818 # ls-files expands on read, but does not write.
820 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
821 git -C sparse-index ls-files &&
822 test_region index ensure_full_index trace2.txt
825 test_expect_success
'index.sparse disabled inline uses full index' '
828 # When index.sparse is disabled inline with `git status`, the
829 # index is expanded at the beginning of the execution then never
830 # converted back to sparse. It is then written to disk as a full index.
832 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
833 git -C sparse-index -c index.sparse=false status &&
834 ! test_region index convert_to_sparse trace2.txt &&
835 test_region index ensure_full_index trace2.txt &&
837 # Since index.sparse is set to true at a repo level, the index
838 # is converted from full to sparse when read, then never expanded
839 # over the course of `git status`. It is written to disk as a sparse
842 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
843 git -C sparse-index status &&
844 test_region index convert_to_sparse trace2.txt &&
845 ! test_region index ensure_full_index trace2.txt &&
847 # Now that the index has been written to disk as sparse, it is not
848 # converted to sparse (or expanded to full) when read by `git status`.
850 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
851 git -C sparse-index status &&
852 ! test_region index convert_to_sparse trace2.txt &&
853 ! test_region index ensure_full_index trace2.txt
856 ensure_not_expanded
() {
858 echo >>sparse-index
/untracked.txt
&&
864 GIT_TRACE2_EVENT
="$(pwd)/trace2.txt" \
865 git
-C sparse-index
"$@" ||
return 1
867 GIT_TRACE2_EVENT
="$(pwd)/trace2.txt" \
868 git
-C sparse-index
"$@" ||
return 1
870 test_region
! index ensure_full_index trace2.txt
873 test_expect_success
'sparse-index is not expanded' '
876 ensure_not_expanded status &&
877 ensure_not_expanded ls-files --sparse &&
878 ensure_not_expanded commit --allow-empty -m empty &&
879 echo >>sparse-index/a &&
880 ensure_not_expanded commit -a -m a &&
881 echo >>sparse-index/a &&
882 ensure_not_expanded commit --include a -m a &&
883 echo >>sparse-index/deep/deeper1/a &&
884 ensure_not_expanded commit --include deep/deeper1/a -m deeper &&
885 ensure_not_expanded checkout rename-out-to-out &&
886 ensure_not_expanded checkout - &&
887 ensure_not_expanded switch rename-out-to-out &&
888 ensure_not_expanded switch - &&
889 ensure_not_expanded reset --hard &&
890 ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
891 ensure_not_expanded reset --hard &&
892 ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
894 echo >>sparse-index/README.md &&
895 ensure_not_expanded add -A &&
896 echo >>sparse-index/extra.txt &&
897 ensure_not_expanded add extra.txt &&
898 echo >>sparse-index/untracked.txt &&
899 ensure_not_expanded add . &&
901 for ref in update-deep update-folder1 update-folder2 update-deep
903 echo >>sparse-index/README.md &&
904 ensure_not_expanded reset --hard $ref || return 1
907 ensure_not_expanded reset --mixed base &&
908 ensure_not_expanded reset --hard update-deep &&
909 ensure_not_expanded reset --keep base &&
910 ensure_not_expanded reset --merge update-deep &&
911 ensure_not_expanded reset --hard &&
913 ensure_not_expanded reset base -- deep/a &&
914 ensure_not_expanded reset base -- nonexistent-file &&
915 ensure_not_expanded reset deepest -- deep &&
917 # Although folder1 is outside the sparse definition, it exists as a
918 # directory entry in the index, so the pathspec will not force the
919 # index to be expanded.
920 ensure_not_expanded reset deepest -- folder1 &&
921 ensure_not_expanded reset deepest -- folder1/ &&
923 # Wildcard identifies only in-cone files, no index expansion
924 ensure_not_expanded reset deepest -- deep/\* &&
926 # Wildcard identifies only full sparse directories, no index expansion
927 ensure_not_expanded reset deepest -- folder\* &&
929 ensure_not_expanded checkout -f update-deep &&
930 test_config -C sparse-index pull.twohead ort &&
932 sane_unset GIT_TEST_MERGE_ALGORITHM &&
933 for OPERATION in "merge -m merge" cherry-pick rebase
935 ensure_not_expanded merge -m merge update-folder1 &&
936 ensure_not_expanded merge -m merge update-folder2 || return 1
941 test_expect_success
'sparse-index is not expanded: merge conflict in cone' '
944 for side in right left
946 git -C sparse-index checkout -b expand-$side base &&
947 echo $side >sparse-index/deep/a &&
948 git -C sparse-index commit -a -m "$side" || return 1
952 sane_unset GIT_TEST_MERGE_ALGORITHM &&
953 git -C sparse-index config pull.twohead ort &&
954 ensure_not_expanded ! merge -m merged expand-right
958 test_expect_success
'sparse index is not expanded: diff' '
961 write_script edit-contents <<-\EOF &&
965 # Add file within cone
966 test_sparse_match git sparse-checkout set deep &&
967 run_on_all ../edit-contents deep/testfile &&
968 test_all_match git add deep/testfile &&
969 run_on_all ../edit-contents deep/testfile &&
971 test_all_match git diff &&
972 test_all_match git diff --cached &&
973 ensure_not_expanded diff &&
974 ensure_not_expanded diff --cached &&
976 # Add file outside cone
977 test_all_match git reset --hard &&
978 run_on_all mkdir newdirectory &&
979 run_on_all ../edit-contents newdirectory/testfile &&
980 test_sparse_match git sparse-checkout set newdirectory &&
981 test_all_match git add newdirectory/testfile &&
982 run_on_all ../edit-contents newdirectory/testfile &&
983 test_sparse_match git sparse-checkout set &&
985 test_all_match git diff &&
986 test_all_match git diff --cached &&
987 ensure_not_expanded diff &&
988 ensure_not_expanded diff --cached &&
990 # Merge conflict outside cone
991 # The sparse checkout will report a warning that is not in the
992 # full checkout, so we use `run_on_all` instead of
994 run_on_all git reset --hard &&
995 test_all_match git checkout merge-left &&
996 test_all_match test_must_fail git merge merge-right &&
998 test_all_match git diff &&
999 test_all_match git diff --cached &&
1000 ensure_not_expanded diff &&
1001 ensure_not_expanded diff --cached
1004 test_expect_success
'sparse index is not expanded: blame' '
1010 deep/deeper1/deepest/a
1012 ensure_not_expanded blame $file
1016 test_expect_success
'sparse index is not expanded: fetch/pull' '
1019 git -C sparse-index remote add full "file://$(pwd)/full-checkout" &&
1020 ensure_not_expanded fetch full &&
1021 git -C full-checkout commit --allow-empty -m "for pull merge" &&
1022 git -C sparse-index commit --allow-empty -m "for pull merge" &&
1023 ensure_not_expanded pull full base
1026 test_expect_success
'ls-files' '
1029 # Use a smaller sparse-checkout for reduced output
1030 test_sparse_match git sparse-checkout set &&
1032 # Behavior agrees by default. Sparse index is expanded.
1033 test_all_match git ls-files &&
1035 # With --sparse, the sparse index data changes behavior.
1036 git -C sparse-index ls-files --sparse >actual &&
1038 cat >expect <<-\EOF &&
1052 test_cmp expect actual &&
1054 # With --sparse and no sparse index, nothing changes.
1055 git -C sparse-checkout ls-files >dense &&
1056 git -C sparse-checkout ls-files --sparse >sparse &&
1057 test_cmp dense sparse &&
1059 # Set up a strange condition of having a file edit
1060 # outside of the sparse-checkout cone. This is just
1061 # to verify that sparse-checkout and sparse-index
1062 # behave the same in this case.
1063 write_script edit-content <<-\EOF &&
1065 echo content >>folder1/a
1067 run_on_sparse ../edit-content &&
1069 # ls-files does not currently notice modified files whose
1070 # cache entries are marked SKIP_WORKTREE. This may change
1071 # in the future, but here we test that sparse index does
1072 # not accidentally create a change of behavior.
1073 test_sparse_match git ls-files --modified &&
1074 test_must_be_empty sparse-checkout-out &&
1075 test_must_be_empty sparse-index-out &&
1077 git -C sparse-index ls-files --sparse --modified >sparse-index-out &&
1078 test_must_be_empty sparse-index-out &&
1080 # Add folder1 to the sparse-checkout cone and
1081 # check that ls-files shows the expanded files.
1082 test_sparse_match git sparse-checkout add folder1 &&
1083 test_sparse_match git ls-files --modified &&
1085 test_all_match git ls-files &&
1086 git -C sparse-index ls-files --sparse >actual &&
1088 cat >expect <<-\EOF &&
1104 test_cmp expect actual &&
1106 # Double-check index expansion is avoided
1107 ensure_not_expanded ls-files --sparse
1110 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
1111 # in this scenario, but it shouldn't.
1112 test_expect_success
'reset mixed and checkout orphan' '
1115 test_all_match git checkout rename-out-to-in &&
1117 # Sparse checkouts do not agree with full checkouts about
1118 # how to report a directory/file conflict during a reset.
1119 # This command would fail with test_all_match because the
1120 # full checkout reports "T folder1/0/1" while a sparse
1121 # checkout reports "D folder1/0/1". This matches because
1122 # the sparse checkouts skip "adding" the other side of
1124 test_sparse_match git reset --mixed HEAD~1 &&
1125 test_sparse_match git ls-files --stage &&
1126 test_sparse_match git status --porcelain=v2 &&
1128 # At this point, sparse-checkouts behave differently
1129 # from the full-checkout.
1130 test_sparse_match git checkout --orphan new-branch &&
1131 test_sparse_match git ls-files --stage &&
1132 test_sparse_match git status --porcelain=v2
1135 test_expect_success
'add everything with deep new file' '
1138 run_on_sparse git sparse-checkout set deep/deeper1/deepest &&
1140 run_on_all touch deep/deeper1/x &&
1141 test_all_match git add . &&
1142 test_all_match git status --porcelain=v2
1145 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1146 # directory/file conflicts, even without sparse-checkout. Use this
1147 # test only as a documentation of the incorrect behavior, not a
1148 # measure of how it _should_ behave.
1149 test_expect_success
'checkout behaves oddly with df-conflict-1' '
1152 test_sparse_match git sparse-checkout disable &&
1154 write_script edit-content <<-\EOF &&
1155 echo content >>folder1/larger-content
1159 run_on_all ../edit-content &&
1160 test_all_match git status --porcelain=v2 &&
1162 git -C sparse-checkout sparse-checkout init --cone &&
1163 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1165 test_all_match git status --porcelain=v2 &&
1167 # This checkout command should fail, because we have a staged
1168 # change to folder1/larger-content, but the destination changes
1169 # folder1 to a file.
1170 git -C full-checkout checkout df-conflict-1 \
1171 1>full-checkout-out \
1172 2>full-checkout-err &&
1173 git -C sparse-checkout checkout df-conflict-1 \
1174 1>sparse-checkout-out \
1175 2>sparse-checkout-err &&
1176 git -C sparse-index checkout df-conflict-1 \
1177 1>sparse-index-out \
1178 2>sparse-index-err &&
1180 # Instead, the checkout deletes the folder1 file and adds the
1181 # folder1/larger-content file, leaving all other paths that were
1182 # in folder1/ as deleted (without any warning).
1183 cat >expect <<-EOF &&
1185 A folder1/larger-content
1187 test_cmp expect full-checkout-out &&
1188 test_cmp expect sparse-checkout-out &&
1190 # The sparse-index reports no output
1191 test_must_be_empty sparse-index-out &&
1193 # stderr: Switched to branch df-conflict-1
1194 test_cmp full-checkout-err sparse-checkout-err &&
1195 test_cmp full-checkout-err sparse-checkout-err
1198 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1199 # directory/file conflicts, even without sparse-checkout. Use this
1200 # test only as a documentation of the incorrect behavior, not a
1201 # measure of how it _should_ behave.
1202 test_expect_success
'checkout behaves oddly with df-conflict-2' '
1205 test_sparse_match git sparse-checkout disable &&
1207 write_script edit-content <<-\EOF &&
1208 echo content >>folder2/larger-content
1212 run_on_all ../edit-content &&
1213 test_all_match git status --porcelain=v2 &&
1215 git -C sparse-checkout sparse-checkout init --cone &&
1216 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1218 test_all_match git status --porcelain=v2 &&
1220 # This checkout command should fail, because we have a staged
1221 # change to folder1/larger-content, but the destination changes
1222 # folder1 to a file.
1223 git -C full-checkout checkout df-conflict-2 \
1224 1>full-checkout-out \
1225 2>full-checkout-err &&
1226 git -C sparse-checkout checkout df-conflict-2 \
1227 1>sparse-checkout-out \
1228 2>sparse-checkout-err &&
1229 git -C sparse-index checkout df-conflict-2 \
1230 1>sparse-index-out \
1231 2>sparse-index-err &&
1233 # The full checkout deviates from the df-conflict-1 case here!
1234 # It drops the change to folder1/larger-content and leaves the
1235 # folder1 path as-is on disk. The sparse-index behaves the same.
1236 test_must_be_empty full-checkout-out &&
1237 test_must_be_empty sparse-index-out &&
1239 # In the sparse-checkout case, the checkout deletes the folder1
1240 # file and adds the folder1/larger-content file, leaving all other
1241 # paths that were in folder1/ as deleted (without any warning).
1242 cat >expect <<-EOF &&
1244 A folder2/larger-content
1246 test_cmp expect sparse-checkout-out &&
1248 # Switched to branch df-conflict-1
1249 test_cmp full-checkout-err sparse-checkout-err &&
1250 test_cmp full-checkout-err sparse-index-err