Skip to content

Commit da6fe05

Browse files
ffyuandagitster
authored andcommitted
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 <[email protected]> Signed-off-by: Shaoxuan Yuan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5efd533 commit da6fe05

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

builtin/mv.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,18 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
376376
goto act_on_entry;
377377
}
378378

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+
}
379391
/*
380392
* We check if the paths are in the sparse-checkout
381393
* definition as a very final check, since that

t/t7002-mv-sparse-checkout.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ test_expect_success 'move clean path from in-cone to out-of-cone' '
323323
grep "S folder1/d" actual
324324
'
325325

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' '
327327
test_when_finished "cleanup_sparse_checkout" &&
328328
setup_sparse_checkout &&
329329
echo "sub/file1 overwrite" >sub/file1 &&
@@ -359,7 +359,7 @@ test_expect_failure 'move clean path from in-cone to out-of-cone overwrite' '
359359
# This test is testing the same behavior as the
360360
# "move clean path from in-cone to out-of-cone overwrite" above.
361361
# 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' '
363363
test_when_finished "cleanup_sparse_checkout" &&
364364
setup_sparse_checkout &&
365365
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'
392392
test_cmp expect actual
393393
'
394394

395-
test_expect_failure 'move directory with one of the files overwrite' '
395+
test_expect_success 'move directory with one of the files overwrite' '
396396
test_when_finished "cleanup_sparse_checkout" &&
397397
mkdir -p folder1/dir &&
398398
touch folder1/dir/file1 &&

0 commit comments

Comments
 (0)