Skip to content

Commit a127331

Browse files
stefanbellergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e465796 commit a127331

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

builtin/mv.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,18 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
251251
int pos;
252252
if (show_only || verbose)
253253
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;
263266
}
264267

265268
if (mode == WORKING_DIRECTORY)

t/t7001-mv.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ test_expect_success 'setup submodule' '
292292
echo content >file &&
293293
git add file &&
294294
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" &&
295298
git branch submodule
296299
'
297300

@@ -475,4 +478,17 @@ test_expect_success 'mv -k does not accidentally destroy submodules' '
475478
git checkout .
476479
'
477480

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+
478494
test_done

0 commit comments

Comments
 (0)