#!/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)