Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

#!/bin/bash
readarray -t repos < ./repositories.txt

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[...]
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
do
    repo_name="`basename $repo .git`"
    if test -d "$repo_name"
    then
        cd "$repo_name"
        git pull
        cd ..
    else
        git clone "$repo"
    fi
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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading