Message ID | 20220619032549.156335-8-shaoxuan.yuan02@gmail.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | mv: fix out-of-cone file/directory move logic | expand |
Shaoxuan Yuan wrote: > Originally, "git mv" a sparse file from out-of-cone to > in-cone does not update the moved file's sparsity (remove its > SKIP_WORKTREE bit). And the corresponding cache entry is, unexpectedly, > not checked out in the working tree. > > Update the behavior so that: > 1. Moving from out-of-cone to in-cone removes the SKIP_WORKTREE bit from > corresponding cache entry. > 2. The moved cache entry is checked out in the working tree to reflect > the updated sparsity. > > Signed-off-by: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com> > --- > builtin/mv.c | 18 ++++++++++++++++++ > t/t7002-mv-sparse-checkout.sh | 4 +--- > 2 files changed, 19 insertions(+), 3 deletions(-) > > diff --git a/builtin/mv.c b/builtin/mv.c > index cb3441c7cb..a8b9f55654 100644 > --- a/builtin/mv.c > +++ b/builtin/mv.c > @@ -13,6 +13,7 @@ > #include "string-list.h" > #include "parse-options.h" > #include "submodule.h" > +#include "entry.h" > > static const char * const builtin_mv_usage[] = { > N_("git mv [<options>] <source>... <destination>"), > @@ -399,6 +400,11 @@ int cmd_mv(int argc, const char **argv, const char *prefix) > const char *src = source[i], *dst = destination[i]; > enum update_mode mode = modes[i]; > int pos; > + struct checkout state = CHECKOUT_INIT; > + state.istate = &the_index; > + > + if (force) > + state.force = 1; > if (show_only || verbose) > printf(_("Renaming %s to %s\n"), src, dst); > if (show_only) > @@ -424,6 +430,18 @@ int cmd_mv(int argc, const char **argv, const char *prefix) > pos = cache_name_pos(src, strlen(src)); > assert(pos >= 0); > rename_cache_entry_at(pos, dst); At first I wasn't sure how this would handle moving whole "sparse" directories (i.e., directories containing all 'SKIP_WORKTREE' entries), since this loop only iterates over 'argc'. The good news is: it does work, successfully moving each file in the directory individually! Unfortunately, the *reason* it works is because 'mv' changes the value of 'argc' to include the new directories. All this to say - your implementation is good (and IMO doesn't require any changes), it just happens to sit alongside somewhat questionable code. :) > + > + if (mode & SPARSE) { > + if (path_in_sparse_checkout(dst, &the_index)) { Nit: this can be consolidated into a single condition: if ((mode & SPARSE) && path_in_sparse_checkout(dst, &the_index)) { > + int dst_pos; > + > + dst_pos = cache_name_pos(dst, strlen(dst)); > + active_cache[dst_pos]->ce_flags &= ~CE_SKIP_WORKTREE; > + > + if (checkout_entry(active_cache[dst_pos], &state, NULL, NULL)) > + die(_("cannot checkout %s"), ce->name); > + } > + } > } > > if (gitmodules_modified) > diff --git a/t/t7002-mv-sparse-checkout.sh b/t/t7002-mv-sparse-checkout.sh > index 30e13b9979..7734119197 100755 > --- a/t/t7002-mv-sparse-checkout.sh > +++ b/t/t7002-mv-sparse-checkout.sh > @@ -237,7 +237,6 @@ test_expect_success 'can move out-of-cone directory with --sparse' ' > git mv --sparse folder1 sub 1>actual 2>stderr && > test_must_be_empty stderr && > > - git sparse-checkout reapply && > test_path_is_dir sub/folder1 && > test_path_is_file sub/folder1/file1 > ' > @@ -260,7 +259,6 @@ test_expect_success 'can move out-of-cone file with --sparse' ' > git mv --sparse folder1/file1 sub 1>actual 2>stderr && > test_must_be_empty stderr && > > - git sparse-checkout reapply && > ! test_path_is_dir sub/folder1 && > test_path_is_file sub/file1 > ' > @@ -278,7 +276,7 @@ test_expect_success 'refuse to move sparse file to existing destination' ' > test_cmp expect stderr > ' > > -test_expect_failure 'move sparse file to existing destination with --force and --sparse' ' > +test_expect_success 'move sparse file to existing destination with --force and --sparse' ' > test_when_finished "cleanup_sparse_checkout" && > mkdir folder1 && > touch folder1/file1 &&
diff --git a/builtin/mv.c b/builtin/mv.c index cb3441c7cb..a8b9f55654 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -13,6 +13,7 @@ #include "string-list.h" #include "parse-options.h" #include "submodule.h" +#include "entry.h" static const char * const builtin_mv_usage[] = { N_("git mv [<options>] <source>... <destination>"), @@ -399,6 +400,11 @@ int cmd_mv(int argc, const char **argv, const char *prefix) const char *src = source[i], *dst = destination[i]; enum update_mode mode = modes[i]; int pos; + struct checkout state = CHECKOUT_INIT; + state.istate = &the_index; + + if (force) + state.force = 1; if (show_only || verbose) printf(_("Renaming %s to %s\n"), src, dst); if (show_only) @@ -424,6 +430,18 @@ int cmd_mv(int argc, const char **argv, const char *prefix) pos = cache_name_pos(src, strlen(src)); assert(pos >= 0); rename_cache_entry_at(pos, dst); + + if (mode & SPARSE) { + if (path_in_sparse_checkout(dst, &the_index)) { + int dst_pos; + + dst_pos = cache_name_pos(dst, strlen(dst)); + active_cache[dst_pos]->ce_flags &= ~CE_SKIP_WORKTREE; + + if (checkout_entry(active_cache[dst_pos], &state, NULL, NULL)) + die(_("cannot checkout %s"), ce->name); + } + } } if (gitmodules_modified) diff --git a/t/t7002-mv-sparse-checkout.sh b/t/t7002-mv-sparse-checkout.sh index 30e13b9979..7734119197 100755 --- a/t/t7002-mv-sparse-checkout.sh +++ b/t/t7002-mv-sparse-checkout.sh @@ -237,7 +237,6 @@ test_expect_success 'can move out-of-cone directory with --sparse' ' git mv --sparse folder1 sub 1>actual 2>stderr && test_must_be_empty stderr && - git sparse-checkout reapply && test_path_is_dir sub/folder1 && test_path_is_file sub/folder1/file1 ' @@ -260,7 +259,6 @@ test_expect_success 'can move out-of-cone file with --sparse' ' git mv --sparse folder1/file1 sub 1>actual 2>stderr && test_must_be_empty stderr && - git sparse-checkout reapply && ! test_path_is_dir sub/folder1 && test_path_is_file sub/file1 ' @@ -278,7 +276,7 @@ test_expect_success 'refuse to move sparse file to existing destination' ' test_cmp expect stderr ' -test_expect_failure 'move sparse file to existing destination with --force and --sparse' ' +test_expect_success 'move sparse file to existing destination with --force and --sparse' ' test_when_finished "cleanup_sparse_checkout" && mkdir folder1 && touch folder1/file1 &&
Originally, "git mv" a sparse file from out-of-cone to in-cone does not update the moved file's sparsity (remove its SKIP_WORKTREE bit). And the corresponding cache entry is, unexpectedly, not checked out in the working tree. Update the behavior so that: 1. Moving from out-of-cone to in-cone removes the SKIP_WORKTREE bit from corresponding cache entry. 2. The moved cache entry is checked out in the working tree to reflect the updated sparsity. Signed-off-by: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com> --- builtin/mv.c | 18 ++++++++++++++++++ t/t7002-mv-sparse-checkout.sh | 4 +--- 2 files changed, 19 insertions(+), 3 deletions(-)