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