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