Skip to content

Instantly share code, notes, and snippets.

@palozano
Last active December 13, 2022 12:12
Show Gist options
  • Save palozano/8d9960d89c095bc500e3c6041afa331f to your computer and use it in GitHub Desktop.
Save palozano/8d9960d89c095bc500e3c6041afa331f to your computer and use it in GitHub Desktop.
Git hook validator for convetional commits
#!/usr/bin/env python
import re, sys, os
sys.tracebacklimit = 0
FAIL_MESSAGE = """
Conventional Commit validation failed.
A commit message must be as follows:
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
where:
<type> must be: build, ci, chore, docs, feat, fix, perf, refactor, revert, style, test;
<description> must be in lower-case letters (e.g., re-write "Merge branch..." to "chore: merge branch...");
Please, rewrite your commit message, or use `git commit --no-verify` to bypass this hook.
More information: https://www.conventionalcommits.org
"""
def main():
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(FAIL_MESSAGE)
if __name__ == "__main__":
main()
@palozano
Copy link
Author

palozano commented Dec 13, 2022

Copy this file into .git/hooks and make it executable (chmod +x commit-msg).

It won't let you commit unless the commit message fullfils the Conventional Commmits convention.

Source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment