aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/git/swap-protocol.bash
blob: e1a82d138ec2bff71578a01153858f0fa5eb1064 (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
26
#! /usr/bin/env bash

# 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
git remote -v \
| while read remote url etc; do
  # Set fetch/push URL seperately
  [[ $etc =~ push ]] && extra='--push' || extra=''

  if [[ $url =~ : ]]; then
    # git@ -> https://
    <<<$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