Can I link git submodules with some kind of fallback URL? If SSH clone fails, git should be able to clone using https
Asked Answered
A

2

9

(This is something that's cropped up for me mainly when using GitHub, so I don't mind too much if any solution is GitHub-specific. But the problem isn't GitHub-specific per se, and any solution needn't necessarily be either.)

You can always clone a public GitHub repo using https:

git clone https://github.com/my-user-name/SDL-mirror

If you have an SSH key that GitHub knows about, you can also clone the repo using SSH:

git clone [email protected]:my-user-name/SDL-mirror.git

(I think similar options apply when using Bitbucket.)

You have these two options when adding a submodule, too.

When adding a submodule, you'll often add the SSH version, so you can commit directly from the submodule after testing your changes in situ.

But what about people trying to clone your repo? They might not have SSH access to GitHub. So they'll clone your repo using the HTTPS URL... and then come unstuck when Git tries to clone the submodules using the SSH URLs.

This can also be an issue if you use a Git repo for system provisioning.

Ideally, I'd like Git to pull from the SSH URL if possible, and the HTTPS URL if not. That should make things work out of the box for everybody. Is there some way I can get it to do that?

Atonsah answered 5/11, 2016 at 0:35 Comment(0)
O
9

What you can do is:

That way, for your local repo, you are always using ssh, but for anyone cloning your repo, there are only https references.

Organography answered 5/11, 2016 at 7:48 Comment(0)
D
0

I made a bash script that can clone such repository without ssh permissions.
It first clones the repository normally, and before it pulls all the submodules, it substitutes all ssh-based urls with https based urls in .gitmodules.
usage: gitclone.sh <https-based-url>

gitclone.sh

#!/bin/bash

# first extracting the repository name
rep_regex="\\/([^\\/]*)\\.git$"
if [[ $1 =~ $rep_regex ]]
then
    reponame="${BASH_REMATCH[1]}"
else
    echo "$1 doesn't match the know pattern" >&2
    exit
fi

echo $reponame

rm -rf $reponame
git clone $1
cd $reponame
ls
sed -E "s/git@([^\:]+):/https:\/\/\1\//g" .gitmodules > .gitmodules1
mv .gitmodules1 .gitmodules
git submodule update --init --recursive
Demello answered 21/2, 2022 at 9:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.