Skip to content

Instantly share code, notes, and snippets.

@phooky
Last active December 16, 2020 16:54
Show Gist options
  • Save phooky/8b473f30dad4b8ee160b6e2a07db6e78 to your computer and use it in GitHub Desktop.
Save phooky/8b473f30dad4b8ee160b6e2a07db6e78 to your computer and use it in GitHub Desktop.
A quick bash script for changing the "master" branch of your github repo(s) to an alternative of your choosing.
#!/bin/bash
# There's a better way to do this! Use:
# https://github.com/dfm/rename-github-default-branch
# instead.
#
# Also: don't change the default branch name on your gists! Github appears to have them
# locked to master; it will break your gist.
set -e
usage(){
echo "Usage: $0 [--user USERNAME] [--owner OWNER] [--token TOKEN] NEW_DEFAULT [PATH ...]"
echo "Replace the 'master' branch of a github project with NEW_DEFAULT."
echo "Suggested replacements: 'main', 'trunk', 'root', 'default', 'canon'"
echo "If no path or paths to repositories are provided, operates on the"
echo "current working directory."
echo ""
echo " -u, --user The github user (defaults to current username)"
echo " -o, --owner The github project owner (defaults to user)"
echo " -t, --token The github API token (defaults to pw auth)"
echo " --no-delete Do not delete the 'master' branch from origin"
echo ""
echo "If no token is provided, (deprecated) password authentication will be used."
exit 1
}
DEFAULT=""
OWNER=$USER
REPO_PATHS=()
while [ $# -gt 0 ]; do
case "$1" in
-t|--token)
shift
if [ -n "$1" ] && [ ${1:0:1} != "-" ]; then
TOKEN="$1"
shift
else
echo "Error: no token provided" >&2
usage
fi
;;
--no-delete)
shift
NO_DELETE=1
;;
-u|--user)
shift
if [ -n "$1" ] && [ ${1:0:1} != "-" ]; then
USER="$1"
shift
else
echo "Error: no user provided" >&2
usage
fi
;;
-o|--owner)
shift
if [ -n "$1" ] && [ ${1:0:1} != "-" ]; then
OWNER="$1"
shift
else
echo "Error: no owner provided" >&2
usage
fi
;;
-*|--*=)
echo "Error: unknown flag $1" >&2
usage
;;
*) # preserve positional arguments
if [ -z "${DEFAULT}" ]; then
DEFAULT="$1"
else
REPO_PATHS+=("$1")
fi
shift
;;
esac
done
if [ -z "${REPO_PATHS[*]}" ]; then
REPO_PATHS="."
fi
#echo "user ${USER} owner ${OWNER} default ${DEFAULT} paths ${REPO_PATHS[*]}"
if [ -z "${DEFAULT}" ]; then
echo "Error: new default branch name not provided!" >&2
usage
fi
if [ -n "${TOKEN}" ]; then
USER=${USER}:${TOKEN}
fi
change_current_master() {
git checkout master
git branch -m ${DEFAULT}
git push origin -u ${DEFAULT}
curl -i -u ${USER} --header "Content-Type: application/json" --request PATCH --data "{\"name\":\"${REPO}\", \"default_branch\": \"${DEFAULT}\" }" https://api.github.com/repos/${OWNER}/${REPO}
if [ -z "${NO_DELETE}" ]; then
git push origin --delete master
fi
}
for REPO_PATH in "${REPO_PATHS[@]}"; do
pushd ${REPO_PATH} >/dev/null
TL=`git rev-parse --show-toplevel`
REPO=`basename ${TL}`
echo "Transitioning ${REPO} from master -> ${DEFAULT} (in `pwd`)"
change_current_master
popd >/dev/null
done
@phooky
Copy link
Author

phooky commented Jun 12, 2020

TODOs:

  • use a token instead of deprecated password challenge
  • ignore repos that already have a non-master default
  • add option to scan all of a user's repos and do bulk updates
  • document/usage examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment