Daevin Daevin - 2 months ago 20
Git Question

Clone multiple repositories into common parent folder structure

My issue is that I have a client, let's call them 'Evil Corp', that provides my company with lots of business through various projects, which we use GitHub repos for. Let's take 2 and call them 'TotallyNotPolluting' and 'CoveringUpPolluting'.

I would like for my employees to be able to clone the 'TotallyNotPolluting' repo into a folder 'EvilCorp' by default when cloning.

Behaviour:



So the starting structure is:

E:
--Work
--Repos



  1. Have starting folder structure:
    E:\Work\Repos
    to start

  2. In Git Bash/GitHub Desktop/whatever clone the repo 'TotallyNotPolluting' (selected location is the starting folder structure
    E:\Work\Repos
    )

  3. Have ending folder structure:
    E:\Work\Repos\EvilCorp\TotallyNotPolluting

  4. In Git Bash/GitHub Desktop/whatever clone the repo 'CoveringUpPolluting' (selected location is the starting folder structure
    E:\Work\Repos
    )

  5. Have ending folder structure:
    E:\Work\Repos\EvilCorp\CoveringUpPolluting



So the final structure is:

E:
--Work
--Repos
--EvilCorp
--TotallyNotPolluting
--CoveringUpPolluting


This way, all the repos are organized into client folders without employees taking extra steps to do so.

Is this possible? To have the Clone action create then clone into (if one doesn't exist) or clone into (if one does) a parent directory?

NOTE: I have looked into using Submodules, but I don't want a repository "EvilCorp" that contains other repositories. I just want the folder when cloning.

Answer

You can write a script like below. I've not tested it properly but this should give you the right direction to move it. The purpose of this shell script is to create a parent directory and then individually create each sub-folder, check if sub-folder is empty. If empty, clone the repo. This shell script can be distributed to employees as an executable then, when run, will clone repos into right places.

#create the directory, if it does not exist

directoryName="<your directory name here>"
mkdir -p directoryName
repos="TotallyNotPolluting CoveringUpPolluting"
forwardSlash="/"
#go into directory
pushd $directoryName



for repo in $repos; do

    #check if directory is not present, if true, create the directory

    [ ! -d $repo ] && mkdir -p $repo

    #go into directory

    currentDirectory=$directoryName$forwardSlash$repo
    if [ "$(ls -A $currentDirectory)" ]; then

     echo "directory is not empty, skipping to clone"
    else
        pushd $directoryName$forwardSlash$repo
        git clone "git@$codeHost:$codeUser/$repo.git"
    fi

done
Comments