Skip to content

Instantly share code, notes, and snippets.

@Nikoloutsos
Created March 22, 2022 23:53
Show Gist options
  • Select an option

  • Save Nikoloutsos/ca78836e174882514bbb70877805d6a6 to your computer and use it in GitHub Desktop.

Select an option

Save Nikoloutsos/ca78836e174882514bbb70877805d6a6 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Used for adding some colors in the console. ๐ŸŒˆ
# https://medium.com/@f3igao/get-started-with-git-hooks-5a489725c639
REDBOLD='\033[0;31;1m'
GREENBOLD='\033[0;32;1m'
NORMAL='\033[0m'
# The below input_file is file ".git/COMMIT_EDITMSG" where commits are stored
INPUT_FILE=$1
# It will copy the commit string from ".git/COMMIT_EDITMSG"
COMMIT_MESSAGE=`head -n1 $INPUT_FILE`
# Verify message starts with a correct VERB
VERB_REGEX="^(Merge|Add|Drop|Fix|Bump|Make|Start|Stop|Optimize|Document|Refactor|Reformat|Rearrange|Redraw|Reword|Scaffold|fixup!) .+$"
if [[ $COMMIT_MESSAGE =~ $VERB_REGEX ]]; then
echo "โœ… Detected valid commit message verb"
else
echo "๐Ÿ‘ฎโ€โ™€๏ธ ${REDBOLD}Hold on! Git hook police here. ${NORMAL}"
echo "Your commit message doesn't start with a valid verb (e.g Add/Refactor/Reward/Document) "
echo "'$COMMIT_MESSAGE' is not adhering to iOS Blueground git rules. ( https://github.com/bluegroundltd/manifesto/blob/master/make/Git_Practices.md#summary-keywords )"
exit 1
fi
# Verify message contains <= 50 chars
LENGTH_REGEX="^.{1,50}$"
if [[ $COMMIT_MESSAGE =~ $LENGTH_REGEX ]]; then
echo "โœ… Commit message is <= 50 chars"
else
echo "๐Ÿ‘ฎโ€โ™€๏ธ ${REDBOLD}Hold on! Git hook police here. ${NORMAL}"
echo "Your commit message exceeds the limit of 50 chars "
echo "'$COMMIT_MESSAGE' is not adhering to iOS Blueground git rules. ( https://github.com/bluegroundltd/manifesto/blob/master/make/Git_Practices.md#summary-keywords )"
exit 1
fi
# All checks passed!
echo "${GREENBOLD}All commit-msg hook checks passed. ๐ŸŽ‰${NORMAL}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment