|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Git COMMIT-MSG hook for validating commit message |
| 5 | + * See https://docs.google.com/document/d/1rk04jEuGfk9kYzfqCuOlPTSJw3hEDZJTBN5E5f1SALo/edit |
| 6 | + * |
| 7 | + * Installation: |
| 8 | + * >> cd <angular-repo> |
| 9 | + * >> ln -s validate-commit-msg.js .git/hooks/commit-msg |
| 10 | + */ |
| 11 | +var fs = require('fs'); |
| 12 | +var util = require('util'); |
| 13 | + |
| 14 | + |
| 15 | +var MAX_LENGTH = 70; |
| 16 | +var PATTERN = /^(?:fixup!\s*)?(\w*)(\((\w+)\))?\: (.*)$/; |
| 17 | +var IGNORED = /^WIP\:/; |
| 18 | +var TYPES = { |
| 19 | + feat: true, |
| 20 | + fix: true, |
| 21 | + docs: true, |
| 22 | + style: true, |
| 23 | + refactor: true, |
| 24 | + test: true, |
| 25 | + chore: true, |
| 26 | + revert: true |
| 27 | +}; |
| 28 | + |
| 29 | + |
| 30 | +var error = function() { |
| 31 | + // gitx does not display it |
| 32 | + // http://gitx.lighthouseapp.com/projects/17830/tickets/294-feature-display-hook-error-message-when-hook-fails |
| 33 | + // https://groups.google.com/group/gitx/browse_thread/thread/a03bcab60844b812 |
| 34 | + console.error('INVALID COMMIT MSG: ' + util.format.apply(null, arguments)); |
| 35 | +}; |
| 36 | + |
| 37 | + |
| 38 | +var validateMessage = function(message) { |
| 39 | + var isValid = true; |
| 40 | + |
| 41 | + if (IGNORED.test(message)) { |
| 42 | + console.log('Commit message validation ignored.'); |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + if (message.length > MAX_LENGTH) { |
| 47 | + error('is longer than %d characters !', MAX_LENGTH); |
| 48 | + isValid = false; |
| 49 | + } |
| 50 | + |
| 51 | + var match = PATTERN.exec(message); |
| 52 | + |
| 53 | + if (!match) { |
| 54 | + error('does not match "<type>(<scope>): <subject>" ! was: "' + message + '"\nNote: <scope> must be only letters.'); |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + var type = match[1]; |
| 59 | + var scope = match[3]; |
| 60 | + var subject = match[4]; |
| 61 | + |
| 62 | + if (!TYPES.hasOwnProperty(type)) { |
| 63 | + error('"%s" is not allowed type !', type); |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + // Some more ideas, do want anything like this ? |
| 68 | + // - allow only specific scopes (eg. fix(docs) should not be allowed ? |
| 69 | + // - auto correct the type to lower case ? |
| 70 | + // - auto correct first letter of the subject to lower case ? |
| 71 | + // - auto add empty line after subject ? |
| 72 | + // - auto remove empty () ? |
| 73 | + // - auto correct typos in type ? |
| 74 | + // - store incorrect messages, so that we can learn |
| 75 | + |
| 76 | + return isValid; |
| 77 | +}; |
| 78 | + |
| 79 | + |
| 80 | +var firstLineFromBuffer = function(buffer) { |
| 81 | + return buffer.toString().split('\n').shift(); |
| 82 | +}; |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +// publish for testing |
| 87 | +exports.validateMessage = validateMessage; |
| 88 | + |
| 89 | +// hacky start if not run by jasmine :-D |
| 90 | +if (process.argv.join('').indexOf('jasmine-node') === -1) { |
| 91 | + var commitMsgFile = process.argv[2]; |
| 92 | + var incorrectLogFile = commitMsgFile.replace('COMMIT_EDITMSG', 'logs/incorrect-commit-msgs'); |
| 93 | + |
| 94 | + fs.readFile(commitMsgFile, function(err, buffer) { |
| 95 | + var msg = firstLineFromBuffer(buffer); |
| 96 | + |
| 97 | + if (!validateMessage(msg)) { |
| 98 | + fs.appendFile(incorrectLogFile, msg + '\n', function() { |
| 99 | + process.exit(1); |
| 100 | + }); |
| 101 | + } else { |
| 102 | + process.exit(0); |
| 103 | + } |
| 104 | + }); |
| 105 | +} |
0 commit comments