Skip to content

Instantly share code, notes, and snippets.

@Klathmon
Created July 9, 2020 15:54
Show Gist options
  • Save Klathmon/62eba132898475390c08590eaf837586 to your computer and use it in GitHub Desktop.
Save Klathmon/62eba132898475390c08590eaf837586 to your computer and use it in GitHub Desktop.
megaclone
#!/bin/bash
# Usage: clone_all_repos.sh [organization] <output directory>
# must have jq installed (brew install jq or apt install jq)
ORG="lanetix"
PER_PAGE=100
GIT_OUTPUT_DIRECTORY=${2:-"."}
# Go to https://github.com/settings/tokens and generate a new github token, make sure to give it access to all repo permissions, then paste the token below
GITHUB_TOKEN="TOKEN_GOES_HERE"
mkdir -p $GIT_OUTPUT_DIRECTORY
echo "Cloning repos in $ORG to $GIT_OUTPUT_DIRECTORY/..."
for ((PAGE=1; ; PAGE+=1)); do
REPO_COUNT=0
ERROR=0
while read REPO_NAME ; do
((REPO_COUNT++))
echo -n "Cloning $REPO_NAME to $GIT_OUTPUT_DIRECTORY/$REPO_NAME... "
git clone git@github.com:$ORG/$REPO_NAME.git $GIT_OUTPUT_DIRECTORY/$REPO_NAME
cd "$GIT_OUTPUT_DIRECTORY/$REPO_NAME"
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
git branch --track "${branch##*/}" "$branch"
done
git gc --prune
cd ../
echo "done"
done < <(curl -u :$GITHUB_TOKEN -s "https://api.github.com/orgs/$ORG/repos?per_page=$PER_PAGE&page=$PAGE" | jq -r ".[]|.name")
if [ $ERROR -eq 1 ] ; then exit 1 ; fi
if [ $REPO_COUNT -ne $PER_PAGE ] ; then exit 0 ; fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment