Last active
April 24, 2019 02:28
-
-
Save fenying/66e36f530c76061035b1f6146696ff14 to your computer and use it in GitHub Desktop.
A tool for switching git remote repository URL.
This file contains 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
#!/bin/bash | |
# Usage | |
# | |
# We assume the link of remote repository is of SSH style like: | |
# | |
# <git-user>@<domain>:<scope>/<project-name>.git | |
# | |
# git-switch-remote-repo <remote-name> full <full-repo-link> | |
# git-switch-remote-repo <remote-name> domain <new-domain> | |
# git-switch-remote-repo <remote-name> user <new-git-user> | |
# git-switch-remote-repo <remote-name> scope <new-scope> | |
# git-switch-remote-repo <remote-name> project <new-project-name> | |
# | |
# e.g. | |
# | |
# git-switch-remote-repo origin full git@github.com:abc/hello.git | |
# git-switch-remote-repo origin domain gitee.com | |
# git-switch-remote-repo origin user mygit | |
# git-switch-remote-repo origin scope microsoft | |
# git-switch-remote-repo origin project typescript | |
# | |
REPO_NAME=$1 | |
if [ -z "$REPO_NAME" ]; then | |
echo "ERROR: The name of remote repository must be specified." | |
exit | |
fi; | |
apply_repo() { | |
verify_repo $3 | |
git remote remove $1 | |
git remote add $1 $3 | |
if git fetch --all; then | |
for i in $(git branch | cut -c 3-); | |
do | |
if ! git branch -u $REPO_NAME/$i $i; then | |
echo "Failed to setup upstream for branchs" | |
break; | |
fi | |
done | |
else | |
echo "Failed to setup upstream for branchs" | |
fi | |
echo "Updated repository from ‘$2 to '$3'." | |
} | |
verify_repo() { | |
THE_USER=$(echo $1 | grep -Po '\w+(?=@)') | |
THE_DOMAIN=$(echo $1 | grep -Po '(?<=@).+(?=:)') | |
THE_SCOPE=$(echo $1 | grep -Po '(?<=:).+(?=/)') | |
THE_PROJECT=$(echo $1 | grep -Po '(?<=/).+(?=\.git)') | |
if [ -z "$THE_USER" -o -z "$THE_DOMAIN" -o -z "$THE_SCOPE" -o -z "$THE_PROJECT" ]; then | |
echo "ERROR: The repo '$1' is illegal." | |
exit | |
fi | |
} | |
case "$2" in | |
"user") | |
NEW_USER=$3 | |
;; | |
"domain") | |
NEW_DOMAIN=$3 | |
;; | |
"scope") | |
NEW_SCOPE=$3 | |
;; | |
"project") | |
NEW_PROJECT=$3 | |
;; | |
"full") | |
NEW_FULL=$3 | |
;; | |
*) | |
echo "ERROR: Unknown action '$2'." | |
exit | |
;; | |
esac | |
if [ -z "$3" ]; then | |
echo "ERROR: An argument is required." | |
exit | |
fi; | |
CUR_REPO=$(git remote -v | grep -P "^$REPO_NAME\t" | grep push | grep -Po '\w+@.+?\.git') | |
if [ -z "$CUR_REPO" ]; then | |
if [ -z "$NEW_FULL" ]; then | |
echo "ERROR: No such remote repository, please input a full link." | |
exit; | |
fi | |
apply_repo $REPO_NAME $CUR_REPO $NEW_FULL | |
exit; | |
fi | |
if [ ! -z "$NEW_FULL" ]; then | |
apply_repo $REPO_NAME $CUR_REPO $NEW_FULL | |
exit; | |
fi | |
verify_repo $CUR_REPO | |
CUR_USER=$(echo $CUR_REPO | grep -Po '\w+(?=@)') | |
CUR_DOMAIN=$(echo $CUR_REPO | grep -Po '(?<=@).+(?=:)') | |
CUR_SCOPE=$(echo $CUR_REPO | grep -Po '(?<=:).+(?=/)') | |
CUR_PROJECT=$(echo $CUR_REPO | grep -Po '(?<=/).+(?=\.git)') | |
if [ ! -z "$NEW_USER" ]; then | |
CUR_USER=$NEW_USER | |
fi | |
if [ ! -z "$NEW_DOMAIN" ]; then | |
CUR_DOMAIN=$NEW_DOMAIN | |
fi | |
if [ ! -z "$NEW_SCOPE" ]; then | |
CUR_SCOPE=$NEW_SCOPE | |
fi | |
if [ ! -z "$NEW_PROJECT" ]; then | |
CUR_PROJECT=$NEW_PROJECT | |
fi | |
NEW_REPO="${CUR_USER}@${CUR_DOMAIN}:${CUR_SCOPE}/${CUR_PROJECT}.git" | |
if [ "$NEW_REPO" != "$CUR_REPO" ]; then | |
apply_repo $REPO_NAME $CUR_REPO $NEW_REPO | |
else | |
echo "Nothing changed" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment