Skip to content

Commit 5784db1

Browse files
ffyuandagitster
authored andcommitted
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 <[email protected]> Helped-by: Victoria Dye <[email protected]> Signed-off-by: Shaoxuan Yuan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9284c3c commit 5784db1

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

builtin/mv.c

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,13 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
171171
};
172172
const char **source, **destination, **dest_path, **submodule_gitfile;
173173
const char *dst_w_slash;
174-
enum update_mode *modes;
174+
enum update_mode *modes, dst_mode = 0;
175175
struct stat st;
176176
struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
177177
struct lock_file lock_file = LOCK_INIT;
178178
struct cache_entry *ce;
179179
struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
180+
struct string_list dirty_paths = STRING_LIST_INIT_NODUP;
180181

181182
git_config(git_default_config, NULL);
182183

@@ -213,10 +214,21 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
213214
if (!path_in_sparse_checkout(dst_w_slash, &the_index) &&
214215
empty_dir_has_sparse_contents(dst_w_slash)) {
215216
destination = internal_prefix_pathspec(dst_w_slash, argv, argc, DUP_BASENAME);
217+
dst_mode = SKIP_WORKTREE_DIR;
216218
} else if (argc != 1) {
217219
die(_("destination '%s' is not a directory"), dest_path[0]);
218220
} else {
219221
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;
220232
}
221233
}
222234
if (dst_w_slash != dest_path[0]) {
@@ -410,6 +422,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
410422
const char *src = source[i], *dst = destination[i];
411423
enum update_mode mode = modes[i];
412424
int pos;
425+
int sparse_and_dirty = 0;
413426
struct checkout state = CHECKOUT_INIT;
414427
state.istate = &the_index;
415428

@@ -420,6 +433,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
420433
if (show_only)
421434
continue;
422435
if (!(mode & (INDEX | SPARSE | SKIP_WORKTREE_DIR)) &&
436+
!(dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) &&
423437
rename(src, dst) < 0) {
424438
if (ignore_errors)
425439
continue;
@@ -439,17 +453,55 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
439453

440454
pos = cache_name_pos(src, strlen(src));
441455
assert(pos >= 0);
456+
if (!(mode & SPARSE) && !lstat(src, &st))
457+
sparse_and_dirty = ce_modified(active_cache[pos], &st, 0);
442458
rename_cache_entry_at(pos, dst);
443459

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];
447485

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+
}
453505
}
454506
}
455507

@@ -461,6 +513,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
461513
die(_("Unable to write new index file"));
462514

463515
string_list_clear(&src_for_dst, 0);
516+
string_list_clear(&dirty_paths, 0);
464517
UNLEAK(source);
465518
UNLEAK(dest_path);
466519
free(submodule_gitfile);

t/t7002-mv-sparse-checkout.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ test_expect_success 'move sparse file to existing destination with --force and -
290290
test_cmp expect sub/file1
291291
'
292292

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' '
294294
test_when_finished "cleanup_sparse_checkout" &&
295295
setup_sparse_checkout &&
296296
@@ -419,7 +419,7 @@ test_expect_failure 'move directory with one of the files overwrite' '
419419
test_cmp expect actual
420420
'
421421

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' '
423423
test_when_finished "cleanup_sparse_checkout" &&
424424
setup_sparse_checkout &&
425425
echo "modified" >>sub/d &&
@@ -439,7 +439,7 @@ test_expect_failure 'move dirty path from in-cone to out-of-cone' '
439439
grep "H folder1/d" actual
440440
'
441441

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' '
443443
test_when_finished "cleanup_sparse_checkout" &&
444444
setup_sparse_checkout &&
445445
@@ -458,7 +458,7 @@ test_expect_failure 'move dir from in-cone to out-of-cone' '
458458
grep "S folder1/dir/e" actual
459459
'
460460

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' '
462462
test_when_finished "cleanup_sparse_checkout" &&
463463
setup_sparse_checkout &&
464464
touch sub/dir/e2 sub/dir/e3 &&

0 commit comments

Comments
 (0)