Skip to content

Instantly share code, notes, and snippets.

@eyarz eyarz/commit-msg-hook.py
Last active Aug 11, 2019

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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

This comment has been minimized.

Copy link

commented Aug 10, 2019

Is it pre-commit or commit-msg hook?

@eyarz

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.