Skip to content

Commit b6f51e3

Browse files
ffyuandagitster
authored andcommitted
mv: cleanup empty WORKING_DIRECTORY
Originally, moving from-in-to-out may leave an empty <source> directory on-disk (this kind of directory is marked as WORKING_DIRECTORY). Cleanup such directories if they are empty (don't have any entries under them). Modify two tests that take <source> as WORKING_DIRECTORY to test this behavior. Suggested-by: Derrick Stolee <[email protected]> Signed-off-by: Shaoxuan Yuan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5784db1 commit b6f51e3

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

builtin/mv.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ 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+
const char **src_dir = NULL;
175+
int src_dir_nr = 0, src_dir_alloc = 0;
176+
struct strbuf a_src_dir = STRBUF_INIT;
174177
enum update_mode *modes, dst_mode = 0;
175178
struct stat st;
176179
struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
@@ -313,6 +316,10 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
313316

314317
/* last - first >= 1 */
315318
modes[i] |= WORKING_DIRECTORY;
319+
320+
ALLOC_GROW(src_dir, src_dir_nr + 1, src_dir_alloc);
321+
src_dir[src_dir_nr++] = src;
322+
316323
n = argc + last - first;
317324
REALLOC_ARRAY(source, n);
318325
REALLOC_ARRAY(destination, n);
@@ -505,6 +512,26 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
505512
}
506513
}
507514

515+
/*
516+
* cleanup the empty src_dirs
517+
*/
518+
for (i = 0; i < src_dir_nr; i++) {
519+
int dummy;
520+
strbuf_addstr(&a_src_dir, src_dir[i]);
521+
/*
522+
* if entries under a_src_dir are all moved away,
523+
* recursively remove a_src_dir to cleanup
524+
*/
525+
if (index_range_of_same_dir(a_src_dir.buf, a_src_dir.len,
526+
&dummy, &dummy) < 1) {
527+
remove_dir_recursively(&a_src_dir, 0);
528+
}
529+
strbuf_reset(&a_src_dir);
530+
}
531+
532+
strbuf_release(&a_src_dir);
533+
free(src_dir);
534+
508535
if (gitmodules_modified)
509536
stage_updated_gitmodules(&the_index);
510537

t/t7002-mv-sparse-checkout.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ test_expect_success 'move dirty path from in-cone to out-of-cone' '
442442
test_expect_success 'move dir from in-cone to out-of-cone' '
443443
test_when_finished "cleanup_sparse_checkout" &&
444444
setup_sparse_checkout &&
445+
mkdir sub/dir/deep &&
445446
446447
test_must_fail git mv sub/dir folder1 2>stderr &&
447448
cat sparse_error_header >expect &&
@@ -452,6 +453,7 @@ test_expect_success 'move dir from in-cone to out-of-cone' '
452453
git mv --sparse sub/dir folder1 2>stderr &&
453454
test_must_be_empty stderr &&
454455
456+
test_path_is_missing sub/dir &&
455457
test_path_is_missing folder1 &&
456458
git ls-files -t >actual &&
457459
! grep "H sub/dir/e" actual &&
@@ -461,6 +463,7 @@ test_expect_success 'move dir from in-cone to out-of-cone' '
461463
test_expect_success 'move partially-dirty dir from in-cone to out-of-cone' '
462464
test_when_finished "cleanup_sparse_checkout" &&
463465
setup_sparse_checkout &&
466+
mkdir sub/dir/deep &&
464467
touch sub/dir/e2 sub/dir/e3 &&
465468
git add sub/dir/e2 sub/dir/e3 &&
466469
echo "modified" >>sub/dir/e2 &&
@@ -476,6 +479,7 @@ test_expect_success 'move partially-dirty dir from in-cone to out-of-cone' '
476479
477480
git mv --sparse sub/dir folder1 2>stderr &&
478481
482+
test_path_is_missing sub/dir &&
479483
test_path_is_missing folder1/dir/e &&
480484
test_path_is_file folder1/dir/e2 &&
481485
test_path_is_file folder1/dir/e3 &&

0 commit comments

Comments
 (0)