Last active
June 19, 2020 23:46
-
-
Save alexanderjeurissen/21458bf1180dc1c09925d5d468f7d9bf to your computer and use it in GitHub Desktop.
Rename github master branch to main, change default github branch automatically using `hub. invoke as: rename_master_to_main username/repo
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 zsh | |
set -e | |
function echoStep { | |
export __TOTAL_START="${__TOTAL_START:-$(date +'%s')}" | |
__STEP=$1 | |
__STEP_START=$(date +'%s') | |
printf $'\e[37;44m\033[1m'"$1" | |
echo $'\033[0m' | |
} | |
function echoDone { | |
__STEP_DONE=$(date +'%s') | |
__STEP_TIME=$(($__STEP_DONE - $__STEP_START)) | |
__TOTAL_TIME=$(($__STEP_DONE - $__TOTAL_START)) | |
printf $'\e[42m\033[30m'"$__STEP - DONE in ($__STEP_TIME s) of total ($__TOTAL_TIME s)" | |
echo $'\033[0m\n' | |
} | |
function echoWarning { | |
printf $'\e[7;49;31m\033[1m'"$1" | |
echo $'\033[0m' | |
} | |
function ensureBrew { | |
echoStep "Ensuring $1 brew is installed and up to date..." | |
# Brew's exit code is not consistent when we need to upgrade, so | |
# we need to check the output. | |
set +e | |
OUTPUT=$(brew install $1 2>&1) | |
set -e | |
if [[ $OUTPUT =~ To\ upgrade ]]; then | |
brew upgrade $1 | |
fi | |
} | |
echoStep "installing dependencies" | |
ensureBrew jq | |
ensureBrew hub | |
echoDone | |
echo "" | |
read -s -k '?Press any key to continue.' | |
echo "" | |
echo "" | |
echoStep "RENAME local master -> main" | |
master_branch=$(git branch -l master) | |
if [[ -z "${master_branch// }" ]]; then | |
echo "- master branch present ? [NO]" | |
# echoWarning "Master branch not present in this repository, exiting.. " | |
# exit 1 | |
else | |
echo "- master branch present ? [YES]" | |
fi | |
main_branch=$(git branch -l main) | |
if [[ -z "${main_branch// }" ]]; then | |
echo "- main branch present ? [NO]" | |
git branch -m master main | |
else | |
echo "- main branch present ? [YES]" | |
fi | |
echoDone | |
echo "" | |
read -s -k '?Press any key to continue.' | |
echo "" | |
echo "" | |
echoStep "COPY remote:master -> remote:main" | |
git push origin main -u | |
echoDone | |
echo "" | |
read -s -k '?Press any key to continue.' | |
echo "" | |
echo "" | |
echoStep "CHANGE default branch -> main" | |
main_default_branch=$(hub api repos/$1 | jq '.default_branch == "main"') | |
if [[ "$main_default_branch" == "true" ]]; then | |
echo "- current default_branch = main? [YES]" | |
else | |
echo "- default_branch is set to main? [NO]" | |
hub api repos/$1 -X PATCH -F default_branch=main &> /dev/null | |
new_default_branch=$(hub api repos/$1 | jq '.default_branch == "main"') | |
if [[ "$new_default_branch" == "true" ]]; then | |
echo "- default_branch changed to main? [YES]" | |
else | |
echo "- default_branch changed to main? [NO]" | |
echoWarning "Something went wrong changing the default branch, please complete this step manually" | |
read -s -k '?Press any key to continue .' | |
fi | |
fi | |
echoStep "DELETE remote master" | |
remote_master_branch=$(git ls-remote --heads [email protected]:$1.git master) | |
if [[ -z "${remote_master_branch// }" ]]; then | |
echo "- remote master branch present ? [NO]" | |
else | |
echo "- remote master branch present ? [YES]" | |
git push origin --delete master | |
fi | |
echoDone |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment