]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1092-sparse-checkout-compatibility.sh
Merge branch 'jc/find-header'
[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 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
53 do
54 git checkout -b update-$dir base &&
55 echo "updated $dir" >$dir/a &&
56 git commit -a -m "update $dir" || return 1
57 done &&
58
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" &&
71
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/..." &&
80
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/..." &&
91
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/..." &&
101
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" &&
107
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" &&
113
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" &&
120
121 for side in left right
122 do
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 &&
130
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" &&
136
137 git checkout -f base &&
138 git reset --hard
139 )
140 '
141
142 init_repos () {
143 rm -rf full-checkout sparse-checkout sparse-index &&
144
145 # create repos in initial state
146 cp -r initial-repo full-checkout &&
147 git -C full-checkout reset --hard &&
148
149 cp -r initial-repo sparse-checkout &&
150 git -C sparse-checkout reset --hard &&
151
152 cp -r initial-repo sparse-index &&
153 git -C sparse-index reset --hard &&
154
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
161 }
162
163 run_on_sparse () {
164 (
165 cd sparse-checkout &&
166 GIT_PROGRESS_DELAY=100000 "$@" >../sparse-checkout-out 2>../sparse-checkout-err
167 ) &&
168 (
169 cd sparse-index &&
170 GIT_PROGRESS_DELAY=100000 "$@" >../sparse-index-out 2>../sparse-index-err
171 )
172 }
173
174 run_on_all () {
175 (
176 cd full-checkout &&
177 GIT_PROGRESS_DELAY=100000 "$@" >../full-checkout-out 2>../full-checkout-err
178 ) &&
179 run_on_sparse "$@"
180 }
181
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
188 }
189
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
194 }
195
196 test_sparse_unstaged () {
197 file=$1 &&
198 for repo in sparse-checkout sparse-index
199 do
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
204 }
205
206 test_expect_success 'sparse-index contents' '
207 init_repos &&
208
209 git -C sparse-index ls-files --sparse --stage >cache &&
210 for dir in folder1 folder2 x
211 do
212 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
213 grep "040000 $TREE 0 $dir/" cache \
214 || return 1
215 done &&
216
217 git -C sparse-index sparse-checkout set folder1 &&
218
219 git -C sparse-index ls-files --sparse --stage >cache &&
220 for dir in deep folder2 x
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 git -C sparse-index sparse-checkout set deep/deeper1 &&
228
229 git -C sparse-index ls-files --sparse --stage >cache &&
230 for dir in deep/deeper2 folder1 folder2 x
231 do
232 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
233 grep "040000 $TREE 0 $dir/" cache \
234 || return 1
235 done &&
236
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
240 '
241
242 test_expect_success 'expanded in-memory index matches full index' '
243 init_repos &&
244 test_sparse_match git ls-files --stage
245 '
246
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
261 '
262
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
269 '
270
271 test_expect_success 'add, commit, checkout' '
272 init_repos &&
273
274 write_script edit-contents <<-\EOF &&
275 echo text >>$1
276 EOF
277 run_on_all ../edit-contents README.md &&
278
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" &&
282
283 test_all_match git checkout HEAD~1 &&
284 test_all_match git checkout - &&
285
286 run_on_all ../edit-contents README.md &&
287
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" &&
291
292 test_all_match git checkout HEAD~1 &&
293 test_all_match git checkout - &&
294
295 run_on_all ../edit-contents deep/newfile &&
296
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" &&
302
303 test_all_match git checkout HEAD~1 &&
304 test_all_match git checkout -
305 '
306
307 test_expect_success 'deep changes during checkout' '
308 init_repos &&
309
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
313 '
314
315 test_expect_success 'add outside sparse cone' '
316 init_repos &&
317
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
327 '
328
329 test_expect_success 'commit including unstaged changes' '
330 init_repos &&
331
332 write_script edit-file <<-\EOF &&
333 echo $1 >$2
334 EOF
335
336 run_on_all ../edit-file 1 a &&
337 run_on_all ../edit-file 1 deep/a &&
338
339 test_all_match git commit -m "-a" -a &&
340 test_all_match git status --porcelain=v2 &&
341
342 run_on_all ../edit-file 2 a &&
343 run_on_all ../edit-file 2 deep/a &&
344
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 &&
349
350 run_on_all ../edit-file 3 a &&
351 run_on_all ../edit-file 3 deep/a &&
352
353 test_all_match git commit -m "--amend" -a --amend &&
354 test_all_match git status --porcelain=v2
355 '
356
357 test_expect_success 'status/add: outside sparse cone' '
358 init_repos &&
359
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 &&
364
365 test_sparse_match git status &&
366
367 write_script edit-contents <<-\EOF &&
368 echo text >>$1
369 EOF
370 run_on_sparse ../edit-contents folder1/a &&
371 run_on_all ../edit-contents folder1/new &&
372
373 test_sparse_match git status --porcelain=v2 &&
374
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 &&
387
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} &&
392
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}
398 '
399
400 test_expect_success 'checkout and reset --hard' '
401 init_repos &&
402
403 test_all_match git checkout update-folder1 &&
404 test_all_match git status --porcelain=v2 &&
405
406 test_all_match git checkout update-deep &&
407 test_all_match git status --porcelain=v2 &&
408
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
413 '
414
415 test_expect_success 'diff --cached' '
416 init_repos &&
417
418 write_script edit-contents <<-\EOF &&
419 echo text >>README.md
420 EOF
421 run_on_all ../edit-contents &&
422
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
428 '
429
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 &&
434
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
440 do
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
447 '
448
449 test_expect_success 'diff with directory/file conflicts' '
450 init_repos &&
451
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
458 do
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
467 '
468
469 test_expect_success 'log with pathspec outside sparse definition' '
470 init_repos &&
471
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 &&
478
479 test_all_match git checkout update-folder1 &&
480 test_all_match git log -- folder1/a
481 '
482
483 test_expect_success 'blame with pathspec inside sparse definition' '
484 init_repos &&
485
486 for file in a \
487 deep/a \
488 deep/deeper1/a \
489 deep/deeper1/deepest/a
490 do
491 test_all_match git blame $file
492 done
493 '
494
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 &&
501
502 for file in a \
503 deep/a \
504 deep/deeper1/a \
505 deep/deeper1/deepest/a
506 do
507 test_sparse_match test_must_fail git blame $file &&
508 cat >expect <<-EOF &&
509 fatal: Cannot lstat '"'"'$file'"'"': No such file or directory
510 EOF
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
516 '
517
518 test_expect_success 'checkout and reset (mixed)' '
519 init_repos &&
520
521 test_all_match git checkout -b reset-test update-deep &&
522 test_all_match git reset deepest &&
523
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
532 '
533
534 test_expect_success 'checkout and reset (merge)' '
535 init_repos &&
536
537 write_script edit-contents <<-\EOF &&
538 echo text >>$1
539 EOF
540
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 &&
545
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
549 '
550
551 test_expect_success 'checkout and reset (keep)' '
552 init_repos &&
553
554 write_script edit-contents <<-\EOF &&
555 echo text >>$1
556 EOF
557
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 &&
562
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
566 '
567
568 test_expect_success 'reset with pathspecs inside sparse definition' '
569 init_repos &&
570
571 write_script edit-contents <<-\EOF &&
572 echo text >>$1
573 EOF
574
575 test_all_match git checkout -b reset-test update-deep &&
576 run_on_all ../edit-contents deep/a &&
577
578 test_all_match git reset base -- deep/a &&
579 test_all_match git status --porcelain=v2 &&
580
581 test_all_match git reset base -- nonexistent-file &&
582 test_all_match git status --porcelain=v2 &&
583
584 test_all_match git reset deepest -- deep &&
585 test_all_match git status --porcelain=v2
586 '
587
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 &&
593
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 &&
598
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
603 '
604
605 test_expect_success 'reset with wildcard pathspec' '
606 init_repos &&
607
608 test_all_match git reset update-deep -- deep\* &&
609 test_all_match git ls-files -s -- deep &&
610
611 test_all_match git reset deepest -- deep\*\*\* &&
612 test_all_match git ls-files -s -- deep &&
613
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.
618
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 &&
622
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 &&
626
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
630 '
631
632 test_expect_success 'merge, cherry-pick, and rebase' '
633 init_repos &&
634
635 for OPERATION in "merge -m merge" cherry-pick "rebase --apply" "rebase --merge"
636 do
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
643 '
644
645 test_expect_success 'merge with conflict outside cone' '
646 init_repos &&
647
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 &&
652
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 &&
657
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 &&
664
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 &&
673
674 test_all_match git merge --continue &&
675 test_all_match git status --porcelain=v2 &&
676 test_all_match git rev-parse HEAD^{tree}
677 '
678
679 test_expect_success 'cherry-pick/rebase with conflict outside cone' '
680 init_repos &&
681
682 for OPERATION in cherry-pick rebase
683 do
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 &&
689
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 &&
694
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 &&
704
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 &&
716
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
721 '
722
723 test_expect_success 'merge with outside renames' '
724 init_repos &&
725
726 for type in out-to-out out-to-in in-to-out
727 do
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
733 '
734
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 &&
739
740 write_script edit-conflict <<-\EOF &&
741 echo $1 >conflict
742 EOF
743
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" &&
748
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" &&
753
754 test_all_match test_must_fail git cherry-pick to-cherry-pick
755 '
756
757 test_expect_success 'clean' '
758 init_repos &&
759
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" &&
764
765 run_on_sparse mkdir folder1 &&
766 run_on_all touch folder1/bogus &&
767
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 &&
773
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 &&
778
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 &&
783
784 test_sparse_match test_path_is_dir folder1
785 '
786
787 test_expect_success 'submodule handling' '
788 init_repos &&
789
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" &&
795
796 run_on_all git submodule add "$(pwd)/initial-repo" modules/sub &&
797 test_all_match git commit -m "add submodule" &&
798
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
804 '
805
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 &&
812
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 &&
817
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
823 '
824
825 test_expect_success 'index.sparse disabled inline uses full index' '
826 init_repos &&
827
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 &&
836
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 &&
846
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
854 '
855
856 ensure_not_expanded () {
857 rm -f trace2.txt &&
858 echo >>sparse-index/untracked.txt &&
859
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
871 }
872
873 test_expect_success 'sparse-index is not expanded' '
874 init_repos &&
875
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 &&
893
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 . &&
900
901 for ref in update-deep update-folder1 update-folder2 update-deep
902 do
903 echo >>sparse-index/README.md &&
904 ensure_not_expanded reset --hard $ref || return 1
905 done &&
906
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 &&
912
913 ensure_not_expanded reset base -- deep/a &&
914 ensure_not_expanded reset base -- nonexistent-file &&
915 ensure_not_expanded reset deepest -- deep &&
916
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/ &&
922
923 # Wildcard identifies only in-cone files, no index expansion
924 ensure_not_expanded reset deepest -- deep/\* &&
925
926 # Wildcard identifies only full sparse directories, no index expansion
927 ensure_not_expanded reset deepest -- folder\* &&
928
929 ensure_not_expanded checkout -f update-deep &&
930 test_config -C sparse-index pull.twohead ort &&
931 (
932 sane_unset GIT_TEST_MERGE_ALGORITHM &&
933 for OPERATION in "merge -m merge" cherry-pick rebase
934 do
935 ensure_not_expanded merge -m merge update-folder1 &&
936 ensure_not_expanded merge -m merge update-folder2 || return 1
937 done
938 )
939 '
940
941 test_expect_success 'sparse-index is not expanded: merge conflict in cone' '
942 init_repos &&
943
944 for side in right left
945 do
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 &&
950
951 (
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
955 )
956 '
957
958 test_expect_success 'sparse index is not expanded: diff' '
959 init_repos &&
960
961 write_script edit-contents <<-\EOF &&
962 echo text >>$1
963 EOF
964
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 &&
970
971 test_all_match git diff &&
972 test_all_match git diff --cached &&
973 ensure_not_expanded diff &&
974 ensure_not_expanded diff --cached &&
975
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 &&
984
985 test_all_match git diff &&
986 test_all_match git diff --cached &&
987 ensure_not_expanded diff &&
988 ensure_not_expanded diff --cached &&
989
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 &&
997
998 test_all_match git diff &&
999 test_all_match git diff --cached &&
1000 ensure_not_expanded diff &&
1001 ensure_not_expanded diff --cached
1002 '
1003
1004 test_expect_success 'sparse index is not expanded: blame' '
1005 init_repos &&
1006
1007 for file in a \
1008 deep/a \
1009 deep/deeper1/a \
1010 deep/deeper1/deepest/a
1011 do
1012 ensure_not_expanded blame $file
1013 done
1014 '
1015
1016 test_expect_success 'sparse index is not expanded: fetch/pull' '
1017 init_repos &&
1018
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
1024 '
1025
1026 test_expect_success 'ls-files' '
1027 init_repos &&
1028
1029 # Use a smaller sparse-checkout for reduced output
1030 test_sparse_match git sparse-checkout set &&
1031
1032 # Behavior agrees by default. Sparse index is expanded.
1033 test_all_match git ls-files &&
1034
1035 # With --sparse, the sparse index data changes behavior.
1036 git -C sparse-index ls-files --sparse >actual &&
1037
1038 cat >expect <<-\EOF &&
1039 a
1040 deep/
1041 e
1042 folder1-
1043 folder1.x
1044 folder1/
1045 folder10
1046 folder2/
1047 g
1048 x/
1049 z
1050 EOF
1051
1052 test_cmp expect actual &&
1053
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 &&
1058
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
1066 EOF
1067 run_on_sparse ../edit-content &&
1068
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 &&
1076
1077 git -C sparse-index ls-files --sparse --modified >sparse-index-out &&
1078 test_must_be_empty sparse-index-out &&
1079
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 &&
1084
1085 test_all_match git ls-files &&
1086 git -C sparse-index ls-files --sparse >actual &&
1087
1088 cat >expect <<-\EOF &&
1089 a
1090 deep/
1091 e
1092 folder1-
1093 folder1.x
1094 folder1/0/0/0
1095 folder1/0/1
1096 folder1/a
1097 folder10
1098 folder2/
1099 g
1100 x/
1101 z
1102 EOF
1103
1104 test_cmp expect actual &&
1105
1106 # Double-check index expansion is avoided
1107 ensure_not_expanded ls-files --sparse
1108 '
1109
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 &&
1114
1115 test_all_match git checkout rename-out-to-in &&
1116
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 &&
1127
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
1133 '
1134
1135 test_expect_success 'add everything with deep new file' '
1136 init_repos &&
1137
1138 run_on_sparse git sparse-checkout set deep/deeper1/deepest &&
1139
1140 run_on_all touch deep/deeper1/x &&
1141 test_all_match git add . &&
1142 test_all_match git status --porcelain=v2
1143 '
1144
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 &&
1151
1152 test_sparse_match git sparse-checkout disable &&
1153
1154 write_script edit-content <<-\EOF &&
1155 echo content >>folder1/larger-content
1156 git add folder1
1157 EOF
1158
1159 run_on_all ../edit-content &&
1160 test_all_match git status --porcelain=v2 &&
1161
1162 git -C sparse-checkout sparse-checkout init --cone &&
1163 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1164
1165 test_all_match git status --porcelain=v2 &&
1166
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 &&
1179
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
1186 EOF
1187 test_cmp expect full-checkout-out &&
1188 test_cmp expect sparse-checkout-out &&
1189
1190 # The sparse-index reports no output
1191 test_must_be_empty sparse-index-out &&
1192
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
1196 '
1197
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 &&
1204
1205 test_sparse_match git sparse-checkout disable &&
1206
1207 write_script edit-content <<-\EOF &&
1208 echo content >>folder2/larger-content
1209 git add folder2
1210 EOF
1211
1212 run_on_all ../edit-content &&
1213 test_all_match git status --porcelain=v2 &&
1214
1215 git -C sparse-checkout sparse-checkout init --cone &&
1216 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1217
1218 test_all_match git status --porcelain=v2 &&
1219
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 &&
1232
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 &&
1238
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
1245 EOF
1246 test_cmp expect sparse-checkout-out &&
1247
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
1251 '
1252
1253 test_done