Зависимости:
Не думаю, что URL-адрес git git@private:32000/lab/rollup-umd-documentation.git
действителен.Номер порта 32000
перепутан с путем.Поэтому я заменяю URL-адреса, такие как git@github.com:foo/bar.git
на ssh://git@github.com/foo/bar.git
script mygit
:
#!/usr/bin/env bash
set -eu -o pipefail
for param in "$@"; do
case "$param" in
--force-domain=*)
private_domain="${param#*=}"
;;
--force-port=*)
private_port="${param#*=}"
;;
*://*|*@*/*)
git_url="$param"
;;
esac
done
declare -a sed_opts=(-e "s,^\\([a-z]\\+\\)@\\([^:]\\+\\):,ssh://\\1@\\2/,")
[[ -n "${private_domain:-}" ]] && sed_opts+=(
-e "s,//\\(.\\+@\\)\\?[^/:]\\+,//\\1$private_domain,"
)
[[ -n "${private_port:-}" ]] && sed_opts+=(
-e "s,//\\(.\\+@\\)\\?\\([^:/]\\+\\)\\(:[0-9]\\+\\)\\?,//\\1\\2:${private_port},"
)
if [[ -z "${git_url}" ]]; then
cat <<HELP_MESSAGE
$0 [--force-domain=domain] [--force-port=port] git-repo-url
HELP_MESSAGE
exit 1
fi
new_git_url="$(echo "$git_url" | sed "${sed_opts[@]}")"
echo "${new_git_url}"
Тестовый код:
#!/usr/bin/env bash
set -eu
declare -a test_git_urls=(
git@github.com:lab/rollup-umd-documentation.git
https://github.com/lab/rollup-umd-documentation.git
http://github.com/lab/rollup-umd-documentation.git
ssh://git@git.gitlab.com:200/deploy-tools/ssl-gandi.git
https://git.gitlab.com/deploy-tools/ssl-gandi-gitlab.git
https://git.gitlab.com:31000/deploy-tools/ssl-gandi-gitlab.git
)
declare -a private_domain_urls=(
ssh://git@private/lab/rollup-umd-documentation.git # git@private:lab/rollup-umd-documentation.git
https://private/lab/rollup-umd-documentation.git
http://private/lab/rollup-umd-documentation.git
ssh://git@private:200/deploy-tools/ssl-gandi.git
https://private/deploy-tools/ssl-gandi-gitlab.git
https://private:31000/deploy-tools/ssl-gandi-gitlab.git
)
declare -a private_port_urls=(
ssh://git@private:32000/lab/rollup-umd-documentation.git # git@private:32000/lab/rollup-umd-documentation.git
https://private:32000/lab/rollup-umd-documentation.git
http://private:32000/lab/rollup-umd-documentation.git
ssh://git@private:32000/deploy-tools/ssl-gandi.git
https://private:32000/deploy-tools/ssl-gandi-gitlab.git
https://private:32000/deploy-tools/ssl-gandi-gitlab.git
)
for index in {0..5}; do
git_url="${test_git_urls[$index]}"
echo "$index: $git_url"
new_git_url="$(./mygit --force-domain=private "$git_url")"
[[ "$new_git_url" = "${private_domain_urls[$index]}" ]] && echo -n OK || echo " $new_git_url" '!=' "${private_domain_urls[$index]}"
new_git_url="$(./mygit "$git_url" --force-domain=private --force-port=32000)"
[[ "$new_git_url" = "${private_port_urls[$index]}" ]] && echo -n OK || echo " $new_git_url" '!=' "${private_port_urls[$index]}"
echo ""
done
Примеры:
./mygit git@github.com:lab/rollup-umd-documentation.git --force-domain=private --force-port=32000
ssh://git@private:32000/lab/rollup-umd-documentation.git
./mygit git@github.com:lab/rollup-umd-documentation.git --force-domain=private
ssh://git@private/lab/rollup-umd-documentation.git
./mygit --force-domain=private --force-port=32000 \
https://github.com:31000/deploy-tools/ssl-gandi-gitlab.git
https://private:32000/deploy-tools/ssl-gandi-gitlab.git