]> git.ipfire.org Git - thirdparty/git.git/commitdiff
mv: check if <destination> exists in index to handle overwriting
authorShaoxuan Yuan <shaoxuan.yuan02@gmail.com>
Thu, 30 Jun 2022 02:37:35 +0000 (10:37 +0800)
committerJunio C Hamano <gitster@pobox.com>
Fri, 1 Jul 2022 21:50:16 +0000 (14:50 -0700)
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@gmail.com>
Acked-by: Derrick Stolee <derrickstolee@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/mv.c
t/t7002-mv-sparse-checkout.sh

index 520be8577484535ec08ee4ba1b54c218b32610b4..7d9627938a6ebce401ef8cf073cc942aa098a26b 100644 (file)
@@ -202,11 +202,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) &&
index 1510b5ed6a79040920f59fe6a72c4f16a1b71eff..6d2fb4f8d2634138bb6091af14c57c94f788f941 100755 (executable)
@@ -262,7 +262,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 &&
@@ -275,7 +275,7 @@ test_expect_failure 'refuse to move sparse file to existing destination' '
        test_cmp expect stderr
 '
 
-test_expect_failure 'move sparse file to existing destination with --force and --sparse' '
+test_expect_success 'move sparse file to existing destination with --force and --sparse' '
        test_when_finished "cleanup_sparse_checkout" &&
        mkdir folder1 &&
        touch folder1/file1 &&