-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.sh
More file actions
640 lines (553 loc) · 18.1 KB
/
library.sh
File metadata and controls
640 lines (553 loc) · 18.1 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
#!/bin/bash
# Shellib Git Library - Git repository management utilities
# Load dependencies
if [ ! -f "$HOME/bin/grab" ]; then
mkdir -p $HOME/bin
export PATH=$PATH:$HOME/bin
curl -o $HOME/bin/grab -L https://github.com/shellib/grab/raw/master/grab.sh && chmod +x $HOME/bin/grab
fi
source $(grab github.com/shellib/toml as toml)
# Detect if URL is GitHub or GitLab
is_github() {
local url="$1"
echo "$url" | grep -q "github.com"
}
# Extract host from URL
extract_host() {
local url="$1"
echo "$url" | sed -n 's/.*:\/\/\([^\/]*\)\/.*/\1/p'
}
# Extract organization from URL
extract_organization() {
local url="$1"
echo "$url" | sed -n 's/.*\/\/[^\/]*\/\([^\/]*\)\/.*/\1/p'
}
# Extract repository name from URL (including nested paths like group/subgroup/repo)
extract_repository() {
local url="$1"
# Remove protocol and host, then remove the first path segment (org), then extract the repo path
# Also strip common GitLab/GitHub path suffixes like /-/, /blob/, /tree/, etc.
local repo_path
repo_path=$(echo "$url" | sed -n 's|.*://[^/]*/[^/]*/\([^?#]*\).*|\1|p')
# Remove .git suffix
repo_path=$(echo "${repo_path:-}" | sed 's/\.git$//')
# Remove GitLab/GitHub specific path segments
repo_path=$(echo "${repo_path:-}" | sed 's|/-/.*||')
repo_path=$(echo "${repo_path:-}" | sed 's|/blob/.*||')
repo_path=$(echo "${repo_path:-}" | sed 's|/tree/.*||')
repo_path=$(echo "${repo_path:-}" | sed 's|/pull/.*||')
repo_path=$(echo "${repo_path:-}" | sed 's|/merge_requests/.*||')
echo "$repo_path"
}
# Extract path after repository from URL
extract_path() {
local url="$1"
echo "$url" | sed -n 's/.*\/\/[^\/]*\/[^\/]*\/[^\/]*\(\/.*\)/\1/p'
}
# Extract pull request number from GitHub URL
extract_github_pr_number() {
local url="$1"
echo "$url" | sed -n 's/.*pull\/\([0-9]*\).*/\1/p'
}
# Extract merge request number from GitLab URL
extract_gitlab_mr_number() {
local url="$1"
echo "$url" | sed -n 's/.*merge_requests\/\([0-9]*\).*/\1/p'
}
# Build SSH clone URL
build_ssh_url() {
local host="$1"
local org="$2"
local repo="$3"
local username="${4:-}" # Optional username override
if [ -n "${username:-}" ]; then
# When username is specified, use it instead of org
echo "git@$host:$username/$repo.git"
else
# Use org and repo (repo may contain slashes for nested paths)
echo "git@$host:$org/$repo.git"
fi
}
# Build HTTPS clone URL
build_https_url() {
local host="$1"
local org="$2"
local repo="$3"
# repo may contain slashes for nested paths (e.g., subgroup/myrepo)
echo "https://$host/$org/$repo.git"
}
# Parse TOML config and extract method and username
get_config_values() {
local host="$1"
local org="$2"
local repo="$3"
local config_file="${GIT_CONFIG_FILE:-$HOME/.config/qutebrowser/git.toml}"
# Default values - start with empty method so we can detect when config is found
local method=""
local username=""
if [ -f "$config_file" ]; then
# Try repo-specific config first
local repo_section="\"$host/$org/$repo\""
if toml::has_section "$repo_section" "$config_file"; then
method=$(toml::get_value "$repo_section" "method" "$config_file")
username=$(toml::get_value "$repo_section" "username" "$config_file")
fi
# If not found or empty, try host-specific config
if [ -z "$method" ]; then
local host_section="\"$host\""
if toml::has_section "$host_section" "$config_file"; then
local host_method
local host_username
host_method=$(toml::get_value "$host_section" "method" "$config_file")
host_username=$(toml::get_value "$host_section" "username" "$config_file")
if [ -n "${host_method:-}" ]; then
method="$host_method"
if [ -z "${username:-}" ]; then
username="${host_username:-}"
fi
fi
fi
fi
# If not found or empty, try global config
if [ -z "$method" ]; then
if toml::has_section "default" "$config_file"; then
local default_method
default_method=$(toml::get_value "default" "method" "$config_file")
if [ -n "${default_method:-}" ]; then
method="$default_method"
fi
fi
fi
fi
# Default to fallback if nothing found
if [ -z "${method:-}" ]; then
method="fallback"
fi
echo "$method|$username"
}
# Get clone method from config (ssh|https|fallback) - for backward compatibility
get_method() {
local config_values
config_values=$(get_config_values "$1" "$2" "$3")
echo "${config_values%%|*}"
}
# Get username from config
get_username() {
local config_values
config_values=$(get_config_values "$1" "$2" "$3")
echo "${config_values##*|}"
}
# Try SSH clone with timeout, fallback to HTTPS if it fails
clone_with_fallback() {
local host="$1"
local org="$2"
local repo="$3"
local target_dir="$4"
local username="${5:-}" # Optional username
local ssh_url
local https_url
ssh_url=$(build_ssh_url "$host" "$org" "$repo" "$username")
https_url=$(build_https_url "$host" "$org" "$repo")
echo "Attempting SSH clone: $ssh_url"
# Set SSH timeout and try SSH clone
if timeout 10 git -c core.sshCommand="ssh -o ConnectTimeout=10 -o ServerAliveInterval=10 -o ServerAliveCountMax=1" clone "$ssh_url" "$target_dir" 2>/dev/null; then
echo "SSH clone successful"
return 0
else
echo "SSH clone failed, trying HTTPS: $https_url"
# Clean up partial clone if it exists
rm -rf "$target_dir" 2>/dev/null
# Clone with HTTPS
if git clone "$https_url" "$target_dir"; then
echo "HTTPS clone successful"
# Configure git to always use HTTPS for this host to avoid future SSH timeouts
(cd "$target_dir" && git config url."https://$host/".insteadOf "git@$host:")
echo "Configured git to use HTTPS for $host"
return 0
else
echo "HTTPS clone failed"
return 1
fi
fi
}
# Clone repository using configured method
clone() {
local host="$1"
local org="$2"
local repo="$3"
local target_dir="$4"
local config_values
config_values=$(get_config_values "$host" "$org" "$repo")
local method="${config_values%%|*}"
local username="${config_values##*|}"
local ssh_url
local https_url
ssh_url=$(build_ssh_url "$host" "$org" "$repo" "$username")
https_url=$(build_https_url "$host" "$org" "$repo")
case "$method" in
"ssh")
echo "Using SSH (configured): $ssh_url"
git clone "$ssh_url" "$target_dir"
;;
"https")
echo "Using HTTPS (configured): $https_url"
git clone "$https_url" "$target_dir"
;;
"fallback"|*)
clone_with_fallback "$host" "$org" "$repo" "$target_dir" "$username"
;;
esac
}
# Build workspace path
workspace_path() {
local host="$1"
local org="$2"
local repo="${3:-}" # Optional: if provided, includes the full path to the repo
if [ -n "${repo:-}" ]; then
echo "~/workspace/src/$host/$org/$repo"
else
echo "~/workspace/src/$host/$org"
fi
}
# Test functions
test_is_github() {
if is_github "https://github.com/owner/repo"; then
echo "✓ is_github detects GitHub URL"
else
echo "✗ is_github failed to detect GitHub URL"
return 1
fi
if ! is_github "https://gitlab.example.com/owner/repo"; then
echo "✓ is_github correctly rejects GitLab URL"
else
echo "✗ is_github incorrectly detected GitLab URL as GitHub"
return 1
fi
return 0
}
test_extract_host() {
local host
host=$(extract_host "https://gitlab.example.com/owner/repo")
if [ "${host:-}" = "gitlab.example.com" ]; then
echo "✓ extract_host works"
return 0
else
echo "✗ extract_host failed, got: '$host'"
return 1
fi
}
test_extract_organization() {
local org
org=$(extract_organization "https://github.com/testorg/testrepo")
if [ "${org:-}" = "testorg" ]; then
echo "✓ extract_organization works"
return 0
else
echo "✗ extract_organization failed, got: '$org'"
return 1
fi
}
test_extract_repository() {
local repo
repo=$(extract_repository "https://github.com/testorg/testrepo")
if [ "${repo:-}" = "testrepo" ]; then
echo "✓ extract_repository works"
else
echo "✗ extract_repository failed, got: '$repo'"
return 1
fi
# Test nested repository path
local nested_repo
nested_repo=$(extract_repository "https://gitserver.example.com/mygroup/subgroup/myrepo")
if [ "${nested_repo:-}" = "subgroup/myrepo" ]; then
echo "✓ extract_repository handles nested paths"
return 0
else
echo "✗ extract_repository failed for nested path, got: '$nested_repo', expected: 'subgroup/myrepo'"
return 1
fi
}
test_extract_path() {
local path
path=$(extract_path "https://github.com/owner/repo/blob/main/file.txt")
if [ "${path:-}" = "/blob/main/file.txt" ]; then
echo "✓ extract_path works"
return 0
else
echo "✗ extract_path failed, got: '$path'"
return 1
fi
}
test_extract_github_pr_number() {
local pr
pr=$(extract_github_pr_number "https://github.com/owner/repo/pull/123")
if [ "${pr:-}" = "123" ]; then
echo "✓ extract_github_pr_number works"
return 0
else
echo "✗ extract_github_pr_number failed, got: '$pr'"
return 1
fi
}
test_extract_gitlab_mr_number() {
local mr
mr=$(extract_gitlab_mr_number "https://gitlab.example.com/owner/repo/-/merge_requests/456")
if [ "${mr:-}" = "456" ]; then
echo "✓ extract_gitlab_mr_number works"
return 0
else
echo "✗ extract_gitlab_mr_number failed, got: '$mr'"
return 1
fi
}
test_build_ssh_url() {
local ssh_url
ssh_url=$(build_ssh_url "github.com" "owner" "repo")
if [ "${ssh_url:-}" = "git@github.com:owner/repo.git" ]; then
echo "✓ build_ssh_url works without username"
else
echo "✗ build_ssh_url failed without username, got: '$ssh_url'"
return 1
fi
# Test with username override
local ssh_url_with_user
ssh_url_with_user=$(build_ssh_url "gitlab.com" "owner" "repo" "customuser")
if [ "${ssh_url_with_user:-}" = "git@gitlab.com:customuser/repo.git" ]; then
echo "✓ build_ssh_url works with username override"
return 0
else
echo "✗ build_ssh_url failed with username, got: '$ssh_url_with_user'"
return 1
fi
}
test_build_https_url() {
local https_url
https_url=$(build_https_url "gitlab.example.com" "owner" "repo")
if [ "${https_url:-}" = "https://gitlab.example.com/owner/repo.git" ]; then
echo "✓ build_https_url works"
return 0
else
echo "✗ build_https_url failed, got: '$https_url'"
return 1
fi
}
test_workspace_path() {
local workspace
workspace=$(workspace_path "gitlab.example.com" "owner")
if [ "${workspace:-}" = "~/workspace/src/gitlab.example.com/owner" ]; then
echo "✓ workspace_path works"
return 0
else
echo "✗ workspace_path failed, got: '$workspace'"
return 1
fi
}
test_build_ssh_url_nested() {
local ssh_url
ssh_url=$(build_ssh_url "gitserver.example.com" "mygroup" "subgroup/myrepo")
if [ "${ssh_url:-}" = "git@gitserver.example.com:mygroup/subgroup/myrepo.git" ]; then
echo "✓ build_ssh_url works with nested repository path"
return 0
else
echo "✗ build_ssh_url failed with nested path, got: '$ssh_url'"
return 1
fi
}
test_build_https_url_nested() {
local https_url
https_url=$(build_https_url "gitserver.example.com" "mygroup" "subgroup/myrepo")
if [ "${https_url:-}" = "https://gitserver.example.com/mygroup/subgroup/myrepo.git" ]; then
echo "✓ build_https_url works with nested repository path"
return 0
else
echo "✗ build_https_url failed with nested path, got: '$https_url'"
return 1
fi
}
test_workspace_path_nested() {
local workspace
workspace=$(workspace_path "gitserver.example.com" "mygroup" "subgroup/myrepo")
if [ "${workspace:-}" = "~/workspace/src/gitserver.example.com/mygroup/subgroup/myrepo" ]; then
echo "✓ workspace_path works with nested repository path"
return 0
else
echo "✗ workspace_path failed with nested path, got: '$workspace'"
return 1
fi
}
test_get_config_values_default() {
local temp_config=$(mktemp)
export GIT_CONFIG_FILE="$temp_config"
cat > "$temp_config" << 'EOF'
[default]
method = "ssh"
EOF
local config_values
config_values=$(get_config_values "unknown.com" "org" "repo")
local method="${config_values%%|*}"
local username="${config_values##*|}"
if [ "${method:-}" = "ssh" ] && [ -z "${username:-}" ]; then
echo "✓ get_config_values uses default config"
else
echo "✗ get_config_values default failed, method: '$method', username: '$username'"
rm "$temp_config"
return 1
fi
rm "$temp_config"
unset GIT_CONFIG_FILE
return 0
}
test_get_config_values_host() {
local temp_config=$(mktemp)
export GIT_CONFIG_FILE="$temp_config"
cat > "$temp_config" << 'EOF'
[default]
method = "ssh"
["github.com"]
method = "https"
username = "hostuser"
EOF
local config_values
config_values=$(get_config_values "github.com" "org" "repo")
local method="${config_values%%|*}"
local username="${config_values##*|}"
if [ "${method:-}" = "https" ] && [ "${username:-}" = "hostuser" ]; then
echo "✓ get_config_values uses host-specific config"
else
echo "✗ get_config_values host failed, method: '$method', username: '$username'"
rm "$temp_config"
return 1
fi
rm "$temp_config"
unset GIT_CONFIG_FILE
return 0
}
test_get_config_values_repo() {
local temp_config=$(mktemp)
export GIT_CONFIG_FILE="$temp_config"
cat > "$temp_config" << 'EOF'
[default]
method = "ssh"
["github.com"]
method = "https"
["gitlab.example.com/org/special-repo"]
method = "fallback"
username = "repouser"
EOF
local config_values
config_values=$(get_config_values "gitlab.example.com" "org" "special-repo")
local method="${config_values%%|*}"
local username="${config_values##*|}"
if [ "${method:-}" = "fallback" ] && [ "${username:-}" = "repouser" ]; then
echo "✓ get_config_values uses repo-specific config"
else
echo "✗ get_config_values repo failed, method: '$method', username: '$username'"
rm "$temp_config"
return 1
fi
rm "$temp_config"
unset GIT_CONFIG_FILE
return 0
}
test_get_config_values_no_config() {
local temp_config="/tmp/nonexistent_config_file_$$"
export GIT_CONFIG_FILE="$temp_config"
local config_values
config_values=$(get_config_values "github.com" "org" "repo")
local method="${config_values%%|*}"
local username="${config_values##*|}"
if [ "${method:-}" = "fallback" ] && [ -z "${username:-}" ]; then
echo "✓ get_config_values defaults when no config file"
else
echo "✗ get_config_values no-file failed, method: '$method', username: '$username'"
return 1
fi
unset GIT_CONFIG_FILE
return 0
}
test_get_method() {
local temp_config=$(mktemp)
export GIT_CONFIG_FILE="$temp_config"
cat > "$temp_config" << 'EOF'
[default]
method = "ssh"
["github.com"]
method = "https"
EOF
# Test backward compatibility function
local method
method=$(get_method "unknown.com" "org" "repo")
if [ "${method:-}" = "ssh" ]; then
echo "✓ get_method backward compatibility works"
else
echo "✗ get_method backward compatibility failed, got: '$method'"
rm "$temp_config"
return 1
fi
rm "$temp_config"
unset GIT_CONFIG_FILE
return 0
}
test_clone() {
# Mock git to test function logic
git() {
echo "Mock git called with: $*"
return 0
}
# Create a temporary TOML config file
local temp_config=$(mktemp)
export GIT_CONFIG_FILE="$temp_config"
cat > "$temp_config" << 'EOF'
["github.com"]
method = "ssh"
EOF
# Test configured SSH clone
local result
result=$(clone "github.com" "owner" "repo" "target" 2>&1)
if echo "${result:-}" | grep -q "Using SSH (configured)"; then
echo "✓ clone uses configured SSH"
else
echo "✗ clone SSH config failed: $result"
rm "$temp_config"
return 1
fi
rm "$temp_config"
unset GIT_CONFIG_FILE
return 0
}
# Run all tests
run_tests() {
echo "Running tests for git library..."
local failed=0
test_is_github || failed=1
test_extract_host || failed=1
test_extract_organization || failed=1
test_extract_repository || failed=1
test_extract_path || failed=1
test_extract_github_pr_number || failed=1
test_extract_gitlab_mr_number || failed=1
test_build_ssh_url || failed=1
test_build_https_url || failed=1
test_workspace_path || failed=1
test_build_ssh_url_nested || failed=1
test_build_https_url_nested || failed=1
test_workspace_path_nested || failed=1
test_get_config_values_default || failed=1
test_get_config_values_host || failed=1
test_get_config_values_repo || failed=1
test_get_config_values_no_config || failed=1
test_get_method || failed=1
test_clone || failed=1
if [ $failed -eq 0 ]; then
echo "All tests passed!"
return 0
else
echo "Some tests failed!"
return 1
fi
}
# Run tests if --test is passed and we're not being sourced
if [ "${1:-}" = "--test" ] && [ "${BASH_SOURCE[0]}" = "${0}" ]; then
run_tests
exit $?
fi