Created
June 25, 2015 16:06
-
-
Save dignifiedquire/cb237d55a3d81bf95e92 to your computer and use it in GitHub Desktop.
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 node | |
/** | |
* Git COMMIT-MSG hook for validating commit message | |
* See https://docs.google.com/document/d/1rk04jEuGfk9kYzfqCuOlPTSJw3hEDZJTBN5E5f1SALo/edit | |
* | |
* Installation: | |
* >> cd <repo> | |
* >> ln -s ../../validate-commit-msg.js .git/hooks/commit-msg | |
*/ | |
var fs = require('fs') | |
var util = require('util') | |
var path = require('path') | |
var MAX_LENGTH = 100 | |
var PATTERN = /^(\w*)(\(([\w\$\.\-\*]*)\))?\: (.*)$/ | |
var IGNORED = /^(WIP\:|Merge pull request)/ | |
var TYPES = { | |
feat: true, | |
fix: true, | |
docs: true, | |
style: true, | |
refactor: true, | |
test: true, | |
chore: true | |
} | |
var error = function () { | |
// gitx does not display it | |
// https://groups.google.com/group/gitx/browse_thread/thread/a03bcab60844b812 | |
console.error('INVALID COMMIT MSG: ' + util.format.apply(null, arguments)) | |
} | |
var validateMessage = function (message) { | |
var isValid = true | |
if (IGNORED.test(message)) { | |
console.log('Commit message validation ignored.') | |
return true | |
} | |
if (message.length > MAX_LENGTH) { | |
error('is longer than %d characters !', MAX_LENGTH) | |
isValid = false | |
} | |
var match = PATTERN.exec(message) | |
if (!match) { | |
error('does not match "<type>(<scope>): <subject>" !') | |
return false | |
} | |
var type = match[1] | |
if (!TYPES.hasOwnProperty(type)) { | |
error('"%s" is not allowed type !\n Valid <type> values are: %s', type, | |
Object.keys(TYPES).join(', ')) | |
return false | |
} | |
// Some more ideas, do want anything like this ? | |
// - allow only specific scopes (eg. fix(docs) should not be allowed ? | |
// - auto correct the type to lower case ? | |
// - auto correct first letter of the subject to lower case ? | |
// - auto add empty line after subject ? | |
// - auto remove empty () ? | |
// - auto correct typos in type ? | |
// - store incorrect messages, so that we can learn | |
return isValid | |
} | |
var firstLineFromBuffer = function (buffer) { | |
return buffer.toString().split('\n').shift() | |
} | |
// publish for testing | |
exports.validateMessage = validateMessage | |
// lame test if run by git (so that it does not trigger during testing) | |
if (process.env.GIT_DIR) { | |
var commitMsgFile = process.argv[2] | |
var incorrectLogFile = path.dirname(commitMsgFile) + '/logs/incorrect-commit-msgs' | |
fs.readFile(commitMsgFile, function (err, buffer) { | |
var msg = firstLineFromBuffer(buffer) | |
if (!validateMessage(msg)) { | |
fs.appendFile(incorrectLogFile, msg + '\n', function () { | |
process.exit(1) | |
}) | |
} else { | |
process.exit(0) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment