]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1092-sparse-checkout-compatibility.sh
clone: allow "--bare" with "-o"
[thirdparty/git.git] / t / t1092-sparse-checkout-compatibility.sh
1 #!/bin/sh
2
3 test_description='compare full workdir to sparse workdir'
4
5 GIT_TEST_SPLIT_INDEX=0
6 GIT_TEST_SPARSE_INDEX=
7
8 . ./test-lib.sh
9
10 test_expect_success 'setup' '
11 git init initial-repo &&
12 (
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
55 do
56 git checkout -b update-$dir base &&
57 echo "updated $dir" >$dir/a &&
58 git commit -a -m "update $dir" || return 1
59 done &&
60
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" &&
73
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/..." &&
82
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/..." &&
93
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/..." &&
103
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" &&
109
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" &&
115
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" &&
122
123 for side in left right
124 do
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 &&
132
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" &&
138
139 git checkout -f base &&
140 git reset --hard
141 )
142 '
143
144 init_repos () {
145 rm -rf full-checkout sparse-checkout sparse-index &&
146
147 # create repos in initial state
148 cp -r initial-repo full-checkout &&
149 git -C full-checkout reset --hard &&
150
151 cp -r initial-repo sparse-checkout &&
152 git -C sparse-checkout reset --hard &&
153
154 cp -r initial-repo sparse-index &&
155 git -C sparse-index reset --hard &&
156
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
163 }
164
165 run_on_sparse () {
166 (
167 cd sparse-checkout &&
168 GIT_PROGRESS_DELAY=100000 "$@" >../sparse-checkout-out 2>../sparse-checkout-err
169 ) &&
170 (
171 cd sparse-index &&
172 GIT_PROGRESS_DELAY=100000 "$@" >../sparse-index-out 2>../sparse-index-err
173 )
174 }
175
176 run_on_all () {
177 (
178 cd full-checkout &&
179 GIT_PROGRESS_DELAY=100000 "$@" >../full-checkout-out 2>../full-checkout-err
180 ) &&
181 run_on_sparse "$@"
182 }
183
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
190 }
191
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
196 }
197
198 test_sparse_unstaged () {
199 file=$1 &&
200 for repo in sparse-checkout sparse-index
201 do
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
206 }
207
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 &&
217
218 # Check that the directories outside of the sparse-checkout cone
219 # have sparse directory entries.
220 for dir in $SPARSE_DIRS
221 do
222 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
223 grep "040000 $TREE 0 $dir/" cache \
224 || return 1
225 done &&
226
227 # Check that the directories in the sparse-checkout cone
228 # are not sparse directory entries.
229 for dir in $CONE_DIRS
230 do
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
237 }
238
239 test_expect_success 'sparse-index contents' '
240 init_repos &&
241
242 # Remove deep, add three other directories.
243 test_sparse_checkout_set \
244 "folder1 folder2 x" \
245 "before deep" &&
246
247 # Remove folder1, add deep
248 test_sparse_checkout_set \
249 "deep folder2 x" \
250 "before folder1" &&
251
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" &&
257
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" &&
265
266 # Drop down to only files at root
267 test_sparse_checkout_set \
268 "" \
269 "before deep folder1 folder2 x" &&
270
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
274 '
275
276 test_expect_success 'expanded in-memory index matches full index' '
277 init_repos &&
278 test_sparse_match git ls-files --stage
279 '
280
281 test_expect_success 'root directory cannot be sparse' '
282 init_repos &&
283
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 &&
288
289 # Verify sparse directories still present, root directory is not sparse
290 cat >expect <<-EOF &&
291 before/
292 folder1/
293 folder2/
294 x/
295 EOF
296 git -C sparse-index ls-files --sparse >actual &&
297 test_cmp expect actual
298 '
299
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
314 '
315
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
321 '
322
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
329 '
330
331 test_expect_success 'add, commit, checkout' '
332 init_repos &&
333
334 write_script edit-contents <<-\EOF &&
335 echo text >>$1
336 EOF
337 run_on_all ../edit-contents README.md &&
338
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" &&
342
343 test_all_match git checkout HEAD~1 &&
344 test_all_match git checkout - &&
345
346 run_on_all ../edit-contents README.md &&
347
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" &&
351
352 test_all_match git checkout HEAD~1 &&
353 test_all_match git checkout - &&
354
355 run_on_all ../edit-contents deep/newfile &&
356
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" &&
362
363 test_all_match git checkout HEAD~1 &&
364 test_all_match git checkout -
365 '
366
367 test_expect_success 'deep changes during checkout' '
368 init_repos &&
369
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
373 '
374
375 test_expect_success 'checkout with modified sparse directory' '
376 init_repos &&
377
378 test_all_match git checkout rename-in-to-out -- . &&
379 test_sparse_match git sparse-checkout reapply &&
380 test_all_match git checkout base
381 '
382
383 test_expect_success 'add outside sparse cone' '
384 init_repos &&
385
386 run_on_sparse mkdir folder1 &&
387 run_on_sparse ../edit-contents folder1/a &&
388 run_on_sparse ../edit-contents folder1/newfile &&
389 test_sparse_match test_must_fail git add folder1/a &&
390 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
391 test_sparse_unstaged folder1/a &&
392 test_sparse_match test_must_fail git add folder1/newfile &&
393 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
394 test_sparse_unstaged folder1/newfile
395 '
396
397 test_expect_success 'commit including unstaged changes' '
398 init_repos &&
399
400 write_script edit-file <<-\EOF &&
401 echo $1 >$2
402 EOF
403
404 run_on_all ../edit-file 1 a &&
405 run_on_all ../edit-file 1 deep/a &&
406
407 test_all_match git commit -m "-a" -a &&
408 test_all_match git status --porcelain=v2 &&
409
410 run_on_all ../edit-file 2 a &&
411 run_on_all ../edit-file 2 deep/a &&
412
413 test_all_match git commit -m "--include" --include deep/a &&
414 test_all_match git status --porcelain=v2 &&
415 test_all_match git commit -m "--include" --include a &&
416 test_all_match git status --porcelain=v2 &&
417
418 run_on_all ../edit-file 3 a &&
419 run_on_all ../edit-file 3 deep/a &&
420
421 test_all_match git commit -m "--amend" -a --amend &&
422 test_all_match git status --porcelain=v2
423 '
424
425 test_expect_success 'status/add: outside sparse cone' '
426 init_repos &&
427
428 # folder1 is at HEAD, but outside the sparse cone
429 run_on_sparse mkdir folder1 &&
430 cp initial-repo/folder1/a sparse-checkout/folder1/a &&
431 cp initial-repo/folder1/a sparse-index/folder1/a &&
432
433 test_sparse_match git status &&
434
435 write_script edit-contents <<-\EOF &&
436 echo text >>$1
437 EOF
438 run_on_all ../edit-contents folder1/a &&
439 run_on_all ../edit-contents folder1/new &&
440
441 test_sparse_match git status --porcelain=v2 &&
442
443 # Adding the path outside of the sparse-checkout cone should fail.
444 test_sparse_match test_must_fail git add folder1/a &&
445 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
446 test_sparse_unstaged folder1/a &&
447 test_all_match git add --refresh folder1/a &&
448 test_must_be_empty sparse-checkout-err &&
449 test_sparse_unstaged folder1/a &&
450 test_sparse_match test_must_fail git add folder1/new &&
451 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
452 test_sparse_unstaged folder1/new &&
453 test_sparse_match git add --sparse folder1/a &&
454 test_sparse_match git add --sparse folder1/new &&
455
456 test_all_match git add --sparse . &&
457 test_all_match git status --porcelain=v2 &&
458 test_all_match git commit -m folder1/new &&
459 test_all_match git rev-parse HEAD^{tree} &&
460
461 run_on_all ../edit-contents folder1/newer &&
462 test_all_match git add --sparse folder1/ &&
463 test_all_match git status --porcelain=v2 &&
464 test_all_match git commit -m folder1/newer &&
465 test_all_match git rev-parse HEAD^{tree}
466 '
467
468 test_expect_success 'checkout and reset --hard' '
469 init_repos &&
470
471 test_all_match git checkout update-folder1 &&
472 test_all_match git status --porcelain=v2 &&
473
474 test_all_match git checkout update-deep &&
475 test_all_match git status --porcelain=v2 &&
476
477 test_all_match git checkout -b reset-test &&
478 test_all_match git reset --hard deepest &&
479 test_all_match git reset --hard update-folder1 &&
480 test_all_match git reset --hard update-folder2
481 '
482
483 test_expect_success 'diff --cached' '
484 init_repos &&
485
486 write_script edit-contents <<-\EOF &&
487 echo text >>README.md
488 EOF
489 run_on_all ../edit-contents &&
490
491 test_all_match git diff &&
492 test_all_match git diff --cached &&
493 test_all_match git add README.md &&
494 test_all_match git diff &&
495 test_all_match git diff --cached
496 '
497
498 # NEEDSWORK: sparse-checkout behaves differently from full-checkout when
499 # running this test with 'df-conflict-2' after 'df-conflict-1'.
500 test_expect_success 'diff with renames and conflicts' '
501 init_repos &&
502
503 for branch in rename-out-to-out \
504 rename-out-to-in \
505 rename-in-to-out \
506 df-conflict-1 \
507 fd-conflict
508 do
509 test_all_match git checkout rename-base &&
510 test_all_match git checkout $branch -- . &&
511 test_all_match git status --porcelain=v2 &&
512 test_all_match git diff --cached --no-renames &&
513 test_all_match git diff --cached --find-renames || return 1
514 done
515 '
516
517 test_expect_success 'diff with directory/file conflicts' '
518 init_repos &&
519
520 for branch in rename-out-to-out \
521 rename-out-to-in \
522 rename-in-to-out \
523 df-conflict-1 \
524 df-conflict-2 \
525 fd-conflict
526 do
527 git -C full-checkout reset --hard &&
528 test_sparse_match git reset --hard &&
529 test_all_match git checkout $branch &&
530 test_all_match git checkout rename-base -- . &&
531 test_all_match git status --porcelain=v2 &&
532 test_all_match git diff --cached --no-renames &&
533 test_all_match git diff --cached --find-renames || return 1
534 done
535 '
536
537 test_expect_success 'log with pathspec outside sparse definition' '
538 init_repos &&
539
540 test_all_match git log -- a &&
541 test_all_match git log -- folder1/a &&
542 test_all_match git log -- folder2/a &&
543 test_all_match git log -- deep/a &&
544 test_all_match git log -- deep/deeper1/a &&
545 test_all_match git log -- deep/deeper1/deepest/a &&
546
547 test_all_match git checkout update-folder1 &&
548 test_all_match git log -- folder1/a
549 '
550
551 test_expect_success 'blame with pathspec inside sparse definition' '
552 init_repos &&
553
554 for file in a \
555 deep/a \
556 deep/deeper1/a \
557 deep/deeper1/deepest/a
558 do
559 test_all_match git blame $file
560 done
561 '
562
563 # Without a revision specified, blame will error if passed any file that
564 # is not present in the working directory (even if the file is tracked).
565 # Here we just verify that this is also true with sparse checkouts.
566 test_expect_success 'blame with pathspec outside sparse definition' '
567 init_repos &&
568 test_sparse_match git sparse-checkout set &&
569
570 for file in a \
571 deep/a \
572 deep/deeper1/a \
573 deep/deeper1/deepest/a
574 do
575 test_sparse_match test_must_fail git blame $file &&
576 cat >expect <<-EOF &&
577 fatal: Cannot lstat '"'"'$file'"'"': No such file or directory
578 EOF
579 # We compare sparse-checkout-err and sparse-index-err in
580 # `test_sparse_match`. Given we know they are the same, we
581 # only check the content of sparse-index-err here.
582 test_cmp expect sparse-index-err
583 done
584 '
585
586 test_expect_success 'checkout and reset (mixed)' '
587 init_repos &&
588
589 test_all_match git checkout -b reset-test update-deep &&
590 test_all_match git reset deepest &&
591
592 # Because skip-worktree is preserved, resetting to update-folder1
593 # will show worktree changes for folder1/a in full-checkout, but not
594 # in sparse-checkout or sparse-index.
595 git -C full-checkout reset update-folder1 >full-checkout-out &&
596 test_sparse_match git reset update-folder1 &&
597 grep "M folder1/a" full-checkout-out &&
598 ! grep "M folder1/a" sparse-checkout-out &&
599 run_on_sparse test_path_is_missing folder1
600 '
601
602 test_expect_success 'checkout and reset (merge)' '
603 init_repos &&
604
605 write_script edit-contents <<-\EOF &&
606 echo text >>$1
607 EOF
608
609 test_all_match git checkout -b reset-test update-deep &&
610 run_on_all ../edit-contents a &&
611 test_all_match git reset --merge deepest &&
612 test_all_match git status --porcelain=v2 &&
613
614 test_all_match git reset --hard update-deep &&
615 run_on_all ../edit-contents deep/a &&
616 test_all_match test_must_fail git reset --merge deepest
617 '
618
619 test_expect_success 'checkout and reset (keep)' '
620 init_repos &&
621
622 write_script edit-contents <<-\EOF &&
623 echo text >>$1
624 EOF
625
626 test_all_match git checkout -b reset-test update-deep &&
627 run_on_all ../edit-contents a &&
628 test_all_match git reset --keep deepest &&
629 test_all_match git status --porcelain=v2 &&
630
631 test_all_match git reset --hard update-deep &&
632 run_on_all ../edit-contents deep/a &&
633 test_all_match test_must_fail git reset --keep deepest
634 '
635
636 test_expect_success 'reset with pathspecs inside sparse definition' '
637 init_repos &&
638
639 write_script edit-contents <<-\EOF &&
640 echo text >>$1
641 EOF
642
643 test_all_match git checkout -b reset-test update-deep &&
644 run_on_all ../edit-contents deep/a &&
645
646 test_all_match git reset base -- deep/a &&
647 test_all_match git status --porcelain=v2 &&
648
649 test_all_match git reset base -- nonexistent-file &&
650 test_all_match git status --porcelain=v2 &&
651
652 test_all_match git reset deepest -- deep &&
653 test_all_match git status --porcelain=v2
654 '
655
656 # Although the working tree differs between full and sparse checkouts after
657 # reset, the state of the index is the same.
658 test_expect_success 'reset with pathspecs outside sparse definition' '
659 init_repos &&
660 test_all_match git checkout -b reset-test base &&
661
662 test_sparse_match git reset update-folder1 -- folder1 &&
663 git -C full-checkout reset update-folder1 -- folder1 &&
664 test_all_match git ls-files -s -- folder1 &&
665
666 test_sparse_match git reset update-folder2 -- folder2/a &&
667 git -C full-checkout reset update-folder2 -- folder2/a &&
668 test_all_match git ls-files -s -- folder2/a
669 '
670
671 test_expect_success 'reset with wildcard pathspec' '
672 init_repos &&
673
674 test_all_match git reset update-deep -- deep\* &&
675 test_all_match git ls-files -s -- deep &&
676
677 test_all_match git reset deepest -- deep\*\*\* &&
678 test_all_match git ls-files -s -- deep &&
679
680 # The following `git reset`s result in updating the index on files with
681 # `skip-worktree` enabled. To avoid failing due to discrepencies in reported
682 # "modified" files, `test_sparse_match` reset is performed separately from
683 # "full-checkout" reset, then the index contents of all repos are verified.
684
685 test_sparse_match git reset update-folder1 -- \*/a &&
686 git -C full-checkout reset update-folder1 -- \*/a &&
687 test_all_match git ls-files -s -- deep/a folder1/a &&
688
689 test_sparse_match git reset update-folder2 -- folder\* &&
690 git -C full-checkout reset update-folder2 -- folder\* &&
691 test_all_match git ls-files -s -- folder10 folder1 folder2 &&
692
693 test_sparse_match git reset base -- folder1/\* &&
694 git -C full-checkout reset base -- folder1/\* &&
695 test_all_match git ls-files -s -- folder1
696 '
697
698 test_expect_success 'reset hard with removed sparse dir' '
699 init_repos &&
700
701 run_on_all git rm -r --sparse folder1 &&
702 test_all_match git status --porcelain=v2 &&
703
704 test_all_match git reset --hard &&
705 test_all_match git status --porcelain=v2 &&
706
707 cat >expect <<-\EOF &&
708 folder1/
709 EOF
710
711 git -C sparse-index ls-files --sparse folder1 >out &&
712 test_cmp expect out
713 '
714
715 test_expect_success 'update-index modify outside sparse definition' '
716 init_repos &&
717
718 write_script edit-contents <<-\EOF &&
719 echo text >>$1
720 EOF
721
722 # Create & modify folder1/a
723 # Note that this setup is a manual way of reaching the erroneous
724 # condition in which a `skip-worktree` enabled, outside-of-cone file
725 # exists on disk. It is used here to ensure `update-index` is stable
726 # and behaves predictably if such a condition occurs.
727 run_on_sparse mkdir -p folder1 &&
728 run_on_sparse cp ../initial-repo/folder1/a folder1/a &&
729 run_on_all ../edit-contents folder1/a &&
730
731 # If file has skip-worktree enabled, but the file is present, it is
732 # treated the same as if skip-worktree is disabled
733 test_all_match git status --porcelain=v2 &&
734 test_all_match git update-index folder1/a &&
735 test_all_match git status --porcelain=v2 &&
736
737 # When skip-worktree is disabled (even on files outside sparse cone), file
738 # is updated in the index
739 test_sparse_match git update-index --no-skip-worktree folder1/a &&
740 test_all_match git status --porcelain=v2 &&
741 test_all_match git update-index folder1/a &&
742 test_all_match git status --porcelain=v2
743 '
744
745 test_expect_success 'update-index --add outside sparse definition' '
746 init_repos &&
747
748 write_script edit-contents <<-\EOF &&
749 echo text >>$1
750 EOF
751
752 # Create folder1, add new file
753 run_on_sparse mkdir -p folder1 &&
754 run_on_all ../edit-contents folder1/b &&
755
756 # The *untracked* out-of-cone file is added to the index because it does
757 # not have a `skip-worktree` bit to signal that it should be ignored
758 # (unlike in `git add`, which will fail due to the file being outside
759 # the sparse checkout definition).
760 test_all_match git update-index --add folder1/b &&
761 test_all_match git status --porcelain=v2
762 '
763
764 # NEEDSWORK: `--remove`, unlike the rest of `update-index`, does not ignore
765 # `skip-worktree` entries by default and will remove them from the index.
766 # The `--ignore-skip-worktree-entries` flag must be used in conjunction with
767 # `--remove` to ignore the `skip-worktree` entries and prevent their removal
768 # from the index.
769 test_expect_success 'update-index --remove outside sparse definition' '
770 init_repos &&
771
772 # When --ignore-skip-worktree-entries is _not_ specified:
773 # out-of-cone, not-on-disk files are removed from the index
774 test_sparse_match git update-index --remove folder1/a &&
775 cat >expect <<-EOF &&
776 D folder1/a
777 EOF
778 test_sparse_match git diff --cached --name-status &&
779 test_cmp expect sparse-checkout-out &&
780
781 # Reset the state
782 test_all_match git reset --hard &&
783
784 # When --ignore-skip-worktree-entries is specified, out-of-cone
785 # (skip-worktree) files are ignored
786 test_sparse_match git update-index --remove --ignore-skip-worktree-entries folder1/a &&
787 test_sparse_match git diff --cached --name-status &&
788 test_must_be_empty sparse-checkout-out &&
789
790 # Reset the state
791 test_all_match git reset --hard &&
792
793 # --force-remove supercedes --ignore-skip-worktree-entries, removing
794 # a skip-worktree file from the index (and disk) when both are specified
795 # with --remove
796 test_sparse_match git update-index --force-remove --ignore-skip-worktree-entries folder1/a &&
797 cat >expect <<-EOF &&
798 D folder1/a
799 EOF
800 test_sparse_match git diff --cached --name-status &&
801 test_cmp expect sparse-checkout-out
802 '
803
804 test_expect_success 'update-index with directories' '
805 init_repos &&
806
807 # update-index will exit silently when provided with a directory name
808 # containing a trailing slash
809 test_all_match git update-index deep/ folder1/ &&
810 grep "Ignoring path deep/" sparse-checkout-err &&
811 grep "Ignoring path folder1/" sparse-checkout-err &&
812
813 # When update-index is given a directory name WITHOUT a trailing slash, it will
814 # behave in different ways depending on the status of the directory on disk:
815 # * if it exists, the command exits with an error ("add individual files instead")
816 # * if it does NOT exist (e.g., in a sparse-checkout), it is assumed to be a
817 # file and either triggers an error ("does not exist and --remove not passed")
818 # or is ignored completely (when using --remove)
819 test_all_match test_must_fail git update-index deep &&
820 run_on_all test_must_fail git update-index folder1 &&
821 test_must_fail git -C full-checkout update-index --remove folder1 &&
822 test_sparse_match git update-index --remove folder1 &&
823 test_all_match git status --porcelain=v2
824 '
825
826 test_expect_success 'update-index --again file outside sparse definition' '
827 init_repos &&
828
829 test_all_match git checkout -b test-reupdate &&
830
831 # Update HEAD without modifying the index to introduce a difference in
832 # folder1/a
833 test_sparse_match git reset --soft update-folder1 &&
834
835 # Because folder1/a differs in the index vs HEAD,
836 # `git update-index --no-skip-worktree --again` will effectively perform
837 # `git update-index --no-skip-worktree folder1/a` and remove the skip-worktree
838 # flag from folder1/a
839 test_sparse_match git update-index --no-skip-worktree --again &&
840 test_sparse_match git status --porcelain=v2 &&
841
842 cat >expect <<-EOF &&
843 D folder1/a
844 EOF
845 test_sparse_match git diff --name-status &&
846 test_cmp expect sparse-checkout-out
847 '
848
849 test_expect_success 'update-index --cacheinfo' '
850 init_repos &&
851
852 deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
853 folder2_oid=$(git -C full-checkout rev-parse update-folder2:folder2) &&
854 folder1_a_oid=$(git -C full-checkout rev-parse update-folder1:folder1/a) &&
855
856 test_all_match git update-index --cacheinfo 100644 $deep_a_oid deep/a &&
857 test_all_match git status --porcelain=v2 &&
858
859 # Cannot add sparse directory, even in sparse index case
860 test_all_match test_must_fail git update-index --add --cacheinfo 040000 $folder2_oid folder2/ &&
861
862 # Sparse match only: the new outside-of-cone entry is added *without* skip-worktree,
863 # so `git status` reports it as "deleted" in the worktree
864 test_sparse_match git update-index --add --cacheinfo 100644 $folder1_a_oid folder1/a &&
865 test_sparse_match git status --porcelain=v2 &&
866 cat >expect <<-EOF &&
867 MD folder1/a
868 EOF
869 test_sparse_match git status --short -- folder1/a &&
870 test_cmp expect sparse-checkout-out &&
871
872 # To return folder1/a to "normal" for a sparse checkout (ignored &
873 # outside-of-cone), add the skip-worktree flag.
874 test_sparse_match git update-index --skip-worktree folder1/a &&
875 cat >expect <<-EOF &&
876 S folder1/a
877 EOF
878 test_sparse_match git ls-files -t -- folder1/a &&
879 test_cmp expect sparse-checkout-out
880 '
881
882 for MERGE_TREES in "base HEAD update-folder2" \
883 "update-folder1 update-folder2" \
884 "update-folder2"
885 do
886 test_expect_success "'read-tree -mu $MERGE_TREES' with files outside sparse definition" '
887 init_repos &&
888
889 # Although the index matches, without --no-sparse-checkout, outside-of-
890 # definition files will not exist on disk for sparse checkouts
891 test_all_match git read-tree -mu $MERGE_TREES &&
892 test_all_match git status --porcelain=v2 &&
893 test_path_is_missing sparse-checkout/folder2 &&
894 test_path_is_missing sparse-index/folder2 &&
895
896 test_all_match git read-tree --reset -u HEAD &&
897 test_all_match git status --porcelain=v2 &&
898
899 test_all_match git read-tree -mu --no-sparse-checkout $MERGE_TREES &&
900 test_all_match git status --porcelain=v2 &&
901 test_cmp sparse-checkout/folder2/a sparse-index/folder2/a &&
902 test_cmp sparse-checkout/folder2/a full-checkout/folder2/a
903
904 '
905 done
906
907 test_expect_success 'read-tree --merge with edit/edit conflicts in sparse directories' '
908 init_repos &&
909
910 # Merge of multiple changes to same directory (but not same files) should
911 # succeed
912 test_all_match git read-tree -mu base rename-base update-folder1 &&
913 test_all_match git status --porcelain=v2 &&
914
915 test_all_match git reset --hard &&
916
917 test_all_match git read-tree -mu rename-base update-folder2 &&
918 test_all_match git status --porcelain=v2 &&
919
920 test_all_match git reset --hard &&
921
922 test_all_match test_must_fail git read-tree -mu base update-folder1 rename-out-to-in &&
923 test_all_match test_must_fail git read-tree -mu rename-out-to-in update-folder1
924 '
925
926 test_expect_success 'read-tree --prefix' '
927 init_repos &&
928
929 # If files differing between the index and target <commit-ish> exist
930 # inside the prefix, `read-tree --prefix` should fail
931 test_all_match test_must_fail git read-tree --prefix=deep/ deepest &&
932 test_all_match test_must_fail git read-tree --prefix=folder1/ update-folder1 &&
933
934 # If no differing index entries exist matching the prefix,
935 # `read-tree --prefix` updates the index successfully
936 test_all_match git rm -rf deep/deeper1/deepest/ &&
937 test_all_match git read-tree --prefix=deep/deeper1/deepest -u deepest &&
938 test_all_match git status --porcelain=v2 &&
939
940 test_all_match git rm -rf --sparse folder1/ &&
941 test_all_match git read-tree --prefix=folder1/ -u update-folder1 &&
942 test_all_match git status --porcelain=v2 &&
943
944 test_all_match git rm -rf --sparse folder2/0 &&
945 test_all_match git read-tree --prefix=folder2/0/ -u rename-out-to-out &&
946 test_all_match git status --porcelain=v2
947 '
948
949 test_expect_success 'read-tree --merge with directory-file conflicts' '
950 init_repos &&
951
952 test_all_match git checkout -b test-branch rename-base &&
953
954 # Although the index matches, without --no-sparse-checkout, outside-of-
955 # definition files will not exist on disk for sparse checkouts
956 test_sparse_match git read-tree -mu rename-out-to-out &&
957 test_sparse_match git status --porcelain=v2 &&
958 test_path_is_missing sparse-checkout/folder2 &&
959 test_path_is_missing sparse-index/folder2 &&
960
961 test_sparse_match git read-tree --reset -u HEAD &&
962 test_sparse_match git status --porcelain=v2 &&
963
964 test_sparse_match git read-tree -mu --no-sparse-checkout rename-out-to-out &&
965 test_sparse_match git status --porcelain=v2 &&
966 test_cmp sparse-checkout/folder2/0/1 sparse-index/folder2/0/1
967 '
968
969 test_expect_success 'merge, cherry-pick, and rebase' '
970 init_repos &&
971
972 for OPERATION in "merge -m merge" cherry-pick "rebase --apply" "rebase --merge"
973 do
974 test_all_match git checkout -B temp update-deep &&
975 test_all_match git $OPERATION update-folder1 &&
976 test_all_match git rev-parse HEAD^{tree} &&
977 test_all_match git $OPERATION update-folder2 &&
978 test_all_match git rev-parse HEAD^{tree} || return 1
979 done
980 '
981
982 test_expect_success 'merge with conflict outside cone' '
983 init_repos &&
984
985 test_all_match git checkout -b merge-tip merge-left &&
986 test_all_match git status --porcelain=v2 &&
987 test_all_match test_must_fail git merge -m merge merge-right &&
988 test_all_match git status --porcelain=v2 &&
989
990 # Resolve the conflict in different ways:
991 # 1. Revert to the base
992 test_all_match git checkout base -- deep/deeper2/a &&
993 test_all_match git status --porcelain=v2 &&
994
995 # 2. Add the file with conflict markers
996 test_sparse_match test_must_fail git add folder1/a &&
997 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
998 test_sparse_unstaged folder1/a &&
999 test_all_match git add --sparse folder1/a &&
1000 test_all_match git status --porcelain=v2 &&
1001
1002 # 3. Rename the file to another sparse filename and
1003 # accept conflict markers as resolved content.
1004 run_on_all mv folder2/a folder2/z &&
1005 test_sparse_match test_must_fail git add folder2 &&
1006 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1007 test_sparse_unstaged folder2/z &&
1008 test_all_match git add --sparse folder2 &&
1009 test_all_match git status --porcelain=v2 &&
1010
1011 test_all_match git merge --continue &&
1012 test_all_match git status --porcelain=v2 &&
1013 test_all_match git rev-parse HEAD^{tree}
1014 '
1015
1016 test_expect_success 'cherry-pick/rebase with conflict outside cone' '
1017 init_repos &&
1018
1019 for OPERATION in cherry-pick rebase
1020 do
1021 test_all_match git checkout -B tip &&
1022 test_all_match git reset --hard merge-left &&
1023 test_all_match git status --porcelain=v2 &&
1024 test_all_match test_must_fail git $OPERATION merge-right &&
1025 test_all_match git status --porcelain=v2 &&
1026
1027 # Resolve the conflict in different ways:
1028 # 1. Revert to the base
1029 test_all_match git checkout base -- deep/deeper2/a &&
1030 test_all_match git status --porcelain=v2 &&
1031
1032 # 2. Add the file with conflict markers
1033 # NEEDSWORK: Even though the merge conflict removed the
1034 # SKIP_WORKTREE bit from the index entry for folder1/a, we should
1035 # warn that this is a problematic add.
1036 test_sparse_match test_must_fail git add folder1/a &&
1037 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1038 test_sparse_unstaged folder1/a &&
1039 test_all_match git add --sparse folder1/a &&
1040 test_all_match git status --porcelain=v2 &&
1041
1042 # 3. Rename the file to another sparse filename and
1043 # accept conflict markers as resolved content.
1044 # NEEDSWORK: This mode now fails, because folder2/z is
1045 # outside of the sparse-checkout cone and does not match an
1046 # existing index entry with the SKIP_WORKTREE bit cleared.
1047 run_on_all mv folder2/a folder2/z &&
1048 test_sparse_match test_must_fail git add folder2 &&
1049 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
1050 test_sparse_unstaged folder2/z &&
1051 test_all_match git add --sparse folder2 &&
1052 test_all_match git status --porcelain=v2 &&
1053
1054 test_all_match git $OPERATION --continue &&
1055 test_all_match git status --porcelain=v2 &&
1056 test_all_match git rev-parse HEAD^{tree} || return 1
1057 done
1058 '
1059
1060 test_expect_success 'merge with outside renames' '
1061 init_repos &&
1062
1063 for type in out-to-out out-to-in in-to-out
1064 do
1065 test_all_match git reset --hard &&
1066 test_all_match git checkout -f -b merge-$type update-deep &&
1067 test_all_match git merge -m "$type" rename-$type &&
1068 test_all_match git rev-parse HEAD^{tree} || return 1
1069 done
1070 '
1071
1072 # Sparse-index fails to convert the index in the
1073 # final 'git cherry-pick' command.
1074 test_expect_success 'cherry-pick with conflicts' '
1075 init_repos &&
1076
1077 write_script edit-conflict <<-\EOF &&
1078 echo $1 >conflict
1079 EOF
1080
1081 test_all_match git checkout -b to-cherry-pick &&
1082 run_on_all ../edit-conflict ABC &&
1083 test_all_match git add conflict &&
1084 test_all_match git commit -m "conflict to pick" &&
1085
1086 test_all_match git checkout -B base HEAD~1 &&
1087 run_on_all ../edit-conflict DEF &&
1088 test_all_match git add conflict &&
1089 test_all_match git commit -m "conflict in base" &&
1090
1091 test_all_match test_must_fail git cherry-pick to-cherry-pick
1092 '
1093
1094 test_expect_success 'stash' '
1095 init_repos &&
1096
1097 write_script edit-contents <<-\EOF &&
1098 echo text >>$1
1099 EOF
1100
1101 # Stash a sparse directory (folder1)
1102 test_all_match git checkout -b test-branch rename-base &&
1103 test_all_match git reset --soft rename-out-to-out &&
1104 test_all_match git stash &&
1105 test_all_match git status --porcelain=v2 &&
1106
1107 # Apply the sparse directory stash without reinstating the index
1108 test_all_match git stash apply -q &&
1109 test_all_match git status --porcelain=v2 &&
1110
1111 # Reset to state where stash can be applied
1112 test_sparse_match git sparse-checkout reapply &&
1113 test_all_match git reset --hard rename-out-to-out &&
1114
1115 # Apply the sparse directory stash *with* reinstating the index
1116 test_all_match git stash apply --index -q &&
1117 test_all_match git status --porcelain=v2 &&
1118
1119 # Reset to state where we will get a conflict applying the stash
1120 test_sparse_match git sparse-checkout reapply &&
1121 test_all_match git reset --hard update-folder1 &&
1122
1123 # Apply the sparse directory stash with conflicts
1124 test_all_match test_must_fail git stash apply --index -q &&
1125 test_all_match test_must_fail git stash apply -q &&
1126 test_all_match git status --porcelain=v2 &&
1127
1128 # Reset to base branch
1129 test_sparse_match git sparse-checkout reapply &&
1130 test_all_match git reset --hard base &&
1131
1132 # Stash & unstash an untracked file outside of the sparse checkout
1133 # definition.
1134 run_on_sparse mkdir -p folder1 &&
1135 run_on_all ../edit-contents folder1/new &&
1136 test_all_match git stash -u &&
1137 test_all_match git status --porcelain=v2 &&
1138
1139 test_all_match git stash pop -q &&
1140 test_all_match git status --porcelain=v2
1141 '
1142
1143 test_expect_success 'checkout-index inside sparse definition' '
1144 init_repos &&
1145
1146 run_on_all rm -f deep/a &&
1147 test_all_match git checkout-index -- deep/a &&
1148 test_all_match git status --porcelain=v2 &&
1149
1150 echo test >>new-a &&
1151 run_on_all cp ../new-a a &&
1152 test_all_match test_must_fail git checkout-index -- a &&
1153 test_all_match git checkout-index -f -- a &&
1154 test_all_match git status --porcelain=v2
1155 '
1156
1157 test_expect_success 'checkout-index outside sparse definition' '
1158 init_repos &&
1159
1160 # Without --ignore-skip-worktree-bits, outside-of-cone files will trigger
1161 # an error
1162 test_sparse_match test_must_fail git checkout-index -- folder1/a &&
1163 test_i18ngrep "folder1/a has skip-worktree enabled" sparse-checkout-err &&
1164 test_path_is_missing folder1/a &&
1165
1166 # With --ignore-skip-worktree-bits, outside-of-cone files are checked out
1167 test_sparse_match git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
1168 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
1169 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a &&
1170
1171 run_on_sparse rm -rf folder1 &&
1172 echo test >new-a &&
1173 run_on_sparse mkdir -p folder1 &&
1174 run_on_all cp ../new-a folder1/a &&
1175
1176 test_all_match test_must_fail git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
1177 test_all_match git checkout-index -f --ignore-skip-worktree-bits -- folder1/a &&
1178 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
1179 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a
1180 '
1181
1182 test_expect_success 'checkout-index with folders' '
1183 init_repos &&
1184
1185 # Inside checkout definition
1186 test_all_match test_must_fail git checkout-index -f -- deep/ &&
1187
1188 # Outside checkout definition
1189 # Note: although all tests fail (as expected), the messaging differs. For
1190 # non-sparse index checkouts, the error is that the "file" does not appear
1191 # in the index; for sparse checkouts, the error is explicitly that the
1192 # entry is a sparse directory.
1193 run_on_all test_must_fail git checkout-index -f -- folder1/ &&
1194 test_cmp full-checkout-err sparse-checkout-err &&
1195 ! test_cmp full-checkout-err sparse-index-err &&
1196 grep "is a sparse directory" sparse-index-err
1197 '
1198
1199 test_expect_success 'checkout-index --all' '
1200 init_repos &&
1201
1202 test_all_match git checkout-index --all &&
1203 test_sparse_match test_path_is_missing folder1 &&
1204
1205 # --ignore-skip-worktree-bits will cause `skip-worktree` files to be
1206 # checked out, causing the outside-of-cone `folder1` to exist on-disk
1207 test_all_match git checkout-index --ignore-skip-worktree-bits --all &&
1208 test_all_match test_path_exists folder1
1209 '
1210
1211 test_expect_success 'clean' '
1212 init_repos &&
1213
1214 echo bogus >>.gitignore &&
1215 run_on_all cp ../.gitignore . &&
1216 test_all_match git add .gitignore &&
1217 test_all_match git commit -m "ignore bogus files" &&
1218
1219 run_on_sparse mkdir folder1 &&
1220 run_on_all mkdir -p deep/untracked-deep &&
1221 run_on_all touch folder1/bogus &&
1222 run_on_all touch folder1/untracked &&
1223 run_on_all touch deep/untracked-deep/bogus &&
1224 run_on_all touch deep/untracked-deep/untracked &&
1225
1226 test_all_match git status --porcelain=v2 &&
1227 test_all_match git clean -f &&
1228 test_all_match git status --porcelain=v2 &&
1229 test_sparse_match ls &&
1230 test_sparse_match ls folder1 &&
1231 run_on_all test_path_exists folder1/bogus &&
1232 run_on_all test_path_is_missing folder1/untracked &&
1233 run_on_all test_path_exists deep/untracked-deep/bogus &&
1234 run_on_all test_path_exists deep/untracked-deep/untracked &&
1235
1236 test_all_match git clean -fd &&
1237 test_all_match git status --porcelain=v2 &&
1238 test_sparse_match ls &&
1239 test_sparse_match ls folder1 &&
1240 run_on_all test_path_exists folder1/bogus &&
1241 run_on_all test_path_exists deep/untracked-deep/bogus &&
1242 run_on_all test_path_is_missing deep/untracked-deep/untracked &&
1243
1244 test_all_match git clean -xf &&
1245 test_all_match git status --porcelain=v2 &&
1246 test_sparse_match ls &&
1247 test_sparse_match ls folder1 &&
1248 run_on_all test_path_is_missing folder1/bogus &&
1249 run_on_all test_path_exists deep/untracked-deep/bogus &&
1250
1251 test_all_match git clean -xdf &&
1252 test_all_match git status --porcelain=v2 &&
1253 test_sparse_match ls &&
1254 test_sparse_match ls folder1 &&
1255 run_on_all test_path_is_missing deep/untracked-deep/bogus &&
1256
1257 test_sparse_match test_path_is_dir folder1
1258 '
1259
1260 for builtin in show rev-parse
1261 do
1262 test_expect_success "$builtin (cached blobs/trees)" "
1263 init_repos &&
1264
1265 test_all_match git $builtin :a &&
1266 test_all_match git $builtin :deep/a &&
1267 test_sparse_match git $builtin :folder1/a &&
1268
1269 # The error message differs depending on whether
1270 # the directory exists in the worktree.
1271 test_all_match test_must_fail git $builtin :deep/ &&
1272 test_must_fail git -C full-checkout $builtin :folder1/ &&
1273 test_sparse_match test_must_fail git $builtin :folder1/ &&
1274
1275 # Change the sparse cone for an extra case:
1276 run_on_sparse git sparse-checkout set deep/deeper1 &&
1277
1278 # deep/deeper2 is a sparse directory in the sparse index.
1279 test_sparse_match test_must_fail git $builtin :deep/deeper2/ &&
1280
1281 # deep/deeper2/deepest is not in the sparse index, but
1282 # will trigger an index expansion.
1283 test_sparse_match test_must_fail git $builtin :deep/deeper2/deepest/
1284 "
1285 done
1286
1287 test_expect_success 'submodule handling' '
1288 init_repos &&
1289
1290 test_sparse_match git sparse-checkout add modules &&
1291 test_all_match mkdir modules &&
1292 test_all_match touch modules/a &&
1293 test_all_match git add modules &&
1294 test_all_match git commit -m "add modules directory" &&
1295
1296 run_on_all git submodule add "$(pwd)/initial-repo" modules/sub &&
1297 test_all_match git commit -m "add submodule" &&
1298
1299 # having a submodule prevents "modules" from collapse
1300 test_sparse_match git sparse-checkout set deep/deeper1 &&
1301 git -C sparse-index ls-files --sparse --stage >cache &&
1302 grep "100644 .* modules/a" cache &&
1303 grep "160000 $(git -C initial-repo rev-parse HEAD) 0 modules/sub" cache
1304 '
1305
1306 # When working with a sparse index, some commands will need to expand the
1307 # index to operate properly. If those commands also write the index back
1308 # to disk, they need to convert the index to sparse before writing.
1309 # This test verifies that both of these events are logged in trace2 logs.
1310 test_expect_success 'sparse-index is expanded and converted back' '
1311 init_repos &&
1312
1313 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1314 git -C sparse-index reset -- folder1/a &&
1315 test_region index convert_to_sparse trace2.txt &&
1316 test_region index ensure_full_index trace2.txt &&
1317
1318 # ls-files expands on read, but does not write.
1319 rm trace2.txt &&
1320 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1321 git -C sparse-index ls-files &&
1322 test_region index ensure_full_index trace2.txt
1323 '
1324
1325 test_expect_success 'index.sparse disabled inline uses full index' '
1326 init_repos &&
1327
1328 # When index.sparse is disabled inline with `git status`, the
1329 # index is expanded at the beginning of the execution then never
1330 # converted back to sparse. It is then written to disk as a full index.
1331 rm -f trace2.txt &&
1332 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1333 git -C sparse-index -c index.sparse=false status &&
1334 ! test_region index convert_to_sparse trace2.txt &&
1335 test_region index ensure_full_index trace2.txt &&
1336
1337 # Since index.sparse is set to true at a repo level, the index
1338 # is converted from full to sparse when read, then never expanded
1339 # over the course of `git status`. It is written to disk as a sparse
1340 # index.
1341 rm -f trace2.txt &&
1342 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1343 git -C sparse-index status &&
1344 test_region index convert_to_sparse trace2.txt &&
1345 ! test_region index ensure_full_index trace2.txt &&
1346
1347 # Now that the index has been written to disk as sparse, it is not
1348 # converted to sparse (or expanded to full) when read by `git status`.
1349 rm -f trace2.txt &&
1350 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
1351 git -C sparse-index status &&
1352 ! test_region index convert_to_sparse trace2.txt &&
1353 ! test_region index ensure_full_index trace2.txt
1354 '
1355
1356 ensure_not_expanded () {
1357 rm -f trace2.txt &&
1358 if test -z "$WITHOUT_UNTRACKED_TXT"
1359 then
1360 echo >>sparse-index/untracked.txt
1361 fi &&
1362
1363 if test "$1" = "!"
1364 then
1365 shift &&
1366 test_must_fail env \
1367 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1368 git -C sparse-index "$@" || return 1
1369 else
1370 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1371 git -C sparse-index "$@" || return 1
1372 fi &&
1373 test_region ! index ensure_full_index trace2.txt
1374 }
1375
1376 test_expect_success 'sparse-index is not expanded' '
1377 init_repos &&
1378
1379 ensure_not_expanded status &&
1380 ensure_not_expanded ls-files --sparse &&
1381 ensure_not_expanded commit --allow-empty -m empty &&
1382 echo >>sparse-index/a &&
1383 ensure_not_expanded commit -a -m a &&
1384 echo >>sparse-index/a &&
1385 ensure_not_expanded commit --include a -m a &&
1386 echo >>sparse-index/deep/deeper1/a &&
1387 ensure_not_expanded commit --include deep/deeper1/a -m deeper &&
1388 ensure_not_expanded checkout rename-out-to-out &&
1389 ensure_not_expanded checkout - &&
1390 ensure_not_expanded switch rename-out-to-out &&
1391 ensure_not_expanded switch - &&
1392 ensure_not_expanded reset --hard &&
1393 ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
1394 ensure_not_expanded reset --hard &&
1395 ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
1396
1397 echo >>sparse-index/README.md &&
1398 ensure_not_expanded add -A &&
1399 echo >>sparse-index/extra.txt &&
1400 ensure_not_expanded add extra.txt &&
1401 echo >>sparse-index/untracked.txt &&
1402 ensure_not_expanded add . &&
1403
1404 ensure_not_expanded checkout-index -f a &&
1405 ensure_not_expanded checkout-index -f --all &&
1406 for ref in update-deep update-folder1 update-folder2 update-deep
1407 do
1408 echo >>sparse-index/README.md &&
1409 ensure_not_expanded reset --hard $ref || return 1
1410 done &&
1411
1412 ensure_not_expanded reset --mixed base &&
1413 ensure_not_expanded reset --hard update-deep &&
1414 ensure_not_expanded reset --keep base &&
1415 ensure_not_expanded reset --merge update-deep &&
1416 ensure_not_expanded reset --hard &&
1417
1418 ensure_not_expanded reset base -- deep/a &&
1419 ensure_not_expanded reset base -- nonexistent-file &&
1420 ensure_not_expanded reset deepest -- deep &&
1421
1422 # Although folder1 is outside the sparse definition, it exists as a
1423 # directory entry in the index, so the pathspec will not force the
1424 # index to be expanded.
1425 ensure_not_expanded reset deepest -- folder1 &&
1426 ensure_not_expanded reset deepest -- folder1/ &&
1427
1428 # Wildcard identifies only in-cone files, no index expansion
1429 ensure_not_expanded reset deepest -- deep/\* &&
1430
1431 # Wildcard identifies only full sparse directories, no index expansion
1432 ensure_not_expanded reset deepest -- folder\* &&
1433
1434 ensure_not_expanded clean -fd &&
1435
1436 ensure_not_expanded checkout -f update-deep &&
1437 test_config -C sparse-index pull.twohead ort &&
1438 (
1439 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1440 for OPERATION in "merge -m merge" cherry-pick rebase
1441 do
1442 ensure_not_expanded merge -m merge update-folder1 &&
1443 ensure_not_expanded merge -m merge update-folder2 || return 1
1444 done
1445 )
1446 '
1447
1448 test_expect_success 'sparse-index is not expanded: merge conflict in cone' '
1449 init_repos &&
1450
1451 for side in right left
1452 do
1453 git -C sparse-index checkout -b expand-$side base &&
1454 echo $side >sparse-index/deep/a &&
1455 git -C sparse-index commit -a -m "$side" || return 1
1456 done &&
1457
1458 (
1459 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1460 git -C sparse-index config pull.twohead ort &&
1461 ensure_not_expanded ! merge -m merged expand-right
1462 )
1463 '
1464
1465 test_expect_success 'sparse-index is not expanded: stash' '
1466 init_repos &&
1467
1468 echo >>sparse-index/a &&
1469 ensure_not_expanded stash &&
1470 ensure_not_expanded stash list &&
1471 ensure_not_expanded stash show stash@{0} &&
1472 ensure_not_expanded stash apply stash@{0} &&
1473 ensure_not_expanded stash drop stash@{0} &&
1474
1475 echo >>sparse-index/deep/new &&
1476 ensure_not_expanded stash -u &&
1477 (
1478 WITHOUT_UNTRACKED_TXT=1 &&
1479 ensure_not_expanded stash pop
1480 ) &&
1481
1482 ensure_not_expanded stash create &&
1483 oid=$(git -C sparse-index stash create) &&
1484 ensure_not_expanded stash store -m "test" $oid &&
1485 ensure_not_expanded reset --hard &&
1486 ensure_not_expanded stash pop
1487 '
1488
1489 test_expect_success 'sparse index is not expanded: diff' '
1490 init_repos &&
1491
1492 write_script edit-contents <<-\EOF &&
1493 echo text >>$1
1494 EOF
1495
1496 # Add file within cone
1497 test_sparse_match git sparse-checkout set deep &&
1498 run_on_all ../edit-contents deep/testfile &&
1499 test_all_match git add deep/testfile &&
1500 run_on_all ../edit-contents deep/testfile &&
1501
1502 test_all_match git diff &&
1503 test_all_match git diff --cached &&
1504 ensure_not_expanded diff &&
1505 ensure_not_expanded diff --cached &&
1506
1507 # Add file outside cone
1508 test_all_match git reset --hard &&
1509 run_on_all mkdir newdirectory &&
1510 run_on_all ../edit-contents newdirectory/testfile &&
1511 test_sparse_match git sparse-checkout set newdirectory &&
1512 test_all_match git add newdirectory/testfile &&
1513 run_on_all ../edit-contents newdirectory/testfile &&
1514 test_sparse_match git sparse-checkout set &&
1515
1516 test_all_match git diff &&
1517 test_all_match git diff --cached &&
1518 ensure_not_expanded diff &&
1519 ensure_not_expanded diff --cached &&
1520
1521 # Merge conflict outside cone
1522 # The sparse checkout will report a warning that is not in the
1523 # full checkout, so we use `run_on_all` instead of
1524 # `test_all_match`
1525 run_on_all git reset --hard &&
1526 test_all_match git checkout merge-left &&
1527 test_all_match test_must_fail git merge merge-right &&
1528
1529 test_all_match git diff &&
1530 test_all_match git diff --cached &&
1531 ensure_not_expanded diff &&
1532 ensure_not_expanded diff --cached
1533 '
1534
1535 test_expect_success 'sparse index is not expanded: show and rev-parse' '
1536 init_repos &&
1537
1538 ensure_not_expanded show :a &&
1539 ensure_not_expanded show :deep/a &&
1540 ensure_not_expanded rev-parse :a &&
1541 ensure_not_expanded rev-parse :deep/a
1542 '
1543
1544 test_expect_success 'sparse index is not expanded: update-index' '
1545 init_repos &&
1546
1547 deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
1548 ensure_not_expanded update-index --cacheinfo 100644 $deep_a_oid deep/a &&
1549
1550 echo "test" >sparse-index/README.md &&
1551 echo "test2" >sparse-index/a &&
1552 rm -f sparse-index/deep/a &&
1553
1554 ensure_not_expanded update-index --add README.md &&
1555 ensure_not_expanded update-index a &&
1556 ensure_not_expanded update-index --remove deep/a &&
1557
1558 ensure_not_expanded reset --soft update-deep &&
1559 ensure_not_expanded update-index --add --remove --again
1560 '
1561
1562 test_expect_success 'sparse index is not expanded: blame' '
1563 init_repos &&
1564
1565 for file in a \
1566 deep/a \
1567 deep/deeper1/a \
1568 deep/deeper1/deepest/a
1569 do
1570 ensure_not_expanded blame $file
1571 done
1572 '
1573
1574 test_expect_success 'sparse index is not expanded: fetch/pull' '
1575 init_repos &&
1576
1577 git -C sparse-index remote add full "file://$(pwd)/full-checkout" &&
1578 ensure_not_expanded fetch full &&
1579 git -C full-checkout commit --allow-empty -m "for pull merge" &&
1580 git -C sparse-index commit --allow-empty -m "for pull merge" &&
1581 ensure_not_expanded pull full base
1582 '
1583
1584 test_expect_success 'sparse index is not expanded: read-tree' '
1585 init_repos &&
1586
1587 ensure_not_expanded checkout -b test-branch update-folder1 &&
1588 for MERGE_TREES in "base HEAD update-folder2" \
1589 "base HEAD rename-base" \
1590 "base update-folder2" \
1591 "base rename-base" \
1592 "update-folder2"
1593 do
1594 ensure_not_expanded read-tree -mu $MERGE_TREES &&
1595 ensure_not_expanded reset --hard || return 1
1596 done &&
1597
1598 rm -rf sparse-index/deep/deeper2 &&
1599 ensure_not_expanded add . &&
1600 ensure_not_expanded commit -m "test" &&
1601
1602 ensure_not_expanded read-tree --prefix=deep/deeper2 -u deepest
1603 '
1604
1605 test_expect_success 'ls-files' '
1606 init_repos &&
1607
1608 # Use a smaller sparse-checkout for reduced output
1609 test_sparse_match git sparse-checkout set &&
1610
1611 # Behavior agrees by default. Sparse index is expanded.
1612 test_all_match git ls-files &&
1613
1614 # With --sparse, the sparse index data changes behavior.
1615 git -C sparse-index ls-files --sparse >actual &&
1616
1617 cat >expect <<-\EOF &&
1618 a
1619 before/
1620 deep/
1621 e
1622 folder1-
1623 folder1.x
1624 folder1/
1625 folder10
1626 folder2/
1627 g
1628 x/
1629 z
1630 EOF
1631
1632 test_cmp expect actual &&
1633
1634 # With --sparse and no sparse index, nothing changes.
1635 git -C sparse-checkout ls-files >dense &&
1636 git -C sparse-checkout ls-files --sparse >sparse &&
1637 test_cmp dense sparse &&
1638
1639 # Set up a strange condition of having a file edit
1640 # outside of the sparse-checkout cone. We want to verify
1641 # that all modes handle this the same, and detect the
1642 # modification.
1643 write_script edit-content <<-\EOF &&
1644 mkdir -p folder1 &&
1645 echo content >>folder1/a
1646 EOF
1647 run_on_all ../edit-content &&
1648
1649 test_all_match git ls-files --modified &&
1650
1651 git -C sparse-index ls-files --sparse --modified >sparse-index-out &&
1652 cat >expect <<-\EOF &&
1653 folder1/a
1654 EOF
1655 test_cmp expect sparse-index-out &&
1656
1657 # Add folder1 to the sparse-checkout cone and
1658 # check that ls-files shows the expanded files.
1659 test_sparse_match git sparse-checkout add folder1 &&
1660 test_all_match git ls-files --modified &&
1661
1662 test_all_match git ls-files &&
1663 git -C sparse-index ls-files --sparse >actual &&
1664
1665 cat >expect <<-\EOF &&
1666 a
1667 before/
1668 deep/
1669 e
1670 folder1-
1671 folder1.x
1672 folder1/0/0/0
1673 folder1/0/1
1674 folder1/a
1675 folder10
1676 folder2/
1677 g
1678 x/
1679 z
1680 EOF
1681
1682 test_cmp expect actual &&
1683
1684 # Double-check index expansion is avoided
1685 ensure_not_expanded ls-files --sparse
1686 '
1687
1688 test_expect_success 'sparse index is not expanded: sparse-checkout' '
1689 init_repos &&
1690
1691 ensure_not_expanded sparse-checkout set deep/deeper2 &&
1692 ensure_not_expanded sparse-checkout set deep/deeper1 &&
1693 ensure_not_expanded sparse-checkout set deep &&
1694 ensure_not_expanded sparse-checkout add folder1 &&
1695 ensure_not_expanded sparse-checkout set deep/deeper1 &&
1696 ensure_not_expanded sparse-checkout set folder2 &&
1697
1698 # Demonstrate that the checks that "folder1/a" is a file
1699 # do not cause a sparse-index expansion (since it is in the
1700 # sparse-checkout cone).
1701 echo >>sparse-index/folder2/a &&
1702 git -C sparse-index add folder2/a &&
1703
1704 ensure_not_expanded sparse-checkout add folder1 &&
1705
1706 # Skip checks here, since deep/deeper1 is inside a sparse directory
1707 # that must be expanded to check whether `deep/deeper1` is a file
1708 # or not.
1709 ensure_not_expanded sparse-checkout set --skip-checks deep/deeper1 &&
1710 ensure_not_expanded sparse-checkout set
1711 '
1712
1713 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
1714 # in this scenario, but it shouldn't.
1715 test_expect_success 'reset mixed and checkout orphan' '
1716 init_repos &&
1717
1718 test_all_match git checkout rename-out-to-in &&
1719
1720 # Sparse checkouts do not agree with full checkouts about
1721 # how to report a directory/file conflict during a reset.
1722 # This command would fail with test_all_match because the
1723 # full checkout reports "T folder1/0/1" while a sparse
1724 # checkout reports "D folder1/0/1". This matches because
1725 # the sparse checkouts skip "adding" the other side of
1726 # the conflict.
1727 test_sparse_match git reset --mixed HEAD~1 &&
1728 test_sparse_match git ls-files --stage &&
1729 test_sparse_match git status --porcelain=v2 &&
1730
1731 # At this point, sparse-checkouts behave differently
1732 # from the full-checkout.
1733 test_sparse_match git checkout --orphan new-branch &&
1734 test_sparse_match git ls-files --stage &&
1735 test_sparse_match git status --porcelain=v2
1736 '
1737
1738 test_expect_success 'add everything with deep new file' '
1739 init_repos &&
1740
1741 run_on_sparse git sparse-checkout set deep/deeper1/deepest &&
1742
1743 run_on_all touch deep/deeper1/x &&
1744 test_all_match git add . &&
1745 test_all_match git status --porcelain=v2
1746 '
1747
1748 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1749 # directory/file conflicts, even without sparse-checkout. Use this
1750 # test only as a documentation of the incorrect behavior, not a
1751 # measure of how it _should_ behave.
1752 test_expect_success 'checkout behaves oddly with df-conflict-1' '
1753 init_repos &&
1754
1755 test_sparse_match git sparse-checkout disable &&
1756
1757 write_script edit-content <<-\EOF &&
1758 echo content >>folder1/larger-content
1759 git add folder1
1760 EOF
1761
1762 run_on_all ../edit-content &&
1763 test_all_match git status --porcelain=v2 &&
1764
1765 git -C sparse-checkout sparse-checkout init --cone &&
1766 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1767
1768 test_all_match git status --porcelain=v2 &&
1769
1770 # This checkout command should fail, because we have a staged
1771 # change to folder1/larger-content, but the destination changes
1772 # folder1 to a file.
1773 git -C full-checkout checkout df-conflict-1 \
1774 1>full-checkout-out \
1775 2>full-checkout-err &&
1776 git -C sparse-checkout checkout df-conflict-1 \
1777 1>sparse-checkout-out \
1778 2>sparse-checkout-err &&
1779 git -C sparse-index checkout df-conflict-1 \
1780 1>sparse-index-out \
1781 2>sparse-index-err &&
1782
1783 # Instead, the checkout deletes the folder1 file and adds the
1784 # folder1/larger-content file, leaving all other paths that were
1785 # in folder1/ as deleted (without any warning).
1786 cat >expect <<-EOF &&
1787 D folder1
1788 A folder1/larger-content
1789 EOF
1790 test_cmp expect full-checkout-out &&
1791 test_cmp expect sparse-checkout-out &&
1792
1793 # The sparse-index reports no output
1794 test_must_be_empty sparse-index-out &&
1795
1796 # stderr: Switched to branch df-conflict-1
1797 test_cmp full-checkout-err sparse-checkout-err &&
1798 test_cmp full-checkout-err sparse-checkout-err
1799 '
1800
1801 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1802 # directory/file conflicts, even without sparse-checkout. Use this
1803 # test only as a documentation of the incorrect behavior, not a
1804 # measure of how it _should_ behave.
1805 test_expect_success 'checkout behaves oddly with df-conflict-2' '
1806 init_repos &&
1807
1808 test_sparse_match git sparse-checkout disable &&
1809
1810 write_script edit-content <<-\EOF &&
1811 echo content >>folder2/larger-content
1812 git add folder2
1813 EOF
1814
1815 run_on_all ../edit-content &&
1816 test_all_match git status --porcelain=v2 &&
1817
1818 git -C sparse-checkout sparse-checkout init --cone &&
1819 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1820
1821 test_all_match git status --porcelain=v2 &&
1822
1823 # This checkout command should fail, because we have a staged
1824 # change to folder1/larger-content, but the destination changes
1825 # folder1 to a file.
1826 git -C full-checkout checkout df-conflict-2 \
1827 1>full-checkout-out \
1828 2>full-checkout-err &&
1829 git -C sparse-checkout checkout df-conflict-2 \
1830 1>sparse-checkout-out \
1831 2>sparse-checkout-err &&
1832 git -C sparse-index checkout df-conflict-2 \
1833 1>sparse-index-out \
1834 2>sparse-index-err &&
1835
1836 # The full checkout deviates from the df-conflict-1 case here!
1837 # It drops the change to folder1/larger-content and leaves the
1838 # folder1 path as-is on disk. The sparse-index behaves the same.
1839 test_must_be_empty full-checkout-out &&
1840 test_must_be_empty sparse-index-out &&
1841
1842 # In the sparse-checkout case, the checkout deletes the folder1
1843 # file and adds the folder1/larger-content file, leaving all other
1844 # paths that were in folder1/ as deleted (without any warning).
1845 cat >expect <<-EOF &&
1846 D folder2
1847 A folder2/larger-content
1848 EOF
1849 test_cmp expect sparse-checkout-out &&
1850
1851 # Switched to branch df-conflict-1
1852 test_cmp full-checkout-err sparse-checkout-err &&
1853 test_cmp full-checkout-err sparse-index-err
1854 '
1855
1856 test_done