Merge branch 'es/test-cmp-typocatcher'
[git] / t / t1091-sparse-checkout-builtin.sh
1 #!/bin/sh
2
3 test_description='sparse checkout builtin tests'
4
5 . ./test-lib.sh
6
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' *)
13 }
14
15 check_files() {
16         list_files "$1" >actual &&
17         shift &&
18         printf "%s\n" $@ >expect &&
19         test_cmp expect actual
20 }
21
22 test_expect_success 'setup' '
23         git init repo &&
24         (
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"
38         )
39 '
40
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
45 '
46
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
58 '
59
60 test_expect_success 'git sparse-checkout init' '
61         git -C repo sparse-checkout init &&
62         cat >expect <<-\EOF &&
63         /*
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
69 '
70
71 test_expect_success 'git sparse-checkout list after init' '
72         git -C repo sparse-checkout list >actual &&
73         cat >expect <<-\EOF &&
74         /*
75         !/*/
76         EOF
77         test_cmp expect actual
78 '
79
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 &&
84         /*
85         !/*/
86         *folder*
87         EOF
88         test_cmp expect repo/.git/info/sparse-checkout &&
89         check_files repo a folder1 folder2
90 '
91
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 &&
96         /*
97         !/*/
98         EOF
99         test_cmp expect actual &&
100         check_files clone a
101 '
102
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 &&
107
108         git -C clone_no_checkout sparse-checkout list >actual &&
109         cat >expect <<-\EOF &&
110         folder1
111         EOF
112         test_cmp expect actual &&
113
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 &&
118
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
123 '
124
125 test_expect_success 'set enables config' '
126         git init empty-config &&
127         (
128                 cd 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
134         )
135 '
136
137 test_expect_success 'set sparse-checkout using builtin' '
138         git -C repo sparse-checkout set "/*" "!/*/" "*folder*" &&
139         cat >expect <<-\EOF &&
140         /*
141         !/*/
142         *folder*
143         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
148 '
149
150 test_expect_success 'set sparse-checkout using --stdin' '
151         cat >expect <<-\EOF &&
152         /*
153         !/*/
154         /folder1/
155         /folder2/
156         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"
162 '
163
164 test_expect_success 'add to sparse-checkout' '
165         cat repo/.git/info/sparse-checkout >expect &&
166         cat >add <<-\EOF &&
167         pattern1
168         /folder1/
169         pattern2
170         EOF
171         cat add >>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"
177 '
178
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
186 '
187
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
194 '
195
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
203 '
204
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  &&
210         echo a >expect &&
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 &&
218         /*
219         !/*/
220         /deep/
221         !/deep/*/
222         /deep/deeper1/
223         !/deep/deeper1/*/
224         /deep/deeper1/deepest/
225         EOF
226         test_cmp expect repo/.git/info/sparse-checkout &&
227         git -C repo sparse-checkout set --stdin 2>err <<-\EOF &&
228         folder1
229         folder2
230         EOF
231         test_must_be_empty err &&
232         check_files repo a folder1 folder2
233 '
234
235 test_expect_success 'cone mode: list' '
236         cat >expect <<-\EOF &&
237         folder1
238         folder2
239         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
244 '
245
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 &&
250         /*
251         !/*/
252         /deep/
253         EOF
254         test_cmp repo/.git/info/sparse-checkout expect
255 '
256
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 &&
261         /*
262         !/*/
263         /deep/
264         !/deep/*/
265         /deep/deeper1/
266         /folder1/
267         EOF
268         test_cmp expect repo/.git/info/sparse-checkout &&
269         check_files repo a deep folder1
270 '
271
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 &&
276         /*
277         !/*/
278         /deep/
279         !/deep/*/
280         /deep/deeper1/
281         /deep/deeper2/
282         EOF
283         test_cmp expect repo/.git/info/sparse-checkout &&
284         check_files repo a deep
285 '
286
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 &&
291         /*
292         !/*/
293         /deep/
294         /folder1/
295         EOF
296         test_cmp expect repo/.git/info/sparse-checkout &&
297         check_files repo a deep folder1
298 '
299
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 &&
304
305         echo update >repo/deep/deeper2/a &&
306         cp repo/.git/info/sparse-checkout expect &&
307         test_write_lines "!/deep/*/" "/deep/deeper1/" >>expect &&
308
309         git -C repo sparse-checkout set deep/deeper1 2>err &&
310
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
317 '
318
319 test_expect_success 'revert to old sparse-checkout on empty update' '
320         git init empty-test &&
321         (
322                 echo >file &&
323                 git add file &&
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
329         )
330 '
331
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
337 '
338
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
344 '
345
346 test_expect_success 'sparse-checkout (init|set|disable) warns with dirty status' '
347         git clone repo dirty &&
348         echo dirty >dirty/folder1/a &&
349
350         git -C dirty sparse-checkout init 2>err &&
351         test_i18ngrep "warning.*The following paths are not up to date" err &&
352
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 &&
356
357         git -C dirty sparse-checkout disable 2>err &&
358         test_must_be_empty err &&
359
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
366 '
367
368 test_expect_success 'sparse-checkout (init|set|disable) warns with unmerged status' '
369         git clone repo unmerged &&
370
371         cat >input <<-EOF &&
372         0 $ZERO_OID     folder1/a
373         100644 $(git -C unmerged rev-parse HEAD:folder1/a) 1    folder1/a
374         EOF
375         git -C unmerged update-index --index-info <input &&
376
377         git -C unmerged sparse-checkout init 2>err &&
378         test_i18ngrep "warning.*The following paths are unmerged" err &&
379
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 &&
383
384         git -C unmerged sparse-checkout disable 2>err &&
385         test_i18ngrep "warning.*The following paths are unmerged" err &&
386
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
391 '
392
393 test_expect_success 'sparse-checkout reapply' '
394         git clone repo tweak &&
395
396         echo dirty >tweak/deep/deeper2/a &&
397
398         cat >input <<-EOF &&
399         0 $ZERO_OID     folder1/a
400         100644 $(git -C tweak rev-parse HEAD:folder1/a) 1       folder1/a
401         EOF
402         git -C tweak update-index --index-info <input &&
403
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 &&
407
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 &&
411
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 &&
417
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 &&
424
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 &&
430
431         git -C tweak sparse-checkout disable
432 '
433
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 &&
439         /*
440         !/*/
441         /folder1/
442         EOF
443         test_cmp expect repo/.git/info/sparse-checkout &&
444         check_files repo a folder1
445 '
446
447 test_expect_success 'interaction with submodules' '
448         git clone repo super &&
449         (
450                 cd super &&
451                 mkdir modules &&
452                 git submodule add ../repo modules/child &&
453                 git add . &&
454                 git commit -m "add submodule" &&
455                 git sparse-checkout init --cone &&
456                 git sparse-checkout set folder1
457         ) &&
458         check_files super a folder1 modules &&
459         check_files super/modules/child a deep folder1 folder2
460 '
461
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
470 '
471
472 test_expect_success 'set using filename keeps file on-disk' '
473         git -C repo sparse-checkout set a deep &&
474         cat >expect <<-\EOF &&
475         /*
476         !/*/
477         /a/
478         /deep/
479         EOF
480         test_cmp expect repo/.git/info/sparse-checkout &&
481         check_files repo a deep
482 '
483
484 check_read_tree_errors () {
485         REPO=$1
486         FILES=$2
487         ERRORS=$3
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 &&
492         if test -z "$ERRORS"
493         then
494                 test_must_be_empty err
495         else
496                 test_i18ngrep "$ERRORS" err
497         fi &&
498         check_files $REPO $FILES
499 }
500
501 test_expect_success 'pattern-checks: /A/**' '
502         cat >repo/.git/info/sparse-checkout <<-\EOF &&
503         /*
504         !/*/
505         /folder1/**
506         EOF
507         check_read_tree_errors repo "a folder1" "disabling cone pattern matching"
508 '
509
510 test_expect_success 'pattern-checks: /A/**/B/' '
511         cat >repo/.git/info/sparse-checkout <<-\EOF &&
512         /*
513         !/*/
514         /deep/**/deepest
515         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"
519 '
520
521 test_expect_success 'pattern-checks: too short' '
522         cat >repo/.git/info/sparse-checkout <<-\EOF &&
523         /*
524         !/*/
525         /
526         EOF
527         check_read_tree_errors repo "a" "disabling cone pattern matching"
528 '
529 test_expect_success 'pattern-checks: not too short' '
530         cat >repo/.git/info/sparse-checkout <<-\EOF &&
531         /*
532         !/*/
533         /b/
534         EOF
535         git -C repo read-tree -mu HEAD 2>err &&
536         test_must_be_empty err &&
537         check_files repo a
538 '
539
540 test_expect_success 'pattern-checks: trailing "*"' '
541         cat >repo/.git/info/sparse-checkout <<-\EOF &&
542         /*
543         !/*/
544         /a*
545         EOF
546         check_read_tree_errors repo "a" "disabling cone pattern matching"
547 '
548
549 test_expect_success 'pattern-checks: starting "*"' '
550         cat >repo/.git/info/sparse-checkout <<-\EOF &&
551         /*
552         !/*/
553         *eep/
554         EOF
555         check_read_tree_errors repo "a deep" "disabling cone pattern matching"
556 '
557
558 test_expect_success 'pattern-checks: contained glob characters' '
559         for c in "[a]" "\\" "?" "*"
560         do
561                 cat >repo/.git/info/sparse-checkout <<-EOF &&
562                 /*
563                 !/*/
564                 something$c-else/
565                 EOF
566                 check_read_tree_errors repo "a" "disabling cone pattern matching"
567         done
568 '
569
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]?
578         EOF
579         ) &&
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 &&
586         /*
587         !/*/
588         /zbad\\dir/
589         !/zbad\\dir/*/
590         /zbad\\dir/bogus/
591         /zdoes\*exist/
592         /zdoes\*not\*exist/
593         /zglob\[!a]\?/
594         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 &&
600         /*
601         !/*/
602         /deep/
603         /folder1/
604         /folder2/
605         /zbad\\dir/
606         /zdoes\*exist/
607         /zglob\[!a]\?/
608         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
613 '
614
615 test_expect_success MINGW 'cone mode replaces backslashes with slashes' '
616         git -C repo sparse-checkout set deep\\deeper1 &&
617         cat >expect <<-\EOF &&
618         /*
619         !/*/
620         /deep/
621         !/deep/*/
622         /deep/deeper1/
623         EOF
624         test_cmp expect repo/.git/info/sparse-checkout &&
625         check_files repo a deep &&
626         check_files repo/deep a deeper1
627 '
628
629 test_done