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 'set enables config' '
104 git init 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 &&
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 &&
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 &&
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 &&
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 &&
204 /deep/deeper1/deepest/
206 test_cmp expect repo/.git/info/sparse-checkout &&
207 git -C repo sparse-checkout set --stdin 2>err <<-\EOF &&
211 test_must_be_empty err &&
212 check_files repo a folder1 folder2
215 test_expect_success 'cone mode: list' '
216 cat >expect <<-\EOF &&
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 &&
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 &&
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 &&
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 &&
276 test_cmp expect repo/.git/info/sparse-checkout &&
277 check_files repo a deep folder1
280 test_expect_success 'not-up-to-date does not block rest of sparsification' '
281 test_when_finished git -C repo sparse-checkout disable &&
282 test_when_finished git -C repo reset --hard &&
283 git -C repo sparse-checkout set deep &&
285 echo update >repo/deep/deeper2/a &&
286 cp repo/.git/info/sparse-checkout expect &&
287 test_write_lines "!/deep/*/" "/deep/deeper1/" >>expect &&
289 git -C repo sparse-checkout set deep/deeper1 2>err &&
291 test_i18ngrep "The following paths are not up to date" err &&
292 test_cmp expect repo/.git/info/sparse-checkout &&
293 check_files repo/deep a deeper1 deeper2 &&
294 check_files repo/deep/deeper1 a deepest &&
295 check_files repo/deep/deeper1/deepest a &&
296 check_files repo/deep/deeper2 a
299 test_expect_success 'revert to old sparse-checkout on empty update' '
300 git init empty-test &&
304 git commit -m "test" &&
305 test_must_fail git sparse-checkout set nothing 2>err &&
306 test_i18ngrep "Sparse checkout leaves no entry on working directory" err &&
307 test_i18ngrep ! ".git/index.lock" err &&
308 git sparse-checkout set file
312 test_expect_success 'fail when lock is taken' '
313 test_when_finished rm -rf repo/.git/info/sparse-checkout.lock &&
314 touch repo/.git/info/sparse-checkout.lock &&
315 test_must_fail git -C repo sparse-checkout set deep 2>err &&
316 test_i18ngrep "Unable to create .*\.lock" err
319 test_expect_success '.gitignore should not warn about cone mode' '
320 git -C repo config --worktree core.sparseCheckoutCone true &&
321 echo "**/bin/*" >repo/.gitignore &&
322 git -C repo reset --hard 2>err &&
323 test_i18ngrep ! "disabling cone patterns" err
326 test_expect_success 'sparse-checkout (init|set|disable) warns with dirty status' '
327 git clone repo dirty &&
328 echo dirty >dirty/folder1/a &&
330 git -C dirty sparse-checkout init 2>err &&
331 test_i18ngrep "warning.*The following paths are not up to date" err &&
333 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
334 test_i18ngrep "warning.*The following paths are not up to date" err &&
335 test_path_is_file dirty/folder1/a &&
337 git -C dirty sparse-checkout disable 2>err &&
338 test_must_be_empty err &&
340 git -C dirty reset --hard &&
341 git -C dirty sparse-checkout init &&
342 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
343 test_path_is_missing dirty/folder1/a &&
344 git -C dirty sparse-checkout disable &&
345 test_path_is_file dirty/folder1/a
348 test_expect_success 'sparse-checkout (init|set|disable) warns with unmerged status' '
349 git clone repo unmerged &&
352 0 0000000000000000000000000000000000000000 folder1/a
353 100644 $(git -C unmerged rev-parse HEAD:folder1/a) 1 folder1/a
355 git -C unmerged update-index --index-info <input &&
357 git -C unmerged sparse-checkout init 2>err &&
358 test_i18ngrep "warning.*The following paths are unmerged" err &&
360 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
361 test_i18ngrep "warning.*The following paths are unmerged" err &&
362 test_path_is_file dirty/folder1/a &&
364 git -C unmerged sparse-checkout disable 2>err &&
365 test_i18ngrep "warning.*The following paths are unmerged" err &&
367 git -C unmerged reset --hard &&
368 git -C unmerged sparse-checkout init &&
369 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* &&
370 git -C unmerged sparse-checkout disable
373 test_expect_success 'cone mode: set with core.ignoreCase=true' '
374 rm repo/.git/info/sparse-checkout &&
375 git -C repo sparse-checkout init --cone &&
376 git -C repo -c core.ignoreCase=true sparse-checkout set folder1 &&
377 cat >expect <<-\EOF &&
382 test_cmp expect repo/.git/info/sparse-checkout &&
383 check_files repo a folder1
386 test_expect_success 'interaction with submodules' '
387 git clone repo super &&
391 git submodule add ../repo modules/child &&
393 git commit -m "add submodule" &&
394 git sparse-checkout init --cone &&
395 git sparse-checkout set folder1
397 check_files super a folder1 modules &&
398 check_files super/modules/child a deep folder1 folder2
401 test_expect_success 'different sparse-checkouts with worktrees' '
402 git -C repo worktree add --detach ../worktree &&
403 check_files worktree "a deep folder1 folder2" &&
404 git -C worktree sparse-checkout init --cone &&
405 git -C repo sparse-checkout set folder1 &&
406 git -C worktree sparse-checkout set deep/deeper1 &&
407 check_files repo a folder1 &&
408 check_files worktree a deep
411 test_expect_success 'set using filename keeps file on-disk' '
412 git -C repo sparse-checkout set a deep &&
413 cat >expect <<-\EOF &&
419 test_cmp expect repo/.git/info/sparse-checkout &&
420 check_files repo a deep
423 check_read_tree_errors () {
427 git -C $REPO -c core.sparseCheckoutCone=false read-tree -mu HEAD 2>err &&
428 test_must_be_empty err &&
429 check_files $REPO "$FILES" &&
430 git -C $REPO read-tree -mu HEAD 2>err &&
433 test_must_be_empty err
435 test_i18ngrep "$ERRORS" err
437 check_files $REPO $FILES
440 test_expect_success 'pattern-checks: /A/**' '
441 cat >repo/.git/info/sparse-checkout <<-\EOF &&
446 check_read_tree_errors repo "a folder1" "disabling cone pattern matching"
449 test_expect_success 'pattern-checks: /A/**/B/' '
450 cat >repo/.git/info/sparse-checkout <<-\EOF &&
455 check_read_tree_errors repo "a deep" "disabling cone pattern matching" &&
456 check_files repo/deep "deeper1" &&
457 check_files repo/deep/deeper1 "deepest"
460 test_expect_success 'pattern-checks: too short' '
461 cat >repo/.git/info/sparse-checkout <<-\EOF &&
466 check_read_tree_errors repo "a" "disabling cone pattern matching"
468 test_expect_success 'pattern-checks: not too short' '
469 cat >repo/.git/info/sparse-checkout <<-\EOF &&
474 git -C repo read-tree -mu HEAD 2>err &&
475 test_must_be_empty err &&
479 test_expect_success 'pattern-checks: trailing "*"' '
480 cat >repo/.git/info/sparse-checkout <<-\EOF &&
485 check_read_tree_errors repo "a" "disabling cone pattern matching"
488 test_expect_success 'pattern-checks: starting "*"' '
489 cat >repo/.git/info/sparse-checkout <<-\EOF &&
494 check_read_tree_errors repo "a deep" "disabling cone pattern matching"
497 test_expect_success 'pattern-checks: contained glob characters' '
498 for c in "[a]" "\\" "?" "*"
500 cat >repo/.git/info/sparse-checkout <<-EOF &&
505 check_read_tree_errors repo "a" "disabling cone pattern matching"
509 test_expect_success BSLASHPSPEC 'pattern-checks: escaped characters' '
510 git clone repo escaped &&
511 TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
512 NEWTREE=$(git -C escaped mktree <<-EOF
513 $(git -C escaped ls-tree HEAD)
514 040000 tree $TREEOID zbad\\dir
515 040000 tree $TREEOID zdoes*exist
516 040000 tree $TREEOID zglob[!a]?
519 COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
520 git -C escaped reset --hard $COMMIT &&
521 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
522 git -C escaped sparse-checkout init --cone &&
523 git -C escaped sparse-checkout set zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" &&
524 cat >expect <<-\EOF &&
534 test_cmp expect escaped/.git/info/sparse-checkout &&
535 check_read_tree_errors escaped "a zbad\\dir zdoes*exist zglob[!a]?" &&
536 git -C escaped ls-tree -d --name-only HEAD >list-expect &&
537 git -C escaped sparse-checkout set --stdin <list-expect &&
538 cat >expect <<-\EOF &&
548 test_cmp expect escaped/.git/info/sparse-checkout &&
549 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
550 git -C escaped sparse-checkout list >list-actual &&
551 test_cmp list-expect list-actual
554 test_expect_success MINGW 'cone mode replaces backslashes with slashes' '
555 git -C repo sparse-checkout set deep\\deeper1 &&
556 cat >expect <<-\EOF &&
563 test_cmp expect repo/.git/info/sparse-checkout &&
564 check_files repo a deep &&
565 check_files repo/deep a deeper1