Skip to content

Instantly share code, notes, and snippets.

@amosshapira
Last active March 9, 2021 04:30
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 amosshapira/9fd2a7ba888f32a45d304adc89f58c1c to your computer and use it in GitHub Desktop.
Save amosshapira/9fd2a7ba888f32a45d304adc89f58c1c to your computer and use it in GitHub Desktop.
Bash script to clone all repositories of a given GitHub organisation
#!/usr/bin/env bash
# "CLONE_ALL_GH_TOKEN" should be a Personal Token from https://github.com/settings/tokens
# with scope "repo"
ORG=<YOUR_ORG_NAME_HERE>
GIT_OUTPUT_DIRECTORY=${1:-"$HOME/github"}
echo Cloning to $GIT_OUTPUT_DIRECTORY
# "100" is the maximum adhered to by GitHub
PER_PAGE=100
cd $GIT_OUTPUT_DIRECTORY || exit 1
rm -f clone-all.out headers
url="https://api.github.com/orgs/$ORG/repos?per_page=$PER_PAGE"
(while [[ -n "$url" ]]
do
echo url: \"$url\" >> clone-all.out 2>&1
curl -D headers -H "Authorization: token $CLONE_ALL_GH_TOKEN" -s "$url" |
jq -r ".[] | select(.archived==false) | .name"
url=$(egrep '^link: ' headers | egrep -o '<[^>]+>; rel="next"' | egrep -o 'https://[^>]+')
done) |
while read REPO_NAME ; do
echo -n ${REPO_NAME}: ""
if [[ -d ${REPO_NAME} ]]
then
(cd $REPO_NAME && git pull -v origin master:master) >> clone-all.out 2>&1 ||
{ echo "ERROR: Unable to update!" ; continue ; }
echo -n updated ""
(cd $REPO_NAME && git remote update --prune origin) >> clone-all.out 2>&1 ||
{ echo "ERROR: Pruning failed!"; continue ; }
echo pruned
else
git clone git@github.com:${ORG}/$REPO_NAME.git $GIT_OUTPUT_DIRECTORY/$REPO_NAME >> clone-all.out 2>&1 ||
{ echo "ERROR: Unable to clone!" ; continue ; }
echo cloned
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment