forked from a/repotool
82 lines
2.4 KiB
Bash
82 lines
2.4 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# Parse a git URL into domain and path components
|
|
# Usage: parse_git_url <url> <perl_regex_command> <jq_command>
|
|
# Returns: JSON with domain and path fields
|
|
parse_git_url() {
|
|
local url="$1"
|
|
local regex="$2"
|
|
local jq="$3"
|
|
|
|
local domain=""
|
|
local path=""
|
|
local output
|
|
|
|
# Check if it's an HTTP(S) URL
|
|
if [[ "$url" =~ ^https?:// ]]; then
|
|
output=($(echo "$url" | $regex '/^https?:\/\/(?:.*?:)?(.*\..*?)\/(.*?)(\.git)?$/ && print "$1 $2" ' -))
|
|
if [[ ${#output[@]} -eq 2 ]]; then
|
|
domain=${output[0]}
|
|
path=${output[1]}
|
|
fi
|
|
# Check if it's an SSH URL
|
|
elif [[ "$url" =~ ^[^:]+@[^:]+: ]] || [[ "$url" =~ ^ssh:// ]]; then
|
|
output=($(echo "$url" | $regex '/^(?:.*?@)?(.*\..*?)(?::|\/)(.*?)(\.git)?$/ && print "$1 $2" ' -))
|
|
if [[ ${#output[@]} -eq 2 ]]; then
|
|
domain=${output[0]}
|
|
path=${output[1]}
|
|
fi
|
|
# Check if it's a bare domain path (e.g., gfx.cafe/oku/trade)
|
|
else
|
|
output=($(echo "$url" | $regex '/^(.*\..*?)\/(.*?)(\.git)?$/ && print "$1 $2" ' -))
|
|
if [[ ${#output[@]} -eq 2 ]]; then
|
|
domain=${output[0]}
|
|
path=${output[1]}
|
|
fi
|
|
fi
|
|
|
|
# Return JSON
|
|
$jq -n --arg domain "$domain" --arg path "$path" '{domain: $domain, path: $path}'
|
|
}
|
|
|
|
# Get domain and path from current git repository's origin
|
|
# Usage: parse_git_origin <perl_regex_command> <jq_command>
|
|
# Returns: JSON with domain and path fields
|
|
parse_git_origin() {
|
|
local regex="$1"
|
|
local jq="$2"
|
|
|
|
# Get origin URL
|
|
local origin_url
|
|
origin_url=$(git config --get remote.origin.url)
|
|
|
|
if [[ -z "$origin_url" ]]; then
|
|
$jq -n '{domain: "", path: ""}'
|
|
return 1
|
|
fi
|
|
|
|
# Parse the URL
|
|
parse_git_url "$origin_url" "$regex" "$jq"
|
|
}
|
|
|
|
# Validate if a URL is a valid git repository URL
|
|
# Usage: valid_url <url>
|
|
# Returns: 1 for HTTP(S), 2 for SSH, 3 for bare domain paths, -1 for invalid
|
|
valid_url() {
|
|
local url="$1"
|
|
|
|
if [[ "$url" =~ ^https?:// ]]; then
|
|
echo "1"
|
|
return 0
|
|
elif [[ "$url" =~ ^[^:]+@[^:]+: ]] || [[ "$url" =~ ^ssh:// ]]; then
|
|
echo "2"
|
|
return 0
|
|
elif [[ "$url" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]+/[^/]+ ]]; then
|
|
# Bare domain path like gfx.cafe/oku/trade
|
|
echo "3"
|
|
return 0
|
|
else
|
|
echo "-1"
|
|
return 1
|
|
fi
|
|
} |