1
0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2024-11-23 12:04:19 +00:00
MarlinFirmware/buildroot/share/git/ghtp

40 lines
1.1 KiB
Plaintext
Raw Normal View History

#!/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.
#
2020-08-21 21:22:53 +00:00
# Optionally, specify a particular remote to change.
#
GH_SSH="git@github\.com:"
GH_HTTPS="https:\/\/github\.com\/"
case "$1" in
2020-08-21 21:22:53 +00:00
-[Hh]) TYPE=HTTPS ; MATCH="git@" ; REPLACE="$GH_SSH/$GH_HTTPS" ;;
-[Ss]) TYPE=SSH ; MATCH="https:" ; REPLACE="$GH_HTTPS/$GH_SSH" ;;
*)
2020-08-10 06:44:11 +00:00
echo "Usage: `basename $0` -h | -s" 1>&2
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-11 01:03:13 +00:00
AWK=$(which gawk || which awk)
2020-08-10 06:44:11 +00:00
2020-08-21 21:22:53 +00:00
# Match all or specified remotes of the other type
REGEX="\t$MATCH" ; [[ $# > 1 ]] && REGEX="^$2$REGEX"
REMOTES=$(git remote -v | egrep "$REGEX" | "$AWK" '{print $1 " " $2}' | sort -u | sed "s/$REPLACE/")
2020-08-21 21:22:53 +00:00
[[ -z $REMOTES ]] && { echo "Nothing to do." ; exit ; }
2020-08-21 21:22:53 +00:00
# Update a remote for each results pair
echo "$REMOTES" | xargs -n2 git remote set-url
echo -n "Remotes set to $TYPE: "
2020-08-10 06:44:11 +00:00
echo "$REMOTES" | "$AWK" '{printf "%s ", $1}'
echo