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 <derrickstolee@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 b6f51e3db978ae2a72c290a10bd205f9e1d6818e
2 files changed +31
builtin/mv.c
+27
@@ -171,6 +171,9 @@ 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 + const char **src_dir = NULL;
175 + int src_dir_nr = 0, src_dir_alloc = 0;
176 + struct strbuf a_src_dir = STRBUF_INIT;
177 enum update_mode *modes, dst_mode = 0;
178 struct stat st;
179 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
@@ -313,6 +316,10 @@ dir_check:
316
317 /* last - first >= 1 */
318 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 +
323 n = argc + last - first;
324 REALLOC_ARRAY(source, n);
325 REALLOC_ARRAY(destination, n);
@@ -505,6 +512,26 @@ remove_entry:
512 }
513 }
514
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 +
535 if (gitmodules_modified)
536 stage_updated_gitmodules(&the_index);
537
t/t7002-mv-sparse-checkout.sh
+4
@@ -442,6 +442,7 @@ test_expect_success 'move dirty path 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 + mkdir sub/dir/deep &&
446
447 test_must_fail git mv sub/dir folder1 2>stderr &&
448 cat sparse_error_header >expect &&
@@ -452,6 +453,7 @@ test_expect_success 'move dir from in-cone to out-of-cone' '
453 git mv --sparse sub/dir folder1 2>stderr &&
454 test_must_be_empty stderr &&
455
456 + test_path_is_missing sub/dir &&
457 test_path_is_missing folder1 &&
458 git ls-files -t >actual &&
459 ! grep "H sub/dir/e" actual &&
@@ -461,6 +463,7 @@ test_expect_success 'move dir from in-cone to out-of-cone' '
463 test_expect_success 'move partially-dirty dir from in-cone to out-of-cone' '
464 test_when_finished "cleanup_sparse_checkout" &&
465 setup_sparse_checkout &&
466 + mkdir sub/dir/deep &&
467 touch sub/dir/e2 sub/dir/e3 &&
468 git add sub/dir/e2 sub/dir/e3 &&
469 echo "modified" >>sub/dir/e2 &&
@@ -476,6 +479,7 @@ test_expect_success 'move partially-dirty dir from in-cone to out-of-cone' '
479
480 git mv --sparse sub/dir folder1 2>stderr &&
481
482 + test_path_is_missing sub/dir &&
483 test_path_is_missing folder1/dir/e &&
484 test_path_is_file folder1/dir/e2 &&
485 test_path_is_file folder1/dir/e3 &&