-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit-clone-all
More file actions
executable file
·118 lines (104 loc) · 2.8 KB
/
git-clone-all
File metadata and controls
executable file
·118 lines (104 loc) · 2.8 KB
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
111
112
113
114
115
116
117
118
#!/bin/bash
# SPDX-FileCopyrightText: 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command curl git jq && exit
function clone_codeberg() {
local acct="${1}"
local line
local mode=users
local data
# Codeberg disables HEAD requests, so download all the account data and store
# in a variable for parsing later.
if ! data=$(curl -fs https://codeberg.org/api/v1/"${mode}/${acct}"/repos); then
return 1
fi
while read -r line; do
if ! [ -d "${line}" ]; then
git clone "${line}.git"
fi
done < <(jq -r '.[]["html_url"]' <<< "${data}")
}
function clone_github() {
local acct="${1}"
local line
local mode
if curl -Ifs https://api.github.com/orgs/"${acct}"/repos > /dev/null; then
mode=orgs
elif curl -Ifs https://api.github.com/users/"${acct}"/repos > /dev/null; then
mode=users
else
return 1
fi
while read -r line; do
if ! [ -d "${line}" ]; then
git clone "${line}.git"
fi
done < <(curl -s https://api.github.com/"${mode}/${acct}"/repos?per_page=100 | jq -r '.[]["html_url"]')
}
function parse_input() {
DOMAIN="${IN//http*\:\/\//}"
DOMAIN="${DOMAIN#www\.}"
DOMAIN="${DOMAIN/\/*/}"
DOMAIN="${DOMAIN%.*}"
ACCOUNT="${IN##*/}"
}
function search_domains {
local domains=(github codeberg)
local domain
for domain in "${domains[@]}"; do
case "${domain}" in
github)
if curl -ILs https://github.com/"${ACCOUNT}" > /dev/null; then
DOMAIN=github
return
fi
;;
codeberg)
if curl -ILs https://codeberg.org/"${ACCOUNT}"/repos > /dev/null; then
DOMAIN=codeberg
return
fi
;;
*) ;;
esac
done
}
IN="${1}"
DOMAIN=
ACCOUNT=
parse_input
if [[ -n "${ACCOUNT}" ]] && [[ "${DOMAIN}" = "${ACCOUNT}" ]]; then
search_domains
fi
mkdir -p "${ACCOUNT}"
pushd "${ACCOUNT}" > /dev/null
case "${DOMAIN}" in
github)
clone_github "${ACCOUNT}"
;;
codeberg)
clone_codeberg "${ACCOUNT}"
;;
*)
show_error "${ACCOUNT@Q} on domain ${DOMAIN@Q} not supported. Exiting."
exit 3
;;
esac
popd > /dev/null