diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2025-12-20 10:53:14 -0500 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2025-12-20 10:54:02 -0500 |
| commit | 7b6dbb921650e2a52929f025b67be57609fda6d4 (patch) | |
| tree | 895416312698cfe4ac61d4eed0f071d7743c0214 /.local/bin/gh-clone-all | |
| parent | Remove openbsd conditional in zsh (diff) | |
| download | dotfiles-7b6dbb921650e2a52929f025b67be57609fda6d4.zip | |
Rework dotfiles to *only* contain dotfiles
Diffstat (limited to '.local/bin/gh-clone-all')
| -rwxr-xr-x | .local/bin/gh-clone-all | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/.local/bin/gh-clone-all b/.local/bin/gh-clone-all new file mode 100755 index 0000000..77de93a --- /dev/null +++ b/.local/bin/gh-clone-all @@ -0,0 +1,102 @@ +#!/usr/bin/env bash +# Clone all repositories from a GitHub user or organization using the gh CLI. +# +# Usage: +# gh-clone-all [--exclude-archived] [--exclude-forks] [--pull] <github-entity> [target-directory] +# +# Args: +# --exclude-archived: If set, skip archived repos (default: include all) +# --exclude-forks: If set, skip forked repos +# --pull: If set, pull latest changes in existing repos +# --limit: The maximum number of repos to clone (default: 10000) +# <github-entity>: The GitHub username or org (e.g. eugenetriguba) +# [target-directory]: Optional directory to clone repos into +set -euo pipefail + +EXCLUDE_ARCHIVED=0 +EXCLUDE_FORKS=0 +PULL_EXISTING=0 +ENTITY="" +TARGET_DIR="." +LIMIT=10000 + +main() { + parse_args "$@" + clone_all_repos +} + +parse_args() { + while [[ $# -gt 0 ]]; do + case "$1" in + --exclude-archived) + EXCLUDE_ARCHIVED=1 + shift + ;; + --exclude-forks) + EXCLUDE_FORKS=1 + shift + ;; + --pull) + PULL_EXISTING=1 + shift + ;; + --limit) + LIMIT="$2" + shift 2 + ;; + -*) + usage + ;; + *) + if [[ -z "$ENTITY" ]]; then + ENTITY="$1" + elif [[ "$TARGET_DIR" == "." ]]; then + TARGET_DIR="$1" + else + usage + fi + shift + ;; + esac + done + if [[ -z "$ENTITY" ]]; then + usage + fi + mkdir -p "$TARGET_DIR" +} + +usage() { + echo "Usage: $0 [--exclude-archived] [--exclude-forks] [--pull] [--limit] <github-entity> [target-directory]" + exit 1 +} + +clone_all_repos() { + gh repo list "$ENTITY" --limit "$LIMIT" --json name,sshUrl,archivedAt,isFork | + jq -c '.[]' | while read -r repo; do + NAME=$(echo "$repo" | jq -r '.name') + CLONE_URL=$(echo "$repo" | jq -r '.sshUrl') + ARCHIVED_AT=$(echo "$repo" | jq -r '.archivedAt') + IS_FORK=$(echo "$repo" | jq -r '.isFork') + DEST="$TARGET_DIR/$NAME" + if [[ "$EXCLUDE_ARCHIVED" -eq 1 && "$ARCHIVED_AT" != "null" ]]; then + continue + fi + if [[ "$EXCLUDE_FORKS" -eq 1 && "$IS_FORK" == "true" ]]; then + continue + fi + if [[ -d "$DEST" ]]; then + echo "Repo $NAME already exists." + if [[ "$PULL_EXISTING" -eq 1 ]]; then + echo "Pulling latest changes in $DEST" + git -C "$DEST" pull --ff-only || echo "Warning: git pull failed in $DEST, continuing..." + else + echo "Skipping $NAME (already exists)" + fi + continue + fi + echo "Cloning $CLONE_URL into $DEST" + git clone "$CLONE_URL" "$DEST" + done +} + +main "$@" |