Automatically clone all repositories defined in conf/gitolite.conf

In this post, I post a simple, hackish script to clone all repositories defined in a gitolite config file. There may be various reasons why one should want to clone a bunch of repositories at a time, e.g., in case one is working with a bigger project composed of several independent components or to simply create a backup of repositories. No matter what the reason is, this script could be handy in such a case:

#!/bin/bash
#
# Copyright 2013, Ruediger Gad <r.c.g@gmx.de>
#
# This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
# To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
# or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
#
#
#
# Script to automatically clone repositories defined in a gitolite config file.
#
# Example:
# initial_repository_checkout.sh ssh://git@foo.com [my_gitolite_config.conf]
#
# The first command line parameter is the URL where the repositories are located at.
#
# The path to the gitolite config can be given as last command line parameter.
# If the path is not given conf/gitolite.conf is used as default path.
#
#
# Please note that this script expects the repo line to start with "repo"
# without any preceding whitespace and to contain exactly one space between
# the "repo" keyword and the path of the repository.
# You can adjust this behavior by tweaking the grep command in the REPOS_LINES assignment.
# This only works for repositories that are explicitly defined via a repo entry.
#

if [ $# -eq 0 ]
then
    echo "Please specify at least a server URL where the repositories are located."
    exit
fi

SERVER_URL=$1

if [ $# -eq 1 ]
then
    GITOLITE_CONFIG=conf/gitolite.conf
    echo "Using default path to gitolite config: ${GITOLITE_CONFIG}"
else
    GITOLITE_CONFIG=$2
fi
echo "Using ${GITOLITE_CONFIG} to get repo lines."

REPO_LINES=$(grep "^repo [a-Z]" ${GITOLITE_CONFIG} | sed 's/^repo //')

for rl in $REPO_LINES
do
    if [ $(echo $rl | grep "/") ]
    then
        dir=$(dirname $rl)
        echo "Found repository in subdirectory. Creating parent directories first: $dir"
        mkdir -p $dir
        cd $dir
        echo "Checking out: $rl"
        git clone ${SERVER_URL}/$rl
        cd -
    else
        echo "Checking repository out: $rl"
        git clone ${SERVER_URL}/$rl
    fi
done

This script can be run again after more repositories were added to the gitolite config. It will simply not clone the already checked out repositories again. In this case, you can safely ignore the error messages.
For updating the cloned repositories you can use the script I posted here earlier.

This entry was posted in Snippets and tagged , , , . Bookmark the permalink.

1 Response to Automatically clone all repositories defined in conf/gitolite.conf

  1. hiramhzr says:

    Thanks, works fine

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.