Created
October 27, 2016 16:09
-
-
Save svagionitis/25eae8dc1ded11f860180c047ddace28 to your computer and use it in GitHub Desktop.
Clone repositories for projects in Stash
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/sh | |
JQ="`which jq` -r" | |
CURL="`which curl` -s" | |
BASENAME="`which basename`" | |
GIT="`which git`" | |
STASH_SERVER="" | |
STASH_PROJECTS="${STASH_SERVER}/rest/api/1.0/projects" | |
usage() | |
{ | |
echo "Usage: $0 <username> <password>" | |
} | |
if [ $# -lt 2 ]; then | |
usage | |
exit 1 | |
fi | |
if [ -z ${1} ]; then | |
usage | |
exit 1 | |
fi | |
USERNAME=${1} | |
if [ -z ${2} ]; then | |
usage | |
exit 1 | |
fi | |
PASSWORD=${2} | |
START=$(date +%s) | |
GET_STASH_PROJECTS="$(${CURL} --user ${USERNAME}:${PASSWORD} "${STASH_PROJECTS}?start=0&limit=10000" | ${JQ} ".values[].key")" | |
for project in ${GET_STASH_PROJECTS} | |
do | |
echo "Project ${project}" | |
GET_REPOS="$(${CURL} --user ${USERNAME}:${PASSWORD} "${STASH_PROJECTS}/${project}/repos" | ${JQ} ".values[].links.clone[].href")" | |
GET_HTTP_REPOS="$(echo "${GET_REPOS}" | grep "http://")" | |
GET_SSH_REPOS="$(echo "${GET_REPOS}" | grep "ssh://")" | |
for repo in ${GET_SSH_REPOS} | |
do | |
REPO_NAME="$(${BASENAME} ${repo})" | |
PROJECT_REPO_PATH="projects/${project}/${REPO_NAME}" | |
# Check if directory already exists | |
# If already exists then update and | |
# continue to the next one | |
if [ -d ${PROJECT_REPO_PATH} ]; then | |
echo "Repo ${REPO_NAME} already exists, update it..." | |
cd ${PROJECT_REPO_PATH} | |
${GIT} fetch || true | |
cd - | |
continue | |
fi | |
mkdir -p "${PROJECT_REPO_PATH}" | |
echo "Cloning ${REPO} in ${PROJECT_REPO_PATH}.." | |
${GIT} clone ${repo} ${PROJECT_REPO_PATH} | |
done | |
done | |
FINISH=$(date +%s) | |
echo "Total time: $(( ($FINISH-$START) / 60 )) minutes, $(( ($FINISH-$START) % 60 )) seconds" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment