dimanche 16 janvier 2022

Importing git repos from exported list

Another post on that topic (see previous post), useful when you want to clone your repos on a brand new disk, but only have access to the github/lab account. (yes, disk failures happen, believe me...)

Step 1:

For Github, you can export the list of your repos with the API:

curl https://api.github.com/users/USERNAME/repos
This returns a full JSON file, but you only need the repos url. To get the SSH-way, you can do this to get a raw file holding the list of URLs:
curl https://api.github.com/users/USERNAME/repos | grep ssh_url > github_repos
In that file, you will have one line per repo, like this:
    "ssh_url": "git@github.com:USERNAME/adepopro.git",

Step 2

Now, assuming you have stored your ssh key on your Github account, you can clone all repos at once with the following script. It does some text-processing (remove quotes, spaces, ...), then a git clone.
if [ "$1" = "" ]
then
    echo "filename missing, exit"
    exit 1
fi

while IFS=":"; read f1 f2 f3
do
# remove quotes
    f2b=$(sed s/\"//g <<< $f2)
    f3b=$(sed s/\"//g <<< $f3)
# remove comma at the end    
    f3c=${f3b/,/}
# remove space at the end    
    f2c=${f2b/ /}
# build path
    p="$f2c:$f3c" 
# clone
    echo "cloning $p"
    git clone "$p"
done < $1
Final note: haven't tried with Gitlab, but I assume it'll be more or less the same.