python-zulip-api/tools/deploy-branch
Anders Kaseorg 53e59c8c09 Rename default branch to ‘main’
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-08-24 12:50:52 -07:00

44 lines
1,008 B
Bash
Executable file

#!/bin/bash
function error_out {
echo -en '\e[0;31m'
echo "$1"
echo -en '\e[0m'
exit 1
}
status=$(git status --porcelain | grep -v '^??')
[ ! -z "$status" ] && error_out "Working directory or index not clean"
old_ref=$(git rev-list --max-count=1 HEAD)
branch=$1
branch_ref=$(git rev-list --max-count=1 "$branch")
[ $? -ne 0 ] && error_out "Unknown branch: $branch"
if [ "$old_ref" == "$branch_ref" ]; then
new_ref=main
else
ref_name=$(git describe --all --exact "$old_ref")
if [ $? -eq 0 ]; then
new_ref=$(echo "$ref_name" | perl -pe 's{^(heads|remotes)/}{}')
else
new_ref=$old_ref
fi
fi
[ -z "$branch" ] && error_out "You must specify a branch name to deploy"
git fetch -p
git rebase origin/main "$branch"
[ $? -ne 0 ] && error_out "Rebase onto origin/main failed"
git push . HEAD:main
git push origin main
[ $? -ne 0 ] && error_out "Push of main to origin/main failed"
git checkout "$new_ref"
git branch -D "$branch"
git push origin ":$branch"