Code

submodules: always use a relative path from gitdir to work tree
[git.git] / git-submodule.sh
index 9bb2e13e929c824a75c648966aaaa61672b9c445..c405caaa0ee6998c4bdd39f97c7d53edcdcbea48 100755 (executable)
@@ -160,18 +160,33 @@ module_clone()
        if test -d "$gitdir"
        then
                mkdir -p "$path"
-               echo "gitdir: $rel_gitdir" >"$path/.git"
                rm -f "$gitdir/index"
        else
                mkdir -p "$gitdir_base"
-               if test -n "$reference"
-               then
-                       git-clone $quiet "$reference" -n "$url" "$path" --separate-git-dir "$gitdir"
-               else
-                       git-clone $quiet -n "$url" "$path" --separate-git-dir "$gitdir"
-               fi ||
+               git clone $quiet -n ${reference:+"$reference"} \
+                       --separate-git-dir "$gitdir" "$url" "$path" ||
                die "$(eval_gettext "Clone of '\$url' into submodule path '\$path' failed")"
        fi
+
+       echo "gitdir: $rel_gitdir" >"$path/.git"
+
+       a=$(cd "$gitdir" && pwd)/
+       b=$(cd "$path" && pwd)/
+       # Remove all common leading directories after a sanity check
+       if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
+               die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
+       fi
+       while test "${a%%/*}" = "${b%%/*}"
+       do
+               a=${a#*/}
+               b=${b#*/}
+       done
+       # Now chop off the trailing '/'s that were added in the beginning
+       a=${a%/}
+       b=${b%/}
+
+       rel=$(echo $a | sed -e 's|[^/]*|..|g')
+       (clear_local_git_env; cd "$path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
 }
 
 #