Repository navigation
Expand file tree
/
Copy pathkgit
More file actions
executable file
·478 lines (419 loc) · 12.2 KB
/
Copy pathkgit
File metadata and controls
executable file
·478 lines (419 loc) · 12.2 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/env bash
set -e
_info() {
( [ -n "$_silence" ] || [ -n "$_quiet" ] ) && return
printf "\e[1;36m${0##*/}:\e[1;37m $@ \e[00m\n"
}
_say() {
( [ -n "$_silence" ] || [ -n "$_quiet" ] ) && return
printf "\e[1;36m${0##*/}:\e[1;37m $@ \e[00m\n"
}
_error() {
[ -n "$_silence" ] && return
printf "\e[1;36m${0##*/}:\e[1;31m error:\e[1;37m $@ \e[00m\n"
}
_die () {
_error "$@"
exit 1
}
# macOS/BSD does not support readlink '-f' flag
another_readlink() {
TARGET_FILE="$1"
cd "$(dirname "$TARGET_FILE")"
TARGET_FILE="$(basename "$TARGET_FILE")"
# Iterate down a (possible) chain of symlinks
while [ -L "$TARGET_FILE" ]; do
TARGET_FILE="$(readlink "$TARGET_FILE")"
cd "$(dirname "$TARGET_FILE")"
TARGET_FILE="$(basename "$TARGET_FILE")"
done
# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
PHYS_DIR="$(pwd -P)"
RESULT="$PHYS_DIR/$TARGET_FILE"
echo "$RESULT"
}
is_kazoo_src() {
if [ -d "$1/rel" ] && [ -d "$1/scripts" ]; then
return 0
else
return 1
fi
}
_script_dir="$(dirname $(another_readlink "$0"))"
if is_kazoo_src "$_script_dir/.."; then
ROOT="$_script_dir/.."
elif is_kazoo_src "$_script_dir"; then
ROOT="$_script_dir"
elif [ -n "$KAZOO_SRC" ] && is_kazoo_src "$KAZOO_SRC"; then
ROOT="$KAZOO_SRC"
else
_die "Cannot find kazoo source root directory. Please set KAZOO_SRC env variable \
or put this script in root of Kazoo source directory"
fi
unset _script_dir
# set the cwd to kazoo source root to avoid headache
pushd "$ROOT" > /dev/null
# global variables
_kazoo_apps=
_work_dirs=
MAX_GIT_RETRIES=3
_git_retries=0
usage() {
cat <<'END' >&2
__ _______ _ __
/ //_/__ / ____ _(_) /_
/ ,< / / / __ `/ / __/
/ /| | / /__/ /_/ / / /_
/_/ |_| /____/\__, /_/\__/ ☢
Welcome to /____/ the FUTURE
A simple wrapper around git and pull request commands
to provide an easy way to develop on Kazoo multi-repo structure
Usage:
kgit.sh [OPTIONS]+ <ACTION> [ACTION_ARGS]+
Where ACTION can be one of these commands to run in specified
Kazoo source folders:
git Runs git command
gh Runs gh command (https://cli.github.com)
hub Runs hub command (https://github.com/github/hub)
glab Runs GitLab CLI command (https://docs.gitlab.com/editor_extensions/gitlab_cli)
Example:
kgit.sh -kapps crossbar,teletype git status --branch --short
Options:
-kapps app_name[,app_name...] add a comma separated list of Kazoo apps to working directories list
-exclude-apps app_name[,app_name...] exclude a comma separated list of Kazoo apps from working directories list
This takes precedence over -kapps, -a and -A.
-kcore add "core" directory to working directories list
-kroot add kazoo source root directory to working directories list
-a, --all-apps add all Kazoo apps to working directories list
-A, -All add "core", all kazoo apps and kazoo source root directory to
list of working directories. This option is default
-exit-on-error exit if command returns exit other than 0
-kchanged loop over working directories and prints the name of files which are
different than "$BASE_BRANCH". "master" is the default BASE_BRANCH
this does not run git/hub/gh/glab commands
-get-root print path to Kazoo source directory and exit
-q be quiet, but still print error messages
-qq be more quiet, also do not print errors
-help shows help
END
}
add_kazoo_apps() {
if [ -z "$_kazoo_apps" ]; then
_kazoo_apps="$1"
else
_kazoo_apps="$_kazoo_apps $1"
fi
}
exclude_kazoo_apps() {
if [ -z "$_exclude_apps" ]; then
_exclude_apps="$1"
else
_exclude_apps="$_exclude_apps $1"
fi
}
while [ $# -gt 0 ]; do
arg="$1"
case $arg in
-kapps)
if [ -n "$2" ]; then
add_kazoo_apps "$(echo "$2" | tr ',' ' ')"
shift
fi
;;
-exclude-apps)
if [ -n "$2" ]; then
exclude_kazoo_apps "$(echo "$2" | tr ',' ' ')"
shift
fi
;;
-a|-all-apps)
include_all_apps=1
;;
-A|-all)
include_all=1
;;
-kcore)
include_core=1
;;
-kroot)
include_root=1
;;
-origin)
if [ -n "$2" ]; then
git_origin="origin/${2#*/}"
shift
fi
;;
-m|-edit-message)
if [ -n "$2" ]; then
_message="$2"
shift
fi
;;
-on-changed)
on_changed=1
;;
-on-changed-header)
on_changed_head=1
;;
-kchanged)
_action="changed"
# TODO: maybe read next args for diff-ing git branch A B
# explicit end of options
explicit_opts_end=1
shift
break
;;
-get-root)
echo -n "$ROOT"
exit 0
;;
-help)
usage
exit 0
;;
-q)
_quiet=1
;;
-qq)
_silence=1
;;
-exit-on-error)
_exit_on_error=1
;;
gh)
_action="gh"
# explicit end of options
explicit_opts_end=1
shift
break
;;
glab)
_action="glab"
# explicit end of options
explicit_opts_end=1
shift
break
;;
git)
_action="git"
# explicit end of options
explicit_opts_end=1
shift
break
;;
hub)
_action="hub"
# explicit end of options
explicit_opts_end=1
shift
break
;;
*)
_die "unknown option $arg"
;;
esac
shift
done
unset arg
BASE_BRANCH="${git_origin:-$BASE_BRANCH}"
# set action argumants
set -- "$@"
if [ -z "$_action" ]; then
usage && exit 1
fi
add_work_dir() {
local path
while [ $# -gt 0 ]; do
path="$1"
[ -f "$1" ] && shift && continue
[ ! -d "$1" ] && _die "directory $1 doesn't exists!"
if [ -z "$_work_dirs" ]; then
_work_dirs="$1"
else
_work_dirs="$_work_dirs $1"
fi
shift
done
}
add_app_to_work_dir() {
local path
while [ $# -gt 0 ]; do
path="$1"
if [[ ! " $_exclude_apps " =~ " ${path#applications/} " ]]; then
add_work_dir "$path"
fi
shift
done
}
parse_paths() {
if [ -n "$include_all" ]; then
add_app_to_work_dir applications/*
add_work_dir "core"
add_work_dir "."
return
fi
[ -n "$include_core" ] && add_work_dir "core"
[ -n "$include_root" ] && add_work_dir "."
[ -n "$include_all_apps" ] && add_app_to_work_dir applications/*
for app in $_kazoo_apps; do
add_app_to_work_dir "applications/$app"
done
unset app
if [ -z "$_work_dirs" ]; then
add_app_to_work_dir applications/*
add_work_dir "core"
add_work_dir "."
fi
_work_dirs="$(echo $_work_dirs | tr ' ' '\n' | sort -u | tr '\n' ' ')"
}
parse_paths
if [ -z "$_work_dirs" ]; then
_die "no working directories were given"
fi
get_changed() {
local base_branch="${BASE_BRANCH:-$(<$ROOT/.base_branch)}"
[ -f $1/.base_branch ] && base_branch="${BASE_BRANCH:-$(<$1/.base_branch)}"
local diff=""
for file in $(git -C $1 --no-pager diff --name-only HEAD $base_branch); do
diff+=" $1/$file"
done
echo "$diff"
}
action_changed() {
local changed=""
local directory
for directory in $_work_dirs; do
if [ -d $directory ]; then
changed+=" $(get_changed "$directory")"
fi
done
for FF in $changed; do
echo $FF
done
}
check_action() {
if [ -n "$_action" ] && [ -z "$(type "$_action" 2>/dev/null)" ]; then
_error "$_action command not found"
[ -n "$_silence" ] && exit 1
echo >&2
case "$_action" in
gh)
echo "Please follow instructions in https://cli.github.com to install it." >&2
;;
git)
echo "Please install git version 2+ using your distro package manager." >&2
;;
glab)
echo "Please follow instructions in https://docs.gitlab.com/editor_extensions/gitlab_cli/#install-the-cli to install it." >&2
;;
hub)
echo "Please follow instructions in https://github.com/github/hub#installation to install it." >&2
;;
esac
exit 1
fi
if [ -z "$*" ]; then
$_action
exit 0
fi
}
run_git() {
local ret=0
local base_branch="${BASE_BRANCH:-$(<$ROOT/.base_branch)}"
[ -f $1/.base_branch ] && base_branch="${BASE_BRANCH:-$(<$1/.base_branch)}"
local dir="$1"
shift
local command="$1"
declare -A netops
netops["fetch"]=1
netops["push"]=1
netops["pull"]=1
if [ -n "$on_changed" ]; then
if [ -n "$(git -C "$dir" status --short --untracked-files=no 2>&1)" ]; then
_error "Not running git, there are some changes: " >&2
git -C "$dir" status
return 1
fi
fi
if [ -n "$on_changed_head" ]; then
if [ -n "$(git -C "$dir" --no-pager diff --name-only HEAD $base_branch 2>&1)" ]; then
_error "Not running git, there are some changes: " >&2
git -C $1 --no-pager diff --name-only HEAD $base_branch
return 1
fi
fi
git -C "$dir" "$@"
ret=$?
if [ $ret -ne 0 ]; then
if [[ 1 = ${netops[$command]} ]]; then
if [ $_git_retries -lt $MAX_GIT_RETRIES ]; then
_say "Failed, retrying... ${_git_retries}/${MAX_GIT_RETRIES}"
let "_git_retries++"
run_git "$dir" "$@"
else
echo "${MAX_GIT_RETRIES} retries performed and still failing, giving up on this one"
fi
fi
fi
_git_retries=0
return $ret
}
loop_wait_gh() {
local counter=1
# may need update this if gitlab rate limit `mr` api calls
if [[ "$@" =~ "pr create" ]] || [[ "$@" =~ "issue create" ]]; then
echo -n "Waiting 60s on stupid GitHub to avoid GraphQL limit on creation"
while [ $counter -le 60 ]; do
if [ $((counter % 5)) -eq 0 ]; then
echo -n "$counter"
else
echo -n '.'
fi
sleep 1
((counter++))
done
echo
fi
}
run_action() {
local ret
local loop_count=1
check_action "$@"
for dir in $_work_dirs; do
_info "$dir"
case "$_action" in
git)
run_git "$dir" "$@"
ret=$?
;;
*)
pushd "$dir" >/dev/null
$_action "$@"
ret=$?
popd >/dev/null
[ $((loop_count % 8)) -eq 0 ] && loop_wait_gh "$@"
((loop_count++))
esac
if [ $ret -ne 0 ] && [ -n "$_exit_on_error" ]; then
_die "$_action failed with non-zero exit code"
fi
done
}
# let git/hub/gh/glab fail, we can stop if user requested -exit-non-error
set +e
case "$_action" in
changed)
action_changed
;;
gh)
run_action "$@"
;;
hub)
run_action "$@"
;;
git)
run_action "$@"
;;
esac