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 git sparse-checkout set nothing &&
132 test_path_is_file .git/config.worktree &&
133 test_cmp_config true core.sparseCheckout
137 test_expect_success
'set sparse-checkout using builtin' '
138 git -C repo sparse-checkout set "/*" "!/*/" "*folder*" &&
139 cat >expect <<-\EOF &&
144 git -C repo sparse-checkout list >actual &&
145 test_cmp expect actual &&
146 test_cmp expect repo/.git/info/sparse-checkout &&
147 check_files repo a folder1 folder2
150 test_expect_success
'set sparse-checkout using --stdin' '
151 cat >expect <<-\EOF &&
157 git -C repo sparse-checkout set --stdin <expect &&
158 git -C repo sparse-checkout list >actual &&
159 test_cmp expect actual &&
160 test_cmp expect repo/.git/info/sparse-checkout &&
161 check_files repo "a folder1 folder2"
164 test_expect_success
'add to sparse-checkout' '
165 cat repo/.git/info/sparse-checkout >expect &&
172 git -C repo sparse-checkout add --stdin <add &&
173 git -C repo sparse-checkout list >actual &&
174 test_cmp expect actual &&
175 test_cmp expect repo/.git/info/sparse-checkout &&
176 check_files repo "a folder1 folder2"
179 test_expect_success
'cone mode: match patterns' '
180 git -C repo config --worktree core.sparseCheckoutCone true &&
181 rm -rf repo/a repo/folder1 repo/folder2 &&
182 git -C repo read-tree -mu HEAD 2>err &&
183 test_i18ngrep ! "disabling cone patterns" err &&
184 git -C repo reset --hard &&
185 check_files repo a folder1 folder2
188 test_expect_success
'cone mode: warn on bad pattern' '
189 test_when_finished mv sparse-checkout repo/.git/info/ &&
190 cp repo/.git/info/sparse-checkout . &&
191 echo "!/deep/deeper/*" >>repo/.git/info/sparse-checkout &&
192 git -C repo read-tree -mu HEAD 2>err &&
193 test_i18ngrep "unrecognized negative pattern" err
196 test_expect_success
'sparse-checkout disable' '
197 test_when_finished rm -rf repo/.git/info/sparse-checkout &&
198 git -C repo sparse-checkout disable &&
199 test_path_is_file repo/.git/info/sparse-checkout &&
200 git -C repo config --list >config &&
201 test_must_fail git config core.sparseCheckout &&
202 check_files repo a deep folder1 folder2
205 test_expect_success
'cone mode: init and set' '
206 git -C repo sparse-checkout init --cone &&
207 git -C repo config --list >config &&
208 test_i18ngrep "core.sparsecheckoutcone=true" config &&
209 list_files repo >dir &&
211 test_cmp expect dir &&
212 git -C repo sparse-checkout set deep/deeper1/deepest/ 2>err &&
213 test_must_be_empty err &&
214 check_files repo a deep &&
215 check_files repo/deep a deeper1 &&
216 check_files repo/deep/deeper1 a deepest &&
217 cat >expect <<-\EOF &&
224 /deep/deeper1/deepest/
226 test_cmp expect repo/.git/info/sparse-checkout &&
227 git -C repo sparse-checkout set --stdin 2>err <<-\EOF &&
231 test_must_be_empty err &&
232 check_files repo a folder1 folder2
235 test_expect_success
'cone mode: list' '
236 cat >expect <<-\EOF &&
240 git -C repo sparse-checkout set --stdin <expect &&
241 git -C repo sparse-checkout list >actual 2>err &&
242 test_must_be_empty err &&
243 test_cmp expect actual
246 test_expect_success
'cone mode: set with nested folders' '
247 git -C repo sparse-checkout set deep deep/deeper1/deepest 2>err &&
248 test_line_count = 0 err &&
249 cat >expect <<-\EOF &&
254 test_cmp repo/.git/info/sparse-checkout expect
257 test_expect_success
'cone mode: add independent path' '
258 git -C repo sparse-checkout set deep/deeper1 &&
259 git -C repo sparse-checkout add folder1 &&
260 cat >expect <<-\EOF &&
268 test_cmp expect repo/.git/info/sparse-checkout &&
269 check_files repo a deep folder1
272 test_expect_success
'cone mode: add sibling path' '
273 git -C repo sparse-checkout set deep/deeper1 &&
274 git -C repo sparse-checkout add deep/deeper2 &&
275 cat >expect <<-\EOF &&
283 test_cmp expect repo/.git/info/sparse-checkout &&
284 check_files repo a deep
287 test_expect_success
'cone mode: add parent path' '
288 git -C repo sparse-checkout set deep/deeper1 folder1 &&
289 git -C repo sparse-checkout add deep &&
290 cat >expect <<-\EOF &&
296 test_cmp expect repo/.git/info/sparse-checkout &&
297 check_files repo a deep folder1
300 test_expect_success
'not-up-to-date does not block rest of sparsification' '
301 test_when_finished git -C repo sparse-checkout disable &&
302 test_when_finished git -C repo reset --hard &&
303 git -C repo sparse-checkout set deep &&
305 echo update >repo/deep/deeper2/a &&
306 cp repo/.git/info/sparse-checkout expect &&
307 test_write_lines "!/deep/*/" "/deep/deeper1/" >>expect &&
309 git -C repo sparse-checkout set deep/deeper1 2>err &&
311 test_i18ngrep "The following paths are not up to date" err &&
312 test_cmp expect repo/.git/info/sparse-checkout &&
313 check_files repo/deep a deeper1 deeper2 &&
314 check_files repo/deep/deeper1 a deepest &&
315 check_files repo/deep/deeper1/deepest a &&
316 check_files repo/deep/deeper2 a
319 test_expect_success
'revert to old sparse-checkout on empty update' '
320 git init empty-test &&
324 git commit -m "test" &&
325 git sparse-checkout set nothing 2>err &&
326 test_i18ngrep ! "Sparse checkout leaves no entry on working directory" err &&
327 test_i18ngrep ! ".git/index.lock" err &&
328 git sparse-checkout set file
332 test_expect_success
'fail when lock is taken' '
333 test_when_finished rm -rf repo/.git/info/sparse-checkout.lock &&
334 touch repo/.git/info/sparse-checkout.lock &&
335 test_must_fail git -C repo sparse-checkout set deep 2>err &&
336 test_i18ngrep "Unable to create .*\.lock" err
339 test_expect_success
'.gitignore should not warn about cone mode' '
340 git -C repo config --worktree core.sparseCheckoutCone true &&
341 echo "**/bin/*" >repo/.gitignore &&
342 git -C repo reset --hard 2>err &&
343 test_i18ngrep ! "disabling cone patterns" err
346 test_expect_success
'sparse-checkout (init|set|disable) warns with dirty status' '
347 git clone repo dirty &&
348 echo dirty >dirty/folder1/a &&
350 git -C dirty sparse-checkout init 2>err &&
351 test_i18ngrep "warning.*The following paths are not up to date" err &&
353 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
354 test_i18ngrep "warning.*The following paths are not up to date" err &&
355 test_path_is_file dirty/folder1/a &&
357 git -C dirty sparse-checkout disable 2>err &&
358 test_must_be_empty err &&
360 git -C dirty reset --hard &&
361 git -C dirty sparse-checkout init &&
362 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
363 test_path_is_missing dirty/folder1/a &&
364 git -C dirty sparse-checkout disable &&
365 test_path_is_file dirty/folder1/a
368 test_expect_success
'sparse-checkout (init|set|disable) warns with unmerged status' '
369 git clone repo unmerged &&
372 0 0000000000000000000000000000000000000000 folder1/a
373 100644 $(git -C unmerged rev-parse HEAD:folder1/a) 1 folder1/a
375 git -C unmerged update-index --index-info <input &&
377 git -C unmerged sparse-checkout init 2>err &&
378 test_i18ngrep "warning.*The following paths are unmerged" err &&
380 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
381 test_i18ngrep "warning.*The following paths are unmerged" err &&
382 test_path_is_file dirty/folder1/a &&
384 git -C unmerged sparse-checkout disable 2>err &&
385 test_i18ngrep "warning.*The following paths are unmerged" err &&
387 git -C unmerged reset --hard &&
388 git -C unmerged sparse-checkout init &&
389 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* &&
390 git -C unmerged sparse-checkout disable
393 test_expect_success
'sparse-checkout reapply' '
394 git clone repo tweak &&
396 echo dirty >tweak/deep/deeper2/a &&
399 0 0000000000000000000000000000000000000000 folder1/a
400 100644 $(git -C tweak rev-parse HEAD:folder1/a) 1 folder1/a
402 git -C tweak update-index --index-info <input &&
404 git -C tweak sparse-checkout init --cone 2>err &&
405 test_i18ngrep "warning.*The following paths are not up to date" err &&
406 test_i18ngrep "warning.*The following paths are unmerged" err &&
408 git -C tweak sparse-checkout set folder2 deep/deeper1 2>err &&
409 test_i18ngrep "warning.*The following paths are not up to date" err &&
410 test_i18ngrep "warning.*The following paths are unmerged" err &&
412 git -C tweak sparse-checkout reapply 2>err &&
413 test_i18ngrep "warning.*The following paths are not up to date" err &&
414 test_path_is_file tweak/deep/deeper2/a &&
415 test_i18ngrep "warning.*The following paths are unmerged" err &&
416 test_path_is_file tweak/folder1/a &&
418 git -C tweak checkout HEAD deep/deeper2/a &&
419 git -C tweak sparse-checkout reapply 2>err &&
420 test_i18ngrep ! "warning.*The following paths are not up to date" err &&
421 test_path_is_missing tweak/deep/deeper2/a &&
422 test_i18ngrep "warning.*The following paths are unmerged" err &&
423 test_path_is_file tweak/folder1/a &&
425 git -C tweak add folder1/a &&
426 git -C tweak sparse-checkout reapply 2>err &&
427 test_must_be_empty err &&
428 test_path_is_missing tweak/deep/deeper2/a &&
429 test_path_is_missing tweak/folder1/a &&
431 git -C tweak sparse-checkout disable
434 test_expect_success
'cone mode: set with core.ignoreCase=true' '
435 rm repo/.git/info/sparse-checkout &&
436 git -C repo sparse-checkout init --cone &&
437 git -C repo -c core.ignoreCase=true sparse-checkout set folder1 &&
438 cat >expect <<-\EOF &&
443 test_cmp expect repo/.git/info/sparse-checkout &&
444 check_files repo a folder1
447 test_expect_success
'interaction with submodules' '
448 git clone repo super &&
452 git submodule add ../repo modules/child &&
454 git commit -m "add submodule" &&
455 git sparse-checkout init --cone &&
456 git sparse-checkout set folder1
458 check_files super a folder1 modules &&
459 check_files super/modules/child a deep folder1 folder2
462 test_expect_success
'different sparse-checkouts with worktrees' '
463 git -C repo worktree add --detach ../worktree &&
464 check_files worktree "a deep folder1 folder2" &&
465 git -C worktree sparse-checkout init --cone &&
466 git -C repo sparse-checkout set folder1 &&
467 git -C worktree sparse-checkout set deep/deeper1 &&
468 check_files repo a folder1 &&
469 check_files worktree a deep
472 test_expect_success
'set using filename keeps file on-disk' '
473 git -C repo sparse-checkout set a deep &&
474 cat >expect <<-\EOF &&
480 test_cmp expect repo/.git/info/sparse-checkout &&
481 check_files repo a deep
484 check_read_tree_errors
() {
488 git
-C $REPO -c core.sparseCheckoutCone
=false read-tree
-mu HEAD
2>err
&&
489 test_must_be_empty err
&&
490 check_files
$REPO "$FILES" &&
491 git
-C $REPO read-tree
-mu HEAD
2>err
&&
494 test_must_be_empty err
496 test_i18ngrep
"$ERRORS" err
498 check_files
$REPO $FILES
501 test_expect_success
'pattern-checks: /A/**' '
502 cat >repo/.git/info/sparse-checkout <<-\EOF &&
507 check_read_tree_errors repo "a folder1" "disabling cone pattern matching"
510 test_expect_success
'pattern-checks: /A/**/B/' '
511 cat >repo/.git/info/sparse-checkout <<-\EOF &&
516 check_read_tree_errors repo "a deep" "disabling cone pattern matching" &&
517 check_files repo/deep "deeper1" &&
518 check_files repo/deep/deeper1 "deepest"
521 test_expect_success
'pattern-checks: too short' '
522 cat >repo/.git/info/sparse-checkout <<-\EOF &&
527 check_read_tree_errors repo "a" "disabling cone pattern matching"
529 test_expect_success
'pattern-checks: not too short' '
530 cat >repo/.git/info/sparse-checkout <<-\EOF &&
535 git -C repo read-tree -mu HEAD 2>err &&
536 test_must_be_empty err &&
540 test_expect_success
'pattern-checks: trailing "*"' '
541 cat >repo/.git/info/sparse-checkout <<-\EOF &&
546 check_read_tree_errors repo "a" "disabling cone pattern matching"
549 test_expect_success
'pattern-checks: starting "*"' '
550 cat >repo/.git/info/sparse-checkout <<-\EOF &&
555 check_read_tree_errors repo "a deep" "disabling cone pattern matching"
558 test_expect_success
'pattern-checks: contained glob characters' '
559 for c in "[a]" "\\" "?" "*"
561 cat >repo/.git/info/sparse-checkout <<-EOF &&
566 check_read_tree_errors repo "a" "disabling cone pattern matching"
570 test_expect_success BSLASHPSPEC
'pattern-checks: escaped characters' '
571 git clone repo escaped &&
572 TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
573 NEWTREE=$(git -C escaped mktree <<-EOF
574 $(git -C escaped ls-tree HEAD)
575 040000 tree $TREEOID zbad\\dir
576 040000 tree $TREEOID zdoes*exist
577 040000 tree $TREEOID zglob[!a]?
580 COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
581 git -C escaped reset --hard $COMMIT &&
582 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
583 git -C escaped sparse-checkout init --cone &&
584 git -C escaped sparse-checkout set zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" &&
585 cat >expect <<-\EOF &&
595 test_cmp expect escaped/.git/info/sparse-checkout &&
596 check_read_tree_errors escaped "a zbad\\dir zdoes*exist zglob[!a]?" &&
597 git -C escaped ls-tree -d --name-only HEAD >list-expect &&
598 git -C escaped sparse-checkout set --stdin <list-expect &&
599 cat >expect <<-\EOF &&
609 test_cmp expect escaped/.git/info/sparse-checkout &&
610 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
611 git -C escaped sparse-checkout list >list-actual &&
612 test_cmp list-expect list-actual
615 test_expect_success MINGW
'cone mode replaces backslashes with slashes' '
616 git -C repo sparse-checkout set deep\\deeper1 &&
617 cat >expect <<-\EOF &&
624 test_cmp expect repo/.git/info/sparse-checkout &&
625 check_files repo a deep &&
626 check_files repo/deep a deeper1