blob: 9a900310186d3876f873d5da5ff94dca4a3da35c [file] [log] [blame]
Derrick Stolee94c09562019-11-21 22:04:33 +00001#!/bin/sh
2
3test_description='sparse checkout builtin tests'
4
Johannes Schindelin06d53142020-11-18 23:44:21 +00005GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00006export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
Johannes Schindelinac873c22022-01-19 17:29:38 +00008GIT_TEST_SPLIT_INDEX=false
9export GIT_TEST_SPLIT_INDEX
10
Derrick Stolee94c09562019-11-21 22:04:33 +000011. ./test-lib.sh
12
Ed Maste761e3d22019-12-20 19:41:14 +000013list_files() {
14 # Do not replace this with 'ls "$1"', as "ls" with BSD-lineage
15 # enables "-A" by default for root and ends up including ".git" and
16 # such in its output. (Note, though, that running the test suite as
17 # root is generally not recommended.)
18 (cd "$1" && printf '%s\n' *)
19}
20
Derrick Stolee522e6412020-01-24 21:19:31 +000021check_files() {
22 list_files "$1" >actual &&
23 shift &&
24 printf "%s\n" $@ >expect &&
25 test_cmp expect actual
26}
27
Derrick Stolee94c09562019-11-21 22:04:33 +000028test_expect_success 'setup' '
29 git init repo &&
30 (
31 cd repo &&
32 echo "initial" >a &&
33 mkdir folder1 folder2 deep &&
34 mkdir deep/deeper1 deep/deeper2 &&
35 mkdir deep/deeper1/deepest &&
36 cp a folder1 &&
37 cp a folder2 &&
38 cp a deep &&
39 cp a deep/deeper1 &&
40 cp a deep/deeper2 &&
41 cp a deep/deeper1/deepest &&
42 git add . &&
43 git commit -m "initial commit"
44 )
45'
46
Elijah Newren45c5e472021-12-14 04:09:05 +000047test_expect_success 'git sparse-checkout list (not sparse)' '
48 test_must_fail git -C repo sparse-checkout list >list 2>err &&
49 test_must_be_empty list &&
50 test_i18ngrep "this worktree is not sparse" err
51'
52
53test_expect_success 'git sparse-checkout list (not sparse)' '
54 git -C repo sparse-checkout set &&
55 rm repo/.git/info/sparse-checkout &&
Derrick Stolee94c09562019-11-21 22:04:33 +000056 git -C repo sparse-checkout list >list 2>err &&
57 test_must_be_empty list &&
58 test_i18ngrep "this worktree is not sparse (sparse-checkout file may not exist)" err
59'
60
61test_expect_success 'git sparse-checkout list (populated)' '
62 test_when_finished rm -f repo/.git/info/sparse-checkout &&
Derrick Stoleed622c342020-01-24 21:19:32 +000063 cat >repo/.git/info/sparse-checkout <<-\EOF &&
64 /folder1/*
65 /deep/
66 **/a
67 !*bin*
Derrick Stolee94c09562019-11-21 22:04:33 +000068 EOF
69 cp repo/.git/info/sparse-checkout expect &&
70 git -C repo sparse-checkout list >list &&
71 test_cmp expect list
72'
73
Derrick Stoleebab3c352019-11-21 22:04:34 +000074test_expect_success 'git sparse-checkout init' '
75 git -C repo sparse-checkout init &&
Derrick Stoleed622c342020-01-24 21:19:32 +000076 cat >expect <<-\EOF &&
77 /*
78 !/*/
Derrick Stoleebab3c352019-11-21 22:04:34 +000079 EOF
80 test_cmp expect repo/.git/info/sparse-checkout &&
81 test_cmp_config -C repo true core.sparsecheckout &&
Derrick Stolee522e6412020-01-24 21:19:31 +000082 check_files repo a
Derrick Stoleebab3c352019-11-21 22:04:34 +000083'
84
Jonathan Tan7f448422022-01-21 09:44:41 -080085test_expect_success 'git sparse-checkout init in empty repo' '
86 test_when_finished rm -rf empty-repo blank-template &&
87 git init --template= empty-repo &&
88 git -C empty-repo sparse-checkout init
89'
90
Derrick Stoleebab3c352019-11-21 22:04:34 +000091test_expect_success 'git sparse-checkout list after init' '
92 git -C repo sparse-checkout list >actual &&
Derrick Stoleed622c342020-01-24 21:19:32 +000093 cat >expect <<-\EOF &&
94 /*
95 !/*/
Derrick Stoleebab3c352019-11-21 22:04:34 +000096 EOF
97 test_cmp expect actual
98'
99
100test_expect_success 'init with existing sparse-checkout' '
101 echo "*folder*" >> repo/.git/info/sparse-checkout &&
102 git -C repo sparse-checkout init &&
Derrick Stoleed622c342020-01-24 21:19:32 +0000103 cat >expect <<-\EOF &&
104 /*
105 !/*/
106 *folder*
Derrick Stoleebab3c352019-11-21 22:04:34 +0000107 EOF
108 test_cmp expect repo/.git/info/sparse-checkout &&
Derrick Stolee522e6412020-01-24 21:19:31 +0000109 check_files repo a folder1 folder2
Derrick Stoleebab3c352019-11-21 22:04:34 +0000110'
111
Derrick Stoleed89f09c2019-11-21 22:04:35 +0000112test_expect_success 'clone --sparse' '
Derrick Stolee47dbf102020-01-24 21:19:34 +0000113 git clone --sparse "file://$(pwd)/repo" clone &&
Derrick Stoleed89f09c2019-11-21 22:04:35 +0000114 git -C clone sparse-checkout list >actual &&
Derrick Stoleed622c342020-01-24 21:19:32 +0000115 cat >expect <<-\EOF &&
116 /*
117 !/*/
Derrick Stoleed89f09c2019-11-21 22:04:35 +0000118 EOF
119 test_cmp expect actual &&
Derrick Stolee522e6412020-01-24 21:19:31 +0000120 check_files clone a
Derrick Stoleed89f09c2019-11-21 22:04:35 +0000121'
122
Derrick Stolee391c3a12021-12-16 16:13:41 +0000123test_expect_success 'switching to cone mode with non-cone mode patterns' '
124 git init bad-patterns &&
125 (
126 cd bad-patterns &&
127 git sparse-checkout init &&
128 git sparse-checkout add dir &&
Derrick Stolee7316dc52022-02-07 21:33:01 +0000129 git config --worktree core.sparseCheckoutCone true &&
Derrick Stoleea3eca582021-12-16 16:13:42 +0000130 test_must_fail git sparse-checkout add dir 2>err &&
131 grep "existing sparse-checkout patterns do not use cone mode" err
Derrick Stolee391c3a12021-12-16 16:13:41 +0000132 )
133'
134
Elijah Newrenb5bfc082020-06-05 02:41:39 +0000135test_expect_success 'interaction with clone --no-checkout (unborn index)' '
136 git clone --no-checkout "file://$(pwd)/repo" clone_no_checkout &&
137 git -C clone_no_checkout sparse-checkout init --cone &&
138 git -C clone_no_checkout sparse-checkout set folder1 &&
139
140 git -C clone_no_checkout sparse-checkout list >actual &&
141 cat >expect <<-\EOF &&
142 folder1
143 EOF
144 test_cmp expect actual &&
145
146 # nothing checked out, expect "No such file or directory"
147 ! ls clone_no_checkout/* >actual &&
148 test_must_be_empty actual &&
149 test_path_is_missing clone_no_checkout/.git/index &&
150
151 # No branch is checked out until we manually switch to one
Johannes Schindelin06d53142020-11-18 23:44:21 +0000152 git -C clone_no_checkout switch main &&
Elijah Newrenb5bfc082020-06-05 02:41:39 +0000153 test_path_is_file clone_no_checkout/.git/index &&
154 check_files clone_no_checkout a folder1
155'
156
Derrick Stoleef6039a92019-11-21 22:04:36 +0000157test_expect_success 'set enables config' '
Derrick Stolee53255912022-02-07 21:33:02 +0000158 git init worktree-config &&
Derrick Stoleef6039a92019-11-21 22:04:36 +0000159 (
Derrick Stolee53255912022-02-07 21:33:02 +0000160 cd worktree-config &&
Derrick Stoleef6039a92019-11-21 22:04:36 +0000161 test_commit test file &&
162 test_path_is_missing .git/config.worktree &&
Derrick Stoleeace224a2020-05-04 18:27:43 +0000163 git sparse-checkout set nothing &&
Derrick Stoleef6039a92019-11-21 22:04:36 +0000164 test_path_is_file .git/config.worktree &&
Derrick Stoleef6039a92019-11-21 22:04:36 +0000165 test_cmp_config true core.sparseCheckout
166 )
167'
168
169test_expect_success 'set sparse-checkout using builtin' '
170 git -C repo sparse-checkout set "/*" "!/*/" "*folder*" &&
Derrick Stoleed622c342020-01-24 21:19:32 +0000171 cat >expect <<-\EOF &&
172 /*
173 !/*/
174 *folder*
Derrick Stoleef6039a92019-11-21 22:04:36 +0000175 EOF
176 git -C repo sparse-checkout list >actual &&
177 test_cmp expect actual &&
178 test_cmp expect repo/.git/info/sparse-checkout &&
Derrick Stolee522e6412020-01-24 21:19:31 +0000179 check_files repo a folder1 folder2
Derrick Stoleef6039a92019-11-21 22:04:36 +0000180'
181
Derrick Stolee7bffca92019-11-21 22:04:37 +0000182test_expect_success 'set sparse-checkout using --stdin' '
Derrick Stoleed622c342020-01-24 21:19:32 +0000183 cat >expect <<-\EOF &&
184 /*
185 !/*/
186 /folder1/
187 /folder2/
Derrick Stolee7bffca92019-11-21 22:04:37 +0000188 EOF
189 git -C repo sparse-checkout set --stdin <expect &&
190 git -C repo sparse-checkout list >actual &&
191 test_cmp expect actual &&
192 test_cmp expect repo/.git/info/sparse-checkout &&
Derrick Stolee522e6412020-01-24 21:19:31 +0000193 check_files repo "a folder1 folder2"
Derrick Stolee7bffca92019-11-21 22:04:37 +0000194'
195
Derrick Stolee2631dc82020-02-11 15:02:23 +0000196test_expect_success 'add to sparse-checkout' '
Derrick Stoleea3eca582021-12-16 16:13:42 +0000197 cat repo/.git/info/sparse-checkout >old &&
198 test_when_finished cp old repo/.git/info/sparse-checkout &&
Derrick Stolee2631dc82020-02-11 15:02:23 +0000199 cat >add <<-\EOF &&
200 pattern1
201 /folder1/
202 pattern2
203 EOF
Derrick Stoleea3eca582021-12-16 16:13:42 +0000204 cat old >expect &&
Derrick Stolee2631dc82020-02-11 15:02:23 +0000205 cat add >>expect &&
206 git -C repo sparse-checkout add --stdin <add &&
207 git -C repo sparse-checkout list >actual &&
208 test_cmp expect actual &&
209 test_cmp expect repo/.git/info/sparse-checkout &&
210 check_files repo "a folder1 folder2"
211'
212
Derrick Stolee53255912022-02-07 21:33:02 +0000213test_expect_success 'worktree: add copies sparse-checkout patterns' '
214 cat repo/.git/info/sparse-checkout >old &&
215 test_when_finished cp old repo/.git/info/sparse-checkout &&
216 test_when_finished git -C repo worktree remove ../worktree &&
217 git -C repo sparse-checkout set --no-cone "/*" &&
218 git -C repo worktree add --quiet ../worktree 2>err &&
219 test_must_be_empty err &&
220 new="$(git -C worktree rev-parse --git-path info/sparse-checkout)" &&
221 test_path_is_file "$new" &&
222 test_cmp repo/.git/info/sparse-checkout "$new" &&
223 git -C worktree sparse-checkout set --cone &&
224 test_cmp_config -C worktree true core.sparseCheckoutCone &&
225 test_must_fail git -C repo core.sparseCheckoutCone
226'
227
Derrick Stolee879321e2019-11-21 22:04:40 +0000228test_expect_success 'cone mode: match patterns' '
229 git -C repo config --worktree core.sparseCheckoutCone true &&
230 rm -rf repo/a repo/folder1 repo/folder2 &&
Derrick Stolee96cc8ab2019-11-21 22:04:41 +0000231 git -C repo read-tree -mu HEAD 2>err &&
232 test_i18ngrep ! "disabling cone patterns" err &&
Derrick Stolee879321e2019-11-21 22:04:40 +0000233 git -C repo reset --hard &&
Derrick Stolee522e6412020-01-24 21:19:31 +0000234 check_files repo a folder1 folder2
Derrick Stolee879321e2019-11-21 22:04:40 +0000235'
236
Derrick Stolee96cc8ab2019-11-21 22:04:41 +0000237test_expect_success 'cone mode: warn on bad pattern' '
238 test_when_finished mv sparse-checkout repo/.git/info/ &&
239 cp repo/.git/info/sparse-checkout . &&
240 echo "!/deep/deeper/*" >>repo/.git/info/sparse-checkout &&
241 git -C repo read-tree -mu HEAD 2>err &&
242 test_i18ngrep "unrecognized negative pattern" err
243'
244
Derrick Stolee72918c12019-11-21 22:04:38 +0000245test_expect_success 'sparse-checkout disable' '
Derrick Stolee99dfa6f2019-11-21 22:04:47 +0000246 test_when_finished rm -rf repo/.git/info/sparse-checkout &&
Derrick Stolee72918c12019-11-21 22:04:38 +0000247 git -C repo sparse-checkout disable &&
Derrick Stolee99dfa6f2019-11-21 22:04:47 +0000248 test_path_is_file repo/.git/info/sparse-checkout &&
Derrick Stolee72918c12019-11-21 22:04:38 +0000249 git -C repo config --list >config &&
250 test_must_fail git config core.sparseCheckout &&
Derrick Stolee522e6412020-01-24 21:19:31 +0000251 check_files repo a deep folder1 folder2
Derrick Stolee72918c12019-11-21 22:04:38 +0000252'
253
Derrick Stoleedcc5fd52021-03-30 13:11:01 +0000254test_expect_success 'sparse-index enabled and disabled' '
Johannes Schindelinac873c22022-01-19 17:29:38 +0000255 git -C repo sparse-checkout init --cone --sparse-index &&
256 test_cmp_config -C repo true index.sparse &&
257 git -C repo ls-files --sparse >sparse &&
258 git -C repo sparse-checkout disable &&
259 git -C repo ls-files --sparse >full &&
Derrick Stoleedcc5fd52021-03-30 13:11:01 +0000260
Johannes Schindelinac873c22022-01-19 17:29:38 +0000261 cat >expect <<-\EOF &&
262 @@ -1,4 +1,7 @@
263 a
264 -deep/
265 -folder1/
266 -folder2/
267 +deep/a
268 +deep/deeper1/a
269 +deep/deeper1/deepest/a
270 +deep/deeper2/a
271 +folder1/a
272 +folder2/a
273 EOF
Derrick Stoleec2a29402021-12-22 14:20:55 +0000274
Johannes Schindelinac873c22022-01-19 17:29:38 +0000275 diff -u sparse full | tail -n +3 >actual &&
276 test_cmp expect actual &&
Derrick Stoleec2a29402021-12-22 14:20:55 +0000277
Johannes Schindelinac873c22022-01-19 17:29:38 +0000278 git -C repo config --list >config &&
Junio C Hamano6249ce22022-02-25 15:47:33 -0800279 test_cmp_config -C repo false index.sparse
Derrick Stoleedcc5fd52021-03-30 13:11:01 +0000280'
281
Derrick Stoleeaf09ce22019-11-21 22:04:42 +0000282test_expect_success 'cone mode: init and set' '
283 git -C repo sparse-checkout init --cone &&
284 git -C repo config --list >config &&
285 test_i18ngrep "core.sparsecheckoutcone=true" config &&
Ed Maste761e3d22019-12-20 19:41:14 +0000286 list_files repo >dir &&
Derrick Stoleeaf09ce22019-11-21 22:04:42 +0000287 echo a >expect &&
288 test_cmp expect dir &&
289 git -C repo sparse-checkout set deep/deeper1/deepest/ 2>err &&
290 test_must_be_empty err &&
Derrick Stolee522e6412020-01-24 21:19:31 +0000291 check_files repo a deep &&
292 check_files repo/deep a deeper1 &&
293 check_files repo/deep/deeper1 a deepest &&
Derrick Stoleed622c342020-01-24 21:19:32 +0000294 cat >expect <<-\EOF &&
295 /*
296 !/*/
297 /deep/
298 !/deep/*/
299 /deep/deeper1/
300 !/deep/deeper1/*/
301 /deep/deeper1/deepest/
Derrick Stoleeaf09ce22019-11-21 22:04:42 +0000302 EOF
303 test_cmp expect repo/.git/info/sparse-checkout &&
Derrick Stoleed622c342020-01-24 21:19:32 +0000304 git -C repo sparse-checkout set --stdin 2>err <<-\EOF &&
305 folder1
306 folder2
Derrick Stoleeaf09ce22019-11-21 22:04:42 +0000307 EOF
308 test_must_be_empty err &&
Derrick Stolee522e6412020-01-24 21:19:31 +0000309 check_files repo a folder1 folder2
Derrick Stoleeaf09ce22019-11-21 22:04:42 +0000310'
311
Derrick Stoleede119512019-12-30 15:33:12 +0000312test_expect_success 'cone mode: list' '
Derrick Stoleed622c342020-01-24 21:19:32 +0000313 cat >expect <<-\EOF &&
314 folder1
315 folder2
Derrick Stoleede119512019-12-30 15:33:12 +0000316 EOF
317 git -C repo sparse-checkout set --stdin <expect &&
318 git -C repo sparse-checkout list >actual 2>err &&
319 test_must_be_empty err &&
320 test_cmp expect actual
321'
322
Derrick Stoleee9de4872019-11-21 22:04:45 +0000323test_expect_success 'cone mode: set with nested folders' '
324 git -C repo sparse-checkout set deep deep/deeper1/deepest 2>err &&
325 test_line_count = 0 err &&
Derrick Stoleed622c342020-01-24 21:19:32 +0000326 cat >expect <<-\EOF &&
327 /*
328 !/*/
329 /deep/
Derrick Stoleee9de4872019-11-21 22:04:45 +0000330 EOF
331 test_cmp repo/.git/info/sparse-checkout expect
332'
333
Derrick Stolee2631dc82020-02-11 15:02:23 +0000334test_expect_success 'cone mode: add independent path' '
335 git -C repo sparse-checkout set deep/deeper1 &&
336 git -C repo sparse-checkout add folder1 &&
337 cat >expect <<-\EOF &&
338 /*
339 !/*/
340 /deep/
341 !/deep/*/
342 /deep/deeper1/
343 /folder1/
344 EOF
345 test_cmp expect repo/.git/info/sparse-checkout &&
346 check_files repo a deep folder1
347'
348
349test_expect_success 'cone mode: add sibling path' '
350 git -C repo sparse-checkout set deep/deeper1 &&
351 git -C repo sparse-checkout add deep/deeper2 &&
352 cat >expect <<-\EOF &&
353 /*
354 !/*/
355 /deep/
356 !/deep/*/
357 /deep/deeper1/
358 /deep/deeper2/
359 EOF
360 test_cmp expect repo/.git/info/sparse-checkout &&
361 check_files repo a deep
362'
363
364test_expect_success 'cone mode: add parent path' '
365 git -C repo sparse-checkout set deep/deeper1 folder1 &&
366 git -C repo sparse-checkout add deep &&
367 cat >expect <<-\EOF &&
368 /*
369 !/*/
370 /deep/
371 /folder1/
372 EOF
373 test_cmp expect repo/.git/info/sparse-checkout &&
374 check_files repo a deep folder1
375'
376
Elijah Newrenf56f31a2020-03-27 00:48:53 +0000377test_expect_success 'not-up-to-date does not block rest of sparsification' '
Elijah Newren72064ee2020-03-27 00:48:48 +0000378 test_when_finished git -C repo sparse-checkout disable &&
Derrick Stoleecff4e912019-11-21 22:04:51 +0000379 test_when_finished git -C repo reset --hard &&
Derrick Stolee2631dc82020-02-11 15:02:23 +0000380 git -C repo sparse-checkout set deep &&
Elijah Newrenf56f31a2020-03-27 00:48:53 +0000381
Derrick Stoleee0912282019-11-21 22:04:46 +0000382 echo update >repo/deep/deeper2/a &&
383 cp repo/.git/info/sparse-checkout expect &&
Elijah Newrenf56f31a2020-03-27 00:48:53 +0000384 test_write_lines "!/deep/*/" "/deep/deeper1/" >>expect &&
385
386 git -C repo sparse-checkout set deep/deeper1 2>err &&
387
Elijah Newren22ab0b32020-03-27 00:48:58 +0000388 test_i18ngrep "The following paths are not up to date" err &&
Elijah Newrenf56f31a2020-03-27 00:48:53 +0000389 test_cmp expect repo/.git/info/sparse-checkout &&
390 check_files repo/deep a deeper1 deeper2 &&
391 check_files repo/deep/deeper1 a deepest &&
392 check_files repo/deep/deeper1/deepest a &&
393 check_files repo/deep/deeper2 a
Derrick Stoleee0912282019-11-21 22:04:46 +0000394'
395
396test_expect_success 'revert to old sparse-checkout on empty update' '
397 git init empty-test &&
398 (
399 echo >file &&
400 git add file &&
401 git commit -m "test" &&
Derrick Stoleeace224a2020-05-04 18:27:43 +0000402 git sparse-checkout set nothing 2>err &&
403 test_i18ngrep ! "Sparse checkout leaves no entry on working directory" err &&
Derrick Stoleee0912282019-11-21 22:04:46 +0000404 test_i18ngrep ! ".git/index.lock" err &&
405 git sparse-checkout set file
406 )
407'
408
Derrick Stoleefb10ca52019-11-21 22:04:48 +0000409test_expect_success 'fail when lock is taken' '
410 test_when_finished rm -rf repo/.git/info/sparse-checkout.lock &&
411 touch repo/.git/info/sparse-checkout.lock &&
412 test_must_fail git -C repo sparse-checkout set deep 2>err &&
Martin Ã…gren4605a732020-03-08 09:46:27 +0100413 test_i18ngrep "Unable to create .*\.lock" err
Derrick Stoleefb10ca52019-11-21 22:04:48 +0000414'
415
Derrick Stoleef75a69f2019-11-21 22:04:49 +0000416test_expect_success '.gitignore should not warn about cone mode' '
417 git -C repo config --worktree core.sparseCheckoutCone true &&
418 echo "**/bin/*" >repo/.gitignore &&
419 git -C repo reset --hard 2>err &&
420 test_i18ngrep ! "disabling cone patterns" err
421'
422
Elijah Newrenf56f31a2020-03-27 00:48:53 +0000423test_expect_success 'sparse-checkout (init|set|disable) warns with dirty status' '
Derrick Stoleecff4e912019-11-21 22:04:51 +0000424 git clone repo dirty &&
425 echo dirty >dirty/folder1/a &&
Elijah Newrenf56f31a2020-03-27 00:48:53 +0000426
427 git -C dirty sparse-checkout init 2>err &&
Elijah Newren22ab0b32020-03-27 00:48:58 +0000428 test_i18ngrep "warning.*The following paths are not up to date" err &&
Elijah Newrenf56f31a2020-03-27 00:48:53 +0000429
430 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
Elijah Newren22ab0b32020-03-27 00:48:58 +0000431 test_i18ngrep "warning.*The following paths are not up to date" err &&
Elijah Newrenf56f31a2020-03-27 00:48:53 +0000432 test_path_is_file dirty/folder1/a &&
433
434 git -C dirty sparse-checkout disable 2>err &&
435 test_must_be_empty err &&
436
Derrick Stoleecff4e912019-11-21 22:04:51 +0000437 git -C dirty reset --hard &&
438 git -C dirty sparse-checkout init &&
439 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
Elijah Newrenf56f31a2020-03-27 00:48:53 +0000440 test_path_is_missing dirty/folder1/a &&
441 git -C dirty sparse-checkout disable &&
442 test_path_is_file dirty/folder1/a
Derrick Stoleecff4e912019-11-21 22:04:51 +0000443'
444
Elijah Newrenebb568b2020-03-27 00:48:59 +0000445test_expect_success 'sparse-checkout (init|set|disable) warns with unmerged status' '
446 git clone repo unmerged &&
447
448 cat >input <<-EOF &&
brian m. carlsond827bce2020-07-29 23:13:51 +0000449 0 $ZERO_OID folder1/a
Elijah Newrenebb568b2020-03-27 00:48:59 +0000450 100644 $(git -C unmerged rev-parse HEAD:folder1/a) 1 folder1/a
451 EOF
452 git -C unmerged update-index --index-info <input &&
453
454 git -C unmerged sparse-checkout init 2>err &&
455 test_i18ngrep "warning.*The following paths are unmerged" err &&
456
457 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
458 test_i18ngrep "warning.*The following paths are unmerged" err &&
459 test_path_is_file dirty/folder1/a &&
460
461 git -C unmerged sparse-checkout disable 2>err &&
462 test_i18ngrep "warning.*The following paths are unmerged" err &&
463
464 git -C unmerged reset --hard &&
465 git -C unmerged sparse-checkout init &&
466 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* &&
467 git -C unmerged sparse-checkout disable
468'
469
Derrick Stolee49fdd512021-09-24 15:39:07 +0000470test_expect_failure 'sparse-checkout reapply' '
Elijah Newren5644ca22020-03-27 00:49:01 +0000471 git clone repo tweak &&
472
473 echo dirty >tweak/deep/deeper2/a &&
474
475 cat >input <<-EOF &&
brian m. carlsond827bce2020-07-29 23:13:51 +0000476 0 $ZERO_OID folder1/a
Elijah Newren5644ca22020-03-27 00:49:01 +0000477 100644 $(git -C tweak rev-parse HEAD:folder1/a) 1 folder1/a
478 EOF
479 git -C tweak update-index --index-info <input &&
480
481 git -C tweak sparse-checkout init --cone 2>err &&
482 test_i18ngrep "warning.*The following paths are not up to date" err &&
483 test_i18ngrep "warning.*The following paths are unmerged" err &&
484
485 git -C tweak sparse-checkout set folder2 deep/deeper1 2>err &&
486 test_i18ngrep "warning.*The following paths are not up to date" err &&
487 test_i18ngrep "warning.*The following paths are unmerged" err &&
488
489 git -C tweak sparse-checkout reapply 2>err &&
490 test_i18ngrep "warning.*The following paths are not up to date" err &&
491 test_path_is_file tweak/deep/deeper2/a &&
492 test_i18ngrep "warning.*The following paths are unmerged" err &&
493 test_path_is_file tweak/folder1/a &&
494
495 git -C tweak checkout HEAD deep/deeper2/a &&
496 git -C tweak sparse-checkout reapply 2>err &&
497 test_i18ngrep ! "warning.*The following paths are not up to date" err &&
498 test_path_is_missing tweak/deep/deeper2/a &&
499 test_i18ngrep "warning.*The following paths are unmerged" err &&
500 test_path_is_file tweak/folder1/a &&
501
Derrick Stolee49fdd512021-09-24 15:39:07 +0000502 # NEEDSWORK: We are asking to update a file outside of the
503 # sparse-checkout cone, but this is no longer allowed.
Elijah Newren5644ca22020-03-27 00:49:01 +0000504 git -C tweak add folder1/a &&
505 git -C tweak sparse-checkout reapply 2>err &&
506 test_must_be_empty err &&
507 test_path_is_missing tweak/deep/deeper2/a &&
508 test_path_is_missing tweak/folder1/a &&
509
510 git -C tweak sparse-checkout disable
511'
512
Elijah Newrenf7480122022-02-19 16:44:41 +0000513test_expect_success 'reapply can handle config options' '
514 git -C repo sparse-checkout init --cone --no-sparse-index &&
515 git -C repo config --worktree --list >actual &&
516 cat >expect <<-\EOF &&
517 core.sparsecheckout=true
518 core.sparsecheckoutcone=true
Junio C Hamano96717642022-03-06 21:25:30 -0800519 index.sparse=false
Elijah Newrenf7480122022-02-19 16:44:41 +0000520 EOF
521 test_cmp expect actual &&
522
523 git -C repo sparse-checkout reapply --no-cone --no-sparse-index &&
524 git -C repo config --worktree --list >actual &&
525 cat >expect <<-\EOF &&
526 core.sparsecheckout=true
Junio C Hamano96717642022-03-06 21:25:30 -0800527 core.sparsecheckoutcone=false
528 index.sparse=false
Elijah Newrenf7480122022-02-19 16:44:41 +0000529 EOF
530 test_cmp expect actual &&
531
532 git -C repo sparse-checkout reapply --cone --sparse-index &&
533 git -C repo config --worktree --list >actual &&
534 cat >expect <<-\EOF &&
535 core.sparsecheckout=true
536 core.sparsecheckoutcone=true
537 index.sparse=true
538 EOF
539 test_cmp expect actual &&
540
541 git -C repo sparse-checkout disable
542'
543
Derrick Stolee190a65f2019-12-13 18:09:53 +0000544test_expect_success 'cone mode: set with core.ignoreCase=true' '
Elijah Newren72064ee2020-03-27 00:48:48 +0000545 rm repo/.git/info/sparse-checkout &&
Derrick Stolee190a65f2019-12-13 18:09:53 +0000546 git -C repo sparse-checkout init --cone &&
547 git -C repo -c core.ignoreCase=true sparse-checkout set folder1 &&
Derrick Stoleed622c342020-01-24 21:19:32 +0000548 cat >expect <<-\EOF &&
549 /*
550 !/*/
551 /folder1/
Derrick Stolee190a65f2019-12-13 18:09:53 +0000552 EOF
553 test_cmp expect repo/.git/info/sparse-checkout &&
Derrick Stolee522e6412020-01-24 21:19:31 +0000554 check_files repo a folder1
Derrick Stolee190a65f2019-12-13 18:09:53 +0000555'
556
Derrick Stolee4fd683b2019-12-30 15:33:13 +0000557test_expect_success 'interaction with submodules' '
558 git clone repo super &&
559 (
560 cd super &&
561 mkdir modules &&
562 git submodule add ../repo modules/child &&
563 git add . &&
564 git commit -m "add submodule" &&
565 git sparse-checkout init --cone &&
566 git sparse-checkout set folder1
567 ) &&
Derrick Stolee522e6412020-01-24 21:19:31 +0000568 check_files super a folder1 modules &&
569 check_files super/modules/child a deep folder1 folder2
Derrick Stolee4fd683b2019-12-30 15:33:13 +0000570'
571
Derrick Stolee3c754062020-01-24 21:19:33 +0000572test_expect_success 'different sparse-checkouts with worktrees' '
Derrick Stolee53255912022-02-07 21:33:02 +0000573 git -C repo sparse-checkout set --cone deep folder1 &&
Derrick Stolee3c754062020-01-24 21:19:33 +0000574 git -C repo worktree add --detach ../worktree &&
Derrick Stolee53255912022-02-07 21:33:02 +0000575 check_files worktree "a deep folder1" &&
576 git -C repo sparse-checkout set --cone folder1 &&
577 git -C worktree sparse-checkout set --cone deep/deeper1 &&
578 check_files repo "a folder1" &&
579 check_files worktree "a deep"
Derrick Stolee3c754062020-01-24 21:19:33 +0000580'
581
Derrick Stoleef998a3f2020-01-31 20:16:15 +0000582test_expect_success 'set using filename keeps file on-disk' '
Elijah Newren4ce50432022-02-19 16:44:44 +0000583 git -C repo sparse-checkout set --skip-checks a deep &&
Derrick Stoleef998a3f2020-01-31 20:16:15 +0000584 cat >expect <<-\EOF &&
585 /*
586 !/*/
587 /a/
588 /deep/
589 EOF
590 test_cmp expect repo/.git/info/sparse-checkout &&
591 check_files repo a deep
592'
593
Derrick Stolee41de0c62020-01-24 21:19:36 +0000594check_read_tree_errors () {
595 REPO=$1
596 FILES=$2
597 ERRORS=$3
Derrick Stoleed585f0e2020-01-31 20:16:10 +0000598 git -C $REPO -c core.sparseCheckoutCone=false read-tree -mu HEAD 2>err &&
599 test_must_be_empty err &&
600 check_files $REPO "$FILES" &&
Derrick Stolee41de0c62020-01-24 21:19:36 +0000601 git -C $REPO read-tree -mu HEAD 2>err &&
602 if test -z "$ERRORS"
603 then
604 test_must_be_empty err
605 else
606 test_i18ngrep "$ERRORS" err
607 fi &&
608 check_files $REPO $FILES
609}
610
611test_expect_success 'pattern-checks: /A/**' '
612 cat >repo/.git/info/sparse-checkout <<-\EOF &&
613 /*
614 !/*/
615 /folder1/**
616 EOF
617 check_read_tree_errors repo "a folder1" "disabling cone pattern matching"
618'
619
620test_expect_success 'pattern-checks: /A/**/B/' '
621 cat >repo/.git/info/sparse-checkout <<-\EOF &&
622 /*
623 !/*/
624 /deep/**/deepest
625 EOF
626 check_read_tree_errors repo "a deep" "disabling cone pattern matching" &&
627 check_files repo/deep "deeper1" &&
628 check_files repo/deep/deeper1 "deepest"
629'
630
Derrick Stolee9e6d3e62020-01-24 21:19:37 +0000631test_expect_success 'pattern-checks: too short' '
632 cat >repo/.git/info/sparse-checkout <<-\EOF &&
633 /*
634 !/*/
Derrick Stolee6c11c6a2020-02-20 20:07:06 +0000635 /
Derrick Stolee9e6d3e62020-01-24 21:19:37 +0000636 EOF
637 check_read_tree_errors repo "a" "disabling cone pattern matching"
638'
Derrick Stolee6c11c6a2020-02-20 20:07:06 +0000639test_expect_success 'pattern-checks: not too short' '
640 cat >repo/.git/info/sparse-checkout <<-\EOF &&
641 /*
642 !/*/
643 /b/
644 EOF
645 git -C repo read-tree -mu HEAD 2>err &&
646 test_must_be_empty err &&
647 check_files repo a
648'
Derrick Stolee9e6d3e62020-01-24 21:19:37 +0000649
Derrick Stolee9abc60f2020-01-31 20:16:08 +0000650test_expect_success 'pattern-checks: trailing "*"' '
651 cat >repo/.git/info/sparse-checkout <<-\EOF &&
652 /*
653 !/*/
654 /a*
655 EOF
656 check_read_tree_errors repo "a" "disabling cone pattern matching"
657'
658
659test_expect_success 'pattern-checks: starting "*"' '
660 cat >repo/.git/info/sparse-checkout <<-\EOF &&
661 /*
662 !/*/
663 *eep/
664 EOF
665 check_read_tree_errors repo "a deep" "disabling cone pattern matching"
666'
667
668test_expect_success 'pattern-checks: contained glob characters' '
669 for c in "[a]" "\\" "?" "*"
670 do
671 cat >repo/.git/info/sparse-checkout <<-EOF &&
672 /*
673 !/*/
674 something$c-else/
675 EOF
Eric Sunshinedb5875a2021-12-09 00:11:12 -0500676 check_read_tree_errors repo "a" "disabling cone pattern matching" || return 1
Derrick Stolee9abc60f2020-01-31 20:16:08 +0000677 done
678'
679
Derrick Stoleee53ffe22020-01-31 20:16:13 +0000680test_expect_success BSLASHPSPEC 'pattern-checks: escaped characters' '
Derrick Stolee4f52c2c2020-01-31 20:16:09 +0000681 git clone repo escaped &&
682 TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
683 NEWTREE=$(git -C escaped mktree <<-EOF
684 $(git -C escaped ls-tree HEAD)
685 040000 tree $TREEOID zbad\\dir
686 040000 tree $TREEOID zdoes*exist
Derrick Stoleee53ffe22020-01-31 20:16:13 +0000687 040000 tree $TREEOID zglob[!a]?
Derrick Stolee4f52c2c2020-01-31 20:16:09 +0000688 EOF
689 ) &&
690 COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
691 git -C escaped reset --hard $COMMIT &&
Derrick Stoleee53ffe22020-01-31 20:16:13 +0000692 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
Derrick Stolee4f52c2c2020-01-31 20:16:09 +0000693 git -C escaped sparse-checkout init --cone &&
Elijah Newren8dd7c472022-02-19 16:44:45 +0000694 git -C escaped sparse-checkout set --skip-checks zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" &&
Derrick Stoleed585f0e2020-01-31 20:16:10 +0000695 cat >expect <<-\EOF &&
Derrick Stolee9abc60f2020-01-31 20:16:08 +0000696 /*
697 !/*/
Derrick Stolee4f52c2c2020-01-31 20:16:09 +0000698 /zbad\\dir/
699 !/zbad\\dir/*/
Derrick Stoleed585f0e2020-01-31 20:16:10 +0000700 /zbad\\dir/bogus/
Derrick Stolee4f52c2c2020-01-31 20:16:09 +0000701 /zdoes\*exist/
Derrick Stoleed585f0e2020-01-31 20:16:10 +0000702 /zdoes\*not\*exist/
Derrick Stoleee53ffe22020-01-31 20:16:13 +0000703 /zglob\[!a]\?/
Derrick Stolee9abc60f2020-01-31 20:16:08 +0000704 EOF
Derrick Stoleed585f0e2020-01-31 20:16:10 +0000705 test_cmp expect escaped/.git/info/sparse-checkout &&
Derrick Stoleee53ffe22020-01-31 20:16:13 +0000706 check_read_tree_errors escaped "a zbad\\dir zdoes*exist zglob[!a]?" &&
Derrick Stoleee55682e2020-01-31 20:16:12 +0000707 git -C escaped ls-tree -d --name-only HEAD >list-expect &&
708 git -C escaped sparse-checkout set --stdin <list-expect &&
Derrick Stoleebd64de42020-01-31 20:16:11 +0000709 cat >expect <<-\EOF &&
710 /*
711 !/*/
712 /deep/
713 /folder1/
714 /folder2/
715 /zbad\\dir/
716 /zdoes\*exist/
Derrick Stoleee53ffe22020-01-31 20:16:13 +0000717 /zglob\[!a]\?/
Derrick Stoleebd64de42020-01-31 20:16:11 +0000718 EOF
719 test_cmp expect escaped/.git/info/sparse-checkout &&
Derrick Stoleee53ffe22020-01-31 20:16:13 +0000720 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
Derrick Stoleee55682e2020-01-31 20:16:12 +0000721 git -C escaped sparse-checkout list >list-actual &&
722 test_cmp list-expect list-actual
Derrick Stolee9abc60f2020-01-31 20:16:08 +0000723'
724
Derrick Stoleeef076592020-02-11 15:02:24 +0000725test_expect_success MINGW 'cone mode replaces backslashes with slashes' '
726 git -C repo sparse-checkout set deep\\deeper1 &&
727 cat >expect <<-\EOF &&
728 /*
729 !/*/
730 /deep/
731 !/deep/*/
732 /deep/deeper1/
733 EOF
734 test_cmp expect repo/.git/info/sparse-checkout &&
735 check_files repo a deep &&
736 check_files repo/deep a deeper1
737'
738
Derrick Stolee55dfcf92021-09-08 01:42:33 +0000739test_expect_success 'cone mode clears ignored subdirectories' '
740 rm repo/.git/info/sparse-checkout &&
741
742 git -C repo sparse-checkout init --cone &&
743 git -C repo sparse-checkout set deep/deeper1 &&
744
745 cat >repo/.gitignore <<-\EOF &&
746 obj/
747 *.o
748 EOF
749
750 git -C repo add .gitignore &&
751 git -C repo commit -m ".gitignore" &&
752
753 mkdir -p repo/obj repo/folder1/obj repo/deep/deeper2/obj &&
754 for file in folder1/obj/a obj/a folder1/file.o folder1.o \
755 deep/deeper2/obj/a deep/deeper2/file.o file.o
756 do
757 echo ignored >repo/$file || return 1
758 done &&
759
760 git -C repo status --porcelain=v2 >out &&
761 test_must_be_empty out &&
762
763 git -C repo sparse-checkout reapply &&
764 test_path_is_missing repo/folder1 &&
765 test_path_is_missing repo/deep/deeper2 &&
766 test_path_is_dir repo/obj &&
767 test_path_is_file repo/file.o &&
768
769 git -C repo status --porcelain=v2 >out &&
770 test_must_be_empty out &&
771
772 git -C repo sparse-checkout set deep/deeper2 &&
773 test_path_is_missing repo/deep/deeper1 &&
774 test_path_is_dir repo/deep/deeper2 &&
775 test_path_is_dir repo/obj &&
776 test_path_is_file repo/file.o &&
777
778 >repo/deep/deeper2/ignored.o &&
779 >repo/deep/deeper2/untracked &&
780
781 # When an untracked file is in the way, all untracked files
782 # (even ignored files) are preserved.
783 git -C repo sparse-checkout set folder1 2>err &&
784 grep "contains untracked files" err &&
785 test_path_is_file repo/deep/deeper2/ignored.o &&
786 test_path_is_file repo/deep/deeper2/untracked &&
787
788 # The rest of the cone matches expectation
789 test_path_is_missing repo/deep/deeper1 &&
790 test_path_is_dir repo/obj &&
791 test_path_is_file repo/file.o &&
792
793 git -C repo status --porcelain=v2 >out &&
794 echo "? deep/deeper2/untracked" >expect &&
795 test_cmp expect out
796'
797
Derrick Stoleea481d432021-12-16 16:13:40 +0000798test_expect_success 'malformed cone-mode patterns' '
799 git -C repo sparse-checkout init --cone &&
800 mkdir -p repo/foo/bar &&
801 touch repo/foo/bar/x repo/foo/y &&
802 cat >repo/.git/info/sparse-checkout <<-\EOF &&
803 /*
804 !/*/
805 /foo/
806 !/foo/*/
807 /foo/\*/
808 EOF
809
810 # Listing the patterns will notice the duplicate pattern and
811 # emit a warning. It will list the patterns directly instead
812 # of using the cone-mode translation to a set of directories.
813 git -C repo sparse-checkout list >actual 2>err &&
814 test_cmp repo/.git/info/sparse-checkout actual &&
815 grep "warning: your sparse-checkout file may have issues: pattern .* is repeated" err &&
816 grep "warning: disabling cone pattern matching" err
817'
818
Elijah Newrenbb8b5e92022-02-19 16:44:43 +0000819test_expect_success 'set from subdir pays attention to prefix' '
820 git -C repo sparse-checkout disable &&
821 git -C repo/deep sparse-checkout set --cone deeper2 ../folder1 &&
822
823 git -C repo sparse-checkout list >actual &&
824
825 cat >expect <<-\EOF &&
826 deep/deeper2
827 folder1
828 EOF
829 test_cmp expect actual
830'
831
832test_expect_success 'add from subdir pays attention to prefix' '
833 git -C repo sparse-checkout set --cone deep/deeper2 &&
834 git -C repo/deep sparse-checkout add deeper1/deepest ../folder1 &&
835
836 git -C repo sparse-checkout list >actual &&
837
838 cat >expect <<-\EOF &&
839 deep/deeper1/deepest
840 deep/deeper2
841 folder1
842 EOF
843 test_cmp expect actual
844'
845
846test_expect_success 'set from subdir in non-cone mode throws an error' '
847 git -C repo sparse-checkout disable &&
848 test_must_fail git -C repo/deep sparse-checkout set --no-cone deeper2 ../folder1 2>error &&
849
850 grep "run from the toplevel directory in non-cone mode" error
851'
852
853test_expect_success 'set from subdir in non-cone mode throws an error' '
854 git -C repo sparse-checkout set --no-cone deep/deeper2 &&
855 test_must_fail git -C repo/deep sparse-checkout add deeper1/deepest ../folder1 2>error &&
856
857 grep "run from the toplevel directory in non-cone mode" error
858'
859
Elijah Newren4ce50432022-02-19 16:44:44 +0000860test_expect_success 'by default, cone mode will error out when passed files' '
861 git -C repo sparse-checkout reapply --cone &&
862 test_must_fail git -C repo sparse-checkout add .gitignore 2>error &&
863
864 grep ".gitignore.*is not a directory" error
865'
866
867test_expect_success 'by default, non-cone mode will warn on individual files' '
868 git -C repo sparse-checkout reapply --no-cone &&
869 git -C repo sparse-checkout add .gitignore 2>warning &&
870
871 grep "pass a leading slash before paths.*if you want a single file" warning
872'
873
Derrick Stolee94c09562019-11-21 22:04:33 +0000874test_done