-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit-clone-all
More file actions
executable file
·512 lines (423 loc) · 13.7 KB
/
git-clone-all
File metadata and controls
executable file
·512 lines (423 loc) · 13.7 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
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
#!/usr/bin/env bash
SCRIPT_PATH="$(realpath "$0")"
SCRIPT_NAME="$(basename "$SCRIPT_PATH")"
# {{{ color variables
export C_RED='\033[0;31m'
export C_BLUE='\033[0;34m'
export C_CYAN='\033[0;36m'
export C_BLACK='\033[0;30m'
export C_GREEN='\033[0;32m'
export C_WHITE='\033[0;37m'
export C_PURPLE='\033[0;35m'
export C_YELLOW='\033[0;33m'
export C_DARK_GRAY='\033[0;30m'
export C_LIGHT_RED='\033[0;31m'
export C_LIGHT_GRAY='\033[0;37m'
export C_LIGHT_BLUE='\033[0;34m'
export C_LIGHT_CYAN='\033[0;36m'
export C_LIGHT_GREEN='\033[0;32m'
export C_BROWN_ORANGE='\033[0;33m'
export C_LIGHT_PURPLE='\033[0;35m'
export C_RED_BOLD='\033[1;31m'
export C_BLUE_BOLD='\033[1;34m'
export C_CYAN_BOLD='\033[1;36m'
export C_BLACK_BOLD='\033[1;30m'
export C_GREEN_BOLD='\033[1;32m'
export C_WHITE_BOLD='\033[1;37m'
export C_PURPLE_BOLD='\033[1;35m'
export C_YELLOE_BOLD='\033[1;33m'
export C_DARK_GRAE_BOLD='\033[1;30m'
export C_LIGHT_REE_BOLD='\033[1;31m'
export C_LIGHT_GRAY_BOLD='\033[1;37m'
export C_LIGHT_BLUE_BOLD='\033[1;34m'
export C_LIGHT_CYAE_BOLD='\033[1;36m'
export C_LIGHT_GREEE_BOLD='\033[1;32m'
export C_BROWN_ORANGE_BOLD='\033[1;33m'
export C_LIGHT_PURPLE_BOLD='\033[1;35m'
export C_RED_UNDERLINED='\033[4;31m'
export C_BLUE_UNDERLINED='\033[4;34m'
export C_CYAN_UNDERLINED='\033[4;36m'
export C_BLACK_UNDERLINED='\033[4;30m'
export C_GREEN_UNDERLINED='\033[4;32m'
export C_WHITE_UNDERLINED='\033[4;37m'
export C_PURPLE_UNDERLINED='\033[4;35m'
export C_YELLOW_UNDERLINED='\033[4;33m'
export C_DARK_GRAY_UNDERLINED='\033[4;30m'
export C_LIGHT_RED_UNDERLINED='\033[4;31m'
export C_LIGHT_GRAY_UNDERLINED='\033[4;37m'
export C_LIGHT_BLUE_UNDERLINED='\033[4;34m'
export C_LIGHT_CYAN_UNDERLINED='\033[4;36m'
export C_LIGHT_GREEN_UNDERLINED='\033[4;32m'
export C_BROWN_ORANGE_UNDERLINED='\033[4;33m'
export C_LIGHT_PURPLE_UNDERLINED='\033[4;35m'
export C_RED_BLINKING='\033[5;31m'
export C_BLUE_BLINKING='\033[5;34m'
export C_CYAN_BLINKING='\033[5;36m'
export C_BLACK_BLINKING='\033[5;30m'
export C_GREEN_BLINKING='\033[5;32m'
export C_WHITE_BLINKING='\033[5;37m'
export C_PURPLE_BLINKING='\033[5;35m'
export C_YELLOW_BLINKING='\033[5;33m'
export C_DARK_GRAY_BLINKING='\033[5;30m'
export C_LIGHT_RED_BLINKING='\033[5;31m'
export C_LIGHT_GRAY_BLINKING='\033[5;37m'
export C_LIGHT_BLUE_BLINKING='\033[5;34m'
export C_LIGHT_CYAN_BLINKING='\033[5;36m'
export C_LIGHT_GREEN_BLINKING='\033[5;32m'
export C_BROWN_ORANGE_BLINKING='\033[5;33m'
export C_LIGHT_PURPLE_BLINKING='\033[5;35m'
export C_ARG="$C_BLUE"
export C_ERROR="$C_RED"
export C_TEXT="$C_WHITE"
export C_DIR="$C_PURPLE"
export C_NUMBER="$C_GREEN"
export C_REPO="$C_LIGHT_PURPLE"
export C_TEXT_HIGHLIGHT="$C_YELLOW"
# }}}
# {{{ argument variable definitions
DEFAULT_JOBS=8
DEFAULT_LIMIT=25
DEFAULT_DIR="$(pwd)"
DIR=""
DEPTH=0
JOBS=-1
LIMIT=-1
OWNER=""
# }}}
# {{{ functions
echo_usage() {
local -r SCRIPT_NAME="$1"
if [[ -z "$SCRIPT_NAME" ]]; then
echo -e "${C_ERROR}* Script name not provided or empty.${NC}"
exit 1
fi
echo -e "${C_TEXT}* Usage $SCRIPT_NAME${NC} ${C_ARG}[...args]${NC}"
echo ""
echo -e "${C_TEXT}For help, run:${NC} ${C_TEXT_HIGHLIGHT}./$SCRIPT_NAME}{NC} ${C_ARG}--help${NC}"
}
echo_description() {
echo -e "${C_TEXT} This script can be useful for quickly cloning a large number of${NC}"
echo -e "${C_TEXT} repositories from GitHub, especially for organizations with a large${NC}"
echo -e "${C_TEXT} number of repositories.${NC}"
echo ""
echo -e "${C_TEXT} It can also be used for backing up all of a user's or organization's${NC}"
echo -e "${C_TEXT} repositories to a local machine.${NC}"
echo ""
echo -e "${C_TEXT} By utilizing parallel jobs, the cloning process can be significantly${NC}"
echo -e "${C_TEXT} faster compared to cloning each echo repository one by one.${NC}"
}
echo_flags() {
echo -e " ${C_TEXT_HIGHLIGHT}Flags:${NC}"
echo ""
echo -e " ${C_ARG}--help${NC}${C_TEXT}: Show this help text${NC}"
echo ""
echo -e " ${C_ARG}--depth${NC}${C_TEXT}: The depth at which to clone the remote repository. Defaults${NC}"
echo -e " ${C_TEXT}to -1, which is the full history. Any other value will limit${NC}"
echo -e " ${C_TEXT}the clone to the specified number of commits.${NC}"
echo ""
echo -e " ${C_ARG}--dir${NC}${C_TEXT}: Sets the directory where the repositories will be cloned.${NC}"
echo ""
echo -e " ${C_ARG}--owner${NC}${C_TEXT}: Sets the username or organization name to clone all${NC}"
echo -e " ${C_TEXT}repositories for.${NC}"
echo -e " ${C_TEXT}This argument is required and must be followed by the desired${NC}"
echo -e " ${C_TEXT}username or organization name.${NC}"
echo -e " ${C_TEXT}Only repositories owned by the specified user or organization${NC}"
echo -e " ${C_TEXT}will be echo tloned.${NC}"
echo ""
echo -e " ${C_ARG}--limit${NC}${C_TEXT}: Sets the maximum number of repositories that will be cloned.${NC}"
echo -e " ${C_TEXT}This argument is optional and must be followed by a numerical${NC}"
echo -e " ${C_TEXT}value. If not specified, all repositories owned by the${NC}"
echo -e " ${C_TEXT}specified user or organization will be cloned.${NC}"
echo ""
echo -e " ${C_ARG}--jobs${NC}${C_TEXT}: Sets the number of clone processes to run in parallel.${NC}"
echo -e " ${C_TEXT}This argument is optional and must be followed by a numerical${NC}"
echo -e " ${C_TEXT}value. If not specified, the default value of 1 will be used.${NC}"
echo -e " ${C_TEXT}Increasing the number of jobs can speed up the cloning process,${NC}"
echo -e " ${C_TEXT}but may also put more strain on system resources.${NC}"
echo -e " ${C_TEXT}It is recommended to use a value that is appropriate for your${NC}"
echo -e " ${C_TEXT}system capabilities.${NC}"
}
echo_examples() {
local -r SCRIPT_NAME="$1"
if [[ -z "$SCRIPT_NAME" ]]; then
echo -e "${C_ERROR}Script name not provided or empty.${NC}"
exit 1
fi
echo -e " ${C_TEXT_HIGHLIGHT}Examples:${NC}"
echo ""
echo -e " ${C_TEXT}- To clone a maximum of${NC} ${C_NUMBER}500${NC} ${C_TEXT}repositories owned by the user 'me'${NC}"
echo -e " ${C_TEXT}using${NC} ${C_NUMBER}16${NC} ${C_TEXT}parallel jobs into the current directory, run:${NC}"
echo -e " ${C_TEXT}\$${NC} ${C_TEXT_HIGHLIGHT}./$SCRIPT_NAME${NC} ${C_ARG}--owner${NC} ${C_TEXT}me${NC} ${C_ARG}--jobs${NC} ${C_TEXT}16${NC} ${C_ARG}--limit${NC} ${C_NUMBER}500${NC} ${C_ARG}--dir${NC} ${C_TEXT}.${NC}"
echo ""
echo -e " ${C_TEXT}- To clone all repositories owned by the organization microsoft"
echo -e " ${C_TEXT}using${NC} ${C_NUMBER}20${NC} ${C_TEXT}parallel jobs and a max of${NC} ${C_NUMBER}500${NC} ${C_TEXT}repositories, into the${NC}"
echo -e " ${C_TEXT}directory at${NC} ${C_TEXT_HIGHLIGHT}./microsoft${NC}${C_TEXT}, you can run:${NC}"
echo -e " ${C_TEXT}\$${NC} ${C_TEXT_HIGHLIGHT}./$SCRIPT_NAME${NC} ${C_ARG}--owner${NC} microsoft ${C_ARG}--jobs${NC} ${C_NUMBER}20${NC} ${C_ARG}--limit${NC} ${C_NUMBER}500${NC} ${C_ARG}--dir${NC} ${C_TEXT_HIGHLIGHT}./microsoft"
}
echo_help() {
local -r SCRIPT_NAME="$1"
if [[ -z "$SCRIPT_NAME" ]]; then
echo -e "${C_ERROR}Script name not provided or empty.${NC}"
exit 1
fi
echo_description
echo ""
echo ""
echo_flags
echo ""
echo ""
echo_examples "$SCRIPT_NAME"
}
# }}}
# {{{ environment validation
FOUND_ERRORS=0
GH_PATH="$(which gh)"
JQ_PATH="$(which jq)"
PARALLEL_PATH="$(which parallel)"
if [[ -z "$GH_PATH" ]]; then
echo -e "${C_ERROR}Error: The GitHub CLI (gh) is required but was not found.${NC}"
FOUND_ERRORS=1
fi
if [[ -z "$PARALLEL_PATH" ]]; then
echo -e "${C_ERROR}Error: The parallel utility is required but was not found.${NC}"
FOUND_ERRORS=1
fi
# TODO: Recommend actual install commands based on distro
if [[ -z "$JQ_PATH" ]]; then
echo -e "${C_ERROR}Error: The JQ utility is required but was not found.${NC}"
FOUND_ERRORS=1
fi
if [[ $FOUND_ERRORS -eq 1 ]]; then
echo ""
echo -e "${C_HIGHLIGHT}See --help for usage instructions.${NC}"
exit 1
fi
# }}}
# {{{ parse arguments
for ((i = 1; i <= $#; i++)); do
case ${!i} in
--help)
echo_help "$SCRIPT_NAME"
exit 0
;;
--jobs)
next_index=$((i + 1))
JOBS=${!next_index}
;;
--limit)
next_index=$((i + 1))
LIMIT=${!next_index}
;;
--owner)
next_index=$((i + 1))
OWNER=${!next_index}
;;
--depth)
next_index=$((i + 1))
DEPTH=${!next_index}
;;
--dir)
next_index=$((i + 1))
DIR=${!next_index}
;;
esac
done
# }}}
# {{{ set default values
if [[ $JOBS -eq -1 ]]; then
JOBS=$DEFAULT_JOBS
fi
if [[ $LIMIT -eq -1 ]]; then
LIMIT=$DEFAULT_LIMIT
fi
if [[ -z "$DIR" ]]; then
DIR="$DEFAULT_DIR"
fi
# }}}
# {{{ validate final argument values
FOUND_ERRORS=0
if [[ "$LIMIT" -lt 1 ]]; then
echo -e "${C_ERROR}Error: --limit must be greater than 1${NC}"
FOUND_ERRORS=1
fi
if [[ "$JOBS" -lt 1 ]]; then
echo -e "${C_ERROR}Error: --jobs must be at least 1${NC}"
FOUND_ERRORS=1
fi
if [[ "$DEPTH" -lt 0 ]]; then
echo -e "${C_ERROR}Error: --depth must be at least 0 if provided${NC}"
FOUND_ERRORS=1
fi
if [[ -z "$OWNER" ]]; then
echo -e "${C_ERROR}Error: --owner not specified${NC}"
FOUND_ERRORS=1
fi
if [[ ! -d "$DIR" ]]; then
echo -e "${C_ERROR}Error: Output directory${NC} ${C_DIR}$DIR${NC} ${C_ERROR}does not exist.${NC}"
FOUND_ERRORS=1
fi
if [[ $FOUND_ERRORS -eq 1 ]]; then
echo ""
echo -e "${C_TEXT_HIGHLIGHT}See --help for usage instructions.${NC}"
exit 1
fi
# }}}
echo -e "${C_TEXT}* Output directory:${NC} ${C_DIR}$DIR${NC}"
echo -e "${C_TEXT}* Cloning at most${NC} ${C_NUMBER}$LIMIT${NC} repositories.${NC}"
echo -e "${C_TEXT}* Will clone${NC} ${C_NUMBER}$JOBS${NC} repositories concurrently.${NC}"
if [[ $DEPTH -eq 0 ]]; then
echo -e "${C_TEXT}* Will clone${NC} ${C_TEXT_HIGHLIGHT}full${NC} commit histories.${NC}"
else
echo -e "${C_TEXT}* Will clone only the last${NC} ${C_NUMBER}$DEPTH${NC} commits.${NC}"
fi
echo ""
echo -e "${C_TEXT}* Fetching repositories for${NC} ${C_TEXT_HIGHLIGHT}$OWNER${NC}${C_TEXT}...${NC}"
GH_REPOS_LIST_JSON="$(gh repo list "$OWNER" --json name --limit "$LIMIT")"
mapfile -t GH_REPOS_LIST < <(echo "$GH_REPOS_LIST_JSON" | jq -r '.[].name')
GH_REPOS_LIST_COUNT="${#GH_REPOS_LIST[@]}"
if [[ -z "$GH_REPOS_LIST_COUNT" ]]; then
echo -e "${C_ERROR}Error: Found no repositories for${NC} ${C_TEXT_HIGHLIGHT}$OWNER${NC}${C_ERROR}.${NC}"
exit 1
fi
NEW_GH_REPOS_LIST=()
SKIP_COUNT=0
for GH_REPO in "${GH_REPOS_LIST[@]}"; do
if [[ ! -d "$DIR/$GH_REPO" ]]; then
NEW_GH_REPOS_LIST+=("$GH_REPO")
else
if [[ $SKIP_COUNT -eq 0 ]]; then
echo ""
fi
((SKIP_COUNT++))
echo -e "${C_TEXT}- Found existing repository${NC} ${C_REPO}$GH_REPO${NC}${C_TEXT}${NC}"
fi
done
EXISTING_REPO_COUNT=$((${#GH_REPOS_LIST[@]} - ${#NEW_GH_REPOS_LIST[@]}))
NEW_REPO_COUNT=${#NEW_GH_REPOS_LIST[@]}
if [[ $NEW_REPO_COUNT -eq 0 ]]; then
echo ""
echo -e "${C_TEXT}* All repositories already present in${NC} ${C_TEXT_HIGHLIGHT}$DIR${NC}${C_TEXT}, nothing to do${NC}"
exit 0
fi
echo ""
echo -e "* Found ${C_NUMBER}$GH_REPOS_LIST_COUNT${NC} repositories."
echo -e "* ${C_NUMBER}$NEW_REPO_COUNT${NC} new repositories will be cloned in ${C_TEXT_HIGHLIGHT}$DIR/${NC}"
echo -e "* ${C_NUMBER}$EXISTING_REPO_COUNT${NC} existing repositories will be skipped."
echo ""
REPOS=("${NEW_GH_REPOS_LIST[@]}")
# export variables to be used in the clone_repos function
export ARG_OWNER=$OWNER
export ARG_DIR=$DIR
export ARG_DEPTH=$DEPTH
export C_ERROR
export C_TEXT
export C_TEXT_HIGHLIGHT
export C_NUMBER
export C_REPO
export C_ARG
export C_DIR
clone_repo() {
REPO="$1"
CWD="$(pwd)"
echo -e "${C_TEXT}- Cloning${NC} ${C_REPO}$ARG_OWNER/$REPO${NC}${C_TEXT}...${NC}"
cd "$ARG_DIR" || exit 1
if [[ "$ARG_DEPTH" -gt 0 ]]; then
git clone --depth "$ARG_DEPTH" git@github.com:"$ARG_OWNER"/"$REPO" "$REPO" > /dev/null 2>&1
cd "$REPO" || exit
git fetch --all > /dev/null 2>&1
cd ..
else
git clone git@github.com:"$ARG_OWNER"/"$REPO" "$REPO" > /dev/null 2>&1
cd "$REPO" || exit
git fetch --all > /dev/null 2>&1
cd ..
fi
cd "$CWD" || exit 1
}
export -f clone_repo
parallel -j "$JOBS" clone_repo ::: "${REPOS[@]}"
# Cleanup variables
unset ARG_OWNER
unset ARG_DIR
unset ARG_DEPTH
unset C_ERROR
unset C_TEXT
unset C_TEXT_HIGHLIGHT
unset C_NUMBER
unset C_REPO
unset C_ARG
unset C_DIR
# {{{ unset color variables
unset C_RED
unset C_BLUE
unset C_CYAN
unset C_BLACK
unset C_GREEN
unset C_WHITE
unset C_PURPLE
unset C_YELLOW
unset C_DARK_GRAY
unset C_LIGHT_RED
unset C_LIGHT_GRAY
unset C_LIGHT_BLUE
unset C_LIGHT_CYAN
unset C_LIGHT_GREEN
unset C_BROWN_ORANGE
unset C_LIGHT_PURPLE
unset C_RED_BOLD
unset C_BLUE_BOLD
unset C_CYAN_BOLD
unset C_BLACK_BOLD
unset C_GREEN_BOLD
unset C_WHITE_BOLD
unset C_PURPLE_BOLD
unset C_YELLOE_BOLD
unset C_DARK_GRAE_BOLD
unset C_LIGHT_REE_BOLD
unset C_LIGHT_GRAY_BOLD
unset C_LIGHT_BLUE_BOLD
unset C_LIGHT_CYAE_BOLD
unset C_LIGHT_GREEE_BOLD
unset C_BROWN_ORANGE_BOLD
unset C_LIGHT_PURPLE_BOLD
unset C_RED_UNDERLINED
unset C_BLUE_UNDERLINED
unset C_CYAN_UNDERLINED
unset C_BLACK_UNDERLINED
unset C_GREEN_UNDERLINED
unset C_WHITE_UNDERLINED
unset C_PURPLE_UNDERLINED
unset C_YELLOW_UNDERLINED
unset C_DARK_GRAY_UNDERLINED
unset C_LIGHT_RED_UNDERLINED
unset C_LIGHT_GRAY_UNDERLINED
unset C_LIGHT_BLUE_UNDERLINED
unset C_LIGHT_CYAN_UNDERLINED
unset C_LIGHT_GREEN_UNDERLINED
unset C_BROWN_ORANGE_UNDERLINED
unset C_LIGHT_PURPLE_UNDERLINED
unset C_RED_BLINKING
unset C_BLUE_BLINKING
unset C_CYAN_BLINKING
unset C_BLACK_BLINKING
unset C_GREEN_BLINKING
unset C_WHITE_BLINKING
unset C_PURPLE_BLINKING
unset C_YELLOW_BLINKING
unset C_DARK_GRAY_BLINKING
unset C_LIGHT_RED_BLINKING
unset C_LIGHT_GRAY_BLINKING
unset C_LIGHT_BLUE_BLINKING
unset C_LIGHT_CYAN_BLINKING
unset C_LIGHT_GREEN_BLINKING
unset C_BROWN_ORANGE_BLINKING
unset C_LIGHT_PURPLE_BLINKING
unset C_ARG
unset C_ERROR
unset C_TEXT
unset C_DIR
unset C_NUMBER
unset C_REPO
unset C_TEXT_HIGHLIGHT
unset NC
# }}}