submodules: always use a relative path from gitdir to work tree

Since recently a submodule with name <name> has its git directory in the .git/modules/<name> directory of the superproject while the work tree contains a gitfile pointing there. To make that work the git directory has the core.worktree configuration set in its config file to point back to the work tree. That core.worktree is an absolute path set by the initial clone of the submodule. A relative path is preferable here because it allows the superproject to be moved around without invalidating that setting, so compute and set that relative path after cloning or reactivating the submodule. This also fixes a bug when moving a submodule around inside the superproject, as the current code forgot to update the setting to the new submodule work tree location. Enhance t7400 to ensure that future versions won't re-add absolute paths by accident and that moving a superproject won't break submodules. Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jens Lehmann committed Mar 4, 2012 at 22:15 UTC d75219b4a8a9c44520ddca234cde992498383b89
2 files changed +38
git-submodule.sh
+18
@@ -169,6 +169,24 @@ module_clone()
169 fi
170
171 echo "gitdir: $rel_gitdir" >"$path/.git"
172 +
173 + a=$(cd "$gitdir" && pwd)/
174 + b=$(cd "$path" && pwd)/
175 + # Remove all common leading directories after a sanity check
176 + if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
177 + die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
178 + fi
179 + while test "${a%%/*}" = "${b%%/*}"
180 + do
181 + a=${a#*/}
182 + b=${b#*/}
183 + done
184 + # Now chop off the trailing '/'s that were added in the beginning
185 + a=${a%/}
186 + b=${b%/}
187 +
188 + rel=$(echo $a | sed -e 's|[^/]*|..|g')
189 + (clear_local_git_env; cd "$path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
190 }
191
192 #
t/t7400-submodule-basic.sh
+20
@@ -81,6 +81,13 @@ test_expect_success 'submodule add' '
81 test ! -s actual &&
82 echo "gitdir: ../.git/modules/submod" >expect &&
83 test_cmp expect submod/.git &&
84 + (
85 + cd submod &&
86 + git config core.worktree >actual &&
87 + echo "../../../submod" >expect &&
88 + test_cmp expect actual &&
89 + rm -f actual expect
90 + ) &&
91 git submodule init
92 ) &&
93
@@ -500,4 +507,17 @@ test_expect_success 'relative path works with user@host:path' '
507 )
508 '
509
510 +test_expect_success 'moving the superproject does not break submodules' '
511 + (
512 + cd addtest &&
513 + git submodule status >expect
514 + )
515 + mv addtest addtest2 &&
516 + (
517 + cd addtest2 &&
518 + git submodule status >actual &&
519 + test_cmp expect actual
520 + )
521 +'
522 +
523 test_done