Skip to content

Instantly share code, notes, and snippets.

@eldridge
Created August 2, 2013 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eldridge/6142752 to your computer and use it in GitHub Desktop.
Save eldridge/6142752 to your computer and use it in GitHub Desktop.
#!/bin/bash
ARGS=$(getopt -o p:b:l -l project:base:lower-case -n migrate -- "$@")
[ $? -eq 0 ] || exit 1
eval set -- "$ARGS"
BASE=git@github.com: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://git@git.example.com/$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