-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgib
executable file
·1092 lines (1001 loc) · 35.2 KB
/
gib
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
readonly SCRIPT_NAME="$(basename $0)"
readonly SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
readonly GITHUB_MAX_PER_PAGE=100
readonly GITLAB_MAX_PER_PAGE=100
readonly DEFAULT_SPIN_INTERVAL=0.3
readonly LOCAL_CONFIG_FILE_PATH='./.gibconf'
readonly GLOBAL_CONFIG_FILE_PATH="$HOME/.gibconf"
readonly SYSTEM_CONFIG_FILE_PATH='/etc/gibconf'
readonly WORKSPACE_NAME_WIDTH=-20
readonly CONFIG_LINE_WIDTH=-80
readonly DIR_WIDTH=-25
readonly DEFAULT_CURL_TIMEOUT_OPTIONS='--connect-timeout 5 --max-time 60'
readonly DEFAULT_CURL_RETRY_OPTIONS='--retry 5 --retry-delay 0 --retry-max-time 60'
readonly DEFAULT_GIT_RETRY_NUM=5
readonly DEFAULT_GIT_RETRY_MAXIMUM_DELAY=30
is_empty()
{
[ -z "${1}" ]
}
is_set()
{
[ -n "${1}" ]
}
is_tty()
{
[ -t "${1}" ]
}
is_dir()
{
[ -d "${1}" ]
}
is_readable_file()
{
[ -f "${1}" ] && [ -r "${1}" ]
}
is_number()
{
[[ "${1}" =~ ^-?[0-9]+$ ]]
}
has_command()
{
command -v "${1}" &> /dev/null
}
# Join elements with delimiter
join_by()
{
local d="${1}"; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}";
}
# Display the rotating cursor
spin()
{
local spinner="/-\|"
while :; do
for (( i = 0; i < ${#spinner}; i++ )); do
printf '%s\r' "${spinner:$i:1}"
sleep "${1:-1}"
done
done
}
readonly CO_NORMAL=$(echo -en "\e[0m") # Reset
readonly CO_RED=$(echo -en "\e[31m")
readonly CO_GREEN=$(echo -en "\e[32m")
readonly CO_BO_RED=$(echo -en "\e[1;31m")
readonly CO_BO_GREEN=$(echo -en "\e[1;32m")
__log_headers=()
__log_field_delimiter=': '
log_push_header()
{
while [ "${#}" \> 0 ]; do
__log_headers["${#__log_headers[@]}"]="${1}"
shift
done
}
log_pop_header()
{
if [ "${#__log_headers[@]}" \> 0 ]; then
unset __log_headers["${#__log_headers[@]}"-1]
fi
}
__log_buffer=()
log_flush()
{
for l in "${__log_buffer[@]}"; do
echo -e "$l"
done
unset __log_buffer
}
log_error()
{
is_set "$__log_off" && return
local line=$(join_by "${__log_field_delimiter}" ${__log_levels:+error} "${__log_headers[@]}" "${@}")
line="${__log_color_stderr:+${CO_RED}}${line}${__log_color_stderr:+${CO_NORMAL}}"
if is_set "$__log_buffered_mode"; then
__log_buffer+=( "$line" )
else
echo -e "$line" >&2
fi
}
log_info()
{
is_set "$__log_off" && return
local line=$(join_by "${__log_field_delimiter}" ${__log_levels:+info} "${__log_headers[@]}" "${@}")
if is_set "$__log_buffered_mode"; then
__log_buffer+=( "$line" )
else
echo -e "${line}" >&2
fi
}
log_checkup_message()
{
is_set "$__log_off" && return
local line=$(join_by "${__log_field_delimiter}" ${__log_levels:+info} "${__log_headers[@]}" "${@}")
line="${__log_color_stderr:+${CO_BO_RED}}${line}${__log_color_stderr:+${CO_NORMAL}}"
if is_set "$__log_buffered_mode"; then
__log_buffer+=( "$line" )
else
echo -e "${line}" >&2
fi
}
log_sub_command_message()
{
is_set "$__log_off" && return
if [ "${1}" = 0 ]; then
local co="$CO_BO_GREEN"
else
local co="$CO_BO_RED"
fi
shift
local line=$(join_by "${__log_field_delimiter}" ${__log_levels:+info} "${__log_headers[@]}" "${@}")
line="${__log_color_stderr:+${co}}${line}${__log_color_stderr:+${CO_NORMAL}}"
if is_set "$__log_buffered_mode"; then
__log_buffer+=( "$line" )
else
echo -e "${line}" >&2
fi
}
declare -r EC_ERROR=1 # General error
declare -r EC_ENOENT=97 # No such file or directory
declare -r EC_ILLEGAL_CMD=127 # Command not found
declare -r EC_FATAL_SIGNAL_BASE=128 # Base value for fatal error signal "n"
# Default trap handler
on_trapped()
{
is_set "$spin_pid" && kill -9 "$spin_pid" &> /dev/null
local num name
if is_number "${1}"; then
num="${1}"
name=$(kill -l "${1}")
else
name="${1}"
num=$(kill -l "${1}")
fi
log_info "Interrupted by signal $name"
exit "$((EC_FATAL_SIGNAL_BASE + num))"
}
set_trap_handler()
{
local func="${1}" sig; shift
for sig ; do
trap "$func $sig" "$sig"
done
}
usage()
{
cat << EOF >&2
USAGE: $SCRIPT_NAME [-h | --help] [-v | --verbose] [-q | --quiet] [--force]
[--local] [--global] [--system] [-f <config file> | --file <config file>]
[-l | --list] [-w <workspace name> | --workspace <workspace name>]
[--repourls[=<url>]] [--repodirs[=<path>]] [--users[=<user>]]
[--orgs[=<organization>]] [--starrings[=<user>]] [--watchings[=<user>]] [--followings[=<user>]]
[--clone] [--checkup]
COMMAND
OPTIONS
-v, --verbose
Verbose output. Progress will be reported to stdout.
-q, --quiet
Operate quietly. Error message and progress is not reported to stdout and stderr.
--force
Enable forced mode to execute commands. Perform forced clone when it appears with the --clone option.
--local
Load the configuration file in the current directory ./.gibconf.
--global
Load user-wide configuration file ~/.gibconf.
--system
Load system-wide configuration file /etc/gibconf.
-f <config file>, --file <config file>
Use the given config file.
-l, --list
Output the configuration file. The format command is git config.
-w <workspace name>, --workspace <workspace name>
Specify the workspace name to operate. Each workspace contains the repositories, directories, users, Only one workspace can be specified per run.
--repourls[=<url>]
Specify the section of the repository urls.
--repodirs[=<path>]
Specify the section of the repository directories.
--users[=<user>]
Specify the section of the users.
--orgs[=<organization>]
Specify the section of the organizations.
--starrings[=<user>]
Specify the section of the starred by user.
--watchings[=<user>]
Specify the section of the watching by user.
--followings[=<user>]
Specify the section of the following by user.
--clone
Batch cloning or forced clone if the repository exists and --force option is used.
--checkup
Batch check the repositories, including their default branch and working tree status.
EOF
exit
}
verbose_left_align()
{
if [ "$output_mode" = verbose ]; then
if is_set "${2}"; then
str=$(printf "%*s%*s%*s%s" "$WORKSPACE_NAME_WIDTH" "$workspace_name.$key" "$CONFIG_LINE_WIDTH" "| '$conf_line'" "$DIR_WIDTH" "| ${1}" "| ${2}")
else
str=$(printf "%*s%*s%s" "$WORKSPACE_NAME_WIDTH" "$workspace_name.$key" "$CONFIG_LINE_WIDTH" "| '$conf_line'" "| ${1}")
fi
echo -n "$str"
__length_of_left_aligned="${#str}"
fi
}
verbose_right_most_align()
{
if [ "$output_mode" = verbose ]; then
if [ "${1}" = 0 ]; then
local co="$CO_BO_GREEN"
local msg="${2:-SUCCESS}"
else
local co="$CO_BO_RED"
local msg="${3:-FAILED}"
fi
if is_set "$__log_color_stdout"; then
rightmost_width="$(($(tput cols)-${__length_of_left_aligned}))"
else
rightmost_width="$((209-${__length_of_left_aligned}))"
fi
printf "${__log_color_stdout:+${co}}%*s${__log_color_stdout:+${CO_NORMAL}}\n" "$rightmost_width" "$msg"
fi
}
verbose()
{
if [ "$output_mode" = verbose ]; then
echo -e "${*}"
fi
}
# Get username or organization name
get_name_from_url()
{
local name=$(basename "${1}" 2> /dev/null)
echo -n "${name%.*}"
}
check_deps()
{
if ! has_command git; then
log_error 'Running this script require the Git<https://git-scm.com/> to be installed'
exit "$EC_ILLEGAL_CMD"
fi
if ! has_command jq; then
log_error 'Running this script require the jq<https://stedolan.github.io/jq/> to be installed'
exit "$EC_ILLEGAL_CMD"
fi
if ! has_command curl; then
log_error 'Running this script require the curl<https://curl.haxx.se/> to be installed'
exit "$EC_ILLEGAL_CMD"
fi
if ! has_command realpath; then
log_error 'Running this script require the Coreutils<http://www.gnu.org/software/coreutils/> to be installed'
exit "$EC_ILLEGAL_CMD"
fi
}
# Check if current directory is git repository
is_inside_git_repo()
{
git rev-parse --is-inside-work-tree &> /dev/null
}
remote_git_url()
{
git config --local --get remote.origin.url 2> /dev/null
}
find_git_url_from_repodir()
{
if ! _pushd "${1}"; then
return 1
fi
if ! is_inside_git_repo; then
return 1
fi
echo $(remote_git_url)
_popd
}
readonly GITHUB_API_LIST_USER='https://api.github.com/users/<NAME>'
readonly GITHUB_API_LIST_USER_REPOS='https://api.github.com/users/<NAME>/repos'
readonly GITLAB_API_LIST_USER_PROJECTS='https://gitlab.com/api/v4/users/<NAME>/projects'
readonly GITHUB_API_LIST_ORG_REPOS='https://api.github.com/orgs/<NAME>/repos'
readonly GITLAB_API_LIST_GROUP_PROJECTS='https://gitlab.com/api/v4/groups/<NAME>/projects'
readonly GITHUB_API_LIST_USER_STARRED='https://api.github.com/users/<NAME>/starred'
readonly GITHUB_API_LIST_USER_SUBSCRIPTIONS='https://api.github.com/users/<NAME>/subscriptions'
readonly GITHUB_API_LIST_USER_FOLLOWING='https://api.github.com/users/<NAME>/following'
query_repos_of_user_in_github()
{
token="${token:-$default_access_token}"
token="${token:+ --header \"Authorization: token $token\"}"
pageno=1
repos=''
while :; do
list=$(eval "curl -q ${DEFAULT_CURL_TIMEOUT_OPTIONS} ${DEFAULT_CURL_RETRY_OPTIONS} -s${token} '${1}?per_page=${GITHUB_MAX_PER_PAGE}&page=${pageno}'" 2> /dev/null | jq -r '.[] .ssh_url' 2> /dev/null)
is_empty "$list" && break
repos+=" $list"
let pageno++
done
echo "$repos"
}
query_repos_of_user_in_gitlab()
{
token="${token:-$default_access_token}"
token="${token:+ --header \"PRIVATE-TOKEN: $token\"}"
pageno=1
repos=''
while :; do
list=$(eval "curl -q ${DEFAULT_CURL_TIMEOUT_OPTIONS} ${DEFAULT_CURL_RETRY_OPTIONS} -s${token} '${1}?per_page=${GITLAB_MAX_PER_PAGE}&page=${pageno}'" 2> /dev/null | jq -r '.[] .ssh_url_to_repo' 2> /dev/null)
is_empty "$list" && break
repos+=" $list"
let pageno++
done
echo "$repos"
}
query_following_peoples_in_github()
{
token="${token:-$default_access_token}"
token="${token:+ --header \"Authorization: token $token\"}"
pageno=1
peoples=''
while :; do
list=$(eval "curl -q ${DEFAULT_CURL_TIMEOUT_OPTIONS} ${DEFAULT_CURL_RETRY_OPTIONS} -s${token} '${1}?per_page=${GITHUB_MAX_PER_PAGE}&page=${pageno}'" 2> /dev/null | jq -r '.[] .login' 2> /dev/null)
is_empty "$list" && break
peoples+=" $list"
let pageno++
done
echo "$peoples"
}
mkpd()
{
mkdir -p "${1}" &> /dev/null && pushd "${1}" &> /dev/null; rv="${?}"
[ "$rv" = 0 ] && ws_relpath=$(realpath --relative-to="${workspace_base_dir}" "${PWD}" 2> /dev/null)
return "$rv"
}
_pushd()
{
pushd "${1}" &> /dev/null; rv="${?}"
[ "$rv" = 0 ] && ws_relpath=$(realpath --relative-to="${workspace_base_dir}" "${PWD}" 2> /dev/null)
return "$rv"
}
_popd()
{
popd &> /dev/null; rv="${?}"
[ "$rv" = 0 ] && ws_relpath=$(realpath --relative-to="${workspace_base_dir}" "${PWD}" 2> /dev/null)
return "$rv"
}
load_config()
{
eval "$conf_line" &> /dev/null
}
clean_config()
{
unset url dir branch token user org api
}
load_settings()
{
if is_empty "$workspace_name"; then
if ! workspace_name=$(git config -f "$config_file_loc" --get gib.default-workspace-name 2> /dev/null); then
log_error "'gib.default-workspace-name' not found"
exit "$EC_ERROR"
fi
fi
if ! workspace_base_dir=$(git config -f "$config_file_loc" --get "$workspace_name".workspace-base-dir 2> /dev/null); then
log_error "'$workspace_name.workspace-base-dir' not found"
exit "$EC_ERROR"
fi
default_access_token=$(git config -f $config_file_loc --get $workspace_name.default-access-token 2> /dev/null)
}
verbose_settings()
{
if [ "$output_mode" = verbose ]; then
echo "Action: $action"
echo " Configuration file: $config_file_loc"
echo " Output mode: $output_mode"
echo " Name of the workspace: $workspace_name"
echo " Base directory of the workspace: $workspace_base_dir"
is_set "$default_access_token" && echo " Default access token: $default_access_token"
echo
fi
}
# 1: Command
# 2: Number of retries
# 3: Retry maximum interval in seconds
git_command_autoretry()
{
local retry_num="${2:-${DEFAULT_GIT_RETRY_NUM}}"
local retry_maximum_delay="${3:-${DEFAULT_GIT_RETRY_MAXIMUM_DELAY}}"
local backoff=1
local times=1
while :; do
eval "git ${1}" &> /dev/null; rv="${?}"
if [ "$rv" = 0 ] || [ "$times" -ge "${retry_num}" ]; then
break
fi
sleep "$backoff"
[ "$((backoff*2))" -le "$retry_maximum_delay" ] && backoff="$((backoff*2))"
let times++
done
return "$rv"
}
# 1: repo dir
# 2: repo url
force_clone()
{
if ! _pushd "${1}"; then
log_error "$ws_relpath" "Failed to change directory '${1}'"
return 1
fi
if ! is_inside_git_repo; then
log_error "$ws_relpath" "Directory '${1}' is not a git repository"
return 1
fi
for remote in $(git remote 2> /dev/null); do
git remote rm "$remote" &> /dev/null
done
git remote add origin "${2}" &> /dev/null; rv="${?}"
if [ "$rv" = 0 ]; then
git fetch origin &> /dev/null; rv="${?}"
fi
if [ "$rv" != 0 ]; then
log_error "$ws_relpath" "Failed to changing the repository url to '${2}'"
return 1
fi
if is_empty "$branch"; then
if ! branch=$(LC_ALL=C git remote show origin 2> /dev/null | grep -oP '(?<=HEAD branch: )[^ ]+$'); then
log_error "$ws_relpath" "Remote head branch of the '${2}' not found"
return 1
fi
git remote set-head origin "$branch" &> /dev/null
else
git remote set-head origin -a &> /dev/null
fi
git clean -fd &> /dev/null
git reset --hard HEAD &> /dev/null
git checkout "$branch" &> /dev/null
if [ "${?}" != 0 ]; then
log_error "$ws_relpath" "Failed to checkout branch to '$branch' in '${2}'"
return 1
fi
git reset --hard origin/"$branch" &> /dev/null
branches=$(git branch 2> /dev/null | grep -v \* | xargs)
if is_set "$branches"; then
git branch -D "$branches" &> /dev/null
fi
git branch --set-upstream-to=origin/"$branch" "$branch" &> /dev/null
_popd
}
# 1: repo dir
# 2: repo url
clone_repo()
{
repo_dir="${1:-$(get_name_from_url ${2})}"
repo_url="${2:-$(find_git_url_from_repodir ${1})}"
verbose_left_align "$repo_dir" "$repo_url"
if ! is_dir "$repo_dir"; then
if is_empty "$repo_url"; then
verbose_right_most_align 1
log_error "$ws_relpath" "'$repo_dir' does not exist and the url is undefined"
return
fi
git_command_autoretry "clone -q${branch:+ -b $branch} $repo_url $repo_dir"; rv="${?}"
verbose_right_most_align "$rv"
[ "$rv" != 0 ] && log_error "$ws_relpath" "Clone '$repo_url' failed"
elif is_set "$is_force"; then
errmsg=$(force_clone "$repo_dir" "$repo_url" 2>&1 1> /dev/null); rv="${?}"
verbose_right_most_align "$rv"
is_set "$errmsg" && echo -e "$errmsg"
else
verbose_right_most_align 1
log_error "$ws_relpath" "'$repo_dir' already exists"
fi
}
clone()
{
load_settings
verbose_settings
if ! workspace_base_dir=$(realpath "$workspace_base_dir" 2> /dev/null) \
|| ! mkpd "$workspace_base_dir"; then
log_error "Failed to create directory '$workspace_base_dir'"
exit "$EC_ERROR"
fi
if [ "$output_mode" = normal ]; then
spin "$DEFAULT_SPIN_INTERVAL" &
spin_pid="${!}"
fi
for key in "${!kv_map[@]}"; do
log_push_header "$workspace_name.$key"
value="${kv_map[$key]}"
git config -f "$config_file_loc" --get-all "$workspace_name"."$key" "$value" 2> /dev/null | while read conf_line; do
log_push_header "'$conf_line'"
clean_config
if ! load_config; then
log_error "Failed to load '$conf_line'"
log_pop_header
continue
fi
case "$key" in
repourls )
if is_empty "$url"; then
log_error "url is undefined"
log_pop_header
continue
fi
clone_repo "$dir" "$url" ;;
repodirs )
if is_empty "$dir"; then
log_error "dir is undefined"
log_pop_header
continue
fi
clone_repo "$dir" "$url" ;;
followings )
if is_set "$api" && [ "$api" != github ]; then
log_error "'$api' is not yet supported"
log_pop_header
continue
fi
if is_empty "$user"; then
log_error "user is undefined"
log_pop_header
continue
fi
if ! mkpd "${dir:-$user}"; then
log_error "Failed to create directory '${dir:-$user}'"
log_pop_header
continue
fi
api_url="${GITHUB_API_LIST_USER_FOLLOWING//<NAME>/${user}}"
peoples=$(query_following_peoples_in_github "$api_url")
peoples_num=$(wc -w <<< "$peoples")
verbose "Get '$peoples_num' following peoples by querying the '$api_url'"
for people in $peoples; do
mkpd "$people"
api_url="${GITHUB_API_LIST_USER_REPOS//<NAME>/${people}}"
repos=$(query_repos_of_user_in_github "$api_url")
repos_num=$(wc -w <<< "$repos")
verbose "Get '$repos_num' repositories by querying the '$api_url'"
for repo in $repos; do
clone_repo '' "$repo"
done
_popd
done
_popd ;;
users | orgs | starrings | watchings )
case "$api" in
'' | github )
case "$key" in
users )
if is_empty "$user"; then
log_error "user is undefined"
log_pop_header
continue
fi
dir="${dir:-$user}"
api_url="${GITHUB_API_LIST_USER_REPOS//<NAME>/${user}}" ;;
orgs )
if is_empty "$org"; then
log_error "org is undefined"
log_pop_header
continue
fi
dir="${dir:-$org}"
api_url="${GITHUB_API_LIST_ORG_REPOS//<NAME>/${org}}" ;;
starrings )
if is_empty "$user"; then
log_error "user is undefined"
log_pop_header
continue
fi
dir="${dir:-$user}"
api_url="${GITHUB_API_LIST_USER_STARRED//<NAME>/${user}}" ;;
watchings )
if is_empty "$user"; then
log_error "user is undefined"
log_pop_header
continue
fi
dir="${dir:-$user}"
api_url="${GITHUB_API_LIST_USER_SUBSCRIPTIONS//<NAME>/${user}}" ;;
esac
if ! mkpd "$dir"; then
log_error "Failed to create directory '$dir'"
log_pop_header
continue
fi
repos=$(query_repos_of_user_in_github "$api_url")
repos_num=$(wc -w <<< "$repos")
verbose "Get '$repos_num' repositories by querying the '$api_url'"
for repo in $repos; do
clone_repo '' "$repo"
done
_popd ;;
gitlab )
case "$key" in
users )
if is_empty "$user"; then
log_error "user is undefined"
log_pop_header
continue
fi
dir="${dir:-$user}"
api_url="${GITLAB_API_LIST_USER_PROJECTS//<NAME>/${user}}" ;;
orgs )
if is_empty "$org"; then
log_error "org is undefined"
log_pop_header
continue
fi
dir="${dir:-$org}"
api_url="${GITLAB_API_LIST_GROUP_PROJECTS//<NAME>/${org}}" ;;
* )
log_error "'$api' is not yet supported"
log_pop_header
continue ;;
esac
if ! mkpd "$dir"; then
log_error "Failed to create directory '$dir'"
log_pop_header
continue
fi
repos=$(query_repos_of_user_in_gitlab "$api_url")
repos_num=$(wc -w <<< "$repos")
verbose "Get '$repos_num' repositories by querying the '$api_url'"
for repo in $repos; do
clone_repo '' "$repo"
done
_popd ;;
* )
log_error "'$api' is not yet supported"
log_pop_header
continue ;;
esac ;;
* )
log_error "Impossible error"
exit "$EC_ERROR" ;;
esac
clean_config
log_pop_header
done
log_pop_header
done
is_set "$spin_pid" && kill -9 "$spin_pid" &> /dev/null
_popd
}
bulk_run_impl()
{
load_settings
verbose_settings
if ! workspace_base_dir=$(realpath "$workspace_base_dir" 2> /dev/null) \
|| ! _pushd "$workspace_base_dir"; then
log_error "Failed to change directory '$workspace_base_dir'"
exit "$EC_ERROR"
fi
if [ "$output_mode" = normal ]; then
spin "$DEFAULT_SPIN_INTERVAL" &
spin_pid="${!}"
fi
for key in "${!kv_map[@]}"; do
log_push_header "$workspace_name.$key"
value="${kv_map[$key]}"
git config -f "$config_file_loc" --get-all "$workspace_name"."$key" "$value" 2> /dev/null | while read conf_line; do
log_push_header "'$conf_line'"
clean_config
if ! load_config; then
log_error "Failed to load '$conf_line'"
log_pop_header
continue
fi
case "$key" in
repourls )
if is_empty "$url"; then
log_error "url is undefined"
log_pop_header
continue
fi
"$repofn" "$dir" "$url" ;;
repodirs )
if is_empty "$dir"; then
log_error "dir is undefined"
log_pop_header
continue
fi
"$repofn" "$dir" "$url" ;;
followings )
if is_set "$api" && [ "$api" != github ]; then
log_error "'$api' is not yet supported"
log_pop_header
continue
fi
if is_empty "$user"; then
log_error "user is undefined"
log_pop_header
continue
fi
if ! _pushd "${dir:-$user}"; then
log_error "Failed to change directory '${dir:-$user}'"
log_pop_header
continue
fi
api_url="${GITHUB_API_LIST_USER_FOLLOWING//<NAME>/${user}}"
peoples=$(query_following_peoples_in_github "$api_url")
peoples_num=$(wc -w <<< "$peoples")
verbose "Get '$peoples_num' following peoples by querying the '$api_url'"
for people in $peoples; do
if ! _pushd "$people"; then
log_error "Failed to change directory '$people'"
log_pop_header
continue
fi
api_url="${GITHUB_API_LIST_USER_REPOS//<NAME>/${people}}"
repos=$(query_repos_of_user_in_github "$api_url")
repos_num=$(wc -w <<< "$repos")
verbose "Get '$repos_num' repositories by querying the '$api_url'"
for repo in $repos; do
"$repofn" '' "$repo"
done
_popd
done
_popd ;;
users | orgs | starrings | watchings )
case "$api" in
'' | github )
case "$key" in
users )
if is_empty "$user"; then
log_error "user is undefined"
log_pop_header
continue
fi
dir="${dir:-$user}"
api_url="${GITHUB_API_LIST_USER_REPOS//<NAME>/${user}}" ;;
orgs )
if is_empty "$org"; then
log_error "org is undefined"
log_pop_header
continue
fi
dir="${dir:-$org}"
api_url="${GITHUB_API_LIST_ORG_REPOS//<NAME>/${org}}" ;;
starrings )
if is_empty "$user"; then
log_error "user is undefined"
log_pop_header
continue
fi
dir="${dir:-$user}"
api_url="${GITHUB_API_LIST_USER_STARRED//<NAME>/${user}}" ;;
watchings )
if is_empty "$user"; then
log_error "user is undefined"
log_pop_header
continue
fi
dir="${dir:-$user}"
api_url="${GITHUB_API_LIST_USER_SUBSCRIPTIONS//<NAME>/${user}}" ;;
esac
if ! _pushd "$dir"; then
log_error "Failed to change directory '$dir'"
log_pop_header
continue
fi
repos=$(query_repos_of_user_in_github "$api_url")
repos_num=$(wc -w <<< "$repos")
verbose "Get '$repos_num' repositories by querying the '$api_url'"
for repo in $repos; do
"$repofn" '' "$repo"
done
_popd ;;
gitlab )
case "$key" in
users )
if is_empty "$user"; then
log_error "user is undefined"
log_pop_header
continue
fi
dir="${dir:-$user}"
api_url="${GITLAB_API_LIST_USER_PROJECTS//<NAME>/${user}}" ;;
orgs )
if is_empty "$org"; then
log_error "org is undefined"
log_pop_header
continue
fi
dir="${dir:-$org}"
api_url="${GITLAB_API_LIST_GROUP_PROJECTS//<NAME>/${org}}" ;;
* )
log_error "'$api' is not yet supported"
log_pop_header
continue ;;
esac
if ! _pushd "$dir"; then
log_error "Failed to change directory '$dir'"
log_pop_header
continue
fi
repos=$(query_repos_of_user_in_gitlab "$api_url")
repos_num=$(wc -w <<< "$repos")
verbose "Get '$repos_num' repositories by querying the '$api_url'"
for repo in $repos; do
"$repofn" '' "$repo"
done
_popd ;;
* )
log_error "'$api' is not yet supported"
log_pop_header
continue ;;
esac ;;
* )
log_error "Impossible error"
exit "$EC_ERROR" ;;
esac
clean_config
log_pop_header
done
log_pop_header
done
is_set "$spin_pid" && kill -9 "$spin_pid" &> /dev/null
_popd
}
# 1: repo dir
# 2: repo url
check_repo()
{
repo_dir="${1:-$(get_name_from_url ${2})}"
repo_url="${2:-$(find_git_url_from_repodir ${1})}"
verbose_left_align "$repo_dir" "$repo_url"
if ! _pushd "$repo_dir"; then
verbose_right_most_align 1 PASSED FAILED
log_error "$ws_relpath" "Failed to change directory '$repo_dir'"
return
fi
if ! is_inside_git_repo; then
verbose_right_most_align 1 PASSED FAILED
log_error "$ws_relpath" "Directory '$repo_dir' is not a git repository"
return
fi
__log_buffered_mode=true
current_branch=$(git branch -q 2> /dev/null | grep \* | cut -d ' ' -f2)
if is_set "$branch"; then
if [ "$current_branch" != "$branch" ]; then
log_checkup_message "$ws_relpath" "The current branch is '$current_branch' and default branch is '$branch'"
fi
else
if ! first_branch=$(LC_ALL=C git remote show origin 2> /dev/null | grep -oP '(?<=HEAD branch: )[^ ]+$'); then
log_checkup_message "$ws_relpath" "Remote head branch of the '${1}' not found"
fi
if [ "$current_branch" != "$first_branch" ]; then
log_checkup_message "$ws_relpath" "The current branch is '$current_branch' and remote head branch is '$first_branch'"
fi
fi
tree_status=$(git status -s 2> /dev/null)
is_set "$tree_status" && log_checkup_message "$ws_relpath" "The working tree status of Repository '${repo_url}': \n${tree_status}"
if [ -n "$__log_buffer" ]; then
verbose_right_most_align 1 PASSED FAILED
log_flush
else
verbose_right_most_align 0 PASSED FAILED
fi
unset __log_buffered_mode
_popd
}
checkup()
{
repofn=check_repo
bulk_run_impl
}
# 1: repo dir
# 2: repo url
command_repo()
{
repo_dir="${1:-$(get_name_from_url ${2})}"
repo_url="${2:-$(find_git_url_from_repodir ${1})}"
verbose_left_align "$repo_dir" "$repo_url"
if ! _pushd "$repo_dir"; then
verbose_right_most_align 1
log_error "$ws_relpath" "Failed to change directory '${repo_dir}'"
return
fi
if ! is_inside_git_repo; then
verbose_right_most_align 1
log_error "$ws_relpath" "Directory '${repo_dir}' is not a git repository"
return
fi
errmsg=$(eval "$sub_command" 2>&1); rv="${?}"
verbose_right_most_align "$rv"
is_set "$errmsg" && log_sub_command_message "$rv" "$ws_relpath" "The output of Repository '${repo_url}': \n${errmsg}"
_popd
}
bulk_run()
{
repofn=command_repo
bulk_run_impl
}
list_all()
{
git config -f "$config_file_loc" -l 2> /dev/null
}
list_values()