Skip to content

Instantly share code, notes, and snippets.

@benjyiw
Last active April 28, 2022 16:13
Show Gist options
  • Select an option

  • Save benjyiw/3b2f699bb933702a5fb8926c70e1a86a to your computer and use it in GitHub Desktop.

Select an option

Save benjyiw/3b2f699bb933702a5fb8926c70e1a86a to your computer and use it in GitHub Desktop.
pulls or clones all repositories in a list of orgs and/or users
#!/bin/bash
# pulls or clones all repositories in a list of orgs and/or users
set -euo pipefail
# required input
GITHUB_USER=""
GITHUB_PAT=""
REPOS_PATH="${HOME}/git"
orgs=(
# orgs go here ...
)
users=(
# users go here ...
)
GITHUB_URL="https://api.github.com/"
get_repos() {
# orgs or users
local identity_type="${1}"
# org name or user name
local identity="${2}"
curl -u ${GITHUB_USER}:${GITHUB_PAT} -s \
"${GITHUB_URL}${identity_type}/${identity}/repos?per_page=200" |
jq -r '.[].ssh_url'
}
clone_or_pull() {
local repo="${1}"
local dir="${repo/*:}"
dir="${REPOS_PATH}/${dir%.git}"
if [[ -d ${dir} ]]; then
git -C "${dir}" pull --quiet
else
git clone --quiet "${repo}" "${dir}"
fi
}
for org in ${orgs[@]}; do
repos=$(get_repos orgs $org)
for repo in ${repos[@]}; do
clone_or_pull "${repo}"
done
done
for user in ${users[@]}; do
repos=$(get_repos users $user)
for repo in ${repos[@]}; do
clone_or_pull "${repo}"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment