Sync with 2.36.6
[git/debian.git] / t / t1092-sparse-checkout-compatibility.sh
blobaee93c82c5dd62ae1b49b47a68f70efd251fc454
1 #!/bin/sh
3 test_description='compare full workdir to sparse workdir'
5 GIT_TEST_SPLIT_INDEX=0
6 GIT_TEST_SPARSE_INDEX=
8 . ./test-lib.sh
10 test_expect_success 'setup' '
11 git init initial-repo &&
13 GIT_TEST_SPARSE_INDEX=0 &&
14 cd initial-repo &&
15 echo a >a &&
16 echo "after deep" >e &&
17 echo "after folder1" >g &&
18 echo "after x" >z &&
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 &&
28 cp a folder1 &&
29 cp a folder2 &&
30 cp a x &&
31 cp a deep &&
32 cp a deep/before &&
33 cp a deep/deeper1 &&
34 cp a deep/deeper2 &&
35 cp a deep/later &&
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 &&
44 >folder1- &&
45 >folder1.x &&
46 >folder10 &&
47 cp -r deep/deeper1/0 folder1 &&
48 cp -r deep/deeper1/0 folder2 &&
49 echo >>folder1/0/0/0 &&
50 echo >>folder2/0/1 &&
51 git add . &&
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
59 done &&
61 git checkout -b rename-base base &&
62 cat >folder1/larger-content <<-\EOF &&
63 matching
64 lines
65 help
66 inexact
67 renames
68 EOF
69 cp folder1/larger-content folder2/ &&
70 cp folder1/larger-content deep/deeper1/ &&
71 git add . &&
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 &&
78 echo >>folder2/0/1 &&
79 echo stuff >>deep/deeper1/a &&
80 git add . &&
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 &&
86 rm folder2/0/1 &&
87 mkdir folder2/0/1 &&
88 echo >>folder2/0/1/1 &&
89 mv folder1/larger-content deep/deeper1/edited-content &&
90 echo >>deep/deeper1/edited-content &&
91 git add . &&
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 &&
96 echo >>folder2/0/1 &&
97 rm -rf folder1/0/0 &&
98 echo >>folder1/0/0 &&
99 mv deep/deeper1/larger-content folder1/edited-content &&
100 echo >>folder1/edited-content &&
101 git add . &&
102 git commit -m "rename deep/deeper1/... to folder1/..." &&
104 git checkout -b df-conflict-1 base &&
105 rm -rf folder1 &&
106 echo content >folder1 &&
107 git add . &&
108 git commit -m "dir to file" &&
110 git checkout -b df-conflict-2 base &&
111 rm -rf folder2 &&
112 echo content >folder2 &&
113 git add . &&
114 git commit -m "dir to file" &&
116 git checkout -b fd-conflict base &&
117 rm a &&
118 mkdir a &&
119 echo content >a/a &&
120 git add . &&
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 &&
129 git add . &&
130 git commit -m "$side" || return 1
131 done &&
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 &&
140 git reset --hard
144 init_repos () {
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 run_on_sparse () {
167 cd sparse-checkout &&
168 GIT_PROGRESS_DELAY=100000 "$@" >../sparse-checkout-out 2>../sparse-checkout-err
169 ) &&
171 cd sparse-index &&
172 GIT_PROGRESS_DELAY=100000 "$@" >../sparse-index-out 2>../sparse-index-err
176 run_on_all () {
178 cd full-checkout &&
179 GIT_PROGRESS_DELAY=100000 "$@" >../full-checkout-out 2>../full-checkout-err
180 ) &&
181 run_on_sparse "$@"
184 test_all_match () {
185 run_on_all "$@" &&
186 test_cmp full-checkout-out sparse-checkout-out &&
187 test_cmp full-checkout-out sparse-index-out &&
188 test_cmp full-checkout-err sparse-checkout-err &&
189 test_cmp full-checkout-err sparse-index-err
192 test_sparse_match () {
193 run_on_sparse "$@" &&
194 test_cmp sparse-checkout-out sparse-index-out &&
195 test_cmp sparse-checkout-err sparse-index-err
198 test_sparse_unstaged () {
199 file=$1 &&
200 for repo in sparse-checkout sparse-index
202 # Skip "unmerged" paths
203 git -C $repo diff --staged --diff-filter=u -- "$file" >diff &&
204 test_must_be_empty diff || return 1
205 done
208 # Usage: test_sprase_checkout_set "<c1> ... <cN>" "<s1> ... <sM>"
209 # Verifies that "git sparse-checkout set <c1> ... <cN>" succeeds and
210 # leaves the sparse index in a state where <s1> ... <sM> are sparse
211 # directories (and <c1> ... <cN> are not).
212 test_sparse_checkout_set () {
213 CONE_DIRS=$1 &&
214 SPARSE_DIRS=$2 &&
215 git -C sparse-index sparse-checkout set --skip-checks $CONE_DIRS &&
216 git -C sparse-index ls-files --sparse --stage >cache &&
218 # Check that the directories outside of the sparse-checkout cone
219 # have sparse directory entries.
220 for dir in $SPARSE_DIRS
222 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
223 grep "040000 $TREE 0 $dir/" cache \
224 || return 1
225 done &&
227 # Check that the directories in the sparse-checkout cone
228 # are not sparse directory entries.
229 for dir in $CONE_DIRS
231 # Allow TREE to not exist because
232 # $dir does not exist at HEAD.
233 TREE=$(git -C sparse-index rev-parse HEAD:$dir) ||
234 ! grep "040000 $TREE 0 $dir/" cache \
235 || return 1
236 done
239 test_expect_success 'sparse-index contents' '
240 init_repos &&
242 # Remove deep, add three other directories.
243 test_sparse_checkout_set \
244 "folder1 folder2 x" \
245 "before deep" &&
247 # Remove folder1, add deep
248 test_sparse_checkout_set \
249 "deep folder2 x" \
250 "before folder1" &&
252 # Replace deep with deep/deeper2 (dropping deep/deeper1)
253 # Add folder1
254 test_sparse_checkout_set \
255 "deep/deeper2 folder1 folder2 x" \
256 "before deep/deeper1" &&
258 # Replace deep/deeper2 with deep/deeper1
259 # Replace folder1 with folder1/0/0
260 # Replace folder2 with non-existent folder2/2/3
261 # Add non-existent "bogus"
262 test_sparse_checkout_set \
263 "bogus deep/deeper1 folder1/0/0 folder2/2/3 x" \
264 "before deep/deeper2 folder2/0" &&
266 # Drop down to only files at root
267 test_sparse_checkout_set \
268 "" \
269 "before deep folder1 folder2 x" &&
271 # Disabling the sparse-index replaces tree entries with full ones
272 git -C sparse-index sparse-checkout init --no-sparse-index &&
273 test_sparse_match git ls-files --stage --sparse
276 test_expect_success 'expanded in-memory index matches full index' '
277 init_repos &&
278 test_sparse_match git ls-files --stage
281 test_expect_success 'root directory cannot be sparse' '
282 init_repos &&
284 # Remove all in-cone files and directories from the index, collapse index
285 # with `git sparse-checkout reapply`
286 git -C sparse-index rm -r . &&
287 git -C sparse-index sparse-checkout reapply &&
289 # Verify sparse directories still present, root directory is not sparse
290 cat >expect <<-EOF &&
291 before/
292 folder1/
293 folder2/
296 git -C sparse-index ls-files --sparse >actual &&
297 test_cmp expect actual
300 test_expect_success 'status with options' '
301 init_repos &&
302 test_sparse_match ls &&
303 test_all_match git status --porcelain=v2 &&
304 test_all_match git status --porcelain=v2 -z -u &&
305 test_all_match git status --porcelain=v2 -uno &&
306 run_on_all touch README.md &&
307 test_all_match git status --porcelain=v2 &&
308 test_all_match git status --porcelain=v2 -z -u &&
309 test_all_match git status --porcelain=v2 -uno &&
310 test_all_match git add README.md &&
311 test_all_match git status --porcelain=v2 &&
312 test_all_match git status --porcelain=v2 -z -u &&
313 test_all_match git status --porcelain=v2 -uno
316 test_expect_success 'status with diff in unexpanded sparse directory' '
317 init_repos &&
318 test_all_match git checkout rename-base &&
319 test_all_match git reset --soft rename-out-to-out &&
320 test_all_match git status --porcelain=v2
323 test_expect_success 'status reports sparse-checkout' '
324 init_repos &&
325 git -C sparse-checkout status >full &&
326 git -C sparse-index status >sparse &&
327 test_i18ngrep "You are in a sparse checkout with " full &&
328 test_i18ngrep "You are in a sparse checkout." sparse
331 test_expect_success 'add, commit, checkout' '
332 init_repos &&
334 write_script edit-contents <<-\EOF &&
335 echo text >>$1
337 run_on_all ../edit-contents README.md &&
339 test_all_match git add README.md &&
340 test_all_match git status --porcelain=v2 &&
341 test_all_match git commit -m "Add README.md" &&
343 test_all_match git checkout HEAD~1 &&
344 test_all_match git checkout - &&
346 run_on_all ../edit-contents README.md &&
348 test_all_match git add -A &&
349 test_all_match git status --porcelain=v2 &&
350 test_all_match git commit -m "Extend README.md" &&
352 test_all_match git checkout HEAD~1 &&
353 test_all_match git checkout - &&
355 run_on_all ../edit-contents deep/newfile &&
357 test_all_match git status --porcelain=v2 -uno &&
358 test_all_match git status --porcelain=v2 &&
359 test_all_match git add . &&
360 test_all_match git status --porcelain=v2 &&
361 test_all_match git commit -m "add deep/newfile" &&
363 test_all_match git checkout HEAD~1 &&
364 test_all_match git checkout -
367 test_expect_success 'deep changes during checkout' '
368 init_repos &&
370 test_sparse_match git sparse-checkout set deep/deeper1/deepest &&
371 test_all_match git checkout deepest &&
372 test_all_match git checkout base
375 test_expect_success 'checkout with modified sparse directory' '
376 init_repos &&
378 test_all_match git checkout rename-in-to-out -- . &&
379 test_sparse_match git sparse-checkout reapply &&
380 test_all_match git checkout base
383 test_expect_success 'add outside sparse cone' '
384 init_repos &&
386 run_on_sparse mkdir folder1 &&
387 run_on_sparse ../edit-contents folder1/a &&
388 run_on_sparse ../edit-contents folder1/newfile &&
389 test_sparse_match test_must_fail git add folder1/a &&
390 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
391 test_sparse_unstaged folder1/a &&
392 test_sparse_match test_must_fail git add folder1/newfile &&
393 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
394 test_sparse_unstaged folder1/newfile
397 test_expect_success 'commit including unstaged changes' '
398 init_repos &&
400 write_script edit-file <<-\EOF &&
401 echo $1 >$2
404 run_on_all ../edit-file 1 a &&
405 run_on_all ../edit-file 1 deep/a &&
407 test_all_match git commit -m "-a" -a &&
408 test_all_match git status --porcelain=v2 &&
410 run_on_all ../edit-file 2 a &&
411 run_on_all ../edit-file 2 deep/a &&
413 test_all_match git commit -m "--include" --include deep/a &&
414 test_all_match git status --porcelain=v2 &&
415 test_all_match git commit -m "--include" --include a &&
416 test_all_match git status --porcelain=v2 &&
418 run_on_all ../edit-file 3 a &&
419 run_on_all ../edit-file 3 deep/a &&
421 test_all_match git commit -m "--amend" -a --amend &&
422 test_all_match git status --porcelain=v2
425 test_expect_success 'status/add: outside sparse cone' '
426 init_repos &&
428 # folder1 is at HEAD, but outside the sparse cone
429 run_on_sparse mkdir folder1 &&
430 cp initial-repo/folder1/a sparse-checkout/folder1/a &&
431 cp initial-repo/folder1/a sparse-index/folder1/a &&
433 test_sparse_match git status &&
435 write_script edit-contents <<-\EOF &&
436 echo text >>$1
438 run_on_all ../edit-contents folder1/a &&
439 run_on_all ../edit-contents folder1/new &&
441 test_sparse_match git status --porcelain=v2 &&
443 # Adding the path outside of the sparse-checkout cone should fail.
444 test_sparse_match test_must_fail git add folder1/a &&
445 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
446 test_sparse_unstaged folder1/a &&
447 test_all_match git add --refresh folder1/a &&
448 test_must_be_empty sparse-checkout-err &&
449 test_sparse_unstaged folder1/a &&
450 test_sparse_match test_must_fail git add folder1/new &&
451 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
452 test_sparse_unstaged folder1/new &&
453 test_sparse_match git add --sparse folder1/a &&
454 test_sparse_match git add --sparse folder1/new &&
456 test_all_match git add --sparse . &&
457 test_all_match git status --porcelain=v2 &&
458 test_all_match git commit -m folder1/new &&
459 test_all_match git rev-parse HEAD^{tree} &&
461 run_on_all ../edit-contents folder1/newer &&
462 test_all_match git add --sparse folder1/ &&
463 test_all_match git status --porcelain=v2 &&
464 test_all_match git commit -m folder1/newer &&
465 test_all_match git rev-parse HEAD^{tree}
468 test_expect_success 'checkout and reset --hard' '
469 init_repos &&
471 test_all_match git checkout update-folder1 &&
472 test_all_match git status --porcelain=v2 &&
474 test_all_match git checkout update-deep &&
475 test_all_match git status --porcelain=v2 &&
477 test_all_match git checkout -b reset-test &&
478 test_all_match git reset --hard deepest &&
479 test_all_match git reset --hard update-folder1 &&
480 test_all_match git reset --hard update-folder2
483 test_expect_success 'diff --cached' '
484 init_repos &&
486 write_script edit-contents <<-\EOF &&
487 echo text >>README.md
489 run_on_all ../edit-contents &&
491 test_all_match git diff &&
492 test_all_match git diff --cached &&
493 test_all_match git add README.md &&
494 test_all_match git diff &&
495 test_all_match git diff --cached
498 # NEEDSWORK: sparse-checkout behaves differently from full-checkout when
499 # running this test with 'df-conflict-2' after 'df-conflict-1'.
500 test_expect_success 'diff with renames and conflicts' '
501 init_repos &&
503 for branch in rename-out-to-out \
504 rename-out-to-in \
505 rename-in-to-out \
506 df-conflict-1 \
507 fd-conflict
509 test_all_match git checkout rename-base &&
510 test_all_match git checkout $branch -- . &&
511 test_all_match git status --porcelain=v2 &&
512 test_all_match git diff --cached --no-renames &&
513 test_all_match git diff --cached --find-renames || return 1
514 done
517 test_expect_success 'diff with directory/file conflicts' '
518 init_repos &&
520 for branch in rename-out-to-out \
521 rename-out-to-in \
522 rename-in-to-out \
523 df-conflict-1 \
524 df-conflict-2 \
525 fd-conflict
527 git -C full-checkout reset --hard &&
528 test_sparse_match git reset --hard &&
529 test_all_match git checkout $branch &&
530 test_all_match git checkout rename-base -- . &&
531 test_all_match git status --porcelain=v2 &&
532 test_all_match git diff --cached --no-renames &&
533 test_all_match git diff --cached --find-renames || return 1
534 done
537 test_expect_success 'log with pathspec outside sparse definition' '
538 init_repos &&
540 test_all_match git log -- a &&
541 test_all_match git log -- folder1/a &&
542 test_all_match git log -- folder2/a &&
543 test_all_match git log -- deep/a &&
544 test_all_match git log -- deep/deeper1/a &&
545 test_all_match git log -- deep/deeper1/deepest/a &&
547 test_all_match git checkout update-folder1 &&
548 test_all_match git log -- folder1/a
551 test_expect_success 'blame with pathspec inside sparse definition' '
552 init_repos &&
554 for file in a \
555 deep/a \
556 deep/deeper1/a \
557 deep/deeper1/deepest/a
559 test_all_match git blame $file
560 done
563 # Without a revision specified, blame will error if passed any file that
564 # is not present in the working directory (even if the file is tracked).
565 # Here we just verify that this is also true with sparse checkouts.
566 test_expect_success 'blame with pathspec outside sparse definition' '
567 init_repos &&
568 test_sparse_match git sparse-checkout set &&
570 for file in a \
571 deep/a \
572 deep/deeper1/a \
573 deep/deeper1/deepest/a
575 test_sparse_match test_must_fail git blame $file &&
576 cat >expect <<-EOF &&
577 fatal: Cannot lstat '"'"'$file'"'"': No such file or directory
579 # We compare sparse-checkout-err and sparse-index-err in
580 # `test_sparse_match`. Given we know they are the same, we
581 # only check the content of sparse-index-err here.
582 test_cmp expect sparse-index-err
583 done
586 test_expect_success 'checkout and reset (mixed)' '
587 init_repos &&
589 test_all_match git checkout -b reset-test update-deep &&
590 test_all_match git reset deepest &&
592 # Because skip-worktree is preserved, resetting to update-folder1
593 # will show worktree changes for folder1/a in full-checkout, but not
594 # in sparse-checkout or sparse-index.
595 git -C full-checkout reset update-folder1 >full-checkout-out &&
596 test_sparse_match git reset update-folder1 &&
597 grep "M folder1/a" full-checkout-out &&
598 ! grep "M folder1/a" sparse-checkout-out &&
599 run_on_sparse test_path_is_missing folder1
602 test_expect_success 'checkout and reset (merge)' '
603 init_repos &&
605 write_script edit-contents <<-\EOF &&
606 echo text >>$1
609 test_all_match git checkout -b reset-test update-deep &&
610 run_on_all ../edit-contents a &&
611 test_all_match git reset --merge deepest &&
612 test_all_match git status --porcelain=v2 &&
614 test_all_match git reset --hard update-deep &&
615 run_on_all ../edit-contents deep/a &&
616 test_all_match test_must_fail git reset --merge deepest
619 test_expect_success 'checkout and reset (keep)' '
620 init_repos &&
622 write_script edit-contents <<-\EOF &&
623 echo text >>$1
626 test_all_match git checkout -b reset-test update-deep &&
627 run_on_all ../edit-contents a &&
628 test_all_match git reset --keep deepest &&
629 test_all_match git status --porcelain=v2 &&
631 test_all_match git reset --hard update-deep &&
632 run_on_all ../edit-contents deep/a &&
633 test_all_match test_must_fail git reset --keep deepest
636 test_expect_success 'reset with pathspecs inside sparse definition' '
637 init_repos &&
639 write_script edit-contents <<-\EOF &&
640 echo text >>$1
643 test_all_match git checkout -b reset-test update-deep &&
644 run_on_all ../edit-contents deep/a &&
646 test_all_match git reset base -- deep/a &&
647 test_all_match git status --porcelain=v2 &&
649 test_all_match git reset base -- nonexistent-file &&
650 test_all_match git status --porcelain=v2 &&
652 test_all_match git reset deepest -- deep &&
653 test_all_match git status --porcelain=v2
656 # Although the working tree differs between full and sparse checkouts after
657 # reset, the state of the index is the same.
658 test_expect_success 'reset with pathspecs outside sparse definition' '
659 init_repos &&
660 test_all_match git checkout -b reset-test base &&
662 test_sparse_match git reset update-folder1 -- folder1 &&
663 git -C full-checkout reset update-folder1 -- folder1 &&
664 test_all_match git ls-files -s -- folder1 &&
666 test_sparse_match git reset update-folder2 -- folder2/a &&
667 git -C full-checkout reset update-folder2 -- folder2/a &&
668 test_all_match git ls-files -s -- folder2/a
671 test_expect_success 'reset with wildcard pathspec' '
672 init_repos &&
674 test_all_match git reset update-deep -- deep\* &&
675 test_all_match git ls-files -s -- deep &&
677 test_all_match git reset deepest -- deep\*\*\* &&
678 test_all_match git ls-files -s -- deep &&
680 # The following `git reset`s result in updating the index on files with
681 # `skip-worktree` enabled. To avoid failing due to discrepencies in reported
682 # "modified" files, `test_sparse_match` reset is performed separately from
683 # "full-checkout" reset, then the index contents of all repos are verified.
685 test_sparse_match git reset update-folder1 -- \*/a &&
686 git -C full-checkout reset update-folder1 -- \*/a &&
687 test_all_match git ls-files -s -- deep/a folder1/a &&
689 test_sparse_match git reset update-folder2 -- folder\* &&
690 git -C full-checkout reset update-folder2 -- folder\* &&
691 test_all_match git ls-files -s -- folder10 folder1 folder2 &&
693 test_sparse_match git reset base -- folder1/\* &&
694 git -C full-checkout reset base -- folder1/\* &&
695 test_all_match git ls-files -s -- folder1
698 test_expect_success 'reset hard with removed sparse dir' '
699 init_repos &&
701 run_on_all git rm -r --sparse folder1 &&
702 test_all_match git status --porcelain=v2 &&
704 test_all_match git reset --hard &&
705 test_all_match git status --porcelain=v2 &&
707 cat >expect <<-\EOF &&
708 folder1/
711 git -C sparse-index ls-files --sparse folder1 >out &&
712 test_cmp expect out
715 test_expect_success 'update-index modify outside sparse definition' '
716 init_repos &&
718 write_script edit-contents <<-\EOF &&
719 echo text >>$1
722 # Create & modify folder1/a
723 # Note that this setup is a manual way of reaching the erroneous
724 # condition in which a `skip-worktree` enabled, outside-of-cone file
725 # exists on disk. It is used here to ensure `update-index` is stable
726 # and behaves predictably if such a condition occurs.
727 run_on_sparse mkdir -p folder1 &&
728 run_on_sparse cp ../initial-repo/folder1/a folder1/a &&
729 run_on_all ../edit-contents folder1/a &&
731 # If file has skip-worktree enabled, but the file is present, it is
732 # treated the same as if skip-worktree is disabled
733 test_all_match git status --porcelain=v2 &&
734 test_all_match git update-index folder1/a &&
735 test_all_match git status --porcelain=v2 &&
737 # When skip-worktree is disabled (even on files outside sparse cone), file
738 # is updated in the index
739 test_sparse_match git update-index --no-skip-worktree folder1/a &&
740 test_all_match git status --porcelain=v2 &&
741 test_all_match git update-index folder1/a &&
742 test_all_match git status --porcelain=v2
745 test_expect_success 'update-index --add outside sparse definition' '
746 init_repos &&
748 write_script edit-contents <<-\EOF &&
749 echo text >>$1
752 # Create folder1, add new file
753 run_on_sparse mkdir -p folder1 &&
754 run_on_all ../edit-contents folder1/b &&
756 # The *untracked* out-of-cone file is added to the index because it does
757 # not have a `skip-worktree` bit to signal that it should be ignored
758 # (unlike in `git add`, which will fail due to the file being outside
759 # the sparse checkout definition).
760 test_all_match git update-index --add folder1/b &&
761 test_all_match git status --porcelain=v2
764 # NEEDSWORK: `--remove`, unlike the rest of `update-index`, does not ignore
765 # `skip-worktree` entries by default and will remove them from the index.
766 # The `--ignore-skip-worktree-entries` flag must be used in conjunction with
767 # `--remove` to ignore the `skip-worktree` entries and prevent their removal
768 # from the index.
769 test_expect_success 'update-index --remove outside sparse definition' '
770 init_repos &&
772 # When --ignore-skip-worktree-entries is _not_ specified:
773 # out-of-cone, not-on-disk files are removed from the index
774 test_sparse_match git update-index --remove folder1/a &&
775 cat >expect <<-EOF &&
776 D folder1/a
778 test_sparse_match git diff --cached --name-status &&
779 test_cmp expect sparse-checkout-out &&
781 # Reset the state
782 test_all_match git reset --hard &&
784 # When --ignore-skip-worktree-entries is specified, out-of-cone
785 # (skip-worktree) files are ignored
786 test_sparse_match git update-index --remove --ignore-skip-worktree-entries folder1/a &&
787 test_sparse_match git diff --cached --name-status &&
788 test_must_be_empty sparse-checkout-out &&
790 # Reset the state
791 test_all_match git reset --hard &&
793 # --force-remove supercedes --ignore-skip-worktree-entries, removing
794 # a skip-worktree file from the index (and disk) when both are specified
795 # with --remove
796 test_sparse_match git update-index --force-remove --ignore-skip-worktree-entries folder1/a &&
797 cat >expect <<-EOF &&
798 D folder1/a
800 test_sparse_match git diff --cached --name-status &&
801 test_cmp expect sparse-checkout-out
804 test_expect_success 'update-index with directories' '
805 init_repos &&
807 # update-index will exit silently when provided with a directory name
808 # containing a trailing slash
809 test_all_match git update-index deep/ folder1/ &&
810 grep "Ignoring path deep/" sparse-checkout-err &&
811 grep "Ignoring path folder1/" sparse-checkout-err &&
813 # When update-index is given a directory name WITHOUT a trailing slash, it will
814 # behave in different ways depending on the status of the directory on disk:
815 # * if it exists, the command exits with an error ("add individual files instead")
816 # * if it does NOT exist (e.g., in a sparse-checkout), it is assumed to be a
817 # file and either triggers an error ("does not exist and --remove not passed")
818 # or is ignored completely (when using --remove)
819 test_all_match test_must_fail git update-index deep &&
820 run_on_all test_must_fail git update-index folder1 &&
821 test_must_fail git -C full-checkout update-index --remove folder1 &&
822 test_sparse_match git update-index --remove folder1 &&
823 test_all_match git status --porcelain=v2
826 test_expect_success 'update-index --again file outside sparse definition' '
827 init_repos &&
829 test_all_match git checkout -b test-reupdate &&
831 # Update HEAD without modifying the index to introduce a difference in
832 # folder1/a
833 test_sparse_match git reset --soft update-folder1 &&
835 # Because folder1/a differs in the index vs HEAD,
836 # `git update-index --no-skip-worktree --again` will effectively perform
837 # `git update-index --no-skip-worktree folder1/a` and remove the skip-worktree
838 # flag from folder1/a
839 test_sparse_match git update-index --no-skip-worktree --again &&
840 test_sparse_match git status --porcelain=v2 &&
842 cat >expect <<-EOF &&
843 D folder1/a
845 test_sparse_match git diff --name-status &&
846 test_cmp expect sparse-checkout-out
849 test_expect_success 'update-index --cacheinfo' '
850 init_repos &&
852 deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
853 folder2_oid=$(git -C full-checkout rev-parse update-folder2:folder2) &&
854 folder1_a_oid=$(git -C full-checkout rev-parse update-folder1:folder1/a) &&
856 test_all_match git update-index --cacheinfo 100644 $deep_a_oid deep/a &&
857 test_all_match git status --porcelain=v2 &&
859 # Cannot add sparse directory, even in sparse index case
860 test_all_match test_must_fail git update-index --add --cacheinfo 040000 $folder2_oid folder2/ &&
862 # Sparse match only: the new outside-of-cone entry is added *without* skip-worktree,
863 # so `git status` reports it as "deleted" in the worktree
864 test_sparse_match git update-index --add --cacheinfo 100644 $folder1_a_oid folder1/a &&
865 test_sparse_match git status --porcelain=v2 &&
866 cat >expect <<-EOF &&
867 MD folder1/a
869 test_sparse_match git status --short -- folder1/a &&
870 test_cmp expect sparse-checkout-out &&
872 # To return folder1/a to "normal" for a sparse checkout (ignored &
873 # outside-of-cone), add the skip-worktree flag.
874 test_sparse_match git update-index --skip-worktree folder1/a &&
875 cat >expect <<-EOF &&
876 S folder1/a
878 test_sparse_match git ls-files -t -- folder1/a &&
879 test_cmp expect sparse-checkout-out
882 for MERGE_TREES in "base HEAD update-folder2" \
883 "update-folder1 update-folder2" \
884 "update-folder2"
886 test_expect_success "'read-tree -mu $MERGE_TREES' with files outside sparse definition" '
887 init_repos &&
889 # Although the index matches, without --no-sparse-checkout, outside-of-
890 # definition files will not exist on disk for sparse checkouts
891 test_all_match git read-tree -mu $MERGE_TREES &&
892 test_all_match git status --porcelain=v2 &&
893 test_path_is_missing sparse-checkout/folder2 &&
894 test_path_is_missing sparse-index/folder2 &&
896 test_all_match git read-tree --reset -u HEAD &&
897 test_all_match git status --porcelain=v2 &&
899 test_all_match git read-tree -mu --no-sparse-checkout $MERGE_TREES &&
900 test_all_match git status --porcelain=v2 &&
901 test_cmp sparse-checkout/folder2/a sparse-index/folder2/a &&
902 test_cmp sparse-checkout/folder2/a full-checkout/folder2/a
905 done
907 test_expect_success 'read-tree --merge with edit/edit conflicts in sparse directories' '
908 init_repos &&
910 # Merge of multiple changes to same directory (but not same files) should
911 # succeed
912 test_all_match git read-tree -mu base rename-base update-folder1 &&
913 test_all_match git status --porcelain=v2 &&
915 test_all_match git reset --hard &&
917 test_all_match git read-tree -mu rename-base update-folder2 &&
918 test_all_match git status --porcelain=v2 &&
920 test_all_match git reset --hard &&
922 test_all_match test_must_fail git read-tree -mu base update-folder1 rename-out-to-in &&
923 test_all_match test_must_fail git read-tree -mu rename-out-to-in update-folder1
926 test_expect_success 'read-tree --prefix' '
927 init_repos &&
929 # If files differing between the index and target <commit-ish> exist
930 # inside the prefix, `read-tree --prefix` should fail
931 test_all_match test_must_fail git read-tree --prefix=deep/ deepest &&
932 test_all_match test_must_fail git read-tree --prefix=folder1/ update-folder1 &&
934 # If no differing index entries exist matching the prefix,
935 # `read-tree --prefix` updates the index successfully
936 test_all_match git rm -rf deep/deeper1/deepest/ &&
937 test_all_match git read-tree --prefix=deep/deeper1/deepest -u deepest &&
938 test_all_match git status --porcelain=v2 &&
940 test_all_match git rm -rf --sparse folder1/ &&
941 test_all_match git read-tree --prefix=folder1/ -u update-folder1 &&
942 test_all_match git status --porcelain=v2 &&
944 test_all_match git rm -rf --sparse folder2/0 &&
945 test_all_match git read-tree --prefix=folder2/0/ -u rename-out-to-out &&
946 test_all_match git status --porcelain=v2
949 test_expect_success 'read-tree --merge with directory-file conflicts' '
950 init_repos &&
952 test_all_match git checkout -b test-branch rename-base &&
954 # Although the index matches, without --no-sparse-checkout, outside-of-
955 # definition files will not exist on disk for sparse checkouts
956 test_sparse_match git read-tree -mu rename-out-to-out &&
957 test_sparse_match git status --porcelain=v2 &&
958 test_path_is_missing sparse-checkout/folder2 &&
959 test_path_is_missing sparse-index/folder2 &&
961 test_sparse_match git read-tree --reset -u HEAD &&
962 test_sparse_match git status --porcelain=v2 &&
964 test_sparse_match git read-tree -mu --no-sparse-checkout rename-out-to-out &&
965 test_sparse_match git status --porcelain=v2 &&
966 test_cmp sparse-checkout/folder2/0/1 sparse-index/folder2/0/1
969 test_expect_success 'merge, cherry-pick, and rebase' '
970 init_repos &&
972 for OPERATION in "merge -m merge" cherry-pick "rebase --apply" "rebase --merge"
974 test_all_match git checkout -B temp update-deep &&
975 test_all_match git $OPERATION update-folder1 &&
976 test_all_match git rev-parse HEAD^{tree} &&
977 test_all_match git $OPERATION update-folder2 &&
978 test_all_match git rev-parse HEAD^{tree} || return 1
979 done
982 test_expect_success 'merge with conflict outside cone' '
983 init_repos &&
985 test_all_match git checkout -b merge-tip merge-left &&
986 test_all_match git status --porcelain=v2 &&
987 test_all_match test_must_fail git merge -m merge merge-right &&
988 test_all_match git status --porcelain=v2 &&
990 # Resolve the conflict in different ways:
991 # 1. Revert to the base
992 test_all_match git checkout base -- deep/deeper2/a &&
993 test_all_match git status --porcelain=v2 &&
995 # 2. Add the file with conflict markers
996 test_sparse_match test_must_fail git add folder1/a &&
997 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
998 test_sparse_unstaged folder1/a &&
999 test_all_match git add --sparse folder1/a &&
1000 test_all_match git status --porcelain=v2 &&
1002 # 3. Rename the file to another sparse filename and
1003 # accept conflict markers as resolved content.
1004 run_on_all mv folder2/a folder2/z &&
1005 test_sparse_match test_must_fail git add folder2 &&
1006 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1007 test_sparse_unstaged folder2/z &&
1008 test_all_match git add --sparse folder2 &&
1009 test_all_match git status --porcelain=v2 &&
1011 test_all_match git merge --continue &&
1012 test_all_match git status --porcelain=v2 &&
1013 test_all_match git rev-parse HEAD^{tree}
1016 test_expect_success 'cherry-pick/rebase with conflict outside cone' '
1017 init_repos &&
1019 for OPERATION in cherry-pick rebase
1021 test_all_match git checkout -B tip &&
1022 test_all_match git reset --hard merge-left &&
1023 test_all_match git status --porcelain=v2 &&
1024 test_all_match test_must_fail git $OPERATION merge-right &&
1025 test_all_match git status --porcelain=v2 &&
1027 # Resolve the conflict in different ways:
1028 # 1. Revert to the base
1029 test_all_match git checkout base -- deep/deeper2/a &&
1030 test_all_match git status --porcelain=v2 &&
1032 # 2. Add the file with conflict markers
1033 # NEEDSWORK: Even though the merge conflict removed the
1034 # SKIP_WORKTREE bit from the index entry for folder1/a, we should
1035 # warn that this is a problematic add.
1036 test_sparse_match test_must_fail git add folder1/a &&
1037 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1038 test_sparse_unstaged folder1/a &&
1039 test_all_match git add --sparse folder1/a &&
1040 test_all_match git status --porcelain=v2 &&
1042 # 3. Rename the file to another sparse filename and
1043 # accept conflict markers as resolved content.
1044 # NEEDSWORK: This mode now fails, because folder2/z is
1045 # outside of the sparse-checkout cone and does not match an
1046 # existing index entry with the SKIP_WORKTREE bit cleared.
1047 run_on_all mv folder2/a folder2/z &&
1048 test_sparse_match test_must_fail git add folder2 &&
1049 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1050 test_sparse_unstaged folder2/z &&
1051 test_all_match git add --sparse folder2 &&
1052 test_all_match git status --porcelain=v2 &&
1054 test_all_match git $OPERATION --continue &&
1055 test_all_match git status --porcelain=v2 &&
1056 test_all_match git rev-parse HEAD^{tree} || return 1
1057 done
1060 test_expect_success 'merge with outside renames' '
1061 init_repos &&
1063 for type in out-to-out out-to-in in-to-out
1065 test_all_match git reset --hard &&
1066 test_all_match git checkout -f -b merge-$type update-deep &&
1067 test_all_match git merge -m "$type" rename-$type &&
1068 test_all_match git rev-parse HEAD^{tree} || return 1
1069 done
1072 # Sparse-index fails to convert the index in the
1073 # final 'git cherry-pick' command.
1074 test_expect_success 'cherry-pick with conflicts' '
1075 init_repos &&
1077 write_script edit-conflict <<-\EOF &&
1078 echo $1 >conflict
1081 test_all_match git checkout -b to-cherry-pick &&
1082 run_on_all ../edit-conflict ABC &&
1083 test_all_match git add conflict &&
1084 test_all_match git commit -m "conflict to pick" &&
1086 test_all_match git checkout -B base HEAD~1 &&
1087 run_on_all ../edit-conflict DEF &&
1088 test_all_match git add conflict &&
1089 test_all_match git commit -m "conflict in base" &&
1091 test_all_match test_must_fail git cherry-pick to-cherry-pick
1094 test_expect_success 'stash' '
1095 init_repos &&
1097 write_script edit-contents <<-\EOF &&
1098 echo text >>$1
1101 # Stash a sparse directory (folder1)
1102 test_all_match git checkout -b test-branch rename-base &&
1103 test_all_match git reset --soft rename-out-to-out &&
1104 test_all_match git stash &&
1105 test_all_match git status --porcelain=v2 &&
1107 # Apply the sparse directory stash without reinstating the index
1108 test_all_match git stash apply -q &&
1109 test_all_match git status --porcelain=v2 &&
1111 # Reset to state where stash can be applied
1112 test_sparse_match git sparse-checkout reapply &&
1113 test_all_match git reset --hard rename-out-to-out &&
1115 # Apply the sparse directory stash *with* reinstating the index
1116 test_all_match git stash apply --index -q &&
1117 test_all_match git status --porcelain=v2 &&
1119 # Reset to state where we will get a conflict applying the stash
1120 test_sparse_match git sparse-checkout reapply &&
1121 test_all_match git reset --hard update-folder1 &&
1123 # Apply the sparse directory stash with conflicts
1124 test_all_match test_must_fail git stash apply --index -q &&
1125 test_all_match test_must_fail git stash apply -q &&
1126 test_all_match git status --porcelain=v2 &&
1128 # Reset to base branch
1129 test_sparse_match git sparse-checkout reapply &&
1130 test_all_match git reset --hard base &&
1132 # Stash & unstash an untracked file outside of the sparse checkout
1133 # definition.
1134 run_on_sparse mkdir -p folder1 &&
1135 run_on_all ../edit-contents folder1/new &&
1136 test_all_match git stash -u &&
1137 test_all_match git status --porcelain=v2 &&
1139 test_all_match git stash pop -q &&
1140 test_all_match git status --porcelain=v2
1143 test_expect_success 'checkout-index inside sparse definition' '
1144 init_repos &&
1146 run_on_all rm -f deep/a &&
1147 test_all_match git checkout-index -- deep/a &&
1148 test_all_match git status --porcelain=v2 &&
1150 echo test >>new-a &&
1151 run_on_all cp ../new-a a &&
1152 test_all_match test_must_fail git checkout-index -- a &&
1153 test_all_match git checkout-index -f -- a &&
1154 test_all_match git status --porcelain=v2
1157 test_expect_success 'checkout-index outside sparse definition' '
1158 init_repos &&
1160 # Without --ignore-skip-worktree-bits, outside-of-cone files will trigger
1161 # an error
1162 test_sparse_match test_must_fail git checkout-index -- folder1/a &&
1163 test_i18ngrep "folder1/a has skip-worktree enabled" sparse-checkout-err &&
1164 test_path_is_missing folder1/a &&
1166 # With --ignore-skip-worktree-bits, outside-of-cone files are checked out
1167 test_sparse_match git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
1168 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
1169 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a &&
1171 run_on_sparse rm -rf folder1 &&
1172 echo test >new-a &&
1173 run_on_sparse mkdir -p folder1 &&
1174 run_on_all cp ../new-a folder1/a &&
1176 test_all_match test_must_fail git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
1177 test_all_match git checkout-index -f --ignore-skip-worktree-bits -- folder1/a &&
1178 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
1179 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a
1182 test_expect_success 'checkout-index with folders' '
1183 init_repos &&
1185 # Inside checkout definition
1186 test_all_match test_must_fail git checkout-index -f -- deep/ &&
1188 # Outside checkout definition
1189 # Note: although all tests fail (as expected), the messaging differs. For
1190 # non-sparse index checkouts, the error is that the "file" does not appear
1191 # in the index; for sparse checkouts, the error is explicitly that the
1192 # entry is a sparse directory.
1193 run_on_all test_must_fail git checkout-index -f -- folder1/ &&
1194 test_cmp full-checkout-err sparse-checkout-err &&
1195 ! test_cmp full-checkout-err sparse-index-err &&
1196 grep "is a sparse directory" sparse-index-err
1199 test_expect_success 'checkout-index --all' '
1200 init_repos &&
1202 test_all_match git checkout-index --all &&
1203 test_sparse_match test_path_is_missing folder1 &&
1205 # --ignore-skip-worktree-bits will cause `skip-worktree` files to be
1206 # checked out, causing the outside-of-cone `folder1` to exist on-disk
1207 test_all_match git checkout-index --ignore-skip-worktree-bits --all &&
1208 test_all_match test_path_exists folder1
1211 test_expect_success 'clean' '
1212 init_repos &&
1214 echo bogus >>.gitignore &&
1215 run_on_all cp ../.gitignore . &&
1216 test_all_match git add .gitignore &&
1217 test_all_match git commit -m "ignore bogus files" &&
1219 run_on_sparse mkdir folder1 &&
1220 run_on_all mkdir -p deep/untracked-deep &&
1221 run_on_all touch folder1/bogus &&
1222 run_on_all touch folder1/untracked &&
1223 run_on_all touch deep/untracked-deep/bogus &&
1224 run_on_all touch deep/untracked-deep/untracked &&
1226 test_all_match git status --porcelain=v2 &&
1227 test_all_match git clean -f &&
1228 test_all_match git status --porcelain=v2 &&
1229 test_sparse_match ls &&
1230 test_sparse_match ls folder1 &&
1231 run_on_all test_path_exists folder1/bogus &&
1232 run_on_all test_path_is_missing folder1/untracked &&
1233 run_on_all test_path_exists deep/untracked-deep/bogus &&
1234 run_on_all test_path_exists deep/untracked-deep/untracked &&
1236 test_all_match git clean -fd &&
1237 test_all_match git status --porcelain=v2 &&
1238 test_sparse_match ls &&
1239 test_sparse_match ls folder1 &&
1240 run_on_all test_path_exists folder1/bogus &&
1241 run_on_all test_path_exists deep/untracked-deep/bogus &&
1242 run_on_all test_path_is_missing deep/untracked-deep/untracked &&
1244 test_all_match git clean -xf &&
1245 test_all_match git status --porcelain=v2 &&
1246 test_sparse_match ls &&
1247 test_sparse_match ls folder1 &&
1248 run_on_all test_path_is_missing folder1/bogus &&
1249 run_on_all test_path_exists deep/untracked-deep/bogus &&
1251 test_all_match git clean -xdf &&
1252 test_all_match git status --porcelain=v2 &&
1253 test_sparse_match ls &&
1254 test_sparse_match ls folder1 &&
1255 run_on_all test_path_is_missing deep/untracked-deep/bogus &&
1257 test_sparse_match test_path_is_dir folder1
1260 for builtin in show rev-parse
1262 test_expect_success "$builtin (cached blobs/trees)" "
1263 init_repos &&
1265 test_all_match git $builtin :a &&
1266 test_all_match git $builtin :deep/a &&
1267 test_sparse_match git $builtin :folder1/a &&
1269 # The error message differs depending on whether
1270 # the directory exists in the worktree.
1271 test_all_match test_must_fail git $builtin :deep/ &&
1272 test_must_fail git -C full-checkout $builtin :folder1/ &&
1273 test_sparse_match test_must_fail git $builtin :folder1/ &&
1275 # Change the sparse cone for an extra case:
1276 run_on_sparse git sparse-checkout set deep/deeper1 &&
1278 # deep/deeper2 is a sparse directory in the sparse index.
1279 test_sparse_match test_must_fail git $builtin :deep/deeper2/ &&
1281 # deep/deeper2/deepest is not in the sparse index, but
1282 # will trigger an index expansion.
1283 test_sparse_match test_must_fail git $builtin :deep/deeper2/deepest/
1285 done
1287 test_expect_success 'submodule handling' '
1288 init_repos &&
1290 test_sparse_match git sparse-checkout add modules &&
1291 test_all_match mkdir modules &&
1292 test_all_match touch modules/a &&
1293 test_all_match git add modules &&
1294 test_all_match git commit -m "add modules directory" &&
1296 test_config_global protocol.file.allow always &&
1298 run_on_all git submodule add "$(pwd)/initial-repo" modules/sub &&
1299 test_all_match git commit -m "add submodule" &&
1301 # having a submodule prevents "modules" from collapse
1302 test_sparse_match git sparse-checkout set deep/deeper1 &&
1303 git -C sparse-index ls-files --sparse --stage >cache &&
1304 grep "100644 .* modules/a" cache &&
1305 grep "160000 $(git -C initial-repo rev-parse HEAD) 0 modules/sub" cache
1308 # When working with a sparse index, some commands will need to expand the
1309 # index to operate properly. If those commands also write the index back
1310 # to disk, they need to convert the index to sparse before writing.
1311 # This test verifies that both of these events are logged in trace2 logs.
1312 test_expect_success 'sparse-index is expanded and converted back' '
1313 init_repos &&
1315 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1316 git -C sparse-index reset -- folder1/a &&
1317 test_region index convert_to_sparse trace2.txt &&
1318 test_region index ensure_full_index trace2.txt &&
1320 # ls-files expands on read, but does not write.
1321 rm trace2.txt &&
1322 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1323 git -C sparse-index ls-files &&
1324 test_region index ensure_full_index trace2.txt
1327 test_expect_success 'index.sparse disabled inline uses full index' '
1328 init_repos &&
1330 # When index.sparse is disabled inline with `git status`, the
1331 # index is expanded at the beginning of the execution then never
1332 # converted back to sparse. It is then written to disk as a full index.
1333 rm -f trace2.txt &&
1334 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1335 git -C sparse-index -c index.sparse=false status &&
1336 ! test_region index convert_to_sparse trace2.txt &&
1337 test_region index ensure_full_index trace2.txt &&
1339 # Since index.sparse is set to true at a repo level, the index
1340 # is converted from full to sparse when read, then never expanded
1341 # over the course of `git status`. It is written to disk as a sparse
1342 # index.
1343 rm -f trace2.txt &&
1344 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1345 git -C sparse-index status &&
1346 test_region index convert_to_sparse trace2.txt &&
1347 ! test_region index ensure_full_index trace2.txt &&
1349 # Now that the index has been written to disk as sparse, it is not
1350 # converted to sparse (or expanded to full) when read by `git status`.
1351 rm -f trace2.txt &&
1352 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1353 git -C sparse-index status &&
1354 ! test_region index convert_to_sparse trace2.txt &&
1355 ! test_region index ensure_full_index trace2.txt
1358 ensure_not_expanded () {
1359 rm -f trace2.txt &&
1360 if test -z "$WITHOUT_UNTRACKED_TXT"
1361 then
1362 echo >>sparse-index/untracked.txt
1363 fi &&
1365 if test "$1" = "!"
1366 then
1367 shift &&
1368 test_must_fail env \
1369 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1370 git -C sparse-index "$@" || return 1
1371 else
1372 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1373 git -C sparse-index "$@" || return 1
1374 fi &&
1375 test_region ! index ensure_full_index trace2.txt
1378 test_expect_success 'sparse-index is not expanded' '
1379 init_repos &&
1381 ensure_not_expanded status &&
1382 ensure_not_expanded ls-files --sparse &&
1383 ensure_not_expanded commit --allow-empty -m empty &&
1384 echo >>sparse-index/a &&
1385 ensure_not_expanded commit -a -m a &&
1386 echo >>sparse-index/a &&
1387 ensure_not_expanded commit --include a -m a &&
1388 echo >>sparse-index/deep/deeper1/a &&
1389 ensure_not_expanded commit --include deep/deeper1/a -m deeper &&
1390 ensure_not_expanded checkout rename-out-to-out &&
1391 ensure_not_expanded checkout - &&
1392 ensure_not_expanded switch rename-out-to-out &&
1393 ensure_not_expanded switch - &&
1394 ensure_not_expanded reset --hard &&
1395 ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
1396 ensure_not_expanded reset --hard &&
1397 ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
1399 echo >>sparse-index/README.md &&
1400 ensure_not_expanded add -A &&
1401 echo >>sparse-index/extra.txt &&
1402 ensure_not_expanded add extra.txt &&
1403 echo >>sparse-index/untracked.txt &&
1404 ensure_not_expanded add . &&
1406 ensure_not_expanded checkout-index -f a &&
1407 ensure_not_expanded checkout-index -f --all &&
1408 for ref in update-deep update-folder1 update-folder2 update-deep
1410 echo >>sparse-index/README.md &&
1411 ensure_not_expanded reset --hard $ref || return 1
1412 done &&
1414 ensure_not_expanded reset --mixed base &&
1415 ensure_not_expanded reset --hard update-deep &&
1416 ensure_not_expanded reset --keep base &&
1417 ensure_not_expanded reset --merge update-deep &&
1418 ensure_not_expanded reset --hard &&
1420 ensure_not_expanded reset base -- deep/a &&
1421 ensure_not_expanded reset base -- nonexistent-file &&
1422 ensure_not_expanded reset deepest -- deep &&
1424 # Although folder1 is outside the sparse definition, it exists as a
1425 # directory entry in the index, so the pathspec will not force the
1426 # index to be expanded.
1427 ensure_not_expanded reset deepest -- folder1 &&
1428 ensure_not_expanded reset deepest -- folder1/ &&
1430 # Wildcard identifies only in-cone files, no index expansion
1431 ensure_not_expanded reset deepest -- deep/\* &&
1433 # Wildcard identifies only full sparse directories, no index expansion
1434 ensure_not_expanded reset deepest -- folder\* &&
1436 ensure_not_expanded clean -fd &&
1438 ensure_not_expanded checkout -f update-deep &&
1439 test_config -C sparse-index pull.twohead ort &&
1441 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1442 for OPERATION in "merge -m merge" cherry-pick rebase
1444 ensure_not_expanded merge -m merge update-folder1 &&
1445 ensure_not_expanded merge -m merge update-folder2 || return 1
1446 done
1450 test_expect_success 'sparse-index is not expanded: merge conflict in cone' '
1451 init_repos &&
1453 for side in right left
1455 git -C sparse-index checkout -b expand-$side base &&
1456 echo $side >sparse-index/deep/a &&
1457 git -C sparse-index commit -a -m "$side" || return 1
1458 done &&
1461 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1462 git -C sparse-index config pull.twohead ort &&
1463 ensure_not_expanded ! merge -m merged expand-right
1467 test_expect_success 'sparse-index is not expanded: stash' '
1468 init_repos &&
1470 echo >>sparse-index/a &&
1471 ensure_not_expanded stash &&
1472 ensure_not_expanded stash list &&
1473 ensure_not_expanded stash show stash@{0} &&
1474 ensure_not_expanded stash apply stash@{0} &&
1475 ensure_not_expanded stash drop stash@{0} &&
1477 echo >>sparse-index/deep/new &&
1478 ensure_not_expanded stash -u &&
1480 WITHOUT_UNTRACKED_TXT=1 &&
1481 ensure_not_expanded stash pop
1482 ) &&
1484 ensure_not_expanded stash create &&
1485 oid=$(git -C sparse-index stash create) &&
1486 ensure_not_expanded stash store -m "test" $oid &&
1487 ensure_not_expanded reset --hard &&
1488 ensure_not_expanded stash pop
1491 test_expect_success 'sparse index is not expanded: diff' '
1492 init_repos &&
1494 write_script edit-contents <<-\EOF &&
1495 echo text >>$1
1498 # Add file within cone
1499 test_sparse_match git sparse-checkout set deep &&
1500 run_on_all ../edit-contents deep/testfile &&
1501 test_all_match git add deep/testfile &&
1502 run_on_all ../edit-contents deep/testfile &&
1504 test_all_match git diff &&
1505 test_all_match git diff --cached &&
1506 ensure_not_expanded diff &&
1507 ensure_not_expanded diff --cached &&
1509 # Add file outside cone
1510 test_all_match git reset --hard &&
1511 run_on_all mkdir newdirectory &&
1512 run_on_all ../edit-contents newdirectory/testfile &&
1513 test_sparse_match git sparse-checkout set newdirectory &&
1514 test_all_match git add newdirectory/testfile &&
1515 run_on_all ../edit-contents newdirectory/testfile &&
1516 test_sparse_match git sparse-checkout set &&
1518 test_all_match git diff &&
1519 test_all_match git diff --cached &&
1520 ensure_not_expanded diff &&
1521 ensure_not_expanded diff --cached &&
1523 # Merge conflict outside cone
1524 # The sparse checkout will report a warning that is not in the
1525 # full checkout, so we use `run_on_all` instead of
1526 # `test_all_match`
1527 run_on_all git reset --hard &&
1528 test_all_match git checkout merge-left &&
1529 test_all_match test_must_fail git merge merge-right &&
1531 test_all_match git diff &&
1532 test_all_match git diff --cached &&
1533 ensure_not_expanded diff &&
1534 ensure_not_expanded diff --cached
1537 test_expect_success 'sparse index is not expanded: show and rev-parse' '
1538 init_repos &&
1540 ensure_not_expanded show :a &&
1541 ensure_not_expanded show :deep/a &&
1542 ensure_not_expanded rev-parse :a &&
1543 ensure_not_expanded rev-parse :deep/a
1546 test_expect_success 'sparse index is not expanded: update-index' '
1547 init_repos &&
1549 deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
1550 ensure_not_expanded update-index --cacheinfo 100644 $deep_a_oid deep/a &&
1552 echo "test" >sparse-index/README.md &&
1553 echo "test2" >sparse-index/a &&
1554 rm -f sparse-index/deep/a &&
1556 ensure_not_expanded update-index --add README.md &&
1557 ensure_not_expanded update-index a &&
1558 ensure_not_expanded update-index --remove deep/a &&
1560 ensure_not_expanded reset --soft update-deep &&
1561 ensure_not_expanded update-index --add --remove --again
1564 test_expect_success 'sparse index is not expanded: blame' '
1565 init_repos &&
1567 for file in a \
1568 deep/a \
1569 deep/deeper1/a \
1570 deep/deeper1/deepest/a
1572 ensure_not_expanded blame $file
1573 done
1576 test_expect_success 'sparse index is not expanded: fetch/pull' '
1577 init_repos &&
1579 git -C sparse-index remote add full "file://$(pwd)/full-checkout" &&
1580 ensure_not_expanded fetch full &&
1581 git -C full-checkout commit --allow-empty -m "for pull merge" &&
1582 git -C sparse-index commit --allow-empty -m "for pull merge" &&
1583 ensure_not_expanded pull full base
1586 test_expect_success 'sparse index is not expanded: read-tree' '
1587 init_repos &&
1589 ensure_not_expanded checkout -b test-branch update-folder1 &&
1590 for MERGE_TREES in "base HEAD update-folder2" \
1591 "base HEAD rename-base" \
1592 "base update-folder2" \
1593 "base rename-base" \
1594 "update-folder2"
1596 ensure_not_expanded read-tree -mu $MERGE_TREES &&
1597 ensure_not_expanded reset --hard || return 1
1598 done &&
1600 rm -rf sparse-index/deep/deeper2 &&
1601 ensure_not_expanded add . &&
1602 ensure_not_expanded commit -m "test" &&
1604 ensure_not_expanded read-tree --prefix=deep/deeper2 -u deepest
1607 test_expect_success 'ls-files' '
1608 init_repos &&
1610 # Use a smaller sparse-checkout for reduced output
1611 test_sparse_match git sparse-checkout set &&
1613 # Behavior agrees by default. Sparse index is expanded.
1614 test_all_match git ls-files &&
1616 # With --sparse, the sparse index data changes behavior.
1617 git -C sparse-index ls-files --sparse >actual &&
1619 cat >expect <<-\EOF &&
1621 before/
1622 deep/
1624 folder1-
1625 folder1.x
1626 folder1/
1627 folder10
1628 folder2/
1634 test_cmp expect actual &&
1636 # With --sparse and no sparse index, nothing changes.
1637 git -C sparse-checkout ls-files >dense &&
1638 git -C sparse-checkout ls-files --sparse >sparse &&
1639 test_cmp dense sparse &&
1641 # Set up a strange condition of having a file edit
1642 # outside of the sparse-checkout cone. We want to verify
1643 # that all modes handle this the same, and detect the
1644 # modification.
1645 write_script edit-content <<-\EOF &&
1646 mkdir -p folder1 &&
1647 echo content >>folder1/a
1649 run_on_all ../edit-content &&
1651 test_all_match git ls-files --modified &&
1653 git -C sparse-index ls-files --sparse --modified >sparse-index-out &&
1654 cat >expect <<-\EOF &&
1655 folder1/a
1657 test_cmp expect sparse-index-out &&
1659 # Add folder1 to the sparse-checkout cone and
1660 # check that ls-files shows the expanded files.
1661 test_sparse_match git sparse-checkout add folder1 &&
1662 test_all_match git ls-files --modified &&
1664 test_all_match git ls-files &&
1665 git -C sparse-index ls-files --sparse >actual &&
1667 cat >expect <<-\EOF &&
1669 before/
1670 deep/
1672 folder1-
1673 folder1.x
1674 folder1/0/0/0
1675 folder1/0/1
1676 folder1/a
1677 folder10
1678 folder2/
1684 test_cmp expect actual &&
1686 # Double-check index expansion is avoided
1687 ensure_not_expanded ls-files --sparse
1690 test_expect_success 'sparse index is not expanded: sparse-checkout' '
1691 init_repos &&
1693 ensure_not_expanded sparse-checkout set deep/deeper2 &&
1694 ensure_not_expanded sparse-checkout set deep/deeper1 &&
1695 ensure_not_expanded sparse-checkout set deep &&
1696 ensure_not_expanded sparse-checkout add folder1 &&
1697 ensure_not_expanded sparse-checkout set deep/deeper1 &&
1698 ensure_not_expanded sparse-checkout set folder2 &&
1700 # Demonstrate that the checks that "folder1/a" is a file
1701 # do not cause a sparse-index expansion (since it is in the
1702 # sparse-checkout cone).
1703 echo >>sparse-index/folder2/a &&
1704 git -C sparse-index add folder2/a &&
1706 ensure_not_expanded sparse-checkout add folder1 &&
1708 # Skip checks here, since deep/deeper1 is inside a sparse directory
1709 # that must be expanded to check whether `deep/deeper1` is a file
1710 # or not.
1711 ensure_not_expanded sparse-checkout set --skip-checks deep/deeper1 &&
1712 ensure_not_expanded sparse-checkout set
1715 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
1716 # in this scenario, but it shouldn't.
1717 test_expect_success 'reset mixed and checkout orphan' '
1718 init_repos &&
1720 test_all_match git checkout rename-out-to-in &&
1722 # Sparse checkouts do not agree with full checkouts about
1723 # how to report a directory/file conflict during a reset.
1724 # This command would fail with test_all_match because the
1725 # full checkout reports "T folder1/0/1" while a sparse
1726 # checkout reports "D folder1/0/1". This matches because
1727 # the sparse checkouts skip "adding" the other side of
1728 # the conflict.
1729 test_sparse_match git reset --mixed HEAD~1 &&
1730 test_sparse_match git ls-files --stage &&
1731 test_sparse_match git status --porcelain=v2 &&
1733 # At this point, sparse-checkouts behave differently
1734 # from the full-checkout.
1735 test_sparse_match git checkout --orphan new-branch &&
1736 test_sparse_match git ls-files --stage &&
1737 test_sparse_match git status --porcelain=v2
1740 test_expect_success 'add everything with deep new file' '
1741 init_repos &&
1743 run_on_sparse git sparse-checkout set deep/deeper1/deepest &&
1745 run_on_all touch deep/deeper1/x &&
1746 test_all_match git add . &&
1747 test_all_match git status --porcelain=v2
1750 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1751 # directory/file conflicts, even without sparse-checkout. Use this
1752 # test only as a documentation of the incorrect behavior, not a
1753 # measure of how it _should_ behave.
1754 test_expect_success 'checkout behaves oddly with df-conflict-1' '
1755 init_repos &&
1757 test_sparse_match git sparse-checkout disable &&
1759 write_script edit-content <<-\EOF &&
1760 echo content >>folder1/larger-content
1761 git add folder1
1764 run_on_all ../edit-content &&
1765 test_all_match git status --porcelain=v2 &&
1767 git -C sparse-checkout sparse-checkout init --cone &&
1768 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1770 test_all_match git status --porcelain=v2 &&
1772 # This checkout command should fail, because we have a staged
1773 # change to folder1/larger-content, but the destination changes
1774 # folder1 to a file.
1775 git -C full-checkout checkout df-conflict-1 \
1776 1>full-checkout-out \
1777 2>full-checkout-err &&
1778 git -C sparse-checkout checkout df-conflict-1 \
1779 1>sparse-checkout-out \
1780 2>sparse-checkout-err &&
1781 git -C sparse-index checkout df-conflict-1 \
1782 1>sparse-index-out \
1783 2>sparse-index-err &&
1785 # Instead, the checkout deletes the folder1 file and adds the
1786 # folder1/larger-content file, leaving all other paths that were
1787 # in folder1/ as deleted (without any warning).
1788 cat >expect <<-EOF &&
1789 D folder1
1790 A folder1/larger-content
1792 test_cmp expect full-checkout-out &&
1793 test_cmp expect sparse-checkout-out &&
1795 # The sparse-index reports no output
1796 test_must_be_empty sparse-index-out &&
1798 # stderr: Switched to branch df-conflict-1
1799 test_cmp full-checkout-err sparse-checkout-err &&
1800 test_cmp full-checkout-err sparse-checkout-err
1803 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1804 # directory/file conflicts, even without sparse-checkout. Use this
1805 # test only as a documentation of the incorrect behavior, not a
1806 # measure of how it _should_ behave.
1807 test_expect_success 'checkout behaves oddly with df-conflict-2' '
1808 init_repos &&
1810 test_sparse_match git sparse-checkout disable &&
1812 write_script edit-content <<-\EOF &&
1813 echo content >>folder2/larger-content
1814 git add folder2
1817 run_on_all ../edit-content &&
1818 test_all_match git status --porcelain=v2 &&
1820 git -C sparse-checkout sparse-checkout init --cone &&
1821 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1823 test_all_match git status --porcelain=v2 &&
1825 # This checkout command should fail, because we have a staged
1826 # change to folder1/larger-content, but the destination changes
1827 # folder1 to a file.
1828 git -C full-checkout checkout df-conflict-2 \
1829 1>full-checkout-out \
1830 2>full-checkout-err &&
1831 git -C sparse-checkout checkout df-conflict-2 \
1832 1>sparse-checkout-out \
1833 2>sparse-checkout-err &&
1834 git -C sparse-index checkout df-conflict-2 \
1835 1>sparse-index-out \
1836 2>sparse-index-err &&
1838 # The full checkout deviates from the df-conflict-1 case here!
1839 # It drops the change to folder1/larger-content and leaves the
1840 # folder1 path as-is on disk. The sparse-index behaves the same.
1841 test_must_be_empty full-checkout-out &&
1842 test_must_be_empty sparse-index-out &&
1844 # In the sparse-checkout case, the checkout deletes the folder1
1845 # file and adds the folder1/larger-content file, leaving all other
1846 # paths that were in folder1/ as deleted (without any warning).
1847 cat >expect <<-EOF &&
1848 D folder2
1849 A folder2/larger-content
1851 test_cmp expect sparse-checkout-out &&
1853 # Switched to branch df-conflict-1
1854 test_cmp full-checkout-err sparse-checkout-err &&
1855 test_cmp full-checkout-err sparse-index-err
1858 test_done