Last active
December 13, 2022 12:12
-
-
Save palozano/8d9960d89c095bc500e3c6041afa331f to your computer and use it in GitHub Desktop.
Git hook validator for convetional commits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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