From 61d000b51d45dd66c7e0d4b40ac7446e211f196c Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Tue, 30 Apr 2024 06:01:16 +0100 Subject: [PATCH] Add bumpver pre-commit hook (#688) This bumpver is configured to automatically run the bumpver_pre_commit.sh script before it makes the release commit. The script checks for common mistakes, such as making a release commit in a wrong branch, or trying to push to a wrong remote. --- bumpver_pre_commit.sh | 59 +++++++++++++++++++++++++++++++++++++++++++ setup.cfg | 1 + 2 files changed, 60 insertions(+) create mode 100755 bumpver_pre_commit.sh diff --git a/bumpver_pre_commit.sh b/bumpver_pre_commit.sh new file mode 100755 index 000000000..57986077f --- /dev/null +++ b/bumpver_pre_commit.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +# This script is configured to run automatically by bumpver +# before it creates the release commit. +# We check for common mistakes, such as making a release commit +# in a wrong branch, or trying to push to a wrong remote. +# +# For now, only two checks are implemented: +# +# 1. Check that the current branch matches either release/* or support/* +# +# 2. Check that the remote 'origin' is pointing to the origin repository, +# and not a fork. Note however that this assumes that origin is the default remote +# where new branches are pushed. If the user configured a different default remote, +# this check will not save them in the current implementation. +# +# Future work: +# - make sure the main branch is up-to-date with origin/main +# - make sure the HEAD commit was branched off of main branch, +# although this rule should only apply to release/* branches, not support/* branches +# +# Ideally, some of these check would be handled by bumpver itself: +# Restricting releases from branch: https://github.com/mbarkhau/bumpver/issues/198 +# Restricting releases to specified remote: https://github.com/mbarkhau/bumpver/issues/234 + +set -euo pipefail + +ORIGIN="github\.com[:/]aiidalab/aiidalab-qe" + +error=0 + +branch=$(git branch --show-current) + +# Explicitly disallow master/main branch +if [[ $branch = "master" || $branch = "main" ]];then + echo "ERROR: You should not run bumpver from main/master branch!" + echo "Make sure your main branch is up-to-date with origin ('git pull origin main')" + echo "and create a release branch first, e.g. 'git switch -c release/v2.0.0'" + error=1 +fi + +# Only allow release/* and support/* branches +if [[ ! $branch =~ 'release/' && ! $branch =~ 'support/' ]];then + echo "ERROR: The branch name must be either release/ or support/" + error=1 +fi + +# TODO: We need to check which remote is actually configured for push! +origin_url=$(git remote get-url --push --all origin) +if [[ ! $origin_url =~ $ORIGIN ]];then + echo "ERROR: Wrong default repo remote set!" + echo "got: $origin_url" + echo "expected: $ORIGIN" + error=1 +fi + +if [[ $error != 0 ]];then + exit 1 +fi diff --git a/setup.cfg b/setup.cfg index 2a758cc19..8cec53fdd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -72,6 +72,7 @@ commit_message = "Bump version {old_version} -> {new_version}" commit = True tag = True push = True +pre_commit_hook = ./bumpver_pre_commit.sh [bumpver:file_patterns] src/aiidalab_qe/version.py =