Last active
June 20, 2019 17:07
-
-
Save kevinhcross/f8d71d4a0ae1e7258025b58693d73348 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# Script to update git remotes after the github organization rename | |
# | |
# Usage: | |
# | |
# ./update-github-remotes.sh <old-name> <new-name> 2>&1 | tee remote-update.log | |
# | |
# The output should say "Script completed" if all updates successful. | |
set -e | |
function do_update { | |
git_remote_push_url="$1" | |
repo_name=$(expr "$git_remote_push_url" : '.*\/\(.*\)\.git$') | |
if [[ "$git_remote_push_url" == http* ]]; then | |
set -x | |
git remote set-url origin "https://github.com/$NEW_NAME/$repo_name.git" | |
set +x | |
else | |
set -x | |
git remote set-url origin "git@github.com:$NEW_NAME/$repo_name.git" | |
set +x | |
fi | |
} | |
OLD_NAME=$1 | |
NEW_NAME=$2 | |
start_dir=$(PWD) | |
#for git_dir in $(find . -name .git -type d); do | |
while IFS= read -r -d '' git_dir; do | |
echo "==========================================" | |
target_dir=$(dirname "$git_dir") | |
echo "target dir: $target_dir" | |
cd "$target_dir" | |
git_remote_push_url=$(git remote get-url --push origin || echo 'no remote') | |
if [[ "$git_remote_push_url" =~ github.com.*$OLD_NAME ]]; then | |
echo "ACTION: updating : $target_dir" | |
do_update "$git_remote_push_url" | |
else | |
echo "ACTION: not ours : $git_remote_push_url" | |
fi | |
cd "$start_dir" | |
done < <(find . -name .git -type d -print0) | |
echo "Script completed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment