This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloneall.sh
111 lines (96 loc) · 2.39 KB
/
cloneall.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#1/bin/sh
# cloneall.sh - (c)2019 xzvf. (github.com/x-zvf/cloneall)
# LICENSE: MIT, see LICENSE file
clonedir=$(pwd)
username=""
usessh=false
promptpass=false
curlopts=""
usage() {
echo "cloneall.sh - clone all your github repos at once"
echo "Usage:"
echo 'cloneall.sh [(-s|-h)] -u <username> [-p] [-d <directory>]'
echo 'Options:'
echo '-s Use ssh'
echo '-h Use http (default)'
echo '-u <username> specify the github username to clone all repos from'
echo '-p Authenticate as specified user. This is for cloning private repos'
echo ' and will prompt the user to input their github password.'
echo '-d <directory> specify the directory to clone all the repos to.'
echo ' a seperate folder for each repo inside this folder'
echo ' will still be created. Defaults to current directory.'
echo 'made by xzvf'
}
# Parse options passed to script
while getopts "shpu:d:" o; do
case "${o}" in
h)
usessh=false
;;
s)
usessh=true
;;
p)
promptpass=true
;;
u)
username=${OPTARG}
;;
d)
clonedir=${OPTARG}
;;
*)
echo 'Invalid argument'
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
# Argument validation
if [ -z $username ];
then
echo 'Username must not be empty'
usage
exit 2
fi
if ! [ -d $clonedir ];
then
echo 'The directory specified is not valid'
usage
exit 3
fi
# Authenticate if required
if $promptpass;
then
curlopts="$curlopts --user '$username'"
fi
# 100 should be fine for most
res=$(curl $curlopts "https://api.github.com/users/$username/repos?per_page=100")
# Response error handling
if [ -n "$(echo \"$res\"| grep ' \"message\": \"Not Found\"')" ];
then
echo "User $username not found"
exit 4
fi
if [ -n "$(echo \"$res\"| grep ' \"message\": \"Bad credentials\"')" ];
then
echo "Invalid credentials for user $username"
exit 5
fi
# Parse response
repos=$(echo "$res"| grep full_name | cut -d '"' -f 4)
#echo $res
echo "Found repos:"
echo "$repos"
cd $clonedir
# Clone all found repos
for r in $repos
do
if $usessh
then
git clone "git@github.com:$r.git"
else
git clone "https://github.com/$r.git"
fi
done