Skip to content

Commit 8a26a39

Browse files
ffyuandagitster
authored andcommitted
mv: check if <destination> exists in index to handle overwriting
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 <[email protected]> Acked-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6645b03 commit 8a26a39

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

builtin/mv.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,20 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
202202
bad = _("bad source");
203203
goto act_on_entry;
204204
}
205-
206-
if (!ignore_sparse)
205+
if (!ignore_sparse) {
207206
string_list_append(&only_match_skip_worktree, src);
208-
else
207+
goto act_on_entry;
208+
}
209+
/* Check if dst exists in index */
210+
if (cache_name_pos(dst, strlen(dst)) < 0) {
209211
modes[i] = SPARSE;
212+
goto act_on_entry;
213+
}
214+
if (!force) {
215+
bad = _("destination exists");
216+
goto act_on_entry;
217+
}
218+
modes[i] = SPARSE;
210219
goto act_on_entry;
211220
}
212221
if (!strncmp(src, dst, length) &&

t/t7002-mv-sparse-checkout.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ test_expect_success 'can move out-of-cone file with --sparse' '
262262
test_path_is_file sub/file1
263263
'
264264

265-
test_expect_failure 'refuse to move sparse file to existing destination' '
265+
test_expect_success 'refuse to move sparse file to existing destination' '
266266
test_when_finished "cleanup_sparse_checkout" &&
267267
mkdir folder1 &&
268268
touch folder1/file1 &&
@@ -275,7 +275,7 @@ test_expect_failure 'refuse to move sparse file to existing destination' '
275275
test_cmp expect stderr
276276
'
277277

278-
test_expect_failure 'move sparse file to existing destination with --force and --sparse' '
278+
test_expect_success 'move sparse file to existing destination with --force and --sparse' '
279279
test_when_finished "cleanup_sparse_checkout" &&
280280
mkdir folder1 &&
281281
touch folder1/file1 &&

0 commit comments

Comments
 (0)