diff mbox series

[v1,7/7] mv: check overwrite for in-to-out move

Message ID 20220719132809.409247-8-shaoxuan.yuan02@gmail.com (mailing list archive)
State Superseded
Headers show
Series mv: from in-cone to out-of-cone | expand

Commit Message

Shaoxuan Yuan July 19, 2022, 1:28 p.m. UTC
Add checking logic for overwritng when moving from in-cone to
out-of-cone. It is the index version of the original overwrite logic.

Signed-off-by: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com>
---
 builtin/mv.c                  | 12 ++++++++++++
 t/t7002-mv-sparse-checkout.sh |  2 +-
 2 files changed, 13 insertions(+), 1 deletion(-)

Comments

Derrick Stolee July 19, 2022, 6:15 p.m. UTC | #1
On 7/19/2022 9:28 AM, Shaoxuan Yuan wrote:
> Add checking logic for overwritng when moving from in-cone to

s/overwritng/overwriting/

> out-of-cone. It is the index version of the original overwrite logic.

Code looks good to me.

Thanks,
-Stolee
diff mbox series

Patch

diff --git a/builtin/mv.c b/builtin/mv.c
index d35994c443..5ed3bd3431 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -365,6 +365,18 @@  int cmd_mv(int argc, const char **argv, const char *prefix)
 			goto act_on_entry;
 		}
 
+		if (ignore_sparse &&
+		    (dst_mode & SKIP_WORKTREE_DIR) &&
+		    index_entry_exists(&the_index, dst, strlen(dst))) {
+			bad = _("destination exists in the index");
+			if (force) {
+				if (verbose)
+					warning(_("overwriting '%s'"), dst);
+				bad = NULL;
+			} else {
+				goto act_on_entry;
+			}
+		}
 		/*
 		 * We check if the paths are in the sparse-checkout
 		 * definition as a very final check, since that
diff --git a/t/t7002-mv-sparse-checkout.sh b/t/t7002-mv-sparse-checkout.sh
index dafe15b9cf..5d810f3af0 100755
--- a/t/t7002-mv-sparse-checkout.sh
+++ b/t/t7002-mv-sparse-checkout.sh
@@ -323,7 +323,7 @@  test_expect_success 'move clean path from in-cone to out-of-cone' '
 	grep -x "S folder1/d" actual
 '
 
-test_expect_failure 'move clean path from in-cone to out-of-cone overwrite' '
+test_expect_success 'move clean path from in-cone to out-of-cone overwrite' '
 	test_when_finished "cleanup_sparse_checkout" &&
 	setup_sparse_checkout &&
 	echo "sub/file1 overwrite" >sub/file1 &&