34 lines
959 B
Bash
34 lines
959 B
Bash
#!/usr/bin/env bash
|
|
set -Eeuo pipefail
|
|
|
|
if [[ ! ( "${2-}" =~ ^([-_[:alnum:]]+)/([-_.[:alnum:]]+)$ ) ]]; then
|
|
echo "Usage: ${0##*/} <hostname> <owner>/<repo>" >&2
|
|
exit 1
|
|
fi
|
|
|
|
hostname="$1"
|
|
owner="${BASH_REMATCH[1]}"
|
|
repo="${BASH_REMATCH[2]}"
|
|
|
|
https="https://$hostname/$owner/$repo.git"
|
|
ssh="git@$hostname:$owner/$repo.git"
|
|
|
|
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
|
if git remote | grep --quiet "^$owner$"; then
|
|
echo "Configuring remote $owner/$repo for HTTPS fetch and SSH push" >&2
|
|
|
|
git remote set-url "$owner" "$https"
|
|
git remote set-url --push "$owner" "$ssh"
|
|
else
|
|
echo "Adding remote $owner/$repo with HTTPS fetch and SSH push" >&2
|
|
|
|
git remote add -f "$owner" "$https"
|
|
git remote set-url --push "$owner" "$ssh"
|
|
fi
|
|
else
|
|
echo "Cloning from remote $owner/$repo with HTTPS fetch and SSH push" >&2
|
|
|
|
git clone --origin "$owner" "$https" "$repo"
|
|
cd "$repo"
|
|
git remote set-url --push "$owner" "$ssh"
|
|
fi
|