Last active
December 15, 2021 17:57
-
-
Save eyarz/64770d343b9cab442b257869f497af9e to your computer and use it in GitHub Desktop.
Git commit-msg hook to verify commit message convention => https://datree.io/blog/git-commit-message-conventions-for-readable-git-log/
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
#!/usr/bin/env python | |
""" | |
Git commit hook: | |
.git/hooks/commit-msg | |
Check commit message according to guidelines | |
""" | |
import sys | |
import re | |
REGEX = '' | |
# e.g. ^(feat|fix|docs|style|refactor|test|build|ci|perf)(\(.+\))?\:\s(.{3,}) | |
GUIDELINE_LINK = '<link to relvent commit mesage guideline>' | |
# e.g. https://github.com/angular/angular/blob/master/CONTRIBUTING.md#commit | |
with open(sys.argv[1]) as commit: | |
lines = commit.readlines() | |
if len(lines) == 0: | |
sys.stderr.write("\nEmpty commit message\n") | |
sys.stderr.write("\n - Refer commit guide: {}\n\n".format(help_address)) | |
sys.exit(1) | |
match_regex = re.match('({})'.format(REGEX), lines[0]) | |
if match_regex is None: | |
sys.stderr.write("\nYour commit message subject line does not follow the guideline\n") | |
sys.stderr.write("\n - Refer commit guideline: {}\n\n".format(GUIDELINE_LINK)) | |
sys.exit(1) | |
sys.stderr.write("\nYour commit message looks good! \n\n") | |
sys.exit(0) |
Is it pre-commit
or commit-msg
hook?
You are right! I was using the wrong git hook name 😱
The right git hook is commit-msg
so I updated the text accordingly.
Thank you for noticing :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you need more RegEx's, I have in my blog post the following best practices: