blob: 77de93aadf112ff9d7ba16d6644a45047fc89ce2 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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 "$@"
|