Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 110 lines (84 sloc) 2.675 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 */
36831ec Michał Gołębiowski refactor(jshint): reduce duplication & test all JS files
mzgol authored
11
12 'use strict';
13
5dbd942 Vojta Jina chore(scripts): add commit-msg hook (validation)
vojtajina authored
14 var fs = require('fs');
15 var util = require('util');
16
17
1ae34aa Igor Minar chore(validate-commit-msg.js): increase the max line limit for commit me...
IgorMinar authored
18 var MAX_LENGTH = 100;
36831ec Michał Gołębiowski refactor(jshint): reduce duplication & test all JS files
mzgol authored
19 var PATTERN = /^(?:fixup!\s*)?(\w*)(\(([\w\$\.\*/-]*)\))?\: (.*)$/;
5dbd942 Vojta Jina chore(scripts): add commit-msg hook (validation)
vojtajina authored
20 var IGNORED = /^WIP\:/;
21 var TYPES = {
22 feat: true,
23 fix: true,
24 docs: true,
25 style: true,
26 refactor: true,
a14266e Matias Niemelä chore(CHANGELOG): introduce perf() section for performance-related commi...
matsko authored
27 perf: true,
5dbd942 Vojta Jina chore(scripts): add commit-msg hook (validation)
vojtajina authored
28 test: true,
cfe13b5 Igor Minar chore(validate-commit-msg): recognize 'revert' as valid commit type
IgorMinar authored
29 chore: true,
30 revert: true
5dbd942 Vojta Jina chore(scripts): add commit-msg hook (validation)
vojtajina authored
31 };
32
33
34 var error = function() {
35 // gitx does not display it
36 // http://gitx.lighthouseapp.com/projects/17830/tickets/294-feature-display-hook-error-message-when-hook-fails
37 // https://groups.google.com/group/gitx/browse_thread/thread/a03bcab60844b812
38 console.error('INVALID COMMIT MSG: ' + util.format.apply(null, arguments));
39 };
40
41
42 var validateMessage = function(message) {
43 var isValid = true;
44
45 if (IGNORED.test(message)) {
46 console.log('Commit message validation ignored.');
47 return true;
48 }
49
50 if (message.length > MAX_LENGTH) {
51 error('is longer than %d characters !', MAX_LENGTH);
52 isValid = false;
53 }
54
55 var match = PATTERN.exec(message);
56
57 if (!match) {
6131521 Miško Hevery fix(git-validator): support fixup and better errors
mhevery authored
58 error('does not match "<type>(<scope>): <subject>" ! was: ' + message);
5dbd942 Vojta Jina chore(scripts): add commit-msg hook (validation)
vojtajina authored
59 return false;
60 }
61
62 var type = match[1];
63 var scope = match[3];
64 var subject = match[4];
65
66 if (!TYPES.hasOwnProperty(type)) {
67 error('"%s" is not allowed type !', type);
68 return false;
69 }
70
71 // Some more ideas, do want anything like this ?
72 // - allow only specific scopes (eg. fix(docs) should not be allowed ?
73 // - auto correct the type to lower case ?
74 // - auto correct first letter of the subject to lower case ?
75 // - auto add empty line after subject ?
76 // - auto remove empty () ?
77 // - auto correct typos in type ?
78 // - store incorrect messages, so that we can learn
79
80 return isValid;
81 };
82
83
84 var firstLineFromBuffer = function(buffer) {
85 return buffer.toString().split('\n').shift();
86 };
87
88
89
90 // publish for testing
91 exports.validateMessage = validateMessage;
92
93 // hacky start if not run by jasmine :-D
94 if (process.argv.join('').indexOf('jasmine-node') === -1) {
95 var commitMsgFile = process.argv[2];
96 var incorrectLogFile = commitMsgFile.replace('COMMIT_EDITMSG', 'logs/incorrect-commit-msgs');
97
98 fs.readFile(commitMsgFile, function(err, buffer) {
99 var msg = firstLineFromBuffer(buffer);
100
101 if (!validateMessage(msg)) {
102 fs.appendFile(incorrectLogFile, msg + '\n', function() {
103 process.exit(1);
104 });
105 } else {
106 process.exit(0);
107 }
108 });
109 }
Something went wrong with that request. Please try again.