Skip to content

Commit

Permalink
Add hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
hwinther-tietoevry committed Oct 12, 2023
1 parent bfad98b commit 01c848a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.githooks/* eol=lf
3 changes: 3 additions & 0 deletions .gitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[core]
hooksPath = .githooks
autocrlf = true
50 changes: 50 additions & 0 deletions .githooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
#set -x
##########################################################
# Validates the commit message with the following rules: #
# 1. The message cannot be empty #
# 2. In dev branch, the format must follow the standard #
# 3. In main branch, the related JIRA issue needs to be #
# mentioned before the standard message format #
##########################################################

# shopt -s nocasematch # Case insensitive Cases
COMMIT_MSG_FILE=$1 # Recieve the commit message .git/COMMIT_EDITMSG

if [[ -z $(grep '[^[:space:]]' $COMMIT_MSG_FILE) ]] ; then
echo "Empty commit message is not allowed" >&2
exit 1
fi

DEV_BRANCH="dev"
MAIN_BRANCH="main"

current_branch="$(git rev-parse --abbrev-ref HEAD)"

case "$current_branch" in
$DEV_BRANCH)
error_msg="Aborting commit. Please use the following format: <Imperative verb> <short description of change>"
commit_regex_string="^(Add|Change|Remove|Fix|Update|Bump|Merge) [A-Za-z0-9 \/\'\,\._-]+$"
;;

$MAIN_BRANCH)
error_msg="Aborting commit. Please prefix JIRA issue (NLNN-XXXX) and a verb (Add|Change|Remove|Fix|Update|Bump|Merge) to the commit message."
commit_regex_string="^NLNN-[0-9]+ (Add|Change|Remove|Fix|Update|Bump|Merge) [A-Za-z0-9 \/\'\,\._-]+$"
;;

\
*)
error_msg="Aborting commit. Please prefix JIRA issue (NLNN-XXXX) and a verb (Add|Change|Remove|Fix|Update|Bump|Merge) to the commit message."
commit_regex_string="^NLNN-[0-9]+ (Add|Change|Remove|Fix|Update|Bump|Merge) [A-Za-z0-9 \/\'\,\._-]+$"
# echo "Unhandled branch value $current_branch"
# exit 1
;;
esac

COMMIT_MSG=$(cat ${COMMIT_MSG_FILE})
if [[ ! $COMMIT_MSG =~ $commit_regex_string ]]; then
echo "$error_msg"
exit 1
fi

exit 0
21 changes: 21 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
#set -x
###########################################################
# Validates the branch name with the following rules: #
# 1. It starts with feature/NLNN-<4 digits>-<any message> #
# 2. Or bugfix/NLNN-<4 digits>-<any message> #
###########################################################

local_branch="$(git rev-parse --abbrev-ref HEAD)"

# valid_branch_regex="^(feature|bugfix|improvement|library|prerelease|release|hotfix)\/[a-z0-9._-]+$"
valid_branch_regex="^(feature|bugfix)\/NLNN-[0-9]+-[A-Za-z0-9-]+$"

message="There is something wrong with your branch name. Branch names in this project must adhere to this contract: $valid_branch_regex. Your commit will be rejected. You should rename your branch to a valid name and try again."

if [[ ! $local_branch =~ $valid_branch_regex ]]; then
echo "$message"
exit 1
fi

exit 0
14 changes: 14 additions & 0 deletions .githooks/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
#set -x
######################################################
# Prepends NLNN-<issue number> to the commit message #
######################################################

ISSUE_NUMBER_REGEX="feature\/NLNN-([0-9]+)"

if [[ $(git branch --show-current) =~ $ISSUE_NUMBER_REGEX ]]; then
issue_number=$(echo ${BASH_REMATCH[1]})
echo "NLNN-$issue_number $(cat $1)" > $1
fi

exit 0

0 comments on commit 01c848a

Please sign in to comment.