Home > Software engineering >  Clone a group of Gitlab projects or update if project already exists
Clone a group of Gitlab projects or update if project already exists

Time:01-03

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:

[...]
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?

CodePudding user response:

#! /bin/sh
set -e

while read repo
do
    repo_name="`basename $repo .git`"
    if test -d "$repo_name"
    then
        git -C "$repo_name" pull
    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.

  • Related