Last active
April 17, 2023 11:36
-
-
Save nikhilw/7492c3d0426543f983acedbf1fc9f600 to your computer and use it in GitHub Desktop.
git commit hook using node to ensure commit message follows a convention
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 python3 | |
# NOTES: | |
# 1. This is one of the three implementation, this one is in Python. if you do not have python installed, there are 'node' and 'scala' counterparts. | |
# 2. To use this file, place it in .git/hooks/ in your repository. | |
# 3. You can also use this script with the 'husky' npm module. | |
import sys | |
import os | |
import re | |
filename = sys.argv[1] or os.getenv('GIT_PARAMS') | |
with open(filename, 'r') as f: | |
msg = f.read() | |
msg = re.sub(r'\r\n|\r|\n', ' ', msg) | |
maxLen = 50 if len(msg) > 50 else len(msg) | |
msg = msg[0:maxLen] | |
matchTest = re.search('([A-Z]+-?\d+|Adhoc)\: (.+: )?(Added|Removed|BugFix|Modified|Feature|Merged|Refactored|Release): (.+){15,}$', msg) | |
exitCode = 0 if (not (matchTest is None)) else 1 | |
if (exitCode == 0): | |
print("SUCCESS: Commit ACCEPTED.") | |
else: | |
print("ERROR: Commit REJECTED. REASON: Your commit message: '" + msg + "' does not follow the commit message convention."); | |
print("Convention: '[JIRAID]: [Optional clientId]: [One Primary Change Type: Added|Removed|BugFix|Modified|Feature|Merged|Refactored|Release]: [Message describing the change. If absolutely necessary, you can add multiple Change types here.]"); | |
print("Sample: IT-123: Added: git hook to validate commit message."); | |
exit(exitCode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment