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 <shaoxuan.yuan02@gmail.com> Acked-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shaoxuan Yuan committed Jun 30, 2022 at 10:37 UTC 6645b03ca5af33bbce1001257ee22832559ec966
2 files changed +21 -4
builtin/mv.c
+19 -2
@@ -186,11 +186,28 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
186
187 length = strlen(src);
188 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)) {
202 bad = _("bad source");
203 goto act_on_entry;
204 }
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;
211 }
212 if (!strncmp(src, dst, length) &&
213 (dst[length] == 0 || dst[length] == '/')) {
t/t7002-mv-sparse-checkout.sh
+2 -2
@@ -241,7 +241,7 @@ test_expect_failure 'can move out-of-cone directory with --sparse' '
241 test_path_is_file sub/folder1/file1
242 '
243
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' '
245 test_when_finished "cleanup_sparse_checkout" &&
246 setup_sparse_checkout &&
247
@@ -252,7 +252,7 @@ test_expect_failure 'refuse to move out-of-cone file without --sparse' '
252 test_cmp expect stderr
253 '
254
255 -test_expect_failure 'can move out-of-cone file with --sparse' '
255 +test_expect_success 'can move out-of-cone file with --sparse' '
256 test_when_finished "cleanup_sparse_checkout" &&
257 setup_sparse_checkout &&
258