Skip to content

Instantly share code, notes, and snippets.

@l8nite
Created March 2, 2016 05:40
Show Gist options
  • Save l8nite/e136dd221458dd4a9dd3 to your computer and use it in GitHub Desktop.
Save l8nite/e136dd221458dd4a9dd3 to your computer and use it in GitHub Desktop.
Clone all repositories in stash that you have access to
#!/bin/bash
if [[ "$STASH_USER" == "" || "$STASH_PASS" == "" ]]; then
echo "FATAL: You need to set STASH_USER and/or STASH_PASS"
exit 1
fi
STASH_HOST=${STASH_HOST:-stash.hq.practicefusion.com}
STASH_URL=https://${STASH_USER}:${STASH_PASS}@${STASH_HOST}
while read projectPath; do
projectName=$(echo $projectPath | sed -e 's/^\/projects\///')
while read cloneUrl; do
repoName=$(basename $cloneUrl | sed -e 's/\.git$//')
projectJson=$(curl -k -s ${STASH_URL}/rest/api/1.0${projectPath}/repos/${repoName})
# ignore forks
if echo $projectJson | jq -e 'has("origin")' &>/dev/null; then
continue;
fi
# ignore the DEAD project (deprecated/unused code)
if [[ "$projectName" == "DEAD" ]]; then
continue;
fi
defaultBranch=$(curl -k -s ${STASH_URL}/rest/api/1.0${projectPath}/repos/${repoName}/branches/default | jq '.displayId')
echo "Fetching $projectName/$repoName ($defaultBranch)..."
mkdir -p $projectName
pushd $projectName &>/dev/null
if [[ -d $repoName/.git ]]; then
pushd $repoName &>/dev/null
sem --jobs 20 --id $projectName "git fetch; git checkout $defaultBranch; git pull --rebase"
popd &>/dev/null
else
sem --jobs 20 --id $projectName "git clone $cloneUrl"
fi
popd &>/dev/null
done < <(curl -k -s ${STASH_URL}/rest/api/1.0${projectPath}/repos?limit=500 | jq -r '.values[].links.clone[] | select(.name=="ssh") | .href')
sem --wait --id $projectName
done < <(curl -k -s ${STASH_URL}/rest/api/1.0/projects?limit=500 | jq -r '.values[].link.url')
@l8nite
Copy link
Author

l8nite commented Mar 2, 2016

Obviously has some specifics (like the default STASH_HOST and skipping a project named DEAD) from my use case, but it should be easy to adapt. Runs 20 clones/fetches at a time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment