mv: allow moving nested submodules

When directories are moved using `git mv` all files in the directory have been just moved, but no further action was taken on them. This was done by assigning the mode = WORKING_DIRECTORY to the files inside a moved directory. submodules however need to update their link to the git directory as well as updates to the .gitmodules file. By removing the condition of `mode != INDEX` (the remaining modes are BOTH and WORKING_DIRECTORY) for the required submodule actions, we perform these for submodules in a moved directory. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Apr 19, 2016 at 11:32 UTC a127331cd812336235cb95b45b7e4c52c433be7f
2 files changed +28 -9
builtin/mv.c
+12 -9
@@ -251,15 +251,18 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
251 int pos;
252 if (show_only || verbose)
253 printf(_("Renaming %s to %s\n"), src, dst);
254 - if (!show_only && mode != INDEX) {
255 - if (rename(src, dst) < 0 && !ignore_errors)
256 - die_errno(_("renaming '%s' failed"), src);
257 - if (submodule_gitfile[i]) {
258 - if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
259 - connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
260 - if (!update_path_in_gitmodules(src, dst))
261 - gitmodules_modified = 1;
262 - }
254 + if (show_only)
255 + continue;
256 + if (mode != INDEX && rename(src, dst) < 0) {
257 + if (ignore_errors)
258 + continue;
259 + die_errno(_("renaming '%s' failed"), src);
260 + }
261 + if (submodule_gitfile[i]) {
262 + if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
263 + connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
264 + if (!update_path_in_gitmodules(src, dst))
265 + gitmodules_modified = 1;
266 }
267
268 if (mode == WORKING_DIRECTORY)
t/t7001-mv.sh
+16
@@ -292,6 +292,9 @@ test_expect_success 'setup submodule' '
292 echo content >file &&
293 git add file &&
294 git commit -m "added sub and file" &&
295 + mkdir -p deep/directory/hierachy &&
296 + git submodule add ./. deep/directory/hierachy/sub &&
297 + git commit -m "added another submodule" &&
298 git branch submodule
299 '
300
@@ -475,4 +478,17 @@ test_expect_success 'mv -k does not accidentally destroy submodules' '
478 git checkout .
479 '
480
481 +test_expect_success 'moving a submodule in nested directories' '
482 + (
483 + cd deep &&
484 + git mv directory ../ &&
485 + # git status would fail if the update of linking git dir to
486 + # work dir of the submodule failed.
487 + git status &&
488 + git config -f ../.gitmodules submodule.deep/directory/hierachy/sub.path >../actual &&
489 + echo "directory/hierachy/sub" >../expect
490 + ) &&
491 + test_cmp actual expect
492 +'
493 +
494 test_done