Skip to content

Commit

Permalink
Merge pull request #44 from LerianStudio/feature/implement-githooks
Browse files Browse the repository at this point in the history
insert githooks
  • Loading branch information
lfbarrile01 authored Nov 29, 2024
2 parents 207b5b4 + 5423c0c commit 5b57a4a
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .githooks/commit-msg/commit-msg
Original file line number Diff line number Diff line change
@@ -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"
51 changes: 51 additions & 0 deletions .githooks/pre-commit/pre-commit
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions .githooks/pre-push/pre-push
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions .githooks/pre-receive/pre-receive
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5b57a4a

Please sign in to comment.