-
Notifications
You must be signed in to change notification settings - Fork 7
/
git-upgrade-remote-to-ssh.sh
executable file
·513 lines (438 loc) · 17.7 KB
/
git-upgrade-remote-to-ssh.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
#!/usr/bin/env bash
### git-upgrade-remote-to-ssh.sh
# set -x
function git_upgrade_remote_to_ssh_help {
## git_upgrade_remote_to_ssh_help() -- print help
local __file__=$(basename "${0}")
echo "${__file__}: [<type>:ssh] [<repopath:.> [<remotebranch:origin>]]"
echo " Upgrade a git remote.name.url to a {ssh, https, http, git} url"
echo ""
echo " ssh|https|http|git # ssh is the default"
echo ""
echo " -t / --test -- run tests and echo PASS/FAIL"
echo " -h / --help -- print (this) help"
echo ""
echo " # git -C ./ --config --get remote.origin.url # print 'origin' URL in ./"
echo " $ ${__file__} -h # print (this) help"
echo " $ ${__file__} # ./ remote.origin.url to ssh"
echo " $ ${__file__} . # ./" remote.origin.url to ssh
echo " $ ${__file__} . upstream # ./" remote.upstream.url to ssh
echo " $ ${__file__} ./path # ./path" remote.origin.url to ssh
echo " $ ${__file__} ./path upstream # ./path" remote.upstream.url to ssh
echo " $ ${__file__} ssh ./path upstream # ./path remote.upstream.url to ssh"
echo " $ ${__file__} https ./path upstream # ./path remote.upstream.url to https"
echo " $ ${__file__} http ./path upstream # ./path remote.upstream.url to http"
echo " $ ${__file__} git ./path upstream # ./path remote.upstream.url to git"
echo ""
}
function git_upgrade_url_to_ssh {
## git_upgrade_url_to_ssh() -- transform git/http/https URLs to SSH URLs
local _currenturl="${1}"
local _ssh_url___="$(echo "${_currenturl}" \
| sed 's,^\(git\|https\|http\)://\(.*\)$,ssh://git@\2,' - \
| sed 's,^ssh://\(git@\)\(.*\)@\(.*\),ssh://\2@\3,' -)"
echo "${_ssh_url___}"
}
function git_upgrade_url_to_http {
## git_upgrade_url_to_https() -- transform git/http/https URLs to HTTPS URLs
local _currenturl="${1}"
local _https_url_="$(echo "${_currenturl}" \
| sed 's,^\(git\|https\|ssh\)://\(.*\)$,http://\2,' - \
| sed 's,^http://\(git@\)\(.*\)@\(.*\),http://\2@\3,' -)"
echo "${_https_url_}"
}
function git_upgrade_url_to_https {
## git_upgrade_url_to_https() -- transform git/http/https URLs to HTTPS URLs
local _currenturl="${1}"
local _https_url_="$(echo "${_currenturl}" \
| sed 's,^\(git\|http\|ssh\)://\(.*\)$,https://\2,' - \
| sed 's,^https://\(git@\)\(.*\)@\(.*\),https://\2@\3,' -)"
echo "${_https_url_}"
}
function git_upgrade_url_to_git {
## git_upgrade_url_to_https() -- transform git/http/https URLs to HTTPS URLs
local _currenturl="${1}"
local _git_url___="$(echo "${_currenturl}" \
| sed 's,^\(https\|http\|ssh\)://\(.*\)$,git://\2,' - \
| sed 's,^git://\(git@\)\(.*\)@\(.*\),git://\2@\3,' -)"
echo "${_git_url___}"
}
function assertEqual {
## assertEqual() -- assert that "$1" == "$2"
local _arg1="${1}"
local _arg2="${2}"
if [ "${_arg1}" == "${_arg2}" ]; then
return 0
else
echo "ERR: assertEqual <--------------------------->";
return 1
fi
}
function test_git_upgrade_url_to_ssh {
## test_git_upgrade_url_to_ssh() -- test scheme:// to ssh:// transform
assertEqual \
"ssh://git@bitbucket.org/vinay.sajip/sarge#abc" \
"$(git_upgrade_url_to_ssh 'ssh://git@bitbucket.org/vinay.sajip/sarge#abc')"
assertEqual \
"ssh://git@github.org/westurner/dotfiles#abc" \
"$(git_upgrade_url_to_ssh 'ssh://git@github.org/westurner/dotfiles#abc')"
assertEqual \
"ssh://git@github.com/github/gitignore#abc" \
"$(git_upgrade_url_to_ssh 'https://github.com/github/gitignore#abc')"
assertEqual \
"ssh://git@github.com/github/gitignore#abc" \
"$(git_upgrade_url_to_ssh 'http://github.com/github/gitignore#abc')"
assertEqual \
"ssh://git@bitbucket.org/vinay.sajip/sarge#abc" \
"$(git_upgrade_url_to_ssh 'https://bitbucket.org/vinay.sajip/sarge#abc')"
assertEqual \
"ssh://git@bitbucket.org/vinay.sajip/sarge#abc" \
"$(git_upgrade_url_to_ssh 'http://bitbucket.org/vinay.sajip/sarge#abc')"
assertEqual \
"ssh://git@abc" \
"$(git_upgrade_url_to_ssh 'http://abc')"
assertEqual \
"ssh://user2@localhost:442/origin2/#cba" \
"$(git_upgrade_url_to_ssh 'https://user2@localhost:442/origin2/#cba')"
assertEqual \
"ssh://user2@abc" \
"$(git_upgrade_url_to_ssh 'http://user2@abc')"
assertEqual \
"ssh://git@localhost:442/origin/#efg" \
"$(git_upgrade_url_to_ssh 'git://localhost:442/origin/#efg')"
assertEqual \
"/path/to/#123" \
"$(git_upgrade_url_to_ssh '/path/to/#123')"
assertEqual \
'.' \
"$(git_upgrade_url_to_ssh '.')"
}
function test_git_upgrade_url_to_http {
## test_git_upgrade_url_to_http() -- test URL to HTTPS
assertEqual \
"http://git@bitbucket.org/vinay.sajip/sarge#abc" \
"$(git_upgrade_url_to_http 'ssh://git@bitbucket.org/vinay.sajip/sarge#abc')"
assertEqual \
"http://git@github.org/westurner/dotfiles#abc" \
"$(git_upgrade_url_to_http 'ssh://git@github.org/westurner/dotfiles#abc')"
assertEqual \
"http://github.com/github/gitignore#abc" \
"$(git_upgrade_url_to_http 'https://github.com/github/gitignore#abc')"
assertEqual \
"http://github.com/github/gitignore#abc" \
"$(git_upgrade_url_to_http 'http://github.com/github/gitignore#abc')"
assertEqual \
"http://bitbucket.org/vinay.sajip/sarge#abc" \
"$(git_upgrade_url_to_http 'https://bitbucket.org/vinay.sajip/sarge#abc')"
assertEqual \
"http://bitbucket.org/vinay.sajip/sarge#abc" \
"$(git_upgrade_url_to_http 'http://bitbucket.org/vinay.sajip/sarge#abc')"
assertEqual \
"http://abc" \
"$(git_upgrade_url_to_http 'http://abc')"
assertEqual \
"http://user2@localhost:442/origin2/#cba" \
"$(git_upgrade_url_to_http 'https://user2@localhost:442/origin2/#cba')"
assertEqual \
"http://user2@abc" \
"$(git_upgrade_url_to_http 'http://user2@abc')"
assertEqual \
"http://localhost:442/origin/#efg" \
"$(git_upgrade_url_to_http 'git://localhost:442/origin/#efg')"
assertEqual \
"/path/to/#123" \
"$(git_upgrade_url_to_http '/path/to/#123')"
assertEqual \
'.' \
"$(git_upgrade_url_to_http '.')"
}
function test_git_upgrade_url_to_https {
## test_git_upgrade_url_to_https() -- test URL to HTTPS
assertEqual \
"https://git@bitbucket.org/vinay.sajip/sarge#abc" \
"$(git_upgrade_url_to_https 'ssh://git@bitbucket.org/vinay.sajip/sarge#abc')"
assertEqual \
"https://git@github.org/westurner/dotfiles#abc" \
"$(git_upgrade_url_to_https 'ssh://git@github.org/westurner/dotfiles#abc')"
assertEqual \
"https://github.com/github/gitignore#abc" \
"$(git_upgrade_url_to_https 'https://github.com/github/gitignore#abc')"
assertEqual \
"https://github.com/github/gitignore#abc" \
"$(git_upgrade_url_to_https 'http://github.com/github/gitignore#abc')"
assertEqual \
"https://bitbucket.org/vinay.sajip/sarge#abc" \
"$(git_upgrade_url_to_https 'https://bitbucket.org/vinay.sajip/sarge#abc')"
assertEqual \
"https://bitbucket.org/vinay.sajip/sarge#abc" \
"$(git_upgrade_url_to_https 'http://bitbucket.org/vinay.sajip/sarge#abc')"
assertEqual \
"https://abc" \
"$(git_upgrade_url_to_https 'http://abc')"
assertEqual \
"https://user2@localhost:442/origin2/#cba" \
"$(git_upgrade_url_to_https 'https://user2@localhost:442/origin2/#cba')"
assertEqual \
"https://user2@abc" \
"$(git_upgrade_url_to_https 'http://user2@abc')"
assertEqual \
"https://localhost:442/origin/#efg" \
"$(git_upgrade_url_to_https 'git://localhost:442/origin/#efg')"
assertEqual \
"/path/to/#123" \
"$(git_upgrade_url_to_https '/path/to/#123')"
assertEqual \
'.' \
"$(git_upgrade_url_to_https '.')"
}
function test_git_upgrade_url_to_git {
## test_git_upgrade_url_to_git() -- test scheme:// to git://
assertEqual \
"git://git@bitbucket.org/vinay.sajip/sarge#abc" \
"$(git_upgrade_url_to_git 'ssh://git@bitbucket.org/vinay.sajip/sarge#abc')"
assertEqual \
"git://git@github.org/westurner/dotfiles#abc" \
"$(git_upgrade_url_to_git 'ssh://git@github.org/westurner/dotfiles#abc')"
assertEqual \
"git://github.com/github/gitignore#abc" \
"$(git_upgrade_url_to_git 'https://github.com/github/gitignore#abc')"
assertEqual \
"git://github.com/github/gitignore#abc" \
"$(git_upgrade_url_to_git 'http://github.com/github/gitignore#abc')"
assertEqual \
"git://bitbucket.org/vinay.sajip/sarge#abc" \
"$(git_upgrade_url_to_git 'https://bitbucket.org/vinay.sajip/sarge#abc')"
assertEqual \
"git://bitbucket.org/vinay.sajip/sarge#abc" \
"$(git_upgrade_url_to_git 'http://bitbucket.org/vinay.sajip/sarge#abc')"
assertEqual \
"git://abc" \
"$(git_upgrade_url_to_git 'http://abc')"
assertEqual \
"git://user2@localhost:442/origin2/#cba" \
"$(git_upgrade_url_to_git 'https://user2@localhost:442/origin2/#cba')"
assertEqual \
"git://user2@abc" \
"$(git_upgrade_url_to_git 'http://user2@abc')"
assertEqual \
"git://localhost:442/origin/#efg" \
"$(git_upgrade_url_to_git 'git://localhost:442/origin/#efg')"
assertEqual \
"/path/to/#123" \
"$(git_upgrade_url_to_git '/path/to/#123')"
assertEqual \
'.' \
"$(git_upgrade_url_to_git '.')"
}
function git_upgrade_remote_to_ssh {
## git_upgrade_remote_to_ssh() -- upgrade remote.${2:-origin}.url to ssh://
local _path="${1:-"."}"
local _name="${2:-"origin"}"
local _GIT="git -C ${_path}"
local _currenturl=$($_GIT config --get "remote.${_name}.url")
local _newurl=$(git_upgrade_url_to_ssh "${_currenturl}")
test -n "${_newurl}"
echo "# $_GIT remote set-url ${_name} ${_currenturl}" # XXX singleescape
(set -x; $_GIT remote set-url "${_name}" "${_newurl}")
}
function git_upgrade_remote_to_http {
## git_upgrade_remote_to_http() -- upgrade remote.${2:-origin}.url to http:/
local _path="${1:-"."}"
local _name="${2:-"origin"}"
local _GIT="git -C ${_path}"
local _currenturl=$($_GIT config --get "remote.${_name}.url")
local _newurl=$(git_upgrade_url_to_http "${_currenturl}")
test -n "${_newurl}"
echo "# $_GIT remote set-url ${_name} ${_currenturl}" # XXX singleescape
(set -x; $_GIT remote set-url "${_name}" "${_newurl}")
}
function git_upgrade_remote_to_https {
## git_upgrade_remote_to_https() -- upgrade remote.${2:-origin}.url to https:/
local _path="${1:-"."}"
local _name="${2:-"origin"}"
local _GIT="git -C ${_path}"
local _currenturl=$($_GIT config --get "remote.${_name}.url")
local _newurl=$(git_upgrade_url_to_https "${_currenturl}")
test -n "${_newurl}"
echo "# $_GIT remote set-url ${_name} ${_currenturl}" # XXX singleescape
(set -x; $_GIT remote set-url "${_name}" "${_newurl}")
}
function git_upgrade_remote_to_git {
## git_upgrade_remote_to_git() -- upgrade remote.${2:-origin}.url to https://
local _path="${1:-"."}"
local _name="${2:-"origin"}"
local _GIT="git -C ${_path}"
local _currenturl=$($_GIT config --get "remote.${_name}.url")
local _newurl=$(git_upgrade_url_to_git "${_currenturl}")
test -n "${_newurl}"
echo "# $_GIT remote set-url ${_name} ${_currenturl}" # XXX singleescape
(set -x; $_GIT remote set-url "${_name}" "${_newurl}")
}
function test_git_upgrade_remote__main {
## test_git_upgrade_remote__main() -- tests git_upgrade_remote_to_ssh
set -x
set +e
## print git_upgrade_remote_to_ssh_help
git_upgrade_remote_to_ssh_help || return 1
test -n "$(git_upgrade_remote_to_ssh_help)" || return 1
## test git_upgrade_url_to_ssh
test_git_upgrade_url_to_ssh || return 1
## test git_upgrade_url_to_http
test_git_upgrade_url_to_http || return 1
## test git_upgrade_url_to_https
test_git_upgrade_url_to_https || return 1
## test git_upgrade_url_to_git
test_git_upgrade_url_to_git || return 1
#echo "HERE"
#return 1
## test git_upgrade_remote_to_ssh
function _setUp_testrepo {
function __setUp_testrepo {
## -- create a git repository for testing
local _repodir="${1:-"./tst"}"
local _originurl_http="${2:-"http://localhost:440/origin/#abc"}"
local _originurl2_https="${3:-"https://user2@localhost:442/origin2/#cba"}"
local _GIT="git -C ${_repodir}"
mkdir -p "${_repodir}"
$_GIT init -q
local _testfilename="README"
local _testfilepath="${_repodir}/${_testfilename}"
echo "${_testfilename}" > "${_testfilepath}"
$_GIT add "${_testfilename}"
$_GIT commit -q "${_testfilename}" -m "DOC: ${_testfilename}"
test -n "${_originurl_http}" && \
$_GIT remote add origin "${_originurl_http}"
test -n "${_originurl2_https}" && \
$_GIT remote add origin2 "${_originurl2_https}"
$_GIT remote -v
}
(set -x; __setUp_testrepo "${@}")
}
function _tearDown_testrepo {
## -- rmthing
local _repodir="${1:-"./tst"}"
(set -x; rm -rf "${_repodir}")
}
function assert_remote_url {
local _repodir="${1:-"./tst"}"
local _testvalue="${2}"
local _name="${3}"
test -n "${_name}"
local _GIT="git -C ${_repodir}"
local _currenturl=$($_GIT config --get "remote.${_name}.url")
test "${_currenturl}" = "${_testvalue}"
}
local _repodir="./tst"
local _originurl_http="http://localhost:440/origin/#abc"
local _originurl_ssh="ssh://git@localhost:440/origin/#abc"
local _originurl2_https="https://user2@localhost:442/origin2/#cba"
local _originurl2_ssh="ssh://user2@localhost:442/origin2/#cba"
function _test_git_upgrade_remote_to_ssh_this_repo_null {
## if '.' or no args then upgrade the origin remote in '.'
_tearDown_testrepo
_setUp_testrepo
git_upgrade_remote_to_ssh
assert_remote_url "${_repodir}" "${_originurl_ssh}" "origin"
}
# _test_git_upgrade_remote_to_ssh_this_repo_null [destructive]
function _test_git_upgrade_remote_to_ssh_this_repo_dot {
## if '.' or no args then upgrade the origin remote in '.'
_tearDown_testrepo
_setUp_testrepo
git_upgrade_remote_to_ssh .
assert_remote_url "${_repodir}" "${_originurl_ssh}" "origin"
}
# _test_git_upgrade_remote_to_ssh_this_repo_dot [destructive]
function _test_git_upgrade_remote_to_ssh_0 {
_tearDown_testrepo
_setUp_testrepo
git_upgrade_remote_to_ssh "${_repodir}"
assert_remote_url "${_repodir}" "${_originurl_ssh}" origin
git_upgrade_remote_to_ssh "${_repodir}" origin2
assert_remote_url "${_repodir}" "${_originurl2_ssh}" origin2
}
_test_git_upgrade_remote_to_ssh_0 || return
function _test_git_upgrade_remote_to_ssh_1 {
_tearDown_testrepo
_setUp_testrepo
git_upgrade_remote_to_ssh "${_repodir}" origin
assert_remote_url "${_repodir}" "${_originurl_ssh}" origin
git_upgrade_remote_to_ssh "${_repodir}" origin2
assert_remote_url "${_repodir}" "${_originurl2_ssh}" origin2
}
_test_git_upgrade_remote_to_ssh_1 || return
function _test_git_upgrade_remote_to_ssh_2 {
_tearDown_testrepo
_setUp_testrepo
git_upgrade_remote_to_ssh "${_repodir}" origin "remotes/origin"
assert_remote_url "${_repodir}" "${_originurl_ssh}" origin
git_upgrade_remote_to_ssh "${_repodir}" origin2
assert_remote_url "${_repodir}" "${_originurl2_ssh}" origin2
}
_test_git_upgrade_remote_to_ssh_2 || return
function _test_git_upgrade_remote_to_ssh_3 {
_tearDown_testrepo
_setUp_testrepo
git_upgrade_remote_to_ssh "${_repodir}" origin
assert_remote_url "${_repodir}" "${_originurl_ssh}" origin
git_upgrade_remote_to_ssh "${_repodir}" origin2
assert_remote_url "${_repodir}" "${_originurl2_ssh}" origin2
}
_test_git_upgrade_remote_to_ssh_3 || return
_tearDown_testrepo
return
}
function git_upgrade_remote_scheme__main {
## git_upgrade_remote_scheme__main -- main argument handling
local scheme
for arg in ${@}; do
case "${arg}" in
ssh|Ssh|SSH)
shift
scheme="ssh";;
https|Https|HTTPS)
shift
scheme="https";;
http|Http|HTTP)
shift
scheme="http";;
git|Git|GIT)
shift
schema="git";;
-t|--test)
shift # remove -t or --test from ${@}
git_upgrade_remote_to_ssh_help
test_git_upgrade_remote__main "${@}" && \
echo 'PASS' || echo 'FAIL'
return
;;
-h|--help)
git_upgrade_remote_to_ssh_help
return
;;
esac
done
case "${scheme}" in
ssh)
git_upgrade_remote_to_ssh "${@}";
return
;;
https)
git_upgrade_remote_to_https "${@}"
return
;;
http)
git_upgrade_remote_to_http "${@}"
return
;;
git)
git_upgrade_remote_to_git "${@}"
return
;;
esac
return
}
if [ -n "${BASH_SOURCE}" ] && [ "${BASH_SOURCE}" == "${0}" ]; then
git_upgrade_remote_scheme__main "${@}"
exit
fi