Skip to contentSkip to conversation

'git mv' doesn't move submodule if it's in a subdirectory

PATCH New Albin Otterhäll 3 participants 7 messages Jun 16, 2016

lore
Share & tools
lore
#0 lore raw

I've a submodule located in a subdirectory ({git_rep}/home/{directory}/{submodule}), and I wanted to move the whole directory up a level ({git_rep}/{directory}/{submodule}). But when I used 'git mv {directory} ../' the '.gitmodule' file didn't get modified.

Best regards,
Albin Otterhäll

#1 lore raw
Quoted 7 lines · Albin Otterhäll

I've a submodule located in a subdirectory ({git_rep}/home/{directory}/{submodule}), and I wanted to move the whole directory up a level ({git_rep}/{directory}/{submodule}). But when I used 'git mv {directory} ../' the '.gitmodule' file didn't get modified.

Best regards,
Albin Otterhäll

Thanks for the bug report!
Which version of Git do you use? (Did you try different versions?)

#2 lore raw
Quoted 11 lines · Stefan Beller
Quoted 7 lines · Albin Otterhäll

I've a submodule located in a subdirectory ({git_rep}/home/{directory}/{submodule}), and I wanted to move the whole directory up a level ({git_rep}/{directory}/{submodule}). But when I used 'git mv {directory} ../' the '.gitmodule' file didn't get modified.

Best regards,
Albin Otterhäll

Thanks for the bug report!
Which version of Git do you use? (Did you try different versions?)

I think I can reproduce the problem. A regression test (which currently fails) could look like

t/t7001-mv.sh +3 -0
index 4008fae..3b96a9a 100755
@@ -292,6 +292,9 @@ test_expect_success 'setup submodule' '
        echo content >file &&
        git add file &&
        git commit -m "added sub and file" &&
+       mkdir -p deep/directory/hierachy &&
+       git submodule add ./. deep/directory/hierachy/sub &&
+       git commit -m "added another submodule" &&
        git branch submodule
 '
 
@@ -475,4 +478,14 @@ test_expect_success 'mv -k does not accidentally

destroy submodules' '
git checkout .
'

+test_expect_failure 'moving a submodule in nested directories' '
+ (

+               cd deep &&
+               git mv directory ../ &&
+               git status
+               # currently git status exits with 128
+               # fatal: Not a git repository:

directory/hierachy/sub/../../../../.git/modules/deep/directory/hierachy/sub
+ )
+'
+
test_done

#3 lore raw
Quoted 2 lines · Stefan Beller

I think I can reproduce the problem. A regression test (which currently fails) could look like

Thanks. I however do not think this is a regression.

Changes around 0656781f (mv: update the path entry in .gitmodules for moved submodules, 2013-08-06) did introduce "git mv dir1 dir2" when 'dir1' is a submodule, but I do not think it went beyond that. I do not see any effort to treat a submodule that is discovered by scanning a directory that was given from the command line, i.e. prepare_move_submodule() is not called for them, and the entries in the submodule_gitfile[] array that correspond to them are explicitly set to NULL in the loop.

Quoted 31 lines
t/t7001-mv.sh +3 -0
index 4008fae..3b96a9a 100755
@@ -292,6 +292,9 @@ test_expect_success 'setup submodule' '
        echo content >file &&
        git add file &&
        git commit -m "added sub and file" &&
+       mkdir -p deep/directory/hierachy &&
+       git submodule add ./. deep/directory/hierachy/sub &&
+       git commit -m "added another submodule" &&
        git branch submodule
 '
 
@@ -475,4 +478,14 @@ test_expect_success 'mv -k does not accidentally

destroy submodules' '
git checkout .
'

t/t7001-mv.sh +7 -0
+test_expect_failure 'moving a submodule in nested directories' '
+       (
+               cd deep &&
+               git mv directory ../ &&
+               git status
+               # currently git status exits with 128
+               # fatal: Not a git repository:

directory/hierachy/sub/../../../../.git/modules/deep/directory/hierachy/sub

t/t7001-mv.sh +3 -0
+       )
+'
+
 test_done
#4 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 2 lines · Stefan Beller

I think I can reproduce the problem. A regression test (which currently fails) could look like

Thanks. I however do not think this is a regression.

Quoted 8 lines

Changes around 0656781f (mv: update the path entry in .gitmodules for moved submodules, 2013-08-06) did introduce "git mv dir1 dir2" when 'dir1' is a submodule, but I do not think it went beyond that. I do not see any effort to treat a submodule that is discovered by scanning a directory that was given from the command line, i.e. prepare_move_submodule() is not called for them, and the entries in the submodule_gitfile[] array that correspond to them are explicitly set to NULL in the loop.

Also I just realize this is not exactly the same bug as reported. Albin complains about the .gitmodules file not being adjusted, whereas the test case I wrote breaks commands in your superproject, i.e. git status or git diff just dies.

(Manually inspecting the .gitmodules file turns out it is not adjusted as well.)

Quoted 33 lines
Quoted 33 lines
t/t7001-mv.sh +3 -0
index 4008fae..3b96a9a 100755
@@ -292,6 +292,9 @@ test_expect_success 'setup submodule' '
        echo content >file &&
        git add file &&
        git commit -m "added sub and file" &&
+       mkdir -p deep/directory/hierachy &&
+       git submodule add ./. deep/directory/hierachy/sub &&
+       git commit -m "added another submodule" &&
        git branch submodule
 '
 
@@ -475,4 +478,14 @@ test_expect_success 'mv -k does not accidentally

destroy submodules' '
git checkout .
'

t/t7001-mv.sh +7 -0
+test_expect_failure 'moving a submodule in nested directories' '
+       (
+               cd deep &&
+               git mv directory ../ &&
+               git status
+               # currently git status exits with 128
+               # fatal: Not a git repository:

directory/hierachy/sub/../../../../.git/modules/deep/directory/hierachy/sub

t/t7001-mv.sh +3 -0
+       )
+'
+
 test_done
#5 lore raw

PATCH 1/1 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.

---

Albin,
I think this would fix your problem.

Developed on origin/master (it may apply on older versions, though I did not test)

Thanks,
Stefan

 builtin/mv.c  |  7 +++++--
 t/t7001-mv.sh | 16 ++++++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)
builtin/mv.c +5 -2
index aeae855..65fff11 100644
@@ -252,9 +252,12 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		int pos;
 		if (show_only || verbose)
 			printf(_("Renaming %s to %s\n"), src, dst);
-		if (!show_only && mode != INDEX) {
-			if (rename(src, dst) < 0 && !ignore_errors)
+		if (!show_only) {
+			if (mode != INDEX &&
+			    rename(src, dst) < 0 &&
+			    !ignore_errors)
 				die_errno(_("renaming '%s' failed"), src);
+
 			if (submodule_gitfile[i]) {
 				if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
 					connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
t/t7001-mv.sh +16 -0
index 4008fae..4a2570e 100755
@@ -292,6 +292,9 @@ test_expect_success 'setup submodule' '
 	echo content >file &&
 	git add file &&
 	git commit -m "added sub and file" &&
+	mkdir -p deep/directory/hierachy &&
+	git submodule add ./. deep/directory/hierachy/sub &&
+	git commit -m "added another submodule" &&
 	git branch submodule
 '
 
@@ -475,4 +478,17 @@ test_expect_success 'mv -k does not accidentally destroy submodules' '
 	git checkout .
 '
 
+test_expect_success 'moving a submodule in nested directories' '
+	(
+		cd deep &&
+		git mv directory ../ &&
+		# git status would fail if the update of linking git dir to
+		# work dir of the submodule failed.
+		git status &&
+		git config -f ../.gitmodules submodule.deep/directory/hierachy/sub.path >../actual &&
+		echo "directory/hierachy/sub" >../expect
+	) &&
+	test_cmp actual expect
+'
+
 test_done
Signature · 1 line
2.8.1.211.g630c034
#6 lore raw
In reply to Stefan Beller
Quoted 6 lines · Stefan Beller
Quoted 5 lines · Albin Otterhäll

I've a submodule located in a subdirectory ({git_rep}/home/{directory}/{submodule}), and I wanted to move the whole directory up a level ({git_rep}/{directory}/{submodule}). But when I used 'git mv {directory} ../' the '.gitmodule' file didn't get modified.

Quoted 6 lines
Quoted 2 lines

Best regards,
Albin Otterhäll

Thanks for the bug report!
Which version of Git do you use? (Did you try different versions?)

I'm using 2.8.0 (on an Arch system). Haven't tested on any other version.