Last active
December 16, 2015 08:29
-
-
Save ndhoule/5406700 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
#!/usr/bin/env bash | |
ORGNAME="hackreactor" | |
GIT_CREDENTIAL_HELPER=$(git config --global --get credential.helper) | |
REPOS=('layout' 'twittler' 'paybuddy' 'data-structures' 'n-queens' 'more-data-structures' 'javascript_koans' 'subclass-dance-party' 'underbar' 'mytunes' 'chat-server' 'chat-client' 'recursion' 'regexen' 'web-historian' 'blackjack' 'databases' 'more-databases' 'meteor') | |
github_username=0 | |
github_password=0 | |
spacedPrint() { | |
echo -e "\n****************\n****************\n$1\n****************\n****************" | |
} | |
get_credentials() { | |
read -p "Username: " github_username | |
read -s -p "Password: " github_password | |
echo "" | |
# Make sure they provided both username and password and do a test auth to | |
# confirm their credentials are good | |
if [[ github_username == 0 && github_password == 0 ]]; then | |
echo "Sorry, one of your credentials was left blank. Please enter them now." | |
get_credentials | |
fi | |
if [[ github_username != 0 && github_password != 0 ]]; then | |
if curl -ss -u "${github_username}:${github_password}" https://api.github.com/users/${github_username} | grep -q '"message": "Bad credentials"'; then | |
echo "Sorry, login to github failed. Please re-enter your credentials." | |
get_credentials | |
fi | |
fi | |
} | |
test_folder_presence() { | |
directory=$(pwd)/$(basename ${1}) | |
if [[ -d $directory ]]; then | |
spacedPrint "Warning!" | |
echo "A folder named $(basename ${1}) already exists. You may be trying to" | |
echo "clone to the directory where you keep all your repositories. You're" | |
echo "facing potential data loss!" | |
echo "" | |
echo "If you're sure you want to clone here, type 'y' and hit enter to delete" | |
echo "the folder and continue. Type 'n' to bail out:" | |
read input | |
if [[ "$input" = "y" ]]; then | |
rm -rf $directory | |
elif [[ "$input" = "n" ]]; then | |
echo "Okay :( Exiting." | |
exit 0 | |
else | |
echo "" | |
echo "Invalid response. Please try again." | |
test_folder_presence $1 | |
fi | |
fi | |
} | |
clear | |
echo "-~= OMG PUBLICITY =~-" | |
echo "Hello! I'm a script that will publicize all of your forked repositories." | |
echo "In order for this script to work, you'll need to have your GitHub credentials" | |
echo "stored in your keychain." | |
echo "" | |
echo "Press enter when you're ready to continue." | |
read trash | |
clear | |
echo "I'll need your GitHub credentials to continue. Please enter them now." | |
get_credentials | |
echo "" | |
echo "Thanks!" | |
echo "" | |
echo "One more thing..." | |
echo "This script will create new readmes for all your projects. The readme will" | |
echo "point to a project you'd like to show off, like your personal project." | |
echo "Please enter the name of that project:" | |
read projname | |
echo "Please enter the URL to that project's GitHub repository, or to where it's hosted:" | |
read projurl | |
echo "" | |
echo "Great, thanks! I'll publicize your repos for you now." | |
echo -e "\n" | |
# Set credential helper to something more long-term for the duration of the script | |
git config --global credential.helper 'cache --timeout=3600' | |
# Clone down ALL the repos | |
for repo_name in "${REPOS[@]}"; do | |
repo_url=https://github.com/${github_username}/${repo_name}.git | |
# Skip to the next repo if the user doesn't have this one | |
if curl -ss -u "${github_username}:${github_password}" https://api.github.com/repos/${github_username}/${repo_name} | grep -q '"message": "Not Found"'; then | |
echo -e "${repo_name} not found on your GitHub account. Moving on to the next repository...\n" | |
continue | |
fi | |
spacedPrint "About to clone down the ${repo_name}..." | |
# Clone down the repo | |
test_folder_presence ${repo_name} | |
git clone ${repo_url} | |
cd ${repo_name} | |
spacedPrint "Listing branches..." | |
git branch -a | sed "s/remotes\/origin\///g" | |
# List usernames if | |
if git branch -a | grep -q $github_username; then | |
spacedPrint "Listing branches with your username..." | |
git branch -a | sed "s/remotes\/origin\///g" | grep $github_username | |
fi | |
echo "Copy-paste the branch you'd like. To skip, leave this blank and hit enter." | |
read branchname | |
# If they leave the branch name blank, bail out of this iteration | |
if [[ $branchname = "" ]]; then | |
cd .. | |
rm -rf ${repo_name} | |
continue | |
fi | |
# If the user has selected a branch other than the master branch, then delete | |
# the master branch and set the master branch to point at $branchname | |
if [[ $branchname != 'master' ]]; then | |
git checkout -b dummy_branch | |
git branch -f master "origin/${branchname}" | |
git checkout master | |
fi | |
# Delete all branches except for the master branch, and set up the new | |
# destination (this will also remove all remote branch tracking) | |
git branch -a | git branch -a | grep -v -E "(\* master|remotes\/origin)" | xargs git branch -D | |
git remote rm origin | |
git remote add origin ${repo_url} | |
echo -e "#${repo_name}\n\nThis is a copy of the work I did on a private repo, originally a project from the [Hack Reactor](http://hackreactor.com) curriculum. This project was worked on with a pair, and as such is representative of the kind of problems that I've tackled, but not of my solo work. For a better perspective on my own work, please see [$projname]($projurl)." > README.md | |
git add "README.md" | |
git commit -m "Added readme" | |
# Rename the old repo | |
DATA="{\"name\":\"${repo_name}.old\"}" | |
echo "https://api.github.com/repos/${github_username}/${repo_name}" | |
curl -ss -u "${github_username}:${github_password}" -X PATCH --data ${DATA} https://api.github.com/repos/${github_username}/${repo_name} | |
# Create a new, empty remote repository | |
DATA="{\"name\":\"${repo_name}\"}" | |
curl -ss -u "${github_username}:${github_password}" --data ${DATA} https://api.github.com/user/repos | |
# Push up the fresh/clean repo | |
echo "Pushing up the ${repo_name} repository..." | |
git push origin master | |
cd .. | |
# Remove the temp files | |
echo "Removing the temporary directory..." | |
rm -rf ${repo_name} && echo "Done." | |
done | |
# Reset credential helper | |
git config --global credential.helper $GIT_CREDENTIAL_HELPER |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment