-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitssh-sessions.sh
More file actions
678 lines (561 loc) · 21.3 KB
/
Copy pathgitssh-sessions.sh
File metadata and controls
678 lines (561 loc) · 21.3 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
#!/bin/sh
#================================================================#
# GitSSH SESSION MANAGEMENT MODULE
# Functions for managing user sessions and persistent storage
#================================================================#
#================================================================#
# PERSISTENT STORAGE FUNCTIONS
#================================================================#
# Get persistent user configuration for repository
_get_persistent_user() {
repo_id="$1"
_check_dependencies || return 1
[ -f "$GIT_SSH_CONFIG_FILE" ] && jq -r --arg repo "$repo_id" '.[$repo] // empty' "$GIT_SSH_CONFIG_FILE" 2>/dev/null
}
# Save persistent user configuration for repository
_save_persistent_user() {
repo_id="$1"
user_info="$2"
_check_dependencies || return 1
temp_file=$(_create_temp_file)
if jq --arg repo "$repo_id" --arg user "$user_info" '.[$repo] = $user' "$GIT_SSH_CONFIG_FILE" > "$temp_file"; then
_safe_file_replace "$temp_file" "$GIT_SSH_CONFIG_FILE"
else
rm -f "$temp_file"
return 1
fi
}
# Remove persistent user configuration for repository
_remove_persistent_user() {
repo_id="$1"
_check_dependencies || return 1
temp_file=$(_create_temp_file)
if jq --arg repo "$repo_id" 'del(.[$repo])' "$GIT_SSH_CONFIG_FILE" > "$temp_file"; then
_safe_file_replace "$temp_file" "$GIT_SSH_CONFIG_FILE"
else
rm -f "$temp_file"
return 1
fi
}
#================================================================#
# SESSION STORAGE FUNCTIONS
#================================================================#
# Get session user for repository
_get_session_user() {
repo_id="$1"
[ -f "$GIT_SESSION_TEMP" ] && grep "^$repo_id:" "$GIT_SESSION_TEMP" | cut -d: -f2- 2>/dev/null
}
# Save session user for repository
_save_session_user() {
repo_id="$1"
user_info="$2"
# Remove existing entry for this repo
if [ -f "$GIT_SESSION_TEMP" ]; then
grep -v "^$repo_id:" "$GIT_SESSION_TEMP" > "$GIT_SESSION_TEMP.tmp" 2>/dev/null || true
mv "$GIT_SESSION_TEMP.tmp" "$GIT_SESSION_TEMP" 2>/dev/null || true
fi
# Add new entry
printf "%s:%s\n" "$repo_id" "$user_info" >> "$GIT_SESSION_TEMP"
}
# Remove session user for repository
_remove_session_user() {
repo_id="$1"
if [ -f "$GIT_SESSION_TEMP" ]; then
grep -v "^$repo_id:" "$GIT_SESSION_TEMP" > "$GIT_SESSION_TEMP.tmp" 2>/dev/null || true
mv "$GIT_SESSION_TEMP.tmp" "$GIT_SESSION_TEMP" 2>/dev/null || true
fi
}
# Check if user is set in current session
_has_session_user() {
repo_id="$1"
session_user=$(_get_session_user "$repo_id")
[ -n "$session_user" ]
}
#================================================================#
# USER SESSION MANAGEMENT
#================================================================#
# Set git user/session for current repository
session_set() {
# Handle CLI arguments
target_user=""
while [ $# -gt 0 ]; do
case "$1" in
--user)
target_user="$2"
shift 2
;;
--help|-h)
printf "Usage: gitssh session set [--user <username>]\n"
return 0
;;
*)
break
;;
esac
done
repo_id=$(_get_repo_id)
if [ -z "$repo_id" ]; then
_print_error "Not in a git repository"
return 1
fi
if [ -n "$target_user" ]; then
# Non-interactive mode
if ! _user_exists "$target_user"; then
_print_error "User '$target_user' not found"
return 1
fi
_apply_configured_user "$repo_id" "$target_user"
else
# Interactive mode (keep existing session_set logic)
_interactive_user_selection "$repo_id"
fi
}
# Auto-prompt when entering repositories
_git_auto_prompt() {
repo_id=$(_get_repo_id)
[ -z "$repo_id" ] && return 0
# Skip if already configured in session
if _has_session_user "$repo_id"; then
session_user=$(_get_session_user "$repo_id")
printf "Session user for %s: %s\n" "$(basename "$repo_id")" "$session_user"
return 0
fi
# Check for saved configuration
persistent_user=$(_get_persistent_user "$repo_id")
if [ -n "$persistent_user" ] && [ "$persistent_user" != "null" ] && [ "$persistent_user" != "empty" ]; then
printf "Found saved config for %s: %s\n" "$(basename "$repo_id")" "$persistent_user"
saved_name=$(printf "%s" "$persistent_user" | sed 's/ <.*//')
saved_email=$(printf "%s" "$persistent_user" | sed 's/.*<\(.*\)>.*/\1/')
_set_git_config "user.name" "$saved_name" "local"
_set_git_config "user.email" "$saved_email" "local"
_save_session_user "$repo_id" "$persistent_user"
printf "Use saved config? (Y/n): "
read -r use_saved
case "$use_saved" in
[Nn]*)
session_set
;;
*)
_print_success "Applied saved configuration"
# Show quick status
origin_url=$(_get_remote_url)
case "$origin_url" in
https://github.com/*)
_print_info "Tip: Use 'gitssh remote convert' for passwordless access"
;;
esac
;;
esac
return 0
fi
# New repository setup
printf "Git Repository Detected: %s\n" "$(basename "$repo_id")"
origin_url=$(_get_remote_url)
if [ -n "$origin_url" ]; then
printf "Remote: %s\n" "$origin_url"
remote_user=$(_extract_username_from_remote "$origin_url")
[ -n "$remote_user" ] && printf "Remote user: %s\n" "$remote_user"
case "$origin_url" in
https://github.com/*)
printf "Using HTTPS remote\n"
_print_info "Tip: Use 'gitssh remote convert' to convert to SSH for passwordless access"
;;
esac
fi
current_name=$(_get_git_config "user.name" "local")
current_email=$(_get_git_config "user.email" "local")
if [ -n "$current_name" ] && [ -n "$current_email" ]; then
printf "Current user: %s <%s>\n" "$current_name" "$current_email"
# Check for user/remote mismatch
if [ -n "$remote_user" ] && [ "$remote_user" != "$current_name" ]; then
_print_warning "Git user ($current_name) doesn't match remote user ($remote_user)"
# Suggest matching user if available
if _check_dependencies 2>/dev/null && _user_exists "$remote_user"; then
_print_info "You have '$remote_user' configured - use 'gitssh session set' to switch"
fi
fi
printf "\n"
printf "Use current user for this repository? (Y/n): "
read -r keep_user
case "$keep_user" in
[Nn]*)
session_set
;;
*)
user_info="$current_name <$current_email>"
_save_session_user "$repo_id" "$user_info"
_save_persistent_user "$repo_id" "$user_info"
_print_success "Saved current user for future sessions"
;;
esac
else
printf "No git user configured for this repository\n"
printf "Setting up user configuration...\n"
session_set
fi
printf "\n"
}
#================================================================#
# SESSION MANAGEMENT COMMANDS
#================================================================#
# Show current session and persistent configurations
session_show() {
case "$1" in
--verbose|-v)
_show_detailed_session_info
;;
*)
_show_basic_session_info
;;
esac
}
session_clear() {
rm -f "$GIT_SESSION_TEMP"
_print_success "Cleared session mappings"
}
session_forget() {
repo_id=$(_get_repo_id)
if [ -z "$repo_id" ]; then
_print_error "Not in a git repository"
return 1
fi
if _check_dependencies && [ -f "$GIT_SSH_CONFIG_FILE" ]; then
_remove_persistent_user "$repo_id"
_remove_session_user "$repo_id"
_print_success "Removed config for $(basename "$repo_id")"
fi
}
#================================================================#
# PRIVATE HELPER FUNCTIONS
#================================================================#
# Handle custom user input
_handle_custom_user_input() {
repo_id="$1"
printf "Enter name: "
read -r custom_name
custom_name=$(_trim "$custom_name")
if _is_empty "$custom_name"; then
_print_error "Name cannot be empty"
return 1
fi
printf "Enter email: "
read -r custom_email
custom_email=$(_trim "$custom_email")
if _is_empty "$custom_email"; then
_print_error "Email cannot be empty"
return 1
fi
if ! _validate_email "$custom_email"; then
_print_error "Invalid email format"
return 1
fi
_set_git_config "user.name" "$custom_name" "local"
_set_git_config "user.email" "$custom_email" "local"
user_info="$custom_name <$custom_email>"
_save_session_user "$repo_id" "$user_info"
_save_persistent_user "$repo_id" "$user_info"
_print_success "Set custom user: $user_info"
}
# Apply configured user to repository
_apply_configured_user() {
repo_id="$1"
username="$2"
user_details=$(_get_user_details "$username")
name=$(printf "%s" "$user_details" | jq -r '.name')
email=$(printf "%s" "$user_details" | jq -r '.email')
_set_git_config "user.name" "$name" "local"
_set_git_config "user.email" "$email" "local"
user_info="$name <$email>"
_save_session_user "$repo_id" "$user_info"
_save_persistent_user "$repo_id" "$user_info"
_print_success "Set user: $user_info"
}
#================================================================#
# SESSION VALIDATION AND CLEANUP
#================================================================#
# Validate session file integrity
_validate_session_file() {
[ ! -f "$GIT_SESSION_TEMP" ] && return 0 # No file is valid
# Check if file has valid format (repo_path:user_info)
while IFS=: read -r repo_path user_info; do
if [ -z "$repo_path" ] || [ -z "$user_info" ]; then
return 1 # Invalid format
fi
# Check if repo_path exists and is a git repository
if [ ! -d "$repo_path" ] || ! git -C "$repo_path" rev-parse --git-dir >/dev/null 2>&1; then
# Repository no longer exists, mark for cleanup
continue
fi
done < "$GIT_SESSION_TEMP"
return 0
}
# Clean up session entries for non-existent repositories
_cleanup_invalid_sessions() {
[ ! -f "$GIT_SESSION_TEMP" ] && return 0
temp_file=$(_create_temp_file)
while IFS=: read -r repo_path user_info; do
# Keep entry only if repository still exists
if [ -d "$repo_path" ] && git -C "$repo_path" rev-parse --git-dir >/dev/null 2>&1; then
printf "%s:%s\n" "$repo_path" "$user_info" >> "$temp_file"
fi
done < "$GIT_SESSION_TEMP"
_safe_file_replace "$temp_file" "$GIT_SESSION_TEMP"
}
# Get session statistics
_get_session_stats() {
active_repos=0
invalid_repos=0
if [ -f "$GIT_SESSION_TEMP" ]; then
while IFS=: read -r repo_path user_info; do
if [ -d "$repo_path" ] && git -C "$repo_path" rev-parse --git-dir >/dev/null 2>&1; then
active_repos=$((active_repos + 1))
else
invalid_repos=$((invalid_repos + 1))
fi
done < "$GIT_SESSION_TEMP"
fi
printf "active:%d,invalid:%d" "$active_repos" "$invalid_repos"
}
_interactive_user_selection() {
repo_id="$1"
printf "\nSelect Git User Configuration:\n"
printf "=============================\n"
# Check if we have configured users
if _check_dependencies 2>/dev/null && [ -f "$GIT_SSH_USERS_FILE" ]; then
users=$(_get_configured_users)
if [ -n "$users" ]; then
printf "Configured users:\n"
# Convert users to a numbered list without subshell
user_list=""
i=1
for username in $users; do
user_details=$(_get_user_details "$username")
name=$(printf "%s" "$user_details" | jq -r '.name')
email=$(printf "%s" "$user_details" | jq -r '.email')
printf " %d. %s <%s>\n" "$i" "$name" "$email"
user_list="$user_list $username"
i=$((i + 1))
done
printf " c. Enter custom user\n"
user_count=$((i - 1))
printf "\nSelect option (1-%d, c): " "$user_count"
read -r choice
case "$choice" in
[0-9]*)
# Convert choice to username
selected_user=""
current_i=1
for username in $users; do
if [ "$current_i" -eq "$choice" ]; then
selected_user="$username"
break
fi
current_i=$((current_i + 1))
done
if [ -n "$selected_user" ] && _user_exists "$selected_user"; then
_apply_configured_user "$repo_id" "$selected_user"
return 0
else
_print_error "Invalid selection"
return 1
fi
;;
[Cc]*)
_handle_custom_user_input "$repo_id"
;;
*)
_print_error "Invalid option"
return 1
;;
esac
else
printf "No configured users found\n"
_handle_custom_user_input "$repo_id"
fi
else
printf "User management not available (jq not found)\n"
_handle_custom_user_input "$repo_id"
fi
}
# Show basic session information
_show_basic_session_info() {
repo_id=$(_get_repo_id)
if [ -n "$repo_id" ]; then
printf "Current Repository: %s\n" "$(basename "$repo_id")"
# Show session user
session_user=$(_get_session_user "$repo_id")
if [ -n "$session_user" ]; then
printf "Session User: %s\n" "$session_user"
else
printf "Session User: (not set)\n"
fi
# Show current git config
current_user=$(_get_effective_git_user)
printf "Git Config: %s\n" "$current_user"
# Show remote info
remote_url=$(_get_remote_url)
if [ -n "$remote_url" ]; then
printf "Remote: %s\n" "$remote_url"
remote_user=$(_extract_username_from_remote "$remote_url")
[ -n "$remote_user" ] && printf "Remote User: %s\n" "$remote_user"
fi
else
printf "Not in a git repository\n"
fi
}
# Show detailed session information
_show_detailed_session_info() {
_show_basic_session_info
printf "\nSession Statistics:\n"
stats=$(_get_session_stats)
active=$(printf "%s" "$stats" | cut -d',' -f1 | cut -d':' -f2)
invalid=$(printf "%s" "$stats" | cut -d',' -f2 | cut -d':' -f2)
printf "Active repositories: %s\n" "$active"
if [ "$invalid" -gt 0 ]; then
printf "Invalid entries: %s\n" "$invalid"
fi
# Show configured users
if _check_dependencies 2>/dev/null; then
printf "\nConfigured Users:\n"
users=$(_get_configured_users)
if [ -n "$users" ]; then
printf "%s" "$users" | while read -r username; do
user_details=$(_get_user_details "$username")
name=$(printf "%s" "$user_details" | jq -r '.name')
email=$(printf "%s" "$user_details" | jq -r '.email')
printf " %s: %s <%s>\n" "$username" "$name" "$email"
done
else
printf " (none configured)\n"
fi
fi
}
#================================================================#
# ADVANCED SESSION OPERATIONS
#================================================================#
# List all repositories in current session
git_session_list() {
printf "Session Repositories:\n"
printf "====================\n"
if [ ! -f "$GIT_SESSION_TEMP" ] || [ ! -s "$GIT_SESSION_TEMP" ]; then
printf " (no repositories in session)\n"
return 0
fi
i=1
while IFS=: read -r repo_path user_info; do
repo_name=$(basename "$repo_path")
# Check if repository still exists
if [ -d "$repo_path" ] && git -C "$repo_path" rev-parse --git-dir >/dev/null 2>&1; then
printf " %d. %s\n" "$i" "$repo_name"
printf " Path: %s\n" "$repo_path"
printf " User: %s\n" "$user_info"
# Show current branch if possible
current_branch=$(git -C "$repo_path" branch --show-current 2>/dev/null)
[ -n "$current_branch" ] && printf " Branch: %s\n" "$current_branch"
printf "\n"
i=$((i + 1))
fi
done < "$GIT_SESSION_TEMP"
# Show statistics
stats=$(_get_session_stats)
active=$(printf "%s" "$stats" | cut -d',' -f1 | cut -d':' -f2)
invalid=$(printf "%s" "$stats" | cut -d',' -f2 | cut -d':' -f2)
printf "Session Summary: %s active repositories" "$active"
if [ "$invalid" -gt 0 ]; then
printf ", %s invalid entries (use git_session_cleanup)" "$invalid"
fi
printf "\n"
}
# Clean up invalid session entries
git_session_cleanup() {
printf "Cleaning up session...\n"
stats_before=$(_get_session_stats)
invalid_before=$(printf "%s" "$stats_before" | cut -d',' -f2 | cut -d':' -f2)
if [ "$invalid_before" -eq 0 ]; then
_print_info "No cleanup needed"
return 0
fi
_cleanup_invalid_sessions
stats_after=$(_get_session_stats)
active_after=$(printf "%s" "$stats_after" | cut -d',' -f1 | cut -d':' -f2)
_print_success "Removed $invalid_before invalid entries, $active_after repositories remain"
}
# Export session to file
git_session_export() {
output_file="${1:-git-session-export.json}"
if [ ! -f "$GIT_SESSION_TEMP" ] || [ ! -s "$GIT_SESSION_TEMP" ]; then
_print_info "No session data to export"
return 0
fi
temp_file=$(_create_temp_file)
printf '{\n "session_repositories": {\n' > "$temp_file"
first=true
while IFS=: read -r repo_path user_info; do
if [ -d "$repo_path" ] && git -C "$repo_path" rev-parse --git-dir >/dev/null 2>&1; then
if [ "$first" = "false" ]; then
printf ',\n' >> "$temp_file"
fi
repo_name=$(basename "$repo_path")
printf ' "%s": {"path": "%s", "user": "%s"}' "$repo_name" "$repo_path" "$user_info" >> "$temp_file"
first=false
fi
done < "$GIT_SESSION_TEMP"
printf '\n },\n "export_date": "%s"\n}\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$temp_file"
if _safe_file_replace "$temp_file" "$output_file"; then
_print_success "Session exported to $output_file"
else
_print_error "Failed to export session"
return 1
fi
}
# Import session from file
git_session_import() {
import_file="${1:-git-session-export.json}"
if [ ! -f "$import_file" ]; then
_print_error "Import file not found: $import_file"
return 1
fi
if ! _validate_json_file "$import_file" ".session_repositories"; then
_print_error "Invalid import file format"
return 1
fi
printf "Importing session from: %s\n" "$import_file"
# Show what will be imported
printf "Session data to import:\n"
jq -r '.session_repositories | to_entries[] | " " + .key + ": " + .value.user' "$import_file" 2>/dev/null
printf "\nProceed with import? (y/N): "
read -r confirm
case "$confirm" in
[Yy]*) ;;
*) printf "Import cancelled\n"; return 0 ;;
esac
# Import session data
imported_count=0
while IFS= read -r line; do
repo_name=$(printf "%s" "$line" | jq -r '.key')
repo_path=$(printf "%s" "$line" | jq -r '.value.path')
user_info=$(printf "%s" "$line" | jq -r '.value.user')
# Validate repository exists
if [ -d "$repo_path" ] && git -C "$repo_path" rev-parse --git-dir >/dev/null 2>&1; then
_save_session_user "$repo_path" "$user_info"
imported_count=$((imported_count + 1))
else
_print_warning "Skipping missing repository: $repo_path"
fi
done << EOF
$(jq -c '.session_repositories | to_entries[]' "$import_file" 2>/dev/null)
EOF
_print_success "Imported $imported_count repository sessions"
}
session_list() {
git_session_list # Keeping existing implementation
}
session_cleanup() {
git_session_cleanup # Keeping existing implementation
}
session_export() {
git_session_export "$@" # Keeping existing implementation
}
session_import() {
git_session_import "$@" # Keeping existing implementation
}