Last active
May 27, 2017 20:42
-
-
Save therealkevinard/ce2d13c07adb48076bf216e2e5b5be83 to your computer and use it in GitHub Desktop.
I changed my github username. This sh helped find all my local workspace repos and fix the remote url (and https auth, if necessary)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# MIT License: *especially* the warranty\liability limitations. | |
# Please be responsible when using bash to recursively alter git repos. | |
# | |
# usage ./updaterepos-find.sh | |
# where to start find. | |
base="/home/me/workspaces" | |
# github username mapping | |
olduser=oldusername_here | |
newuser=newusername_here | |
# auth mapping - if you use https remotes (https://user:[email protected]/user/repo/repo.git) and need to normalize the pass part. | |
oldauth=oldauthstring_here | |
newauth=newauthstring_here | |
for local in `find ${base} -maxdepth 5 -type d -iname ".git"`; do | |
cd "${local}/.." | |
echo "[x] In: $(pwd)" | |
remote=$(git remote -v) | |
echo "${remote}" | |
if [ "${remote}" = "" ]; then | |
echo "[-]no remote" | |
else | |
IFS=' ' read -r -a repoparts <<< ${remote} | |
reponame=${repoparts[0]} | |
repourl=${repoparts[1]} | |
if [[ ${repourl} == *"${newuser}"* ]]; then | |
echo "checking repo parts..." | |
printf " %s\n" "${repoparts[@]}" | |
echo "" | |
#-- rewrite | |
newurl=$(sed -e "s|${olduser}|${newuser}|g" <<< ${repourl}) | |
newurl=$(sed -e "s|${oldauth}|${newauth}|g" <<< ${newurl}) | |
cmd="git remote set-url ${reponame} ${newurl}" | |
#-- execute | |
printf "[-]Preparing to:\n${cmd}\n" | |
eval ${cmd} | |
else | |
echo 'not your repo' | |
fi | |
echo "[O]Done" | |
fi | |
echo "====================" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And of course there's a bug :)
if [[ ${repourl} == *"${newuser}"* ]]; then
should beif [[ ${repourl} == *"${olduser}"* ]]; then