3 test_description
='compare full workdir to sparse workdir'
10 test_expect_success
'setup' '
11 git init initial-repo &&
13 GIT_TEST_SPARSE_INDEX=0 &&
16 echo "after deep" >e &&
17 echo "after folder1" >g &&
19 mkdir folder1 folder2 deep x &&
20 mkdir deep/deeper1 deep/deeper2 deep/before deep/later &&
21 mkdir deep/deeper1/deepest &&
22 echo "after deeper1" >deep/e &&
23 echo "after deepest" >deep/deeper1/e &&
32 cp a deep/deeper1/deepest &&
33 cp -r deep/deeper1/deepest deep/deeper2 &&
34 mkdir deep/deeper1/0 &&
35 mkdir deep/deeper1/0/0 &&
36 touch deep/deeper1/0/1 &&
37 touch deep/deeper1/0/0/0 &&
41 cp -r deep/deeper1/0 folder1 &&
42 cp -r deep/deeper1/0 folder2 &&
43 echo >>folder1/0/0/0 &&
46 git commit -m "initial commit" &&
47 git checkout -b base &&
48 for dir in folder1 folder2 deep
50 git checkout -b update-$dir &&
51 echo "updated $dir" >$dir/a &&
52 git commit -a -m "update $dir" || return 1
55 git checkout -b rename-base base &&
56 cat >folder1/larger-content <<-\EOF &&
63 cp folder1/larger-content folder2/ &&
64 cp folder1/larger-content deep/deeper1/ &&
66 git commit -m "add interesting rename content" &&
68 git checkout -b rename-out-to-out rename-base &&
69 mv folder1/a folder2/b &&
70 mv folder1/larger-content folder2/edited-content &&
71 echo >>folder2/edited-content &&
73 echo stuff >>deep/deeper1/a &&
75 git commit -m "rename folder1/... to folder2/..." &&
77 git checkout -b rename-out-to-in rename-base &&
78 mv folder1/a deep/deeper1/b &&
79 echo more stuff >>deep/deeper1/a &&
82 echo >>folder2/0/1/1 &&
83 mv folder1/larger-content deep/deeper1/edited-content &&
84 echo >>deep/deeper1/edited-content &&
86 git commit -m "rename folder1/... to deep/deeper1/..." &&
88 git checkout -b rename-in-to-out rename-base &&
89 mv deep/deeper1/a folder1/b &&
93 mv deep/deeper1/larger-content folder1/edited-content &&
94 echo >>folder1/edited-content &&
96 git commit -m "rename deep/deeper1/... to folder1/..." &&
98 git checkout -b df-conflict-1 base &&
100 echo content >folder1 &&
102 git commit -m "dir to file" &&
104 git checkout -b df-conflict-2 base &&
106 echo content >folder2 &&
108 git commit -m "dir to file" &&
110 git checkout -b fd-conflict base &&
115 git commit -m "file to dir" &&
117 for side in left right
119 git checkout -b merge-$side base &&
120 echo $side >>deep/deeper2/a &&
121 echo $side >>folder1/a &&
122 echo $side >>folder2/a &&
124 git commit -m "$side" || return 1
127 git checkout -b deepest base &&
128 echo "updated deepest" >deep/deeper1/deepest/a &&
129 git commit -a -m "update deepest" &&
131 git checkout -f base &&
137 rm -rf full-checkout sparse-checkout sparse-index
&&
139 # create repos in initial state
140 cp -r initial-repo full-checkout
&&
141 git
-C full-checkout
reset --hard &&
143 cp -r initial-repo sparse-checkout
&&
144 git
-C sparse-checkout
reset --hard &&
146 cp -r initial-repo sparse-index
&&
147 git
-C sparse-index
reset --hard &&
149 # initialize sparse-checkout definitions
150 git
-C sparse-checkout sparse-checkout init
--cone &&
151 git
-C sparse-checkout sparse-checkout
set deep
&&
152 git
-C sparse-index sparse-checkout init
--cone --sparse-index &&
153 test_cmp_config
-C sparse-index true index.sparse
&&
154 git
-C sparse-index sparse-checkout
set deep
159 cd sparse-checkout
&&
160 GIT_PROGRESS_DELAY
=100000 "$@" >..
/sparse-checkout-out
2>..
/sparse-checkout-err
164 GIT_PROGRESS_DELAY
=100000 "$@" >..
/sparse-index-out
2>..
/sparse-index-err
171 GIT_PROGRESS_DELAY
=100000 "$@" >..
/full-checkout-out
2>..
/full-checkout-err
178 test_cmp full-checkout-out sparse-checkout-out
&&
179 test_cmp full-checkout-out sparse-index-out
&&
180 test_cmp full-checkout-err sparse-checkout-err
&&
181 test_cmp full-checkout-err sparse-index-err
184 test_sparse_match
() {
185 run_on_sparse
"$@" &&
186 test_cmp sparse-checkout-out sparse-index-out
&&
187 test_cmp sparse-checkout-err sparse-index-err
190 test_expect_success
'sparse-index contents' '
193 test-tool -C sparse-index read-cache --table >cache &&
194 for dir in folder1 folder2 x
196 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
197 grep "040000 tree $TREE $dir/" cache \
201 git -C sparse-index sparse-checkout set folder1 &&
203 test-tool -C sparse-index read-cache --table >cache &&
204 for dir in deep folder2 x
206 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
207 grep "040000 tree $TREE $dir/" cache \
211 git -C sparse-index sparse-checkout set deep/deeper1 &&
213 test-tool -C sparse-index read-cache --table >cache &&
214 for dir in deep/deeper2 folder1 folder2 x
216 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
217 grep "040000 tree $TREE $dir/" cache \
221 # Disabling the sparse-index removes tree entries with full ones
222 git -C sparse-index sparse-checkout init --no-sparse-index &&
224 test-tool -C sparse-index read-cache --table >cache &&
225 ! grep "040000 tree" cache &&
226 test_sparse_match test-tool read-cache --table
229 test_expect_success
'expanded in-memory index matches full index' '
231 test_sparse_match test-tool read-cache --expand --table
234 test_expect_success
'status with options' '
236 test_sparse_match ls &&
237 test_all_match git status --porcelain=v2 &&
238 test_all_match git status --porcelain=v2 -z -u &&
239 test_all_match git status --porcelain=v2 -uno &&
240 run_on_all touch README.md &&
241 test_all_match git status --porcelain=v2 &&
242 test_all_match git status --porcelain=v2 -z -u &&
243 test_all_match git status --porcelain=v2 -uno &&
244 test_all_match git add README.md &&
245 test_all_match git status --porcelain=v2 &&
246 test_all_match git status --porcelain=v2 -z -u &&
247 test_all_match git status --porcelain=v2 -uno
250 test_expect_success
'status reports sparse-checkout' '
252 git -C sparse-checkout status >full &&
253 git -C sparse-index status >sparse &&
254 test_i18ngrep "You are in a sparse checkout with " full &&
255 test_i18ngrep "You are in a sparse checkout." sparse
258 test_expect_success
'add, commit, checkout' '
261 write_script edit-contents <<-\EOF &&
264 run_on_all ../edit-contents README.md &&
266 test_all_match git add README.md &&
267 test_all_match git status --porcelain=v2 &&
268 test_all_match git commit -m "Add README.md" &&
270 test_all_match git checkout HEAD~1 &&
271 test_all_match git checkout - &&
273 run_on_all ../edit-contents README.md &&
275 test_all_match git add -A &&
276 test_all_match git status --porcelain=v2 &&
277 test_all_match git commit -m "Extend README.md" &&
279 test_all_match git checkout HEAD~1 &&
280 test_all_match git checkout - &&
282 run_on_all ../edit-contents deep/newfile &&
284 test_all_match git status --porcelain=v2 -uno &&
285 test_all_match git status --porcelain=v2 &&
286 test_all_match git add . &&
287 test_all_match git status --porcelain=v2 &&
288 test_all_match git commit -m "add deep/newfile" &&
290 test_all_match git checkout HEAD~1 &&
291 test_all_match git checkout -
294 test_expect_success
'commit including unstaged changes' '
297 write_script edit-file <<-\EOF &&
301 run_on_all ../edit-file 1 a &&
302 run_on_all ../edit-file 1 deep/a &&
304 test_all_match git commit -m "-a" -a &&
305 test_all_match git status --porcelain=v2 &&
307 run_on_all ../edit-file 2 a &&
308 run_on_all ../edit-file 2 deep/a &&
310 test_all_match git commit -m "--include" --include deep/a &&
311 test_all_match git status --porcelain=v2 &&
312 test_all_match git commit -m "--include" --include a &&
313 test_all_match git status --porcelain=v2 &&
315 run_on_all ../edit-file 3 a &&
316 run_on_all ../edit-file 3 deep/a &&
318 test_all_match git commit -m "--amend" -a --amend &&
319 test_all_match git status --porcelain=v2
322 test_expect_success
'status/add: outside sparse cone' '
325 # folder1 is at HEAD, but outside the sparse cone
326 run_on_sparse mkdir folder1 &&
327 cp initial-repo/folder1/a sparse-checkout/folder1/a &&
328 cp initial-repo/folder1/a sparse-index/folder1/a &&
330 test_sparse_match git status &&
332 write_script edit-contents <<-\EOF &&
335 run_on_sparse ../edit-contents folder1/a &&
336 run_on_all ../edit-contents folder1/new &&
338 test_sparse_match git status --porcelain=v2 &&
340 # Adding the path outside of the sparse-checkout cone should fail.
341 test_sparse_match test_must_fail git add folder1/a &&
342 test_sparse_match test_must_fail git add --refresh folder1/a &&
344 # NEEDSWORK: Adding a newly-tracked file outside the cone succeeds
345 test_sparse_match git add folder1/new &&
347 test_all_match git add . &&
348 test_all_match git status --porcelain=v2 &&
349 test_all_match git commit -m folder1/new &&
350 test_all_match git rev-parse HEAD^{tree} &&
352 run_on_all ../edit-contents folder1/newer &&
353 test_all_match git add folder1/ &&
354 test_all_match git status --porcelain=v2 &&
355 test_all_match git commit -m folder1/newer &&
356 test_all_match git rev-parse HEAD^{tree}
359 test_expect_success
'checkout and reset --hard' '
362 test_all_match git checkout update-folder1 &&
363 test_all_match git status --porcelain=v2 &&
365 test_all_match git checkout update-deep &&
366 test_all_match git status --porcelain=v2 &&
368 test_all_match git checkout -b reset-test &&
369 test_all_match git reset --hard deepest &&
370 test_all_match git reset --hard update-folder1 &&
371 test_all_match git reset --hard update-folder2
374 test_expect_success
'diff --staged' '
377 write_script edit-contents <<-\EOF &&
378 echo text >>README.md
380 run_on_all ../edit-contents &&
382 test_all_match git diff &&
383 test_all_match git diff --staged &&
384 test_all_match git add README.md &&
385 test_all_match git diff &&
386 test_all_match git diff --staged
389 # NEEDSWORK: sparse-checkout behaves differently from full-checkout when
390 # running this test with 'df-conflict-2' after 'df-conflict-1'.
391 test_expect_success
'diff with renames and conflicts' '
394 for branch in rename-out-to-out \
400 test_all_match git checkout rename-base &&
401 test_all_match git checkout $branch -- . &&
402 test_all_match git status --porcelain=v2 &&
403 test_all_match git diff --staged --no-renames &&
404 test_all_match git diff --staged --find-renames || return 1
408 test_expect_success
'diff with directory/file conflicts' '
411 for branch in rename-out-to-out \
418 git -C full-checkout reset --hard &&
419 test_sparse_match git reset --hard &&
420 test_all_match git checkout $branch &&
421 test_all_match git checkout rename-base -- . &&
422 test_all_match git status --porcelain=v2 &&
423 test_all_match git diff --staged --no-renames &&
424 test_all_match git diff --staged --find-renames || return 1
428 test_expect_success
'log with pathspec outside sparse definition' '
431 test_all_match git log -- a &&
432 test_all_match git log -- folder1/a &&
433 test_all_match git log -- folder2/a &&
434 test_all_match git log -- deep/a &&
435 test_all_match git log -- deep/deeper1/a &&
436 test_all_match git log -- deep/deeper1/deepest/a &&
438 test_all_match git checkout update-folder1 &&
439 test_all_match git log -- folder1/a
442 test_expect_success
'blame with pathspec inside sparse definition' '
445 test_all_match git blame a &&
446 test_all_match git blame deep/a &&
447 test_all_match git blame deep/deeper1/a &&
448 test_all_match git blame deep/deeper1/deepest/a
451 # TODO: blame currently does not support blaming files outside of the
452 # sparse definition. It complains that the file doesn't exist locally.
453 test_expect_failure
'blame with pathspec outside sparse definition' '
456 test_all_match git blame folder1/a &&
457 test_all_match git blame folder2/a &&
458 test_all_match git blame deep/deeper2/a &&
459 test_all_match git blame deep/deeper2/deepest/a
462 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
463 # in this scenario, but it shouldn't.
464 test_expect_failure
'checkout and reset (mixed)' '
467 test_all_match git checkout -b reset-test update-deep &&
468 test_all_match git reset deepest &&
469 test_all_match git reset update-folder1 &&
470 test_all_match git reset update-folder2
473 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
474 # in this scenario, but it shouldn't.
475 test_expect_success
'checkout and reset (mixed) [sparse]' '
478 test_sparse_match git checkout -b reset-test update-deep &&
479 test_sparse_match git reset deepest &&
480 test_sparse_match git reset update-folder1 &&
481 test_sparse_match git reset update-folder2
484 test_expect_success
'merge' '
487 test_all_match git checkout -b merge update-deep &&
488 test_all_match git merge -m "folder1" update-folder1 &&
489 test_all_match git rev-parse HEAD^{tree} &&
490 test_all_match git merge -m "folder2" update-folder2 &&
491 test_all_match git rev-parse HEAD^{tree}
494 # NEEDSWORK: This test is documenting current behavior, but that
495 # behavior can be confusing to users so there is desire to change it.
496 # Right now, users might be using this flow to work through conflicts,
497 # so any solution should present advice to users who try this sequence
498 # of commands to follow whatever new method we create.
499 test_expect_success
'merge with conflict outside cone' '
502 test_all_match git checkout -b merge-tip merge-left &&
503 test_all_match git status --porcelain=v2 &&
504 test_all_match test_must_fail git merge -m merge merge-right &&
505 test_all_match git status --porcelain=v2 &&
507 # Resolve the conflict in different ways:
508 # 1. Revert to the base
509 test_all_match git checkout base -- deep/deeper2/a &&
510 test_all_match git status --porcelain=v2 &&
512 # 2. Add the file with conflict markers
513 test_all_match git add folder1/a &&
514 test_all_match git status --porcelain=v2 &&
516 # 3. Rename the file to another sparse filename and
517 # accept conflict markers as resolved content.
518 run_on_all mv folder2/a folder2/z &&
519 test_all_match git add folder2 &&
520 test_all_match git status --porcelain=v2 &&
522 test_all_match git merge --continue &&
523 test_all_match git status --porcelain=v2 &&
524 test_all_match git rev-parse HEAD^{tree}
527 test_expect_success
'merge with outside renames' '
530 for type in out-to-out out-to-in in-to-out
532 test_all_match git reset --hard &&
533 test_all_match git checkout -f -b merge-$type update-deep &&
534 test_all_match git merge -m "$type" rename-$type &&
535 test_all_match git rev-parse HEAD^{tree} || return 1
539 # Sparse-index fails to convert the index in the
540 # final 'git cherry-pick' command.
541 test_expect_success
'cherry-pick with conflicts' '
544 write_script edit-conflict <<-\EOF &&
548 test_all_match git checkout -b to-cherry-pick &&
549 run_on_all ../edit-conflict ABC &&
550 test_all_match git add conflict &&
551 test_all_match git commit -m "conflict to pick" &&
553 test_all_match git checkout -B base HEAD~1 &&
554 run_on_all ../edit-conflict DEF &&
555 test_all_match git add conflict &&
556 test_all_match git commit -m "conflict in base" &&
558 test_all_match test_must_fail git cherry-pick to-cherry-pick
561 test_expect_success
'clean' '
564 echo bogus >>.gitignore &&
565 run_on_all cp ../.gitignore . &&
566 test_all_match git add .gitignore &&
567 test_all_match git commit -m "ignore bogus files" &&
569 run_on_sparse mkdir folder1 &&
570 run_on_all touch folder1/bogus &&
572 test_all_match git status --porcelain=v2 &&
573 test_all_match git clean -f &&
574 test_all_match git status --porcelain=v2 &&
575 test_sparse_match ls &&
576 test_sparse_match ls folder1 &&
578 test_all_match git clean -xf &&
579 test_all_match git status --porcelain=v2 &&
580 test_sparse_match ls &&
581 test_sparse_match ls folder1 &&
583 test_all_match git clean -xdf &&
584 test_all_match git status --porcelain=v2 &&
585 test_sparse_match ls &&
586 test_sparse_match ls folder1 &&
588 test_sparse_match test_path_is_dir folder1
591 test_expect_success
'submodule handling' '
594 test_all_match mkdir modules &&
595 test_all_match touch modules/a &&
596 test_all_match git add modules &&
597 test_all_match git commit -m "add modules directory" &&
599 run_on_all git submodule add "$(pwd)/initial-repo" modules/sub &&
600 test_all_match git commit -m "add submodule" &&
602 # having a submodule prevents "modules" from collapse
603 test-tool -C sparse-index read-cache --table >cache &&
604 grep "100644 blob .* modules/a" cache &&
605 grep "160000 commit $(git -C initial-repo rev-parse HEAD) modules/sub" cache
608 test_expect_success
'sparse-index is expanded and converted back' '
611 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
612 git -C sparse-index -c core.fsmonitor="" reset --hard &&
613 test_region index convert_to_sparse trace2.txt &&
614 test_region index ensure_full_index trace2.txt
617 ensure_not_expanded
() {
619 echo >>sparse-index
/untracked.txt
&&
620 GIT_TRACE2_EVENT
="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING
=10 \
621 git
-C sparse-index
"$@" &&
622 test_region
! index ensure_full_index trace2.txt
625 test_expect_success
'sparse-index is not expanded' '
628 ensure_not_expanded status &&
629 ensure_not_expanded commit --allow-empty -m empty &&
630 echo >>sparse-index/a &&
631 ensure_not_expanded commit -a -m a &&
632 echo >>sparse-index/a &&
633 ensure_not_expanded commit --include a -m a &&
634 echo >>sparse-index/deep/deeper1/a &&
635 ensure_not_expanded commit --include deep/deeper1/a -m deeper &&
636 ensure_not_expanded checkout rename-out-to-out &&
637 ensure_not_expanded checkout - &&
638 ensure_not_expanded switch rename-out-to-out &&
639 ensure_not_expanded switch - &&
640 git -C sparse-index reset --hard &&
641 ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
642 git -C sparse-index reset --hard &&
643 ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
645 echo >>sparse-index/README.md &&
646 ensure_not_expanded add -A &&
647 echo >>sparse-index/extra.txt &&
648 ensure_not_expanded add extra.txt &&
649 echo >>sparse-index/untracked.txt &&
650 ensure_not_expanded add .
653 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
654 # in this scenario, but it shouldn't.
655 test_expect_success
'reset mixed and checkout orphan' '
658 test_all_match git checkout rename-out-to-in &&
660 # Sparse checkouts do not agree with full checkouts about
661 # how to report a directory/file conflict during a reset.
662 # This command would fail with test_all_match because the
663 # full checkout reports "T folder1/0/1" while a sparse
664 # checkout reports "D folder1/0/1". This matches because
665 # the sparse checkouts skip "adding" the other side of
667 test_sparse_match git reset --mixed HEAD~1 &&
668 test_sparse_match test-tool read-cache --table --expand &&
669 test_sparse_match git status --porcelain=v2 &&
671 # At this point, sparse-checkouts behave differently
672 # from the full-checkout.
673 test_sparse_match git checkout --orphan new-branch &&
674 test_sparse_match test-tool read-cache --table --expand &&
675 test_sparse_match git status --porcelain=v2
678 test_expect_success
'add everything with deep new file' '
681 run_on_sparse git sparse-checkout set deep/deeper1/deepest &&
683 run_on_all touch deep/deeper1/x &&
684 test_all_match git add . &&
685 test_all_match git status --porcelain=v2
688 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
689 # directory/file conflicts, even without sparse-checkout. Use this
690 # test only as a documentation of the incorrect behavior, not a
691 # measure of how it _should_ behave.
692 test_expect_success
'checkout behaves oddly with df-conflict-1' '
695 test_sparse_match git sparse-checkout disable &&
697 write_script edit-content <<-\EOF &&
698 echo content >>folder1/larger-content
702 run_on_all ../edit-content &&
703 test_all_match git status --porcelain=v2 &&
705 git -C sparse-checkout sparse-checkout init --cone &&
706 git -C sparse-index sparse-checkout init --cone --sparse-index &&
708 test_all_match git status --porcelain=v2 &&
710 # This checkout command should fail, because we have a staged
711 # change to folder1/larger-content, but the destination changes
713 git -C full-checkout checkout df-conflict-1 \
714 1>full-checkout-out \
715 2>full-checkout-err &&
716 git -C sparse-checkout checkout df-conflict-1 \
717 1>sparse-checkout-out \
718 2>sparse-checkout-err &&
719 git -C sparse-index checkout df-conflict-1 \
721 2>sparse-index-err &&
723 # Instead, the checkout deletes the folder1 file and adds the
724 # folder1/larger-content file, leaving all other paths that were
725 # in folder1/ as deleted (without any warning).
726 cat >expect <<-EOF &&
728 A folder1/larger-content
730 test_cmp expect full-checkout-out &&
731 test_cmp expect sparse-checkout-out &&
733 # The sparse-index reports no output
734 test_must_be_empty sparse-index-out &&
736 # stderr: Switched to branch df-conflict-1
737 test_cmp full-checkout-err sparse-checkout-err &&
738 test_cmp full-checkout-err sparse-checkout-err
741 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
742 # directory/file conflicts, even without sparse-checkout. Use this
743 # test only as a documentation of the incorrect behavior, not a
744 # measure of how it _should_ behave.
745 test_expect_success
'checkout behaves oddly with df-conflict-2' '
748 test_sparse_match git sparse-checkout disable &&
750 write_script edit-content <<-\EOF &&
751 echo content >>folder2/larger-content
755 run_on_all ../edit-content &&
756 test_all_match git status --porcelain=v2 &&
758 git -C sparse-checkout sparse-checkout init --cone &&
759 git -C sparse-index sparse-checkout init --cone --sparse-index &&
761 test_all_match git status --porcelain=v2 &&
763 # This checkout command should fail, because we have a staged
764 # change to folder1/larger-content, but the destination changes
766 git -C full-checkout checkout df-conflict-2 \
767 1>full-checkout-out \
768 2>full-checkout-err &&
769 git -C sparse-checkout checkout df-conflict-2 \
770 1>sparse-checkout-out \
771 2>sparse-checkout-err &&
772 git -C sparse-index checkout df-conflict-2 \
774 2>sparse-index-err &&
776 # The full checkout deviates from the df-conflict-1 case here!
777 # It drops the change to folder1/larger-content and leaves the
778 # folder1 path as-is on disk. The sparse-index behaves the same.
779 test_must_be_empty full-checkout-out &&
780 test_must_be_empty sparse-index-out &&
782 # In the sparse-checkout case, the checkout deletes the folder1
783 # file and adds the folder1/larger-content file, leaving all other
784 # paths that were in folder1/ as deleted (without any warning).
785 cat >expect <<-EOF &&
787 A folder2/larger-content
789 test_cmp expect sparse-checkout-out &&
791 # Switched to branch df-conflict-1
792 test_cmp full-checkout-err sparse-checkout-err &&
793 test_cmp full-checkout-err sparse-index-err