-
Notifications
You must be signed in to change notification settings - Fork 169
/
add_mirror.sh
executable file
·531 lines (501 loc) · 15.5 KB
/
add_mirror.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
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
#!/bin/bash
#Created by Sam Gleske
#MIT License
#Created Tue Sep 10 23:01:08 EDT 2013
#USAGE
# ./add_mirror.sh --git --project-name someproject --mirror http://example.com/project.git
#bash option stop on first error
set -e
#Include all user options and dependencies
git_mirrors_dir="${0%/*}"
source ${git_mirrors_dir}/includes.sh
#check if api version is set
[ -z $gitlab_api_version ] && gitlab_api_version=4
#export env vars for python script
export gitlab_user_token_secret gitlab_url gitlab_namespace gitlab_user ssl_verify gitlab_api_version
PROGNAME="${0##*/}"
PROGVERSION="${VERSION}"
#Default script options
svn=false
git=false
bzr=false
hg=false
project_name=""
mirror=""
force=false
no_create_set="${no_create_set:-false}"
no_remote_set="${no_remote_set:-false}"
http_remote="${http_remote:-false}"
#
# ARGUMENT HANDLING
#
usage()
{
cat <<EOF
${PROGNAME} ${PROGVERSION} - MIT License by Sam Gleske
USAGE:
${PROGNAME} TYPE --project NAME --mirror URL [--authors-file FILE]
DESCRIPTION:
This will add a git or SVN repository to be mirrored by GitLab. It
first checks to see if the project exists in gitlab. If it does
not exist then it creates it. It will then clone and check in the
first copy into GitLab. From there you must use the update_mirror.sh
script or git git-mirrors.sh script.
-h,--help Show help
-v,--version Show program version
--authors-file FILE
An authors file to pass to git-svn for mapping
SVN users to git users.
-f,--force Force add project even if it already exists.
Any program errors will automatically continue.
-m,--mirror URL Repository URL to be mirrored.
-n,--no-create URL Set a static remote without attempting to resolve
the remote in GitLab. This allows more generic
mirroring without needing to be specifically for
GitLab.
-l,--no-remote This is a local only mirror. There is no remote
to push to. This is meant for mirroring remote
projects on a developer machine.
-p,--project-name NAME
Set a GitLab project name to NAME.
REPOSITORY TYPES:
At least one repository TYPE is required.
--bzr Mirror a Bazaar repository (must be explicitly set)
--git Mirror a git repository (must be explicitly set)
--hg Mirror a Mercurial repository (must be explicitly set)
--svn Mirror a SVN repository (must be explicitly set)
EOF
}
#Short options are one letter. If an argument follows a short opt then put a colon (:) after it
SHORTOPTS="hvflm:n:p:"
LONGOPTS="help,version,force,git,svn,bzr,hg,mirror:,no-create:,no-remote,project-name:,authors-file:"
ARGS=$(getopt -s bash --options "${SHORTOPTS}" --longoptions "${LONGOPTS}" --name "${PROGNAME}" -- "$@")
eval set -- "$ARGS"
while true; do
case $1 in
-h|--help)
usage
exit 1
;;
-v|--version)
echo "${PROGNAME} ${PROGVERSION}"
exit 1
;;
--git)
git=true
shift
;;
--svn)
svn=true
shift
;;
--bzr)
bzr=true
shift
;;
--hg)
hg=true
shift
;;
-f|--force)
force=true
set +e
shift
;;
-p|--project-name)
project_name="${2}"
shift 2
;;
-m|--mirror)
mirror="${2}"
shift 2
;;
-n|--no-create)
no_create_set=true
no_create="${2}"
shift 2
;;
-l|--no-remote)
no_remote_set=true
shift 1
;;
--authors-file)
authors_file="${2}"
shift 2
;;
--)
shift
break
;;
*)
shift
;;
esac
done
#
# Program functions
#
function preflight() {
STATUS=0
#test for multiple repository types
types=0
selected_types=()
if ${git};then
((types += 1))
selected_types+=('--git')
fi
if ${svn};then
((types += 1))
selected_types+=('--svn')
fi
if ${bzr};then
((types += 1))
selected_types+=('--bzr')
fi
if ${hg};then
((types += 1))
selected_types+=('--hg')
fi
if [ "${types}" -eq "0" ];then
red_echo -n "Must select at least one repository type. e.g. "
yellow_echo "--git"
STATUS=1
elif [ "${types}" -gt "1" ];then
red_echo -n "Multiple repository types not allowed. Found:"
for x in ${selected_types[@]};do
yellow_echo -n " $x"
done
echo ""
STATUS=1
fi
#test required project_name option
if [ -z "${project_name}" ];then
red_echo -n "Missing "
yellow_echo -n "--project-name"
red_echo " option."
STATUS=1
fi
#test required mirror option
if [ -z "${mirror}" ];then
red_echo -n "Missing "
yellow_echo -n "--mirror"
red_echo " option."
STATUS=1
fi
#test no_create_set environment variable (must be bool)
if [ ! "${no_create_set}" = "true" ] && [ ! "${no_create_set}" = "false" ];then
red_echo -n "no_create_set="
yellow_echo -n "${no_create_set}"
red_echo -n " is not a valid option for no_create_set! Must be "
yellow_echo -n "true"
red_echo -n " or "
yellow_echo -n "false"
red_echo "."
STATUS=1
elif ${no_create_set} && [ -z "${no_create}" ];then
yellow_echo -n "--no-create"
red_echo " option must have a git remote to push."
STATUS=1
fi
#test no_remote_set environment variable (must be bool)
if [ ! "${no_remote_set}" = "true" ] && [ ! "${no_remote_set}" = "false" ];then
red_echo -n "no_remote_set="
yellow_echo -n "${no_remote_set}"
red_echo -n " is not a valid option for no_remote_set! Must be "
yellow_echo -n "true"
red_echo -n " or "
yellow_echo -n "false"
red_echo "."
STATUS=1
fi
#test authors_file path for existence
if [ ! -z "${authors_file}" -a ! -f "${authors_file}" ];then
red_echo -n "Specified "
yellow_echo -n "--authors-file"
red_echo " does not exist!"
STATUS=1
fi
#test ssl_verify environment variable (must be bool)
if [ ! "${ssl_verify}" = "true" ] && [ ! "${ssl_verify}" = "false" ];then
red_echo -n "ssl_verify="
yellow_echo -n "${ssl_verify}"
red_echo -n " is not a valid option for ssl_verify! Must be "
yellow_echo -n "true"
red_echo -n " or "
yellow_echo -n "false"
red_echo "."
STATUS=1
fi
#test http_remote environment variable (must be bool)
if [ ! "${http_remote}" = "true" ] && [ ! "${http_remote}" = "false" ];then
red_echo -n "http_remote="
yellow_echo -n "${http_remote}"
red_echo -n " is not a valid option for http_remote! Must be "
yellow_echo -n "true"
red_echo -n " or "
yellow_echo -n "false"
red_echo "."
STATUS=1
fi
#test enable_colors environment variable (must be bool)
if [ ! "${enable_colors}" = "true" ] && [ ! "${enable_colors}" = "false" ];then
red_echo -n "enable_colors="
yellow_echo -n "${enable_colors}"
red_echo -n " is not a valid option for enable_colors! Must be "
yellow_echo -n "true"
red_echo -n " or "
yellow_echo -n "false"
red_echo "."
STATUS=1
fi
#test issues_enabled environment variable (must be bool)
if [ ! "${issues_enabled}" = "true" ] && [ ! "${issues_enabled}" = "false" ];then
red_echo -n "issues_enabled="
yellow_echo -n "${issues_enabled}"
red_echo -n " is not a valid option for issues_enabled! Must be "
yellow_echo -n "true"
red_echo -n " or "
yellow_echo -n "false"
red_echo "."
STATUS=1
fi
#test wall_enabled environment variable (must be bool)
if [ ! "${wall_enabled}" = "true" ] && [ ! "${wall_enabled}" = "false" ];then
red_echo -n "wall_enabled="
yellow_echo -n "${wall_enabled}"
red_echo -n " is not a valid option for wall_enabled! Must be "
yellow_echo -n "true"
red_echo -n " or "
yellow_echo -n "false"
red_echo "."
STATUS=1
fi
#test wiki_enabled environment variable (must be bool)
if [ ! "${wiki_enabled}" = "true" ] && [ ! "${wiki_enabled}" = "false" ];then
red_echo -n "wiki_enabled="
yellow_echo -n "${wiki_enabled}"
red_echo -n " is not a valid option for wiki_enabled! Must be "
yellow_echo -n "true"
red_echo -n " or "
yellow_echo -n "false"
red_echo "."
STATUS=1
fi
#test snippets_enabled environment variable (must be bool)
if [ ! "${snippets_enabled}" = "true" ] && [ ! "${snippets_enabled}" = "false" ];then
red_echo -n "snippets_enabled="
yellow_echo -n "${snippets_enabled}"
red_echo -n " is not a valid option for snippets_enabled! Must be "
yellow_echo -n "true"
red_echo -n " or "
yellow_echo -n "false"
red_echo "."
STATUS=1
fi
#test public environment variable (must be bool)
if [ ! "${public}" = "true" ] && [ ! "${public}" = "false" ];then
red_echo -n "public="
yellow_echo -n "${public}"
red_echo -n " is not a valid option for public! Must be "
yellow_echo -n "true"
red_echo -n " or "
yellow_echo -n "false"
red_echo "."
STATUS=1
fi
#test merge_requests_enabled environment variable (must be bool)
if [ ! "${merge_requests_enabled}" = "true" ] && [ ! "${merge_requests_enabled}" = "false" ];then
red_echo -n "merge_requests_enabled="
yellow_echo -n "${merge_requests_enabled}"
red_echo -n " is not a valid option for merge_requests_enabled! Must be "
yellow_echo -n "true"
red_echo -n " or "
yellow_echo -n "false"
red_echo "."
STATUS=1
fi
return ${STATUS}
}
#
# Main execution
#
#Run a preflight check on options for compatibility.
if ! preflight 1>&2;then
echo "Command aborted due to previous errors." 1>&2
exit 1
fi
#Check for namespace directory existence
if [ ! -e "${repo_dir}/${gitlab_namespace}" ];then
mkdir -p "${repo_dir}/${gitlab_namespace}"
elif [ ! -d "${repo_dir}/${gitlab_namespace}" ];then
red_echo "Error: \"${repo_dir}/${gitlab_namespace}\" exists but is not a directory." 1>&2
exit 1
elif [ -d "${repo_dir}/${gitlab_namespace}/${project_name}" ] && ! ${force};then
red_echo "Error: \"${repo_dir}/${gitlab_namespace}/${project_name}\" exists already. Aborting command." 1>&2
exit 1
fi
#Resolve the $authors_file path because of changing working directories
if [ ! -z "${authors_file}" ];then
if ! echo "${authors_file}" | grep '^/' &> /dev/null;then
authors_file="${PWD}/${authors_file}"
authors_file="$(echo ${authors_file} | sed 's#/./#/#g')"
fi
fi
cd "${git_mirrors_dir}"
#Set up project creation options based on config.sh to be passed to create manage_gitlab_project.py
CREATE_OPTS=""
if ${issues_enabled};then
CREATE_OPTS="--issues ${CREATE_OPTS}"
fi
if ${wall_enabled};then
CREATE_OPTS="--wall ${CREATE_OPTS}"
fi
if ${merge_requests_enabled};then
CREATE_OPTS="--merge ${CREATE_OPTS}"
fi
if ${wiki_enabled};then
CREATE_OPTS="--wiki ${CREATE_OPTS}"
fi
if ${snippets_enabled};then
CREATE_OPTS="--snippets ${CREATE_OPTS}"
fi
if ${public};then
CREATE_OPTS="--public ${CREATE_OPTS}"
fi
if ${http_remote};then
CREATE_OPTS="--http ${CREATE_OPTS}"
fi
#Get the remote gitlab url for the specified project.
#If the project doesn't already exist in gitlab then create it.
if ! ${no_remote_set} && [ -z "${no_create}" ];then
green_echo "Resolving gitlab remote." 1>&2
if python lib/manage_gitlab_project.py --create --desc "Mirror of ${mirror}" ${CREATE_OPTS} "${project_name}" 1> /dev/null;then
gitlab_remote=$(python lib/manage_gitlab_project.py --create --desc "Mirror of ${mirror}" ${CREATE_OPTS} "${project_name}")
else
red_echo "There was an unknown issue with manage_gitlab_project.py" 1>&2
exit 1
fi
if [ -z "${gitlab_remote}" ];then
red_echo "There was an unknown issue with manage_gitlab_project.py" 1>&2
exit 1
fi
else
if ! ${no_remote_set};then
green_echo -n "Using remote: " 1>&2
echo "${no_create}" 1>&2
gitlab_remote="${no_create}"
else
echo "Local only mirror." 1>&2
fi
fi
if ${git};then
#create a mirror
green_echo "Creating mirror from ${mirror}" 1>&2
cd "${repo_dir}/${gitlab_namespace}"
git clone --mirror "${mirror}" "${project_name}"
cd "${project_name}"
#add the gitlab remote
if ! ${no_remote_set};then
green_echo "Adding gitlab remote to project." 1>&2
git remote add gitlab "${gitlab_remote}"
git config --add remote.gitlab.push '+refs/heads/*:refs/heads/*'
git config --add remote.gitlab.push '+refs/tags/*:refs/tags/*'
git config remote.gitlab.mirror true
#Check the initial repository into gitlab
green_echo "Checking the mirror into gitlab." 1>&2
git fetch
if ${http_remote};then
git config credential.helper store
fi
git push gitlab
if [ ! -z "${no_create}" ];then
git config gitlabmirrors.nocreate true
fi
else
git config gitlabmirrors.noremote true
fi
green_echo "All done!" 1>&2
elif ${svn};then
#create a mirror
green_echo "Creating mirror from ${mirror}" 1>&2
cd "${repo_dir}/${gitlab_namespace}"
if [ ! -z "${authors_file}" ];then
git svn clone "${mirror}" "${project_name}" ${git_svn_additional_options} --authors-file="${authors_file}"
else
git svn clone "${mirror}" "${project_name}" ${git_svn_additional_options}
fi
#add the gitlab remote
if ! ${no_remote_set};then
green_echo "Adding gitlab remote to project." 1>&2
cd "${project_name}"
git remote add gitlab "${gitlab_remote}"
git config --add remote.gitlab.push '+refs/heads/*:refs/heads/*'
git config --add remote.gitlab.push '+refs/remotes/tags/*:refs/tags/*'
git config remote.gitlab.mirror true
#Check the initial repository into gitlab
green_echo "Checking the mirror into gitlab." 1>&2
git reset --hard
git svn fetch
cd .git
git config --bool core.bare true
if ${http_remote};then
git config credential.helper store
fi
git push gitlab
git config --bool core.bare false
else
git config gitlabmirrors.noremote true
fi
green_echo "All done!" 1>&2
elif ${bzr};then
#create a mirror
green_echo "Creating mirror from ${mirror}" 1>&2
cd "${repo_dir}/${gitlab_namespace}"
git clone --mirror bzr::"${mirror}" "${project_name}"
# cleaning repo
cd "${project_name}"
git gc --aggressive
#add the gitlab remote
if ! ${no_remote_set};then
git remote add gitlab "${gitlab_remote}"
git config --add remote.gitlab.push '+refs/heads/*:refs/heads/*'
git config --add remote.gitlab.push '+refs/tags/*:refs/tags/*'
git config remote.gitlab.mirror true
#Check the initial repository into gitlab
green_echo "Checking the mirror into gitlab." 1>&2
if ${http_remote};then
git config credential.helper store
fi
git push gitlab
green_echo "All done!" 1>&2
else
git config gitlabmirrors.noremote true
fi
elif ${hg};then
#create a mirror
green_echo "Creating mirror from ${mirror}" 1>&2
cd "${repo_dir}/${gitlab_namespace}"
git clone --mirror hg::"${mirror}" "${project_name}"
# cleaning repo
cd "${project_name}"
git gc --aggressive
#add the gitlab remote
if ! ${no_remote_set};then
git remote add gitlab "${gitlab_remote}"
git config --add remote.gitlab.push '+refs/heads/*:refs/heads/*'
git config --add remote.gitlab.push '+refs/tags/*:refs/tags/*'
git config remote.gitlab.mirror true
#Check the initial repository into gitlab
green_echo "Checking the mirror into gitlab." 1>&2
if ${http_remote};then
git config credential.helper store
fi
git push gitlab
green_echo "All done!" 1>&2
else
git config gitlabmirrors.noremote true
fi
else
red_echo "Something has gone very wrong. You should never see this message." 1>&2
exit 1
fi