Skip to content

Instantly share code, notes, and snippets.

Last active Dec 15, 2021
What would you like to do?
Git commit-msg hook to verify commit message convention =>
#!/usr/bin/env python
Git commit hook:
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.
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))
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.stderr.write("\nYour commit message looks good! \n\n")
Copy link

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

Copy link

tiendq commented Aug 10, 2019

Is it pre-commit or commit-msg hook?

Copy link

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