Merge branch 'cb/credential-doc-fixes'
[git/raj.git] / t / t1091-sparse-checkout-builtin.sh
blob88cdde255cdad7a0b138033b3ff9a997e4a2c0b2
1 #!/bin/sh
3 test_description='sparse checkout builtin tests'
5 . ./test-lib.sh
7 list_files() {
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' *)
15 check_files() {
16 list_files "$1" >actual &&
17 shift &&
18 printf "%s\n" $@ >expect &&
19 test_cmp expect actual
22 test_expect_success 'setup' '
23 git init repo &&
25 cd repo &&
26 echo "initial" >a &&
27 mkdir folder1 folder2 deep &&
28 mkdir deep/deeper1 deep/deeper2 &&
29 mkdir deep/deeper1/deepest &&
30 cp a folder1 &&
31 cp a folder2 &&
32 cp a deep &&
33 cp a deep/deeper1 &&
34 cp a deep/deeper2 &&
35 cp a deep/deeper1/deepest &&
36 git add . &&
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 &&
50 /folder1/*
51 /deep/
52 **/a
53 !*bin*
54 EOF
55 cp repo/.git/info/sparse-checkout expect &&
56 git -C repo sparse-checkout list >list &&
57 test_cmp expect list
60 test_expect_success 'git sparse-checkout init' '
61 git -C repo sparse-checkout init &&
62 cat >expect <<-\EOF &&
64 !/*/
65 EOF
66 test_cmp expect repo/.git/info/sparse-checkout &&
67 test_cmp_config -C repo true core.sparsecheckout &&
68 check_files repo a
71 test_expect_success 'git sparse-checkout list after init' '
72 git -C repo sparse-checkout list >actual &&
73 cat >expect <<-\EOF &&
75 !/*/
76 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 &&
85 !/*/
86 *folder*
87 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 &&
97 !/*/
98 EOF
99 test_cmp expect actual &&
100 check_files clone a
103 test_expect_success 'set enables config' '
104 git init empty-config &&
106 cd empty-config &&
107 test_commit test file &&
108 test_path_is_missing .git/config.worktree &&
109 git sparse-checkout set nothing &&
110 test_path_is_file .git/config.worktree &&
111 test_cmp_config true core.sparseCheckout
115 test_expect_success 'set sparse-checkout using builtin' '
116 git -C repo sparse-checkout set "/*" "!/*/" "*folder*" &&
117 cat >expect <<-\EOF &&
119 !/*/
120 *folder*
122 git -C repo sparse-checkout list >actual &&
123 test_cmp expect actual &&
124 test_cmp expect repo/.git/info/sparse-checkout &&
125 check_files repo a folder1 folder2
128 test_expect_success 'set sparse-checkout using --stdin' '
129 cat >expect <<-\EOF &&
131 !/*/
132 /folder1/
133 /folder2/
135 git -C repo sparse-checkout set --stdin <expect &&
136 git -C repo sparse-checkout list >actual &&
137 test_cmp expect actual &&
138 test_cmp expect repo/.git/info/sparse-checkout &&
139 check_files repo "a folder1 folder2"
142 test_expect_success 'add to sparse-checkout' '
143 cat repo/.git/info/sparse-checkout >expect &&
144 cat >add <<-\EOF &&
145 pattern1
146 /folder1/
147 pattern2
149 cat add >>expect &&
150 git -C repo sparse-checkout add --stdin <add &&
151 git -C repo sparse-checkout list >actual &&
152 test_cmp expect actual &&
153 test_cmp expect repo/.git/info/sparse-checkout &&
154 check_files repo "a folder1 folder2"
157 test_expect_success 'cone mode: match patterns' '
158 git -C repo config --worktree core.sparseCheckoutCone true &&
159 rm -rf repo/a repo/folder1 repo/folder2 &&
160 git -C repo read-tree -mu HEAD 2>err &&
161 test_i18ngrep ! "disabling cone patterns" err &&
162 git -C repo reset --hard &&
163 check_files repo a folder1 folder2
166 test_expect_success 'cone mode: warn on bad pattern' '
167 test_when_finished mv sparse-checkout repo/.git/info/ &&
168 cp repo/.git/info/sparse-checkout . &&
169 echo "!/deep/deeper/*" >>repo/.git/info/sparse-checkout &&
170 git -C repo read-tree -mu HEAD 2>err &&
171 test_i18ngrep "unrecognized negative pattern" err
174 test_expect_success 'sparse-checkout disable' '
175 test_when_finished rm -rf repo/.git/info/sparse-checkout &&
176 git -C repo sparse-checkout disable &&
177 test_path_is_file repo/.git/info/sparse-checkout &&
178 git -C repo config --list >config &&
179 test_must_fail git config core.sparseCheckout &&
180 check_files repo a deep folder1 folder2
183 test_expect_success 'cone mode: init and set' '
184 git -C repo sparse-checkout init --cone &&
185 git -C repo config --list >config &&
186 test_i18ngrep "core.sparsecheckoutcone=true" config &&
187 list_files repo >dir &&
188 echo a >expect &&
189 test_cmp expect dir &&
190 git -C repo sparse-checkout set deep/deeper1/deepest/ 2>err &&
191 test_must_be_empty err &&
192 check_files repo a deep &&
193 check_files repo/deep a deeper1 &&
194 check_files repo/deep/deeper1 a deepest &&
195 cat >expect <<-\EOF &&
197 !/*/
198 /deep/
199 !/deep/*/
200 /deep/deeper1/
201 !/deep/deeper1/*/
202 /deep/deeper1/deepest/
204 test_cmp expect repo/.git/info/sparse-checkout &&
205 git -C repo sparse-checkout set --stdin 2>err <<-\EOF &&
206 folder1
207 folder2
209 test_must_be_empty err &&
210 check_files repo a folder1 folder2
213 test_expect_success 'cone mode: list' '
214 cat >expect <<-\EOF &&
215 folder1
216 folder2
218 git -C repo sparse-checkout set --stdin <expect &&
219 git -C repo sparse-checkout list >actual 2>err &&
220 test_must_be_empty err &&
221 test_cmp expect actual
224 test_expect_success 'cone mode: set with nested folders' '
225 git -C repo sparse-checkout set deep deep/deeper1/deepest 2>err &&
226 test_line_count = 0 err &&
227 cat >expect <<-\EOF &&
229 !/*/
230 /deep/
232 test_cmp repo/.git/info/sparse-checkout expect
235 test_expect_success 'cone mode: add independent path' '
236 git -C repo sparse-checkout set deep/deeper1 &&
237 git -C repo sparse-checkout add folder1 &&
238 cat >expect <<-\EOF &&
240 !/*/
241 /deep/
242 !/deep/*/
243 /deep/deeper1/
244 /folder1/
246 test_cmp expect repo/.git/info/sparse-checkout &&
247 check_files repo a deep folder1
250 test_expect_success 'cone mode: add sibling path' '
251 git -C repo sparse-checkout set deep/deeper1 &&
252 git -C repo sparse-checkout add deep/deeper2 &&
253 cat >expect <<-\EOF &&
255 !/*/
256 /deep/
257 !/deep/*/
258 /deep/deeper1/
259 /deep/deeper2/
261 test_cmp expect repo/.git/info/sparse-checkout &&
262 check_files repo a deep
265 test_expect_success 'cone mode: add parent path' '
266 git -C repo sparse-checkout set deep/deeper1 folder1 &&
267 git -C repo sparse-checkout add deep &&
268 cat >expect <<-\EOF &&
270 !/*/
271 /deep/
272 /folder1/
274 test_cmp expect repo/.git/info/sparse-checkout &&
275 check_files repo a deep folder1
278 test_expect_success 'not-up-to-date does not block rest of sparsification' '
279 test_when_finished git -C repo sparse-checkout disable &&
280 test_when_finished git -C repo reset --hard &&
281 git -C repo sparse-checkout set deep &&
283 echo update >repo/deep/deeper2/a &&
284 cp repo/.git/info/sparse-checkout expect &&
285 test_write_lines "!/deep/*/" "/deep/deeper1/" >>expect &&
287 git -C repo sparse-checkout set deep/deeper1 2>err &&
289 test_i18ngrep "The following paths are not up to date" err &&
290 test_cmp expect repo/.git/info/sparse-checkout &&
291 check_files repo/deep a deeper1 deeper2 &&
292 check_files repo/deep/deeper1 a deepest &&
293 check_files repo/deep/deeper1/deepest a &&
294 check_files repo/deep/deeper2 a
297 test_expect_success 'revert to old sparse-checkout on empty update' '
298 git init empty-test &&
300 echo >file &&
301 git add file &&
302 git commit -m "test" &&
303 git sparse-checkout set nothing 2>err &&
304 test_i18ngrep ! "Sparse checkout leaves no entry on working directory" err &&
305 test_i18ngrep ! ".git/index.lock" err &&
306 git sparse-checkout set file
310 test_expect_success 'fail when lock is taken' '
311 test_when_finished rm -rf repo/.git/info/sparse-checkout.lock &&
312 touch repo/.git/info/sparse-checkout.lock &&
313 test_must_fail git -C repo sparse-checkout set deep 2>err &&
314 test_i18ngrep "Unable to create .*\.lock" err
317 test_expect_success '.gitignore should not warn about cone mode' '
318 git -C repo config --worktree core.sparseCheckoutCone true &&
319 echo "**/bin/*" >repo/.gitignore &&
320 git -C repo reset --hard 2>err &&
321 test_i18ngrep ! "disabling cone patterns" err
324 test_expect_success 'sparse-checkout (init|set|disable) warns with dirty status' '
325 git clone repo dirty &&
326 echo dirty >dirty/folder1/a &&
328 git -C dirty sparse-checkout init 2>err &&
329 test_i18ngrep "warning.*The following paths are not up to date" err &&
331 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
332 test_i18ngrep "warning.*The following paths are not up to date" err &&
333 test_path_is_file dirty/folder1/a &&
335 git -C dirty sparse-checkout disable 2>err &&
336 test_must_be_empty err &&
338 git -C dirty reset --hard &&
339 git -C dirty sparse-checkout init &&
340 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
341 test_path_is_missing dirty/folder1/a &&
342 git -C dirty sparse-checkout disable &&
343 test_path_is_file dirty/folder1/a
346 test_expect_success 'sparse-checkout (init|set|disable) warns with unmerged status' '
347 git clone repo unmerged &&
349 cat >input <<-EOF &&
350 0 0000000000000000000000000000000000000000 folder1/a
351 100644 $(git -C unmerged rev-parse HEAD:folder1/a) 1 folder1/a
353 git -C unmerged update-index --index-info <input &&
355 git -C unmerged sparse-checkout init 2>err &&
356 test_i18ngrep "warning.*The following paths are unmerged" err &&
358 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
359 test_i18ngrep "warning.*The following paths are unmerged" err &&
360 test_path_is_file dirty/folder1/a &&
362 git -C unmerged sparse-checkout disable 2>err &&
363 test_i18ngrep "warning.*The following paths are unmerged" err &&
365 git -C unmerged reset --hard &&
366 git -C unmerged sparse-checkout init &&
367 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* &&
368 git -C unmerged sparse-checkout disable
371 test_expect_success 'sparse-checkout reapply' '
372 git clone repo tweak &&
374 echo dirty >tweak/deep/deeper2/a &&
376 cat >input <<-EOF &&
377 0 0000000000000000000000000000000000000000 folder1/a
378 100644 $(git -C tweak rev-parse HEAD:folder1/a) 1 folder1/a
380 git -C tweak update-index --index-info <input &&
382 git -C tweak sparse-checkout init --cone 2>err &&
383 test_i18ngrep "warning.*The following paths are not up to date" err &&
384 test_i18ngrep "warning.*The following paths are unmerged" err &&
386 git -C tweak sparse-checkout set folder2 deep/deeper1 2>err &&
387 test_i18ngrep "warning.*The following paths are not up to date" err &&
388 test_i18ngrep "warning.*The following paths are unmerged" err &&
390 git -C tweak sparse-checkout reapply 2>err &&
391 test_i18ngrep "warning.*The following paths are not up to date" err &&
392 test_path_is_file tweak/deep/deeper2/a &&
393 test_i18ngrep "warning.*The following paths are unmerged" err &&
394 test_path_is_file tweak/folder1/a &&
396 git -C tweak checkout HEAD deep/deeper2/a &&
397 git -C tweak sparse-checkout reapply 2>err &&
398 test_i18ngrep ! "warning.*The following paths are not up to date" err &&
399 test_path_is_missing tweak/deep/deeper2/a &&
400 test_i18ngrep "warning.*The following paths are unmerged" err &&
401 test_path_is_file tweak/folder1/a &&
403 git -C tweak add folder1/a &&
404 git -C tweak sparse-checkout reapply 2>err &&
405 test_must_be_empty err &&
406 test_path_is_missing tweak/deep/deeper2/a &&
407 test_path_is_missing tweak/folder1/a &&
409 git -C tweak sparse-checkout disable
412 test_expect_success 'cone mode: set with core.ignoreCase=true' '
413 rm repo/.git/info/sparse-checkout &&
414 git -C repo sparse-checkout init --cone &&
415 git -C repo -c core.ignoreCase=true sparse-checkout set folder1 &&
416 cat >expect <<-\EOF &&
418 !/*/
419 /folder1/
421 test_cmp expect repo/.git/info/sparse-checkout &&
422 check_files repo a folder1
425 test_expect_success 'interaction with submodules' '
426 git clone repo super &&
428 cd super &&
429 mkdir modules &&
430 git submodule add ../repo modules/child &&
431 git add . &&
432 git commit -m "add submodule" &&
433 git sparse-checkout init --cone &&
434 git sparse-checkout set folder1
435 ) &&
436 check_files super a folder1 modules &&
437 check_files super/modules/child a deep folder1 folder2
440 test_expect_success 'different sparse-checkouts with worktrees' '
441 git -C repo worktree add --detach ../worktree &&
442 check_files worktree "a deep folder1 folder2" &&
443 git -C worktree sparse-checkout init --cone &&
444 git -C repo sparse-checkout set folder1 &&
445 git -C worktree sparse-checkout set deep/deeper1 &&
446 check_files repo a folder1 &&
447 check_files worktree a deep
450 test_expect_success 'set using filename keeps file on-disk' '
451 git -C repo sparse-checkout set a deep &&
452 cat >expect <<-\EOF &&
454 !/*/
456 /deep/
458 test_cmp expect repo/.git/info/sparse-checkout &&
459 check_files repo a deep
462 check_read_tree_errors () {
463 REPO=$1
464 FILES=$2
465 ERRORS=$3
466 git -C $REPO -c core.sparseCheckoutCone=false read-tree -mu HEAD 2>err &&
467 test_must_be_empty err &&
468 check_files $REPO "$FILES" &&
469 git -C $REPO read-tree -mu HEAD 2>err &&
470 if test -z "$ERRORS"
471 then
472 test_must_be_empty err
473 else
474 test_i18ngrep "$ERRORS" err
475 fi &&
476 check_files $REPO $FILES
479 test_expect_success 'pattern-checks: /A/**' '
480 cat >repo/.git/info/sparse-checkout <<-\EOF &&
482 !/*/
483 /folder1/**
485 check_read_tree_errors repo "a folder1" "disabling cone pattern matching"
488 test_expect_success 'pattern-checks: /A/**/B/' '
489 cat >repo/.git/info/sparse-checkout <<-\EOF &&
491 !/*/
492 /deep/**/deepest
494 check_read_tree_errors repo "a deep" "disabling cone pattern matching" &&
495 check_files repo/deep "deeper1" &&
496 check_files repo/deep/deeper1 "deepest"
499 test_expect_success 'pattern-checks: too short' '
500 cat >repo/.git/info/sparse-checkout <<-\EOF &&
502 !/*/
505 check_read_tree_errors repo "a" "disabling cone pattern matching"
507 test_expect_success 'pattern-checks: not too short' '
508 cat >repo/.git/info/sparse-checkout <<-\EOF &&
510 !/*/
513 git -C repo read-tree -mu HEAD 2>err &&
514 test_must_be_empty err &&
515 check_files repo a
518 test_expect_success 'pattern-checks: trailing "*"' '
519 cat >repo/.git/info/sparse-checkout <<-\EOF &&
521 !/*/
524 check_read_tree_errors repo "a" "disabling cone pattern matching"
527 test_expect_success 'pattern-checks: starting "*"' '
528 cat >repo/.git/info/sparse-checkout <<-\EOF &&
530 !/*/
531 *eep/
533 check_read_tree_errors repo "a deep" "disabling cone pattern matching"
536 test_expect_success 'pattern-checks: contained glob characters' '
537 for c in "[a]" "\\" "?" "*"
539 cat >repo/.git/info/sparse-checkout <<-EOF &&
541 !/*/
542 something$c-else/
544 check_read_tree_errors repo "a" "disabling cone pattern matching"
545 done
548 test_expect_success BSLASHPSPEC 'pattern-checks: escaped characters' '
549 git clone repo escaped &&
550 TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
551 NEWTREE=$(git -C escaped mktree <<-EOF
552 $(git -C escaped ls-tree HEAD)
553 040000 tree $TREEOID zbad\\dir
554 040000 tree $TREEOID zdoes*exist
555 040000 tree $TREEOID zglob[!a]?
557 ) &&
558 COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
559 git -C escaped reset --hard $COMMIT &&
560 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
561 git -C escaped sparse-checkout init --cone &&
562 git -C escaped sparse-checkout set zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" &&
563 cat >expect <<-\EOF &&
565 !/*/
566 /zbad\\dir/
567 !/zbad\\dir/*/
568 /zbad\\dir/bogus/
569 /zdoes\*exist/
570 /zdoes\*not\*exist/
571 /zglob\[!a]\?/
573 test_cmp expect escaped/.git/info/sparse-checkout &&
574 check_read_tree_errors escaped "a zbad\\dir zdoes*exist zglob[!a]?" &&
575 git -C escaped ls-tree -d --name-only HEAD >list-expect &&
576 git -C escaped sparse-checkout set --stdin <list-expect &&
577 cat >expect <<-\EOF &&
579 !/*/
580 /deep/
581 /folder1/
582 /folder2/
583 /zbad\\dir/
584 /zdoes\*exist/
585 /zglob\[!a]\?/
587 test_cmp expect escaped/.git/info/sparse-checkout &&
588 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
589 git -C escaped sparse-checkout list >list-actual &&
590 test_cmp list-expect list-actual
593 test_expect_success MINGW 'cone mode replaces backslashes with slashes' '
594 git -C repo sparse-checkout set deep\\deeper1 &&
595 cat >expect <<-\EOF &&
597 !/*/
598 /deep/
599 !/deep/*/
600 /deep/deeper1/
602 test_cmp expect repo/.git/info/sparse-checkout &&
603 check_files repo a deep &&
604 check_files repo/deep a deeper1
607 test_done