From 737a5453d9ec6ceb7b3b1e6767aa5cfb64cbc4b2 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 21 Aug 2020 16:22:53 -0500 Subject: [PATCH] Allow 'ghtp' to set a specific remote --- buildroot/share/git/ghtp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/buildroot/share/git/ghtp b/buildroot/share/git/ghtp index 3c0e21e371..14afd8a27b 100755 --- a/buildroot/share/git/ghtp +++ b/buildroot/share/git/ghtp @@ -5,13 +5,15 @@ # Set all remotes in the current repo to HTTPS or SSH connection. # Useful when switching environments, using public wifi, etc. # +# Optionally, specify a particular remote to change. +# 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" ;; + -[Hh]) TYPE=HTTPS ; MATCH="git@" ; REPLACE="$GH_SSH/$GH_HTTPS" ;; + -[Ss]) TYPE=SSH ; MATCH="https:" ; REPLACE="$GH_HTTPS/$GH_SSH" ;; *) echo "Usage: `basename $0` -h | -s" 1>&2 echo -e " \e[0;92m-h\e[0m to switch to HTTPS" 1>&2 @@ -22,12 +24,14 @@ esac AWK=$(which gawk || which awk) -REMOTES=$(git remote -v | egrep "\t$MATCH" | "$AWK" '{print $1 " " $2}' | sort -u | sed "s/$FORMULA/") +# 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/") -if [[ -z $REMOTES ]]; then - echo "Nothing to do." ; exit -fi +[[ -z $REMOTES ]] && { echo "Nothing to do." ; exit ; } +# Update a remote for each results pair echo "$REMOTES" | xargs -n2 git remote set-url echo -n "Remotes set to $TYPE: "