sparse-index: update command for expand/collapse test
[git.git] / t / t1091-sparse-checkout-builtin.sh
blob71236981e6484f3d2fc0dde9ec7e0ea52f7a6d73
1 #!/bin/sh
3 test_description='sparse checkout builtin tests'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 . ./test-lib.sh
10 list_files() {
11 # Do not replace this with 'ls "$1"', as "ls" with BSD-lineage
12 # enables "-A" by default for root and ends up including ".git" and
13 # such in its output. (Note, though, that running the test suite as
14 # root is generally not recommended.)
15 (cd "$1" && printf '%s\n' *)
18 check_files() {
19 list_files "$1" >actual &&
20 shift &&
21 printf "%s\n" $@ >expect &&
22 test_cmp expect actual
25 test_expect_success 'setup' '
26 git init repo &&
28 cd repo &&
29 echo "initial" >a &&
30 mkdir folder1 folder2 deep &&
31 mkdir deep/deeper1 deep/deeper2 &&
32 mkdir deep/deeper1/deepest &&
33 cp a folder1 &&
34 cp a folder2 &&
35 cp a deep &&
36 cp a deep/deeper1 &&
37 cp a deep/deeper2 &&
38 cp a deep/deeper1/deepest &&
39 git add . &&
40 git commit -m "initial commit"
44 test_expect_success 'git sparse-checkout list (empty)' '
45 git -C repo sparse-checkout list >list 2>err &&
46 test_must_be_empty list &&
47 test_i18ngrep "this worktree is not sparse (sparse-checkout file may not exist)" err
50 test_expect_success 'git sparse-checkout list (populated)' '
51 test_when_finished rm -f repo/.git/info/sparse-checkout &&
52 cat >repo/.git/info/sparse-checkout <<-\EOF &&
53 /folder1/*
54 /deep/
55 **/a
56 !*bin*
57 EOF
58 cp repo/.git/info/sparse-checkout expect &&
59 git -C repo sparse-checkout list >list &&
60 test_cmp expect list
63 test_expect_success 'git sparse-checkout init' '
64 git -C repo sparse-checkout init &&
65 cat >expect <<-\EOF &&
67 !/*/
68 EOF
69 test_cmp expect repo/.git/info/sparse-checkout &&
70 test_cmp_config -C repo true core.sparsecheckout &&
71 check_files repo a
74 test_expect_success 'git sparse-checkout list after init' '
75 git -C repo sparse-checkout list >actual &&
76 cat >expect <<-\EOF &&
78 !/*/
79 EOF
80 test_cmp expect actual
83 test_expect_success 'init with existing sparse-checkout' '
84 echo "*folder*" >> repo/.git/info/sparse-checkout &&
85 git -C repo sparse-checkout init &&
86 cat >expect <<-\EOF &&
88 !/*/
89 *folder*
90 EOF
91 test_cmp expect repo/.git/info/sparse-checkout &&
92 check_files repo a folder1 folder2
95 test_expect_success 'clone --sparse' '
96 git clone --sparse "file://$(pwd)/repo" clone &&
97 git -C clone sparse-checkout list >actual &&
98 cat >expect <<-\EOF &&
100 !/*/
102 test_cmp expect actual &&
103 check_files clone a
106 test_expect_success 'interaction with clone --no-checkout (unborn index)' '
107 git clone --no-checkout "file://$(pwd)/repo" clone_no_checkout &&
108 git -C clone_no_checkout sparse-checkout init --cone &&
109 git -C clone_no_checkout sparse-checkout set folder1 &&
111 git -C clone_no_checkout sparse-checkout list >actual &&
112 cat >expect <<-\EOF &&
113 folder1
115 test_cmp expect actual &&
117 # nothing checked out, expect "No such file or directory"
118 ! ls clone_no_checkout/* >actual &&
119 test_must_be_empty actual &&
120 test_path_is_missing clone_no_checkout/.git/index &&
122 # No branch is checked out until we manually switch to one
123 git -C clone_no_checkout switch main &&
124 test_path_is_file clone_no_checkout/.git/index &&
125 check_files clone_no_checkout a folder1
128 test_expect_success 'set enables config' '
129 git init empty-config &&
131 cd empty-config &&
132 test_commit test file &&
133 test_path_is_missing .git/config.worktree &&
134 git sparse-checkout set nothing &&
135 test_path_is_file .git/config.worktree &&
136 test_cmp_config true core.sparseCheckout
140 test_expect_success 'set sparse-checkout using builtin' '
141 git -C repo sparse-checkout set "/*" "!/*/" "*folder*" &&
142 cat >expect <<-\EOF &&
144 !/*/
145 *folder*
147 git -C repo sparse-checkout list >actual &&
148 test_cmp expect actual &&
149 test_cmp expect repo/.git/info/sparse-checkout &&
150 check_files repo a folder1 folder2
153 test_expect_success 'set sparse-checkout using --stdin' '
154 cat >expect <<-\EOF &&
156 !/*/
157 /folder1/
158 /folder2/
160 git -C repo sparse-checkout set --stdin <expect &&
161 git -C repo sparse-checkout list >actual &&
162 test_cmp expect actual &&
163 test_cmp expect repo/.git/info/sparse-checkout &&
164 check_files repo "a folder1 folder2"
167 test_expect_success 'add to sparse-checkout' '
168 cat repo/.git/info/sparse-checkout >expect &&
169 cat >add <<-\EOF &&
170 pattern1
171 /folder1/
172 pattern2
174 cat add >>expect &&
175 git -C repo sparse-checkout add --stdin <add &&
176 git -C repo sparse-checkout list >actual &&
177 test_cmp expect actual &&
178 test_cmp expect repo/.git/info/sparse-checkout &&
179 check_files repo "a folder1 folder2"
182 test_expect_success 'cone mode: match patterns' '
183 git -C repo config --worktree core.sparseCheckoutCone true &&
184 rm -rf repo/a repo/folder1 repo/folder2 &&
185 git -C repo read-tree -mu HEAD 2>err &&
186 test_i18ngrep ! "disabling cone patterns" err &&
187 git -C repo reset --hard &&
188 check_files repo a folder1 folder2
191 test_expect_success 'cone mode: warn on bad pattern' '
192 test_when_finished mv sparse-checkout repo/.git/info/ &&
193 cp repo/.git/info/sparse-checkout . &&
194 echo "!/deep/deeper/*" >>repo/.git/info/sparse-checkout &&
195 git -C repo read-tree -mu HEAD 2>err &&
196 test_i18ngrep "unrecognized negative pattern" err
199 test_expect_success 'sparse-checkout disable' '
200 test_when_finished rm -rf repo/.git/info/sparse-checkout &&
201 git -C repo sparse-checkout disable &&
202 test_path_is_file repo/.git/info/sparse-checkout &&
203 git -C repo config --list >config &&
204 test_must_fail git config core.sparseCheckout &&
205 check_files repo a deep folder1 folder2
208 test_expect_success 'sparse-index enabled and disabled' '
209 git -C repo sparse-checkout init --cone --sparse-index &&
210 test_cmp_config -C repo true index.sparse &&
211 test-tool -C repo read-cache --table >cache &&
212 grep " tree " cache &&
214 git -C repo sparse-checkout disable &&
215 test-tool -C repo read-cache --table >cache &&
216 ! grep " tree " cache &&
217 git -C repo config --list >config &&
218 ! grep index.sparse config
221 test_expect_success 'cone mode: init and set' '
222 git -C repo sparse-checkout init --cone &&
223 git -C repo config --list >config &&
224 test_i18ngrep "core.sparsecheckoutcone=true" config &&
225 list_files repo >dir &&
226 echo a >expect &&
227 test_cmp expect dir &&
228 git -C repo sparse-checkout set deep/deeper1/deepest/ 2>err &&
229 test_must_be_empty err &&
230 check_files repo a deep &&
231 check_files repo/deep a deeper1 &&
232 check_files repo/deep/deeper1 a deepest &&
233 cat >expect <<-\EOF &&
235 !/*/
236 /deep/
237 !/deep/*/
238 /deep/deeper1/
239 !/deep/deeper1/*/
240 /deep/deeper1/deepest/
242 test_cmp expect repo/.git/info/sparse-checkout &&
243 git -C repo sparse-checkout set --stdin 2>err <<-\EOF &&
244 folder1
245 folder2
247 test_must_be_empty err &&
248 check_files repo a folder1 folder2
251 test_expect_success 'cone mode: list' '
252 cat >expect <<-\EOF &&
253 folder1
254 folder2
256 git -C repo sparse-checkout set --stdin <expect &&
257 git -C repo sparse-checkout list >actual 2>err &&
258 test_must_be_empty err &&
259 test_cmp expect actual
262 test_expect_success 'cone mode: set with nested folders' '
263 git -C repo sparse-checkout set deep deep/deeper1/deepest 2>err &&
264 test_line_count = 0 err &&
265 cat >expect <<-\EOF &&
267 !/*/
268 /deep/
270 test_cmp repo/.git/info/sparse-checkout expect
273 test_expect_success 'cone mode: add independent path' '
274 git -C repo sparse-checkout set deep/deeper1 &&
275 git -C repo sparse-checkout add folder1 &&
276 cat >expect <<-\EOF &&
278 !/*/
279 /deep/
280 !/deep/*/
281 /deep/deeper1/
282 /folder1/
284 test_cmp expect repo/.git/info/sparse-checkout &&
285 check_files repo a deep folder1
288 test_expect_success 'cone mode: add sibling path' '
289 git -C repo sparse-checkout set deep/deeper1 &&
290 git -C repo sparse-checkout add deep/deeper2 &&
291 cat >expect <<-\EOF &&
293 !/*/
294 /deep/
295 !/deep/*/
296 /deep/deeper1/
297 /deep/deeper2/
299 test_cmp expect repo/.git/info/sparse-checkout &&
300 check_files repo a deep
303 test_expect_success 'cone mode: add parent path' '
304 git -C repo sparse-checkout set deep/deeper1 folder1 &&
305 git -C repo sparse-checkout add deep &&
306 cat >expect <<-\EOF &&
308 !/*/
309 /deep/
310 /folder1/
312 test_cmp expect repo/.git/info/sparse-checkout &&
313 check_files repo a deep folder1
316 test_expect_success 'not-up-to-date does not block rest of sparsification' '
317 test_when_finished git -C repo sparse-checkout disable &&
318 test_when_finished git -C repo reset --hard &&
319 git -C repo sparse-checkout set deep &&
321 echo update >repo/deep/deeper2/a &&
322 cp repo/.git/info/sparse-checkout expect &&
323 test_write_lines "!/deep/*/" "/deep/deeper1/" >>expect &&
325 git -C repo sparse-checkout set deep/deeper1 2>err &&
327 test_i18ngrep "The following paths are not up to date" err &&
328 test_cmp expect repo/.git/info/sparse-checkout &&
329 check_files repo/deep a deeper1 deeper2 &&
330 check_files repo/deep/deeper1 a deepest &&
331 check_files repo/deep/deeper1/deepest a &&
332 check_files repo/deep/deeper2 a
335 test_expect_success 'revert to old sparse-checkout on empty update' '
336 git init empty-test &&
338 echo >file &&
339 git add file &&
340 git commit -m "test" &&
341 git sparse-checkout set nothing 2>err &&
342 test_i18ngrep ! "Sparse checkout leaves no entry on working directory" err &&
343 test_i18ngrep ! ".git/index.lock" err &&
344 git sparse-checkout set file
348 test_expect_success 'fail when lock is taken' '
349 test_when_finished rm -rf repo/.git/info/sparse-checkout.lock &&
350 touch repo/.git/info/sparse-checkout.lock &&
351 test_must_fail git -C repo sparse-checkout set deep 2>err &&
352 test_i18ngrep "Unable to create .*\.lock" err
355 test_expect_success '.gitignore should not warn about cone mode' '
356 git -C repo config --worktree core.sparseCheckoutCone true &&
357 echo "**/bin/*" >repo/.gitignore &&
358 git -C repo reset --hard 2>err &&
359 test_i18ngrep ! "disabling cone patterns" err
362 test_expect_success 'sparse-checkout (init|set|disable) warns with dirty status' '
363 git clone repo dirty &&
364 echo dirty >dirty/folder1/a &&
366 git -C dirty sparse-checkout init 2>err &&
367 test_i18ngrep "warning.*The following paths are not up to date" err &&
369 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
370 test_i18ngrep "warning.*The following paths are not up to date" err &&
371 test_path_is_file dirty/folder1/a &&
373 git -C dirty sparse-checkout disable 2>err &&
374 test_must_be_empty err &&
376 git -C dirty reset --hard &&
377 git -C dirty sparse-checkout init &&
378 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
379 test_path_is_missing dirty/folder1/a &&
380 git -C dirty sparse-checkout disable &&
381 test_path_is_file dirty/folder1/a
384 test_expect_success 'sparse-checkout (init|set|disable) warns with unmerged status' '
385 git clone repo unmerged &&
387 cat >input <<-EOF &&
388 0 $ZERO_OID folder1/a
389 100644 $(git -C unmerged rev-parse HEAD:folder1/a) 1 folder1/a
391 git -C unmerged update-index --index-info <input &&
393 git -C unmerged sparse-checkout init 2>err &&
394 test_i18ngrep "warning.*The following paths are unmerged" err &&
396 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
397 test_i18ngrep "warning.*The following paths are unmerged" err &&
398 test_path_is_file dirty/folder1/a &&
400 git -C unmerged sparse-checkout disable 2>err &&
401 test_i18ngrep "warning.*The following paths are unmerged" err &&
403 git -C unmerged reset --hard &&
404 git -C unmerged sparse-checkout init &&
405 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* &&
406 git -C unmerged sparse-checkout disable
409 test_expect_success 'sparse-checkout reapply' '
410 git clone repo tweak &&
412 echo dirty >tweak/deep/deeper2/a &&
414 cat >input <<-EOF &&
415 0 $ZERO_OID folder1/a
416 100644 $(git -C tweak rev-parse HEAD:folder1/a) 1 folder1/a
418 git -C tweak update-index --index-info <input &&
420 git -C tweak sparse-checkout init --cone 2>err &&
421 test_i18ngrep "warning.*The following paths are not up to date" err &&
422 test_i18ngrep "warning.*The following paths are unmerged" err &&
424 git -C tweak sparse-checkout set folder2 deep/deeper1 2>err &&
425 test_i18ngrep "warning.*The following paths are not up to date" err &&
426 test_i18ngrep "warning.*The following paths are unmerged" err &&
428 git -C tweak sparse-checkout reapply 2>err &&
429 test_i18ngrep "warning.*The following paths are not up to date" err &&
430 test_path_is_file tweak/deep/deeper2/a &&
431 test_i18ngrep "warning.*The following paths are unmerged" err &&
432 test_path_is_file tweak/folder1/a &&
434 git -C tweak checkout HEAD deep/deeper2/a &&
435 git -C tweak sparse-checkout reapply 2>err &&
436 test_i18ngrep ! "warning.*The following paths are not up to date" err &&
437 test_path_is_missing tweak/deep/deeper2/a &&
438 test_i18ngrep "warning.*The following paths are unmerged" err &&
439 test_path_is_file tweak/folder1/a &&
441 git -C tweak add folder1/a &&
442 git -C tweak sparse-checkout reapply 2>err &&
443 test_must_be_empty err &&
444 test_path_is_missing tweak/deep/deeper2/a &&
445 test_path_is_missing tweak/folder1/a &&
447 git -C tweak sparse-checkout disable
450 test_expect_success 'cone mode: set with core.ignoreCase=true' '
451 rm repo/.git/info/sparse-checkout &&
452 git -C repo sparse-checkout init --cone &&
453 git -C repo -c core.ignoreCase=true sparse-checkout set folder1 &&
454 cat >expect <<-\EOF &&
456 !/*/
457 /folder1/
459 test_cmp expect repo/.git/info/sparse-checkout &&
460 check_files repo a folder1
463 test_expect_success 'interaction with submodules' '
464 git clone repo super &&
466 cd super &&
467 mkdir modules &&
468 git submodule add ../repo modules/child &&
469 git add . &&
470 git commit -m "add submodule" &&
471 git sparse-checkout init --cone &&
472 git sparse-checkout set folder1
473 ) &&
474 check_files super a folder1 modules &&
475 check_files super/modules/child a deep folder1 folder2
478 test_expect_success 'different sparse-checkouts with worktrees' '
479 git -C repo worktree add --detach ../worktree &&
480 check_files worktree "a deep folder1 folder2" &&
481 git -C worktree sparse-checkout init --cone &&
482 git -C repo sparse-checkout set folder1 &&
483 git -C worktree sparse-checkout set deep/deeper1 &&
484 check_files repo a folder1 &&
485 check_files worktree a deep
488 test_expect_success 'set using filename keeps file on-disk' '
489 git -C repo sparse-checkout set a deep &&
490 cat >expect <<-\EOF &&
492 !/*/
494 /deep/
496 test_cmp expect repo/.git/info/sparse-checkout &&
497 check_files repo a deep
500 check_read_tree_errors () {
501 REPO=$1
502 FILES=$2
503 ERRORS=$3
504 git -C $REPO -c core.sparseCheckoutCone=false read-tree -mu HEAD 2>err &&
505 test_must_be_empty err &&
506 check_files $REPO "$FILES" &&
507 git -C $REPO read-tree -mu HEAD 2>err &&
508 if test -z "$ERRORS"
509 then
510 test_must_be_empty err
511 else
512 test_i18ngrep "$ERRORS" err
513 fi &&
514 check_files $REPO $FILES
517 test_expect_success 'pattern-checks: /A/**' '
518 cat >repo/.git/info/sparse-checkout <<-\EOF &&
520 !/*/
521 /folder1/**
523 check_read_tree_errors repo "a folder1" "disabling cone pattern matching"
526 test_expect_success 'pattern-checks: /A/**/B/' '
527 cat >repo/.git/info/sparse-checkout <<-\EOF &&
529 !/*/
530 /deep/**/deepest
532 check_read_tree_errors repo "a deep" "disabling cone pattern matching" &&
533 check_files repo/deep "deeper1" &&
534 check_files repo/deep/deeper1 "deepest"
537 test_expect_success 'pattern-checks: too short' '
538 cat >repo/.git/info/sparse-checkout <<-\EOF &&
540 !/*/
543 check_read_tree_errors repo "a" "disabling cone pattern matching"
545 test_expect_success 'pattern-checks: not too short' '
546 cat >repo/.git/info/sparse-checkout <<-\EOF &&
548 !/*/
551 git -C repo read-tree -mu HEAD 2>err &&
552 test_must_be_empty err &&
553 check_files repo a
556 test_expect_success 'pattern-checks: trailing "*"' '
557 cat >repo/.git/info/sparse-checkout <<-\EOF &&
559 !/*/
562 check_read_tree_errors repo "a" "disabling cone pattern matching"
565 test_expect_success 'pattern-checks: starting "*"' '
566 cat >repo/.git/info/sparse-checkout <<-\EOF &&
568 !/*/
569 *eep/
571 check_read_tree_errors repo "a deep" "disabling cone pattern matching"
574 test_expect_success 'pattern-checks: contained glob characters' '
575 for c in "[a]" "\\" "?" "*"
577 cat >repo/.git/info/sparse-checkout <<-EOF &&
579 !/*/
580 something$c-else/
582 check_read_tree_errors repo "a" "disabling cone pattern matching"
583 done
586 test_expect_success BSLASHPSPEC 'pattern-checks: escaped characters' '
587 git clone repo escaped &&
588 TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
589 NEWTREE=$(git -C escaped mktree <<-EOF
590 $(git -C escaped ls-tree HEAD)
591 040000 tree $TREEOID zbad\\dir
592 040000 tree $TREEOID zdoes*exist
593 040000 tree $TREEOID zglob[!a]?
595 ) &&
596 COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
597 git -C escaped reset --hard $COMMIT &&
598 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
599 git -C escaped sparse-checkout init --cone &&
600 git -C escaped sparse-checkout set zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" &&
601 cat >expect <<-\EOF &&
603 !/*/
604 /zbad\\dir/
605 !/zbad\\dir/*/
606 /zbad\\dir/bogus/
607 /zdoes\*exist/
608 /zdoes\*not\*exist/
609 /zglob\[!a]\?/
611 test_cmp expect escaped/.git/info/sparse-checkout &&
612 check_read_tree_errors escaped "a zbad\\dir zdoes*exist zglob[!a]?" &&
613 git -C escaped ls-tree -d --name-only HEAD >list-expect &&
614 git -C escaped sparse-checkout set --stdin <list-expect &&
615 cat >expect <<-\EOF &&
617 !/*/
618 /deep/
619 /folder1/
620 /folder2/
621 /zbad\\dir/
622 /zdoes\*exist/
623 /zglob\[!a]\?/
625 test_cmp expect escaped/.git/info/sparse-checkout &&
626 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
627 git -C escaped sparse-checkout list >list-actual &&
628 test_cmp list-expect list-actual
631 test_expect_success MINGW 'cone mode replaces backslashes with slashes' '
632 git -C repo sparse-checkout set deep\\deeper1 &&
633 cat >expect <<-\EOF &&
635 !/*/
636 /deep/
637 !/deep/*/
638 /deep/deeper1/
640 test_cmp expect repo/.git/info/sparse-checkout &&
641 check_files repo a deep &&
642 check_files repo/deep a deeper1
645 test_expect_success 'cone mode clears ignored subdirectories' '
646 rm repo/.git/info/sparse-checkout &&
648 git -C repo sparse-checkout init --cone &&
649 git -C repo sparse-checkout set deep/deeper1 &&
651 cat >repo/.gitignore <<-\EOF &&
652 obj/
656 git -C repo add .gitignore &&
657 git -C repo commit -m ".gitignore" &&
659 mkdir -p repo/obj repo/folder1/obj repo/deep/deeper2/obj &&
660 for file in folder1/obj/a obj/a folder1/file.o folder1.o \
661 deep/deeper2/obj/a deep/deeper2/file.o file.o
663 echo ignored >repo/$file || return 1
664 done &&
666 git -C repo status --porcelain=v2 >out &&
667 test_must_be_empty out &&
669 git -C repo sparse-checkout reapply &&
670 test_path_is_missing repo/folder1 &&
671 test_path_is_missing repo/deep/deeper2 &&
672 test_path_is_dir repo/obj &&
673 test_path_is_file repo/file.o &&
675 git -C repo status --porcelain=v2 >out &&
676 test_must_be_empty out &&
678 git -C repo sparse-checkout set deep/deeper2 &&
679 test_path_is_missing repo/deep/deeper1 &&
680 test_path_is_dir repo/deep/deeper2 &&
681 test_path_is_dir repo/obj &&
682 test_path_is_file repo/file.o &&
684 >repo/deep/deeper2/ignored.o &&
685 >repo/deep/deeper2/untracked &&
687 # When an untracked file is in the way, all untracked files
688 # (even ignored files) are preserved.
689 git -C repo sparse-checkout set folder1 2>err &&
690 grep "contains untracked files" err &&
691 test_path_is_file repo/deep/deeper2/ignored.o &&
692 test_path_is_file repo/deep/deeper2/untracked &&
694 # The rest of the cone matches expectation
695 test_path_is_missing repo/deep/deeper1 &&
696 test_path_is_dir repo/obj &&
697 test_path_is_file repo/file.o &&
699 git -C repo status --porcelain=v2 >out &&
700 echo "? deep/deeper2/untracked" >expect &&
701 test_cmp expect out
704 test_done