Files
dotfiles/.config/zsh/autoload/git/git-https-and-ssh
Julian Prein 19fc37299a git:https-and-ssh: Make POSIX-shell compliant
Don't know why this had two shebangs and how I never noticed.
Apparently I only launched the script through the autoloaded
function in zsh.
2024-10-30 10:52:01 +01:00

21 lines
578 B
Bash
Executable File

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