Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 107 lines (83 sloc) 2.66 kb
5dbd942 Vojta Jina chore(scripts): add commit-msg hook (validation)
vojtajina authored
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>
3661bab Daniel Lamb revert(validate-commit-msg): fix incorrect comment
daniellmb authored
9 * >> ln -s ../../validate-commit-msg.js .git/hooks/commit-msg
5dbd942 Vojta Jina chore(scripts): add commit-msg hook (validation)
vojtajina authored
10 */
11 var fs = require('fs');
12 var util = require('util');
13
14
1ae34aa Igor Minar chore(validate-commit-msg.js): increase the max line limit for commit me...
IgorMinar authored
15 var MAX_LENGTH = 100;
6131521 Miško Hevery fix(git-validator): support fixup and better errors
mhevery authored
16 var PATTERN = /^(?:fixup!\s*)?(\w*)(\(([\w\$\.\-\*/]*)\))?\: (.*)$/;
5dbd942 Vojta Jina chore(scripts): add commit-msg hook (validation)
vojtajina authored
17 var IGNORED = /^WIP\:/;
18 var TYPES = {
19 feat: true,
20 fix: true,
21 docs: true,
22 style: true,
23 refactor: true,
a14266e Matias Niemelä chore(CHANGELOG): introduce perf() section for performance-related commi...
matsko authored
24 perf: true,
5dbd942 Vojta Jina chore(scripts): add commit-msg hook (validation)
vojtajina authored
25 test: true,
cfe13b5 Igor Minar chore(validate-commit-msg): recognize 'revert' as valid commit type
IgorMinar authored
26 chore: true,
27 revert: true
5dbd942 Vojta Jina chore(scripts): add commit-msg hook (validation)
vojtajina authored
28 };
29
30
31 var 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
39 var 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) {
6131521 Miško Hevery fix(git-validator): support fixup and better errors
mhevery authored
55 error('does not match "<type>(<scope>): <subject>" ! was: ' + message);
5dbd942 Vojta Jina chore(scripts): add commit-msg hook (validation)
vojtajina authored
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
81 var firstLineFromBuffer = function(buffer) {
82 return buffer.toString().split('\n').shift();
83 };
84
85
86
87 // publish for testing
88 exports.validateMessage = validateMessage;
89
90 // hacky start if not run by jasmine :-D
91 if (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 }
Something went wrong with that request. Please try again.