Skip to content

Instantly share code, notes, and snippets.

@elenzil
Created February 16, 2017 00:31
Show Gist options
  • Save elenzil/8ab0b2fcfbf2b04de551a7cb44af16b8 to your computer and use it in GitHub Desktop.
Save elenzil/8ab0b2fcfbf2b04de551a7cb44af16b8 to your computer and use it in GitHub Desktop.
osx bash script to convert all repositories to use ssh urls. useful when moving to ssh access from http, which is good for two-factor authentication.
#!/bin/bash
# this is provided as-is, with no guarantees that it will work or not explode your computer.
# written for OSX.
#
# orion elenzil 2017
#
# Motivation:
# If you are moving to two-factor authentication,
# you may also want to move from http-access to ssh-access.
# You know you're using http-access if you use URLs like "https://github.com/foo/bar" when cloning.
# But if you're moving to ssh-access, you'll need to update the URLs of all your repositories.
# In my case i had about 100 git repositories cloned, so wrote a script.
# a few constants
SERVICE_SPEC="[email protected]:"
LOCATION_OF_ALL_REPOSITORY_PARENT_FOLDERS="$HOME/git"
echo "this script will change all repositories to use SSH urls."
echo "it assumes that all your repositories are located as $LOCATION_OF_ALL_REPOSITORY_PARENT_FOLDERS/<github user>/<repository> ,"
echo "and that all folders of that pattern are git repositories. (altho it's benign if some aren't)"
read -p "do you want to continue ? [y/N] " prompt
if [[ $prompt != "y" && $prompt != "Y" && $prompt != "yes" && $prompt != "Yes" ]]
then
exit 0
fi
WD1=`pwd`
pushd "$LOCATION_OF_ALL_REPOSITORY_PARENT_FOLDERS"
WD2=`pwd`
if [ "$WD1" != "$WD2" ]
then
echo "pwd is not as expected. expected: \"$WD2\" actual:\"$WD1\"."
exit 1
fi
for USERX in `find . -maxdepth 1 -type d \( ! -name . \)`
do
USER=`echo "$USERX" | sed -e 's/^\.\///'`
pushd "$USER" > /dev/null
for REPOX in `find . -maxdepth 1 -type d \( ! -name . \)`
do
REPO=`echo "$REPOX" | sed -e 's/^\.\///'`
pushd "$REPO" > /dev/null
REPO_SPEC="$SERVICE_SPEC$USER/$REPO.git"
echo "setting ssh-url: $REPO_SPEC"
git remote set-url origin "$REPO_SPEC"
popd > /dev/null
done
popd > /dev/null
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment