From 7161070ca1d3383570f46ecf96e0ddad8a16f053 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Sun, 24 Sep 2023 11:28:40 +0800 Subject: Update --- bin/git/swap-protocol.bash | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'bin/git/swap-protocol.bash') diff --git a/bin/git/swap-protocol.bash b/bin/git/swap-protocol.bash index b7f4db2..e1a82d1 100755 --- a/bin/git/swap-protocol.bash +++ b/bin/git/swap-protocol.bash @@ -1,8 +1,10 @@ #! /usr/bin/env bash -# Get the first remote URL within git/https protocol on github.com -# Swap the protocol, and apply new protocol to every remaining remotes -target='' +# Swap protocol for every git remotes, for example: +# git@gitlab.com:me/repo -> +# https://gitlab.com/me/repo + +login=${1:-git} extra='' # For each remote @@ -11,15 +13,14 @@ git remote -v \ # Set fetch/push URL seperately [[ $etc =~ push ]] && extra='--push' || extra='' - if [[ $url =~ git@.*github.com ]]; then - target=${target:-https} + if [[ $url =~ : ]]; then # git@ -> https:// - [[ $target == https ]] && sed -E 's#^git@(.+):(.+)$#https://\1/\2#' <<<$url | xargs git remote set-url $extra $remote - elif [[ $url =~ https://.*github.com ]]; then - target=${target:-git} - # https:// -> git@ - [[ $target == git ]] && sed -E 's#^https://([^/]+)/(.+)$#git@\1:\2#' <<<$url | xargs git remote set-url $extra $remote + <<<$url sed -E 's#^.+@(.+):(.+)$#https://\1/\2#' | xargs git remote set-url ${extra} ${remote} + elif [[ $url =~ ^http ]]; then + # http[s]:// -> git@ + <<<$url sed -E "s#^https?://([^/]+)/(.+)\$#${login}@\1:\2#" | xargs git remote set-url ${extra} ${remote} fi done +# Print current remotes git remote -v -- cgit v1.2.3-70-g09d2