sparse-checkout: create 'add' subcommand
[git.git] / t / t1091-sparse-checkout-builtin.sh
blobf9265de5e869e0f7d77104f4a2873156f0876c82
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 test_must_fail git sparse-checkout set nothing &&
110 test_path_is_file .git/config.worktree &&
111 test_must_fail git config core.sparseCheckout &&
112 git sparse-checkout set "/*" &&
113 test_cmp_config true core.sparseCheckout
117 test_expect_success 'set sparse-checkout using builtin' '
118 git -C repo sparse-checkout set "/*" "!/*/" "*folder*" &&
119 cat >expect <<-\EOF &&
121 !/*/
122 *folder*
124 git -C repo sparse-checkout list >actual &&
125 test_cmp expect actual &&
126 test_cmp expect repo/.git/info/sparse-checkout &&
127 check_files repo a folder1 folder2
130 test_expect_success 'set sparse-checkout using --stdin' '
131 cat >expect <<-\EOF &&
133 !/*/
134 /folder1/
135 /folder2/
137 git -C repo sparse-checkout set --stdin <expect &&
138 git -C repo sparse-checkout list >actual &&
139 test_cmp expect actual &&
140 test_cmp expect repo/.git/info/sparse-checkout &&
141 check_files repo "a folder1 folder2"
144 test_expect_success 'add to sparse-checkout' '
145 cat repo/.git/info/sparse-checkout >expect &&
146 cat >add <<-\EOF &&
147 pattern1
148 /folder1/
149 pattern2
151 cat add >>expect &&
152 git -C repo sparse-checkout add --stdin <add &&
153 git -C repo sparse-checkout list >actual &&
154 test_cmp expect actual &&
155 test_cmp expect repo/.git/info/sparse-checkout &&
156 check_files repo "a folder1 folder2"
159 test_expect_success 'cone mode: match patterns' '
160 git -C repo config --worktree core.sparseCheckoutCone true &&
161 rm -rf repo/a repo/folder1 repo/folder2 &&
162 git -C repo read-tree -mu HEAD 2>err &&
163 test_i18ngrep ! "disabling cone patterns" err &&
164 git -C repo reset --hard &&
165 check_files repo a folder1 folder2
168 test_expect_success 'cone mode: warn on bad pattern' '
169 test_when_finished mv sparse-checkout repo/.git/info/ &&
170 cp repo/.git/info/sparse-checkout . &&
171 echo "!/deep/deeper/*" >>repo/.git/info/sparse-checkout &&
172 git -C repo read-tree -mu HEAD 2>err &&
173 test_i18ngrep "unrecognized negative pattern" err
176 test_expect_success 'sparse-checkout disable' '
177 test_when_finished rm -rf repo/.git/info/sparse-checkout &&
178 git -C repo sparse-checkout disable &&
179 test_path_is_file repo/.git/info/sparse-checkout &&
180 git -C repo config --list >config &&
181 test_must_fail git config core.sparseCheckout &&
182 check_files repo a deep folder1 folder2
185 test_expect_success 'cone mode: init and set' '
186 git -C repo sparse-checkout init --cone &&
187 git -C repo config --list >config &&
188 test_i18ngrep "core.sparsecheckoutcone=true" config &&
189 list_files repo >dir &&
190 echo a >expect &&
191 test_cmp expect dir &&
192 git -C repo sparse-checkout set deep/deeper1/deepest/ 2>err &&
193 test_must_be_empty err &&
194 check_files repo a deep &&
195 check_files repo/deep a deeper1 &&
196 check_files repo/deep/deeper1 a deepest &&
197 cat >expect <<-\EOF &&
199 !/*/
200 /deep/
201 !/deep/*/
202 /deep/deeper1/
203 !/deep/deeper1/*/
204 /deep/deeper1/deepest/
206 test_cmp expect repo/.git/info/sparse-checkout &&
207 git -C repo sparse-checkout set --stdin 2>err <<-\EOF &&
208 folder1
209 folder2
211 test_must_be_empty err &&
212 check_files repo a folder1 folder2
215 test_expect_success 'cone mode: list' '
216 cat >expect <<-\EOF &&
217 folder1
218 folder2
220 git -C repo sparse-checkout set --stdin <expect &&
221 git -C repo sparse-checkout list >actual 2>err &&
222 test_must_be_empty err &&
223 test_cmp expect actual
226 test_expect_success 'cone mode: set with nested folders' '
227 git -C repo sparse-checkout set deep deep/deeper1/deepest 2>err &&
228 test_line_count = 0 err &&
229 cat >expect <<-\EOF &&
231 !/*/
232 /deep/
234 test_cmp repo/.git/info/sparse-checkout expect
237 test_expect_success 'cone mode: add independent path' '
238 git -C repo sparse-checkout set deep/deeper1 &&
239 git -C repo sparse-checkout add folder1 &&
240 cat >expect <<-\EOF &&
242 !/*/
243 /deep/
244 !/deep/*/
245 /deep/deeper1/
246 /folder1/
248 test_cmp expect repo/.git/info/sparse-checkout &&
249 check_files repo a deep folder1
252 test_expect_success 'cone mode: add sibling path' '
253 git -C repo sparse-checkout set deep/deeper1 &&
254 git -C repo sparse-checkout add deep/deeper2 &&
255 cat >expect <<-\EOF &&
257 !/*/
258 /deep/
259 !/deep/*/
260 /deep/deeper1/
261 /deep/deeper2/
263 test_cmp expect repo/.git/info/sparse-checkout &&
264 check_files repo a deep
267 test_expect_success 'cone mode: add parent path' '
268 git -C repo sparse-checkout set deep/deeper1 folder1 &&
269 git -C repo sparse-checkout add deep &&
270 cat >expect <<-\EOF &&
272 !/*/
273 /deep/
274 /folder1/
276 test_cmp expect repo/.git/info/sparse-checkout &&
277 check_files repo a deep folder1
280 test_expect_success 'revert to old sparse-checkout on bad update' '
281 test_when_finished git -C repo reset --hard &&
282 git -C repo sparse-checkout set deep &&
283 echo update >repo/deep/deeper2/a &&
284 cp repo/.git/info/sparse-checkout expect &&
285 test_must_fail git -C repo sparse-checkout set deep/deeper1 2>err &&
286 test_i18ngrep "cannot set sparse-checkout patterns" err &&
287 test_cmp repo/.git/info/sparse-checkout expect &&
288 check_files repo/deep a deeper1 deeper2
291 test_expect_success 'revert to old sparse-checkout on empty update' '
292 git init empty-test &&
294 echo >file &&
295 git add file &&
296 git commit -m "test" &&
297 test_must_fail git sparse-checkout set nothing 2>err &&
298 test_i18ngrep "Sparse checkout leaves no entry on working directory" err &&
299 test_i18ngrep ! ".git/index.lock" err &&
300 git sparse-checkout set file
304 test_expect_success 'fail when lock is taken' '
305 test_when_finished rm -rf repo/.git/info/sparse-checkout.lock &&
306 touch repo/.git/info/sparse-checkout.lock &&
307 test_must_fail git -C repo sparse-checkout set deep 2>err &&
308 test_i18ngrep "File exists" err
311 test_expect_success '.gitignore should not warn about cone mode' '
312 git -C repo config --worktree core.sparseCheckoutCone true &&
313 echo "**/bin/*" >repo/.gitignore &&
314 git -C repo reset --hard 2>err &&
315 test_i18ngrep ! "disabling cone patterns" err
318 test_expect_success 'sparse-checkout (init|set|disable) fails with dirty status' '
319 git clone repo dirty &&
320 echo dirty >dirty/folder1/a &&
321 test_must_fail git -C dirty sparse-checkout init &&
322 test_must_fail git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
323 test_must_fail git -C dirty sparse-checkout disable &&
324 git -C dirty reset --hard &&
325 git -C dirty sparse-checkout init &&
326 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
327 git -C dirty sparse-checkout disable
330 test_expect_success 'cone mode: set with core.ignoreCase=true' '
331 git -C repo sparse-checkout init --cone &&
332 git -C repo -c core.ignoreCase=true sparse-checkout set folder1 &&
333 cat >expect <<-\EOF &&
335 !/*/
336 /folder1/
338 test_cmp expect repo/.git/info/sparse-checkout &&
339 check_files repo a folder1
342 test_expect_success 'interaction with submodules' '
343 git clone repo super &&
345 cd super &&
346 mkdir modules &&
347 git submodule add ../repo modules/child &&
348 git add . &&
349 git commit -m "add submodule" &&
350 git sparse-checkout init --cone &&
351 git sparse-checkout set folder1
352 ) &&
353 check_files super a folder1 modules &&
354 check_files super/modules/child a deep folder1 folder2
357 test_expect_success 'different sparse-checkouts with worktrees' '
358 git -C repo worktree add --detach ../worktree &&
359 check_files worktree "a deep folder1 folder2" &&
360 git -C worktree sparse-checkout init --cone &&
361 git -C repo sparse-checkout set folder1 &&
362 git -C worktree sparse-checkout set deep/deeper1 &&
363 check_files repo a folder1 &&
364 check_files worktree a deep
367 test_expect_success 'set using filename keeps file on-disk' '
368 git -C repo sparse-checkout set a deep &&
369 cat >expect <<-\EOF &&
371 !/*/
373 /deep/
375 test_cmp expect repo/.git/info/sparse-checkout &&
376 check_files repo a deep
379 check_read_tree_errors () {
380 REPO=$1
381 FILES=$2
382 ERRORS=$3
383 git -C $REPO -c core.sparseCheckoutCone=false read-tree -mu HEAD 2>err &&
384 test_must_be_empty err &&
385 check_files $REPO "$FILES" &&
386 git -C $REPO read-tree -mu HEAD 2>err &&
387 if test -z "$ERRORS"
388 then
389 test_must_be_empty err
390 else
391 test_i18ngrep "$ERRORS" err
392 fi &&
393 check_files $REPO $FILES
396 test_expect_success 'pattern-checks: /A/**' '
397 cat >repo/.git/info/sparse-checkout <<-\EOF &&
399 !/*/
400 /folder1/**
402 check_read_tree_errors repo "a folder1" "disabling cone pattern matching"
405 test_expect_success 'pattern-checks: /A/**/B/' '
406 cat >repo/.git/info/sparse-checkout <<-\EOF &&
408 !/*/
409 /deep/**/deepest
411 check_read_tree_errors repo "a deep" "disabling cone pattern matching" &&
412 check_files repo/deep "deeper1" &&
413 check_files repo/deep/deeper1 "deepest"
416 test_expect_success 'pattern-checks: too short' '
417 cat >repo/.git/info/sparse-checkout <<-\EOF &&
419 !/*/
422 check_read_tree_errors repo "a" "disabling cone pattern matching"
425 test_expect_success 'pattern-checks: trailing "*"' '
426 cat >repo/.git/info/sparse-checkout <<-\EOF &&
428 !/*/
431 check_read_tree_errors repo "a" "disabling cone pattern matching"
434 test_expect_success 'pattern-checks: starting "*"' '
435 cat >repo/.git/info/sparse-checkout <<-\EOF &&
437 !/*/
438 *eep/
440 check_read_tree_errors repo "a deep" "disabling cone pattern matching"
443 test_expect_success 'pattern-checks: contained glob characters' '
444 for c in "[a]" "\\" "?" "*"
446 cat >repo/.git/info/sparse-checkout <<-EOF &&
448 !/*/
449 something$c-else/
451 check_read_tree_errors repo "a" "disabling cone pattern matching"
452 done
455 test_expect_success BSLASHPSPEC 'pattern-checks: escaped characters' '
456 git clone repo escaped &&
457 TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
458 NEWTREE=$(git -C escaped mktree <<-EOF
459 $(git -C escaped ls-tree HEAD)
460 040000 tree $TREEOID zbad\\dir
461 040000 tree $TREEOID zdoes*exist
462 040000 tree $TREEOID zglob[!a]?
464 ) &&
465 COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
466 git -C escaped reset --hard $COMMIT &&
467 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
468 git -C escaped sparse-checkout init --cone &&
469 git -C escaped sparse-checkout set zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" &&
470 cat >expect <<-\EOF &&
472 !/*/
473 /zbad\\dir/
474 !/zbad\\dir/*/
475 /zbad\\dir/bogus/
476 /zdoes\*exist/
477 /zdoes\*not\*exist/
478 /zglob\[!a]\?/
480 test_cmp expect escaped/.git/info/sparse-checkout &&
481 check_read_tree_errors escaped "a zbad\\dir zdoes*exist zglob[!a]?" &&
482 git -C escaped ls-tree -d --name-only HEAD >list-expect &&
483 git -C escaped sparse-checkout set --stdin <list-expect &&
484 cat >expect <<-\EOF &&
486 !/*/
487 /deep/
488 /folder1/
489 /folder2/
490 /zbad\\dir/
491 /zdoes\*exist/
492 /zglob\[!a]\?/
494 test_cmp expect escaped/.git/info/sparse-checkout &&
495 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
496 git -C escaped sparse-checkout list >list-actual &&
497 test_cmp list-expect list-actual
500 test_done