File indexing completed on 2024-05-12 05:53:20

0001 #!/bin/bash
0002 # Add this to your konsole/.git/hooks/ folder to prevent push if message is not following commit-template
0003 # In your konsole folder type:
0004 # cp ./doc/developer/pre-push ./.git/hooks/pre-push && chmod +x ./.git/hooks/pre-push
0005 
0006 # Get the current branch and apply it to a variable
0007 currentbranch=`git branch | grep \* | cut -d ' ' -f2`
0008 
0009 # Gets the commits for the current branch and outputs to file
0010 git log $currentbranch --pretty=format:"%H" --not master > shafile.txt
0011 
0012 if [ ! -f "./shafile.txt" ]
0013 then
0014       echo "It is not possible to write temporary files"
0015       echo "Check Read/Write permissions or remove this"
0016       echo "pre-push check" 
0017       exit 1
0018 fi
0019 
0020 if [ ! -s "./shafile.txt" ]
0021 then
0022       # if size is 0 there is no commit
0023       rm shafile.txt  >/dev/null 2>&1
0024       exit 0
0025 fi
0026 
0027 echo '
0028 ====================
0029 Commit Message Check
0030 ====================
0031 '
0032 
0033 #colors used here
0034 RED='\033[0;31m'
0035 GREEN='\033[0;32m'
0036 NC='\033[0m'
0037 
0038 passedall=0
0039 # loops through the file an gets the messages
0040 for i in `cat ./shafile.txt`;
0041 do
0042       passed=0
0043       # gets the git commit message based on the sha
0044       git log --format=%B -n 1 "$i" > logfile.txt
0045       # load this message to an array
0046       mapfile -t gitmessage < ./logfile.txt
0047 
0048       # commit title
0049       echo "- ${gitmessage[0]}"
0050 
0051       # analise each line
0052       for (( nline=0; nline<${#gitmessage[@]}+1; nline++ ));
0053       do
0054             line=${gitmessage[$nline]}
0055             if [ ${#line} -gt 72 ]
0056             then
0057                   echo -e "     ${RED}*${NC} You MUST wrap all lines at 72 characters."
0058                   passed=1
0059             fi
0060 
0061             if [[ $nline -eq 1 && ${#line} -gt 0 ]]
0062             then
0063                   echo -e "     ${RED}*${NC} Follow the Subject with a blank line."
0064                   passed=1
0065             fi
0066 
0067             messagecheck=`echo $line | grep -w "BUG\|FEATURE\|FIXED-IN\|CCBUG\|CCMAIL\|GUI\|CHANGELOG"`
0068             if [ -n "$messagecheck" ]
0069             then
0070                   messagecheck=`echo $line | grep ': '`
0071                   if [ -z "$messagecheck" ]
0072                   then 
0073                         echo -e "     ${RED}*${NC} Your commit message has a formatting error."
0074                         echo "       Type ' ' after field specification"
0075                         echo "       -> $line"
0076                         passed=1
0077                   fi
0078             fi
0079       done
0080 
0081       # Check if all passed
0082       if [ $passed -gt 0 ]
0083       then
0084             echo -e "  Commit message ${RED}FAILED${NC}"
0085             passedall=1
0086       else
0087             echo -e "  Commit message ${GREEN}PASSED${NC}"
0088       fi
0089       echo ""
0090 done
0091 # remove temporary files
0092 rm shafile.txt  >/dev/null 2>&1
0093 rm logfile.txt  >/dev/null 2>&1
0094 # Test mode remove the next '#' to test it
0095 #exit 1 
0096 # If all passed in the check it will exit 0
0097 exit $passedall