2016-06-16 14:56:39 -05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
|
|
|
# ghtp (GitHub Transport Protocol)
|
|
|
|
#
|
|
|
|
# Set all remotes in the current repo to HTTPS or SSH connection.
|
|
|
|
# Useful when switching environments, using public wifi, etc.
|
|
|
|
#
|
|
|
|
|
|
|
|
GH_SSH="git@github\.com:"
|
|
|
|
GH_HTTPS="https:\/\/github\.com\/"
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
-[Hh]) TYPE=HTTPS ; MATCH="git@" ; FORMULA="$GH_SSH/$GH_HTTPS" ;;
|
|
|
|
-[Ss]) TYPE=SSH ; MATCH="https:" ; FORMULA="$GH_HTTPS/$GH_SSH" ;;
|
|
|
|
*)
|
2020-08-10 01:44:11 -05:00
|
|
|
echo "Usage: `basename $0` -h | -s" 1>&2
|
2016-06-16 14:56:39 -05:00
|
|
|
echo -e " \e[0;92m-h\e[0m to switch to HTTPS" 1>&2
|
|
|
|
echo -e " \e[0;92m-s\e[0m to switch to SSH" 1>&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2020-08-10 01:44:11 -05:00
|
|
|
which awk >/dev/null && AWK=awk
|
|
|
|
which gawk >/dev/null && AWK=gawk
|
|
|
|
|
|
|
|
REMOTES=$(git remote -v | egrep "\t$MATCH" | "$AWK" '{print $1 " " $2}' | sort -u | sed "s/$FORMULA/")
|
2016-06-16 14:56:39 -05:00
|
|
|
|
|
|
|
if [[ -z $REMOTES ]]; then
|
|
|
|
echo "Nothing to do." ; exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$REMOTES" | xargs -n2 git remote set-url
|
|
|
|
|
|
|
|
echo -n "Remotes set to $TYPE: "
|
2020-08-10 01:44:11 -05:00
|
|
|
echo "$REMOTES" | "$AWK" '{printf "%s ", $1}'
|
2016-06-16 14:56:39 -05:00
|
|
|
echo
|