-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone-it-all.sh
96 lines (80 loc) · 2.66 KB
/
clone-it-all.sh
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
#!/bin/bash
SCRIPT_NAME="clone-it-all"
usage() {
cat <<EOF
Usage: $SCRIPT_NAME <github-username-or-organization> [OPTIONS]
Clone all repositories from a specified GitHub user or organization.
Options:
--with-forks Include forked repositories in the clone operation.
--output <output-folder> Specify the output folder where repositories will be cloned.
--help Show this help message and exit.
Examples:
$SCRIPT_NAME someusername
Clones all non-fork repositories from 'someusername' into a folder named 'someusername'.
$SCRIPT_NAME someusername --with-forks
Clones all repositories, including forks, from 'someusername' into a folder named 'someusername'.
$SCRIPT_NAME someusername --output /path/to/output-folder
Clones all non-fork repositories from 'someusername' into '/path/to/output-folder'.
$SCRIPT_NAME someusername --with-forks --output /path/to/output-folder
Clones all repositories, including forks, from 'someusername' into '/path/to/output-folder'.
EOF
exit 1
}
USER_OR_ORG=""
OUTPUT_DIR=""
WITH_FORKS=false
while [[ $# -gt 0 ]]; do
case "$1" in
--with-forks)
WITH_FORKS=true
shift
;;
--output)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
OUTPUT_DIR="$2"
shift 2
else
echo "Error: --output requires a non-empty option argument."
usage
fi
;;
--help)
usage
;;
-*|--*)
echo "Unknown option: $1"
usage
;;
*)
if [[ -z "$USER_OR_ORG" ]]; then
USER_OR_ORG="$1"
shift
else
echo "Error: Multiple positional arguments provided: '$USER_OR_ORG' and '$1'."
usage
fi
;;
esac
done
if [[ -z "$USER_OR_ORG" ]]; then
echo "Error: GitHub username or organization is required."
usage
fi
OUTPUT_DIR=${OUTPUT_DIR:-$USER_OR_ORG}
mkdir -p "$OUTPUT_DIR" || { echo "Error: Failed to create directory '$OUTPUT_DIR'."; exit 1; }
if ! command -v gh &> /dev/null; then
echo "Error: GitHub CLI ('gh') is not installed. Please install it and authenticate."
exit 1
fi
if [ "$WITH_FORKS" = true ]; then
echo "Cloning all repositories (including forks) from '$USER_OR_ORG' into '$OUTPUT_DIR'..."
gh repo list "$USER_OR_ORG" --limit 4000 --json name --jq '.[] | .name' | while read -r repo; do
gh repo clone "$USER_OR_ORG/$repo" "$OUTPUT_DIR/$repo"
done
else
echo "Cloning all non-fork repositories from '$USER_OR_ORG' into '$OUTPUT_DIR'..."
gh repo list "$USER_OR_ORG" --limit 4000 --json name,isFork --jq '.[] | select(.isFork == false) | .name' | while read -r repo; do
gh repo clone "$USER_OR_ORG/$repo" "$OUTPUT_DIR/$repo"
done
fi
echo "Cloning completed."