Created
August 2, 2013 19:34
-
-
Save eldridge/6142752 to your computer and use it in GitHub Desktop.
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 | |
ARGS=$(getopt -o p:b:l -l project:base:lower-case -n migrate -- "$@") | |
[ $? -eq 0 ] || exit 1 | |
eval set -- "$ARGS" | |
[email protected]:example | |
while true | |
do | |
case "$1" in | |
-l|--lower-case) | |
LCASE=1 | |
shift | |
;; | |
-p|--project) | |
PROJECT=$2 | |
shift 2 | |
;; | |
-b|--base) | |
BASE=$2 | |
shift 2 | |
;; | |
--) | |
shift | |
break | |
;; | |
esac | |
done | |
if [ "$PROJECT" == "" ] | |
then | |
echo "migrate: project not specified" | |
exit 1 | |
fi | |
# strip trailing slashes from the base URI | |
BASE=${BASE/%\//} | |
for repo in $@ | |
do | |
case "$repo" in | |
*:*) | |
# this repository spec contains a colon; it must be a full | |
# repository URI. extract the repository name (everything | |
# after the last slash, but excluding the .git suffix) | |
dest=${repo/*\//} | |
dest=${dest/%.git/} | |
;; | |
*) | |
# this repository spec is just a name, so we'll need to add | |
# the base URI as well as append the .git suffix. | |
dest=$repo | |
repo=$BASE/$repo.git | |
;; | |
esac | |
[ $LCASE ] && dest=$(echo $dest | tr [A-Z] [a-z]) | |
git clone --mirror $repo $dest.git | |
if [ $? -ne 0 ] | |
then | |
echo "failed to mirror repository $repo" | |
continue | |
fi | |
curl \ | |
-X POST \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Basic cGFzc3dvcmQ=" \ | |
--data-binary "{\"name\":\"$dest\",\"scmId\":\"git\"}" \ | |
https://stash.example.com/rest/api/1.0/projects/$PROJECT/repos | |
if [ $? -ne 0 ] | |
then | |
echo "failed to create repository $dest under project $PROJECT" | |
exit 1 | |
fi | |
git --git-dir $dest.git remote add stash ssh://[email protected]/$PROJECT/$dest.git | |
git --git-dir $dest.git push --mirror stash | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment