Created
December 4, 2021 08:21
-
-
Save attakei/3e7cc3278e8baeddf1993330d1641093 to your computer and use it in GitHub Desktop.
Git commit-msg hook for Conventional Commits
This file contains 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 | |
import sys | |
from pathlib import Path | |
VALID_TYPES = [ | |
# Rule 1 and 2. | |
"feat", "fix", | |
# Rule 4.(Optional) | |
# "build", "chore", "ci", "docs", "style", "refactor", "perf", "test", | |
] | |
def validate_commit_message(src_path: Path) -> bool: | |
msg = src_path.read_text() | |
rule = re.compile(r"(?P<type>[a-z]+)(\((?P<scope>\w+)\))?!?: (?P<desc>[^\r\n].+)(\n\n(<?P<body>.+))?") | |
matched = rule.match(msg) | |
if not matched: | |
print("Invalid format for 'Conventional Commits'.") | |
print("Please see https://www.conventionalcommits.org/en/v1.0.0/") | |
return False | |
if matched.group("type") not in VALID_TYPES: | |
print(f"Invalid type section. select one from [{', '.join(VALID_TYPES)}]") | |
return False | |
return True | |
if __name__ == "__main__": | |
msg_file_path = Path(sys.argv[1]) | |
result = validate_commit_message(msg_file_path) | |
sys.exit(0 if result else 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment