-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit_repos.sh
executable file
·100 lines (85 loc) · 2.41 KB
/
git_repos.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
#!/bin/sh
# path: /home/klassiker/.local/share/repos/shell/git_repos.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/shell
# date: 2024-07-03T08:58:10+0200
# config
name="mrdotx"
cn="users"
# help
script=$(basename "$0")
help="$script [-h/--help] -- script to perform operations for all repositories
of a user or organization
Usage:
$script [-c/--clone/-i/--infos/-j/--json] [name] [cn]
Settings:
[-c/--clone] = clone all repositories of a user or organization
[-i/--infos] = show count of repos/stargazers/forks of a user or organisation
[-j/--json] = outputs the raw json data of repos
[name] = must be a username or organisation (default=$name)
[cn] = must be users or orgs (default=$cn)
Examples:
$script --clone
$script --clone $name
$script -c $name $cn
$script --infos
$script -i $name
$script --infos $name $cn
$script -j
$script -j $name
$script --json $name $cn"
# functions
get_repos() {
curl -fsS \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/$2/$1/repos?per_page=1000"
}
get_values() {
printf "%s" "$1" \
| awk -F "$2" 'NF > 1 {print $2}' \
| sed "s/$3//"
}
count_values() {
values="$(get_values "$1" "$2" "$3")"
for value in $values; do
count=$((count+value))
done
printf "%s" "$count"
}
# main
case $1 in
-h | --help)
printf "%s\n" "$help"
;;
-c | --clone)
shift
repos=$(get_repos "${1:-$name}" "${2:-$cn}")
procs="$(($(nproc --all) * 4))"
get_values "$repos" '"clone_url": "' '",' \
| xargs -P"$procs" -L1 git clone
;;
-i | --infos)
shift
repos=$(get_repos "${1:-$name}" "${2:-$cn}")
repos_count=$( \
get_values "$repos" '"full_name": "' '",' \
| wc -l \
)
stars_count="$(count_values "$repos" '"stargazers_count": ' ',')"
forks_count="$(count_values "$repos" '"forks_count": ' ',')"
printf "%s | %d %d %d\n" \
"${1:-$name}" \
"$repos_count" \
"$stars_count" \
"$forks_count"
;;
-j | --json)
shift
get_repos "${1:-$name}" "${2:-$cn}"
;;
*)
printf "%s\n" "$help"
exit 1
;;
esac