-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitssh-remotes.sh
More file actions
740 lines (620 loc) · 21.5 KB
/
Copy pathgitssh-remotes.sh
File metadata and controls
740 lines (620 loc) · 21.5 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
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
#!/bin/sh
#================================================================#
# GitSSH REMOTE MANAGEMENT MODULE
# Functions for managing Git remotes and SSH conversion
#================================================================#
#================================================================#
# REMOTE DETECTION AND VALIDATION
#================================================================#
# Check if URL is a GitHub HTTPS URL
_is_github_https_url() {
url="$1"
case "$url" in
https://github.com/*) return 0 ;;
*) return 1 ;;
esac
}
# Check if URL is a GitHub SSH URL
_is_github_ssh_url() {
url="$1"
case "$url" in
git@github-*:*) return 0 ;;
*) return 1 ;;
esac
}
# Extract repository path from GitHub URL
_extract_repo_path() {
url="$1"
case "$url" in
https://github.com/*)
printf "%s" "$url" | sed 's|https://github\.com/||' | sed 's|\.git$||'
;;
printf "%s" "$url" | sed 's|git@github\.com:||' | sed 's|\.git$||'
;;
git@github-*:*)
printf "%s" "$url" | sed 's|git@github-[^:]*:||' | sed 's|\.git$||'
;;
*)
printf ""
;;
esac
}
# Get SSH host from SSH URL
_extract_ssh_host() {
url="$1"
case "$url" in
git@*:*)
printf "%s" "$url" | sed 's|git@\([^:]*\):.*|\1|'
;;
*)
printf ""
;;
esac
}
#================================================================#
# REMOTE CONVERSION FUNCTIONS
#================================================================#
# Convert HTTPS remote to SSH
ssh_remote() {
if ! _is_git_repo; then
_print_error "Not in a git repository"
return 1
fi
printf "Current git remotes:\n"
git remote -v | sed 's/^/ /'
printf "\n"
origin_url=$(_get_remote_url)
if [ -z "$origin_url" ]; then
_print_error "No 'origin' remote found"
return 1
fi
# Check if already using SSH with custom host
if _is_github_ssh_url "$origin_url"; then
ssh_host=$(_extract_ssh_host "$origin_url")
case "$ssh_host" in
github-*)
_print_info "Already using SSH with custom host: $origin_url"
return 0
;;
github.com)
_print_info "Using default GitHub SSH, consider converting to custom host"
;;
esac
fi
# Validate GitHub HTTPS URL
if ! _is_github_https_url "$origin_url"; then
_print_error "Only GitHub HTTPS URLs are supported"
printf "Current URL: %s\n" "$origin_url"
return 1
fi
# Extract repository info
repo_path=$(_extract_repo_path "$origin_url")
username=$(printf "%s" "$repo_path" | cut -d'/' -f1)
repo_name=$(printf "%s" "$repo_path" | cut -d'/' -f2)
printf "Detected GitHub repository: %s/%s\n" "$username" "$repo_name"
# Get available SSH hosts
ssh_hosts=$(_get_ssh_hosts)
if [ -z "$ssh_hosts" ]; then
_print_error "No GitHub SSH hosts found in ~/.ssh/config"
printf "You need to set up SSH hosts first. Example ~/.ssh/config entry:\n"
printf "\n"
printf "Host github-%s\n" "$username"
printf " HostName github.com\n"
printf " User git\n"
printf " IdentityFile ~/.ssh/id_rsa_%s\n" "$username"
printf "\n"
return 1
fi
# Show SSH host options
_show_ssh_host_options "$ssh_hosts" "$username" "$repo_path"
}
# Show available SSH hosts with recommendations
_show_ssh_host_options() {
ssh_hosts="$1"
repo_username="$2"
repo_path="$3"
printf "Available SSH hosts:\n"
i=1
recommended_choice=""
for host in $ssh_hosts; do
host_user=$(printf "%s" "$host" | sed 's/github-//')
# Check if this host matches the repository owner
if [ "$host_user" = "$repo_username" ]; then
printf " %d. %s (matches repository owner - RECOMMENDED)\n" "$i" "$host"
recommended_choice="$i"
else
printf " %d. %s\n" "$i" "$host"
fi
i=$((i + 1))
done
printf "\n"
# Get user selection
host_count=$(printf "%s" "$ssh_hosts" | wc -w)
# Auto-select if there's a clear recommendation
if [ -n "$recommended_choice" ]; then
printf "Auto-select recommended host? (Y/n): "
read -r auto_select
case "$auto_select" in
[Nn]*) choice="" ;;
*) choice="$recommended_choice" ;;
esac
fi
# Get manual selection if needed
while [ -z "$choice" ]; do
printf "Select SSH host (1-%d): " "$host_count"
read -r choice
if [ "$choice" -ge 1 ] && [ "$choice" -le "$host_count" ] 2>/dev/null; then
break
fi
printf "Invalid choice. Please enter a number between 1 and %d\n" "$host_count"
choice=""
done
selected_host=$(printf "%s" "$ssh_hosts" | sed -n "${choice}p")
_convert_remote_to_ssh "$selected_host" "$repo_path"
}
# Convert remote URL to SSH format
_convert_remote_to_ssh() {
ssh_host="$1"
repo_path="$2"
origin_url=$(_get_remote_url)
new_url="git@${ssh_host}:${repo_path}.git"
printf "Converting remote URL:\n"
printf " From: %s\n" "$origin_url"
printf " To: %s\n" "$new_url"
printf "\n"
printf "Proceed with this change? (y/N): "
read -r confirm
case "$confirm" in
[Yy]*) ;;
*) printf "Cancelled\n"; return 0 ;;
esac
if _update_remote_url "origin" "$new_url"; then
_print_success "Successfully updated remote URL"
printf "Updated remotes:\n"
git remote -v | sed 's/^/ /'
printf "Testing SSH connection...\n"
if _test_ssh_connection "$ssh_host" 5; then
_print_success "SSH connection successful!"
_print_info "You can now use 'git push' without entering a password"
else
_print_warning "SSH connection test failed. Please check your SSH setup."
fi
else
_print_error "Failed to update remote URL"
return 1
fi
}
#================================================================#
# REMOTE MANAGEMENT UTILITIES
#================================================================#
# Update remote URL
_update_remote_url() {
remote_name="$1"
new_url="$2"
git remote set-url "$remote_name" "$new_url"
}
# Get all remotes information
_get_all_remotes() {
git remote -v 2>/dev/null
}
# Check if remote exists
_remote_exists() {
remote_name="$1"
git remote get-url "$remote_name" >/dev/null 2>&1
}
# Add new remote
_add_remote() {
remote_name="$1"
remote_url="$2"
git remote add "$remote_name" "$remote_url"
}
# Remove remote
_remove_remote() {
remote_name="$1"
git remote remove "$remote_name"
}
#================================================================#
# ADVANCED REMOTE OPERATIONS
#================================================================#
# Convert all HTTPS remotes to SSH
ssh_remote_all() {
if ! _is_git_repo; then
_print_error "Not in a git repository"
return 1
fi
printf "Converting all HTTPS remotes to SSH...\n"
printf "=====================================\n"
# Get all remotes
remotes_output=$(_get_all_remotes)
if [ -z "$remotes_output" ]; then
_print_info "No remotes configured"
return 0
fi
# Find HTTPS GitHub remotes
https_remotes=""
while IFS= read -r line; do
remote_name=$(printf "%s" "$line" | awk '{print $1}')
remote_url=$(printf "%s" "$line" | awk '{print $2}')
remote_type=$(printf "%s" "$line" | awk '{print $3}')
# Only process fetch URLs and GitHub HTTPS
if [ "$remote_type" = "(fetch)" ] && _is_github_https_url "$remote_url"; then
https_remotes="$https_remotes $remote_name:$remote_url"
fi
done << EOF
$remotes_output
EOF
if [ -z "$https_remotes" ]; then
_print_info "No GitHub HTTPS remotes found"
return 0
fi
# Process each HTTPS remote
converted_count=0
for remote_info in $https_remotes; do
remote_name=$(printf "%s" "$remote_info" | cut -d':' -f1)
remote_url=$(printf "%s" "$remote_info" | cut -d':' -f2-)
printf "\nProcessing remote '%s':\n" "$remote_name"
printf " URL: %s\n" "$remote_url"
repo_path=$(_extract_repo_path "$remote_url")
username=$(printf "%s" "$repo_path" | cut -d'/' -f1)
# Find matching SSH host
ssh_host=$(_find_matching_ssh_host "$username")
if [ -n "$ssh_host" ]; then
new_url="git@${ssh_host}:${repo_path}.git"
printf " Converting to: %s\n" "$new_url"
if _update_remote_url "$remote_name" "$new_url"; then
_print_success " Converted $remote_name"
converted_count=$((converted_count + 1))
else
_print_error " Failed to convert $remote_name"
fi
else
_print_warning " No matching SSH host found for user '$username'"
printf " Skipping remote '%s'\n" "$remote_name"
fi
done
printf "\nConversion Summary:\n"
printf " Converted %d remote(s)\n" "$converted_count"
if [ "$converted_count" -gt 0 ]; then
printf "\nUpdated remotes:\n"
git remote -v | sed 's/^/ /'
fi
}
# Check remote URL format and suggest improvements
check_remote() {
if ! _is_git_repo; then
_print_error "Not in a git repository"
return 1
fi
printf "Remote Analysis:\n"
printf "================\n"
remotes_output=$(_get_all_remotes)
if [ -z "$remotes_output" ]; then
_print_info "No remotes configured"
return 0
fi
# Analyze each remote
while IFS= read -r line; do
remote_name=$(printf "%s" "$line" | awk '{print $1}')
remote_url=$(printf "%s" "$line" | awk '{print $2}')
remote_type=$(printf "%s" "$line" | awk '{print $3}')
# Only analyze fetch URLs
[ "$remote_type" != "(fetch)" ] && continue
printf "\nRemote: %s\n" "$remote_name"
printf " URL: %s\n" "$remote_url"
if _is_github_https_url "$remote_url"; then
printf " Type: GitHub HTTPS\n"
_print_warning " Recommendation: Convert to SSH for passwordless access"
printf " Use: gitssh remote convert\n"
# Check if user has matching SSH host
repo_path=$(_extract_repo_path "$remote_url")
username=$(printf "%s" "$repo_path" | cut -d'/' -f1)
ssh_host=$(_find_matching_ssh_host "$username")
if [ -n "$ssh_host" ]; then
_print_success " Matching SSH host available: $ssh_host"
else
_print_info " No matching SSH host found for user '$username'"
fi
elif _is_github_ssh_url "$remote_url"; then
printf " Type: GitHub SSH\n"
ssh_host=$(_extract_ssh_host "$remote_url")
# Test connection
if _test_ssh_connection "$ssh_host" 3; then
_print_success " Status: SSH connection working"
else
_print_warning " Status: SSH connection failed"
fi
else
printf " Type: Other/Unknown\n"
_print_info " Status: Not a GitHub repository"
fi
done << EOF
$remotes_output
EOF
}
# Add SSH remote for different user
add_ssh_remote() {
if ! _is_git_repo; then
_print_error "Not in a git repository"
return 1
fi
# Get repository info
origin_url=$(_get_remote_url)
if [ -z "$origin_url" ]; then
_print_error "No origin remote found"
return 1
fi
repo_path=$(_extract_repo_path "$origin_url")
if [ -z "$repo_path" ]; then
_print_error "Cannot extract repository path from origin URL"
return 1
fi
repo_name=$(printf "%s" "$repo_path" | cut -d'/' -f2)
printf "Add SSH remote for different user\n"
printf "=================================\n"
printf "Repository: %s\n" "$repo_name"
printf "Current origin: %s\n" "$origin_url"
printf "\n"
# Show available SSH hosts
ssh_hosts=$(_get_ssh_hosts)
if [ -z "$ssh_hosts" ]; then
_print_error "No SSH hosts configured"
return 1
fi
printf "Available SSH hosts:\n"
i=1
for host in $ssh_hosts; do
host_user=$(printf "%s" "$host" | sed 's/github-//')
printf " %d. %s (user: %s)\n" "$i" "$host" "$host_user"
i=$((i + 1))
done
printf "\n"
# Get selection
host_count=$(printf "%s" "$ssh_hosts" | wc -w)
while true; do
printf "Select SSH host (1-%d): " "$host_count"
read -r choice
if [ "$choice" -ge 1 ] && [ "$choice" -le "$host_count" ] 2>/dev/null; then
break
fi
printf "Invalid choice\n"
done
selected_host=$(printf "%s" "$ssh_hosts" | sed -n "${choice}p")
selected_user=$(printf "%s" "$selected_host" | sed 's/github-//')
# Get remote name
printf "Remote name [%s]: " "$selected_user"
read -r remote_name
remote_name=${remote_name:-$selected_user}
# Check if remote already exists
if _remote_exists "$remote_name"; then
_print_warning "Remote '$remote_name' already exists"
printf "Overwrite? (y/N): "
read -r overwrite
case "$overwrite" in
[Yy]*) ;;
*) printf "Cancelled\n"; return 0 ;;
esac
fi
# Create new remote URL with different user
new_repo_path="${selected_user}/${repo_name}"
new_url="git@${selected_host}:${new_repo_path}.git"
printf "Adding remote:\n"
printf " Name: %s\n" "$remote_name"
printf " URL: %s\n" "$new_url"
printf "\n"
printf "Add this remote? (Y/n): "
read -r confirm
case "$confirm" in
[Nn]*) printf "Cancelled\n"; return 0 ;;
esac
if _remote_exists "$remote_name"; then
if _update_remote_url "$remote_name" "$new_url"; then
_print_success "Updated remote '$remote_name'"
else
_print_error "Failed to update remote"
return 1
fi
else
if _add_remote "$remote_name" "$new_url"; then
_print_success "Added remote '$remote_name'"
else
_print_error "Failed to add remote"
return 1
fi
fi
printf "Testing SSH connection...\n"
if _test_ssh_connection "$selected_host" 5; then
_print_success "SSH connection successful!"
else
_print_warning "SSH connection failed - check your SSH setup"
fi
printf "\nCurrent remotes:\n"
git remote -v | sed 's/^/ /'
}
#================================================================#
# PRIVATE HELPER FUNCTIONS
#================================================================#
# Find SSH host that matches username
_find_matching_ssh_host() {
target_username="$1"
ssh_hosts=$(_get_ssh_hosts)
for host in $ssh_hosts; do
host_user=$(printf "%s" "$host" | sed 's/github-//')
if [ "$host_user" = "$target_username" ]; then
printf "%s" "$host"
return 0
fi
done
return 1
}
# Get SSH host suggestions based on configured users
_get_ssh_host_suggestions() {
repo_username="$1"
# First, try to find exact match
ssh_host=$(_find_matching_ssh_host "$repo_username")
if [ -n "$ssh_host" ]; then
printf "exact:%s" "$ssh_host"
return 0
fi
# If no exact match, suggest creating one
if _check_dependencies 2>/dev/null && _user_exists "$repo_username"; then
user_details=$(_get_user_details "$repo_username")
suggested_host=$(printf "%s" "$user_details" | jq -r '.ssh_host')
printf "suggested:%s" "$suggested_host"
return 0
fi
printf "none:"
return 1
}
# Validate SSH host format
_validate_ssh_host() {
ssh_host="$1"
case "$ssh_host" in
github-*) return 0 ;;
github.com) return 0 ;;
*) return 1 ;;
esac
}
# Get remote protocol type
_get_remote_protocol() {
remote_url="$1"
case "$remote_url" in
https://*) printf "https" ;;
git@*:*) printf "ssh" ;;
*) printf "unknown" ;;
esac
}
# Show remote recommendations
show_remote_recommendations() {
if ! _is_git_repo; then
_print_error "Not in a git repository"
return 1
fi
printf "Remote Recommendations:\n"
printf "=======================\n"
origin_url=$(_get_remote_url)
if [ -z "$origin_url" ]; then
_print_info "No origin remote configured"
return 0
fi
protocol=$(_get_remote_protocol "$origin_url")
printf "Current origin: %s (%s)\n" "$origin_url" "$protocol"
case "$protocol" in
https)
if _is_github_https_url "$origin_url"; then
_print_info "Recommendation: Convert to SSH for passwordless access"
printf " Command: gitssh remote convert\n"
repo_path=$(_extract_repo_path "$origin_url")
username=$(printf "%s" "$repo_path" | cut -d'/' -f1)
suggestions=$(_get_ssh_host_suggestions "$username")
case "$suggestions" in
exact:*)
host=$(printf "%s" "$suggestions" | cut -d':' -f2)
_print_success " Matching SSH host available: $host"
;;
suggested:*)
host=$(printf "%s" "$suggestions" | cut -d':' -f2)
_print_info " Suggested SSH host: $host"
;;
none:*)
_print_warning " No SSH host configured for user '$username'"
printf " Consider adding SSH host configuration\n"
;;
esac
else
_print_info "Non-GitHub HTTPS remote - no specific recommendations"
fi
;;
ssh)
if _is_github_ssh_url "$origin_url"; then
ssh_host=$(_extract_ssh_host "$origin_url")
printf "SSH remote configured: %s\n" "$ssh_host"
if _test_ssh_connection "$ssh_host" 3; then
_print_success "SSH connection working properly"
else
_print_warning "SSH connection not working"
printf " Check SSH key configuration for %s\n" "$ssh_host"
fi
else
_print_info "Non-GitHub SSH remote"
fi
;;
*)
_print_warning "Unknown remote protocol"
;;
esac
printf "\nAll remotes:\n"
git remote -v | sed 's/^/ /'
}
#================================================================#
# BULK REMOTE OPERATIONS
#================================================================#
# List all remotes with detailed information
list_remotes() {
if ! _is_git_repo; then
_print_error "Not in a git repository"
return 1
fi
printf "Repository Remotes\n"
printf "==================\n"
remotes_output=$(_get_all_remotes)
if [ -z "$remotes_output" ]; then
_print_info "No remotes configured"
return 0
fi
# Group by remote name
current_remote=""
while IFS= read -r line; do
remote_name=$(printf "%s" "$line" | awk '{print $1}')
remote_url=$(printf "%s" "$line" | awk '{print $2}')
remote_type=$(printf "%s" "$line" | awk '{print $3}')
if [ "$remote_name" != "$current_remote" ]; then
[ -n "$current_remote" ] && printf "\n"
printf "Remote: %s\n" "$remote_name"
current_remote="$remote_name"
fi
protocol=$(_get_remote_protocol "$remote_url")
printf " %s: %s (%s)\n" "$remote_type" "$remote_url" "$protocol"
# Additional info for GitHub remotes
if _is_github_https_url "$remote_url" || _is_github_ssh_url "$remote_url"; then
repo_path=$(_extract_repo_path "$remote_url")
username=$(printf "%s" "$repo_path" | cut -d'/' -f1)
printf " GitHub user: %s\n" "$username"
if [ "$protocol" = "ssh" ]; then
ssh_host=$(_extract_ssh_host "$remote_url")
if _test_ssh_connection "$ssh_host" 2; then
_print_color green " SSH: Connected"
printf "\n"
else
_print_color red " SSH: Failed"
printf "\n"
fi
fi
fi
done << EOF
$remotes_output
EOF
}
#================================================================#
# gitssh-remotes.sh refactoring
#================================================================#
# Replacing ssh_remote() with:
remote_convert() {
ssh_remote "$@" # Keeping existing implementation
}
# Replacing add_ssh_remote() with:
remote_add() {
add_ssh_remote "$@" # Keeping existing implementation
}
# Replacing check_remote() with:
remote_check() {
check_remote "$@" # Keeping existing implementation
}
# Add new function:
remote_list() {
list_remotes "$@" # Keeping existing implementation
}
remote_recommendations() {
show_remote_recommendations "$@" # Keeping existing implementation
}