Last active
May 25, 2024 23:06
-
-
Save c-neto/711d684e9e5658bf05461d0c9940e556 to your computer and use it in GitHub Desktop.
Bash script for cloning all GitHub repositories associated with a specific user, provided as an argument.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Cloning all GitHub repositories belonging to a specific user provided as an argument. | |
# | |
# Command Execution Example: | |
# ./clone-all-repos-github.sh <USERNAME> | |
# | |
# Arguments: | |
# <USERNAME> GitHub username whose repositories will be cloned. | |
# | |
# Usage Examples: | |
# # Clone all repositories of user "c-neto" | |
# $ ./clone-all-repos-github.sh c-neto | |
# | |
# Requirements: | |
# - jq: https://stedolan.github.io/jq/ | |
# - git: https://git-scm.com/ | |
set -eu | |
IFS=$'\n' | |
# GitHub username whose repositories will be cloned | |
USERNAME="$1" | |
# GitHub API URL to get the user's repositories | |
API_URL_GITHUB_REPO="https://api.github.com/users/$USERNAME/repos" | |
# Fetch repository URLs using the GitHub API and jq | |
REPOS_JSON_META=$(curl -s "$API_URL_GITHUB_REPO") | |
# Extracting repository clone URLs | |
REPOS_URL=$(jq --raw-output -c ".[].ssh_url" <<< "$REPOS_JSON_META") | |
# Extracting repository names | |
REPOS_NAME=$(jq --raw-output -c ".[].name" <<< "$REPOS_JSON_META") | |
# create a parent directory to clone the user repos | |
mkdir "$USERNAME" || true | |
cd "$USERNAME" | |
# cloning repository asynchronously | |
for REPO_URL in $REPOS_URL; do | |
echo ">>> clone: $REPO_URL" | |
git clone "$REPO_URL" & | |
done | |
# waiting for all asynchronous operations to finish | |
wait | |
# sync repos asynchronously | |
for REPO_NAME in $REPOS_NAME; do | |
echo ">>> fetch + pull: $REPO_NAME" | |
{ | |
git -C "$REPO_NAME" fetch | |
git -C "$REPO_NAME" pull | |
} & | |
done | |
# waiting for all asynchronous operations to finish | |
wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment