Skip to content

Commit ea640cc

Browse files
trastgitster
authored andcommitted
submodule: fix relative url parsing for scp-style origin
The function resolve_relative_url was not prepared to deal with an scp-style origin 'user@host:path' in the case where 'path' is only a single component. Fix this by extending the logic that strips one path component from the $remoteurl. Also add tests for both styles of URLs. Noticed-by: Jeffrey Phillips Freeman <[email protected]> Signed-off-by: Thomas Rast <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cb198b3 commit ea640cc

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

git-submodule.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,24 @@ resolve_relative_url ()
3636
die "remote ($remote) does not have a url defined in .git/config"
3737
url="$1"
3838
remoteurl=${remoteurl%/}
39+
sep=/
3940
while test -n "$url"
4041
do
4142
case "$url" in
4243
../*)
4344
url="${url#../}"
44-
remoteurl="${remoteurl%/*}"
45+
case "$remoteurl" in
46+
*/*)
47+
remoteurl="${remoteurl%/*}"
48+
;;
49+
*:*)
50+
remoteurl="${remoteurl%:*}"
51+
sep=:
52+
;;
53+
*)
54+
die "cannot strip one component off url '$remoteurl'"
55+
;;
56+
esac
4557
;;
4658
./*)
4759
url="${url#./}"
@@ -50,7 +62,7 @@ resolve_relative_url ()
5062
break;;
5163
esac
5264
done
53-
echo "$remoteurl/${url%/}"
65+
echo "$remoteurl$sep${url%/}"
5466
}
5567

5668
#

t/t7400-submodule-basic.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,42 @@ test_expect_success 'add submodules without specifying an explicit path' '
331331
git config -f .gitmodules submodule.bare.path bare
332332
'
333333

334+
test_expect_success 'set up for relative path tests' '
335+
mkdir reltest &&
336+
(
337+
cd reltest &&
338+
git init &&
339+
mkdir sub &&
340+
(
341+
cd sub &&
342+
git init &&
343+
test_commit foo
344+
) &&
345+
git add sub &&
346+
git config -f .gitmodules submodule.sub.path sub &&
347+
git config -f .gitmodules submodule.sub.url ../subrepo &&
348+
cp .git/config pristine-.git-config
349+
)
350+
'
351+
352+
test_expect_success 'relative path works with URL' '
353+
(
354+
cd reltest &&
355+
cp pristine-.git-config .git/config &&
356+
git config remote.origin.url ssh://hostname/repo &&
357+
git submodule init &&
358+
test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
359+
)
360+
'
361+
362+
test_expect_success 'relative path works with user@host:path' '
363+
(
364+
cd reltest &&
365+
cp pristine-.git-config .git/config &&
366+
git config remote.origin.url user@host:repo &&
367+
git submodule init &&
368+
test "$(git config submodule.sub.url)" = user@host:subrepo
369+
)
370+
'
371+
334372
test_done

0 commit comments

Comments
 (0)