aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rwxr-xr-xbin/git/swap-protocol.bash21
1 files changed, 11 insertions, 10 deletions
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 @@
1#! /usr/bin/env bash 1#! /usr/bin/env bash
2# Get the first remote URL within git/https protocol on github.com
3# Swap the protocol, and apply new protocol to every remaining remotes
4 2
5target='' 3# Swap protocol for every git remotes, for example:
4# git@gitlab.com:me/repo ->
5# https://gitlab.com/me/repo
6
7login=${1:-git}
6extra='' 8extra=''
7 9
8# For each remote 10# For each remote
@@ -11,15 +13,14 @@ git remote -v \
11 # Set fetch/push URL seperately 13 # Set fetch/push URL seperately
12 [[ $etc =~ push ]] && extra='--push' || extra='' 14 [[ $etc =~ push ]] && extra='--push' || extra=''
13 15
14 if [[ $url =~ git@.*github.com ]]; then 16 if [[ $url =~ : ]]; then
15 target=${target:-https}
16 # git@ -> https:// 17 # git@ -> https://
17 [[ $target == https ]] && sed -E 's#^git@(.+):(.+)$#https://\1/\2#' <<<$url | xargs git remote set-url $extra $remote 18 <<<$url sed -E 's#^.+@(.+):(.+)$#https://\1/\2#' | xargs git remote set-url ${extra} ${remote}
18 elif [[ $url =~ https://.*github.com ]]; then 19 elif [[ $url =~ ^http ]]; then
19 target=${target:-git} 20 # http[s]:// -> git@
20 # https:// -> git@ 21 <<<$url sed -E "s#^https?://([^/]+)/(.+)\$#${login}@\1:\2#" | xargs git remote set-url ${extra} ${remote}
21 [[ $target == git ]] && sed -E 's#^https://([^/]+)/(.+)$#git@\1:\2#' <<<$url | xargs git remote set-url $extra $remote
22 fi 22 fi
23done 23done
24 24
25# Print current remotes
25git remote -v 26git remote -v