Last active
April 17, 2023 11:37
-
-
Save nikhilw/fc2920011395f640a6876b105c656334 to your computer and use it in GitHub Desktop.
git commit hook using node to ensure commit message follows a convention
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 node | |
// NOTES: | |
// 1. This is one of the three implementation, if you do not have 'node' installed, there is a 'python' and a 'scala' counterpart. | |
// 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. | |
var fs = require("fs"); | |
console.log("INFO: Validating git commit message......."); | |
var msg = fs.readFileSync(process.argv[2] || process.env.GIT_PARAMS || process.env.HUSKY_GIT_PARAMS, "utf8").replace(/\n/g, " ").substr(0, 50); | |
var matchTest = /^(([A-Z]+-?\d+|Adhoc)\: (.+: )?(Added|Removed|BugFix|Modified|Feature|Merged|Refactored|Release): (.+){15,})$/g.test(msg); | |
var exitCode = matchTest?0:1; | |
if (exitCode === 0) { | |
console.log("SUCCESS: Commit ACCEPTED."); | |
} else { | |
console.log("ERROR: Commit REJECTED. REASON: Your commit message: '" + msg + "' does not follow the commit message convention."); | |
console.log("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.]"); | |
console.log("Sample: IT-123: Added: git hook to validate commit message."); | |
} | |
process.exit(exitCode); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment