67 lines
2.3 KiB
Bash
Executable File
67 lines
2.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
source "$SCRIPT_DIR/../lib/common.sh"
|
|
|
|
# Convert git HTTP(S) remotes to SSH
|
|
convert_git_remote_to_ssh() {
|
|
local repo_dir="${1:-.}"
|
|
|
|
if [ ! -d "$repo_dir/.git" ]; then
|
|
log_warn "Not a git repository: $repo_dir"
|
|
return 1
|
|
fi
|
|
|
|
cd "$repo_dir"
|
|
|
|
# Get all remotes
|
|
local remotes=$(git remote)
|
|
|
|
if [ -z "$remotes" ]; then
|
|
log_info "No remotes found in $repo_dir"
|
|
return 0
|
|
fi
|
|
|
|
for remote in $remotes; do
|
|
local url=$(git remote get-url "$remote")
|
|
local new_url=""
|
|
|
|
# Convert HTTPS to SSH for common Git hosting services
|
|
if [[ "$url" =~ ^https://github\.com/(.+)$ ]]; then
|
|
new_url="git@github.com:${BASH_REMATCH[1]}"
|
|
elif [[ "$url" =~ ^https://gitlab\.com/(.+)$ ]]; then
|
|
new_url="git@gitlab.com:${BASH_REMATCH[1]}"
|
|
elif [[ "$url" =~ ^https://git\.sdf\.org/(.+)$ ]]; then
|
|
# SDF uses port 2222 for SSH
|
|
new_url="ssh://git@git.sdf.org:2222/${BASH_REMATCH[1]}"
|
|
elif [[ "$url" =~ ^https://bitbucket\.org/(.+)$ ]]; then
|
|
new_url="git@bitbucket.org:${BASH_REMATCH[1]}"
|
|
elif [[ "$url" =~ ^http://github\.com/(.+)$ ]]; then
|
|
new_url="git@github.com:${BASH_REMATCH[1]}"
|
|
elif [[ "$url" =~ ^http://gitlab\.com/(.+)$ ]]; then
|
|
new_url="git@gitlab.com:${BASH_REMATCH[1]}"
|
|
elif [[ "$url" =~ ^http://git\.sdf\.org/(.+)$ ]]; then
|
|
# SDF uses port 2222 for SSH
|
|
new_url="ssh://git@git.sdf.org:2222/${BASH_REMATCH[1]}"
|
|
elif [[ "$url" =~ ^http://bitbucket\.org/(.+)$ ]]; then
|
|
new_url="git@bitbucket.org:${BASH_REMATCH[1]}"
|
|
fi
|
|
|
|
if [ -n "$new_url" ]; then
|
|
log_info "Converting $remote: $url -> $new_url"
|
|
git remote set-url "$remote" "$new_url"
|
|
log_success "Converted $remote to SSH"
|
|
else
|
|
log_info "$remote already uses SSH or unsupported URL: $url"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# If called with a directory argument, use that; otherwise use current directory
|
|
TARGET_DIR="${1:-.}"
|
|
|
|
log_info "Converting git remotes to SSH in $TARGET_DIR"
|
|
convert_git_remote_to_ssh "$TARGET_DIR"
|
|
log_success "Git remote conversion complete"
|