]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1092-sparse-checkout-compatibility.sh
checkout-index: add --ignore-skip-worktree-bits option
[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 test-tool -C sparse-index read-cache --table >cache &&
210 for dir in folder1 folder2 x
211 do
212 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
213 grep "040000 tree $TREE $dir/" cache \
214 || return 1
215 done &&
216
217 git -C sparse-index sparse-checkout set folder1 &&
218
219 test-tool -C sparse-index read-cache --table >cache &&
220 for dir in deep folder2 x
221 do
222 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
223 grep "040000 tree $TREE $dir/" cache \
224 || return 1
225 done &&
226
227 git -C sparse-index sparse-checkout set deep/deeper1 &&
228
229 test-tool -C sparse-index read-cache --table >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 $TREE $dir/" cache \
234 || return 1
235 done &&
236
237 # Disabling the sparse-index removes tree entries with full ones
238 git -C sparse-index sparse-checkout init --no-sparse-index &&
239
240 test-tool -C sparse-index read-cache --table >cache &&
241 ! grep "040000 tree" cache &&
242 test_sparse_match test-tool read-cache --table
243 '
244
245 test_expect_success 'expanded in-memory index matches full index' '
246 init_repos &&
247 test_sparse_match test-tool read-cache --expand --table
248 '
249
250 test_expect_success 'status with options' '
251 init_repos &&
252 test_sparse_match ls &&
253 test_all_match git status --porcelain=v2 &&
254 test_all_match git status --porcelain=v2 -z -u &&
255 test_all_match git status --porcelain=v2 -uno &&
256 run_on_all touch README.md &&
257 test_all_match git status --porcelain=v2 &&
258 test_all_match git status --porcelain=v2 -z -u &&
259 test_all_match git status --porcelain=v2 -uno &&
260 test_all_match git add README.md &&
261 test_all_match git status --porcelain=v2 &&
262 test_all_match git status --porcelain=v2 -z -u &&
263 test_all_match git status --porcelain=v2 -uno
264 '
265
266 test_expect_success 'status reports sparse-checkout' '
267 init_repos &&
268 git -C sparse-checkout status >full &&
269 git -C sparse-index status >sparse &&
270 test_i18ngrep "You are in a sparse checkout with " full &&
271 test_i18ngrep "You are in a sparse checkout." sparse
272 '
273
274 test_expect_success 'add, commit, checkout' '
275 init_repos &&
276
277 write_script edit-contents <<-\EOF &&
278 echo text >>$1
279 EOF
280 run_on_all ../edit-contents README.md &&
281
282 test_all_match git add README.md &&
283 test_all_match git status --porcelain=v2 &&
284 test_all_match git commit -m "Add README.md" &&
285
286 test_all_match git checkout HEAD~1 &&
287 test_all_match git checkout - &&
288
289 run_on_all ../edit-contents README.md &&
290
291 test_all_match git add -A &&
292 test_all_match git status --porcelain=v2 &&
293 test_all_match git commit -m "Extend README.md" &&
294
295 test_all_match git checkout HEAD~1 &&
296 test_all_match git checkout - &&
297
298 run_on_all ../edit-contents deep/newfile &&
299
300 test_all_match git status --porcelain=v2 -uno &&
301 test_all_match git status --porcelain=v2 &&
302 test_all_match git add . &&
303 test_all_match git status --porcelain=v2 &&
304 test_all_match git commit -m "add deep/newfile" &&
305
306 test_all_match git checkout HEAD~1 &&
307 test_all_match git checkout -
308 '
309
310 test_expect_success 'deep changes during checkout' '
311 init_repos &&
312
313 test_sparse_match git sparse-checkout set deep/deeper1/deepest &&
314 test_all_match git checkout deepest &&
315 test_all_match git checkout base
316 '
317
318 test_expect_success 'add outside sparse cone' '
319 init_repos &&
320
321 run_on_sparse mkdir folder1 &&
322 run_on_sparse ../edit-contents folder1/a &&
323 run_on_sparse ../edit-contents folder1/newfile &&
324 test_sparse_match test_must_fail git add folder1/a &&
325 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
326 test_sparse_unstaged folder1/a &&
327 test_sparse_match test_must_fail git add folder1/newfile &&
328 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
329 test_sparse_unstaged folder1/newfile
330 '
331
332 test_expect_success 'commit including unstaged changes' '
333 init_repos &&
334
335 write_script edit-file <<-\EOF &&
336 echo $1 >$2
337 EOF
338
339 run_on_all ../edit-file 1 a &&
340 run_on_all ../edit-file 1 deep/a &&
341
342 test_all_match git commit -m "-a" -a &&
343 test_all_match git status --porcelain=v2 &&
344
345 run_on_all ../edit-file 2 a &&
346 run_on_all ../edit-file 2 deep/a &&
347
348 test_all_match git commit -m "--include" --include deep/a &&
349 test_all_match git status --porcelain=v2 &&
350 test_all_match git commit -m "--include" --include a &&
351 test_all_match git status --porcelain=v2 &&
352
353 run_on_all ../edit-file 3 a &&
354 run_on_all ../edit-file 3 deep/a &&
355
356 test_all_match git commit -m "--amend" -a --amend &&
357 test_all_match git status --porcelain=v2
358 '
359
360 test_expect_success 'status/add: outside sparse cone' '
361 init_repos &&
362
363 # folder1 is at HEAD, but outside the sparse cone
364 run_on_sparse mkdir folder1 &&
365 cp initial-repo/folder1/a sparse-checkout/folder1/a &&
366 cp initial-repo/folder1/a sparse-index/folder1/a &&
367
368 test_sparse_match git status &&
369
370 write_script edit-contents <<-\EOF &&
371 echo text >>$1
372 EOF
373 run_on_sparse ../edit-contents folder1/a &&
374 run_on_all ../edit-contents folder1/new &&
375
376 test_sparse_match git status --porcelain=v2 &&
377
378 # Adding the path outside of the sparse-checkout cone should fail.
379 test_sparse_match test_must_fail git add 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 --refresh folder1/a &&
383 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
384 test_sparse_unstaged folder1/a &&
385 test_sparse_match test_must_fail git add folder1/new &&
386 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
387 test_sparse_unstaged folder1/new &&
388 test_sparse_match git add --sparse folder1/a &&
389 test_sparse_match git add --sparse folder1/new &&
390
391 test_all_match git add --sparse . &&
392 test_all_match git status --porcelain=v2 &&
393 test_all_match git commit -m folder1/new &&
394 test_all_match git rev-parse HEAD^{tree} &&
395
396 run_on_all ../edit-contents folder1/newer &&
397 test_all_match git add --sparse folder1/ &&
398 test_all_match git status --porcelain=v2 &&
399 test_all_match git commit -m folder1/newer &&
400 test_all_match git rev-parse HEAD^{tree}
401 '
402
403 test_expect_success 'checkout and reset --hard' '
404 init_repos &&
405
406 test_all_match git checkout update-folder1 &&
407 test_all_match git status --porcelain=v2 &&
408
409 test_all_match git checkout update-deep &&
410 test_all_match git status --porcelain=v2 &&
411
412 test_all_match git checkout -b reset-test &&
413 test_all_match git reset --hard deepest &&
414 test_all_match git reset --hard update-folder1 &&
415 test_all_match git reset --hard update-folder2
416 '
417
418 test_expect_success 'diff --cached' '
419 init_repos &&
420
421 write_script edit-contents <<-\EOF &&
422 echo text >>README.md
423 EOF
424 run_on_all ../edit-contents &&
425
426 test_all_match git diff &&
427 test_all_match git diff --cached &&
428 test_all_match git add README.md &&
429 test_all_match git diff &&
430 test_all_match git diff --cached
431 '
432
433 # NEEDSWORK: sparse-checkout behaves differently from full-checkout when
434 # running this test with 'df-conflict-2' after 'df-conflict-1'.
435 test_expect_success 'diff with renames and conflicts' '
436 init_repos &&
437
438 for branch in rename-out-to-out \
439 rename-out-to-in \
440 rename-in-to-out \
441 df-conflict-1 \
442 fd-conflict
443 do
444 test_all_match git checkout rename-base &&
445 test_all_match git checkout $branch -- . &&
446 test_all_match git status --porcelain=v2 &&
447 test_all_match git diff --cached --no-renames &&
448 test_all_match git diff --cached --find-renames || return 1
449 done
450 '
451
452 test_expect_success 'diff with directory/file conflicts' '
453 init_repos &&
454
455 for branch in rename-out-to-out \
456 rename-out-to-in \
457 rename-in-to-out \
458 df-conflict-1 \
459 df-conflict-2 \
460 fd-conflict
461 do
462 git -C full-checkout reset --hard &&
463 test_sparse_match git reset --hard &&
464 test_all_match git checkout $branch &&
465 test_all_match git checkout rename-base -- . &&
466 test_all_match git status --porcelain=v2 &&
467 test_all_match git diff --cached --no-renames &&
468 test_all_match git diff --cached --find-renames || return 1
469 done
470 '
471
472 test_expect_success 'log with pathspec outside sparse definition' '
473 init_repos &&
474
475 test_all_match git log -- a &&
476 test_all_match git log -- folder1/a &&
477 test_all_match git log -- folder2/a &&
478 test_all_match git log -- deep/a &&
479 test_all_match git log -- deep/deeper1/a &&
480 test_all_match git log -- deep/deeper1/deepest/a &&
481
482 test_all_match git checkout update-folder1 &&
483 test_all_match git log -- folder1/a
484 '
485
486 test_expect_success 'blame with pathspec inside sparse definition' '
487 init_repos &&
488
489 for file in a \
490 deep/a \
491 deep/deeper1/a \
492 deep/deeper1/deepest/a
493 do
494 test_all_match git blame $file
495 done
496 '
497
498 # Without a revision specified, blame will error if passed any file that
499 # is not present in the working directory (even if the file is tracked).
500 # Here we just verify that this is also true with sparse checkouts.
501 test_expect_success 'blame with pathspec outside sparse definition' '
502 init_repos &&
503 test_sparse_match git sparse-checkout set &&
504
505 for file in a \
506 deep/a \
507 deep/deeper1/a \
508 deep/deeper1/deepest/a
509 do
510 test_sparse_match test_must_fail git blame $file &&
511 cat >expect <<-EOF &&
512 fatal: Cannot lstat '"'"'$file'"'"': No such file or directory
513 EOF
514 # We compare sparse-checkout-err and sparse-index-err in
515 # `test_sparse_match`. Given we know they are the same, we
516 # only check the content of sparse-index-err here.
517 test_cmp expect sparse-index-err
518 done
519 '
520
521 test_expect_success 'checkout and reset (mixed)' '
522 init_repos &&
523
524 test_all_match git checkout -b reset-test update-deep &&
525 test_all_match git reset deepest &&
526
527 # Because skip-worktree is preserved, resetting to update-folder1
528 # will show worktree changes for folder1/a in full-checkout, but not
529 # in sparse-checkout or sparse-index.
530 git -C full-checkout reset update-folder1 >full-checkout-out &&
531 test_sparse_match git reset update-folder1 &&
532 grep "M folder1/a" full-checkout-out &&
533 ! grep "M folder1/a" sparse-checkout-out &&
534 run_on_sparse test_path_is_missing folder1
535 '
536
537 test_expect_success 'checkout and reset (merge)' '
538 init_repos &&
539
540 write_script edit-contents <<-\EOF &&
541 echo text >>$1
542 EOF
543
544 test_all_match git checkout -b reset-test update-deep &&
545 run_on_all ../edit-contents a &&
546 test_all_match git reset --merge deepest &&
547 test_all_match git status --porcelain=v2 &&
548
549 test_all_match git reset --hard update-deep &&
550 run_on_all ../edit-contents deep/a &&
551 test_all_match test_must_fail git reset --merge deepest
552 '
553
554 test_expect_success 'checkout and reset (keep)' '
555 init_repos &&
556
557 write_script edit-contents <<-\EOF &&
558 echo text >>$1
559 EOF
560
561 test_all_match git checkout -b reset-test update-deep &&
562 run_on_all ../edit-contents a &&
563 test_all_match git reset --keep deepest &&
564 test_all_match git status --porcelain=v2 &&
565
566 test_all_match git reset --hard update-deep &&
567 run_on_all ../edit-contents deep/a &&
568 test_all_match test_must_fail git reset --keep deepest
569 '
570
571 test_expect_success 'reset with pathspecs inside sparse definition' '
572 init_repos &&
573
574 write_script edit-contents <<-\EOF &&
575 echo text >>$1
576 EOF
577
578 test_all_match git checkout -b reset-test update-deep &&
579 run_on_all ../edit-contents deep/a &&
580
581 test_all_match git reset base -- deep/a &&
582 test_all_match git status --porcelain=v2 &&
583
584 test_all_match git reset base -- nonexistent-file &&
585 test_all_match git status --porcelain=v2 &&
586
587 test_all_match git reset deepest -- deep &&
588 test_all_match git status --porcelain=v2
589 '
590
591 # Although the working tree differs between full and sparse checkouts after
592 # reset, the state of the index is the same.
593 test_expect_success 'reset with pathspecs outside sparse definition' '
594 init_repos &&
595 test_all_match git checkout -b reset-test base &&
596
597 test_sparse_match git reset update-folder1 -- folder1 &&
598 git -C full-checkout reset update-folder1 -- folder1 &&
599 test_all_match git ls-files -s -- folder1 &&
600
601 test_sparse_match git reset update-folder2 -- folder2/a &&
602 git -C full-checkout reset update-folder2 -- folder2/a &&
603 test_all_match git ls-files -s -- folder2/a
604 '
605
606 test_expect_success 'reset with wildcard pathspec' '
607 init_repos &&
608
609 test_all_match git reset update-deep -- deep\* &&
610 test_all_match git ls-files -s -- deep &&
611
612 test_all_match git reset deepest -- deep\*\*\* &&
613 test_all_match git ls-files -s -- deep &&
614
615 # The following `git reset`s result in updating the index on files with
616 # `skip-worktree` enabled. To avoid failing due to discrepencies in reported
617 # "modified" files, `test_sparse_match` reset is performed separately from
618 # "full-checkout" reset, then the index contents of all repos are verified.
619
620 test_sparse_match git reset update-folder1 -- \*/a &&
621 git -C full-checkout reset update-folder1 -- \*/a &&
622 test_all_match git ls-files -s -- deep/a folder1/a &&
623
624 test_sparse_match git reset update-folder2 -- folder\* &&
625 git -C full-checkout reset update-folder2 -- folder\* &&
626 test_all_match git ls-files -s -- folder10 folder1 folder2 &&
627
628 test_sparse_match git reset base -- folder1/\* &&
629 git -C full-checkout reset base -- folder1/\* &&
630 test_all_match git ls-files -s -- folder1
631 '
632
633 test_expect_success 'merge, cherry-pick, and rebase' '
634 init_repos &&
635
636 for OPERATION in "merge -m merge" cherry-pick "rebase --apply" "rebase --merge"
637 do
638 test_all_match git checkout -B temp update-deep &&
639 test_all_match git $OPERATION update-folder1 &&
640 test_all_match git rev-parse HEAD^{tree} &&
641 test_all_match git $OPERATION update-folder2 &&
642 test_all_match git rev-parse HEAD^{tree} || return 1
643 done
644 '
645
646 test_expect_success 'merge with conflict outside cone' '
647 init_repos &&
648
649 test_all_match git checkout -b merge-tip merge-left &&
650 test_all_match git status --porcelain=v2 &&
651 test_all_match test_must_fail git merge -m merge merge-right &&
652 test_all_match git status --porcelain=v2 &&
653
654 # Resolve the conflict in different ways:
655 # 1. Revert to the base
656 test_all_match git checkout base -- deep/deeper2/a &&
657 test_all_match git status --porcelain=v2 &&
658
659 # 2. Add the file with conflict markers
660 test_sparse_match test_must_fail git add folder1/a &&
661 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
662 test_sparse_unstaged folder1/a &&
663 test_all_match git add --sparse folder1/a &&
664 test_all_match git status --porcelain=v2 &&
665
666 # 3. Rename the file to another sparse filename and
667 # accept conflict markers as resolved content.
668 run_on_all mv folder2/a folder2/z &&
669 test_sparse_match test_must_fail git add folder2 &&
670 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
671 test_sparse_unstaged folder2/z &&
672 test_all_match git add --sparse folder2 &&
673 test_all_match git status --porcelain=v2 &&
674
675 test_all_match git merge --continue &&
676 test_all_match git status --porcelain=v2 &&
677 test_all_match git rev-parse HEAD^{tree}
678 '
679
680 test_expect_success 'cherry-pick/rebase with conflict outside cone' '
681 init_repos &&
682
683 for OPERATION in cherry-pick rebase
684 do
685 test_all_match git checkout -B tip &&
686 test_all_match git reset --hard merge-left &&
687 test_all_match git status --porcelain=v2 &&
688 test_all_match test_must_fail git $OPERATION merge-right &&
689 test_all_match git status --porcelain=v2 &&
690
691 # Resolve the conflict in different ways:
692 # 1. Revert to the base
693 test_all_match git checkout base -- deep/deeper2/a &&
694 test_all_match git status --porcelain=v2 &&
695
696 # 2. Add the file with conflict markers
697 # NEEDSWORK: Even though the merge conflict removed the
698 # SKIP_WORKTREE bit from the index entry for folder1/a, we should
699 # warn that this is a problematic add.
700 test_sparse_match test_must_fail git add folder1/a &&
701 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
702 test_sparse_unstaged folder1/a &&
703 test_all_match git add --sparse folder1/a &&
704 test_all_match git status --porcelain=v2 &&
705
706 # 3. Rename the file to another sparse filename and
707 # accept conflict markers as resolved content.
708 # NEEDSWORK: This mode now fails, because folder2/z is
709 # outside of the sparse-checkout cone and does not match an
710 # existing index entry with the SKIP_WORKTREE bit cleared.
711 run_on_all mv folder2/a folder2/z &&
712 test_sparse_match test_must_fail git add folder2 &&
713 grep "Disable or modify the sparsity rules" sparse-checkout-err &&
714 test_sparse_unstaged folder2/z &&
715 test_all_match git add --sparse folder2 &&
716 test_all_match git status --porcelain=v2 &&
717
718 test_all_match git $OPERATION --continue &&
719 test_all_match git status --porcelain=v2 &&
720 test_all_match git rev-parse HEAD^{tree} || return 1
721 done
722 '
723
724 test_expect_success 'merge with outside renames' '
725 init_repos &&
726
727 for type in out-to-out out-to-in in-to-out
728 do
729 test_all_match git reset --hard &&
730 test_all_match git checkout -f -b merge-$type update-deep &&
731 test_all_match git merge -m "$type" rename-$type &&
732 test_all_match git rev-parse HEAD^{tree} || return 1
733 done
734 '
735
736 # Sparse-index fails to convert the index in the
737 # final 'git cherry-pick' command.
738 test_expect_success 'cherry-pick with conflicts' '
739 init_repos &&
740
741 write_script edit-conflict <<-\EOF &&
742 echo $1 >conflict
743 EOF
744
745 test_all_match git checkout -b to-cherry-pick &&
746 run_on_all ../edit-conflict ABC &&
747 test_all_match git add conflict &&
748 test_all_match git commit -m "conflict to pick" &&
749
750 test_all_match git checkout -B base HEAD~1 &&
751 run_on_all ../edit-conflict DEF &&
752 test_all_match git add conflict &&
753 test_all_match git commit -m "conflict in base" &&
754
755 test_all_match test_must_fail git cherry-pick to-cherry-pick
756 '
757
758 test_expect_success 'checkout-index inside sparse definition' '
759 init_repos &&
760
761 run_on_all rm -f deep/a &&
762 test_all_match git checkout-index -- deep/a &&
763 test_all_match git status --porcelain=v2 &&
764
765 echo test >>new-a &&
766 run_on_all cp ../new-a a &&
767 test_all_match test_must_fail git checkout-index -- a &&
768 test_all_match git checkout-index -f -- a &&
769 test_all_match git status --porcelain=v2
770 '
771
772 test_expect_success 'checkout-index outside sparse definition' '
773 init_repos &&
774
775 # Without --ignore-skip-worktree-bits, outside-of-cone files will trigger
776 # an error
777 test_sparse_match test_must_fail git checkout-index -- folder1/a &&
778 test_i18ngrep "folder1/a has skip-worktree enabled" sparse-checkout-err &&
779 test_path_is_missing folder1/a &&
780
781 # With --ignore-skip-worktree-bits, outside-of-cone files are checked out
782 test_sparse_match git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
783 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
784 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a &&
785
786 run_on_sparse rm -rf folder1 &&
787 echo test >new-a &&
788 run_on_sparse mkdir -p folder1 &&
789 run_on_all cp ../new-a folder1/a &&
790
791 test_all_match test_must_fail git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
792 test_all_match git checkout-index -f --ignore-skip-worktree-bits -- folder1/a &&
793 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
794 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a
795 '
796
797 test_expect_success 'checkout-index with folders' '
798 init_repos &&
799
800 # Inside checkout definition
801 test_all_match test_must_fail git checkout-index -f -- deep/ &&
802
803 # Outside checkout definition
804 test_all_match test_must_fail git checkout-index -f -- folder1/
805 '
806
807 test_expect_success 'checkout-index --all' '
808 init_repos &&
809
810 test_all_match git checkout-index --all &&
811 test_sparse_match test_path_is_missing folder1 &&
812
813 # --ignore-skip-worktree-bits will cause `skip-worktree` files to be
814 # checked out, causing the outside-of-cone `folder1` to exist on-disk
815 test_all_match git checkout-index --ignore-skip-worktree-bits --all &&
816 test_all_match test_path_exists folder1
817 '
818
819 test_expect_success 'clean' '
820 init_repos &&
821
822 echo bogus >>.gitignore &&
823 run_on_all cp ../.gitignore . &&
824 test_all_match git add .gitignore &&
825 test_all_match git commit -m "ignore bogus files" &&
826
827 run_on_sparse mkdir folder1 &&
828 run_on_all mkdir -p deep/untracked-deep &&
829 run_on_all touch folder1/bogus &&
830 run_on_all touch folder1/untracked &&
831 run_on_all touch deep/untracked-deep/bogus &&
832 run_on_all touch deep/untracked-deep/untracked &&
833
834 test_all_match git status --porcelain=v2 &&
835 test_all_match git clean -f &&
836 test_all_match git status --porcelain=v2 &&
837 test_sparse_match ls &&
838 test_sparse_match ls folder1 &&
839 run_on_all test_path_exists folder1/bogus &&
840 run_on_all test_path_is_missing folder1/untracked &&
841 run_on_all test_path_exists deep/untracked-deep/bogus &&
842 run_on_all test_path_exists deep/untracked-deep/untracked &&
843
844 test_all_match git clean -fd &&
845 test_all_match git status --porcelain=v2 &&
846 test_sparse_match ls &&
847 test_sparse_match ls folder1 &&
848 run_on_all test_path_exists folder1/bogus &&
849 run_on_all test_path_exists deep/untracked-deep/bogus &&
850 run_on_all test_path_is_missing deep/untracked-deep/untracked &&
851
852 test_all_match git clean -xf &&
853 test_all_match git status --porcelain=v2 &&
854 test_sparse_match ls &&
855 test_sparse_match ls folder1 &&
856 run_on_all test_path_is_missing folder1/bogus &&
857 run_on_all test_path_exists deep/untracked-deep/bogus &&
858
859 test_all_match git clean -xdf &&
860 test_all_match git status --porcelain=v2 &&
861 test_sparse_match ls &&
862 test_sparse_match ls folder1 &&
863 run_on_all test_path_is_missing deep/untracked-deep/bogus &&
864
865 test_sparse_match test_path_is_dir folder1
866 '
867
868 test_expect_success 'submodule handling' '
869 init_repos &&
870
871 test_sparse_match git sparse-checkout add modules &&
872 test_all_match mkdir modules &&
873 test_all_match touch modules/a &&
874 test_all_match git add modules &&
875 test_all_match git commit -m "add modules directory" &&
876
877 run_on_all git submodule add "$(pwd)/initial-repo" modules/sub &&
878 test_all_match git commit -m "add submodule" &&
879
880 # having a submodule prevents "modules" from collapse
881 test_sparse_match git sparse-checkout set deep/deeper1 &&
882 test-tool -C sparse-index read-cache --table >cache &&
883 grep "100644 blob .* modules/a" cache &&
884 grep "160000 commit $(git -C initial-repo rev-parse HEAD) modules/sub" cache
885 '
886
887 # When working with a sparse index, some commands will need to expand the
888 # index to operate properly. If those commands also write the index back
889 # to disk, they need to convert the index to sparse before writing.
890 # This test verifies that both of these events are logged in trace2 logs.
891 test_expect_success 'sparse-index is expanded and converted back' '
892 init_repos &&
893
894 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
895 git -C sparse-index reset -- folder1/a &&
896 test_region index convert_to_sparse trace2.txt &&
897 test_region index ensure_full_index trace2.txt
898 '
899
900 test_expect_success 'index.sparse disabled inline uses full index' '
901 init_repos &&
902
903 # When index.sparse is disabled inline with `git status`, the
904 # index is expanded at the beginning of the execution then never
905 # converted back to sparse. It is then written to disk as a full index.
906 rm -f trace2.txt &&
907 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
908 git -C sparse-index -c index.sparse=false status &&
909 ! test_region index convert_to_sparse trace2.txt &&
910 test_region index ensure_full_index trace2.txt &&
911
912 # Since index.sparse is set to true at a repo level, the index
913 # is converted from full to sparse when read, then never expanded
914 # over the course of `git status`. It is written to disk as a sparse
915 # index.
916 rm -f trace2.txt &&
917 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
918 git -C sparse-index status &&
919 test_region index convert_to_sparse trace2.txt &&
920 ! test_region index ensure_full_index trace2.txt &&
921
922 # Now that the index has been written to disk as sparse, it is not
923 # converted to sparse (or expanded to full) when read by `git status`.
924 rm -f trace2.txt &&
925 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
926 git -C sparse-index status &&
927 ! test_region index convert_to_sparse trace2.txt &&
928 ! test_region index ensure_full_index trace2.txt
929 '
930
931 ensure_not_expanded () {
932 rm -f trace2.txt &&
933 echo >>sparse-index/untracked.txt &&
934
935 if test "$1" = "!"
936 then
937 shift &&
938 test_must_fail env \
939 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
940 git -C sparse-index "$@" || return 1
941 else
942 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
943 git -C sparse-index "$@" || return 1
944 fi &&
945 test_region ! index ensure_full_index trace2.txt
946 }
947
948 test_expect_success 'sparse-index is not expanded' '
949 init_repos &&
950
951 ensure_not_expanded status &&
952 ensure_not_expanded commit --allow-empty -m empty &&
953 echo >>sparse-index/a &&
954 ensure_not_expanded commit -a -m a &&
955 echo >>sparse-index/a &&
956 ensure_not_expanded commit --include a -m a &&
957 echo >>sparse-index/deep/deeper1/a &&
958 ensure_not_expanded commit --include deep/deeper1/a -m deeper &&
959 ensure_not_expanded checkout rename-out-to-out &&
960 ensure_not_expanded checkout - &&
961 ensure_not_expanded switch rename-out-to-out &&
962 ensure_not_expanded switch - &&
963 ensure_not_expanded reset --hard &&
964 ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
965 ensure_not_expanded reset --hard &&
966 ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
967
968 echo >>sparse-index/README.md &&
969 ensure_not_expanded add -A &&
970 echo >>sparse-index/extra.txt &&
971 ensure_not_expanded add extra.txt &&
972 echo >>sparse-index/untracked.txt &&
973 ensure_not_expanded add . &&
974
975 for ref in update-deep update-folder1 update-folder2 update-deep
976 do
977 echo >>sparse-index/README.md &&
978 ensure_not_expanded reset --hard $ref || return 1
979 done &&
980
981 ensure_not_expanded reset --mixed base &&
982 ensure_not_expanded reset --hard update-deep &&
983 ensure_not_expanded reset --keep base &&
984 ensure_not_expanded reset --merge update-deep &&
985 ensure_not_expanded reset --hard &&
986
987 ensure_not_expanded reset base -- deep/a &&
988 ensure_not_expanded reset base -- nonexistent-file &&
989 ensure_not_expanded reset deepest -- deep &&
990
991 # Although folder1 is outside the sparse definition, it exists as a
992 # directory entry in the index, so the pathspec will not force the
993 # index to be expanded.
994 ensure_not_expanded reset deepest -- folder1 &&
995 ensure_not_expanded reset deepest -- folder1/ &&
996
997 # Wildcard identifies only in-cone files, no index expansion
998 ensure_not_expanded reset deepest -- deep/\* &&
999
1000 # Wildcard identifies only full sparse directories, no index expansion
1001 ensure_not_expanded reset deepest -- folder\* &&
1002
1003 ensure_not_expanded clean -fd &&
1004
1005 ensure_not_expanded checkout -f update-deep &&
1006 test_config -C sparse-index pull.twohead ort &&
1007 (
1008 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1009 for OPERATION in "merge -m merge" cherry-pick rebase
1010 do
1011 ensure_not_expanded merge -m merge update-folder1 &&
1012 ensure_not_expanded merge -m merge update-folder2 || return 1
1013 done
1014 )
1015 '
1016
1017 test_expect_success 'sparse-index is not expanded: merge conflict in cone' '
1018 init_repos &&
1019
1020 for side in right left
1021 do
1022 git -C sparse-index checkout -b expand-$side base &&
1023 echo $side >sparse-index/deep/a &&
1024 git -C sparse-index commit -a -m "$side" || return 1
1025 done &&
1026
1027 (
1028 sane_unset GIT_TEST_MERGE_ALGORITHM &&
1029 git -C sparse-index config pull.twohead ort &&
1030 ensure_not_expanded ! merge -m merged expand-right
1031 )
1032 '
1033
1034 test_expect_success 'sparse index is not expanded: diff' '
1035 init_repos &&
1036
1037 write_script edit-contents <<-\EOF &&
1038 echo text >>$1
1039 EOF
1040
1041 # Add file within cone
1042 test_sparse_match git sparse-checkout set deep &&
1043 run_on_all ../edit-contents deep/testfile &&
1044 test_all_match git add deep/testfile &&
1045 run_on_all ../edit-contents deep/testfile &&
1046
1047 test_all_match git diff &&
1048 test_all_match git diff --cached &&
1049 ensure_not_expanded diff &&
1050 ensure_not_expanded diff --cached &&
1051
1052 # Add file outside cone
1053 test_all_match git reset --hard &&
1054 run_on_all mkdir newdirectory &&
1055 run_on_all ../edit-contents newdirectory/testfile &&
1056 test_sparse_match git sparse-checkout set newdirectory &&
1057 test_all_match git add newdirectory/testfile &&
1058 run_on_all ../edit-contents newdirectory/testfile &&
1059 test_sparse_match git sparse-checkout set &&
1060
1061 test_all_match git diff &&
1062 test_all_match git diff --cached &&
1063 ensure_not_expanded diff &&
1064 ensure_not_expanded diff --cached &&
1065
1066 # Merge conflict outside cone
1067 # The sparse checkout will report a warning that is not in the
1068 # full checkout, so we use `run_on_all` instead of
1069 # `test_all_match`
1070 run_on_all git reset --hard &&
1071 test_all_match git checkout merge-left &&
1072 test_all_match test_must_fail git merge merge-right &&
1073
1074 test_all_match git diff &&
1075 test_all_match git diff --cached &&
1076 ensure_not_expanded diff &&
1077 ensure_not_expanded diff --cached
1078 '
1079
1080 test_expect_success 'sparse index is not expanded: blame' '
1081 init_repos &&
1082
1083 for file in a \
1084 deep/a \
1085 deep/deeper1/a \
1086 deep/deeper1/deepest/a
1087 do
1088 ensure_not_expanded blame $file
1089 done
1090 '
1091
1092 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
1093 # in this scenario, but it shouldn't.
1094 test_expect_success 'reset mixed and checkout orphan' '
1095 init_repos &&
1096
1097 test_all_match git checkout rename-out-to-in &&
1098
1099 # Sparse checkouts do not agree with full checkouts about
1100 # how to report a directory/file conflict during a reset.
1101 # This command would fail with test_all_match because the
1102 # full checkout reports "T folder1/0/1" while a sparse
1103 # checkout reports "D folder1/0/1". This matches because
1104 # the sparse checkouts skip "adding" the other side of
1105 # the conflict.
1106 test_sparse_match git reset --mixed HEAD~1 &&
1107 test_sparse_match test-tool read-cache --table --expand &&
1108 test_sparse_match git status --porcelain=v2 &&
1109
1110 # At this point, sparse-checkouts behave differently
1111 # from the full-checkout.
1112 test_sparse_match git checkout --orphan new-branch &&
1113 test_sparse_match test-tool read-cache --table --expand &&
1114 test_sparse_match git status --porcelain=v2
1115 '
1116
1117 test_expect_success 'add everything with deep new file' '
1118 init_repos &&
1119
1120 run_on_sparse git sparse-checkout set deep/deeper1/deepest &&
1121
1122 run_on_all touch deep/deeper1/x &&
1123 test_all_match git add . &&
1124 test_all_match git status --porcelain=v2
1125 '
1126
1127 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1128 # directory/file conflicts, even without sparse-checkout. Use this
1129 # test only as a documentation of the incorrect behavior, not a
1130 # measure of how it _should_ behave.
1131 test_expect_success 'checkout behaves oddly with df-conflict-1' '
1132 init_repos &&
1133
1134 test_sparse_match git sparse-checkout disable &&
1135
1136 write_script edit-content <<-\EOF &&
1137 echo content >>folder1/larger-content
1138 git add folder1
1139 EOF
1140
1141 run_on_all ../edit-content &&
1142 test_all_match git status --porcelain=v2 &&
1143
1144 git -C sparse-checkout sparse-checkout init --cone &&
1145 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1146
1147 test_all_match git status --porcelain=v2 &&
1148
1149 # This checkout command should fail, because we have a staged
1150 # change to folder1/larger-content, but the destination changes
1151 # folder1 to a file.
1152 git -C full-checkout checkout df-conflict-1 \
1153 1>full-checkout-out \
1154 2>full-checkout-err &&
1155 git -C sparse-checkout checkout df-conflict-1 \
1156 1>sparse-checkout-out \
1157 2>sparse-checkout-err &&
1158 git -C sparse-index checkout df-conflict-1 \
1159 1>sparse-index-out \
1160 2>sparse-index-err &&
1161
1162 # Instead, the checkout deletes the folder1 file and adds the
1163 # folder1/larger-content file, leaving all other paths that were
1164 # in folder1/ as deleted (without any warning).
1165 cat >expect <<-EOF &&
1166 D folder1
1167 A folder1/larger-content
1168 EOF
1169 test_cmp expect full-checkout-out &&
1170 test_cmp expect sparse-checkout-out &&
1171
1172 # The sparse-index reports no output
1173 test_must_be_empty sparse-index-out &&
1174
1175 # stderr: Switched to branch df-conflict-1
1176 test_cmp full-checkout-err sparse-checkout-err &&
1177 test_cmp full-checkout-err sparse-checkout-err
1178 '
1179
1180 # NEEDSWORK: 'git checkout' behaves incorrectly in the case of
1181 # directory/file conflicts, even without sparse-checkout. Use this
1182 # test only as a documentation of the incorrect behavior, not a
1183 # measure of how it _should_ behave.
1184 test_expect_success 'checkout behaves oddly with df-conflict-2' '
1185 init_repos &&
1186
1187 test_sparse_match git sparse-checkout disable &&
1188
1189 write_script edit-content <<-\EOF &&
1190 echo content >>folder2/larger-content
1191 git add folder2
1192 EOF
1193
1194 run_on_all ../edit-content &&
1195 test_all_match git status --porcelain=v2 &&
1196
1197 git -C sparse-checkout sparse-checkout init --cone &&
1198 git -C sparse-index sparse-checkout init --cone --sparse-index &&
1199
1200 test_all_match git status --porcelain=v2 &&
1201
1202 # This checkout command should fail, because we have a staged
1203 # change to folder1/larger-content, but the destination changes
1204 # folder1 to a file.
1205 git -C full-checkout checkout df-conflict-2 \
1206 1>full-checkout-out \
1207 2>full-checkout-err &&
1208 git -C sparse-checkout checkout df-conflict-2 \
1209 1>sparse-checkout-out \
1210 2>sparse-checkout-err &&
1211 git -C sparse-index checkout df-conflict-2 \
1212 1>sparse-index-out \
1213 2>sparse-index-err &&
1214
1215 # The full checkout deviates from the df-conflict-1 case here!
1216 # It drops the change to folder1/larger-content and leaves the
1217 # folder1 path as-is on disk. The sparse-index behaves the same.
1218 test_must_be_empty full-checkout-out &&
1219 test_must_be_empty sparse-index-out &&
1220
1221 # In the sparse-checkout case, the checkout deletes the folder1
1222 # file and adds the folder1/larger-content file, leaving all other
1223 # paths that were in folder1/ as deleted (without any warning).
1224 cat >expect <<-EOF &&
1225 D folder2
1226 A folder2/larger-content
1227 EOF
1228 test_cmp expect sparse-checkout-out &&
1229
1230 # Switched to branch df-conflict-1
1231 test_cmp full-checkout-err sparse-checkout-err &&
1232 test_cmp full-checkout-err sparse-index-err
1233 '
1234
1235 test_done