From 0b309228d3fbabd6b70a817d45e899ec800cb7e8 Mon Sep 17 00:00:00 2001 From: Aaron Guise Date: Tue, 2 Apr 2024 11:51:42 +1300 Subject: [PATCH] fix(git): Added git filter for project Ensures angular style commit messages are applied. --- .githooks/commit-msg | 11 +++++ scripts/commit-filter-check.sh | 85 ++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100755 .githooks/commit-msg create mode 100755 scripts/commit-filter-check.sh diff --git a/.githooks/commit-msg b/.githooks/commit-msg new file mode 100755 index 0000000..bb2ada3 --- /dev/null +++ b/.githooks/commit-msg @@ -0,0 +1,11 @@ +#!/bin/sh + +# Run the script and get the return code if successful of if fails +./scripts/commit-filter-check.sh && rc=$? || rc=$? +echo return code : $rc +if [ ${rc} == 1 ]; then + echo "Script return code 1 so commit failed" + exit 1 +else + echo "No error returned so commit successful" +fi diff --git a/scripts/commit-filter-check.sh b/scripts/commit-filter-check.sh new file mode 100755 index 0000000..2d9e3b8 --- /dev/null +++ b/scripts/commit-filter-check.sh @@ -0,0 +1,85 @@ +#!/bin/bash + +commit_message_check (){ + # Get the current branch and apply it to a variable + currentbranch=`git branch | grep \* | cut -d ' ' -f2` + + # Gets the commits for the current branch and outputs to file + git log $currentbranch --pretty=format:"%H" --not master > shafile.txt + + # loops through the file an gets the message + for i in `cat ./shafile.txt`; + do + # gets the git commit message based on the sha + gitmessage=`git log --format=%B -n 1 "$i"` + + ####################### TEST STRINGS comment out line 13 to use ######################################### + #gitmessage="feat sdasdsadsaas (AEROGEAR-asdsada)" + #gitmessage="feat(some txt): some txt (AEROGEAR-****)" + #gitmessage="docs(some txt): some txt (AEROGEAR-1234)" + #gitmessage="fix(some txt): some txt (AEROGEAR-5678)" + ######################################################################################################### + + # Checks gitmessage for string feat, fix, docs and breaking, if the messagecheck var is empty if fails + messagecheck=`echo $gitmessage | grep -w "chore\|feat\|fix\|docs\|breaking"` + if [ -z "$messagecheck" ] + then + echo "Your commit message must begin with one of the following" + echo " feat(feature-name)" + echo " fix(fix-name)" + echo " docs(docs-change)" + echo " " + fi + if [ ${PerformProjectCheck} == "true" ]; then + #check the gitmessage for the Jira number + messagecheck=`echo $gitmessage | grep "(${ProjectID}-"` + if [ -z "$messagecheck" ] + then + echo "Your commit message must end with the following" + echo " (${ProjectID}-****)" + echo "Where **** is the Jira number" + echo " " + fi + fi + messagecheck=`echo $gitmessage | grep ": "` + if [ -z "$messagecheck" ] + then + echo "Your commit message has a formatting error please take note of special characters '():' position and use in the example below" + echo " type(some txt): some txt (${ProjectID}-****)" + echo "Where 'type' is fix, feat, docs or breaking and **** is the Jira number" + echo " " + fi + + if [ ${PerformProjectCheck} == "true" ]; then + # All checks run at the same time by pipeing from one grep to another + messagecheck=`echo $gitmessage | grep -w "chore\|feat\|fix\|docs\|breaking" | grep "(${ProjectID}-" | grep ": "` + else + # All checks run at the same time by pipeing from one grep to another + messagecheck=`echo $gitmessage | grep -w "chore\|feat\|fix\|docs\|breaking" | grep ": "` + fi + + + + # check to see if the messagecheck var is empty + if [ -z "$messagecheck" ] + then + echo "The commit message with sha: '$i' failed " + echo "Please review the following :" + echo " " + echo $gitmessage + echo " " + rm shafile.txt >/dev/null 2>&1 + set -o errexit + else + echo "$messagecheck" + echo "'$i' commit message passed" + fi + done + rm shafile.txt >/dev/null 2>&1 +} + + +ProjectID="NA" # Set to your Jira Project ID if want to track references to tickets. +PerformProjectCheck="false" # Set true if ProjectID is set and you want to ensure Jira ref is included on commit. +# Calling the function +commit_message_check \ No newline at end of file