Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clone all github repositories for en organisation (git clone or git fetch --all)
#/bin/bash
#
# Checkout all repos, or brings branches up to date for an Org
#
read -d '' helptxt <<- "HELP_MSG"
Checkout all repos for a github organisation \\n
\\n
Usage:\\n
\\n
git_get_all org branch... Clone all github repos into the current directory.\\n
If there is a directory already matching a repo then run 'git pull origin branch' for each (ignores errors)\\n
HELP_MSG
set -e
if [[ "$1" = "" ]]; then
echo "Error missing arg"
echo -e $helptxt
exit 1
fi
org=$1
shift
allBranches=( "$@" )
if [[ ${#allBranches[@]} -eq 0 ]]; then
echo "Error missing branch arguments"
echo -e $helptxt
exit 1
fi
# See https://github.com/davidoram/ght
ght repos -o "${org}" > all_repos.txt
while read org_repo; do
echo '--------------------------------------------'
echo "$org_repo"
if [[ $org_repo =~ (.*)/(.*) ]]; then
org=${BASH_REMATCH[1]}
repo=${BASH_REMATCH[2]}
if [[ -d "${repo}" ]]; then
echo "Directory exists"
pushd "${repo}"
set +e
# Get latest of each branch
for branch in ${allBranches[@]}; do
echo " - git pull origin ${branch}"
git pull origin ${branch}
done
set -e
popd
else
echo "New directory - git clone"
git clone git@github.com:${org_repo}.git
fi
fi
done <all_repos.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment