From c2c497544e005239411896632b592901e4fc475a Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Fri, 28 Aug 2020 10:19:25 +0800 Subject: update --- tools/git/swap-protocol.bash | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100755 tools/git/swap-protocol.bash (limited to 'tools/git/swap-protocol.bash') diff --git a/tools/git/swap-protocol.bash b/tools/git/swap-protocol.bash new file mode 100755 index 0000000..b7f4db2 --- /dev/null +++ b/tools/git/swap-protocol.bash @@ -0,0 +1,25 @@ +#! /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='' +extra='' + +# For each remote +git remote -v \ +| while read remote url etc; do + # Set fetch/push URL seperately + [[ $etc =~ push ]] && extra='--push' || extra='' + + if [[ $url =~ git@.*github.com ]]; then + target=${target:-https} + # 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 + fi +done + +git remote -v -- cgit v1.2.3-70-g09d2