Created
February 24, 2014 16:24
-
-
Save skl/9191500 to your computer and use it in GitHub Desktop.
Mirror public git repositories to an Atlassian Stash instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
function stash_create_repo() | |
{ | |
local username="$1" | |
local password="$2" | |
local hostname="$3" | |
local port="$4" | |
local project="$5" | |
local repo="$6" | |
local repouri="ssh://git@${hostname}:${port}/${project}/${repo}.git" | |
# Does repo exist? | |
if git ls-remote "${repouri}" HEAD &>/dev/null; then | |
return 0; | |
fi | |
# Does project exist? | |
if [ 200 -eq "`curl -sL -w "%{http_code}" http://${username}:${password}@${hostname}/rest/api/latest/projects/${project} -o /dev/null`" ]; then | |
echo "Found stash project: ${project}" | |
else | |
echo "Creating stash project: ${project}" | |
curl -u "${username}":"${password}" -X POST -H "Accept: application/json" -H "Content-Type: application/json" \ | |
"http://${hostname}/rest/api/latest/projects" \ | |
-d '{"key":"'${project}'", "name": "'${project}'", "description": "'${project}' (mirror)"}' | |
fi | |
# Create repo | |
echo "Creating stash repo: ${repo}" | |
curl -u "${username}":"${password}" -X POST -H "Accept: application/json" -H "Content-Type: application/json" \ | |
"http://${hostname}/rest/api/latest/projects/${project}/repos" \ | |
-d '{"name": "'${repo}'"}' | |
return 0 | |
} | |
function mirror_repos_to_stash() | |
{ | |
local stash_conf_path=stash.conf | |
local repos_conf_path=repos.conf | |
local local_mirror_root=/var/repos/mirror | |
if [ ! -f "${stash_conf_path}" ]; then | |
echo "Could not find stash configuration at ${stash_conf_path}" | |
return 1 | |
fi | |
if [ ! -f "${repos_conf_path}" ]; then | |
echo "Could not find repos configuration at ${repos_conf_path}" | |
return 1 | |
fi | |
local username=`grep "^username:" "${stash_conf_path}" | cut -d: -f2` | |
local password=`grep "^password:" "${stash_conf_path}" | cut -d: -f2` | |
local hostname=`grep "^hostname:" "${stash_conf_path}" | cut -d: -f2` | |
local port=`grep "^port:" "${stash_conf_path}" | cut -d: -f2` | |
if [ ! -d "${local_mirror_root}" ]; then | |
mkdir -p "${local_mirror_root}" | |
fi | |
cat "${repos_conf_path}" | while read line; do | |
local project="`echo "${line}" | cut -d: -f1`" | |
local repo="`echo "${line}" | cut -d: -f2`" | |
local origin_uri="`echo "${line}" | cut -d: -f3-`" | |
local local_mirror="${local_mirror_root}/${project}/${repo}" | |
local stash_mirror="ssh://git@${hostname}:${port}/${project}/${repo}.git" | |
if [ ! -d "${local_mirror}" ]; then | |
mkdir -p "${local_mirror}" | |
fi | |
cd "${local_mirror}" | |
# Ensure directory is a git repo | |
if ! git config remote.origin.url &>/dev/null; then | |
git clone --mirror "${origin_uri}" . | |
fi | |
# Ensure stash repo exists | |
if stash_create_repo "${username}" "${password}" "${hostname}" "${port}" "${project}" "${repo}"; then | |
echo "Found mirror repo: ${project}/${repo}" | |
else | |
echo "Failed to create mirro repo: ${project}/${repo}" | |
return 1 | |
fi | |
# Ensure stash remote is configured | |
if ! git config remote.stash.url &>/dev/null; then | |
git remote add stash ${stash_mirror} | |
fi | |
git fetch origin | |
git push --all stash | |
git push --tags stash | |
done | |
return 0 | |
} | |
mirror_repos_to_stash $@ |
ghost
commented
Jul 7, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment