git:https-and-ssh: Support passing custom remote

This commit is contained in:
2023-01-25 03:05:51 +01:00
parent 6a48ee8adc
commit 47e13d36a6

View File

@@ -1,20 +1,21 @@
#!/bin/sh #!/bin/sh
#!/usr/bin/zsh #!/usr/bin/zsh
# Sets up origin so that it fetches over https but pushes over ssh. This way # Sets up given remote (or origin) so that it fetches over https but pushes over
# unlocking the ssh key is only needed when really necessary. # ssh. This way unlocking the ssh key is only needed when really necessary.
GIT="git" GIT="git"
origin="$($GIT remote get-url origin)" remote="${1:-origin}"
url="$($GIT remote get-url "$remote")"
if [ "${origin##https://}" = "$origin" ]; then if [ "${url##https://}" = "$url" ]; then
https_origin="${origin##*@}" https_url="${url##*@}"
https_origin="https://${https_origin/://}" https_url="https://${https_url/://}"
$GIT remote set-url origin "$https_origin" $GIT remote set-url "$remote" "$https_url"
$GIT remote set-url --push origin "$origin" $GIT remote set-url --push "$remote" "$url"
else else
ssh_origin="${origin##https://}" ssh_url="${url##https://}"
ssh_origin="git@${ssh_origin/\//:}" ssh_url="git@${ssh_url/\//:}"
$GIT remote set-url --push origin "$ssh_origin" $GIT remote set-url --push "$remote" "$ssh_url"
fi fi