Originally, moving a sparse file into cone can result in unwarned overwrite of existing entry. The expected behavior is that if the <destination> exists in the entry, user should be prompted to supply a [-f|--force] to carry out the operation, or the operation should fail. Add a check mechanism to do that. Signed-off-by: Shaoxuan Yuan <shaoxuan.yuan02@xxxxxxxxx> --- builtin/mv.c | 15 ++++++++++++--- t/t7002-mv-sparse-checkout.sh | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index 9d8494a2e4..abb90d3266 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -201,11 +201,20 @@ int cmd_mv(int argc, const char **argv, const char *prefix) bad = _("bad source"); goto act_on_entry; } - - if (!ignore_sparse) + if (!ignore_sparse) { string_list_append(&only_match_skip_worktree, src); - else + goto act_on_entry; + } + /* Check if dst exists in index */ + if (cache_name_pos(dst, strlen(dst)) < 0) { modes[i] = SPARSE; + goto act_on_entry; + } + if (!force) { + bad = _("destination exists"); + goto act_on_entry; + } + modes[i] = SPARSE; goto act_on_entry; } if (!strncmp(src, dst, length) && diff --git a/t/t7002-mv-sparse-checkout.sh b/t/t7002-mv-sparse-checkout.sh index 1984cf131d..5b61fbad5f 100755 --- a/t/t7002-mv-sparse-checkout.sh +++ b/t/t7002-mv-sparse-checkout.sh @@ -265,7 +265,7 @@ test_expect_success 'can move out-of-cone file with --sparse' ' test_path_is_file sub/file1 ' -test_expect_failure 'refuse to move sparse file to existing destination' ' +test_expect_success 'refuse to move sparse file to existing destination' ' test_when_finished "cleanup_sparse_checkout" && mkdir folder1 && touch folder1/file1 && -- 2.35.1