Files
dotfiles/meta/git-remote
druckdev 0e1c14c3e7 meta:git-remote: Replace here strings with printfs
Some implementations of `sh` seem to not support it.
2020-12-22 16:44:24 +01:00

17 lines
439 B
Bash
Executable File

#!/bin/sh
die() {
printf "$1" >&2
exit ${2:-1}
}
remote="$(git remote get-url origin)"
! printf "$remote" | grep -q '^git@' || die "Using ssh already.\n" 0
host="$(printf "$remote" | cut -d/ -f3)"
grep -q "$host" "$HOME"/.ssh/known_hosts || die "No ssh key for $host found.\n"
ssh_remote="$(printf "$remote" | sed 's_^https\?://_git@_;s_/_:_')"
git remote set-url origin "$ssh_remote"
die "Set origin remote url to $ssh_remote\n" $?