Created
February 21, 2020 22:39
-
-
Save danpetitt/493ed43ce1f6e165fc3d3f972d826cbc to your computer and use it in GitHub Desktop.
Semantic Release Git Hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Config options | |
min_length=4 | |
max_length=50 | |
types=("feat" "fix" "perf") | |
# End config options | |
regexpstart="^(" | |
regexp="${regexpstart}" | |
for type in "${types[@]}" | |
do | |
if [ "$regexp" != "$regexpstart" ]; then | |
regexp="${regexp}|" | |
fi | |
regexp="${regexp}$type" | |
done | |
regexp="${regexp})(\(.+\))?: " | |
regexp="${regexp}.{$min_length,$max_length}$" | |
function print_error() { | |
echo -e "\n\e[1m\e[31m[INVALID COMMIT MESSAGE]" | |
echo -e "------------------------\033[0m\e[0m" | |
echo -e "\e[1mValid types:\e[0m \e[34m${types[@]}\033[0m" | |
echo -e "\e[1mMax length (first line):\e[0m \e[34m$max_length\033[0m" | |
echo -e "\e[1mMin length (first line):\e[0m \e[34m$min_length\033[0m\n" | |
} | |
# get the first line of the commit message | |
INPUT_FILE=$1 | |
START_LINE=`head -n1 $INPUT_FILE` | |
if [[ ! $START_LINE =~ $regexp ]]; then | |
# commit message is invalid according to semantic-release conventions | |
print_error | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can save this into your
.git/hooks/
folder for any repos which exist already; or you can run the following commands that will set things up for every new repo youinit
in the future: