Clone a group of Gitlab projects or update if project already exists

There’s a list of Gitlab projects within one group and SSH links are extracted to a separate file that gets then imported to the bash script:

readarray -t repos < ./repositories.txt

for repo in "${repos[@]}"
    git clone "$repo"

It works perfectly fine when the directory it operates in is empty. Otherwise, I get the following errors:

fatal: destination path 'project1' already exists and is not an empty directory.

It yields error code 128, so I tried adding OR condition to the script:

git clone "$repo" || find . -maxdepth 1 -type d \( ! -name . \) -exec bash -c "cd '{}' && git pull origin master" \;

before I realized the iteration is done by a list of URLs, so the script is not aware about directory it should get into, so it simply doesn’t work as intended.

So, the question is: how to clone projects into directory if they don’t exist there yet, or update them if they do exist?

>Solution :

#! /bin/sh
set -e

while read repo
    repo_name="`basename $repo .git`"
    if test -d "$repo_name"
        cd "$repo_name"
        git pull
        cd ..
        git clone "$repo"
done < ./repositories.txt

"`basename $repo .git`" extract the repository name from the URL and cuts .git from the end (if it’s there). See man basename.

Leave a Reply