Merge branch 'js/submodule-relative'

Teach "git submodule" deal with nested submodule structure where a
module is contained within a module whose origin is specified as a
relative URL to its superproject's origin.
This commit is contained in:
Junio C Hamano 2012-06-28 15:20:55 -07:00
Родитель 30e8e6fdea c517e73d0f
Коммит a3fbb2350d
3 изменённых файлов: 289 добавлений и 10 удалений

Просмотреть файл

@ -30,7 +30,22 @@ nofetch=
update= update=
prefix= prefix=
# Resolve relative url by appending to parent's url # The function takes at most 2 arguments. The first argument is the
# URL that navigates to the submodule origin repo. When relative, this URL
# is relative to the superproject origin URL repo. The second up_path
# argument, if specified, is the relative path that navigates
# from the submodule working tree to the superproject working tree.
#
# The output of the function is the origin URL of the submodule.
#
# The output will either be an absolute URL or filesystem path (if the
# superproject origin URL is an absolute URL or filesystem path,
# respectively) or a relative file system path (if the superproject
# origin URL is a relative file system path).
#
# When the output is a relative file system path, the path is either
# relative to the submodule working tree, if up_path is specified, or to
# the superproject working tree otherwise.
resolve_relative_url () resolve_relative_url ()
{ {
remote=$(get_default_remote) remote=$(get_default_remote)
@ -39,6 +54,21 @@ resolve_relative_url ()
url="$1" url="$1"
remoteurl=${remoteurl%/} remoteurl=${remoteurl%/}
sep=/ sep=/
up_path="$2"
case "$remoteurl" in
*:*|/*)
is_relative=
;;
./*|../*)
is_relative=t
;;
*)
is_relative=t
remoteurl="./$remoteurl"
;;
esac
while test -n "$url" while test -n "$url"
do do
case "$url" in case "$url" in
@ -53,7 +83,12 @@ resolve_relative_url ()
sep=: sep=:
;; ;;
*) *)
die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")" if test -z "$is_relative" || test "." = "$remoteurl"
then
die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
else
remoteurl=.
fi
;; ;;
esac esac
;; ;;
@ -64,7 +99,8 @@ resolve_relative_url ()
break;; break;;
esac esac
done done
echo "$remoteurl$sep${url%/}" remoteurl="$remoteurl$sep${url%/}"
echo "${is_relative:+${up_path}}${remoteurl#./}"
} }
# #
@ -965,14 +1001,26 @@ cmd_sync()
# Possibly a url relative to parent # Possibly a url relative to parent
case "$url" in case "$url" in
./*|../*) ./*|../*)
url=$(resolve_relative_url "$url") || exit # rewrite foo/bar as ../.. to find path from
# submodule work tree to superproject work tree
up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
# guarantee a trailing /
up_path=${up_path%/}/ &&
# path from submodule work tree to submodule origin repo
sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
# path from superproject work tree to submodule origin repo
super_config_url=$(resolve_relative_url "$url") || exit
;;
*)
sub_origin_url="$url"
super_config_url="$url"
;; ;;
esac esac
if git config "submodule.$name.url" >/dev/null 2>/dev/null if git config "submodule.$name.url" >/dev/null 2>/dev/null
then then
say "$(eval_gettext "Synchronizing submodule url for '\$name'")" say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
git config submodule."$name".url "$url" git config submodule."$name".url "$super_config_url"
if test -e "$sm_path"/.git if test -e "$sm_path"/.git
then then
@ -980,7 +1028,7 @@ cmd_sync()
clear_local_git_env clear_local_git_env
cd "$sm_path" cd "$sm_path"
remote=$(get_default_remote) remote=$(get_default_remote)
git config remote."$remote".url "$url" git config remote."$remote".url "$sub_origin_url"
) )
fi fi
fi fi

Просмотреть файл

@ -483,21 +483,72 @@ test_expect_success 'set up for relative path tests' '
git add sub && git add sub &&
git config -f .gitmodules submodule.sub.path sub && git config -f .gitmodules submodule.sub.path sub &&
git config -f .gitmodules submodule.sub.url ../subrepo && git config -f .gitmodules submodule.sub.url ../subrepo &&
cp .git/config pristine-.git-config cp .git/config pristine-.git-config &&
cp .gitmodules pristine-.gitmodules
) )
' '
test_expect_success 'relative path works with URL' ' test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
( (
cd reltest && cd reltest &&
cp pristine-.git-config .git/config && cp pristine-.git-config .git/config &&
cp pristine-.gitmodules .gitmodules &&
git config remote.origin.url ssh://hostname/repo && git config remote.origin.url ssh://hostname/repo &&
git submodule init && git submodule init &&
test "$(git config submodule.sub.url)" = ssh://hostname/subrepo test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
) )
' '
test_expect_success 'relative path works with user@host:path' ' test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
(
cd reltest &&
cp pristine-.git-config .git/config &&
cp pristine-.gitmodules .gitmodules &&
git config remote.origin.url ssh://hostname:22/repo &&
git submodule init &&
test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
)
'
# About the choice of the path in the next test:
# - double-slash side-steps path mangling issues on Windows
# - it is still an absolute local path
# - there cannot be a server with a blank in its name just in case the
# path is used erroneously to access a //server/share style path
test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
(
cd reltest &&
cp pristine-.git-config .git/config &&
cp pristine-.gitmodules .gitmodules &&
git config remote.origin.url "//somewhere else/repo" &&
git submodule init &&
test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
)
'
test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
(
cd reltest &&
cp pristine-.git-config .git/config &&
cp pristine-.gitmodules .gitmodules &&
git config remote.origin.url file:///tmp/repo &&
git submodule init &&
test "$(git config submodule.sub.url)" = file:///tmp/subrepo
)
'
test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
(
cd reltest &&
cp pristine-.git-config .git/config &&
cp pristine-.gitmodules .gitmodules &&
git config remote.origin.url helper:://hostname/repo &&
git submodule init &&
test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
)
'
test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
( (
cd reltest && cd reltest &&
cp pristine-.git-config .git/config && cp pristine-.git-config .git/config &&
@ -507,6 +558,98 @@ test_expect_success 'relative path works with user@host:path' '
) )
' '
test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
(
cd reltest &&
cp pristine-.git-config .git/config &&
cp pristine-.gitmodules .gitmodules &&
git config remote.origin.url user@host:path/to/repo &&
git submodule init &&
test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
)
'
test_expect_success '../subrepo works with relative local path - foo' '
(
cd reltest &&
cp pristine-.git-config .git/config &&
cp pristine-.gitmodules .gitmodules &&
git config remote.origin.url foo &&
# actual: fails with an error
git submodule init &&
test "$(git config submodule.sub.url)" = subrepo
)
'
test_expect_success '../subrepo works with relative local path - foo/bar' '
(
cd reltest &&
cp pristine-.git-config .git/config &&
cp pristine-.gitmodules .gitmodules &&
git config remote.origin.url foo/bar &&
git submodule init &&
test "$(git config submodule.sub.url)" = foo/subrepo
)
'
test_expect_success '../subrepo works with relative local path - ./foo' '
(
cd reltest &&
cp pristine-.git-config .git/config &&
cp pristine-.gitmodules .gitmodules &&
git config remote.origin.url ./foo &&
git submodule init &&
test "$(git config submodule.sub.url)" = subrepo
)
'
test_expect_success '../subrepo works with relative local path - ./foo/bar' '
(
cd reltest &&
cp pristine-.git-config .git/config &&
cp pristine-.gitmodules .gitmodules &&
git config remote.origin.url ./foo/bar &&
git submodule init &&
test "$(git config submodule.sub.url)" = foo/subrepo
)
'
test_expect_success '../subrepo works with relative local path - ../foo' '
(
cd reltest &&
cp pristine-.git-config .git/config &&
cp pristine-.gitmodules .gitmodules &&
git config remote.origin.url ../foo &&
git submodule init &&
test "$(git config submodule.sub.url)" = ../subrepo
)
'
test_expect_success '../subrepo works with relative local path - ../foo/bar' '
(
cd reltest &&
cp pristine-.git-config .git/config &&
cp pristine-.gitmodules .gitmodules &&
git config remote.origin.url ../foo/bar &&
git submodule init &&
test "$(git config submodule.sub.url)" = ../foo/subrepo
)
'
test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
(
cd reltest &&
cp pristine-.git-config .git/config &&
cp pristine-.gitmodules .gitmodules &&
mkdir -p a/b/c &&
(cd a/b/c; git init) &&
git config remote.origin.url ../foo/bar.git &&
git submodule add ../bar/a/b/c ./a/b/c &&
git submodule init &&
test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
)
'
test_expect_success 'moving the superproject does not break submodules' ' test_expect_success 'moving the superproject does not break submodules' '
( (
cd addtest && cd addtest &&

Просмотреть файл

@ -26,7 +26,9 @@ test_expect_success setup '
(cd super-clone && git submodule update --init) && (cd super-clone && git submodule update --init) &&
git clone super empty-clone && git clone super empty-clone &&
(cd empty-clone && git submodule init) && (cd empty-clone && git submodule init) &&
git clone super top-only-clone git clone super top-only-clone &&
git clone super relative-clone &&
(cd relative-clone && git submodule update --init)
' '
test_expect_success 'change submodule' ' test_expect_success 'change submodule' '
@ -86,4 +88,90 @@ test_expect_success '"git submodule sync" should not vivify uninteresting submod
) )
' '
test_expect_success '"git submodule sync" handles origin URL of the form foo' '
(cd relative-clone &&
git remote set-url origin foo &&
git submodule sync &&
(cd submodule &&
#actual fails with: "cannot strip off url foo
test "$(git config remote.origin.url)" = "../submodule"
)
)
'
test_expect_success '"git submodule sync" handles origin URL of the form foo/bar' '
(cd relative-clone &&
git remote set-url origin foo/bar &&
git submodule sync &&
(cd submodule &&
#actual foo/submodule
test "$(git config remote.origin.url)" = "../foo/submodule"
)
)
'
test_expect_success '"git submodule sync" handles origin URL of the form ./foo' '
(cd relative-clone &&
git remote set-url origin ./foo &&
git submodule sync &&
(cd submodule &&
#actual ./submodule
test "$(git config remote.origin.url)" = "../submodule"
)
)
'
test_expect_success '"git submodule sync" handles origin URL of the form ./foo/bar' '
(cd relative-clone &&
git remote set-url origin ./foo/bar &&
git submodule sync &&
(cd submodule &&
#actual ./foo/submodule
test "$(git config remote.origin.url)" = "../foo/submodule"
)
)
'
test_expect_success '"git submodule sync" handles origin URL of the form ../foo' '
(cd relative-clone &&
git remote set-url origin ../foo &&
git submodule sync &&
(cd submodule &&
#actual ../submodule
test "$(git config remote.origin.url)" = "../../submodule"
)
)
'
test_expect_success '"git submodule sync" handles origin URL of the form ../foo/bar' '
(cd relative-clone &&
git remote set-url origin ../foo/bar &&
git submodule sync &&
(cd submodule &&
#actual ../foo/submodule
test "$(git config remote.origin.url)" = "../../foo/submodule"
)
)
'
test_expect_success '"git submodule sync" handles origin URL of the form ../foo/bar with deeply nested submodule' '
(cd relative-clone &&
git remote set-url origin ../foo/bar &&
mkdir -p a/b/c &&
( cd a/b/c &&
git init &&
:> .gitignore &&
git add .gitignore &&
test_tick &&
git commit -m "initial commit" ) &&
git submodule add ../bar/a/b/c ./a/b/c &&
git submodule sync &&
(cd a/b/c &&
#actual ../foo/bar/a/b/c
test "$(git config remote.origin.url)" = "../../../../foo/bar/a/b/c"
)
)
'
test_done test_done