Skip to content

Instantly share code, notes, and snippets.

@lalitkale
Created December 29, 2019 22:32
Show Gist options
  • Save lalitkale/9ba63ad7b0cd3fbea218c4fabfcbd6bd to your computer and use it in GitHub Desktop.
Save lalitkale/9ba63ad7b0cd3fbea218c4fabfcbd6bd to your computer and use it in GitHub Desktop.
How To Setup GitHooks
Git Hooks Template: ~/.git-templates
To create git-hook, create following python file to invoke githooks. Below is the script
commit-msg.py
---
#!/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()
---
If you do not want to do it manually, it can be done by following single line of script, which installs the script into git templates segreation.
pip install enforce-git-message
Reference:
https://dev.to/prahladyeri/how-to-enforce-conventional-commit-messages-using-git-hooks-2bmk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment