mv: from in-cone to out-of-cone

Originally, moving an in-cone <source> to an out-of-cone <destination> was not possible, mainly because such <destination> is a directory that is not present in the working tree. Change the behavior so that we can move an in-cone <source> to out-of-cone <destination> when --sparse is supplied. Notice that <destination> can also be an out-of-cone file path, rather than a directory. Such <source> can be either clean or dirty, and moving it results in different behaviors: A clean move should move <source> to <destination> in the index (do *not* create <destination> in the worktree), then delete <source> from the worktree. A dirty move should move the <source> to the <destination>, both in the working tree and the index, but should *not* remove the resulted path from the working tree and should *not* turn on its CE_SKIP_WORKTREE bit. Optional reading ================ We are strict about cone mode when <destination> is a file path. The reason is that some of the previous tests that use no-cone mode in t7002 are keep breaking, mainly because the `dst_mode = SPARSE;` line added in this patch. Most features developed in both "from-out-to-in" and "from-in-to-out" only care about cone mode situation, as no-cone mode is becoming irrelevant. And because assigning `SPARSE` to `dst_mode` when the repo is in no-cone mode causes miscellaneous bugs, we should just leave this new functionality to be exclusive cone mode and save some time. Helped-by: Derrick Stolee <derrickstolee@github.com> Helped-by: Victoria Dye <vdye@github.com> Signed-off-by: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shaoxuan Yuan committed Aug 9, 2022 at 20:09 UTC 5784db1b22feceafc72454dccd9637d19fdd422c
2 files changed +66 -13
builtin/mv.c
+62 -9
@@ -171,12 +171,13 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
171 };
172 const char **source, **destination, **dest_path, **submodule_gitfile;
173 const char *dst_w_slash;
174 - enum update_mode *modes;
174 + enum update_mode *modes, dst_mode = 0;
175 struct stat st;
176 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
177 struct lock_file lock_file = LOCK_INIT;
178 struct cache_entry *ce;
179 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
180 + struct string_list dirty_paths = STRING_LIST_INIT_NODUP;
181
182 git_config(git_default_config, NULL);
183
@@ -213,10 +214,21 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
214 if (!path_in_sparse_checkout(dst_w_slash, &the_index) &&
215 empty_dir_has_sparse_contents(dst_w_slash)) {
216 destination = internal_prefix_pathspec(dst_w_slash, argv, argc, DUP_BASENAME);
217 + dst_mode = SKIP_WORKTREE_DIR;
218 } else if (argc != 1) {
219 die(_("destination '%s' is not a directory"), dest_path[0]);
220 } else {
221 destination = dest_path;
222 + /*
223 + * <destination> is a file outside of sparse-checkout
224 + * cone. Insist on cone mode here for backward
225 + * compatibility. We don't want dst_mode to be assigned
226 + * for a file when the repo is using no-cone mode (which
227 + * is deprecated at this point) sparse-checkout. As
228 + * SPARSE here is only considering cone-mode situation.
229 + */
230 + if (!path_in_cone_mode_sparse_checkout(destination[0], &the_index))
231 + dst_mode = SPARSE;
232 }
233 }
234 if (dst_w_slash != dest_path[0]) {
@@ -410,6 +422,7 @@ remove_entry:
422 const char *src = source[i], *dst = destination[i];
423 enum update_mode mode = modes[i];
424 int pos;
425 + int sparse_and_dirty = 0;
426 struct checkout state = CHECKOUT_INIT;
427 state.istate = &the_index;
428
@@ -420,6 +433,7 @@ remove_entry:
433 if (show_only)
434 continue;
435 if (!(mode & (INDEX | SPARSE | SKIP_WORKTREE_DIR)) &&
436 + !(dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) &&
437 rename(src, dst) < 0) {
438 if (ignore_errors)
439 continue;
@@ -439,17 +453,55 @@ remove_entry:
453
454 pos = cache_name_pos(src, strlen(src));
455 assert(pos >= 0);
456 + if (!(mode & SPARSE) && !lstat(src, &st))
457 + sparse_and_dirty = ce_modified(active_cache[pos], &st, 0);
458 rename_cache_entry_at(pos, dst);
459
444 - if ((mode & SPARSE) &&
445 - (path_in_sparse_checkout(dst, &the_index))) {
446 - int dst_pos;
460 + if (ignore_sparse &&
461 + core_apply_sparse_checkout &&
462 + core_sparse_checkout_cone) {
463 + /*
464 + * NEEDSWORK: we are *not* paying attention to
465 + * "out-to-out" move (<source> is out-of-cone and
466 + * <destination> is out-of-cone) at this point. It
467 + * should be added in a future patch.
468 + */
469 + if ((mode & SPARSE) &&
470 + path_in_sparse_checkout(dst, &the_index)) {
471 + /* from out-of-cone to in-cone */
472 + int dst_pos = cache_name_pos(dst, strlen(dst));
473 + struct cache_entry *dst_ce = active_cache[dst_pos];
474 +
475 + dst_ce->ce_flags &= ~CE_SKIP_WORKTREE;
476 +
477 + if (checkout_entry(dst_ce, &state, NULL, NULL))
478 + die(_("cannot checkout %s"), dst_ce->name);
479 + } else if ((dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) &&
480 + !(mode & SPARSE) &&
481 + !path_in_sparse_checkout(dst, &the_index)) {
482 + /* from in-cone to out-of-cone */
483 + int dst_pos = cache_name_pos(dst, strlen(dst));
484 + struct cache_entry *dst_ce = active_cache[dst_pos];
485
448 - dst_pos = cache_name_pos(dst, strlen(dst));
449 - active_cache[dst_pos]->ce_flags &= ~CE_SKIP_WORKTREE;
450 -
451 - if (checkout_entry(active_cache[dst_pos], &state, NULL, NULL))
452 - die(_("cannot checkout %s"), active_cache[dst_pos]->name);
486 + /*
487 + * if src is clean, it will suffice to remove it
488 + */
489 + if (!sparse_and_dirty) {
490 + dst_ce->ce_flags |= CE_SKIP_WORKTREE;
491 + unlink_or_warn(src);
492 + } else {
493 + /*
494 + * if src is dirty, move it to the
495 + * destination and create leading
496 + * dirs if necessary
497 + */
498 + char *dst_dup = xstrdup(dst);
499 + string_list_append(&dirty_paths, dst);
500 + safe_create_leading_directories(dst_dup);
501 + FREE_AND_NULL(dst_dup);
502 + rename(src, dst);
503 + }
504 + }
505 }
506 }
507
@@ -461,6 +513,7 @@ remove_entry:
513 die(_("Unable to write new index file"));
514
515 string_list_clear(&src_for_dst, 0);
516 + string_list_clear(&dirty_paths, 0);
517 UNLEAK(source);
518 UNLEAK(dest_path);
519 free(submodule_gitfile);
t/t7002-mv-sparse-checkout.sh
+4 -4
@@ -290,7 +290,7 @@ test_expect_success 'move sparse file to existing destination with --force and -
290 test_cmp expect sub/file1
291 '
292
293 -test_expect_failure 'move clean path from in-cone to out-of-cone' '
293 +test_expect_success 'move clean path from in-cone to out-of-cone' '
294 test_when_finished "cleanup_sparse_checkout" &&
295 setup_sparse_checkout &&
296
@@ -419,7 +419,7 @@ test_expect_failure 'move directory with one of the files overwrite' '
419 test_cmp expect actual
420 '
421
422 -test_expect_failure 'move dirty path from in-cone to out-of-cone' '
422 +test_expect_success 'move dirty path from in-cone to out-of-cone' '
423 test_when_finished "cleanup_sparse_checkout" &&
424 setup_sparse_checkout &&
425 echo "modified" >>sub/d &&
@@ -439,7 +439,7 @@ test_expect_failure 'move dirty path from in-cone to out-of-cone' '
439 grep "H folder1/d" actual
440 '
441
442 -test_expect_failure 'move dir from in-cone to out-of-cone' '
442 +test_expect_success 'move dir from in-cone to out-of-cone' '
443 test_when_finished "cleanup_sparse_checkout" &&
444 setup_sparse_checkout &&
445
@@ -458,7 +458,7 @@ test_expect_failure 'move dir from in-cone to out-of-cone' '
458 grep "S folder1/dir/e" actual
459 '
460
461 -test_expect_failure 'move partially-dirty dir from in-cone to out-of-cone' '
461 +test_expect_success 'move partially-dirty dir from in-cone to out-of-cone' '
462 test_when_finished "cleanup_sparse_checkout" &&
463 setup_sparse_checkout &&
464 touch sub/dir/e2 sub/dir/e3 &&