Skip to content

Instantly share code, notes, and snippets.

@GabrielMMelo
Created December 19, 2020 23:41
Show Gist options
  • Save GabrielMMelo/3a5cf8cac04935abcb7d3fa104c27ef8 to your computer and use it in GitHub Desktop.
Save GabrielMMelo/3a5cf8cac04935abcb7d3fa104c27ef8 to your computer and use it in GitHub Desktop.
Hook for validating a commit
#!/bin/sh
branch="$(git rev-parse --abbrev-ref HEAD)";
commit_regex='((CDW-)([1-9][0-9])|((feat|fix|chore|refactor|style|test|docs)(((\w{0,15})))?))(:.\S.*)';
error_msg="Aborting commit. Your commit message format is invalid, please check the references."
commit_message="$1";
if ! grep -iqE "$commit_regex" <<<"${commit_message}"; then
echo "$error_msg" >&2
exit 1
fi
if [ "$branch" = "master" ]; then
echo "You can't commit directly to master branch"
exit 1
fi
if [ "$branch" = "develop" ]; then
echo "You can't commit directly to develop branch"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment