Last active
December 14, 2015 22:49
-
-
Save pbyrne/5161101 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 | |
main() { | |
CURRENT_ORG=$1 | |
NEW_ORG=$2 | |
STARTING_DIR=$(pwd) | |
# bomb out if not given current and new org names | |
[ -z $CURRENT_ORG ] || [ -z $NEW_ORG ] && missing_arguments | |
announce | |
process_repos | |
} | |
missing_arguments() { | |
echo Missing required argument | |
echo | |
usage | |
} | |
# Print usage instructions and exit | |
usage() { | |
echo rename-git-origin.sh CURRENT_ORG NEW_ORG | |
echo | |
echo Useful when moving your repositories to a new GitHub organization. | |
echo | |
echo ex: renamte-git-origin tstmedia sportngin | |
exit 1 | |
} | |
announce() { | |
echo Renaming the org in all repos in the $STARTING_DIR from $CURRENT_ORG to $NEW_ORG | |
echo | |
} | |
process_repos() { | |
echo Finding Git... | |
echo | |
for repo in $(repos_in_directory); do | |
rename_org_for $repo | |
echo | |
done | |
} | |
# Find all the .git directories within the current directory | |
repos_in_directory() { | |
find $STARTING_DIR -type d -name .git | |
} | |
# Rename the organization of the given repo | |
# | |
# repo_path - Path to a Git repository | |
rename_org_for() { | |
repo_path=$1 | |
echo Reviewing $repo_path | |
origin=$(current_origin $repo_path) | |
echo Currently pointing at $origin | |
if [[ $origin == *$CURRENT_ORG* ]]; then | |
new_origin=$(search_and_replace $origin $CURRENT_ORG $NEW_ORG) | |
echo Renaming to $new_origin | |
perform_rename $repo_path $new_origin | |
else | |
echo "Skipping, since it doesn't currently point to $CURRENT_ORG" | |
fi | |
} | |
# Return the current URL the Git repo has configured as origin | |
# | |
# repo_path - Path to a Git repository | |
current_origin() { | |
cd $repo_path | |
# find 'origin URL (fetch)' just get the URL out | |
git remote -v | grep origin | grep fetch | awk '{ print $2 }' | |
cd $STARTING_DIR | |
} | |
# Search and replace in the given string | |
# | |
# string - String to perform the search and replace against | |
# search_value - What to search for in the string | |
# replace_value - What to replace with | |
search_and_replace() { | |
string=$1 | |
search_value=$2 | |
replace_value=$3 | |
echo ${string/$search_value/$replace_value} | |
} | |
# Actually perform the rename of the given repo to the given origin | |
# | |
# repo_path - Path to a Git repository | |
# new_origin - The URL to set origin to use | |
perform_rename() { | |
repo_path=$1 | |
new_origin=$2 | |
cd $repo_path | |
git remote set-url origin $new_origin | |
cd $STARTING_DIR | |
} | |
main $* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment