From 873f3643aa96c71bdf58924335b8360473c49f97 Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Sat, 15 Jan 2022 17:31:09 +0530 Subject: [PATCH] fish: functions: Add alias to switch git repo between HTTPS and SSH Taken from https://github.com/whonore/dotfiles/blob/main/fish/functions/git_remote_toggle.fish --- .../fish/functions/git_remote_toggle.fish | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 fish/.config/fish/functions/git_remote_toggle.fish diff --git a/fish/.config/fish/functions/git_remote_toggle.fish b/fish/.config/fish/functions/git_remote_toggle.fish new file mode 100644 index 0000000..1d1b6af --- /dev/null +++ b/fish/.config/fish/functions/git_remote_toggle.fish @@ -0,0 +1,76 @@ +function git_remote_toggle --description 'Switch git repository between HTTPS and SSH' + set -l options h/help q/quiet S/ssh H/https + argparse $options -- $argv; or return + + if set -q _flag_help + echo 'Usage: git_remote_toggle [-h/--help] [-q/--quiet] [-S/--ssh] [-H/--https] [remotes ...]' + return 0 + end + + if set -q _flag_ssh; and set -q _flag_https + echo 'Must choose only one of --ssh and --https' + return 1 + end + + if ! __grt_in_git + echo 'Not in a git repository' + return 1 + end + + for remote in $argv + set -l url (git remote get-url $remote 2>/dev/null) + set -l newurl + + if test -z "$url" + if ! set -q _flag_quiet + echo "Remote $remote does not exist" + end + continue + else if __grt_is_ssh $url; and ! set -q _flag_ssh + set newurl (__grt_to_https $url) + else if __grt_is_https $url; and ! set -q _flag_https + set newurl (__grt_to_ssh $url) + end + + if test -n "$newurl" + # Sanity check + if __grt_is_ssh $url; and __grt_is_https $newurl + else if __grt_is_https $url; and __grt_is_ssh $newurl + else + echo "Something went wrong: $url -> $newurl" + continue + end + + if ! set -q _flag_quiet + echo "$remote: $url -> $newurl" + end + git remote set-url $remote $newurl + end + end +end + +# git@github.com:{repo} -> https://github.com/{repo} +function __grt_to_https + set -l url (string replace ':' '/' $argv) + set -l url (string replace 'git@' 'https://' $url) + echo $url +end + +# https://github.com/{repo} -> git@github.com:{repo} +function __grt_to_ssh + set -l url (string replace 'https://' 'git@' $argv) + set -l url (string replace '/' ':' $url) + echo $url +end + +function __grt_is_ssh + string match -qr '^git@' $argv +end + +function __grt_is_https + string match -qr '^https://' $argv +end + +function __grt_in_git + git status 2&>/dev/null +end