This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
starsync.sh
executable file
·205 lines (179 loc) · 4.22 KB
/
starsync.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/zsh
PROGNAME=starsync
USER=
STARDIR=
TOTAL_NEW=0
TOTAL_UPDATED=0
TOTAL_SKIPPED=0
CLONE=1
SHALLOW=
MAX_REPO_SIZE_KB=$((1024 * 50))
main() {
parse-opts "$@"
prompt-for-username
make-star-directory
cd "$STARDIR"
fetch-all-stars
restart-line
show-summary
cd ..
}
parse-opts() {
while test $# -gt 0 ; do
case "$1" in
--no-clones)
CLONE=
;;
--user) ;&
-u)
USER="$2"
shift
;;
--shallow) ;&
-s)
SHALLOW=1
;;
--max-repo-size) ;&
-m)
MAX_REPO_SIZE_KB="$2"
shift
;;
*) ;&
--help) ;&
-h)
show-usage-and-exit
;;
esac
shift
done
}
show-usage-and-exit() {
echo "Usage: $PROGNAME [options]"
echo 'Options:'
echo ' --no-clones Do not clone new stars'
echo ' -h | --help Show this help message'
echo ' -u | --user Set your username. If unset, you will be prompted for it'
echo ' -s | --shallow Perform a shallow clone of new repositories'
echo ' -m | --max-repo-size Set the maximum allowable repository size, in kilobytes.'
echo ' Repositories over this limit will not be cloned.'
echo ' Defaults to 50 megabytes'
exit
}
show-summary() {
if test 0 -ne $TOTAL_SKIPPED ; then
echo -n "Skipped $TOTAL_SKIPPED repositor"
case $TOTAL_SKIPPED in
1) echo -n 'y. ' ;;
*) echo -n 'ies. ' ;;
esac
fi
echo -n "Synced $TOTAL_UPDATED repositor"
if test 1 -eq $TOTAL_UPDATED ; then
echo 'y'
else
echo -n 'ies.'
case $TOTAL_UPDATED in
0)
echo
;;
1)
echo ' Of those, one was new.'
;;
*)
echo " Of those, $TOTAL_NEW were new."
;;
esac
fi
}
prompt-for-username() {
if test -n "$USER" ; then
return
fi
USER="$(whoami)"
if prompt "Using $USER as your username; is this OK?" ; then
return
fi
input USER "Please enter your username"
}
make-star-directory() {
STARDIR="${USER}'s stars"
mkdir -p "$STARDIR"
}
fetch-all-stars() {
local PAGE=1
local READ=1
while test 1 -eq $READ ; do
READ=0
curl -s "https://api.github.com/users/$USER/starred?page=$PAGE" \
| jq -r '.[]|[.git_url, .owner.login, .name, .size]|@tsv' \
| while read -r URL AUTHOR NAME SIZE
do
READ=1
clone-or-pull $URL $AUTHOR $NAME $SIZE
done
PAGE=$((PAGE + 1))
done
}
prompt() {
echo -n "$@ (y/n) "
read -r ANSWER
if test 0 -ne $? ; then
prompt "$@"
return $?
fi
ANSWER="${ANSWER[1]}"
if test "$ANSWER" = y -o Y = "$ANSWER" ; then
return 0
elif test "$ANSWER" = n -o N = "$ANSWER" ; then
return 1
fi
prompt "$@"
}
input() {
local VAR="$1"
shift
echo -n "${@}: "
read -r "$VAR"
if test 0 -ne $? ; then
input "$VAR" "$@"
return
fi
}
restart-line() {
echo -ne '\e[G\e[K'
}
msg() {
restart-line
echo -n "[$@]"
}
clone-or-pull() {
local URL="$1"
local AUTHOR="$2"
local NAME="$3"
local SIZE="$4"
if test $SIZE -ge $MAX_REPO_SIZE_KB ; then
msg "Skipping $AUTHOR/$NAME..."
TOTAL_SKIPPED=$((TOTAL_SKIPPED + 1))
return
fi
TOTAL_UPDATED=$((TOTAL_UPDATED + 1))
if test -d "$AUTHOR/$NAME" ; then
msg "Updating $AUTHOR/$NAME..."
cd "$AUTHOR/$NAME"
git pull --ff-only -q
cd ../..
elif test -n "$CLONE" ; then
msg "Cloning $AUTHOR/$NAME..."
TOTAL_NEW=$((TOTAL_NEW + 1))
mkdir -p "$AUTHOR"
cd "$AUTHOR"
if test -n "$SHALLOW" ; then
git clone -q "$URL" --depth 1
else
git clone -q "$URL"
fi
cd ..
fi
}
PROGNAME="$(basename "$0")"
main "$@"