Skip to content

Instantly share code, notes, and snippets.

@eyarz
Last active December 15, 2021 17:57
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyarz/64770d343b9cab442b257869f497af9e to your computer and use it in GitHub Desktop.
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/
#!/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)
@eyarz
Copy link
Author

eyarz commented Mar 25, 2019

If you need more RegEx's, I have in my blog post the following best practices:

  • Have a commit message
  • Keep a short subject line
  • Don’t end the subject line with a period
  • Start with a capital letter
  • Link to a planning system – Jira
  • Angular Git commit message guidelines
  • Emoji-Log commit message convention

@tiendq
Copy link

tiendq commented Aug 10, 2019

Is it pre-commit or commit-msg hook?

@eyarz
Copy link
Author

eyarz commented Aug 11, 2019

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