# Submodule using different repository URLs

8 messages from 2009-07-13 to 2009-07-13. Participants: Peter Voss, Santi Béjar, Johan Herland.
Thread: https://gitlist.dev/t/20097

## Peter Voss, 2009-07-13 09:57

Subject: Submodule using different repository URLs
Message-ID: <5BE6F3DC-4B00-4D84-8D0E-41057735483F@petervoss.org>
URL: https://gitlist.dev/e/5BE6F3DC-4B00-4D84-8D0E-41057735483F%40petervoss.org

```
Hi,

I want to use the git submodule feature to move part of my code to a  
different repository at github.

The issue is that developers should use different repository URLs for  
the submodule depending on whether they have commit rights or not.

At the beginning I was using the public URL to set-up the submodule:
git submodule add git://github.com/x/mymodule.git mymodule

The issue is that some developers are working behind a firewall that  
blocks the git protocol. These could only use the git@github.com:x/ 
mymodule.git URL to get access.
But other developers can only go through the public URL git:// 
github.com/x/mymodule.git. So whatever I use it won't work for  
everybody.

What's the best way to deal with that? Could I set-up different  
repository URLs for one and the same submodule and use which one is  
appropriate?

Thanks,
--Peter

```

## Santi Béjar, 2009-07-13 10:20

Subject: Re: Submodule using different repository URLs
Message-ID: <adf1fd3d0907130320s726bb80at58d454c830c7d5be@mail.gmail.com>
URL: https://gitlist.dev/e/adf1fd3d0907130320s726bb80at58d454c830c7d5be%40mail.gmail.com
In-Reply-To: <5BE6F3DC-4B00-4D84-8D0E-41057735483F@petervoss.org>

```
2009/7/13 Peter Voss <info@petervoss.org>:
> Hi,
>
> I want to use the git submodule feature to move part of my code to a
> different repository at github.
>
> The issue is that developers should use different repository URLs for the
> submodule depending on whether they have commit rights or not.
>
> At the beginning I was using the public URL to set-up the submodule:
> git submodule add git://github.com/x/mymodule.git mymodule
>
> The issue is that some developers are working behind a firewall that blocks
> the git protocol. These could only use the git@github.com:x/mymodule.git URL
> to get access.
> But other developers can only go through the public URL
> git://github.com/x/mymodule.git. So whatever I use it won't work for
> everybody.
>
> What's the best way to deal with that? Could I set-up different repository
> URLs for one and the same submodule and use which one is appropriate?

After the "git submodule init" you can customize the url. From "man
git-submodule":

init::
        Initialize the submodules, i.e. register each submodule name
        and url found in .gitmodules into .git/config.
        The key used in .git/config is `submodule.$name.url`.
        This command does not alter existing information in .git/config.
        You can then customize the submodule clone URLs in .git/config
        for your local setup and proceed to 'git submodule update';
        you can also just use 'git submodule update --init' without
        the explicit 'init' step if you do not intend to customize
        any submodule locations.

You can also use the config url."<actual url base>".insteadOf = <other
url base>.
See the git-pull manpage for examples.

HTH,
Santi

```

## Johan Herland, 2009-07-13 10:59

Subject: Re: Submodule using different repository URLs
Message-ID: <200907131259.42313.johan@herland.net>
URL: https://gitlist.dev/e/200907131259.42313.johan%40herland.net
In-Reply-To: <5BE6F3DC-4B00-4D84-8D0E-41057735483F@petervoss.org>

```
On Monday 13 July 2009, Peter Voss wrote:
> Hi,
>
> I want to use the git submodule feature to move part of my code to a
> different repository at github.
>
> The issue is that developers should use different repository URLs for
> the submodule depending on whether they have commit rights or not.
>
> At the beginning I was using the public URL to set-up the submodule:
> git submodule add git://github.com/x/mymodule.git mymodule
>
> The issue is that some developers are working behind a firewall that
> blocks the git protocol. These could only use the git@github.com:x/
> mymodule.git URL to get access.
> But other developers can only go through the public URL git://
> github.com/x/mymodule.git. So whatever I use it won't work for
> everybody.
>
> What's the best way to deal with that? Could I set-up different
> repository URLs for one and the same submodule and use which one is
> appropriate?

You might be able to pull this off using relative submodule URLs. If the 
submodule URLs in .gitmodules are relative (i.e. ../foo.git or 
similar), they will be resolved to absolute URLs using the origin URL 
of the super-repo. I.e. if you cloned the super-repo from 
git://github.com/x/mymodule.git, the ../foo.git submodule will be 
cloned from git://github.com/x/foo.git, and if you cloned from 
git@github.com:x/mymodule.git, the submodule will be cloned from 
git@github.com:x/foo.git.

Hope this helps,


Have fun! :)

...Johan


-- 
Johan Herland, <johan@herland.net>
www.herland.net

```

## Peter Voss, 2009-07-13 11:51

Subject: Re: Submodule using different repository URLs
Message-ID: <64652870-CF95-49A7-920A-0F9A6C202B40@petervoss.org>
URL: https://gitlist.dev/e/64652870-CF95-49A7-920A-0F9A6C202B40%40petervoss.org
In-Reply-To: <200907131259.42313.johan@herland.net>

```
Hi Johan,

On 13.07.2009, at 12:59, Johan Herland wrote:

> On Monday 13 July 2009, Peter Voss wrote:
>> Hi,
>>
>> I want to use the git submodule feature to move part of my code to a
>> different repository at github.
>>
>> The issue is that developers should use different repository URLs for
>> the submodule depending on whether they have commit rights or not.
>>
>> At the beginning I was using the public URL to set-up the submodule:
>> git submodule add git://github.com/x/mymodule.git mymodule
>>
>> The issue is that some developers are working behind a firewall that
>> blocks the git protocol. These could only use the git@github.com:x/
>> mymodule.git URL to get access.
>> But other developers can only go through the public URL git://
>> github.com/x/mymodule.git. So whatever I use it won't work for
>> everybody.
>>
>> What's the best way to deal with that? Could I set-up different
>> repository URLs for one and the same submodule and use which one is
>> appropriate?
>
> You might be able to pull this off using relative submodule URLs. If  
> the
> submodule URLs in .gitmodules are relative (i.e. ../foo.git or
> similar), they will be resolved to absolute URLs using the origin URL
> of the super-repo. I.e. if you cloned the super-repo from
> git://github.com/x/mymodule.git, the ../foo.git submodule will be
> cloned from git://github.com/x/foo.git, and if you cloned from
> git@github.com:x/mymodule.git, the submodule will be cloned from
> git@github.com:x/foo.git.

That's a good hint. Unfortunately I can't use this to go up 2  
directories. I.e. I can't get from
git@github.com:xxx/mymodule.git
to
git@github.com:yyy/foo.git

Using the relative URL ../../yyy/foo.git leads to the result:
Clone of 'git@github.com:xxx/yyy/foo.git' into submodule path 'foo'  
failed

So I basically cannot replace the xxx part.

Anyway, thanks for the hint.
--Peter

> Hope this helps,
>
>
> Have fun! :)
>
> ...Johan
>
>
> -- 
> Johan Herland, <johan@herland.net>
> www.herland.net

```

## Peter Voss, 2009-07-13 12:04

Subject: Re: Submodule using different repository URLs
Message-ID: <EDAD14B2-414A-436B-BA85-CBD9A13B2B87@petervoss.org>
URL: https://gitlist.dev/e/EDAD14B2-414A-436B-BA85-CBD9A13B2B87%40petervoss.org
In-Reply-To: <adf1fd3d0907130320s726bb80at58d454c830c7d5be@mail.gmail.com>

```
Hi Santi,

On 13.07.2009, at 12:20, Santi Béjar wrote:

> 2009/7/13 Peter Voss <info@petervoss.org>:
>> Hi,
>>
>> I want to use the git submodule feature to move part of my code to a
>> different repository at github.
>>
>> The issue is that developers should use different repository URLs  
>> for the
>> submodule depending on whether they have commit rights or not.
>>
>> At the beginning I was using the public URL to set-up the submodule:
>> git submodule add git://github.com/x/mymodule.git mymodule
>>
>> The issue is that some developers are working behind a firewall  
>> that blocks
>> the git protocol. These could only use the git@github.com:x/ 
>> mymodule.git URL
>> to get access.
>> But other developers can only go through the public URL
>> git://github.com/x/mymodule.git. So whatever I use it won't work for
>> everybody.
>>
>> What's the best way to deal with that? Could I set-up different  
>> repository
>> URLs for one and the same submodule and use which one is appropriate?
>
> After the "git submodule init" you can customize the url. From "man
> git-submodule":
>
> init::
>        Initialize the submodules, i.e. register each submodule name
>        and url found in .gitmodules into .git/config.
>        The key used in .git/config is `submodule.$name.url`.
>        This command does not alter existing information in .git/ 
> config.
>        You can then customize the submodule clone URLs in .git/config
>        for your local setup and proceed to 'git submodule update';
>        you can also just use 'git submodule update --init' without
>        the explicit 'init' step if you do not intend to customize
>        any submodule locations.
>
> You can also use the config url."<actual url base>".insteadOf = <other
> url base>.
> See the git-pull manpage for examples.

I didn't get the insteadOf config running (maybe I have to update to a  
more recent version of git). But modifying the URL in .git/config  
should be a doable approach for us. In order to add the insteadOf  
section we would have to edit the .git/config file as well, so that  
doesn't seem to be much better.

Thanks for your help,
--Peter

> HTH,
> Santi

```

## Johan Herland, 2009-07-13 12:28

Subject: Re: Submodule using different repository URLs
Message-ID: <200907131428.43652.johan@herland.net>
URL: https://gitlist.dev/e/200907131428.43652.johan%40herland.net
In-Reply-To: <64652870-CF95-49A7-920A-0F9A6C202B40@petervoss.org>

```
On Monday 13 July 2009, Peter Voss wrote:
> On 13.07.2009, at 12:59, Johan Herland wrote:
> > You might be able to pull this off using relative submodule URLs.
>
> That's a good hint. Unfortunately I can't use this to go up 2
> directories. I.e. I can't get from
> git@github.com:xxx/mymodule.git
> to
> git@github.com:yyy/foo.git
>
> Using the relative URL ../../yyy/foo.git leads to the result:
> Clone of 'git@github.com:xxx/yyy/foo.git' into submodule path 'foo'
> failed
>
> So I basically cannot replace the xxx part.

This is due to a small bug in git-submodule.sh. In the 
resolve_relative_url() function, when repeatedly unwrapping '../'s from 
$url in the while loop, the line remoteurl="${remoteurl%/*}" removes 
everything _after_ the last slash, which does not work for the 
git@github.com:xxx part of your URL.

This should be relatively easy to fix, although maybe not as 
straightforwardly trivial as it might seem at first sight.

I don't have the time to look into this now, so feel free to take a stab 
at it.


Have fun! :)

...Johan


-- 
Johan Herland, <johan@herland.net>
www.herland.net

```

## Johan Herland, 2009-07-13 12:43

Subject: Re: Submodule using different repository URLs
Message-ID: <200907131443.25719.johan@herland.net>
URL: https://gitlist.dev/e/200907131443.25719.johan%40herland.net
In-Reply-To: <200907131428.43652.johan@herland.net>

```
On Monday 13 July 2009, Johan Herland wrote:
> On Monday 13 July 2009, Peter Voss wrote:
> > On 13.07.2009, at 12:59, Johan Herland wrote:
> > > You might be able to pull this off using relative submodule URLs.
> >
> > That's a good hint. Unfortunately I can't use this to go up 2
> > directories. I.e. I can't get from
> > git@github.com:xxx/mymodule.git
> > to
> > git@github.com:yyy/foo.git
> >
> > Using the relative URL ../../yyy/foo.git leads to the result:
> > Clone of 'git@github.com:xxx/yyy/foo.git' into submodule path 'foo'
> > failed
> >
> > So I basically cannot replace the xxx part.
>
> This is due to a small bug in git-submodule.sh. In the
> resolve_relative_url() function, when repeatedly unwrapping '../'s
> from $url in the while loop, the line remoteurl="${remoteurl%/*}"
> removes everything _after_ the last slash, which does not work for
> the git@github.com:xxx part of your URL.
>
> This should be relatively easy to fix, although maybe not as
> straightforwardly trivial as it might seem at first sight.
>
> I don't have the time to look into this now, so feel free to take a
> stab at it.

Here's a first stab at it (TOTALLY UNTESTED). Obviously it needs a
testcase (typically in t7400-submodule-basic.sh) as well.


Have fun! :)

...Johan


diff --git a/git-submodule.sh b/git-submodule.sh
index ebed711..7d8f7a7 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -27,12 +27,19 @@ resolve_relative_url ()
 		die "remote ($remote) does not have a url defined in .git/config"
 	url="$1"
 	remoteurl=${remoteurl%/}
+	sep="/"
 	while test -n "$url"
 	do
 		case "$url" in
 		../*)
 			url="${url#../}"
-			remoteurl="${remoteurl%/*}"
+			newremoteurl="${remoteurl%/*}"
+			if test "$newremoteurl" = "$remoteurl" -a "$sep" = "/"
+			then
+				newremoteurl="${remoteurl%:*}"
+				sep=":"
+			fi
+			remoteurl="$newremoteurl"
 			;;
 		./*)
 			url="${url#./}"
@@ -41,7 +48,7 @@ resolve_relative_url ()
 			break;;
 		esac
 	done
-	echo "$remoteurl/${url%/}"
+	echo "$remoteurl$sep${url%/}"
 }
 
 #


-- 
Johan Herland, <johan@herland.net>
www.herland.net

```

## Peter Voss, 2009-07-13 13:33

Subject: Re: Submodule using different repository URLs
Message-ID: <9B026405-27EB-4D48-8D08-461D213453C3@petervoss.org>
URL: https://gitlist.dev/e/9B026405-27EB-4D48-8D08-461D213453C3%40petervoss.org
In-Reply-To: <200907131443.25719.johan@herland.net>

```
Hi Johan,

On 13.07.2009, at 14:43, Johan Herland wrote:

> On Monday 13 July 2009, Johan Herland wrote:
>> On Monday 13 July 2009, Peter Voss wrote:
>>> On 13.07.2009, at 12:59, Johan Herland wrote:
>>>> You might be able to pull this off using relative submodule URLs.
>>>
>>> That's a good hint. Unfortunately I can't use this to go up 2
>>> directories. I.e. I can't get from
>>> git@github.com:xxx/mymodule.git
>>> to
>>> git@github.com:yyy/foo.git
>>>
>>> Using the relative URL ../../yyy/foo.git leads to the result:
>>> Clone of 'git@github.com:xxx/yyy/foo.git' into submodule path 'foo'
>>> failed
>>>
>>> So I basically cannot replace the xxx part.
>>
>> This is due to a small bug in git-submodule.sh. In the
>> resolve_relative_url() function, when repeatedly unwrapping '../'s
>> from $url in the while loop, the line remoteurl="${remoteurl%/*}"
>> removes everything _after_ the last slash, which does not work for
>> the git@github.com:xxx part of your URL.
>>
>> This should be relatively easy to fix, although maybe not as
>> straightforwardly trivial as it might seem at first sight.
>>
>> I don't have the time to look into this now, so feel free to take a
>> stab at it.
>
> Here's a first stab at it (TOTALLY UNTESTED). Obviously it needs a
> testcase (typically in t7400-submodule-basic.sh) as well.

And I thought you didn't have time. ;-) Thanks for this patch. I will  
test this out tomorrow.

Thanks,
--Peter

>
>
> Have fun! :)
>
> ...Johan
>
>
> diff --git a/git-submodule.sh b/git-submodule.sh
> index ebed711..7d8f7a7 100755
> --- a/git-submodule.sh
> +++ b/git-submodule.sh
> @@ -27,12 +27,19 @@ resolve_relative_url ()
> 		die "remote ($remote) does not have a url defined in .git/config"
> 	url="$1"
> 	remoteurl=${remoteurl%/}
> +	sep="/"
> 	while test -n "$url"
> 	do
> 		case "$url" in
> 		../*)
> 			url="${url#../}"
> -			remoteurl="${remoteurl%/*}"
> +			newremoteurl="${remoteurl%/*}"
> +			if test "$newremoteurl" = "$remoteurl" -a "$sep" = "/"
> +			then
> +				newremoteurl="${remoteurl%:*}"
> +				sep=":"
> +			fi
> +			remoteurl="$newremoteurl"
> 			;;
> 		./*)
> 			url="${url#./}"
> @@ -41,7 +48,7 @@ resolve_relative_url ()
> 			break;;
> 		esac
> 	done
> -	echo "$remoteurl/${url%/}"
> +	echo "$remoteurl$sep${url%/}"
> }
>
> #
>
>
> -- 
> Johan Herland, <johan@herland.net>
> www.herland.net

```
