ghtp 877 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env bash
  2. #
  3. # ghtp (GitHub Transport Protocol)
  4. #
  5. # Set all remotes in the current repo to HTTPS or SSH connection.
  6. # Useful when switching environments, using public wifi, etc.
  7. #
  8. GH_SSH="git@github\.com:"
  9. GH_HTTPS="https:\/\/github\.com\/"
  10. case "$1" in
  11. -[Hh]) TYPE=HTTPS ; MATCH="git@" ; FORMULA="$GH_SSH/$GH_HTTPS" ;;
  12. -[Ss]) TYPE=SSH ; MATCH="https:" ; FORMULA="$GH_HTTPS/$GH_SSH" ;;
  13. *)
  14. echo "usage: `basename $0` -h | -s" 1>&2
  15. echo -e " \e[0;92m-h\e[0m to switch to HTTPS" 1>&2
  16. echo -e " \e[0;92m-s\e[0m to switch to SSH" 1>&2
  17. exit 1
  18. ;;
  19. esac
  20. REMOTES=$(git remote -v | egrep "\t$MATCH" | gawk '{print $1 " " $2}' | sort -u | sed "s/$FORMULA/")
  21. if [[ -z $REMOTES ]]; then
  22. echo "Nothing to do." ; exit
  23. fi
  24. echo "$REMOTES" | xargs -n2 git remote set-url
  25. echo -n "Remotes set to $TYPE: "
  26. echo "$REMOTES" | gawk '{printf "%s ", $1}'
  27. echo