Skip to content

Instantly share code, notes, and snippets.

@alexanderjeurissen
Last active June 19, 2020 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderjeurissen/21458bf1180dc1c09925d5d468f7d9bf to your computer and use it in GitHub Desktop.
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
#!/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 git@github.com:$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