3 test_description
='sparse checkout builtin tests'
8 # Do not replace this with 'ls "$1"', as "ls" with BSD-lineage
9 # enables "-A" by default for root and ends up including ".git" and
10 # such in its output. (Note, though, that running the test suite as
11 # root is generally not recommended.)
12 (cd "$1" && printf '%s\n' *)
16 list_files
"$1" >actual
&&
18 printf "%s\n" $@
>expect
&&
19 test_cmp expect actual
22 test_expect_success
'setup' '
27 mkdir folder1 folder2 deep &&
28 mkdir deep/deeper1 deep/deeper2 &&
29 mkdir deep/deeper1/deepest &&
35 cp a deep/deeper1/deepest &&
37 git commit -m "initial commit"
41 test_expect_success
'git sparse-checkout list (empty)' '
42 git -C repo sparse-checkout list >list 2>err &&
43 test_must_be_empty list &&
44 test_i18ngrep "this worktree is not sparse (sparse-checkout file may not exist)" err
47 test_expect_success
'git sparse-checkout list (populated)' '
48 test_when_finished rm -f repo/.git/info/sparse-checkout &&
49 cat >repo/.git/info/sparse-checkout <<-\EOF &&
55 cp repo/.git/info/sparse-checkout expect &&
56 git -C repo sparse-checkout list >list &&
60 test_expect_success
'git sparse-checkout init' '
61 git -C repo sparse-checkout init &&
62 cat >expect <<-\EOF &&
66 test_cmp expect repo/.git/info/sparse-checkout &&
67 test_cmp_config -C repo true core.sparsecheckout &&
71 test_expect_success
'git sparse-checkout list after init' '
72 git -C repo sparse-checkout list >actual &&
73 cat >expect <<-\EOF &&
77 test_cmp expect actual
80 test_expect_success
'init with existing sparse-checkout' '
81 echo "*folder*" >> repo/.git/info/sparse-checkout &&
82 git -C repo sparse-checkout init &&
83 cat >expect <<-\EOF &&
88 test_cmp expect repo/.git/info/sparse-checkout &&
89 check_files repo a folder1 folder2
92 test_expect_success
'clone --sparse' '
93 git clone --sparse "file://$(pwd)/repo" clone &&
94 git -C clone sparse-checkout list >actual &&
95 cat >expect <<-\EOF &&
99 test_cmp expect actual &&
103 test_expect_success
'interaction with clone --no-checkout (unborn index)' '
104 git clone --no-checkout "file://$(pwd)/repo" clone_no_checkout &&
105 git -C clone_no_checkout sparse-checkout init --cone &&
106 git -C clone_no_checkout sparse-checkout set folder1 &&
108 git -C clone_no_checkout sparse-checkout list >actual &&
109 cat >expect <<-\EOF &&
112 test_cmp expect actual &&
114 # nothing checked out, expect "No such file or directory"
115 ! ls clone_no_checkout/* >actual &&
116 test_must_be_empty actual &&
117 test_path_is_missing clone_no_checkout/.git/index &&
119 # No branch is checked out until we manually switch to one
120 git -C clone_no_checkout switch master &&
121 test_path_is_file clone_no_checkout/.git/index &&
122 check_files clone_no_checkout a folder1
125 test_expect_success
'set enables config' '
126 git init empty-config &&
129 test_commit test file &&
130 test_path_is_missing .git/config.worktree &&
131 test_must_fail git sparse-checkout set nothing &&
132 test_path_is_file .git/config.worktree &&
133 test_must_fail git config core.sparseCheckout &&
134 git sparse-checkout set "/*" &&
135 test_cmp_config true core.sparseCheckout
139 test_expect_success
'set sparse-checkout using builtin' '
140 git -C repo sparse-checkout set "/*" "!/*/" "*folder*" &&
141 cat >expect <<-\EOF &&
146 git -C repo sparse-checkout list >actual &&
147 test_cmp expect actual &&
148 test_cmp expect repo/.git/info/sparse-checkout &&
149 check_files repo a folder1 folder2
152 test_expect_success
'set sparse-checkout using --stdin' '
153 cat >expect <<-\EOF &&
159 git -C repo sparse-checkout set --stdin <expect &&
160 git -C repo sparse-checkout list >actual &&
161 test_cmp expect actual &&
162 test_cmp expect repo/.git/info/sparse-checkout &&
163 check_files repo "a folder1 folder2"
166 test_expect_success
'add to sparse-checkout' '
167 cat repo/.git/info/sparse-checkout >expect &&
174 git -C repo sparse-checkout add --stdin <add &&
175 git -C repo sparse-checkout list >actual &&
176 test_cmp expect actual &&
177 test_cmp expect repo/.git/info/sparse-checkout &&
178 check_files repo "a folder1 folder2"
181 test_expect_success
'cone mode: match patterns' '
182 git -C repo config --worktree core.sparseCheckoutCone true &&
183 rm -rf repo/a repo/folder1 repo/folder2 &&
184 git -C repo read-tree -mu HEAD 2>err &&
185 test_i18ngrep ! "disabling cone patterns" err &&
186 git -C repo reset --hard &&
187 check_files repo a folder1 folder2
190 test_expect_success
'cone mode: warn on bad pattern' '
191 test_when_finished mv sparse-checkout repo/.git/info/ &&
192 cp repo/.git/info/sparse-checkout . &&
193 echo "!/deep/deeper/*" >>repo/.git/info/sparse-checkout &&
194 git -C repo read-tree -mu HEAD 2>err &&
195 test_i18ngrep "unrecognized negative pattern" err
198 test_expect_success
'sparse-checkout disable' '
199 test_when_finished rm -rf repo/.git/info/sparse-checkout &&
200 git -C repo sparse-checkout disable &&
201 test_path_is_file repo/.git/info/sparse-checkout &&
202 git -C repo config --list >config &&
203 test_must_fail git config core.sparseCheckout &&
204 check_files repo a deep folder1 folder2
207 test_expect_success
'cone mode: init and set' '
208 git -C repo sparse-checkout init --cone &&
209 git -C repo config --list >config &&
210 test_i18ngrep "core.sparsecheckoutcone=true" config &&
211 list_files repo >dir &&
213 test_cmp expect dir &&
214 git -C repo sparse-checkout set deep/deeper1/deepest/ 2>err &&
215 test_must_be_empty err &&
216 check_files repo a deep &&
217 check_files repo/deep a deeper1 &&
218 check_files repo/deep/deeper1 a deepest &&
219 cat >expect <<-\EOF &&
226 /deep/deeper1/deepest/
228 test_cmp expect repo/.git/info/sparse-checkout &&
229 git -C repo sparse-checkout set --stdin 2>err <<-\EOF &&
233 test_must_be_empty err &&
234 check_files repo a folder1 folder2
237 test_expect_success
'cone mode: list' '
238 cat >expect <<-\EOF &&
242 git -C repo sparse-checkout set --stdin <expect &&
243 git -C repo sparse-checkout list >actual 2>err &&
244 test_must_be_empty err &&
245 test_cmp expect actual
248 test_expect_success
'cone mode: set with nested folders' '
249 git -C repo sparse-checkout set deep deep/deeper1/deepest 2>err &&
250 test_line_count = 0 err &&
251 cat >expect <<-\EOF &&
256 test_cmp repo/.git/info/sparse-checkout expect
259 test_expect_success
'cone mode: add independent path' '
260 git -C repo sparse-checkout set deep/deeper1 &&
261 git -C repo sparse-checkout add folder1 &&
262 cat >expect <<-\EOF &&
270 test_cmp expect repo/.git/info/sparse-checkout &&
271 check_files repo a deep folder1
274 test_expect_success
'cone mode: add sibling path' '
275 git -C repo sparse-checkout set deep/deeper1 &&
276 git -C repo sparse-checkout add deep/deeper2 &&
277 cat >expect <<-\EOF &&
285 test_cmp expect repo/.git/info/sparse-checkout &&
286 check_files repo a deep
289 test_expect_success
'cone mode: add parent path' '
290 git -C repo sparse-checkout set deep/deeper1 folder1 &&
291 git -C repo sparse-checkout add deep &&
292 cat >expect <<-\EOF &&
298 test_cmp expect repo/.git/info/sparse-checkout &&
299 check_files repo a deep folder1
302 test_expect_success
'not-up-to-date does not block rest of sparsification' '
303 test_when_finished git -C repo sparse-checkout disable &&
304 test_when_finished git -C repo reset --hard &&
305 git -C repo sparse-checkout set deep &&
307 echo update >repo/deep/deeper2/a &&
308 cp repo/.git/info/sparse-checkout expect &&
309 test_write_lines "!/deep/*/" "/deep/deeper1/" >>expect &&
311 git -C repo sparse-checkout set deep/deeper1 2>err &&
313 test_i18ngrep "The following paths are not up to date" err &&
314 test_cmp expect repo/.git/info/sparse-checkout &&
315 check_files repo/deep a deeper1 deeper2 &&
316 check_files repo/deep/deeper1 a deepest &&
317 check_files repo/deep/deeper1/deepest a &&
318 check_files repo/deep/deeper2 a
321 test_expect_success
'revert to old sparse-checkout on empty update' '
322 git init empty-test &&
326 git commit -m "test" &&
327 test_must_fail git sparse-checkout set nothing 2>err &&
328 test_i18ngrep "Sparse checkout leaves no entry on working directory" err &&
329 test_i18ngrep ! ".git/index.lock" err &&
330 git sparse-checkout set file
334 test_expect_success
'fail when lock is taken' '
335 test_when_finished rm -rf repo/.git/info/sparse-checkout.lock &&
336 touch repo/.git/info/sparse-checkout.lock &&
337 test_must_fail git -C repo sparse-checkout set deep 2>err &&
338 test_i18ngrep "Unable to create .*\.lock" err
341 test_expect_success
'.gitignore should not warn about cone mode' '
342 git -C repo config --worktree core.sparseCheckoutCone true &&
343 echo "**/bin/*" >repo/.gitignore &&
344 git -C repo reset --hard 2>err &&
345 test_i18ngrep ! "disabling cone patterns" err
348 test_expect_success
'sparse-checkout (init|set|disable) warns with dirty status' '
349 git clone repo dirty &&
350 echo dirty >dirty/folder1/a &&
352 git -C dirty sparse-checkout init 2>err &&
353 test_i18ngrep "warning.*The following paths are not up to date" err &&
355 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
356 test_i18ngrep "warning.*The following paths are not up to date" err &&
357 test_path_is_file dirty/folder1/a &&
359 git -C dirty sparse-checkout disable 2>err &&
360 test_must_be_empty err &&
362 git -C dirty reset --hard &&
363 git -C dirty sparse-checkout init &&
364 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
365 test_path_is_missing dirty/folder1/a &&
366 git -C dirty sparse-checkout disable &&
367 test_path_is_file dirty/folder1/a
370 test_expect_success
'sparse-checkout (init|set|disable) warns with unmerged status' '
371 git clone repo unmerged &&
374 0 0000000000000000000000000000000000000000 folder1/a
375 100644 $(git -C unmerged rev-parse HEAD:folder1/a) 1 folder1/a
377 git -C unmerged update-index --index-info <input &&
379 git -C unmerged sparse-checkout init 2>err &&
380 test_i18ngrep "warning.*The following paths are unmerged" err &&
382 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
383 test_i18ngrep "warning.*The following paths are unmerged" err &&
384 test_path_is_file dirty/folder1/a &&
386 git -C unmerged sparse-checkout disable 2>err &&
387 test_i18ngrep "warning.*The following paths are unmerged" err &&
389 git -C unmerged reset --hard &&
390 git -C unmerged sparse-checkout init &&
391 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* &&
392 git -C unmerged sparse-checkout disable
395 test_expect_success
'sparse-checkout reapply' '
396 git clone repo tweak &&
398 echo dirty >tweak/deep/deeper2/a &&
401 0 0000000000000000000000000000000000000000 folder1/a
402 100644 $(git -C tweak rev-parse HEAD:folder1/a) 1 folder1/a
404 git -C tweak update-index --index-info <input &&
406 git -C tweak sparse-checkout init --cone 2>err &&
407 test_i18ngrep "warning.*The following paths are not up to date" err &&
408 test_i18ngrep "warning.*The following paths are unmerged" err &&
410 git -C tweak sparse-checkout set folder2 deep/deeper1 2>err &&
411 test_i18ngrep "warning.*The following paths are not up to date" err &&
412 test_i18ngrep "warning.*The following paths are unmerged" err &&
414 git -C tweak sparse-checkout reapply 2>err &&
415 test_i18ngrep "warning.*The following paths are not up to date" err &&
416 test_path_is_file tweak/deep/deeper2/a &&
417 test_i18ngrep "warning.*The following paths are unmerged" err &&
418 test_path_is_file tweak/folder1/a &&
420 git -C tweak checkout HEAD deep/deeper2/a &&
421 git -C tweak sparse-checkout reapply 2>err &&
422 test_i18ngrep ! "warning.*The following paths are not up to date" err &&
423 test_path_is_missing tweak/deep/deeper2/a &&
424 test_i18ngrep "warning.*The following paths are unmerged" err &&
425 test_path_is_file tweak/folder1/a &&
427 git -C tweak add folder1/a &&
428 git -C tweak sparse-checkout reapply 2>err &&
429 test_must_be_empty err &&
430 test_path_is_missing tweak/deep/deeper2/a &&
431 test_path_is_missing tweak/folder1/a &&
433 git -C tweak sparse-checkout disable
436 test_expect_success
'cone mode: set with core.ignoreCase=true' '
437 rm repo/.git/info/sparse-checkout &&
438 git -C repo sparse-checkout init --cone &&
439 git -C repo -c core.ignoreCase=true sparse-checkout set folder1 &&
440 cat >expect <<-\EOF &&
445 test_cmp expect repo/.git/info/sparse-checkout &&
446 check_files repo a folder1
449 test_expect_success
'interaction with submodules' '
450 git clone repo super &&
454 git submodule add ../repo modules/child &&
456 git commit -m "add submodule" &&
457 git sparse-checkout init --cone &&
458 git sparse-checkout set folder1
460 check_files super a folder1 modules &&
461 check_files super/modules/child a deep folder1 folder2
464 test_expect_success
'different sparse-checkouts with worktrees' '
465 git -C repo worktree add --detach ../worktree &&
466 check_files worktree "a deep folder1 folder2" &&
467 git -C worktree sparse-checkout init --cone &&
468 git -C repo sparse-checkout set folder1 &&
469 git -C worktree sparse-checkout set deep/deeper1 &&
470 check_files repo a folder1 &&
471 check_files worktree a deep
474 test_expect_success
'set using filename keeps file on-disk' '
475 git -C repo sparse-checkout set a deep &&
476 cat >expect <<-\EOF &&
482 test_cmp expect repo/.git/info/sparse-checkout &&
483 check_files repo a deep
486 check_read_tree_errors
() {
490 git
-C $REPO -c core.sparseCheckoutCone
=false read-tree
-mu HEAD
2>err
&&
491 test_must_be_empty err
&&
492 check_files
$REPO "$FILES" &&
493 git
-C $REPO read-tree
-mu HEAD
2>err
&&
496 test_must_be_empty err
498 test_i18ngrep
"$ERRORS" err
500 check_files
$REPO $FILES
503 test_expect_success
'pattern-checks: /A/**' '
504 cat >repo/.git/info/sparse-checkout <<-\EOF &&
509 check_read_tree_errors repo "a folder1" "disabling cone pattern matching"
512 test_expect_success
'pattern-checks: /A/**/B/' '
513 cat >repo/.git/info/sparse-checkout <<-\EOF &&
518 check_read_tree_errors repo "a deep" "disabling cone pattern matching" &&
519 check_files repo/deep "deeper1" &&
520 check_files repo/deep/deeper1 "deepest"
523 test_expect_success
'pattern-checks: too short' '
524 cat >repo/.git/info/sparse-checkout <<-\EOF &&
529 check_read_tree_errors repo "a" "disabling cone pattern matching"
531 test_expect_success
'pattern-checks: not too short' '
532 cat >repo/.git/info/sparse-checkout <<-\EOF &&
537 git -C repo read-tree -mu HEAD 2>err &&
538 test_must_be_empty err &&
542 test_expect_success
'pattern-checks: trailing "*"' '
543 cat >repo/.git/info/sparse-checkout <<-\EOF &&
548 check_read_tree_errors repo "a" "disabling cone pattern matching"
551 test_expect_success
'pattern-checks: starting "*"' '
552 cat >repo/.git/info/sparse-checkout <<-\EOF &&
557 check_read_tree_errors repo "a deep" "disabling cone pattern matching"
560 test_expect_success
'pattern-checks: contained glob characters' '
561 for c in "[a]" "\\" "?" "*"
563 cat >repo/.git/info/sparse-checkout <<-EOF &&
568 check_read_tree_errors repo "a" "disabling cone pattern matching"
572 test_expect_success BSLASHPSPEC
'pattern-checks: escaped characters' '
573 git clone repo escaped &&
574 TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
575 NEWTREE=$(git -C escaped mktree <<-EOF
576 $(git -C escaped ls-tree HEAD)
577 040000 tree $TREEOID zbad\\dir
578 040000 tree $TREEOID zdoes*exist
579 040000 tree $TREEOID zglob[!a]?
582 COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
583 git -C escaped reset --hard $COMMIT &&
584 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
585 git -C escaped sparse-checkout init --cone &&
586 git -C escaped sparse-checkout set zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" &&
587 cat >expect <<-\EOF &&
597 test_cmp expect escaped/.git/info/sparse-checkout &&
598 check_read_tree_errors escaped "a zbad\\dir zdoes*exist zglob[!a]?" &&
599 git -C escaped ls-tree -d --name-only HEAD >list-expect &&
600 git -C escaped sparse-checkout set --stdin <list-expect &&
601 cat >expect <<-\EOF &&
611 test_cmp expect escaped/.git/info/sparse-checkout &&
612 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
613 git -C escaped sparse-checkout list >list-actual &&
614 test_cmp list-expect list-actual
617 test_expect_success MINGW
'cone mode replaces backslashes with slashes' '
618 git -C repo sparse-checkout set deep\\deeper1 &&
619 cat >expect <<-\EOF &&
626 test_cmp expect repo/.git/info/sparse-checkout &&
627 check_files repo a deep &&
628 check_files repo/deep a deeper1