Merge branch 'cp/git-flush-is-an-env-bool'
[alt-git.git] / t / t7002-mv-sparse-checkout.sh
blob26582ae4e5fb5ebb6aebb6239ecd3ab711b119ed
1 #!/bin/sh
3 test_description='git mv in sparse working trees'
5 . ./test-lib.sh
7 setup_sparse_checkout () {
8 mkdir folder1 &&
9 touch folder1/file1 &&
10 git add folder1 &&
11 git sparse-checkout set --cone sub
14 cleanup_sparse_checkout () {
15 git sparse-checkout disable &&
16 git reset --hard
19 test_expect_success 'setup' "
20 mkdir -p sub/dir sub/dir2 &&
21 touch a b c sub/d sub/dir/e sub/dir2/e &&
22 git add -A &&
23 git commit -m files &&
25 cat >sparse_error_header <<-EOF &&
26 The following paths and/or pathspecs matched paths that exist
27 outside of your sparse-checkout definition, so will not be
28 updated in the index:
29 EOF
31 cat >sparse_hint <<-EOF &&
32 hint: If you intend to update such entries, try one of the following:
33 hint: * Use the --sparse option.
34 hint: * Disable or modify the sparsity rules.
35 hint: Disable this message with \"git config advice.updateSparsePath false\"
36 EOF
38 cat >dirty_error_header <<-EOF &&
39 The following paths have been moved outside the
40 sparse-checkout definition but are not sparse due to local
41 modifications.
42 EOF
44 cat >dirty_hint <<-EOF
45 hint: To correct the sparsity of these paths, do the following:
46 hint: * Use \"git add --sparse <paths>\" to update the index
47 hint: * Use \"git sparse-checkout reapply\" to apply the sparsity rules
48 hint: Disable this message with \"git config advice.updateSparsePath false\"
49 EOF
52 test_expect_success 'mv refuses to move sparse-to-sparse' '
53 test_when_finished rm -f e &&
54 git reset --hard &&
55 git sparse-checkout set --no-cone a &&
56 touch b &&
57 test_must_fail git mv b e 2>stderr &&
58 cat sparse_error_header >expect &&
59 echo b >>expect &&
60 echo e >>expect &&
61 cat sparse_hint >>expect &&
62 test_cmp expect stderr &&
63 git mv --sparse b e 2>stderr &&
64 test_must_be_empty stderr
67 test_expect_success 'mv refuses to move sparse-to-sparse, ignores failure' '
68 test_when_finished rm -f b c e &&
69 git reset --hard &&
70 git sparse-checkout set a &&
72 # tracked-to-untracked
73 touch b &&
74 git mv -k b e 2>stderr &&
75 test_path_exists b &&
76 test_path_is_missing e &&
77 cat sparse_error_header >expect &&
78 echo b >>expect &&
79 echo e >>expect &&
80 cat sparse_hint >>expect &&
81 test_cmp expect stderr &&
83 git mv --sparse b e 2>stderr &&
84 test_must_be_empty stderr &&
85 test_path_is_missing b &&
86 test_path_exists e &&
88 # tracked-to-tracked
89 git reset --hard &&
90 touch b &&
91 git mv -k b c 2>stderr &&
92 test_path_exists b &&
93 test_path_is_missing c &&
94 cat sparse_error_header >expect &&
95 echo b >>expect &&
96 echo c >>expect &&
97 cat sparse_hint >>expect &&
98 test_cmp expect stderr &&
100 git mv --sparse b c 2>stderr &&
101 test_must_be_empty stderr &&
102 test_path_is_missing b &&
103 test_path_exists c
106 test_expect_success 'mv refuses to move non-sparse-to-sparse' '
107 test_when_finished rm -f b c e &&
108 git reset --hard &&
109 git sparse-checkout set a &&
111 # tracked-to-untracked
112 test_must_fail git mv a e 2>stderr &&
113 test_path_exists a &&
114 test_path_is_missing e &&
115 cat sparse_error_header >expect &&
116 echo e >>expect &&
117 cat sparse_hint >>expect &&
118 test_cmp expect stderr &&
119 git mv --sparse a e 2>stderr &&
120 test_must_be_empty stderr &&
121 test_path_is_missing a &&
122 test_path_exists e &&
124 # tracked-to-tracked
125 rm e &&
126 git reset --hard &&
127 test_must_fail git mv a c 2>stderr &&
128 test_path_exists a &&
129 test_path_is_missing c &&
130 cat sparse_error_header >expect &&
131 echo c >>expect &&
132 cat sparse_hint >>expect &&
133 test_cmp expect stderr &&
134 git mv --sparse a c 2>stderr &&
135 test_must_be_empty stderr &&
136 test_path_is_missing a &&
137 test_path_exists c
140 test_expect_success 'mv refuses to move sparse-to-non-sparse' '
141 test_when_finished rm -f b c e &&
142 git reset --hard &&
143 git sparse-checkout set a e &&
145 # tracked-to-untracked
146 touch b &&
147 test_must_fail git mv b e 2>stderr &&
148 cat sparse_error_header >expect &&
149 echo b >>expect &&
150 cat sparse_hint >>expect &&
151 test_cmp expect stderr &&
152 git mv --sparse b e 2>stderr &&
153 test_must_be_empty stderr
156 test_expect_success 'recursive mv refuses to move (possible) sparse' '
157 test_when_finished rm -rf b c e sub2 &&
158 git reset --hard &&
159 # Without cone mode, "sub" and "sub2" do not match
160 git sparse-checkout set sub/dir sub2/dir &&
162 # Add contained contents to ensure we avoid non-existence errors
163 mkdir sub/dir2 &&
164 touch sub/d sub/dir2/e &&
166 test_must_fail git mv sub sub2 2>stderr &&
167 cat sparse_error_header >expect &&
168 cat >>expect <<-\EOF &&
169 sub/d
170 sub2/d
171 sub/dir2/e
172 sub2/dir2/e
174 cat sparse_hint >>expect &&
175 test_cmp expect stderr &&
176 git mv --sparse sub sub2 2>stderr &&
177 test_must_be_empty stderr &&
178 git commit -m "moved sub to sub2" &&
179 git rev-parse HEAD~1:sub >expect &&
180 git rev-parse HEAD:sub2 >actual &&
181 test_cmp expect actual &&
182 git reset --hard HEAD~1
185 test_expect_success 'recursive mv refuses to move sparse' '
186 git reset --hard &&
187 # Use cone mode so "sub/" matches the sparse-checkout patterns
188 git sparse-checkout init --cone &&
189 git sparse-checkout set sub/dir sub2/dir &&
191 # Add contained contents to ensure we avoid non-existence errors
192 mkdir sub/dir2 &&
193 touch sub/dir2/e &&
195 test_must_fail git mv sub sub2 2>stderr &&
196 cat sparse_error_header >expect &&
197 cat >>expect <<-\EOF &&
198 sub/dir2/e
199 sub2/dir2/e
201 cat sparse_hint >>expect &&
202 test_cmp expect stderr &&
203 git mv --sparse sub sub2 2>stderr &&
204 test_must_be_empty stderr &&
205 git commit -m "moved sub to sub2" &&
206 git rev-parse HEAD~1:sub >expect &&
207 git rev-parse HEAD:sub2 >actual &&
208 test_cmp expect actual &&
209 git reset --hard HEAD~1
212 test_expect_success 'can move files to non-sparse dir' '
213 git reset --hard &&
214 git sparse-checkout init --no-cone &&
215 git sparse-checkout set a b c w !/x y/ &&
216 mkdir -p w x/y &&
218 git mv a w/new-a 2>stderr &&
219 git mv b x/y/new-b 2>stderr &&
220 test_must_be_empty stderr
223 test_expect_success 'refuse to move file to non-skip-worktree sparse path' '
224 test_when_finished "cleanup_sparse_checkout" &&
225 git reset --hard &&
226 git sparse-checkout init --no-cone &&
227 git sparse-checkout set a !/x y/ !x/y/z &&
228 mkdir -p x/y/z &&
230 test_must_fail git mv a x/y/z/new-a 2>stderr &&
231 echo x/y/z/new-a | cat sparse_error_header - sparse_hint >expect &&
232 test_cmp expect stderr
235 test_expect_success 'refuse to move out-of-cone directory without --sparse' '
236 test_when_finished "cleanup_sparse_checkout" &&
237 setup_sparse_checkout &&
239 test_must_fail git mv folder1 sub 2>stderr &&
240 cat sparse_error_header >expect &&
241 echo folder1/file1 >>expect &&
242 cat sparse_hint >>expect &&
243 test_cmp expect stderr
246 test_expect_success 'can move out-of-cone directory with --sparse' '
247 test_when_finished "cleanup_sparse_checkout" &&
248 setup_sparse_checkout &&
250 git mv --sparse folder1 sub 2>stderr &&
251 test_must_be_empty stderr &&
253 test_path_is_dir sub/folder1 &&
254 test_path_is_file sub/folder1/file1
257 test_expect_success 'refuse to move out-of-cone file without --sparse' '
258 test_when_finished "cleanup_sparse_checkout" &&
259 setup_sparse_checkout &&
261 test_must_fail git mv folder1/file1 sub 2>stderr &&
262 cat sparse_error_header >expect &&
263 echo folder1/file1 >>expect &&
264 cat sparse_hint >>expect &&
265 test_cmp expect stderr
268 test_expect_success 'can move out-of-cone file with --sparse' '
269 test_when_finished "cleanup_sparse_checkout" &&
270 setup_sparse_checkout &&
272 git mv --sparse folder1/file1 sub 2>stderr &&
273 test_must_be_empty stderr &&
275 test_path_is_file sub/file1
278 test_expect_success 'refuse to move sparse file to existing destination' '
279 test_when_finished "cleanup_sparse_checkout" &&
280 mkdir folder1 &&
281 touch folder1/file1 &&
282 touch sub/file1 &&
283 git add folder1 sub/file1 &&
284 git sparse-checkout set --cone sub &&
286 test_must_fail git mv --sparse folder1/file1 sub 2>stderr &&
287 echo "fatal: destination exists, source=folder1/file1, destination=sub/file1" >expect &&
288 test_cmp expect stderr
291 test_expect_success 'move sparse file to existing destination with --force and --sparse' '
292 test_when_finished "cleanup_sparse_checkout" &&
293 mkdir folder1 &&
294 touch folder1/file1 &&
295 touch sub/file1 &&
296 echo "overwrite" >folder1/file1 &&
297 git add folder1 sub/file1 &&
298 git sparse-checkout set --cone sub &&
300 git mv --sparse --force folder1/file1 sub 2>stderr &&
301 test_must_be_empty stderr &&
302 echo "overwrite" >expect &&
303 test_cmp expect sub/file1
306 test_expect_success 'move clean path from in-cone to out-of-cone' '
307 test_when_finished "cleanup_sparse_checkout" &&
308 setup_sparse_checkout &&
310 test_must_fail git mv sub/d folder1 2>stderr &&
311 cat sparse_error_header >expect &&
312 echo "folder1/d" >>expect &&
313 cat sparse_hint >>expect &&
314 test_cmp expect stderr &&
316 git mv --sparse sub/d folder1 2>stderr &&
317 test_must_be_empty stderr &&
319 test_path_is_missing sub/d &&
320 test_path_is_missing folder1/d &&
321 git ls-files -t >actual &&
322 ! grep "^H sub/d\$" actual &&
323 grep "S folder1/d" actual
326 test_expect_success 'move clean path from in-cone to out-of-cone overwrite' '
327 test_when_finished "cleanup_sparse_checkout" &&
328 setup_sparse_checkout &&
329 echo "sub/file1 overwrite" >sub/file1 &&
330 git add sub/file1 &&
332 test_must_fail git mv sub/file1 folder1 2>stderr &&
333 cat sparse_error_header >expect &&
334 echo "folder1/file1" >>expect &&
335 cat sparse_hint >>expect &&
336 test_cmp expect stderr &&
338 test_must_fail git mv --sparse sub/file1 folder1 2>stderr &&
339 echo "fatal: destination exists in the index, source=sub/file1, destination=folder1/file1" \
340 >expect &&
341 test_cmp expect stderr &&
343 git mv --sparse -f sub/file1 folder1 2>stderr &&
344 test_must_be_empty stderr &&
346 test_path_is_missing sub/file1 &&
347 test_path_is_missing folder1/file1 &&
348 git ls-files -t >actual &&
349 ! grep "H sub/file1" actual &&
350 grep "S folder1/file1" actual &&
352 # compare file content before move and after move
353 echo "sub/file1 overwrite" >expect &&
354 git ls-files -s -- folder1/file1 | awk "{print \$2}" >oid &&
355 git cat-file blob $(cat oid) >actual &&
356 test_cmp expect actual
359 # This test is testing the same behavior as the
360 # "move clean path from in-cone to out-of-cone overwrite" above.
361 # The only difference is the <destination> changes from "folder1" to "folder1/file1"
362 test_expect_success 'move clean path from in-cone to out-of-cone file overwrite' '
363 test_when_finished "cleanup_sparse_checkout" &&
364 setup_sparse_checkout &&
365 echo "sub/file1 overwrite" >sub/file1 &&
366 git add sub/file1 &&
368 test_must_fail git mv sub/file1 folder1/file1 2>stderr &&
369 cat sparse_error_header >expect &&
370 echo "folder1/file1" >>expect &&
371 cat sparse_hint >>expect &&
372 test_cmp expect stderr &&
374 test_must_fail git mv --sparse sub/file1 folder1/file1 2>stderr &&
375 echo "fatal: destination exists in the index, source=sub/file1, destination=folder1/file1" \
376 >expect &&
377 test_cmp expect stderr &&
379 git mv --sparse -f sub/file1 folder1/file1 2>stderr &&
380 test_must_be_empty stderr &&
382 test_path_is_missing sub/file1 &&
383 test_path_is_missing folder1/file1 &&
384 git ls-files -t >actual &&
385 ! grep "H sub/file1" actual &&
386 grep "S folder1/file1" actual &&
388 # compare file content before move and after move
389 echo "sub/file1 overwrite" >expect &&
390 git ls-files -s -- folder1/file1 | awk "{print \$2}" >oid &&
391 git cat-file blob $(cat oid) >actual &&
392 test_cmp expect actual
395 test_expect_success 'move directory with one of the files overwrite' '
396 test_when_finished "cleanup_sparse_checkout" &&
397 mkdir -p folder1/dir &&
398 touch folder1/dir/file1 &&
399 git add folder1 &&
400 git sparse-checkout set --cone sub &&
402 echo test >sub/dir/file1 &&
403 git add sub/dir/file1 &&
405 test_must_fail git mv sub/dir folder1 2>stderr &&
406 cat sparse_error_header >expect &&
407 echo "folder1/dir/e" >>expect &&
408 echo "folder1/dir/file1" >>expect &&
409 cat sparse_hint >>expect &&
410 test_cmp expect stderr &&
412 test_must_fail git mv --sparse sub/dir folder1 2>stderr &&
413 echo "fatal: destination exists in the index, source=sub/dir/file1, destination=folder1/dir/file1" \
414 >expect &&
415 test_cmp expect stderr &&
417 git mv --sparse -f sub/dir folder1 2>stderr &&
418 test_must_be_empty stderr &&
420 test_path_is_missing sub/dir/file1 &&
421 test_path_is_missing sub/dir/e &&
422 test_path_is_missing folder1/file1 &&
423 git ls-files -t >actual &&
424 ! grep "H sub/dir/file1" actual &&
425 ! grep "H sub/dir/e" actual &&
426 grep "S folder1/dir/file1" actual &&
428 # compare file content before move and after move
429 echo test >expect &&
430 git ls-files -s -- folder1/dir/file1 | awk "{print \$2}" >oid &&
431 git cat-file blob $(cat oid) >actual &&
432 test_cmp expect actual
435 test_expect_success 'move dirty path from in-cone to out-of-cone' '
436 test_when_finished "cleanup_sparse_checkout" &&
437 setup_sparse_checkout &&
438 echo "modified" >>sub/d &&
440 test_must_fail git mv sub/d folder1 2>stderr &&
441 cat sparse_error_header >expect &&
442 echo "folder1/d" >>expect &&
443 cat sparse_hint >>expect &&
444 test_cmp expect stderr &&
446 git mv --sparse sub/d folder1 2>stderr &&
447 cat dirty_error_header >expect &&
448 echo "folder1/d" >>expect &&
449 cat dirty_hint >>expect &&
450 test_cmp expect stderr &&
452 test_path_is_missing sub/d &&
453 test_path_is_file folder1/d &&
454 git ls-files -t >actual &&
455 ! grep "^H sub/d\$" actual &&
456 grep "H folder1/d" actual
459 test_expect_success 'move dir from in-cone to out-of-cone' '
460 test_when_finished "cleanup_sparse_checkout" &&
461 setup_sparse_checkout &&
462 mkdir sub/dir/deep &&
464 test_must_fail git mv sub/dir folder1 2>stderr &&
465 cat sparse_error_header >expect &&
466 echo "folder1/dir/e" >>expect &&
467 cat sparse_hint >>expect &&
468 test_cmp expect stderr &&
470 git mv --sparse sub/dir folder1 2>stderr &&
471 test_must_be_empty stderr &&
473 test_path_is_missing sub/dir &&
474 test_path_is_missing folder1 &&
475 git ls-files -t >actual &&
476 ! grep "H sub/dir/e" actual &&
477 grep "S folder1/dir/e" actual
480 test_expect_success 'move partially-dirty dir from in-cone to out-of-cone' '
481 test_when_finished "cleanup_sparse_checkout" &&
482 setup_sparse_checkout &&
483 mkdir sub/dir/deep &&
484 touch sub/dir/e2 sub/dir/e3 &&
485 git add sub/dir/e2 sub/dir/e3 &&
486 echo "modified" >>sub/dir/e2 &&
487 echo "modified" >>sub/dir/e3 &&
489 test_must_fail git mv sub/dir folder1 2>stderr &&
490 cat sparse_error_header >expect &&
491 echo "folder1/dir/e" >>expect &&
492 echo "folder1/dir/e2" >>expect &&
493 echo "folder1/dir/e3" >>expect &&
494 cat sparse_hint >>expect &&
495 test_cmp expect stderr &&
497 git mv --sparse sub/dir folder1 2>stderr &&
498 cat dirty_error_header >expect &&
499 echo "folder1/dir/e2" >>expect &&
500 echo "folder1/dir/e3" >>expect &&
501 cat dirty_hint >>expect &&
502 test_cmp expect stderr &&
504 test_path_is_missing sub/dir &&
505 test_path_is_missing folder1/dir/e &&
506 test_path_is_file folder1/dir/e2 &&
507 test_path_is_file folder1/dir/e3 &&
508 git ls-files -t >actual &&
509 ! grep "H sub/dir/e" actual &&
510 ! grep "H sub/dir/e2" actual &&
511 ! grep "H sub/dir/e3" actual &&
512 grep "S folder1/dir/e" actual &&
513 grep "H folder1/dir/e2" actual &&
514 grep "H folder1/dir/e3" actual
517 test_done