| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git mv in sparse working trees' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | setup_sparse_checkout () { |
| 8 | mkdir folder1 && |
| 9 | touch folder1/file1 && |
| 10 | git add folder1 && |
| 11 | git sparse-checkout set --cone sub |
| 12 | } |
| 13 | |
| 14 | cleanup_sparse_checkout () { |
| 15 | git sparse-checkout disable && |
| 16 | git reset --hard |
| 17 | } |
| 18 | |
| 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 && |
| 24 | |
| 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 |
| 30 | |
| 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 set advice.updateSparsePath false\" |
| 36 | EOF |
| 37 | |
| 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 |
| 43 | |
| 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 set advice.updateSparsePath false\" |
| 49 | EOF |
| 50 | " |
| 51 | |
| 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 |
| 65 | ' |
| 66 | |
| 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 && |
| 71 | |
| 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 && |
| 82 | |
| 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 && |
| 87 | |
| 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 && |
| 99 | |
| 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 |
| 104 | ' |
| 105 | |
| 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 && |
| 110 | |
| 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 && |
| 123 | |
| 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 |
| 138 | ' |
| 139 | |
| 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 && |
| 144 | |
| 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 |
| 154 | ' |
| 155 | |
| 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 && |
| 161 | |
| 162 | # Add contained contents to ensure we avoid non-existence errors |
| 163 | mkdir sub/dir2 && |
| 164 | touch sub/d sub/dir2/e && |
| 165 | |
| 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 |
| 173 | EOF |
| 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 |
| 183 | ' |
| 184 | |
| 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 && |
| 190 | |
| 191 | # Add contained contents to ensure we avoid non-existence errors |
| 192 | mkdir sub/dir2 && |
| 193 | touch sub/dir2/e && |
| 194 | |
| 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 |
| 200 | EOF |
| 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 |
| 210 | ' |
| 211 | |
| 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 && |
| 217 | |
| 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 |
| 221 | ' |
| 222 | |
| 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 && |
| 229 | |
| 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 |
| 233 | ' |
| 234 | |
| 235 | test_expect_success 'refuse to move out-of-cone directory without --sparse' ' |
| 236 | test_when_finished "cleanup_sparse_checkout" && |
| 237 | setup_sparse_checkout && |
| 238 | |
| 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 |
| 244 | ' |
| 245 | |
| 246 | test_expect_success 'can move out-of-cone directory with --sparse' ' |
| 247 | test_when_finished "cleanup_sparse_checkout" && |
| 248 | setup_sparse_checkout && |
| 249 | |
| 250 | git mv --sparse folder1 sub 2>stderr && |
| 251 | test_must_be_empty stderr && |
| 252 | |
| 253 | test_path_is_dir sub/folder1 && |
| 254 | test_path_is_file sub/folder1/file1 |
| 255 | ' |
| 256 | |
| 257 | test_expect_success 'refuse to move out-of-cone file without --sparse' ' |
| 258 | test_when_finished "cleanup_sparse_checkout" && |
| 259 | setup_sparse_checkout && |
| 260 | |
| 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 |
| 266 | ' |
| 267 | |
| 268 | test_expect_success 'can move out-of-cone file with --sparse' ' |
| 269 | test_when_finished "cleanup_sparse_checkout" && |
| 270 | setup_sparse_checkout && |
| 271 | |
| 272 | git mv --sparse folder1/file1 sub 2>stderr && |
| 273 | test_must_be_empty stderr && |
| 274 | |
| 275 | test_path_is_file sub/file1 |
| 276 | ' |
| 277 | |
| 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 && |
| 285 | |
| 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 |
| 289 | ' |
| 290 | |
| 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 && |
| 299 | |
| 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 |
| 304 | ' |
| 305 | |
| 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 && |
| 309 | |
| 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 && |
| 315 | |
| 316 | git mv --sparse sub/d folder1 2>stderr && |
| 317 | test_must_be_empty stderr && |
| 318 | |
| 319 | test_path_is_missing sub/d && |
| 320 | test_path_is_missing folder1/d && |
| 321 | git ls-files -t >actual && |
| 322 | test_grep ! "^H sub/d\$" actual && |
| 323 | test_grep "S folder1/d" actual |
| 324 | ' |
| 325 | |
| 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 && |
| 331 | |
| 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 && |
| 337 | |
| 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 && |
| 342 | |
| 343 | git mv --sparse -f sub/file1 folder1 2>stderr && |
| 344 | test_must_be_empty stderr && |
| 345 | |
| 346 | test_path_is_missing sub/file1 && |
| 347 | test_path_is_missing folder1/file1 && |
| 348 | git ls-files -t >actual && |
| 349 | test_grep ! "H sub/file1" actual && |
| 350 | test_grep "S folder1/file1" actual && |
| 351 | |
| 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 |
| 357 | ' |
| 358 | |
| 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 && |
| 367 | |
| 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 && |
| 373 | |
| 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 && |
| 378 | |
| 379 | git mv --sparse -f sub/file1 folder1/file1 2>stderr && |
| 380 | test_must_be_empty stderr && |
| 381 | |
| 382 | test_path_is_missing sub/file1 && |
| 383 | test_path_is_missing folder1/file1 && |
| 384 | git ls-files -t >actual && |
| 385 | test_grep ! "H sub/file1" actual && |
| 386 | test_grep "S folder1/file1" actual && |
| 387 | |
| 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 |
| 393 | ' |
| 394 | |
| 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 && |
| 401 | |
| 402 | echo test >sub/dir/file1 && |
| 403 | git add sub/dir/file1 && |
| 404 | |
| 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 && |
| 411 | |
| 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 && |
| 416 | |
| 417 | git mv --sparse -f sub/dir folder1 2>stderr && |
| 418 | test_must_be_empty stderr && |
| 419 | |
| 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 | test_grep ! "H sub/dir/file1" actual && |
| 425 | test_grep ! "H sub/dir/e" actual && |
| 426 | test_grep "S folder1/dir/file1" actual && |
| 427 | |
| 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 |
| 433 | ' |
| 434 | |
| 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 && |
| 439 | |
| 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 && |
| 445 | |
| 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 && |
| 451 | |
| 452 | test_path_is_missing sub/d && |
| 453 | test_path_is_file folder1/d && |
| 454 | git ls-files -t >actual && |
| 455 | test_grep ! "^H sub/d\$" actual && |
| 456 | test_grep "H folder1/d" actual |
| 457 | ' |
| 458 | |
| 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 && |
| 463 | |
| 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 && |
| 469 | |
| 470 | git mv --sparse sub/dir folder1 2>stderr && |
| 471 | test_must_be_empty stderr && |
| 472 | |
| 473 | test_path_is_missing sub/dir && |
| 474 | test_path_is_missing folder1 && |
| 475 | git ls-files -t >actual && |
| 476 | test_grep ! "H sub/dir/e" actual && |
| 477 | test_grep "S folder1/dir/e" actual |
| 478 | ' |
| 479 | |
| 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 && |
| 488 | |
| 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 && |
| 496 | |
| 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 && |
| 503 | |
| 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 | test_grep ! "H sub/dir/e" actual && |
| 510 | test_grep ! "H sub/dir/e2" actual && |
| 511 | test_grep ! "H sub/dir/e3" actual && |
| 512 | test_grep "S folder1/dir/e" actual && |
| 513 | test_grep "H folder1/dir/e2" actual && |
| 514 | test_grep "H folder1/dir/e3" actual |
| 515 | ' |
| 516 | |
| 517 | test_done |