mv: check overwrite for in-to-out move

Add checking logic for overwriting when moving from in-cone to out-of-cone. It is the index version of the original overwrite logic. Helped-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shaoxuan Yuan committed Aug 9, 2022 at 20:09 UTC da6fe05b3d624ad5b40472eebfe0499c15ecc93d
2 files changed +15 -3
builtin/mv.c
+12
@@ -376,6 +376,18 @@ dir_check:
376 goto act_on_entry;
377 }
378
379 + if (ignore_sparse &&
380 + (dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) &&
381 + index_entry_exists(&the_index, dst, strlen(dst))) {
382 + bad = _("destination exists in the index");
383 + if (force) {
384 + if (verbose)
385 + warning(_("overwriting '%s'"), dst);
386 + bad = NULL;
387 + } else {
388 + goto act_on_entry;
389 + }
390 + }
391 /*
392 * We check if the paths are in the sparse-checkout
393 * definition as a very final check, since that
t/t7002-mv-sparse-checkout.sh
+3 -3
@@ -323,7 +323,7 @@ test_expect_success 'move clean path from in-cone to out-of-cone' '
323 grep "S folder1/d" actual
324 '
325
326 -test_expect_failure 'move clean path from in-cone to out-of-cone overwrite' '
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 &&
@@ -359,7 +359,7 @@ test_expect_failure 'move clean path from in-cone to out-of-cone overwrite' '
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_failure 'move clean path from in-cone to out-of-cone file overwrite' '
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 &&
@@ -392,7 +392,7 @@ test_expect_failure 'move clean path from in-cone to out-of-cone file overwrite'
392 test_cmp expect actual
393 '
394
395 -test_expect_failure 'move directory with one of the files overwrite' '
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 &&