Last active
November 14, 2020 15:12
-
-
Save AnirudhKonduru/84697ffc809d012e5e08c871bc69492b to your computer and use it in GitHub Desktop.
script to switch git remote protocol between http/https/ssh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eo pipefail | |
if [[ $1 == "" ]]; then | |
echo "usage: script.sh target-protocol [remote-name]" | |
echo "example: script.sh ssh origin" | |
exit | |
fi | |
TARGET_PROTOCOL=$1 | |
if [[ $2 == "" ]]; then | |
TARGET_REMOTES=$(git remote) | |
else | |
TARGET_REMOTES="${*:2}" | |
fi | |
change_remote() { | |
local TARGET_PROTOCOL=${1} | |
local TARGET_REMOTE=${2} | |
# https://regexr.com/52rmj | |
# for an explanation on how this regex was created | |
REGEX='(git@|https?:\/\/)([A-Za-z0-9\.]+)(:|\/)([^\/]+)(\/)(.*)' | |
CURRENT_REMOTE=$(git remote get-url "$TARGET_REMOTE") | |
if [[ $CURRENT_REMOTE =~ $REGEX ]]; then | |
DOMAIN=${BASH_REMATCH[2]} | |
USERNAME=${BASH_REMATCH[4]} | |
REPO_NAME=${BASH_REMATCH[6]} | |
REPO_PATH=$USERNAME'/'$REPO_NAME | |
else | |
echo "Invalid remote $CURRENT_REMOTE" | |
exit | |
fi | |
case $TARGET_PROTOCOL in | |
ssh) | |
NEW_REMOTE="git@$DOMAIN:$REPO_PATH" | |
;; | |
http) | |
NEW_REMOTE="http://$DOMAIN/$REPO_PATH" | |
;; | |
https) | |
NEW_REMOTE="https://$DOMAIN/$REPO_PATH" | |
;; | |
*) | |
echo "Invalid target protocol $TARGET_PROTOCOL" | |
exit | |
;; | |
esac | |
# echo $NEW_REMOTE | |
git remote set-url "$TARGET_REMOTE" "$NEW_REMOTE" | |
echo "remote $TARGET_REMOTE changed from $CURRENT_REMOTE -> $NEW_REMOTE" | |
} | |
for i in ${TARGET_REMOTES}; do | |
change_remote "$TARGET_PROTOCOL" "$i" | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eo pipefail | |
if [[ $2 == "" ]]; then | |
echo "usage: script.sh oldUsername newUsername" | |
exit | |
fi | |
OLD_USERNAME=$1 | |
NEW_USERNAME=$2 | |
TARGET_REMOTES=$(git remote) | |
change_username() { | |
local TARGET_REMOTE=${1} | |
# https://regexr.com/52rmj | |
# for an explanation on how this regex was created | |
REGEX='([git@|https?:\/\/]+[A-Za-z0-9\.]+.[:|\/])([^\/]+)[\/](.*)' | |
CURRENT_REMOTE=$(git remote get-url "$TARGET_REMOTE") | |
if [[ $CURRENT_REMOTE =~ $REGEX ]]; then | |
PRELUDE=${BASH_REMATCH[1]} | |
USERNAME=${BASH_REMATCH[2]} | |
REPO_NAME=${BASH_REMATCH[3]} | |
else | |
echo "Invalid remote $CURRENT_REMOTE" | |
exit | |
fi | |
if [[ $USERNAME == "$OLD_USERNAME" ]]; then | |
NEW_REMOTE=$PRELUDE$NEW_USERNAME'/'$REPO_NAME | |
git remote set-url "$TARGET_REMOTE" "$NEW_REMOTE" | |
echo "remote $TARGET_REMOTE changed from $CURRENT_REMOTE -> $NEW_REMOTE" | |
fi | |
} | |
for i in ${TARGET_REMOTES}; do | |
change_username "$i" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment