Merge branch 'tb/commit-graph-genv2-upgrade-fix' into maint
[git/debian.git] / t / t1092-sparse-checkout-compatibility.sh
blobf9f8c988bb4905939824903b3c59620450238350
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 'add outside sparse cone' '
376 init_repos &&
378 run_on_sparse mkdir folder1 &&
379 run_on_sparse ../edit-contents folder1/a &&
380 run_on_sparse ../edit-contents folder1/newfile &&
381 test_sparse_match test_must_fail git add folder1/a &&
382 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
383 test_sparse_unstaged folder1/a &&
384 test_sparse_match test_must_fail git add folder1/newfile &&
385 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
386 test_sparse_unstaged folder1/newfile
389 test_expect_success 'commit including unstaged changes' '
390 init_repos &&
392 write_script edit-file <<-\EOF &&
393 echo $1 >$2
396 run_on_all ../edit-file 1 a &&
397 run_on_all ../edit-file 1 deep/a &&
399 test_all_match git commit -m "-a" -a &&
400 test_all_match git status --porcelain=v2 &&
402 run_on_all ../edit-file 2 a &&
403 run_on_all ../edit-file 2 deep/a &&
405 test_all_match git commit -m "--include" --include deep/a &&
406 test_all_match git status --porcelain=v2 &&
407 test_all_match git commit -m "--include" --include a &&
408 test_all_match git status --porcelain=v2 &&
410 run_on_all ../edit-file 3 a &&
411 run_on_all ../edit-file 3 deep/a &&
413 test_all_match git commit -m "--amend" -a --amend &&
414 test_all_match git status --porcelain=v2
417 test_expect_success 'status/add: outside sparse cone' '
418 init_repos &&
420 # folder1 is at HEAD, but outside the sparse cone
421 run_on_sparse mkdir folder1 &&
422 cp initial-repo/folder1/a sparse-checkout/folder1/a &&
423 cp initial-repo/folder1/a sparse-index/folder1/a &&
425 test_sparse_match git status &&
427 write_script edit-contents <<-\EOF &&
428 echo text >>$1
430 run_on_all ../edit-contents folder1/a &&
431 run_on_all ../edit-contents folder1/new &&
433 test_sparse_match git status --porcelain=v2 &&
435 # Adding the path outside of the sparse-checkout cone should fail.
436 test_sparse_match test_must_fail git add folder1/a &&
437 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
438 test_sparse_unstaged folder1/a &&
439 test_all_match git add --refresh folder1/a &&
440 test_must_be_empty sparse-checkout-err &&
441 test_sparse_unstaged folder1/a &&
442 test_sparse_match test_must_fail git add folder1/new &&
443 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
444 test_sparse_unstaged folder1/new &&
445 test_sparse_match git add --sparse folder1/a &&
446 test_sparse_match git add --sparse folder1/new &&
448 test_all_match git add --sparse . &&
449 test_all_match git status --porcelain=v2 &&
450 test_all_match git commit -m folder1/new &&
451 test_all_match git rev-parse HEAD^{tree} &&
453 run_on_all ../edit-contents folder1/newer &&
454 test_all_match git add --sparse folder1/ &&
455 test_all_match git status --porcelain=v2 &&
456 test_all_match git commit -m folder1/newer &&
457 test_all_match git rev-parse HEAD^{tree}
460 test_expect_success 'checkout and reset --hard' '
461 init_repos &&
463 test_all_match git checkout update-folder1 &&
464 test_all_match git status --porcelain=v2 &&
466 test_all_match git checkout update-deep &&
467 test_all_match git status --porcelain=v2 &&
469 test_all_match git checkout -b reset-test &&
470 test_all_match git reset --hard deepest &&
471 test_all_match git reset --hard update-folder1 &&
472 test_all_match git reset --hard update-folder2
475 test_expect_success 'diff --cached' '
476 init_repos &&
478 write_script edit-contents <<-\EOF &&
479 echo text >>README.md
481 run_on_all ../edit-contents &&
483 test_all_match git diff &&
484 test_all_match git diff --cached &&
485 test_all_match git add README.md &&
486 test_all_match git diff &&
487 test_all_match git diff --cached
490 # NEEDSWORK: sparse-checkout behaves differently from full-checkout when
491 # running this test with 'df-conflict-2' after 'df-conflict-1'.
492 test_expect_success 'diff with renames and conflicts' '
493 init_repos &&
495 for branch in rename-out-to-out \
496 rename-out-to-in \
497 rename-in-to-out \
498 df-conflict-1 \
499 fd-conflict
501 test_all_match git checkout rename-base &&
502 test_all_match git checkout $branch -- . &&
503 test_all_match git status --porcelain=v2 &&
504 test_all_match git diff --cached --no-renames &&
505 test_all_match git diff --cached --find-renames || return 1
506 done
509 test_expect_success 'diff with directory/file conflicts' '
510 init_repos &&
512 for branch in rename-out-to-out \
513 rename-out-to-in \
514 rename-in-to-out \
515 df-conflict-1 \
516 df-conflict-2 \
517 fd-conflict
519 git -C full-checkout reset --hard &&
520 test_sparse_match git reset --hard &&
521 test_all_match git checkout $branch &&
522 test_all_match git checkout rename-base -- . &&
523 test_all_match git status --porcelain=v2 &&
524 test_all_match git diff --cached --no-renames &&
525 test_all_match git diff --cached --find-renames || return 1
526 done
529 test_expect_success 'log with pathspec outside sparse definition' '
530 init_repos &&
532 test_all_match git log -- a &&
533 test_all_match git log -- folder1/a &&
534 test_all_match git log -- folder2/a &&
535 test_all_match git log -- deep/a &&
536 test_all_match git log -- deep/deeper1/a &&
537 test_all_match git log -- deep/deeper1/deepest/a &&
539 test_all_match git checkout update-folder1 &&
540 test_all_match git log -- folder1/a
543 test_expect_success 'blame with pathspec inside sparse definition' '
544 init_repos &&
546 for file in a \
547 deep/a \
548 deep/deeper1/a \
549 deep/deeper1/deepest/a
551 test_all_match git blame $file
552 done
555 # Without a revision specified, blame will error if passed any file that
556 # is not present in the working directory (even if the file is tracked).
557 # Here we just verify that this is also true with sparse checkouts.
558 test_expect_success 'blame with pathspec outside sparse definition' '
559 init_repos &&
560 test_sparse_match git sparse-checkout set &&
562 for file in a \
563 deep/a \
564 deep/deeper1/a \
565 deep/deeper1/deepest/a
567 test_sparse_match test_must_fail git blame $file &&
568 cat >expect <<-EOF &&
569 fatal: Cannot lstat '"'"'$file'"'"': No such file or directory
571 # We compare sparse-checkout-err and sparse-index-err in
572 # `test_sparse_match`. Given we know they are the same, we
573 # only check the content of sparse-index-err here.
574 test_cmp expect sparse-index-err
575 done
578 test_expect_success 'checkout and reset (mixed)' '
579 init_repos &&
581 test_all_match git checkout -b reset-test update-deep &&
582 test_all_match git reset deepest &&
584 # Because skip-worktree is preserved, resetting to update-folder1
585 # will show worktree changes for folder1/a in full-checkout, but not
586 # in sparse-checkout or sparse-index.
587 git -C full-checkout reset update-folder1 >full-checkout-out &&
588 test_sparse_match git reset update-folder1 &&
589 grep "M folder1/a" full-checkout-out &&
590 ! grep "M folder1/a" sparse-checkout-out &&
591 run_on_sparse test_path_is_missing folder1
594 test_expect_success 'checkout and reset (merge)' '
595 init_repos &&
597 write_script edit-contents <<-\EOF &&
598 echo text >>$1
601 test_all_match git checkout -b reset-test update-deep &&
602 run_on_all ../edit-contents a &&
603 test_all_match git reset --merge deepest &&
604 test_all_match git status --porcelain=v2 &&
606 test_all_match git reset --hard update-deep &&
607 run_on_all ../edit-contents deep/a &&
608 test_all_match test_must_fail git reset --merge deepest
611 test_expect_success 'checkout and reset (keep)' '
612 init_repos &&
614 write_script edit-contents <<-\EOF &&
615 echo text >>$1
618 test_all_match git checkout -b reset-test update-deep &&
619 run_on_all ../edit-contents a &&
620 test_all_match git reset --keep deepest &&
621 test_all_match git status --porcelain=v2 &&
623 test_all_match git reset --hard update-deep &&
624 run_on_all ../edit-contents deep/a &&
625 test_all_match test_must_fail git reset --keep deepest
628 test_expect_success 'reset with pathspecs inside sparse definition' '
629 init_repos &&
631 write_script edit-contents <<-\EOF &&
632 echo text >>$1
635 test_all_match git checkout -b reset-test update-deep &&
636 run_on_all ../edit-contents deep/a &&
638 test_all_match git reset base -- deep/a &&
639 test_all_match git status --porcelain=v2 &&
641 test_all_match git reset base -- nonexistent-file &&
642 test_all_match git status --porcelain=v2 &&
644 test_all_match git reset deepest -- deep &&
645 test_all_match git status --porcelain=v2
648 # Although the working tree differs between full and sparse checkouts after
649 # reset, the state of the index is the same.
650 test_expect_success 'reset with pathspecs outside sparse definition' '
651 init_repos &&
652 test_all_match git checkout -b reset-test base &&
654 test_sparse_match git reset update-folder1 -- folder1 &&
655 git -C full-checkout reset update-folder1 -- folder1 &&
656 test_all_match git ls-files -s -- folder1 &&
658 test_sparse_match git reset update-folder2 -- folder2/a &&
659 git -C full-checkout reset update-folder2 -- folder2/a &&
660 test_all_match git ls-files -s -- folder2/a
663 test_expect_success 'reset with wildcard pathspec' '
664 init_repos &&
666 test_all_match git reset update-deep -- deep\* &&
667 test_all_match git ls-files -s -- deep &&
669 test_all_match git reset deepest -- deep\*\*\* &&
670 test_all_match git ls-files -s -- deep &&
672 # The following `git reset`s result in updating the index on files with
673 # `skip-worktree` enabled. To avoid failing due to discrepencies in reported
674 # "modified" files, `test_sparse_match` reset is performed separately from
675 # "full-checkout" reset, then the index contents of all repos are verified.
677 test_sparse_match git reset update-folder1 -- \*/a &&
678 git -C full-checkout reset update-folder1 -- \*/a &&
679 test_all_match git ls-files -s -- deep/a folder1/a &&
681 test_sparse_match git reset update-folder2 -- folder\* &&
682 git -C full-checkout reset update-folder2 -- folder\* &&
683 test_all_match git ls-files -s -- folder10 folder1 folder2 &&
685 test_sparse_match git reset base -- folder1/\* &&
686 git -C full-checkout reset base -- folder1/\* &&
687 test_all_match git ls-files -s -- folder1
690 test_expect_success 'update-index modify outside sparse definition' '
691 init_repos &&
693 write_script edit-contents <<-\EOF &&
694 echo text >>$1
697 # Create & modify folder1/a
698 # Note that this setup is a manual way of reaching the erroneous
699 # condition in which a `skip-worktree` enabled, outside-of-cone file
700 # exists on disk. It is used here to ensure `update-index` is stable
701 # and behaves predictably if such a condition occurs.
702 run_on_sparse mkdir -p folder1 &&
703 run_on_sparse cp ../initial-repo/folder1/a folder1/a &&
704 run_on_all ../edit-contents folder1/a &&
706 # If file has skip-worktree enabled, but the file is present, it is
707 # treated the same as if skip-worktree is disabled
708 test_all_match git status --porcelain=v2 &&
709 test_all_match git update-index folder1/a &&
710 test_all_match git status --porcelain=v2 &&
712 # When skip-worktree is disabled (even on files outside sparse cone), file
713 # is updated in the index
714 test_sparse_match git update-index --no-skip-worktree folder1/a &&
715 test_all_match git status --porcelain=v2 &&
716 test_all_match git update-index folder1/a &&
717 test_all_match git status --porcelain=v2
720 test_expect_success 'update-index --add outside sparse definition' '
721 init_repos &&
723 write_script edit-contents <<-\EOF &&
724 echo text >>$1
727 # Create folder1, add new file
728 run_on_sparse mkdir -p folder1 &&
729 run_on_all ../edit-contents folder1/b &&
731 # The *untracked* out-of-cone file is added to the index because it does
732 # not have a `skip-worktree` bit to signal that it should be ignored
733 # (unlike in `git add`, which will fail due to the file being outside
734 # the sparse checkout definition).
735 test_all_match git update-index --add folder1/b &&
736 test_all_match git status --porcelain=v2
739 # NEEDSWORK: `--remove`, unlike the rest of `update-index`, does not ignore
740 # `skip-worktree` entries by default and will remove them from the index.
741 # The `--ignore-skip-worktree-entries` flag must be used in conjunction with
742 # `--remove` to ignore the `skip-worktree` entries and prevent their removal
743 # from the index.
744 test_expect_success 'update-index --remove outside sparse definition' '
745 init_repos &&
747 # When --ignore-skip-worktree-entries is _not_ specified:
748 # out-of-cone, not-on-disk files are removed from the index
749 test_sparse_match git update-index --remove folder1/a &&
750 cat >expect <<-EOF &&
751 D folder1/a
753 test_sparse_match git diff --cached --name-status &&
754 test_cmp expect sparse-checkout-out &&
756 # Reset the state
757 test_all_match git reset --hard &&
759 # When --ignore-skip-worktree-entries is specified, out-of-cone
760 # (skip-worktree) files are ignored
761 test_sparse_match git update-index --remove --ignore-skip-worktree-entries folder1/a &&
762 test_sparse_match git diff --cached --name-status &&
763 test_must_be_empty sparse-checkout-out &&
765 # Reset the state
766 test_all_match git reset --hard &&
768 # --force-remove supercedes --ignore-skip-worktree-entries, removing
769 # a skip-worktree file from the index (and disk) when both are specified
770 # with --remove
771 test_sparse_match git update-index --force-remove --ignore-skip-worktree-entries folder1/a &&
772 cat >expect <<-EOF &&
773 D folder1/a
775 test_sparse_match git diff --cached --name-status &&
776 test_cmp expect sparse-checkout-out
779 test_expect_success 'update-index with directories' '
780 init_repos &&
782 # update-index will exit silently when provided with a directory name
783 # containing a trailing slash
784 test_all_match git update-index deep/ folder1/ &&
785 grep "Ignoring path deep/" sparse-checkout-err &&
786 grep "Ignoring path folder1/" sparse-checkout-err &&
788 # When update-index is given a directory name WITHOUT a trailing slash, it will
789 # behave in different ways depending on the status of the directory on disk:
790 # * if it exists, the command exits with an error ("add individual files instead")
791 # * if it does NOT exist (e.g., in a sparse-checkout), it is assumed to be a
792 # file and either triggers an error ("does not exist and --remove not passed")
793 # or is ignored completely (when using --remove)
794 test_all_match test_must_fail git update-index deep &&
795 run_on_all test_must_fail git update-index folder1 &&
796 test_must_fail git -C full-checkout update-index --remove folder1 &&
797 test_sparse_match git update-index --remove folder1 &&
798 test_all_match git status --porcelain=v2
801 test_expect_success 'update-index --again file outside sparse definition' '
802 init_repos &&
804 test_all_match git checkout -b test-reupdate &&
806 # Update HEAD without modifying the index to introduce a difference in
807 # folder1/a
808 test_sparse_match git reset --soft update-folder1 &&
810 # Because folder1/a differs in the index vs HEAD,
811 # `git update-index --no-skip-worktree --again` will effectively perform
812 # `git update-index --no-skip-worktree folder1/a` and remove the skip-worktree
813 # flag from folder1/a
814 test_sparse_match git update-index --no-skip-worktree --again &&
815 test_sparse_match git status --porcelain=v2 &&
817 cat >expect <<-EOF &&
818 D folder1/a
820 test_sparse_match git diff --name-status &&
821 test_cmp expect sparse-checkout-out
824 test_expect_success 'update-index --cacheinfo' '
825 init_repos &&
827 deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
828 folder2_oid=$(git -C full-checkout rev-parse update-folder2:folder2) &&
829 folder1_a_oid=$(git -C full-checkout rev-parse update-folder1:folder1/a) &&
831 test_all_match git update-index --cacheinfo 100644 $deep_a_oid deep/a &&
832 test_all_match git status --porcelain=v2 &&
834 # Cannot add sparse directory, even in sparse index case
835 test_all_match test_must_fail git update-index --add --cacheinfo 040000 $folder2_oid folder2/ &&
837 # Sparse match only: the new outside-of-cone entry is added *without* skip-worktree,
838 # so `git status` reports it as "deleted" in the worktree
839 test_sparse_match git update-index --add --cacheinfo 100644 $folder1_a_oid folder1/a &&
840 test_sparse_match git status --porcelain=v2 &&
841 cat >expect <<-EOF &&
842 MD folder1/a
844 test_sparse_match git status --short -- folder1/a &&
845 test_cmp expect sparse-checkout-out &&
847 # To return folder1/a to "normal" for a sparse checkout (ignored &
848 # outside-of-cone), add the skip-worktree flag.
849 test_sparse_match git update-index --skip-worktree folder1/a &&
850 cat >expect <<-EOF &&
851 S folder1/a
853 test_sparse_match git ls-files -t -- folder1/a &&
854 test_cmp expect sparse-checkout-out
857 for MERGE_TREES in "base HEAD update-folder2" \
858 "update-folder1 update-folder2" \
859 "update-folder2"
861 test_expect_success "'read-tree -mu $MERGE_TREES' with files outside sparse definition" '
862 init_repos &&
864 # Although the index matches, without --no-sparse-checkout, outside-of-
865 # definition files will not exist on disk for sparse checkouts
866 test_all_match git read-tree -mu $MERGE_TREES &&
867 test_all_match git status --porcelain=v2 &&
868 test_path_is_missing sparse-checkout/folder2 &&
869 test_path_is_missing sparse-index/folder2 &&
871 test_all_match git read-tree --reset -u HEAD &&
872 test_all_match git status --porcelain=v2 &&
874 test_all_match git read-tree -mu --no-sparse-checkout $MERGE_TREES &&
875 test_all_match git status --porcelain=v2 &&
876 test_cmp sparse-checkout/folder2/a sparse-index/folder2/a &&
877 test_cmp sparse-checkout/folder2/a full-checkout/folder2/a
880 done
882 test_expect_success 'read-tree --merge with edit/edit conflicts in sparse directories' '
883 init_repos &&
885 # Merge of multiple changes to same directory (but not same files) should
886 # succeed
887 test_all_match git read-tree -mu base rename-base update-folder1 &&
888 test_all_match git status --porcelain=v2 &&
890 test_all_match git reset --hard &&
892 test_all_match git read-tree -mu rename-base update-folder2 &&
893 test_all_match git status --porcelain=v2 &&
895 test_all_match git reset --hard &&
897 test_all_match test_must_fail git read-tree -mu base update-folder1 rename-out-to-in &&
898 test_all_match test_must_fail git read-tree -mu rename-out-to-in update-folder1
901 test_expect_success 'read-tree --prefix' '
902 init_repos &&
904 # If files differing between the index and target <commit-ish> exist
905 # inside the prefix, `read-tree --prefix` should fail
906 test_all_match test_must_fail git read-tree --prefix=deep/ deepest &&
907 test_all_match test_must_fail git read-tree --prefix=folder1/ update-folder1 &&
909 # If no differing index entries exist matching the prefix,
910 # `read-tree --prefix` updates the index successfully
911 test_all_match git rm -rf deep/deeper1/deepest/ &&
912 test_all_match git read-tree --prefix=deep/deeper1/deepest -u deepest &&
913 test_all_match git status --porcelain=v2 &&
915 test_all_match git rm -rf --sparse folder1/ &&
916 test_all_match git read-tree --prefix=folder1/ -u update-folder1 &&
917 test_all_match git status --porcelain=v2 &&
919 test_all_match git rm -rf --sparse folder2/0 &&
920 test_all_match git read-tree --prefix=folder2/0/ -u rename-out-to-out &&
921 test_all_match git status --porcelain=v2
924 test_expect_success 'read-tree --merge with directory-file conflicts' '
925 init_repos &&
927 test_all_match git checkout -b test-branch rename-base &&
929 # Although the index matches, without --no-sparse-checkout, outside-of-
930 # definition files will not exist on disk for sparse checkouts
931 test_sparse_match git read-tree -mu rename-out-to-out &&
932 test_sparse_match git status --porcelain=v2 &&
933 test_path_is_missing sparse-checkout/folder2 &&
934 test_path_is_missing sparse-index/folder2 &&
936 test_sparse_match git read-tree --reset -u HEAD &&
937 test_sparse_match git status --porcelain=v2 &&
939 test_sparse_match git read-tree -mu --no-sparse-checkout rename-out-to-out &&
940 test_sparse_match git status --porcelain=v2 &&
941 test_cmp sparse-checkout/folder2/0/1 sparse-index/folder2/0/1
944 test_expect_success 'merge, cherry-pick, and rebase' '
945 init_repos &&
947 for OPERATION in "merge -m merge" cherry-pick "rebase --apply" "rebase --merge"
949 test_all_match git checkout -B temp update-deep &&
950 test_all_match git $OPERATION update-folder1 &&
951 test_all_match git rev-parse HEAD^{tree} &&
952 test_all_match git $OPERATION update-folder2 &&
953 test_all_match git rev-parse HEAD^{tree} || return 1
954 done
957 test_expect_success 'merge with conflict outside cone' '
958 init_repos &&
960 test_all_match git checkout -b merge-tip merge-left &&
961 test_all_match git status --porcelain=v2 &&
962 test_all_match test_must_fail git merge -m merge merge-right &&
963 test_all_match git status --porcelain=v2 &&
965 # Resolve the conflict in different ways:
966 # 1. Revert to the base
967 test_all_match git checkout base -- deep/deeper2/a &&
968 test_all_match git status --porcelain=v2 &&
970 # 2. Add the file with conflict markers
971 test_sparse_match test_must_fail git add folder1/a &&
972 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
973 test_sparse_unstaged folder1/a &&
974 test_all_match git add --sparse folder1/a &&
975 test_all_match git status --porcelain=v2 &&
977 # 3. Rename the file to another sparse filename and
978 # accept conflict markers as resolved content.
979 run_on_all mv folder2/a folder2/z &&
980 test_sparse_match test_must_fail git add folder2 &&
981 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
982 test_sparse_unstaged folder2/z &&
983 test_all_match git add --sparse folder2 &&
984 test_all_match git status --porcelain=v2 &&
986 test_all_match git merge --continue &&
987 test_all_match git status --porcelain=v2 &&
988 test_all_match git rev-parse HEAD^{tree}
991 test_expect_success 'cherry-pick/rebase with conflict outside cone' '
992 init_repos &&
994 for OPERATION in cherry-pick rebase
996 test_all_match git checkout -B tip &&
997 test_all_match git reset --hard merge-left &&
998 test_all_match git status --porcelain=v2 &&
999 test_all_match test_must_fail git $OPERATION merge-right &&
1000 test_all_match git status --porcelain=v2 &&
1002 # Resolve the conflict in different ways:
1003 # 1. Revert to the base
1004 test_all_match git checkout base -- deep/deeper2/a &&
1005 test_all_match git status --porcelain=v2 &&
1007 # 2. Add the file with conflict markers
1008 # NEEDSWORK: Even though the merge conflict removed the
1009 # SKIP_WORKTREE bit from the index entry for folder1/a, we should
1010 # warn that this is a problematic add.
1011 test_sparse_match test_must_fail git add folder1/a &&
1012 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1013 test_sparse_unstaged folder1/a &&
1014 test_all_match git add --sparse folder1/a &&
1015 test_all_match git status --porcelain=v2 &&
1017 # 3. Rename the file to another sparse filename and
1018 # accept conflict markers as resolved content.
1019 # NEEDSWORK: This mode now fails, because folder2/z is
1020 # outside of the sparse-checkout cone and does not match an
1021 # existing index entry with the SKIP_WORKTREE bit cleared.
1022 run_on_all mv folder2/a folder2/z &&
1023 test_sparse_match test_must_fail git add folder2 &&
1024 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1025 test_sparse_unstaged folder2/z &&
1026 test_all_match git add --sparse folder2 &&
1027 test_all_match git status --porcelain=v2 &&
1029 test_all_match git $OPERATION --continue &&
1030 test_all_match git status --porcelain=v2 &&
1031 test_all_match git rev-parse HEAD^{tree} || return 1
1032 done
1035 test_expect_success 'merge with outside renames' '
1036 init_repos &&
1038 for type in out-to-out out-to-in in-to-out
1040 test_all_match git reset --hard &&
1041 test_all_match git checkout -f -b merge-$type update-deep &&
1042 test_all_match git merge -m "$type" rename-$type &&
1043 test_all_match git rev-parse HEAD^{tree} || return 1
1044 done
1047 # Sparse-index fails to convert the index in the
1048 # final 'git cherry-pick' command.
1049 test_expect_success 'cherry-pick with conflicts' '
1050 init_repos &&
1052 write_script edit-conflict <<-\EOF &&
1053 echo $1 >conflict
1056 test_all_match git checkout -b to-cherry-pick &&
1057 run_on_all ../edit-conflict ABC &&
1058 test_all_match git add conflict &&
1059 test_all_match git commit -m "conflict to pick" &&
1061 test_all_match git checkout -B base HEAD~1 &&
1062 run_on_all ../edit-conflict DEF &&
1063 test_all_match git add conflict &&
1064 test_all_match git commit -m "conflict in base" &&
1066 test_all_match test_must_fail git cherry-pick to-cherry-pick
1069 test_expect_success 'stash' '
1070 init_repos &&
1072 write_script edit-contents <<-\EOF &&
1073 echo text >>$1
1076 # Stash a sparse directory (folder1)
1077 test_all_match git checkout -b test-branch rename-base &&
1078 test_all_match git reset --soft rename-out-to-out &&
1079 test_all_match git stash &&
1080 test_all_match git status --porcelain=v2 &&
1082 # Apply the sparse directory stash without reinstating the index
1083 test_all_match git stash apply -q &&
1084 test_all_match git status --porcelain=v2 &&
1086 # Reset to state where stash can be applied
1087 test_sparse_match git sparse-checkout reapply &&
1088 test_all_match git reset --hard rename-out-to-out &&
1090 # Apply the sparse directory stash *with* reinstating the index
1091 test_all_match git stash apply --index -q &&
1092 test_all_match git status --porcelain=v2 &&
1094 # Reset to state where we will get a conflict applying the stash
1095 test_sparse_match git sparse-checkout reapply &&
1096 test_all_match git reset --hard update-folder1 &&
1098 # Apply the sparse directory stash with conflicts
1099 test_all_match test_must_fail git stash apply --index -q &&
1100 test_all_match test_must_fail git stash apply -q &&
1101 test_all_match git status --porcelain=v2 &&
1103 # Reset to base branch
1104 test_sparse_match git sparse-checkout reapply &&
1105 test_all_match git reset --hard base &&
1107 # Stash & unstash an untracked file outside of the sparse checkout
1108 # definition.
1109 run_on_sparse mkdir -p folder1 &&
1110 run_on_all ../edit-contents folder1/new &&
1111 test_all_match git stash -u &&
1112 test_all_match git status --porcelain=v2 &&
1114 test_all_match git stash pop -q &&
1115 test_all_match git status --porcelain=v2
1118 test_expect_success 'checkout-index inside sparse definition' '
1119 init_repos &&
1121 run_on_all rm -f deep/a &&
1122 test_all_match git checkout-index -- deep/a &&
1123 test_all_match git status --porcelain=v2 &&
1125 echo test >>new-a &&
1126 run_on_all cp ../new-a a &&
1127 test_all_match test_must_fail git checkout-index -- a &&
1128 test_all_match git checkout-index -f -- a &&
1129 test_all_match git status --porcelain=v2
1132 test_expect_success 'checkout-index outside sparse definition' '
1133 init_repos &&
1135 # Without --ignore-skip-worktree-bits, outside-of-cone files will trigger
1136 # an error
1137 test_sparse_match test_must_fail git checkout-index -- folder1/a &&
1138 test_i18ngrep "folder1/a has skip-worktree enabled" sparse-checkout-err &&
1139 test_path_is_missing folder1/a &&
1141 # With --ignore-skip-worktree-bits, outside-of-cone files are checked out
1142 test_sparse_match git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
1143 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
1144 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a &&
1146 run_on_sparse rm -rf folder1 &&
1147 echo test >new-a &&
1148 run_on_sparse mkdir -p folder1 &&
1149 run_on_all cp ../new-a folder1/a &&
1151 test_all_match test_must_fail git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
1152 test_all_match git checkout-index -f --ignore-skip-worktree-bits -- folder1/a &&
1153 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
1154 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a
1157 test_expect_success 'checkout-index with folders' '
1158 init_repos &&
1160 # Inside checkout definition
1161 test_all_match test_must_fail git checkout-index -f -- deep/ &&
1163 # Outside checkout definition
1164 # Note: although all tests fail (as expected), the messaging differs. For
1165 # non-sparse index checkouts, the error is that the "file" does not appear
1166 # in the index; for sparse checkouts, the error is explicitly that the
1167 # entry is a sparse directory.
1168 run_on_all test_must_fail git checkout-index -f -- folder1/ &&
1169 test_cmp full-checkout-err sparse-checkout-err &&
1170 ! test_cmp full-checkout-err sparse-index-err &&
1171 grep "is a sparse directory" sparse-index-err
1174 test_expect_success 'checkout-index --all' '
1175 init_repos &&
1177 test_all_match git checkout-index --all &&
1178 test_sparse_match test_path_is_missing folder1 &&
1180 # --ignore-skip-worktree-bits will cause `skip-worktree` files to be
1181 # checked out, causing the outside-of-cone `folder1` to exist on-disk
1182 test_all_match git checkout-index --ignore-skip-worktree-bits --all &&
1183 test_all_match test_path_exists folder1
1186 test_expect_success 'clean' '
1187 init_repos &&
1189 echo bogus >>.gitignore &&
1190 run_on_all cp ../.gitignore . &&
1191 test_all_match git add .gitignore &&
1192 test_all_match git commit -m "ignore bogus files" &&
1194 run_on_sparse mkdir folder1 &&
1195 run_on_all mkdir -p deep/untracked-deep &&
1196 run_on_all touch folder1/bogus &&
1197 run_on_all touch folder1/untracked &&
1198 run_on_all touch deep/untracked-deep/bogus &&
1199 run_on_all touch deep/untracked-deep/untracked &&
1201 test_all_match git status --porcelain=v2 &&
1202 test_all_match git clean -f &&
1203 test_all_match git status --porcelain=v2 &&
1204 test_sparse_match ls &&
1205 test_sparse_match ls folder1 &&
1206 run_on_all test_path_exists folder1/bogus &&
1207 run_on_all test_path_is_missing folder1/untracked &&
1208 run_on_all test_path_exists deep/untracked-deep/bogus &&
1209 run_on_all test_path_exists deep/untracked-deep/untracked &&
1211 test_all_match git clean -fd &&
1212 test_all_match git status --porcelain=v2 &&
1213 test_sparse_match ls &&
1214 test_sparse_match ls folder1 &&
1215 run_on_all test_path_exists folder1/bogus &&
1216 run_on_all test_path_exists deep/untracked-deep/bogus &&
1217 run_on_all test_path_is_missing deep/untracked-deep/untracked &&
1219 test_all_match git clean -xf &&
1220 test_all_match git status --porcelain=v2 &&
1221 test_sparse_match ls &&
1222 test_sparse_match ls folder1 &&
1223 run_on_all test_path_is_missing folder1/bogus &&
1224 run_on_all test_path_exists deep/untracked-deep/bogus &&
1226 test_all_match git clean -xdf &&
1227 test_all_match git status --porcelain=v2 &&
1228 test_sparse_match ls &&
1229 test_sparse_match ls folder1 &&
1230 run_on_all test_path_is_missing deep/untracked-deep/bogus &&
1232 test_sparse_match test_path_is_dir folder1
1235 for builtin in show rev-parse
1237 test_expect_success "$builtin (cached blobs/trees)" "
1238 init_repos &&
1240 test_all_match git $builtin :a &&
1241 test_all_match git $builtin :deep/a &&
1242 test_sparse_match git $builtin :folder1/a &&
1244 # The error message differs depending on whether
1245 # the directory exists in the worktree.
1246 test_all_match test_must_fail git $builtin :deep/ &&
1247 test_must_fail git -C full-checkout $builtin :folder1/ &&
1248 test_sparse_match test_must_fail git $builtin :folder1/ &&
1250 # Change the sparse cone for an extra case:
1251 run_on_sparse git sparse-checkout set deep/deeper1 &&
1253 # deep/deeper2 is a sparse directory in the sparse index.
1254 test_sparse_match test_must_fail git $builtin :deep/deeper2/ &&
1256 # deep/deeper2/deepest is not in the sparse index, but
1257 # will trigger an index expansion.
1258 test_sparse_match test_must_fail git $builtin :deep/deeper2/deepest/
1260 done
1262 test_expect_success 'submodule handling' '
1263 init_repos &&
1265 test_sparse_match git sparse-checkout add modules &&
1266 test_all_match mkdir modules &&
1267 test_all_match touch modules/a &&
1268 test_all_match git add modules &&
1269 test_all_match git commit -m "add modules directory" &&
1271 run_on_all git submodule add "$(pwd)/initial-repo" modules/sub &&
1272 test_all_match git commit -m "add submodule" &&
1274 # having a submodule prevents "modules" from collapse
1275 test_sparse_match git sparse-checkout set deep/deeper1 &&
1276 git -C sparse-index ls-files --sparse --stage >cache &&
1277 grep "100644 .* modules/a" cache &&
1278 grep "160000 $(git -C initial-repo rev-parse HEAD) 0 modules/sub" cache
1281 # When working with a sparse index, some commands will need to expand the
1282 # index to operate properly. If those commands also write the index back
1283 # to disk, they need to convert the index to sparse before writing.
1284 # This test verifies that both of these events are logged in trace2 logs.
1285 test_expect_success 'sparse-index is expanded and converted back' '
1286 init_repos &&
1288 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1289 git -C sparse-index reset -- folder1/a &&
1290 test_region index convert_to_sparse trace2.txt &&
1291 test_region index ensure_full_index trace2.txt &&
1293 # ls-files expands on read, but does not write.
1294 rm trace2.txt &&
1295 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1296 git -C sparse-index ls-files &&
1297 test_region index ensure_full_index trace2.txt
1300 test_expect_success 'index.sparse disabled inline uses full index' '
1301 init_repos &&
1303 # When index.sparse is disabled inline with `git status`, the
1304 # index is expanded at the beginning of the execution then never
1305 # converted back to sparse. It is then written to disk as a full index.
1306 rm -f trace2.txt &&
1307 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1308 git -C sparse-index -c index.sparse=false status &&
1309 ! test_region index convert_to_sparse trace2.txt &&
1310 test_region index ensure_full_index trace2.txt &&
1312 # Since index.sparse is set to true at a repo level, the index
1313 # is converted from full to sparse when read, then never expanded
1314 # over the course of `git status`. It is written to disk as a sparse
1315 # index.
1316 rm -f trace2.txt &&
1317 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1318 git -C sparse-index status &&
1319 test_region index convert_to_sparse trace2.txt &&
1320 ! test_region index ensure_full_index trace2.txt &&
1322 # Now that the index has been written to disk as sparse, it is not
1323 # converted to sparse (or expanded to full) when read by `git status`.
1324 rm -f trace2.txt &&
1325 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1326 git -C sparse-index status &&
1327 ! test_region index convert_to_sparse trace2.txt &&
1328 ! test_region index ensure_full_index trace2.txt
1331 ensure_not_expanded () {
1332 rm -f trace2.txt &&
1333 if test -z "$WITHOUT_UNTRACKED_TXT"
1334 then
1335 echo >>sparse-index/untracked.txt
1336 fi &&
1338 if test "$1" = "!"
1339 then
1340 shift &&
1341 test_must_fail env \
1342 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1343 git -C sparse-index "$@" || return 1
1344 else
1345 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1346 git -C sparse-index "$@" || return 1
1347 fi &&
1348 test_region ! index ensure_full_index trace2.txt
1351 test_expect_success 'sparse-index is not expanded' '
1352 init_repos &&
1354 ensure_not_expanded status &&
1355 ensure_not_expanded ls-files --sparse &&
1356 ensure_not_expanded commit --allow-empty -m empty &&
1357 echo >>sparse-index/a &&
1358 ensure_not_expanded commit -a -m a &&
1359 echo >>sparse-index/a &&
1360 ensure_not_expanded commit --include a -m a &&
1361 echo >>sparse-index/deep/deeper1/a &&
1362 ensure_not_expanded commit --include deep/deeper1/a -m deeper &&
1363 ensure_not_expanded checkout rename-out-to-out &&
1364 ensure_not_expanded checkout - &&
1365 ensure_not_expanded switch rename-out-to-out &&
1366 ensure_not_expanded switch - &&
1367 ensure_not_expanded reset --hard &&
1368 ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
1369 ensure_not_expanded reset --hard &&
1370 ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
1372 echo >>sparse-index/README.md &&
1373 ensure_not_expanded add -A &&
1374 echo >>sparse-index/extra.txt &&
1375 ensure_not_expanded add extra.txt &&
1376 echo >>sparse-index/untracked.txt &&
1377 ensure_not_expanded add . &&
1379 ensure_not_expanded checkout-index -f a &&
1380 ensure_not_expanded checkout-index -f --all &&
1381 for ref in update-deep update-folder1 update-folder2 update-deep
1383 echo >>sparse-index/README.md &&
1384 ensure_not_expanded reset --hard $ref || return 1
1385 done &&
1387 ensure_not_expanded reset --mixed base &&
1388 ensure_not_expanded reset --hard update-deep &&
1389 ensure_not_expanded reset --keep base &&
1390 ensure_not_expanded reset --merge update-deep &&
1391 ensure_not_expanded reset --hard &&
1393 ensure_not_expanded reset base -- deep/a &&
1394 ensure_not_expanded reset base -- nonexistent-file &&
1395 ensure_not_expanded reset deepest -- deep &&
1397 # Although folder1 is outside the sparse definition, it exists as a
1398 # directory entry in the index, so the pathspec will not force the
1399 # index to be expanded.
1400 ensure_not_expanded reset deepest -- folder1 &&
1401 ensure_not_expanded reset deepest -- folder1/ &&
1403 # Wildcard identifies only in-cone files, no index expansion
1404 ensure_not_expanded reset deepest -- deep/\* &&
1406 # Wildcard identifies only full sparse directories, no index expansion
1407 ensure_not_expanded reset deepest -- folder\* &&
1409 ensure_not_expanded clean -fd &&
1411 ensure_not_expanded checkout -f update-deep &&
1412 test_config -C sparse-index pull.twohead ort &&
1414 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1415 for OPERATION in "merge -m merge" cherry-pick rebase
1417 ensure_not_expanded merge -m merge update-folder1 &&
1418 ensure_not_expanded merge -m merge update-folder2 || return 1
1419 done
1423 test_expect_success 'sparse-index is not expanded: merge conflict in cone' '
1424 init_repos &&
1426 for side in right left
1428 git -C sparse-index checkout -b expand-$side base &&
1429 echo $side >sparse-index/deep/a &&
1430 git -C sparse-index commit -a -m "$side" || return 1
1431 done &&
1434 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1435 git -C sparse-index config pull.twohead ort &&
1436 ensure_not_expanded ! merge -m merged expand-right
1440 test_expect_success 'sparse-index is not expanded: stash' '
1441 init_repos &&
1443 echo >>sparse-index/a &&
1444 ensure_not_expanded stash &&
1445 ensure_not_expanded stash list &&
1446 ensure_not_expanded stash show stash@{0} &&
1447 ensure_not_expanded stash apply stash@{0} &&
1448 ensure_not_expanded stash drop stash@{0} &&
1450 echo >>sparse-index/deep/new &&
1451 ensure_not_expanded stash -u &&
1453 WITHOUT_UNTRACKED_TXT=1 &&
1454 ensure_not_expanded stash pop
1455 ) &&
1457 ensure_not_expanded stash create &&
1458 oid=$(git -C sparse-index stash create) &&
1459 ensure_not_expanded stash store -m "test" $oid &&
1460 ensure_not_expanded reset --hard &&
1461 ensure_not_expanded stash pop
1464 test_expect_success 'sparse index is not expanded: diff' '
1465 init_repos &&
1467 write_script edit-contents <<-\EOF &&
1468 echo text >>$1
1471 # Add file within cone
1472 test_sparse_match git sparse-checkout set deep &&
1473 run_on_all ../edit-contents deep/testfile &&
1474 test_all_match git add deep/testfile &&
1475 run_on_all ../edit-contents deep/testfile &&
1477 test_all_match git diff &&
1478 test_all_match git diff --cached &&
1479 ensure_not_expanded diff &&
1480 ensure_not_expanded diff --cached &&
1482 # Add file outside cone
1483 test_all_match git reset --hard &&
1484 run_on_all mkdir newdirectory &&
1485 run_on_all ../edit-contents newdirectory/testfile &&
1486 test_sparse_match git sparse-checkout set newdirectory &&
1487 test_all_match git add newdirectory/testfile &&
1488 run_on_all ../edit-contents newdirectory/testfile &&
1489 test_sparse_match git sparse-checkout set &&
1491 test_all_match git diff &&
1492 test_all_match git diff --cached &&
1493 ensure_not_expanded diff &&
1494 ensure_not_expanded diff --cached &&
1496 # Merge conflict outside cone
1497 # The sparse checkout will report a warning that is not in the
1498 # full checkout, so we use `run_on_all` instead of
1499 # `test_all_match`
1500 run_on_all git reset --hard &&
1501 test_all_match git checkout merge-left &&
1502 test_all_match test_must_fail git merge merge-right &&
1504 test_all_match git diff &&
1505 test_all_match git diff --cached &&
1506 ensure_not_expanded diff &&
1507 ensure_not_expanded diff --cached
1510 test_expect_success 'sparse index is not expanded: show and rev-parse' '
1511 init_repos &&
1513 ensure_not_expanded show :a &&
1514 ensure_not_expanded show :deep/a &&
1515 ensure_not_expanded rev-parse :a &&
1516 ensure_not_expanded rev-parse :deep/a
1519 test_expect_success 'sparse index is not expanded: update-index' '
1520 init_repos &&
1522 deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
1523 ensure_not_expanded update-index --cacheinfo 100644 $deep_a_oid deep/a &&
1525 echo "test" >sparse-index/README.md &&
1526 echo "test2" >sparse-index/a &&
1527 rm -f sparse-index/deep/a &&
1529 ensure_not_expanded update-index --add README.md &&
1530 ensure_not_expanded update-index a &&
1531 ensure_not_expanded update-index --remove deep/a &&
1533 ensure_not_expanded reset --soft update-deep &&
1534 ensure_not_expanded update-index --add --remove --again
1537 test_expect_success 'sparse index is not expanded: blame' '
1538 init_repos &&
1540 for file in a \
1541 deep/a \
1542 deep/deeper1/a \
1543 deep/deeper1/deepest/a
1545 ensure_not_expanded blame $file
1546 done
1549 test_expect_success 'sparse index is not expanded: fetch/pull' '
1550 init_repos &&
1552 git -C sparse-index remote add full "file://$(pwd)/full-checkout" &&
1553 ensure_not_expanded fetch full &&
1554 git -C full-checkout commit --allow-empty -m "for pull merge" &&
1555 git -C sparse-index commit --allow-empty -m "for pull merge" &&
1556 ensure_not_expanded pull full base
1559 test_expect_success 'sparse index is not expanded: read-tree' '
1560 init_repos &&
1562 ensure_not_expanded checkout -b test-branch update-folder1 &&
1563 for MERGE_TREES in "base HEAD update-folder2" \
1564 "base HEAD rename-base" \
1565 "base update-folder2" \
1566 "base rename-base" \
1567 "update-folder2"
1569 ensure_not_expanded read-tree -mu $MERGE_TREES &&
1570 ensure_not_expanded reset --hard || return 1
1571 done &&
1573 rm -rf sparse-index/deep/deeper2 &&
1574 ensure_not_expanded add . &&
1575 ensure_not_expanded commit -m "test" &&
1577 ensure_not_expanded read-tree --prefix=deep/deeper2 -u deepest
1580 test_expect_success 'ls-files' '
1581 init_repos &&
1583 # Use a smaller sparse-checkout for reduced output
1584 test_sparse_match git sparse-checkout set &&
1586 # Behavior agrees by default. Sparse index is expanded.
1587 test_all_match git ls-files &&
1589 # With --sparse, the sparse index data changes behavior.
1590 git -C sparse-index ls-files --sparse >actual &&
1592 cat >expect <<-\EOF &&
1594 before/
1595 deep/
1597 folder1-
1598 folder1.x
1599 folder1/
1600 folder10
1601 folder2/
1607 test_cmp expect actual &&
1609 # With --sparse and no sparse index, nothing changes.
1610 git -C sparse-checkout ls-files >dense &&
1611 git -C sparse-checkout ls-files --sparse >sparse &&
1612 test_cmp dense sparse &&
1614 # Set up a strange condition of having a file edit
1615 # outside of the sparse-checkout cone. We want to verify
1616 # that all modes handle this the same, and detect the
1617 # modification.
1618 write_script edit-content <<-\EOF &&
1619 mkdir -p folder1 &&
1620 echo content >>folder1/a
1622 run_on_all ../edit-content &&
1624 test_all_match git ls-files --modified &&
1626 git -C sparse-index ls-files --sparse --modified >sparse-index-out &&
1627 cat >expect <<-\EOF &&
1628 folder1/a
1630 test_cmp expect sparse-index-out &&
1632 # Add folder1 to the sparse-checkout cone and
1633 # check that ls-files shows the expanded files.
1634 test_sparse_match git sparse-checkout add folder1 &&
1635 test_all_match git ls-files --modified &&
1637 test_all_match git ls-files &&
1638 git -C sparse-index ls-files --sparse >actual &&
1640 cat >expect <<-\EOF &&
1642 before/
1643 deep/
1645 folder1-
1646 folder1.x
1647 folder1/0/0/0
1648 folder1/0/1
1649 folder1/a
1650 folder10
1651 folder2/
1657 test_cmp expect actual &&
1659 # Double-check index expansion is avoided
1660 ensure_not_expanded ls-files --sparse
1663 test_expect_success 'sparse index is not expanded: sparse-checkout' '
1664 init_repos &&
1666 ensure_not_expanded sparse-checkout set deep/deeper2 &&
1667 ensure_not_expanded sparse-checkout set deep/deeper1 &&
1668 ensure_not_expanded sparse-checkout set deep &&
1669 ensure_not_expanded sparse-checkout add folder1 &&
1670 ensure_not_expanded sparse-checkout set deep/deeper1 &&
1671 ensure_not_expanded sparse-checkout set folder2 &&
1673 # Demonstrate that the checks that "folder1/a" is a file
1674 # do not cause a sparse-index expansion (since it is in the
1675 # sparse-checkout cone).
1676 echo >>sparse-index/folder2/a &&
1677 git -C sparse-index add folder2/a &&
1679 ensure_not_expanded sparse-checkout add folder1 &&
1681 # Skip checks here, since deep/deeper1 is inside a sparse directory
1682 # that must be expanded to check whether `deep/deeper1` is a file
1683 # or not.
1684 ensure_not_expanded sparse-checkout set --skip-checks deep/deeper1 &&
1685 ensure_not_expanded sparse-checkout set
1688 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
1689 # in this scenario, but it shouldn't.
1690 test_expect_success 'reset mixed and checkout orphan' '
1691 init_repos &&
1693 test_all_match git checkout rename-out-to-in &&
1695 # Sparse checkouts do not agree with full checkouts about
1696 # how to report a directory/file conflict during a reset.
1697 # This command would fail with test_all_match because the
1698 # full checkout reports "T folder1/0/1" while a sparse
1699 # checkout reports "D folder1/0/1". This matches because
1700 # the sparse checkouts skip "adding" the other side of
1701 # the conflict.
1702 test_sparse_match git reset --mixed HEAD~1 &&
1703 test_sparse_match git ls-files --stage &&
1704 test_sparse_match git status --porcelain=v2 &&
1706 # At this point, sparse-checkouts behave differently
1707 # from the full-checkout.
1708 test_sparse_match git checkout --orphan new-branch &&
1709 test_sparse_match git ls-files --stage &&
1710 test_sparse_match git status --porcelain=v2
1713 test_expect_success 'add everything with deep new file' '
1714 init_repos &&
1716 run_on_sparse git sparse-checkout set deep/deeper1/deepest &&
1718 run_on_all touch deep/deeper1/x &&
1719 test_all_match git add . &&
1720 test_all_match git status --porcelain=v2
1723 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1724 # directory/file conflicts, even without sparse-checkout. Use this
1725 # test only as a documentation of the incorrect behavior, not a
1726 # measure of how it _should_ behave.
1727 test_expect_success 'checkout behaves oddly with df-conflict-1' '
1728 init_repos &&
1730 test_sparse_match git sparse-checkout disable &&
1732 write_script edit-content <<-\EOF &&
1733 echo content >>folder1/larger-content
1734 git add folder1
1737 run_on_all ../edit-content &&
1738 test_all_match git status --porcelain=v2 &&
1740 git -C sparse-checkout sparse-checkout init --cone &&
1741 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1743 test_all_match git status --porcelain=v2 &&
1745 # This checkout command should fail, because we have a staged
1746 # change to folder1/larger-content, but the destination changes
1747 # folder1 to a file.
1748 git -C full-checkout checkout df-conflict-1 \
1749 1>full-checkout-out \
1750 2>full-checkout-err &&
1751 git -C sparse-checkout checkout df-conflict-1 \
1752 1>sparse-checkout-out \
1753 2>sparse-checkout-err &&
1754 git -C sparse-index checkout df-conflict-1 \
1755 1>sparse-index-out \
1756 2>sparse-index-err &&
1758 # Instead, the checkout deletes the folder1 file and adds the
1759 # folder1/larger-content file, leaving all other paths that were
1760 # in folder1/ as deleted (without any warning).
1761 cat >expect <<-EOF &&
1762 D folder1
1763 A folder1/larger-content
1765 test_cmp expect full-checkout-out &&
1766 test_cmp expect sparse-checkout-out &&
1768 # The sparse-index reports no output
1769 test_must_be_empty sparse-index-out &&
1771 # stderr: Switched to branch df-conflict-1
1772 test_cmp full-checkout-err sparse-checkout-err &&
1773 test_cmp full-checkout-err sparse-checkout-err
1776 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1777 # directory/file conflicts, even without sparse-checkout. Use this
1778 # test only as a documentation of the incorrect behavior, not a
1779 # measure of how it _should_ behave.
1780 test_expect_success 'checkout behaves oddly with df-conflict-2' '
1781 init_repos &&
1783 test_sparse_match git sparse-checkout disable &&
1785 write_script edit-content <<-\EOF &&
1786 echo content >>folder2/larger-content
1787 git add folder2
1790 run_on_all ../edit-content &&
1791 test_all_match git status --porcelain=v2 &&
1793 git -C sparse-checkout sparse-checkout init --cone &&
1794 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1796 test_all_match git status --porcelain=v2 &&
1798 # This checkout command should fail, because we have a staged
1799 # change to folder1/larger-content, but the destination changes
1800 # folder1 to a file.
1801 git -C full-checkout checkout df-conflict-2 \
1802 1>full-checkout-out \
1803 2>full-checkout-err &&
1804 git -C sparse-checkout checkout df-conflict-2 \
1805 1>sparse-checkout-out \
1806 2>sparse-checkout-err &&
1807 git -C sparse-index checkout df-conflict-2 \
1808 1>sparse-index-out \
1809 2>sparse-index-err &&
1811 # The full checkout deviates from the df-conflict-1 case here!
1812 # It drops the change to folder1/larger-content and leaves the
1813 # folder1 path as-is on disk. The sparse-index behaves the same.
1814 test_must_be_empty full-checkout-out &&
1815 test_must_be_empty sparse-index-out &&
1817 # In the sparse-checkout case, the checkout deletes the folder1
1818 # file and adds the folder1/larger-content file, leaving all other
1819 # paths that were in folder1/ as deleted (without any warning).
1820 cat >expect <<-EOF &&
1821 D folder2
1822 A folder2/larger-content
1824 test_cmp expect sparse-checkout-out &&
1826 # Switched to branch df-conflict-1
1827 test_cmp full-checkout-err sparse-checkout-err &&
1828 test_cmp full-checkout-err sparse-index-err
1831 test_done