diff --git a/.githooks/commit-msg/commit-msg b/.githooks/commit-msg/commit-msg new file mode 100755 index 00000000..0e6d69b4 --- /dev/null +++ b/.githooks/commit-msg/commit-msg @@ -0,0 +1,52 @@ +#!/bin/sh +# +# Add a specific emoji to the end of the first line in every commit message +# based on the conventional commits keyword. + +if [ ! -f "$1" ] || grep -q "fixup!" "$1"; then + # Exit if we didn't get a target file for some reason + # or we have a fixup commit + exit 0 +fi + +KEYWORD=$(head -n 1 "$1" | awk '{print $1}' | sed -e 's/://') + +case $KEYWORD in + "feat"|"feat("*) + EMOJI=":sparkles:" + ;; + "fix"|"fix("*) + EMOJI=":bug:" + ;; + "docs"|"docs("*) + EMOJI=":books:" + ;; + "style"|"style("*) + EMOJI=":gem:" + ;; + "refactor"|"refactor("*) + EMOJI=":hammer:" + ;; + "perf"|"perf("*) + EMOJI=":rocket:" + ;; + "test"|"test("*) + EMOJI=":rotating_light:" + ;; + "build"|"build("*) + EMOJI=":package:" + ;; + "ci"|"ci("*) + EMOJI=":construction_worker:" + ;; + "chore"|"chore("*) + EMOJI=":wrench:" + ;; + *) + EMOJI="" + ;; +esac + +MESSAGE=$(sed -E "1s/(.*)/\\1 $EMOJI/" <"$1") + +echo "$MESSAGE" >"$1" diff --git a/.githooks/pre-commit/pre-commit b/.githooks/pre-commit/pre-commit new file mode 100755 index 00000000..e4e8040d --- /dev/null +++ b/.githooks/pre-commit/pre-commit @@ -0,0 +1,51 @@ +#!/bin/bash + +source "$PWD"/common/shell/colors.sh +source "$PWD"/common/shell/ascii.sh + +branch=$(git rev-parse --abbrev-ref HEAD) + +if [[ $branch == "main" || $branch == "develop" || $branch == release/* ]]; then + echo "${bold}You can't commit directly to protected branches" + exit 1 +fi + +commit_msg_type_regex='feature|fix|refactor|style|test|docs|build' +commit_msg_scope_regex='.{1,20}' +commit_msg_description_regex='.{1,100}' +commit_msg_regex="^(${commit_msg_type_regex})(\(${commit_msg_scope_regex}\))?: (${commit_msg_description_regex})\$" +merge_msg_regex="^Merge branch '.+'\$" + +zero_commit="0000000000000000000000000000000000000000" + +# Do not traverse over commits that are already in the repository +excludeExisting="--not --all" + +error="" +while read oldrev newrev refname; do + # branch or tag get deleted + if [ "$newrev" = "$zero_commit" ]; then + continue + fi + + # Check for new branch or tag + if [ "$oldrev" = "$zero_commit" ]; then + rev_span=$(git rev-list $newrev $excludeExisting) + else + rev_span=$(git rev-list $oldrev..$newrev $excludeExisting) + fi + + for commit in $rev_span; do + commit_msg_header=$(git show -s --format=%s $commit) + if ! [[ "$commit_msg_header" =~ (${commit_msg_regex})|(${merge_msg_regex}) ]]; then + echo "$commit" >&2 + echo "ERROR: Invalid commit message format" >&2 + echo "$commit_msg_header" >&2 + error="true" + fi + done +done + +if [ -n "$error" ]; then + exit 1 +fi \ No newline at end of file diff --git a/.githooks/pre-push/pre-push b/.githooks/pre-push/pre-push new file mode 100755 index 00000000..2d8a3708 --- /dev/null +++ b/.githooks/pre-push/pre-push @@ -0,0 +1,17 @@ +#!/bin/bash + +source "$PWD"/common/shell/colors.sh +source "$PWD"/common/shell/ascii.sh + +while read local_ref local_sha remote_ref remote_sha; do + if [[ "$local_ref" =~ ^refs/heads/ ]]; then + branch_name=$(echo "$local_ref" | sed 's|^refs/heads/||') + + if [[ ! "$branch_name" =~ ^(feature|fix|hotfix|docs|refactor|build|test)/.*$ ]]; then + echo "${bold}Branch names must start with 'feature/', 'fix/', 'refactor/', 'docs/', 'test/' or 'hotfix/' followed by either a task id or feature name." + exit 1 + fi + fi +done + +exit 0 diff --git a/.githooks/pre-receive/pre-receive b/.githooks/pre-receive/pre-receive new file mode 100644 index 00000000..6e1aa30d --- /dev/null +++ b/.githooks/pre-receive/pre-receive @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +zero_commit="0000000000000000000000000000000000000000" + +while read oldrev newrev refname; do + + if [[ $oldrev == $zero_commit ]]; then + continue + fi + + if [[ $refname == "refs/heads/main" && $newrev != $zero_commit ]]; then + branch_name=$(basename $refname) + + if [[ $branch_name == release/* ]]; then + continue + else + echo "Error: You can only merge branches that start with 'release/' into the main branch." + exit 1 + fi + fi +done \ No newline at end of file