-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitclass
More file actions
executable file
·307 lines (278 loc) · 7.42 KB
/
gitclass
File metadata and controls
executable file
·307 lines (278 loc) · 7.42 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env bash
delay(){
if [[ $1 = 0 ]]; then
return
fi
echo -ne $2
delay_clocks=("\U1F550" "\U1F551" "\U1F552" "\U1F553" "\U1F554" "\U1F555" "\U1F556" "\U1F557" "\U1F558" "\U1F559" "\U1F55A")
i=0
for x in `seq 1 $1` ; do
echo -en "${delay_clocks[$i]}"
((i++))
sleep 1
echo -en "\033[2D"
done ;
}
# wait between iterations
# delay Time, iteration index, count of all iteratios
waitForLoop(){
if (( $2 < $3 )); then
delay $1 "\U2002\U2002 ${LightYellow}\U00AB${Cyan}shaving${LightYellow}\U00BB ${LightWhite}\U1F410\U2002\U2002"
ClearLine
fi
}
trim() {
local var="$*"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
echo "${var}"
}
function remove_local_changes()
{
# Revert changes to modified files (modified, added, removed).
git reset --hard > /dev/null 2>&1
# Remove all untracked files and directories. (`-f` is `force`, `-d` is `remove directories`)
git clean -fd > /dev/null 2>&1
}
function WithColor() {
Normal="\e[0m"
Bold="\e[1m"
Black="\e[30m"
White="\e[37m"
if [[ $1 == 0 ]]; then
Red=$Normal
Green=$Normal
Yellow=$Normal
Blue=$Normal
Magenta=$Normal
Cyan=$Normal
DarkGray=$Normal
LightRed=$Normal
LightGreen=$Normal
LightYellow=$Normal
LightBlue=$Normal
LightMagenta=$Normal
LightCyan=$Normal
LightWhite=$Normal
else
Red="\e[31m"
Green="\e[32m"
Yellow="\e[33m"
Blue="\e[34m"
Magenta="\e[35m"
Cyan="\e[36m"
DarkGray="\e[90m"
LightRed="\e[91m"
LightGreen="\e[92m"
LightYellow="\e[93m"
LightBlue="\e[94m"
LightMagenta="\e[95m"
LightCyan="\e[96m"
LightWhite="\e[97m"
fi
}
UU_HCHEVRONB="\U00AB"
UU_HCHEVRONE="\U00BB"
display(){
echo -en $*
echo -en ${Normal}
}
SaveCursor(){
echo -en "\e7"
}
RestoreCursor(){
echo -en "\e8"
}
ClearScreen(){
echo -en "\e[2J\e[H"
}
ClearToEndOfScreen(){
echo -en "\e[0J"
}
ClearLine(){
echo -en "\e[2K"
}
class_prefix_from_file=''
load_students(){
students_file=$(<students.lst)
student_array=( ${students_file} )
if [[ ${student_array[0]} = class* ]]; then
# look for first line in students file if its a class alias
class="${student_array[0]/class:/}"
class=$(trim $class)
class_prefix_from_file="${class}."
student_array[0]=''
student_count_delta=-1
fi
}
url=${COURSE_GIT_URL:-'github.com'}
action='pull'
depth_flag=""
ellapsed=5
verbose=0
color=1
repo=${COURSE_GIT_REPO:-'work'}
branch='master'
class_prefix=''
while getopts ":hpc:vk:u:r:b:dit:n" opt; do
case ${opt} in
h ) display "${Bold}Usage:\t ${Normal}$0 ${Normal}[-h] | [-p | -c <depth> [-r <repo>]] [-v] [-i | -t <time>] [user [user]...]\n"
display "\t $0 \t ${Red}pull${Normal} all repos\n"
display "\t $0 user1 user2 ... \t pull repos for user1 user2...\n"
display "${Bold}Options${Normal}\n"
display "\t -p \t\t pull, this is the default\n"
display "\t -c <depth> \t clone optionaly with depth\n"
display "\t -r <repo> \t git remote repo name, by default taken from $COURSE_GIT_REPO or defaulted to 'work'\n"
# display "\t -i \t immediate, no wait between actions on repos\n"
display "\t -k <class> \t class/course name. user will be class.user\n"
display "\t -u <URL> \t git base URL\n"
display "\t -d \t\t remove local repository\n"
display "\t -t n \t\t wait n seconds between actions on repos\n"
display "\t -v \t\t verbose mode\n"
display "\t -n \t\t no color\n"
display "\t -h \t\t help message\n"
exit 0
;;
p ) action="pull"
;;
c ) action="clone"
if [ $OPTARG != "" ]; then
depth_flag="--depth={$OPTARG}"
fi
;;
v ) verbose=1
;;
k) klass_arg=1
if [ $OPTARG != "" ]; then
class_prefix="${OPTARG}."
fi
;;
u) if [ $OPTARG != "" ]; then
url=$OPTARG
fi
;;
r) if [ $OPTARG != "" ]; then
repo=$OPTARG
fi
;;
b) if [ $OPTARG != "" ]; then
branch=$OPTARG
fi
;;
d) action="delete"
;;
i) ellapsed=0
;;
t ) if [ $OPTARG != "" ]; then
ellapsed=$OPTARG
fi
;;
n ) color=0
;;
\? ) echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
esac
done
if [ $verbose == 1 ]; then
ellapsed=6
fi
WithColor $color
ClearScreen
curdir=$PWD
shift $((OPTIND -1))
students=$*
student_count_delta=0
if [ -z "$klass_arg" ]; then
if [ -e class ]; then
# or read class name from file
class=$(<class)
class=$(trim $class)
class_prefix="${class}."
elif [ -e students.lst ]; then
load_students
class_prefix=$class_prefix_from_file
else
class=`basename "$PWD"`
class_prefix="${class}."
fi
fi
if [[ ! -z "${students// }" ]]; then
student_array=( ${students} )
student_count=${#student_array[@]}
else
if [ -z "${student_array}" ]; then
load_students
fi
student_count=${#student_array[@]}
student_count=$((student_count+student_count_delta))
fi
display "${Blue} Retrieving code updates for ${LightCyan}${repo}${Blue} from ${LightCyan}${student_count} ${Blue}students in class ${LightCyan}${class_prefix}${Blue}"
display "${LightBlue} [${LightYellow}\U2248 ${LightCyan}$(($student_count * $ellapsed))${Blue} seconds${LightBlue}]\n"
curr=0
skip=0
for d in ${student_array[@]}; do
d=$(trim $d)
if [ "$d" == "" ];then
continue
fi
((curr++))
ticker="${Normal}${Yellow} \U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027\U2027${Normal}${Blue}(${LightYellow}$curr${Normal}${Green}/$student_count${Blue}) ${Cyan}${Bold}"
if [ ${d:0:1} == "#" ] ; then
display "${Bold}${Yellow} \UFE57${Normal}${LightYellow}\U00AB${Yellow}skipped${Normal}${LightYellow}\U00BB ${ticker} ${d:1} ${LightYellow}\U2715\n"
continue;
fi
rm -f ${d}.log
if [ $action == "delete" ]; then
display "${Bold}${LightRed} \U2623 ${Normal}${LightYellow}\U00AB${Yellow}removed${Normal}${LightYellow}\U00BB ${ticker} ${d} ${LightRed}\U2715\n"
rm -Rf ${d}
continue;
fi
if [ $action == 'clone' ] || [ ! -d "$d" ] || [ ! "$(ls -A $d)" ]; then
display "${Normal}${LightRed}✇ ${LightYellow}${UU_HCHEVRONB}${Cyan}cloning${LightYellow}\U00BB ${ticker} $d "
SaveCursor
git clone $depth_flag git@$url:${class_prefix}$d/$repo.git $d > /dev/null 2> ${d}.log
gitstatus=$?
RestoreCursor
if [ $gitstatus -eq 0 ]; then
display " ${LightGreen} ✓ \n"
rm ${d}.log
else
display " ${Bold}${LightRed} 🔥 ${LightCyan}${repo}${LightRed}?\n"
echo -e "\ngit clone ${depth_flag} git@${url}:${class_prefix}${d}/${repo}.git ${d}" >> ${d}.log
fi
else
cd $d
display "${LightGreen}✇ ${LightYellow}\U00AB ${Cyan}pulling${LightYellow}\U00BB ${ticker} $d "
SaveCursor
display "\n"
git stash > /dev/null 2>&1
git $action > /dev/null 2>${d}.log
gitstatus=$?
RestoreCursor
if [ $gitstatus -eq 0 ]; then
display " ${LightGreen} ✓ \n"
rm ${d}.log
else
display " ${Bold}${LightRed} 🔥${Normal}${LightYellow} pulling ${LightCyan}${repo}\n"
fi
SaveCursor
if (( $verbose == 1 )); then
line=$((curr+3))
display "\033[${line};28r"
RestoreCursor
SaveCursor
git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%n %C(bold cyan) %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --stat -5
# waitForLoop $ellapsed $curr $((student_count+1))
display "\033[;r"
fi
if (( $verbose != 1 )); then
waitForLoop $ellapsed $curr $student_count
fi
RestoreCursor
ClearToEndOfScreen
cd $curdir
fi
done