Pre commit hooks

This commit is contained in:
Igor 2025-03-20 11:53:56 +01:00
parent d693f9a425
commit 840f63d4c6
4 changed files with 83 additions and 0 deletions

View File

@ -1,4 +1,5 @@
exclude: '^docs/|/migrations/|devcontainer.json'
default_install_hook_types: [pre-commit, pre-push]
default_stages: [pre-commit]
minimum_pre_commit_version: "3.2.0"
@ -6,6 +7,24 @@ default_language_version:
python: python3.12
repos:
- repo: local
hooks:
- id: protect-branch-pre-commit
name: protect branch
stages: [pre-commit]
language: python
always_run: true
entry: python .scripts/protect_branch-pre-commit.py
- repo: local
hooks:
- id: protect-branch-pre-push
name: protect branch
stages: [pre-push]
language: python
always_run: true
entry: python .scripts/protect_branch-pre-push.py
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:

View File

@ -0,0 +1,16 @@
# Prevent commit to local main branch
import os
import subprocess
import sys
if __name__ == "__main__":
# Skip check in CI
if os.getenv("CI"):
sys.exit(0)
result = subprocess.run(["git", "symbolic-ref", "HEAD"], stdout=subprocess.PIPE, check=False)
branch = result.stdout.decode("utf-8").strip()
if branch == "refs/heads/main":
print("pre-commit hook: Can not commit to the local main branch.")
sys.exit(1)

View File

@ -0,0 +1,12 @@
# Prevent push to remote main branch
import os
import sys
if __name__ == "__main__":
# SKip check in CI
if os.getenv("CI"):
sys.exit(0)
if os.getenv("PRE_COMMIT_REMOTE_BRANCH") == "refs/heads/main":
print("pre-push hook: Can not push to remote main branch.")
sys.exit(1)

View File

@ -0,0 +1,36 @@
#!/bin/bash
CURRENT_VERSION="$1"
COMMIT_MESSAGE="$2"
echo "Using current version: ${CURRENT_VERSION}"
echo "Using commit message: ${COMMIT_MESSAGE}"
SEMVER_TYPES=([MINOR] [MAJOR] [PATCH])
COUNTER=0
BUMP_RULE=""
for semver in "${SEMVER_TYPES[@]}"; do
if [[ "${COMMIT_MESSAGE}" == *"${semver}"* ]]; then
if [ ${COUNTER} -gt 0 ] ; then
echo "[ERROR] Multiple semvers Found. You can only use one of the following semvers: ${SEMVER_TYPES[*]}"
exit 1
fi
BUMP_RULE=$(echo "${semver}" | tr '[:upper:]' '[:lower:]' | tr -d '[],')
COUNTER=$((COUNTER+1))
fi
done
if [ ${COUNTER} -eq 0 ]; then
echo "[WARNING] No semver found in your commit message. Defaulting to PATCH"
echo "No semver found in your commit message. Defaulting to PATCH" >> "$GITHUB_STEP_SUMMARY"
BUMP_RULE="patch"
fi
pip install semver
echo "Using bump rule: ${BUMP_RULE}"
BUMPED_VERSION=$(python -m semver bump "${BUMP_RULE}" "${CURRENT_VERSION}")
echo "Bump version number: ${BUMPED_VERSION}"
# Return the bumped version
echo "version=$BUMPED_VERSION" >> "$GITHUB_OUTPUT"