Mirroring Git Repositories
There are projects where having a working git repository separated
from the customer repository is a requirement. For this cases, I am
using the following script to parse a YAML file repos.yml
and keep
the repositories in sync.
#!/usr/bin/env bash
function json {
local file=$1
local json=$(cat $file | python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)')
echo ${json}
}
JSON=$(json "repos.yml")
repos=$(echo ${JSON} | jq -r 'keys[]')
for repo in $repos; do
if [[ ! -d $repo ]]; then
origin=$(echo ${JSON} | jq -r ".\"${repo}\" | .origin")
upstream=$(echo ${JSON} | jq -r ".\"${repo}\" | .upstream")
echo "[$repo]:"
echo " - git clone --mirror"
git clone --mirror $upstream
cd $repo
echo " - git set-url --push $origin"
git remote set-url --push origin $origin
cd ../
fi
if [[ -d $repo ]]; then
mirror=$(echo ${JSON} | jq -r ".\"${repo}\" | .mirror")
if [[ $mirror = true ]]; then
echo "[$repo]: "
cd $repo
echo " - git fetch upstream"
git fetch -p origin
echo " - git push mirror"
git push --mirror
cd ../
echo -e "\n"
fi
fi
done
aws-modules.git:
upstream: [email protected]:customer/aws-modules.git
origin: [email protected]:our-company/customer-aws-modules.git
mirror: true
aws-service.git:
upstream: [email protected]:customer/aws-service.git
origin: [email protected]:our-company/customer-aws-service.git
mirror: true
aws-app.git:
upstream: [email protected]:customer/aws-app.git
origin: [email protected]:our-company/customer-aws-app.git
mirror: true