Skip to content

Instantly share code, notes, and snippets.

@danpetitt
Created February 21, 2020 22:39
Show Gist options
  • Save danpetitt/493ed43ce1f6e165fc3d3f972d826cbc to your computer and use it in GitHub Desktop.
Save danpetitt/493ed43ce1f6e165fc3d3f972d826cbc to your computer and use it in GitHub Desktop.
Semantic Release Git Hook
#!/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
@danpetitt
Copy link
Author

danpetitt commented Feb 21, 2020

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 you init in the future:

git config --global init.templatedir '~/.git-templates'
mkdir -p ~/.git-templates/hooks
cp commit-msg ~/.git-templates/hooks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment