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 test-tool -C sparse-index read-cache --table >cache &&
210 for dir in folder1 folder2 x
212 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
213 grep "040000 tree $TREE $dir/" cache \
217 git -C sparse-index sparse-checkout set folder1 &&
219 test-tool -C sparse-index read-cache --table >cache &&
220 for dir in deep folder2 x
222 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
223 grep "040000 tree $TREE $dir/" cache \
227 git -C sparse-index sparse-checkout set deep/deeper1 &&
229 test-tool -C sparse-index read-cache --table >cache &&
230 for dir in deep/deeper2 folder1 folder2 x
232 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
233 grep "040000 tree $TREE $dir/" cache \
237 # Disabling the sparse-index removes tree entries with full ones
238 git -C sparse-index sparse-checkout init --no-sparse-index &&
240 test-tool -C sparse-index read-cache --table >cache &&
241 ! grep "040000 tree" cache &&
242 test_sparse_match test-tool read-cache --table
245 test_expect_success
'expanded in-memory index matches full index' '
247 test_sparse_match test-tool read-cache --expand --table
250 test_expect_success
'status with options' '
252 test_sparse_match ls &&
253 test_all_match git status --porcelain=v2 &&
254 test_all_match git status --porcelain=v2 -z -u &&
255 test_all_match git status --porcelain=v2 -uno &&
256 run_on_all touch README.md &&
257 test_all_match git status --porcelain=v2 &&
258 test_all_match git status --porcelain=v2 -z -u &&
259 test_all_match git status --porcelain=v2 -uno &&
260 test_all_match git add README.md &&
261 test_all_match git status --porcelain=v2 &&
262 test_all_match git status --porcelain=v2 -z -u &&
263 test_all_match git status --porcelain=v2 -uno
266 test_expect_success
'status reports sparse-checkout' '
268 git -C sparse-checkout status >full &&
269 git -C sparse-index status >sparse &&
270 test_i18ngrep "You are in a sparse checkout with " full &&
271 test_i18ngrep "You are in a sparse checkout." sparse
274 test_expect_success
'add, commit, checkout' '
277 write_script edit-contents <<-\EOF &&
280 run_on_all ../edit-contents README.md &&
282 test_all_match git add README.md &&
283 test_all_match git status --porcelain=v2 &&
284 test_all_match git commit -m "Add README.md" &&
286 test_all_match git checkout HEAD~1 &&
287 test_all_match git checkout - &&
289 run_on_all ../edit-contents README.md &&
291 test_all_match git add -A &&
292 test_all_match git status --porcelain=v2 &&
293 test_all_match git commit -m "Extend README.md" &&
295 test_all_match git checkout HEAD~1 &&
296 test_all_match git checkout - &&
298 run_on_all ../edit-contents deep/newfile &&
300 test_all_match git status --porcelain=v2 -uno &&
301 test_all_match git status --porcelain=v2 &&
302 test_all_match git add . &&
303 test_all_match git status --porcelain=v2 &&
304 test_all_match git commit -m "add deep/newfile" &&
306 test_all_match git checkout HEAD~1 &&
307 test_all_match git checkout -
310 test_expect_success
'deep changes during checkout' '
313 test_sparse_match git sparse-checkout set deep/deeper1/deepest &&
314 test_all_match git checkout deepest &&
315 test_all_match git checkout base
318 test_expect_success
'add outside sparse cone' '
321 run_on_sparse mkdir folder1 &&
322 run_on_sparse ../edit-contents folder1/a &&
323 run_on_sparse ../edit-contents folder1/newfile &&
324 test_sparse_match test_must_fail git add folder1/a &&
325 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
326 test_sparse_unstaged folder1/a &&
327 test_sparse_match test_must_fail git add folder1/newfile &&
328 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
329 test_sparse_unstaged folder1/newfile
332 test_expect_success
'commit including unstaged changes' '
335 write_script edit-file <<-\EOF &&
339 run_on_all ../edit-file 1 a &&
340 run_on_all ../edit-file 1 deep/a &&
342 test_all_match git commit -m "-a" -a &&
343 test_all_match git status --porcelain=v2 &&
345 run_on_all ../edit-file 2 a &&
346 run_on_all ../edit-file 2 deep/a &&
348 test_all_match git commit -m "--include" --include deep/a &&
349 test_all_match git status --porcelain=v2 &&
350 test_all_match git commit -m "--include" --include a &&
351 test_all_match git status --porcelain=v2 &&
353 run_on_all ../edit-file 3 a &&
354 run_on_all ../edit-file 3 deep/a &&
356 test_all_match git commit -m "--amend" -a --amend &&
357 test_all_match git status --porcelain=v2
360 test_expect_success
'status/add: outside sparse cone' '
363 # folder1 is at HEAD, but outside the sparse cone
364 run_on_sparse mkdir folder1 &&
365 cp initial-repo/folder1/a sparse-checkout/folder1/a &&
366 cp initial-repo/folder1/a sparse-index/folder1/a &&
368 test_sparse_match git status &&
370 write_script edit-contents <<-\EOF &&
373 run_on_sparse ../edit-contents folder1/a &&
374 run_on_all ../edit-contents folder1/new &&
376 test_sparse_match git status --porcelain=v2 &&
378 # Adding the path outside of the sparse-checkout cone should fail.
379 test_sparse_match test_must_fail git add 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 --refresh folder1/a &&
383 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
384 test_sparse_unstaged folder1/a &&
385 test_sparse_match test_must_fail git add folder1/new &&
386 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
387 test_sparse_unstaged folder1/new &&
388 test_sparse_match git add --sparse folder1/a &&
389 test_sparse_match git add --sparse folder1/new &&
391 test_all_match git add --sparse . &&
392 test_all_match git status --porcelain=v2 &&
393 test_all_match git commit -m folder1/new &&
394 test_all_match git rev-parse HEAD^{tree} &&
396 run_on_all ../edit-contents folder1/newer &&
397 test_all_match git add --sparse folder1/ &&
398 test_all_match git status --porcelain=v2 &&
399 test_all_match git commit -m folder1/newer &&
400 test_all_match git rev-parse HEAD^{tree}
403 test_expect_success
'checkout and reset --hard' '
406 test_all_match git checkout update-folder1 &&
407 test_all_match git status --porcelain=v2 &&
409 test_all_match git checkout update-deep &&
410 test_all_match git status --porcelain=v2 &&
412 test_all_match git checkout -b reset-test &&
413 test_all_match git reset --hard deepest &&
414 test_all_match git reset --hard update-folder1 &&
415 test_all_match git reset --hard update-folder2
418 test_expect_success
'diff --cached' '
421 write_script edit-contents <<-\EOF &&
422 echo text >>README.md
424 run_on_all ../edit-contents &&
426 test_all_match git diff &&
427 test_all_match git diff --cached &&
428 test_all_match git add README.md &&
429 test_all_match git diff &&
430 test_all_match git diff --cached
433 # NEEDSWORK: sparse-checkout behaves differently from full-checkout when
434 # running this test with 'df-conflict-2' after 'df-conflict-1'.
435 test_expect_success
'diff with renames and conflicts' '
438 for branch in rename-out-to-out \
444 test_all_match git checkout rename-base &&
445 test_all_match git checkout $branch -- . &&
446 test_all_match git status --porcelain=v2 &&
447 test_all_match git diff --cached --no-renames &&
448 test_all_match git diff --cached --find-renames || return 1
452 test_expect_success
'diff with directory/file conflicts' '
455 for branch in rename-out-to-out \
462 git -C full-checkout reset --hard &&
463 test_sparse_match git reset --hard &&
464 test_all_match git checkout $branch &&
465 test_all_match git checkout rename-base -- . &&
466 test_all_match git status --porcelain=v2 &&
467 test_all_match git diff --cached --no-renames &&
468 test_all_match git diff --cached --find-renames || return 1
472 test_expect_success
'log with pathspec outside sparse definition' '
475 test_all_match git log -- a &&
476 test_all_match git log -- folder1/a &&
477 test_all_match git log -- folder2/a &&
478 test_all_match git log -- deep/a &&
479 test_all_match git log -- deep/deeper1/a &&
480 test_all_match git log -- deep/deeper1/deepest/a &&
482 test_all_match git checkout update-folder1 &&
483 test_all_match git log -- folder1/a
486 test_expect_success
'blame with pathspec inside sparse definition' '
492 deep/deeper1/deepest/a
494 test_all_match git blame $file
498 # Without a revision specified, blame will error if passed any file that
499 # is not present in the working directory (even if the file is tracked).
500 # Here we just verify that this is also true with sparse checkouts.
501 test_expect_success
'blame with pathspec outside sparse definition' '
503 test_sparse_match git sparse-checkout set &&
508 deep/deeper1/deepest/a
510 test_sparse_match test_must_fail git blame $file &&
511 cat >expect <<-EOF &&
512 fatal: Cannot lstat '"'"'$file'"'"': No such file or directory
514 # We compare sparse-checkout-err and sparse-index-err in
515 # `test_sparse_match`. Given we know they are the same, we
516 # only check the content of sparse-index-err here.
517 test_cmp expect sparse-index-err
521 test_expect_success
'checkout and reset (mixed)' '
524 test_all_match git checkout -b reset-test update-deep &&
525 test_all_match git reset deepest &&
527 # Because skip-worktree is preserved, resetting to update-folder1
528 # will show worktree changes for folder1/a in full-checkout, but not
529 # in sparse-checkout or sparse-index.
530 git -C full-checkout reset update-folder1 >full-checkout-out &&
531 test_sparse_match git reset update-folder1 &&
532 grep "M folder1/a" full-checkout-out &&
533 ! grep "M folder1/a" sparse-checkout-out &&
534 run_on_sparse test_path_is_missing folder1
537 test_expect_success
'checkout and reset (merge)' '
540 write_script edit-contents <<-\EOF &&
544 test_all_match git checkout -b reset-test update-deep &&
545 run_on_all ../edit-contents a &&
546 test_all_match git reset --merge deepest &&
547 test_all_match git status --porcelain=v2 &&
549 test_all_match git reset --hard update-deep &&
550 run_on_all ../edit-contents deep/a &&
551 test_all_match test_must_fail git reset --merge deepest
554 test_expect_success
'checkout and reset (keep)' '
557 write_script edit-contents <<-\EOF &&
561 test_all_match git checkout -b reset-test update-deep &&
562 run_on_all ../edit-contents a &&
563 test_all_match git reset --keep deepest &&
564 test_all_match git status --porcelain=v2 &&
566 test_all_match git reset --hard update-deep &&
567 run_on_all ../edit-contents deep/a &&
568 test_all_match test_must_fail git reset --keep deepest
571 test_expect_success
'reset with pathspecs inside sparse definition' '
574 write_script edit-contents <<-\EOF &&
578 test_all_match git checkout -b reset-test update-deep &&
579 run_on_all ../edit-contents deep/a &&
581 test_all_match git reset base -- deep/a &&
582 test_all_match git status --porcelain=v2 &&
584 test_all_match git reset base -- nonexistent-file &&
585 test_all_match git status --porcelain=v2 &&
587 test_all_match git reset deepest -- deep &&
588 test_all_match git status --porcelain=v2
591 # Although the working tree differs between full and sparse checkouts after
592 # reset, the state of the index is the same.
593 test_expect_success
'reset with pathspecs outside sparse definition' '
595 test_all_match git checkout -b reset-test base &&
597 test_sparse_match git reset update-folder1 -- folder1 &&
598 git -C full-checkout reset update-folder1 -- folder1 &&
599 test_sparse_match git status --porcelain=v2 &&
600 test_all_match git rev-parse HEAD:folder1 &&
602 test_sparse_match git reset update-folder2 -- folder2/a &&
603 git -C full-checkout reset update-folder2 -- folder2/a &&
604 test_sparse_match git status --porcelain=v2 &&
605 test_all_match git rev-parse HEAD:folder2/a
608 test_expect_success
'reset with wildcard pathspec' '
611 test_all_match git reset update-deep -- deep\* &&
612 test_all_match git ls-files -s -- deep &&
614 test_all_match git reset deepest -- deep\*\*\* &&
615 test_all_match git ls-files -s -- deep &&
617 # The following `git reset`s result in updating the index on files with
618 # `skip-worktree` enabled. To avoid failing due to discrepencies in reported
619 # "modified" files, `test_sparse_match` reset is performed separately from
620 # "full-checkout" reset, then the index contents of all repos are verified.
622 test_sparse_match git reset update-folder1 -- \*/a &&
623 git -C full-checkout reset update-folder1 -- \*/a &&
624 test_all_match git ls-files -s -- deep/a folder1/a &&
626 test_sparse_match git reset update-folder2 -- folder\* &&
627 git -C full-checkout reset update-folder2 -- folder\* &&
628 test_all_match git ls-files -s -- folder10 folder1 folder2 &&
630 test_sparse_match git reset base -- folder1/\* &&
631 git -C full-checkout reset base -- folder1/\* &&
632 test_all_match git ls-files -s -- folder1
635 test_expect_success
'merge, cherry-pick, and rebase' '
638 for OPERATION in "merge -m merge" cherry-pick "rebase --apply" "rebase --merge"
640 test_all_match git checkout -B temp update-deep &&
641 test_all_match git $OPERATION update-folder1 &&
642 test_all_match git rev-parse HEAD^{tree} &&
643 test_all_match git $OPERATION update-folder2 &&
644 test_all_match git rev-parse HEAD^{tree} || return 1
648 test_expect_success
'merge with conflict outside cone' '
651 test_all_match git checkout -b merge-tip merge-left &&
652 test_all_match git status --porcelain=v2 &&
653 test_all_match test_must_fail git merge -m merge merge-right &&
654 test_all_match git status --porcelain=v2 &&
656 # Resolve the conflict in different ways:
657 # 1. Revert to the base
658 test_all_match git checkout base -- deep/deeper2/a &&
659 test_all_match git status --porcelain=v2 &&
661 # 2. Add the file with conflict markers
662 test_sparse_match test_must_fail git add folder1/a &&
663 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
664 test_sparse_unstaged folder1/a &&
665 test_all_match git add --sparse folder1/a &&
666 test_all_match git status --porcelain=v2 &&
668 # 3. Rename the file to another sparse filename and
669 # accept conflict markers as resolved content.
670 run_on_all mv folder2/a folder2/z &&
671 test_sparse_match test_must_fail git add folder2 &&
672 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
673 test_sparse_unstaged folder2/z &&
674 test_all_match git add --sparse folder2 &&
675 test_all_match git status --porcelain=v2 &&
677 test_all_match git merge --continue &&
678 test_all_match git status --porcelain=v2 &&
679 test_all_match git rev-parse HEAD^{tree}
682 test_expect_success
'cherry-pick/rebase with conflict outside cone' '
685 for OPERATION in cherry-pick rebase
687 test_all_match git checkout -B tip &&
688 test_all_match git reset --hard merge-left &&
689 test_all_match git status --porcelain=v2 &&
690 test_all_match test_must_fail git $OPERATION merge-right &&
691 test_all_match git status --porcelain=v2 &&
693 # Resolve the conflict in different ways:
694 # 1. Revert to the base
695 test_all_match git checkout base -- deep/deeper2/a &&
696 test_all_match git status --porcelain=v2 &&
698 # 2. Add the file with conflict markers
699 # NEEDSWORK: Even though the merge conflict removed the
700 # SKIP_WORKTREE bit from the index entry for folder1/a, we should
701 # warn that this is a problematic add.
702 test_sparse_match test_must_fail git add folder1/a &&
703 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
704 test_sparse_unstaged folder1/a &&
705 test_all_match git add --sparse folder1/a &&
706 test_all_match git status --porcelain=v2 &&
708 # 3. Rename the file to another sparse filename and
709 # accept conflict markers as resolved content.
710 # NEEDSWORK: This mode now fails, because folder2/z is
711 # outside of the sparse-checkout cone and does not match an
712 # existing index entry with the SKIP_WORKTREE bit cleared.
713 run_on_all mv folder2/a folder2/z &&
714 test_sparse_match test_must_fail git add folder2 &&
715 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
716 test_sparse_unstaged folder2/z &&
717 test_all_match git add --sparse folder2 &&
718 test_all_match git status --porcelain=v2 &&
720 test_all_match git $OPERATION --continue &&
721 test_all_match git status --porcelain=v2 &&
722 test_all_match git rev-parse HEAD^{tree} || return 1
726 test_expect_success
'merge with outside renames' '
729 for type in out-to-out out-to-in in-to-out
731 test_all_match git reset --hard &&
732 test_all_match git checkout -f -b merge-$type update-deep &&
733 test_all_match git merge -m "$type" rename-$type &&
734 test_all_match git rev-parse HEAD^{tree} || return 1
738 # Sparse-index fails to convert the index in the
739 # final 'git cherry-pick' command.
740 test_expect_success
'cherry-pick with conflicts' '
743 write_script edit-conflict <<-\EOF &&
747 test_all_match git checkout -b to-cherry-pick &&
748 run_on_all ../edit-conflict ABC &&
749 test_all_match git add conflict &&
750 test_all_match git commit -m "conflict to pick" &&
752 test_all_match git checkout -B base HEAD~1 &&
753 run_on_all ../edit-conflict DEF &&
754 test_all_match git add conflict &&
755 test_all_match git commit -m "conflict in base" &&
757 test_all_match test_must_fail git cherry-pick to-cherry-pick
760 test_expect_success
'clean' '
763 echo bogus >>.gitignore &&
764 run_on_all cp ../.gitignore . &&
765 test_all_match git add .gitignore &&
766 test_all_match git commit -m "ignore bogus files" &&
768 run_on_sparse mkdir folder1 &&
769 run_on_all touch folder1/bogus &&
771 test_all_match git status --porcelain=v2 &&
772 test_all_match git clean -f &&
773 test_all_match git status --porcelain=v2 &&
774 test_sparse_match ls &&
775 test_sparse_match ls folder1 &&
777 test_all_match git clean -xf &&
778 test_all_match git status --porcelain=v2 &&
779 test_sparse_match ls &&
780 test_sparse_match ls folder1 &&
782 test_all_match git clean -xdf &&
783 test_all_match git status --porcelain=v2 &&
784 test_sparse_match ls &&
785 test_sparse_match ls folder1 &&
787 test_sparse_match test_path_is_dir folder1
790 test_expect_success
'submodule handling' '
793 test_sparse_match git sparse-checkout add modules &&
794 test_all_match mkdir modules &&
795 test_all_match touch modules/a &&
796 test_all_match git add modules &&
797 test_all_match git commit -m "add modules directory" &&
799 run_on_all git submodule add "$(pwd)/initial-repo" modules/sub &&
800 test_all_match git commit -m "add submodule" &&
802 # having a submodule prevents "modules" from collapse
803 test_sparse_match git sparse-checkout set deep/deeper1 &&
804 test-tool -C sparse-index read-cache --table >cache &&
805 grep "100644 blob .* modules/a" cache &&
806 grep "160000 commit $(git -C initial-repo rev-parse HEAD) modules/sub" cache
809 # When working with a sparse index, some commands will need to expand the
810 # index to operate properly. If those commands also write the index back
811 # to disk, they need to convert the index to sparse before writing.
812 # This test verifies that both of these events are logged in trace2 logs.
813 test_expect_success
'sparse-index is expanded and converted back' '
816 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
817 git -C sparse-index reset -- folder1/a &&
818 test_region index convert_to_sparse trace2.txt &&
819 test_region index ensure_full_index trace2.txt &&
821 # ls-files expands on read, but does not write.
823 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
824 git -C sparse-index ls-files &&
825 test_region index ensure_full_index trace2.txt
828 test_expect_success
'index.sparse disabled inline uses full index' '
831 # When index.sparse is disabled inline with `git status`, the
832 # index is expanded at the beginning of the execution then never
833 # converted back to sparse. It is then written to disk as a full index.
835 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
836 git -C sparse-index -c index.sparse=false status &&
837 ! test_region index convert_to_sparse trace2.txt &&
838 test_region index ensure_full_index trace2.txt &&
840 # Since index.sparse is set to true at a repo level, the index
841 # is converted from full to sparse when read, then never expanded
842 # over the course of `git status`. It is written to disk as a sparse
845 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
846 git -C sparse-index status &&
847 test_region index convert_to_sparse trace2.txt &&
848 ! test_region index ensure_full_index trace2.txt &&
850 # Now that the index has been written to disk as sparse, it is not
851 # converted to sparse (or expanded to full) when read by `git status`.
853 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
854 git -C sparse-index status &&
855 ! test_region index convert_to_sparse trace2.txt &&
856 ! test_region index ensure_full_index trace2.txt
859 ensure_not_expanded
() {
861 echo >>sparse-index
/untracked.txt
&&
867 GIT_TRACE2_EVENT
="$(pwd)/trace2.txt" \
868 git
-C sparse-index
"$@" ||
return 1
870 GIT_TRACE2_EVENT
="$(pwd)/trace2.txt" \
871 git
-C sparse-index
"$@" ||
return 1
873 test_region
! index ensure_full_index trace2.txt
876 test_expect_success
'sparse-index is not expanded' '
879 ensure_not_expanded status &&
880 ensure_not_expanded ls-files --sparse &&
881 ensure_not_expanded commit --allow-empty -m empty &&
882 echo >>sparse-index/a &&
883 ensure_not_expanded commit -a -m a &&
884 echo >>sparse-index/a &&
885 ensure_not_expanded commit --include a -m a &&
886 echo >>sparse-index/deep/deeper1/a &&
887 ensure_not_expanded commit --include deep/deeper1/a -m deeper &&
888 ensure_not_expanded checkout rename-out-to-out &&
889 ensure_not_expanded checkout - &&
890 ensure_not_expanded switch rename-out-to-out &&
891 ensure_not_expanded switch - &&
892 ensure_not_expanded reset --hard &&
893 ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
894 ensure_not_expanded reset --hard &&
895 ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
897 echo >>sparse-index/README.md &&
898 ensure_not_expanded add -A &&
899 echo >>sparse-index/extra.txt &&
900 ensure_not_expanded add extra.txt &&
901 echo >>sparse-index/untracked.txt &&
902 ensure_not_expanded add . &&
904 for ref in update-deep update-folder1 update-folder2 update-deep
906 echo >>sparse-index/README.md &&
907 ensure_not_expanded reset --hard $ref || return 1
910 ensure_not_expanded reset --mixed base &&
911 ensure_not_expanded reset --hard update-deep &&
912 ensure_not_expanded reset --keep base &&
913 ensure_not_expanded reset --merge update-deep &&
914 ensure_not_expanded reset --hard &&
916 ensure_not_expanded reset base -- deep/a &&
917 ensure_not_expanded reset base -- nonexistent-file &&
918 ensure_not_expanded reset deepest -- deep &&
920 # Although folder1 is outside the sparse definition, it exists as a
921 # directory entry in the index, so the pathspec will not force the
922 # index to be expanded.
923 ensure_not_expanded reset deepest -- folder1 &&
924 ensure_not_expanded reset deepest -- folder1/ &&
926 # Wildcard identifies only in-cone files, no index expansion
927 ensure_not_expanded reset deepest -- deep/\* &&
929 # Wildcard identifies only full sparse directories, no index expansion
930 ensure_not_expanded reset deepest -- folder\* &&
932 ensure_not_expanded checkout -f update-deep &&
933 test_config -C sparse-index pull.twohead ort &&
935 sane_unset GIT_TEST_MERGE_ALGORITHM &&
936 for OPERATION in "merge -m merge" cherry-pick rebase
938 ensure_not_expanded merge -m merge update-folder1 &&
939 ensure_not_expanded merge -m merge update-folder2 || return 1
944 test_expect_success
'sparse-index is not expanded: merge conflict in cone' '
947 for side in right left
949 git -C sparse-index checkout -b expand-$side base &&
950 echo $side >sparse-index/deep/a &&
951 git -C sparse-index commit -a -m "$side" || return 1
955 sane_unset GIT_TEST_MERGE_ALGORITHM &&
956 git -C sparse-index config pull.twohead ort &&
957 ensure_not_expanded ! merge -m merged expand-right
961 test_expect_success
'sparse index is not expanded: diff' '
964 write_script edit-contents <<-\EOF &&
968 # Add file within cone
969 test_sparse_match git sparse-checkout set deep &&
970 run_on_all ../edit-contents deep/testfile &&
971 test_all_match git add deep/testfile &&
972 run_on_all ../edit-contents deep/testfile &&
974 test_all_match git diff &&
975 test_all_match git diff --cached &&
976 ensure_not_expanded diff &&
977 ensure_not_expanded diff --cached &&
979 # Add file outside cone
980 test_all_match git reset --hard &&
981 run_on_all mkdir newdirectory &&
982 run_on_all ../edit-contents newdirectory/testfile &&
983 test_sparse_match git sparse-checkout set newdirectory &&
984 test_all_match git add newdirectory/testfile &&
985 run_on_all ../edit-contents newdirectory/testfile &&
986 test_sparse_match git sparse-checkout set &&
988 test_all_match git diff &&
989 test_all_match git diff --cached &&
990 ensure_not_expanded diff &&
991 ensure_not_expanded diff --cached &&
993 # Merge conflict outside cone
994 # The sparse checkout will report a warning that is not in the
995 # full checkout, so we use `run_on_all` instead of
997 run_on_all git reset --hard &&
998 test_all_match git checkout merge-left &&
999 test_all_match test_must_fail git merge merge-right &&
1001 test_all_match git diff &&
1002 test_all_match git diff --cached &&
1003 ensure_not_expanded diff &&
1004 ensure_not_expanded diff --cached
1007 test_expect_success
'sparse index is not expanded: blame' '
1013 deep/deeper1/deepest/a
1015 ensure_not_expanded blame $file
1019 test_expect_success
'sparse index is not expanded: fetch/pull' '
1022 git -C sparse-index remote add full "file://$(pwd)/full-checkout" &&
1023 ensure_not_expanded fetch full &&
1024 git -C full-checkout commit --allow-empty -m "for pull merge" &&
1025 git -C sparse-index commit --allow-empty -m "for pull merge" &&
1026 ensure_not_expanded pull full base
1029 test_expect_success
'ls-files' '
1032 # Use a smaller sparse-checkout for reduced output
1033 test_sparse_match git sparse-checkout set &&
1035 # Behavior agrees by default. Sparse index is expanded.
1036 test_all_match git ls-files &&
1038 # With --sparse, the sparse index data changes behavior.
1039 git -C sparse-index ls-files --sparse >actual &&
1041 cat >expect <<-\EOF &&
1055 test_cmp expect actual &&
1057 # With --sparse and no sparse index, nothing changes.
1058 git -C sparse-checkout ls-files >dense &&
1059 git -C sparse-checkout ls-files --sparse >sparse &&
1060 test_cmp dense sparse &&
1062 # Set up a strange condition of having a file edit
1063 # outside of the sparse-checkout cone. This is just
1064 # to verify that sparse-checkout and sparse-index
1065 # behave the same in this case.
1066 write_script edit-content <<-\EOF &&
1068 echo content >>folder1/a
1070 run_on_sparse ../edit-content &&
1072 # ls-files does not currently notice modified files whose
1073 # cache entries are marked SKIP_WORKTREE. This may change
1074 # in the future, but here we test that sparse index does
1075 # not accidentally create a change of behavior.
1076 test_sparse_match git ls-files --modified &&
1077 test_must_be_empty sparse-checkout-out &&
1078 test_must_be_empty sparse-index-out &&
1080 git -C sparse-index ls-files --sparse --modified >sparse-index-out &&
1081 test_must_be_empty sparse-index-out &&
1083 # Add folder1 to the sparse-checkout cone and
1084 # check that ls-files shows the expanded files.
1085 test_sparse_match git sparse-checkout add folder1 &&
1086 test_sparse_match git ls-files --modified &&
1088 test_all_match git ls-files &&
1089 git -C sparse-index ls-files --sparse >actual &&
1091 cat >expect <<-\EOF &&
1107 test_cmp expect actual &&
1109 # Double-check index expansion is avoided
1110 ensure_not_expanded ls-files --sparse
1113 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
1114 # in this scenario, but it shouldn't.
1115 test_expect_success
'reset mixed and checkout orphan' '
1118 test_all_match git checkout rename-out-to-in &&
1120 # Sparse checkouts do not agree with full checkouts about
1121 # how to report a directory/file conflict during a reset.
1122 # This command would fail with test_all_match because the
1123 # full checkout reports "T folder1/0/1" while a sparse
1124 # checkout reports "D folder1/0/1". This matches because
1125 # the sparse checkouts skip "adding" the other side of
1127 test_sparse_match git reset --mixed HEAD~1 &&
1128 test_sparse_match test-tool read-cache --table --expand &&
1129 test_sparse_match git status --porcelain=v2 &&
1131 # At this point, sparse-checkouts behave differently
1132 # from the full-checkout.
1133 test_sparse_match git checkout --orphan new-branch &&
1134 test_sparse_match test-tool read-cache --table --expand &&
1135 test_sparse_match git status --porcelain=v2
1138 test_expect_success
'add everything with deep new file' '
1141 run_on_sparse git sparse-checkout set deep/deeper1/deepest &&
1143 run_on_all touch deep/deeper1/x &&
1144 test_all_match git add . &&
1145 test_all_match git status --porcelain=v2
1148 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1149 # directory/file conflicts, even without sparse-checkout. Use this
1150 # test only as a documentation of the incorrect behavior, not a
1151 # measure of how it _should_ behave.
1152 test_expect_success
'checkout behaves oddly with df-conflict-1' '
1155 test_sparse_match git sparse-checkout disable &&
1157 write_script edit-content <<-\EOF &&
1158 echo content >>folder1/larger-content
1162 run_on_all ../edit-content &&
1163 test_all_match git status --porcelain=v2 &&
1165 git -C sparse-checkout sparse-checkout init --cone &&
1166 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1168 test_all_match git status --porcelain=v2 &&
1170 # This checkout command should fail, because we have a staged
1171 # change to folder1/larger-content, but the destination changes
1172 # folder1 to a file.
1173 git -C full-checkout checkout df-conflict-1 \
1174 1>full-checkout-out \
1175 2>full-checkout-err &&
1176 git -C sparse-checkout checkout df-conflict-1 \
1177 1>sparse-checkout-out \
1178 2>sparse-checkout-err &&
1179 git -C sparse-index checkout df-conflict-1 \
1180 1>sparse-index-out \
1181 2>sparse-index-err &&
1183 # Instead, the checkout deletes the folder1 file and adds the
1184 # folder1/larger-content file, leaving all other paths that were
1185 # in folder1/ as deleted (without any warning).
1186 cat >expect <<-EOF &&
1188 A folder1/larger-content
1190 test_cmp expect full-checkout-out &&
1191 test_cmp expect sparse-checkout-out &&
1193 # The sparse-index reports no output
1194 test_must_be_empty sparse-index-out &&
1196 # stderr: Switched to branch df-conflict-1
1197 test_cmp full-checkout-err sparse-checkout-err &&
1198 test_cmp full-checkout-err sparse-checkout-err
1201 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1202 # directory/file conflicts, even without sparse-checkout. Use this
1203 # test only as a documentation of the incorrect behavior, not a
1204 # measure of how it _should_ behave.
1205 test_expect_success
'checkout behaves oddly with df-conflict-2' '
1208 test_sparse_match git sparse-checkout disable &&
1210 write_script edit-content <<-\EOF &&
1211 echo content >>folder2/larger-content
1215 run_on_all ../edit-content &&
1216 test_all_match git status --porcelain=v2 &&
1218 git -C sparse-checkout sparse-checkout init --cone &&
1219 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1221 test_all_match git status --porcelain=v2 &&
1223 # This checkout command should fail, because we have a staged
1224 # change to folder1/larger-content, but the destination changes
1225 # folder1 to a file.
1226 git -C full-checkout checkout df-conflict-2 \
1227 1>full-checkout-out \
1228 2>full-checkout-err &&
1229 git -C sparse-checkout checkout df-conflict-2 \
1230 1>sparse-checkout-out \
1231 2>sparse-checkout-err &&
1232 git -C sparse-index checkout df-conflict-2 \
1233 1>sparse-index-out \
1234 2>sparse-index-err &&
1236 # The full checkout deviates from the df-conflict-1 case here!
1237 # It drops the change to folder1/larger-content and leaves the
1238 # folder1 path as-is on disk. The sparse-index behaves the same.
1239 test_must_be_empty full-checkout-out &&
1240 test_must_be_empty sparse-index-out &&
1242 # In the sparse-checkout case, the checkout deletes the folder1
1243 # file and adds the folder1/larger-content file, leaving all other
1244 # paths that were in folder1/ as deleted (without any warning).
1245 cat >expect <<-EOF &&
1247 A folder2/larger-content
1249 test_cmp expect sparse-checkout-out &&
1251 # Switched to branch df-conflict-1
1252 test_cmp full-checkout-err sparse-checkout-err &&
1253 test_cmp full-checkout-err sparse-index-err