aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/swap-protocol.bash
blob: b7f4db26c0a1289998991cce15898c555ccb3968 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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