Skip to content

Commit 6645b03

Browse files
ffyuandagitster
authored andcommitted
mv: check if out-of-cone file exists in index with SKIP_WORKTREE bit
Originally, moving a <source> file which is not on-disk but exists in index as a SKIP_WORKTREE enabled cache entry, "giv mv" command errors out with "bad source". Change the checking logic, so that such <source> file makes "giv mv" command warns with "advise_on_updating_sparse_paths()" instead of "bad source"; also user now can supply a "--sparse" flag so this operation can be carried out successfully. Signed-off-by: Shaoxuan Yuan <[email protected]> Acked-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7889755 commit 6645b03

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

builtin/mv.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,28 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
186186

187187
length = strlen(src);
188188
if (lstat(src, &st) < 0) {
189-
/* only error if existence is expected. */
190-
if (modes[i] != SPARSE) {
189+
int pos;
190+
const struct cache_entry *ce;
191+
192+
pos = cache_name_pos(src, length);
193+
if (pos < 0) {
194+
/* only error if existence is expected. */
195+
if (modes[i] != SPARSE)
196+
bad = _("bad source");
197+
goto act_on_entry;
198+
}
199+
200+
ce = active_cache[pos];
201+
if (!ce_skip_worktree(ce)) {
191202
bad = _("bad source");
192203
goto act_on_entry;
193204
}
205+
206+
if (!ignore_sparse)
207+
string_list_append(&only_match_skip_worktree, src);
208+
else
209+
modes[i] = SPARSE;
210+
goto act_on_entry;
194211
}
195212
if (!strncmp(src, dst, length) &&
196213
(dst[length] == 0 || dst[length] == '/')) {

t/t7002-mv-sparse-checkout.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ test_expect_failure 'can move out-of-cone directory with --sparse' '
241241
test_path_is_file sub/folder1/file1
242242
'
243243

244-
test_expect_failure 'refuse to move out-of-cone file without --sparse' '
244+
test_expect_success 'refuse to move out-of-cone file without --sparse' '
245245
test_when_finished "cleanup_sparse_checkout" &&
246246
setup_sparse_checkout &&
247247
@@ -252,7 +252,7 @@ test_expect_failure 'refuse to move out-of-cone file without --sparse' '
252252
test_cmp expect stderr
253253
'
254254

255-
test_expect_failure 'can move out-of-cone file with --sparse' '
255+
test_expect_success 'can move out-of-cone file with --sparse' '
256256
test_when_finished "cleanup_sparse_checkout" &&
257257
setup_sparse_checkout &&
258258

0 commit comments

Comments
 (0)