Skip to content

Instantly share code, notes, and snippets.

@cloudnull
Created April 28, 2016 17:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cloudnull/6e76897f27a2225821c7bc26535e261b to your computer and use it in GitHub Desktop.
Save cloudnull/6e76897f27a2225821c7bc26535e261b to your computer and use it in GitHub Desktop.
Build a mirror of all of the git repositories that the OpenStack Ansible requires from within the ROOT project.
#!/usr/bin/env bash
set -ve
CLONE_TARGET="${CLONE_TARGET:-/var/www/repo/openstackgit}"
# Clone a git repository / Update git repository.
function git_clone {
repo="$1"
dest="$2"
# If the git repository directory is locally found the function will ensure the remote
# is available within the repository. If the remote is not already in the local repository
# the repository URL will be hashed and the first 7 charactors of the hash will be used for
# remote name. Once the remotes are satisfied all refs will be fetched.
if [ -d "$dest/.git" ];then
pushd $dest
if ! git remote -v | grep -q "$repo"; then
remote_hash=$(echo "$repo" | sha1sum | awk '{print $1}')
git remote add ${remote_hash:0:7} "$repo"
fi
git fetch --all
popd
# If the local target directory is not a valid git repository it will be removed and cloned.
elif [ -d "$dest" ];then
rm -rf "$dest"
git clone "$repo" "$dest"
else # If the local target directory does not exist it will be cloned.
git clone "$repo" "$dest"
fi
}
REPOS="$(sort -u <(find . -name "*.yml" -exec grep -r -e 'github.com' -e 'git.openstack.org' {} \; | sed -e 's/\"//g' | sed 's/,//g' | awk '{print $2}' | grep '^http' | grep -v 'pull' | grep -v 'cgit'))"
for repo in $REPOS; do
git_clone "${repo}" "${CLONE_TARGET}/$(basename ${repo})"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment