Skip to content

Instantly share code, notes, and snippets.

@gaikaz
Last active September 22, 2022 21:11
Show Gist options
  • Save gaikaz/9bd8ef6e25b9d8447deb58ea63e7a8a3 to your computer and use it in GitHub Desktop.
Save gaikaz/9bd8ef6e25b9d8447deb58ea63e7a8a3 to your computer and use it in GitHub Desktop.
Clone multiple repositories at once using Github CLI
#!/bin/bash
# Info
b=$(tput bold)
n=$(tput sgr0)
help="Clones all repositories from Github of a given owner
\nchecking out specific branch and optionally filtering by a topic.
\n\n${b}Requires Github CLI (https://cli.github.com) to be installed!${n}
\n\n${b}USAGE${n}\n $0 [-h] -o OWNER -b BRANCH -t TOPIC -d DIR\n"
# Clear any set variables
unset -v owner
unset -v branch
unset -v topic
unset -v dir
unset -v topic_arg
# Parse the arguments
while getopts ':o:b:t:d:h' OPTION; do
[ $OPTION = "?" ] && echo -e $help && exit 1
[ $OPTION = "h" ] && echo -e $help && exit 1
[ $OPTION = "o" ] && owner=$OPTARG
[ $OPTION = "b" ] && branch=$OPTARG
[ $OPTION = "t" ] && topic=$OPTARG
[ $OPTION = "d" ] && dir=$OPTARG
done
# Check if all options are set
if [ -z $owner ] || [ -z $branch ] || [ -z $dir ]; then
echo -e $help
exit 1
fi
# Topic is non mandatory, allow not setting it
if [ -z $topic ]; then
topic_arg=""
else
topic_arg="--topic ${topic}"
fi
# ========================================
# Create the directory requested
mkdir -p ${dir}
# Get all repositories from Github
repos=$(gh repo list ${owner} --source --no-archived --limit 1000 \
${topic_arg} --json name --jq '.[] | .name')
for repo in $repos; do
echo "📝 ${b}${owner}/${repo}${n}"
gh repo clone ${owner}/${repo} ${dir}/${repo} -- -b ${branch}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment