How To Enforce Conventional Commit Messages Using GIT Hooks

git commit -m "added social login feature for authentication using twitter"
git commit -m "feat(authentication): added social login using twitter"
> git log --oneline
61c8ca9 (HEAD -> master) fix: navbar not responsive on mobile
479c48b test: prepared test cases for user authentication
a992020 chore: moved to semantic versioning
b818120 fix: button click even handler firing twice
c6e9a97 fix: login page css
dfdc715 feat(authentication): added social login using twitter
Photo by Chris Ried on Unsplash
logs
hooks
objects
refs
git init .
#!/usr/bin/env python
import re, sys, os
def main():
# example:
# feat(apikey): added the ability to add api key to configuration
pattern = r'(build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert)(\([\w\-]+\))?:\s.*'
filename = sys.argv[1]
ss = open(filename, 'r').read()
m = re.match(pattern, ss)
if m == None: raise Exception("conventional commit validation failed")
if __name__ == "__main__":
main()
> git commit -m "added a new feature for xyz"
Traceback (most recent call last):
File "C:/Users/prahlad/Documents/scripts/check_commit.py", line 22, in <module>
main()
File "C:/Users/prahlad/Documents/scripts/check_commit.py", line 19, in main
if m == None: raise Exception("conventional commit validation failed")
Exception: conventional commit validation failed
Now test with a valid commit message and it should work!
pip install enforce-git-message

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store