Skip to content

Instantly share code, notes, and snippets.

@drichardson
Last active September 11, 2022 15:27
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 drichardson/4f1e831e0feece632ef4cb179955864c to your computer and use it in GitHub Desktop.
Save drichardson/4f1e831e0feece632ef4cb179955864c to your computer and use it in GitHub Desktop.
Clone/update all repositories in an organization you have access to.
#!/bin/bash
# Clone/pull every repo you have access to in a github org into a directory.
# Source: https://gist.github.com/drichardson/4f1e831e0feece632ef4cb179955864c
set -euo pipefail
# Max repos to checkout.
LIMIT=500
usage() {
cat <<-EOF
Clone/update all repositories in a GitHub org that you have access to.
Clones at most $LIMIT repos.
Only source repos (not forks) are cloned.
Usage:
github-org-up <github_org> <destination>
<github_org> is the GitHub organization to use
<destination> is the directory where all repos will be cloned into.
Example:
github-org-up .
EOF
}
if [[ $# -eq 0 ]]; then
echo Missing github_org.
usage
exit 1
fi
ORG=$1
shift
if [[ $# -eq 0 ]]; then
echo Missing desination directory.
usage
exit 1
fi
DEST=$1
shift
cd "$DEST"
if ! hash git; then
cat <<-EOF
git not installed. Install and try again.
EOF
fi
if ! hash gh; then
cat <<-EOF
GitHub cli (gh) not installed. Install and try again.
If you're using Homebrew, run:
brew install gh
Otherwise, download here:
https://cli.github.com/
EOF
exit 1
fi
echo Checking GitHub authorization...
if ! gh auth status; then
# Login instructions already printed.
exit 1
fi
echo Cloning/Updating $ORG Repositories
for repo in $(gh repo list $ORG --limit $LIMIT --no-archived --source --json sshUrl -q .[].sshUrl); do
echo Cloning $repo
base=$(basename $repo)
clone_dir=${base%.*}
if [[ -d $clone_dir ]]; then
echo Existing repo found. Pulling into $clone_dir.
git -C $clone_dir pull --ff-only &
else
echo New repo found. Cloning $repo into $clone_dir.
git clone --progress $repo &
fi
done
wait
echo OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment